1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <canvas/debug.hxx>
23 #include <tools/diagnose_ex.h>
25 #include "viewbackgroundshape.hxx"
28 #include <rtl/math.hxx>
30 #include <comphelper/anytostring.hxx>
31 #include <cppuhelper/exc_hlp.hxx>
33 #include <basegfx/polygon/b2dpolygontools.hxx>
34 #include <basegfx/polygon/b2dpolygon.hxx>
35 #include <basegfx/numeric/ftools.hxx>
36 #include <basegfx/matrix/b2dhommatrix.hxx>
37 #include <basegfx/matrix/b2dhommatrixtools.hxx>
39 #include <com/sun/star/rendering/XCanvas.hpp>
41 #include <canvas/verbosetrace.hxx>
42 #include <canvas/canvastools.hxx>
43 #include <cppcanvas/vclfactory.hxx>
44 #include <cppcanvas/basegfxfactory.hxx>
45 #include <cppcanvas/renderer.hxx>
46 #include <cppcanvas/bitmap.hxx>
48 using namespace ::com::sun::star
;
56 bool ViewBackgroundShape::prefetch( const ::cppcanvas::CanvasSharedPtr
& rDestinationCanvas
,
57 const GDIMetaFileSharedPtr
& rMtf
) const
59 SAL_INFO( "slideshow", "::presentation::internal::ViewBackgroundShape::prefetch()" );
60 ENSURE_OR_RETURN_FALSE( rMtf
,
61 "ViewBackgroundShape::prefetch(): no valid metafile!" );
63 const ::basegfx::B2DHomMatrix
& rCanvasTransform(
64 mpViewLayer
->getTransformation() );
68 rCanvasTransform
!= maLastTransformation
)
70 // buffered bitmap is invalid, re-create
72 // determine transformed page bounds
73 ::basegfx::B2DRectangle aTmpRect
;
74 ::canvas::tools::calcTransformedRectBounds( aTmpRect
,
78 // determine pixel size of bitmap (choose it one pixel
79 // larger, as polygon rendering takes one pixel more
80 // to the right and to the bottom)
81 const ::basegfx::B2ISize
aBmpSizePixel(
82 ::basegfx::fround( aTmpRect
.getRange().getX() + 1),
83 ::basegfx::fround( aTmpRect
.getRange().getY() + 1) );
85 // create a bitmap of appropriate size
86 ::cppcanvas::BitmapSharedPtr
pBitmap(
87 ::cppcanvas::BaseGfxFactory::createBitmap(
91 ENSURE_OR_THROW( pBitmap
,
92 "ViewBackgroundShape::prefetch(): Cannot create background bitmap" );
94 ::cppcanvas::BitmapCanvasSharedPtr
pBitmapCanvas( pBitmap
->getBitmapCanvas() );
96 ENSURE_OR_THROW( pBitmapCanvas
,
97 "ViewBackgroundShape::prefetch(): Cannot create background bitmap canvas" );
100 initSlideBackground( pBitmapCanvas
,
103 // apply linear part of destination canvas transformation (linear means in this context:
104 // transformation without any translational components)
105 ::basegfx::B2DHomMatrix
aLinearTransform( rCanvasTransform
);
106 aLinearTransform
.set( 0, 2, 0.0 );
107 aLinearTransform
.set( 1, 2, 0.0 );
108 pBitmapCanvas
->setTransformation( aLinearTransform
);
110 const basegfx::B2DHomMatrix
aShapeTransform(basegfx::tools::createScaleTranslateB2DHomMatrix(
111 maBounds
.getWidth(), maBounds
.getHeight(),
112 maBounds
.getMinX(), maBounds
.getMinY()));
114 ::cppcanvas::RendererSharedPtr
pRenderer(
115 ::cppcanvas::VCLFactory::createRenderer(
118 ::cppcanvas::Renderer::Parameters() ) );
120 ENSURE_OR_RETURN_FALSE( pRenderer
,
121 "ViewBackgroundShape::prefetch(): Could not create Renderer" );
123 pRenderer
->setTransformation( aShapeTransform
);
126 mxBitmap
= pBitmap
->getUNOBitmap();
130 maLastTransformation
= rCanvasTransform
;
132 return mxBitmap
.is();
135 ViewBackgroundShape::ViewBackgroundShape( const ViewLayerSharedPtr
& rViewLayer
,
136 const ::basegfx::B2DRectangle
& rShapeBounds
) :
137 mpViewLayer( rViewLayer
),
140 maLastTransformation(),
141 maBounds( rShapeBounds
)
143 ENSURE_OR_THROW( mpViewLayer
, "ViewBackgroundShape::ViewBackgroundShape(): Invalid View" );
144 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "ViewBackgroundShape::ViewBackgroundShape(): Invalid ViewLayer canvas" );
147 ViewLayerSharedPtr
ViewBackgroundShape::getViewLayer() const
152 bool ViewBackgroundShape::render( const GDIMetaFileSharedPtr
& rMtf
) const
154 SAL_INFO( "slideshow", "::presentation::internal::ViewBackgroundShape::draw()" );
156 const ::cppcanvas::CanvasSharedPtr
& rDestinationCanvas( mpViewLayer
->getCanvas() );
158 if( !prefetch( rDestinationCanvas
, rMtf
) )
161 ENSURE_OR_RETURN_FALSE( mxBitmap
.is(),
162 "ViewBackgroundShape::draw(): Invalid background bitmap" );
164 ::basegfx::B2DHomMatrix
aTransform( mpViewLayer
->getTransformation() );
166 // invert the linear part of the view transformation
167 // (i.e. the view transformation without translational
168 // components), to be able to leave the canvas
169 // transformation intact (would otherwise destroy possible
170 // clippings, as the clip polygon is relative to the view
171 // coordinate system).
172 aTransform
.set(0,2, 0.0 );
173 aTransform
.set(1,2, 0.0 );
176 rendering::RenderState aRenderState
;
177 ::canvas::tools::initRenderState( aRenderState
);
179 ::canvas::tools::setRenderStateTransform( aRenderState
, aTransform
);
183 rDestinationCanvas
->getUNOCanvas()->drawBitmap( mxBitmap
,
184 rDestinationCanvas
->getViewState(),
187 catch( uno::Exception
& )
189 OSL_FAIL( OUStringToOString(
190 comphelper::anyToString( cppu::getCaughtException() ),
191 RTL_TEXTENCODING_UTF8
).getStr() );
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */