1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // must be first
32*cdf0e10cSrcweir #include <canvas/debug.hxx>
33*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "viewbackgroundshape.hxx"
36*cdf0e10cSrcweir #include "tools.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <rtl/logfile.hxx>
39*cdf0e10cSrcweir #include <rtl/math.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <comphelper/anytostring.hxx>
42*cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
45*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
46*cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
47*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
48*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <com/sun/star/rendering/XCanvas.hpp>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
53*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
54*cdf0e10cSrcweir #include <cppcanvas/vclfactory.hxx>
55*cdf0e10cSrcweir #include <cppcanvas/basegfxfactory.hxx>
56*cdf0e10cSrcweir #include <cppcanvas/renderer.hxx>
57*cdf0e10cSrcweir #include <cppcanvas/bitmap.hxx>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir using namespace ::com::sun::star;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir namespace slideshow
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir     namespace internal
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir         bool ViewBackgroundShape::prefetch( const ::cppcanvas::CanvasSharedPtr&	rDestinationCanvas,
68*cdf0e10cSrcweir                                             const GDIMetaFileSharedPtr&			rMtf ) const
69*cdf0e10cSrcweir         {
70*cdf0e10cSrcweir             RTL_LOGFILE_CONTEXT( aLog, "::presentation::internal::ViewBackgroundShape::prefetch()" );
71*cdf0e10cSrcweir             ENSURE_OR_RETURN_FALSE( rMtf,
72*cdf0e10cSrcweir                                "ViewBackgroundShape::prefetch(): no valid metafile!" );
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir             const ::basegfx::B2DHomMatrix& rCanvasTransform(
75*cdf0e10cSrcweir                 mpViewLayer->getTransformation() );
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir             if( !mxBitmap.is() ||
78*cdf0e10cSrcweir                 rMtf != mpLastMtf ||
79*cdf0e10cSrcweir                 rCanvasTransform != maLastTransformation )
80*cdf0e10cSrcweir             {
81*cdf0e10cSrcweir                 // buffered bitmap is invalid, re-create
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir                 // determine transformed page bounds
84*cdf0e10cSrcweir                 ::basegfx::B2DRectangle aTmpRect;
85*cdf0e10cSrcweir                 ::canvas::tools::calcTransformedRectBounds( aTmpRect,
86*cdf0e10cSrcweir                                                             maBounds,
87*cdf0e10cSrcweir                                                             rCanvasTransform );
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir                 // determine pixel size of bitmap (choose it one pixel
90*cdf0e10cSrcweir                 // larger, as polygon rendering takes one pixel more
91*cdf0e10cSrcweir                 // to the right and to the bottom)
92*cdf0e10cSrcweir                 const ::basegfx::B2ISize aBmpSizePixel(
93*cdf0e10cSrcweir                     ::basegfx::fround( aTmpRect.getRange().getX() + 1),
94*cdf0e10cSrcweir                     ::basegfx::fround( aTmpRect.getRange().getY() + 1) );
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir                 // create a bitmap of appropriate size
97*cdf0e10cSrcweir                 ::cppcanvas::BitmapSharedPtr pBitmap(
98*cdf0e10cSrcweir                     ::cppcanvas::BaseGfxFactory::getInstance().createBitmap(
99*cdf0e10cSrcweir                         rDestinationCanvas,
100*cdf0e10cSrcweir                         aBmpSizePixel ) );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir                 ENSURE_OR_THROW( pBitmap,
103*cdf0e10cSrcweir                                   "ViewBackgroundShape::prefetch(): Cannot create background bitmap" );
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir                 ::cppcanvas::BitmapCanvasSharedPtr pBitmapCanvas( pBitmap->getBitmapCanvas() );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir                 ENSURE_OR_THROW( pBitmapCanvas,
108*cdf0e10cSrcweir                                   "ViewBackgroundShape::prefetch(): Cannot create background bitmap canvas" );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir                 // clear bitmap
111*cdf0e10cSrcweir                 initSlideBackground( pBitmapCanvas,
112*cdf0e10cSrcweir                                      aBmpSizePixel );
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir                 // apply linear part of destination canvas transformation (linear means in this context:
115*cdf0e10cSrcweir                 // transformation without any translational components)
116*cdf0e10cSrcweir                 ::basegfx::B2DHomMatrix aLinearTransform( rCanvasTransform );
117*cdf0e10cSrcweir                 aLinearTransform.set( 0, 2, 0.0 );
118*cdf0e10cSrcweir                 aLinearTransform.set( 1, 2, 0.0 );
119*cdf0e10cSrcweir                 pBitmapCanvas->setTransformation( aLinearTransform );
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir                 const basegfx::B2DHomMatrix aShapeTransform(basegfx::tools::createScaleTranslateB2DHomMatrix(
122*cdf0e10cSrcweir                     maBounds.getWidth(), maBounds.getHeight(),
123*cdf0e10cSrcweir                     maBounds.getMinX(), maBounds.getMinY()));
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir                 ::cppcanvas::RendererSharedPtr pRenderer(
126*cdf0e10cSrcweir                     ::cppcanvas::VCLFactory::getInstance().createRenderer(
127*cdf0e10cSrcweir                         pBitmapCanvas,
128*cdf0e10cSrcweir                         *rMtf.get(),
129*cdf0e10cSrcweir                         ::cppcanvas::Renderer::Parameters() ) );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir                 ENSURE_OR_RETURN_FALSE( pRenderer,
132*cdf0e10cSrcweir                                    "ViewBackgroundShape::prefetch(): Could not create Renderer" );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir                 pRenderer->setTransformation( aShapeTransform );
135*cdf0e10cSrcweir                 pRenderer->draw();
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir                 mxBitmap = pBitmap->getUNOBitmap();
138*cdf0e10cSrcweir             }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir             mpLastMtf 			 = rMtf;
141*cdf0e10cSrcweir             maLastTransformation = rCanvasTransform;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir             return mxBitmap.is();
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         ViewBackgroundShape::ViewBackgroundShape( const ViewLayerSharedPtr& 		rViewLayer,
147*cdf0e10cSrcweir                                                   const ::basegfx::B2DRectangle&	rShapeBounds ) :
148*cdf0e10cSrcweir             mpViewLayer( rViewLayer ),
149*cdf0e10cSrcweir             mxBitmap(),
150*cdf0e10cSrcweir             mpLastMtf(),
151*cdf0e10cSrcweir             maLastTransformation(),
152*cdf0e10cSrcweir             maBounds( rShapeBounds )
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             ENSURE_OR_THROW( mpViewLayer, "ViewBackgroundShape::ViewBackgroundShape(): Invalid View" );
155*cdf0e10cSrcweir             ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewBackgroundShape::ViewBackgroundShape(): Invalid ViewLayer canvas" );
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         ViewLayerSharedPtr ViewBackgroundShape::getViewLayer() const
159*cdf0e10cSrcweir         {
160*cdf0e10cSrcweir             return mpViewLayer;
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         bool ViewBackgroundShape::render( const GDIMetaFileSharedPtr& rMtf ) const
164*cdf0e10cSrcweir         {
165*cdf0e10cSrcweir             RTL_LOGFILE_CONTEXT( aLog, "::presentation::internal::ViewBackgroundShape::draw()" );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir             const ::cppcanvas::CanvasSharedPtr&	rDestinationCanvas( mpViewLayer->getCanvas() );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir             if( !prefetch( rDestinationCanvas, rMtf ) )
170*cdf0e10cSrcweir                 return false;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir             ENSURE_OR_RETURN_FALSE( mxBitmap.is(),
173*cdf0e10cSrcweir                                "ViewBackgroundShape::draw(): Invalid background bitmap" );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir             ::basegfx::B2DHomMatrix aTransform( mpViewLayer->getTransformation() );
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir             // invert the linear part of the view transformation
178*cdf0e10cSrcweir             // (i.e. the view transformation without translational
179*cdf0e10cSrcweir             // components), to be able to leave the canvas
180*cdf0e10cSrcweir             // transformation intact (would otherwise destroy possible
181*cdf0e10cSrcweir             // clippings, as the clip polygon is relative to the view
182*cdf0e10cSrcweir             // coordinate system).
183*cdf0e10cSrcweir             aTransform.set(0,2, 0.0 );
184*cdf0e10cSrcweir             aTransform.set(1,2, 0.0 );
185*cdf0e10cSrcweir             aTransform.invert();
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir             rendering::RenderState aRenderState;
188*cdf0e10cSrcweir             ::canvas::tools::initRenderState( aRenderState );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir             ::canvas::tools::setRenderStateTransform( aRenderState, aTransform );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir             try
193*cdf0e10cSrcweir             {
194*cdf0e10cSrcweir                 rDestinationCanvas->getUNOCanvas()->drawBitmap( mxBitmap,
195*cdf0e10cSrcweir                                                                 rDestinationCanvas->getViewState(),
196*cdf0e10cSrcweir                                                                 aRenderState );
197*cdf0e10cSrcweir             }
198*cdf0e10cSrcweir             catch( uno::Exception& )
199*cdf0e10cSrcweir             {
200*cdf0e10cSrcweir                 OSL_ENSURE( false,
201*cdf0e10cSrcweir                             rtl::OUStringToOString(
202*cdf0e10cSrcweir                                 comphelper::anyToString( cppu::getCaughtException() ),
203*cdf0e10cSrcweir                                 RTL_TEXTENCODING_UTF8 ).getStr() );
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir                 return false;
206*cdf0e10cSrcweir             }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir             return true;
209*cdf0e10cSrcweir         }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir }
213