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
& rCanvasTransform(
55 mpViewLayer
->getTransformation() );
59 rCanvasTransform
!= maLastTransformation
)
61 // buffered bitmap is invalid, re-create
63 // determine transformed page bounds
64 ::basegfx::B2DRectangle aTmpRect
;
65 ::canvas::tools::calcTransformedRectBounds( aTmpRect
,
69 // determine pixel size of bitmap (choose it one pixel
70 // larger, as polygon rendering takes one pixel more
71 // to the right and to the bottom)
72 const ::basegfx::B2ISize
aBmpSizePixel(
73 ::basegfx::fround( aTmpRect
.getRange().getX() + 1),
74 ::basegfx::fround( aTmpRect
.getRange().getY() + 1) );
76 // create a bitmap of appropriate size
77 ::cppcanvas::BitmapSharedPtr
pBitmap(
78 ::cppcanvas::BaseGfxFactory::createBitmap(
82 ENSURE_OR_THROW( pBitmap
,
83 "ViewBackgroundShape::prefetch(): Cannot create background bitmap" );
85 ::cppcanvas::BitmapCanvasSharedPtr
pBitmapCanvas( pBitmap
->getBitmapCanvas() );
87 ENSURE_OR_THROW( pBitmapCanvas
,
88 "ViewBackgroundShape::prefetch(): Cannot create background bitmap canvas" );
91 initSlideBackground( pBitmapCanvas
,
94 // apply linear part of destination canvas transformation (linear means in this context:
95 // transformation without any translational components)
96 ::basegfx::B2DHomMatrix
aLinearTransform( rCanvasTransform
);
97 aLinearTransform
.set( 0, 2, 0.0 );
98 aLinearTransform
.set( 1, 2, 0.0 );
99 pBitmapCanvas
->setTransformation( aLinearTransform
);
101 const basegfx::B2DHomMatrix
aShapeTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
102 maBounds
.getWidth(), maBounds
.getHeight(),
103 maBounds
.getMinX(), maBounds
.getMinY()));
105 ::cppcanvas::RendererSharedPtr
pRenderer(
106 ::cppcanvas::VCLFactory::createRenderer(
109 ::cppcanvas::Renderer::Parameters() ) );
111 ENSURE_OR_RETURN_FALSE( pRenderer
,
112 "ViewBackgroundShape::prefetch(): Could not create Renderer" );
114 pRenderer
->setTransformation( aShapeTransform
);
117 mxBitmap
= pBitmap
->getUNOBitmap();
121 maLastTransformation
= rCanvasTransform
;
123 return mxBitmap
.is();
126 ViewBackgroundShape::ViewBackgroundShape( ViewLayerSharedPtr xViewLayer
,
127 const ::basegfx::B2DRectangle
& rShapeBounds
) :
128 mpViewLayer(std::move( xViewLayer
)),
131 maLastTransformation(),
132 maBounds( rShapeBounds
)
134 ENSURE_OR_THROW( mpViewLayer
, "ViewBackgroundShape::ViewBackgroundShape(): Invalid View" );
135 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "ViewBackgroundShape::ViewBackgroundShape(): Invalid ViewLayer canvas" );
138 const ViewLayerSharedPtr
& ViewBackgroundShape::getViewLayer() const
143 bool ViewBackgroundShape::render( const GDIMetaFileSharedPtr
& rMtf
) const
145 SAL_INFO( "slideshow", "::presentation::internal::ViewBackgroundShape::draw()" );
147 const ::cppcanvas::CanvasSharedPtr
& rDestinationCanvas( mpViewLayer
->getCanvas() );
149 if( !prefetch( rDestinationCanvas
, rMtf
) )
152 ENSURE_OR_RETURN_FALSE( mxBitmap
.is(),
153 "ViewBackgroundShape::draw(): Invalid background bitmap" );
155 ::basegfx::B2DHomMatrix
aTransform( mpViewLayer
->getTransformation() );
157 // invert the linear part of the view transformation
158 // (i.e. the view transformation without translational
159 // components), to be able to leave the canvas
160 // transformation intact (would otherwise destroy possible
161 // clippings, as the clip polygon is relative to the view
162 // coordinate system).
163 aTransform
.set(0,2, 0.0 );
164 aTransform
.set(1,2, 0.0 );
167 rendering::RenderState aRenderState
;
168 ::canvas::tools::initRenderState( aRenderState
);
170 ::canvas::tools::setRenderStateTransform( aRenderState
, aTransform
);
174 rDestinationCanvas
->getUNOCanvas()->drawBitmap( mxBitmap
,
175 rDestinationCanvas
->getViewState(),
178 catch( uno::Exception
& )
180 TOOLS_WARN_EXCEPTION( "slideshow", "" );
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */