1*70f497fbSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*70f497fbSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*70f497fbSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*70f497fbSAndrew Rist * distributed with this work for additional information 6*70f497fbSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*70f497fbSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*70f497fbSAndrew Rist * "License"); you may not use this file except in compliance 9*70f497fbSAndrew Rist * with the License. You may obtain a copy of the License at 10*70f497fbSAndrew Rist * 11*70f497fbSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*70f497fbSAndrew Rist * 13*70f497fbSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*70f497fbSAndrew Rist * software distributed under the License is distributed on an 15*70f497fbSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*70f497fbSAndrew Rist * KIND, either express or implied. See the License for the 17*70f497fbSAndrew Rist * specific language governing permissions and limitations 18*70f497fbSAndrew Rist * under the License. 19*70f497fbSAndrew Rist * 20*70f497fbSAndrew Rist *************************************************************/ 21*70f497fbSAndrew Rist 22*70f497fbSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_slideshow.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir // must be first 28cdf0e10cSrcweir #include <canvas/debug.hxx> 29cdf0e10cSrcweir #include <tools/diagnose_ex.h> 30cdf0e10cSrcweir 31cdf0e10cSrcweir #include "viewbackgroundshape.hxx" 32cdf0e10cSrcweir #include "tools.hxx" 33cdf0e10cSrcweir 34cdf0e10cSrcweir #include <rtl/logfile.hxx> 35cdf0e10cSrcweir #include <rtl/math.hxx> 36cdf0e10cSrcweir 37cdf0e10cSrcweir #include <comphelper/anytostring.hxx> 38cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx> 39cdf0e10cSrcweir 40cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx> 41cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx> 42cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx> 43cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx> 44cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx> 45cdf0e10cSrcweir 46cdf0e10cSrcweir #include <com/sun/star/rendering/XCanvas.hpp> 47cdf0e10cSrcweir 48cdf0e10cSrcweir #include <canvas/verbosetrace.hxx> 49cdf0e10cSrcweir #include <canvas/canvastools.hxx> 50cdf0e10cSrcweir #include <cppcanvas/vclfactory.hxx> 51cdf0e10cSrcweir #include <cppcanvas/basegfxfactory.hxx> 52cdf0e10cSrcweir #include <cppcanvas/renderer.hxx> 53cdf0e10cSrcweir #include <cppcanvas/bitmap.hxx> 54cdf0e10cSrcweir 55cdf0e10cSrcweir using namespace ::com::sun::star; 56cdf0e10cSrcweir 57cdf0e10cSrcweir 58cdf0e10cSrcweir namespace slideshow 59cdf0e10cSrcweir { 60cdf0e10cSrcweir namespace internal 61cdf0e10cSrcweir { 62cdf0e10cSrcweir prefetch(const::cppcanvas::CanvasSharedPtr & rDestinationCanvas,const GDIMetaFileSharedPtr & rMtf) const63cdf0e10cSrcweir bool ViewBackgroundShape::prefetch( const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas, 64cdf0e10cSrcweir const GDIMetaFileSharedPtr& rMtf ) const 65cdf0e10cSrcweir { 66cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "::presentation::internal::ViewBackgroundShape::prefetch()" ); 67cdf0e10cSrcweir ENSURE_OR_RETURN_FALSE( rMtf, 68cdf0e10cSrcweir "ViewBackgroundShape::prefetch(): no valid metafile!" ); 69cdf0e10cSrcweir 70cdf0e10cSrcweir const ::basegfx::B2DHomMatrix& rCanvasTransform( 71cdf0e10cSrcweir mpViewLayer->getTransformation() ); 72cdf0e10cSrcweir 73cdf0e10cSrcweir if( !mxBitmap.is() || 74cdf0e10cSrcweir rMtf != mpLastMtf || 75cdf0e10cSrcweir rCanvasTransform != maLastTransformation ) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir // buffered bitmap is invalid, re-create 78cdf0e10cSrcweir 79cdf0e10cSrcweir // determine transformed page bounds 80cdf0e10cSrcweir ::basegfx::B2DRectangle aTmpRect; 81cdf0e10cSrcweir ::canvas::tools::calcTransformedRectBounds( aTmpRect, 82cdf0e10cSrcweir maBounds, 83cdf0e10cSrcweir rCanvasTransform ); 84cdf0e10cSrcweir 85cdf0e10cSrcweir // determine pixel size of bitmap (choose it one pixel 86cdf0e10cSrcweir // larger, as polygon rendering takes one pixel more 87cdf0e10cSrcweir // to the right and to the bottom) 88cdf0e10cSrcweir const ::basegfx::B2ISize aBmpSizePixel( 89cdf0e10cSrcweir ::basegfx::fround( aTmpRect.getRange().getX() + 1), 90cdf0e10cSrcweir ::basegfx::fround( aTmpRect.getRange().getY() + 1) ); 91cdf0e10cSrcweir 92cdf0e10cSrcweir // create a bitmap of appropriate size 93cdf0e10cSrcweir ::cppcanvas::BitmapSharedPtr pBitmap( 94cdf0e10cSrcweir ::cppcanvas::BaseGfxFactory::getInstance().createBitmap( 95cdf0e10cSrcweir rDestinationCanvas, 96cdf0e10cSrcweir aBmpSizePixel ) ); 97cdf0e10cSrcweir 98cdf0e10cSrcweir ENSURE_OR_THROW( pBitmap, 99cdf0e10cSrcweir "ViewBackgroundShape::prefetch(): Cannot create background bitmap" ); 100cdf0e10cSrcweir 101cdf0e10cSrcweir ::cppcanvas::BitmapCanvasSharedPtr pBitmapCanvas( pBitmap->getBitmapCanvas() ); 102cdf0e10cSrcweir 103cdf0e10cSrcweir ENSURE_OR_THROW( pBitmapCanvas, 104cdf0e10cSrcweir "ViewBackgroundShape::prefetch(): Cannot create background bitmap canvas" ); 105cdf0e10cSrcweir 106cdf0e10cSrcweir // clear bitmap 107cdf0e10cSrcweir initSlideBackground( pBitmapCanvas, 108cdf0e10cSrcweir aBmpSizePixel ); 109cdf0e10cSrcweir 110cdf0e10cSrcweir // apply linear part of destination canvas transformation (linear means in this context: 111cdf0e10cSrcweir // transformation without any translational components) 112cdf0e10cSrcweir ::basegfx::B2DHomMatrix aLinearTransform( rCanvasTransform ); 113cdf0e10cSrcweir aLinearTransform.set( 0, 2, 0.0 ); 114cdf0e10cSrcweir aLinearTransform.set( 1, 2, 0.0 ); 115cdf0e10cSrcweir pBitmapCanvas->setTransformation( aLinearTransform ); 116cdf0e10cSrcweir 117cdf0e10cSrcweir const basegfx::B2DHomMatrix aShapeTransform(basegfx::tools::createScaleTranslateB2DHomMatrix( 118cdf0e10cSrcweir maBounds.getWidth(), maBounds.getHeight(), 119cdf0e10cSrcweir maBounds.getMinX(), maBounds.getMinY())); 120cdf0e10cSrcweir 121cdf0e10cSrcweir ::cppcanvas::RendererSharedPtr pRenderer( 122cdf0e10cSrcweir ::cppcanvas::VCLFactory::getInstance().createRenderer( 123cdf0e10cSrcweir pBitmapCanvas, 124cdf0e10cSrcweir *rMtf.get(), 125cdf0e10cSrcweir ::cppcanvas::Renderer::Parameters() ) ); 126cdf0e10cSrcweir 127cdf0e10cSrcweir ENSURE_OR_RETURN_FALSE( pRenderer, 128cdf0e10cSrcweir "ViewBackgroundShape::prefetch(): Could not create Renderer" ); 129cdf0e10cSrcweir 130cdf0e10cSrcweir pRenderer->setTransformation( aShapeTransform ); 131cdf0e10cSrcweir pRenderer->draw(); 132cdf0e10cSrcweir 133cdf0e10cSrcweir mxBitmap = pBitmap->getUNOBitmap(); 134cdf0e10cSrcweir } 135cdf0e10cSrcweir 136cdf0e10cSrcweir mpLastMtf = rMtf; 137cdf0e10cSrcweir maLastTransformation = rCanvasTransform; 138cdf0e10cSrcweir 139cdf0e10cSrcweir return mxBitmap.is(); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir ViewBackgroundShape(const ViewLayerSharedPtr & rViewLayer,const::basegfx::B2DRectangle & rShapeBounds)142cdf0e10cSrcweir ViewBackgroundShape::ViewBackgroundShape( const ViewLayerSharedPtr& rViewLayer, 143cdf0e10cSrcweir const ::basegfx::B2DRectangle& rShapeBounds ) : 144cdf0e10cSrcweir mpViewLayer( rViewLayer ), 145cdf0e10cSrcweir mxBitmap(), 146cdf0e10cSrcweir mpLastMtf(), 147cdf0e10cSrcweir maLastTransformation(), 148cdf0e10cSrcweir maBounds( rShapeBounds ) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir ENSURE_OR_THROW( mpViewLayer, "ViewBackgroundShape::ViewBackgroundShape(): Invalid View" ); 151cdf0e10cSrcweir ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewBackgroundShape::ViewBackgroundShape(): Invalid ViewLayer canvas" ); 152cdf0e10cSrcweir } 153cdf0e10cSrcweir getViewLayer() const154cdf0e10cSrcweir ViewLayerSharedPtr ViewBackgroundShape::getViewLayer() const 155cdf0e10cSrcweir { 156cdf0e10cSrcweir return mpViewLayer; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir render(const GDIMetaFileSharedPtr & rMtf) const159cdf0e10cSrcweir bool ViewBackgroundShape::render( const GDIMetaFileSharedPtr& rMtf ) const 160cdf0e10cSrcweir { 161cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "::presentation::internal::ViewBackgroundShape::draw()" ); 162cdf0e10cSrcweir 163cdf0e10cSrcweir const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas( mpViewLayer->getCanvas() ); 164cdf0e10cSrcweir 165cdf0e10cSrcweir if( !prefetch( rDestinationCanvas, rMtf ) ) 166cdf0e10cSrcweir return false; 167cdf0e10cSrcweir 168cdf0e10cSrcweir ENSURE_OR_RETURN_FALSE( mxBitmap.is(), 169cdf0e10cSrcweir "ViewBackgroundShape::draw(): Invalid background bitmap" ); 170cdf0e10cSrcweir 171cdf0e10cSrcweir ::basegfx::B2DHomMatrix aTransform( mpViewLayer->getTransformation() ); 172cdf0e10cSrcweir 173cdf0e10cSrcweir // invert the linear part of the view transformation 174cdf0e10cSrcweir // (i.e. the view transformation without translational 175cdf0e10cSrcweir // components), to be able to leave the canvas 176cdf0e10cSrcweir // transformation intact (would otherwise destroy possible 177cdf0e10cSrcweir // clippings, as the clip polygon is relative to the view 178cdf0e10cSrcweir // coordinate system). 179cdf0e10cSrcweir aTransform.set(0,2, 0.0 ); 180cdf0e10cSrcweir aTransform.set(1,2, 0.0 ); 181cdf0e10cSrcweir aTransform.invert(); 182cdf0e10cSrcweir 183cdf0e10cSrcweir rendering::RenderState aRenderState; 184cdf0e10cSrcweir ::canvas::tools::initRenderState( aRenderState ); 185cdf0e10cSrcweir 186cdf0e10cSrcweir ::canvas::tools::setRenderStateTransform( aRenderState, aTransform ); 187cdf0e10cSrcweir 188cdf0e10cSrcweir try 189cdf0e10cSrcweir { 190cdf0e10cSrcweir rDestinationCanvas->getUNOCanvas()->drawBitmap( mxBitmap, 191cdf0e10cSrcweir rDestinationCanvas->getViewState(), 192cdf0e10cSrcweir aRenderState ); 193cdf0e10cSrcweir } 194cdf0e10cSrcweir catch( uno::Exception& ) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir OSL_ENSURE( false, 197cdf0e10cSrcweir rtl::OUStringToOString( 198cdf0e10cSrcweir comphelper::anyToString( cppu::getCaughtException() ), 199cdf0e10cSrcweir RTL_TEXTENCODING_UTF8 ).getStr() ); 200cdf0e10cSrcweir 201cdf0e10cSrcweir return false; 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir return true; 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 207cdf0e10cSrcweir } 208cdf0e10cSrcweir } 209