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 <comphelper/diagnose_ex.hxx>
23 #include <sal/log.hxx>
25 #include "viewbackgroundshape.hxx"
28 #include <basegfx/numeric/ftools.hxx>
29 #include <basegfx/matrix/b2dhommatrix.hxx>
30 #include <basegfx/matrix/b2dhommatrixtools.hxx>
32 #include <com/sun/star/rendering/XCanvas.hpp>
34 #include <canvas/canvastools.hxx>
35 #include <cppcanvas/vclfactory.hxx>
36 #include <cppcanvas/basegfxfactory.hxx>
37 #include <cppcanvas/renderer.hxx>
38 #include <cppcanvas/bitmap.hxx>
41 using namespace ::com::sun::star
;
44 namespace slideshow::internal
47 bool ViewBackgroundShape::prefetch( const ::cppcanvas::CanvasSharedPtr
& rDestinationCanvas
,
48 const GDIMetaFileSharedPtr
& rMtf
) const
50 SAL_INFO( "slideshow", "::presentation::internal::ViewBackgroundShape::prefetch()" );
51 ENSURE_OR_RETURN_FALSE( rMtf
,
52 "ViewBackgroundShape::prefetch(): no valid metafile!" );
54 const ::basegfx::B2DHomMatrix
aCanvasTransform(
55 mpViewLayer
->getTransformation() );
59 aCanvasTransform
!= maLastTransformation
)
61 // buffered bitmap is invalid, re-create
63 // determine transformed page bounds
64 ::basegfx::B2DRectangle aTmpRect
= ::canvas::tools::calcTransformedRectBounds(
68 // determine pixel size of bitmap (choose it one pixel
69 // larger, as polygon rendering takes one pixel more
70 // to the right and to the bottom)
71 const ::basegfx::B2ISize
aBmpSizePixel(
72 ::basegfx::fround( aTmpRect
.getRange().getX() + 1),
73 ::basegfx::fround( aTmpRect
.getRange().getY() + 1) );
75 // create a bitmap of appropriate size
76 ::cppcanvas::BitmapSharedPtr
pBitmap(
77 ::cppcanvas::BaseGfxFactory::createBitmap(
81 ENSURE_OR_THROW( pBitmap
,
82 "ViewBackgroundShape::prefetch(): Cannot create background bitmap" );
84 ::cppcanvas::BitmapCanvasSharedPtr
pBitmapCanvas( pBitmap
->getBitmapCanvas() );
86 ENSURE_OR_THROW( pBitmapCanvas
,
87 "ViewBackgroundShape::prefetch(): Cannot create background bitmap canvas" );
90 initSlideBackground( pBitmapCanvas
,
93 // apply linear part of destination canvas transformation (linear means in this context:
94 // transformation without any translational components)
95 ::basegfx::B2DHomMatrix
aLinearTransform( aCanvasTransform
);
96 aLinearTransform
.set( 0, 2, 0.0 );
97 aLinearTransform
.set( 1, 2, 0.0 );
98 pBitmapCanvas
->setTransformation( aLinearTransform
);
100 const basegfx::B2DHomMatrix
aShapeTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
101 maBounds
.getWidth(), maBounds
.getHeight(),
102 maBounds
.getMinX(), maBounds
.getMinY()));
104 ::cppcanvas::RendererSharedPtr
pRenderer(
105 ::cppcanvas::VCLFactory::createRenderer(
108 ::cppcanvas::Renderer::Parameters() ) );
110 ENSURE_OR_RETURN_FALSE( pRenderer
,
111 "ViewBackgroundShape::prefetch(): Could not create Renderer" );
113 pRenderer
->setTransformation( aShapeTransform
);
116 mxBitmap
= pBitmap
->getUNOBitmap();
120 maLastTransformation
= aCanvasTransform
;
122 return mxBitmap
.is();
125 ViewBackgroundShape::ViewBackgroundShape( ViewLayerSharedPtr xViewLayer
,
126 const ::basegfx::B2DRectangle
& rShapeBounds
) :
127 mpViewLayer(std::move( xViewLayer
)),
130 maLastTransformation(),
131 maBounds( rShapeBounds
)
133 ENSURE_OR_THROW( mpViewLayer
, "ViewBackgroundShape::ViewBackgroundShape(): Invalid View" );
134 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "ViewBackgroundShape::ViewBackgroundShape(): Invalid ViewLayer canvas" );
137 const ViewLayerSharedPtr
& ViewBackgroundShape::getViewLayer() const
142 bool ViewBackgroundShape::render( const GDIMetaFileSharedPtr
& rMtf
) const
144 SAL_INFO( "slideshow", "::presentation::internal::ViewBackgroundShape::draw()" );
146 const ::cppcanvas::CanvasSharedPtr
xDestinationCanvas( mpViewLayer
->getCanvas() );
148 if( !prefetch( xDestinationCanvas
, rMtf
) )
151 ENSURE_OR_RETURN_FALSE( mxBitmap
.is(),
152 "ViewBackgroundShape::draw(): Invalid background bitmap" );
154 ::basegfx::B2DHomMatrix
aTransform( mpViewLayer
->getTransformation() );
156 // invert the linear part of the view transformation
157 // (i.e. the view transformation without translational
158 // components), to be able to leave the canvas
159 // transformation intact (would otherwise destroy possible
160 // clippings, as the clip polygon is relative to the view
161 // coordinate system).
162 aTransform
.set(0,2, 0.0 );
163 aTransform
.set(1,2, 0.0 );
166 rendering::RenderState aRenderState
;
167 ::canvas::tools::initRenderState( aRenderState
);
169 ::canvas::tools::setRenderStateTransform( aRenderState
, aTransform
);
173 xDestinationCanvas
->getUNOCanvas()->drawBitmap( mxBitmap
,
174 xDestinationCanvas
->getViewState(),
177 catch( uno::Exception
& )
179 TOOLS_WARN_EXCEPTION( "slideshow", "" );
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */