Bump version to 6.4-15
[LibreOffice.git] / slideshow / source / engine / shapes / viewbackgroundshape.cxx
blobcdfd10bad100eff91aee206143aa5bc5a30cffb1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 // must be first
22 #include <tools/diagnose_ex.h>
23 #include <sal/log.hxx>
25 #include "viewbackgroundshape.hxx"
26 #include <tools.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/canvastools.hxx>
42 #include <cppcanvas/vclfactory.hxx>
43 #include <cppcanvas/basegfxfactory.hxx>
44 #include <cppcanvas/renderer.hxx>
45 #include <cppcanvas/bitmap.hxx>
47 using namespace ::com::sun::star;
50 namespace slideshow
52 namespace internal
55 bool ViewBackgroundShape::prefetch( const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas,
56 const GDIMetaFileSharedPtr& rMtf ) const
58 SAL_INFO( "slideshow", "::presentation::internal::ViewBackgroundShape::prefetch()" );
59 ENSURE_OR_RETURN_FALSE( rMtf,
60 "ViewBackgroundShape::prefetch(): no valid metafile!" );
62 const ::basegfx::B2DHomMatrix& rCanvasTransform(
63 mpViewLayer->getTransformation() );
65 if( !mxBitmap.is() ||
66 rMtf != mpLastMtf ||
67 rCanvasTransform != maLastTransformation )
69 // buffered bitmap is invalid, re-create
71 // determine transformed page bounds
72 ::basegfx::B2DRectangle aTmpRect;
73 ::canvas::tools::calcTransformedRectBounds( aTmpRect,
74 maBounds,
75 rCanvasTransform );
77 // determine pixel size of bitmap (choose it one pixel
78 // larger, as polygon rendering takes one pixel more
79 // to the right and to the bottom)
80 const ::basegfx::B2ISize aBmpSizePixel(
81 ::basegfx::fround( aTmpRect.getRange().getX() + 1),
82 ::basegfx::fround( aTmpRect.getRange().getY() + 1) );
84 // create a bitmap of appropriate size
85 ::cppcanvas::BitmapSharedPtr pBitmap(
86 ::cppcanvas::BaseGfxFactory::createBitmap(
87 rDestinationCanvas,
88 aBmpSizePixel ) );
90 ENSURE_OR_THROW( pBitmap,
91 "ViewBackgroundShape::prefetch(): Cannot create background bitmap" );
93 ::cppcanvas::BitmapCanvasSharedPtr pBitmapCanvas( pBitmap->getBitmapCanvas() );
95 ENSURE_OR_THROW( pBitmapCanvas,
96 "ViewBackgroundShape::prefetch(): Cannot create background bitmap canvas" );
98 // clear bitmap
99 initSlideBackground( pBitmapCanvas,
100 aBmpSizePixel );
102 // apply linear part of destination canvas transformation (linear means in this context:
103 // transformation without any translational components)
104 ::basegfx::B2DHomMatrix aLinearTransform( rCanvasTransform );
105 aLinearTransform.set( 0, 2, 0.0 );
106 aLinearTransform.set( 1, 2, 0.0 );
107 pBitmapCanvas->setTransformation( aLinearTransform );
109 const basegfx::B2DHomMatrix aShapeTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
110 maBounds.getWidth(), maBounds.getHeight(),
111 maBounds.getMinX(), maBounds.getMinY()));
113 ::cppcanvas::RendererSharedPtr pRenderer(
114 ::cppcanvas::VCLFactory::createRenderer(
115 pBitmapCanvas,
116 *rMtf,
117 ::cppcanvas::Renderer::Parameters() ) );
119 ENSURE_OR_RETURN_FALSE( pRenderer,
120 "ViewBackgroundShape::prefetch(): Could not create Renderer" );
122 pRenderer->setTransformation( aShapeTransform );
123 pRenderer->draw();
125 mxBitmap = pBitmap->getUNOBitmap();
128 mpLastMtf = rMtf;
129 maLastTransformation = rCanvasTransform;
131 return mxBitmap.is();
134 ViewBackgroundShape::ViewBackgroundShape( const ViewLayerSharedPtr& rViewLayer,
135 const ::basegfx::B2DRectangle& rShapeBounds ) :
136 mpViewLayer( rViewLayer ),
137 mxBitmap(),
138 mpLastMtf(),
139 maLastTransformation(),
140 maBounds( rShapeBounds )
142 ENSURE_OR_THROW( mpViewLayer, "ViewBackgroundShape::ViewBackgroundShape(): Invalid View" );
143 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewBackgroundShape::ViewBackgroundShape(): Invalid ViewLayer canvas" );
146 const ViewLayerSharedPtr& ViewBackgroundShape::getViewLayer() const
148 return mpViewLayer;
151 bool ViewBackgroundShape::render( const GDIMetaFileSharedPtr& rMtf ) const
153 SAL_INFO( "slideshow", "::presentation::internal::ViewBackgroundShape::draw()" );
155 const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas( mpViewLayer->getCanvas() );
157 if( !prefetch( rDestinationCanvas, rMtf ) )
158 return false;
160 ENSURE_OR_RETURN_FALSE( mxBitmap.is(),
161 "ViewBackgroundShape::draw(): Invalid background bitmap" );
163 ::basegfx::B2DHomMatrix aTransform( mpViewLayer->getTransformation() );
165 // invert the linear part of the view transformation
166 // (i.e. the view transformation without translational
167 // components), to be able to leave the canvas
168 // transformation intact (would otherwise destroy possible
169 // clippings, as the clip polygon is relative to the view
170 // coordinate system).
171 aTransform.set(0,2, 0.0 );
172 aTransform.set(1,2, 0.0 );
173 aTransform.invert();
175 rendering::RenderState aRenderState;
176 ::canvas::tools::initRenderState( aRenderState );
178 ::canvas::tools::setRenderStateTransform( aRenderState, aTransform );
182 rDestinationCanvas->getUNOCanvas()->drawBitmap( mxBitmap,
183 rDestinationCanvas->getViewState(),
184 aRenderState );
186 catch( uno::Exception& )
188 TOOLS_WARN_EXCEPTION( "slideshow", "" );
189 return false;
192 return true;
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */