merge the formfield patch from ooo-build
[ooovba.git] / sdext / source / minimizer / graphiccollector.cxx
blobda19d2752b1329815ee4f912c7ea2253ec91b8e3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: graphiccollector.cxx,v $
11 * $Revision: 1.5 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "graphiccollector.hxx"
36 #include <com/sun/star/awt/XDevice.hpp>
37 #include <com/sun/star/frame/XFramesSupplier.hpp>
38 #include <com/sun/star/drawing/FillStyle.hpp>
39 #include <com/sun/star/drawing/BitmapMode.hpp>
40 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
41 #include <com/sun/star/presentation/XPresentationPage.hpp>
42 #include <com/sun/star/drawing/XMasterPagesSupplier.hpp>
44 #include "impoptimizer.hxx"
46 using namespace ::rtl;
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::awt;
50 using namespace ::com::sun::star::drawing;
51 using namespace ::com::sun::star::graphic;
52 using namespace ::com::sun::star::frame;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::presentation;
56 const DeviceInfo& GraphicCollector::GetDeviceInfo( const Reference< XComponentContext >& rxFact )
58 static DeviceInfo aDeviceInfo;
59 if( !aDeviceInfo.Width )
61 try
63 Reference< XFramesSupplier > xDesktop( rxFact->getServiceManager()->createInstanceWithContext(
64 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), rxFact ), UNO_QUERY_THROW );
65 Reference< XFrame > xFrame( xDesktop->getActiveFrame() );
66 Reference< XWindow > xWindow( xFrame->getContainerWindow() );
67 Reference< XDevice > xDevice( xWindow, UNO_QUERY_THROW );
68 aDeviceInfo = xDevice->getInfo();
70 catch( Exception& )
74 return aDeviceInfo;
77 void ImpAddEntity( std::vector< GraphicCollector::GraphicEntity >& rGraphicEntities, Reference< XGraphic >& rxGraphic, const GraphicSettings& rGraphicSettings, const GraphicCollector::GraphicUser& rUser )
79 const rtl::OUString aGraphicURL( rUser.maGraphicURL );
80 const rtl::OUString sPackageURL( OUString::createFromAscii( "vnd.sun.star.GraphicObject:" ) );
82 if ( rGraphicSettings.mbEmbedLinkedGraphics || ( !aGraphicURL.getLength() || aGraphicURL.match( sPackageURL, 0 ) ) )
84 std::vector< GraphicCollector::GraphicEntity >::iterator aIter( rGraphicEntities.begin() );
85 while( aIter != rGraphicEntities.end() )
87 if ( aIter->maUser[ 0 ].maGraphicURL == aGraphicURL )
89 if ( rUser.maLogicalSize.Width > aIter->maLogicalSize.Width )
90 aIter->maLogicalSize.Width = rUser.maLogicalSize.Width;
91 if ( rUser.maLogicalSize.Height > aIter->maLogicalSize.Height )
92 aIter->maLogicalSize.Height = rUser.maLogicalSize.Height;
93 aIter->maUser.push_back( rUser );
94 break;
96 aIter++;
98 if ( aIter == rGraphicEntities.end() )
100 GraphicCollector::GraphicEntity aEntity( rxGraphic, rUser );
101 rGraphicEntities.push_back( aEntity );
106 void ImpAddGraphicEntity( const Reference< XComponentContext >& rxMSF, Reference< XShape >& rxShape, const GraphicSettings& rGraphicSettings, std::vector< GraphicCollector::GraphicEntity >& rGraphicEntities )
108 Reference< XGraphic > xGraphic;
109 Reference< XPropertySet > xShapePropertySet( rxShape, UNO_QUERY_THROW );
110 if ( xShapePropertySet->getPropertyValue( TKGet( TK_Graphic ) ) >>= xGraphic )
112 text::GraphicCrop aGraphicCropLogic( 0, 0, 0, 0 );
114 GraphicCollector::GraphicUser aUser;
115 aUser.mxShape = rxShape;
116 aUser.mbFillBitmap = sal_False;
117 xShapePropertySet->getPropertyValue( TKGet( TK_GraphicURL ) ) >>= aUser.maGraphicURL;
118 xShapePropertySet->getPropertyValue( TKGet( TK_GraphicStreamURL ) ) >>= aUser.maGraphicStreamURL;
119 xShapePropertySet->getPropertyValue( TKGet( TK_GraphicCrop ) ) >>= aGraphicCropLogic;
120 awt::Size aLogicalSize( rxShape->getSize() );
122 // calculating the logical size, as if there were no cropping
123 if ( aGraphicCropLogic.Left || aGraphicCropLogic.Right || aGraphicCropLogic.Top || aGraphicCropLogic.Bottom )
125 awt::Size aSize100thMM( GraphicCollector::GetOriginalSize( rxMSF, xGraphic ) );
126 if ( aSize100thMM.Width && aSize100thMM.Height )
128 awt::Size aCropSize( aSize100thMM.Width - ( aGraphicCropLogic.Left + aGraphicCropLogic.Right ),
129 aSize100thMM.Height - ( aGraphicCropLogic.Top + aGraphicCropLogic.Bottom ));
130 if ( aCropSize.Width && aCropSize.Height )
132 awt::Size aNewLogSize( static_cast< sal_Int32 >( static_cast< double >( aSize100thMM.Width * aLogicalSize.Width ) / aCropSize.Width ),
133 static_cast< sal_Int32 >( static_cast< double >( aSize100thMM.Height * aLogicalSize.Height ) / aCropSize.Height ) );
134 aLogicalSize = aNewLogSize;
138 aUser.maGraphicCropLogic = aGraphicCropLogic;
139 aUser.maLogicalSize = aLogicalSize;
140 ImpAddEntity( rGraphicEntities, xGraphic, rGraphicSettings, aUser );
144 void ImpAddFillBitmapEntity( const Reference< XComponentContext >& rxMSF, const Reference< XPropertySet >& rxPropertySet, const awt::Size& rLogicalSize,
145 std::vector< GraphicCollector::GraphicEntity >& rGraphicEntities, const GraphicSettings& rGraphicSettings, const Reference< XPropertySet >& rxPagePropertySet )
149 FillStyle eFillStyle;
150 if ( rxPropertySet->getPropertyValue( TKGet( TK_FillStyle ) ) >>= eFillStyle )
152 if ( eFillStyle == FillStyle_BITMAP )
154 rtl::OUString aFillBitmapURL;
155 Reference< XBitmap > xFillBitmap;
156 if ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmap ) ) >>= xFillBitmap )
158 Reference< XGraphic > xGraphic( xFillBitmap, UNO_QUERY_THROW );
159 if ( xGraphic.is() )
161 awt::Size aLogicalSize( rLogicalSize );
162 Reference< XPropertySetInfo > axPropSetInfo( rxPropertySet->getPropertySetInfo() );
163 if ( axPropSetInfo.is() )
165 if ( axPropSetInfo->hasPropertyByName( TKGet( TK_FillBitmapMode ) ) )
167 BitmapMode eBitmapMode;
168 if ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapMode ) ) >>= eBitmapMode )
170 if ( ( eBitmapMode == BitmapMode_REPEAT ) || ( eBitmapMode == BitmapMode_NO_REPEAT ) )
172 sal_Bool bLogicalSize = sal_False;
173 awt::Size aSize( 0, 0 );
174 if ( ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapLogicalSize ) ) >>= bLogicalSize )
175 && ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapSizeX ) ) >>= aSize.Width )
176 && ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapSizeY ) ) >>= aSize.Height ) )
178 if ( bLogicalSize )
180 if ( !aSize.Width || !aSize.Height )
182 awt::Size aSize100thMM( GraphicCollector::GetOriginalSize( rxMSF, xGraphic ) );
183 if ( aSize100thMM.Width && aSize100thMM.Height )
184 aLogicalSize = aSize100thMM;
186 else
187 aLogicalSize = aSize;
189 else
191 aLogicalSize.Width = sal::static_int_cast< sal_Int32 >( ( static_cast< double >( aLogicalSize.Width ) * aSize.Width ) / -100.0 );
192 aLogicalSize.Height = sal::static_int_cast< sal_Int32 >( ( static_cast< double >( aLogicalSize.Height ) * aSize.Height ) / -100.0 );
199 GraphicCollector::GraphicUser aUser;
200 aUser.mxPropertySet = rxPropertySet;
201 rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapURL ) ) >>= aUser.maGraphicURL;
202 aUser.mbFillBitmap = sal_True;
203 aUser.maLogicalSize = aLogicalSize;
204 aUser.mxPagePropertySet = rxPagePropertySet;
205 ImpAddEntity( rGraphicEntities, xGraphic, rGraphicSettings, aUser );
211 catch( Exception& )
216 void ImpCollectBackgroundGraphic( const Reference< XComponentContext >& rxMSF, const Reference< XDrawPage >& rxDrawPage, const GraphicSettings& rGraphicSettings, std::vector< GraphicCollector::GraphicEntity >& rGraphicEntities )
220 awt::Size aLogicalSize( 28000, 21000 );
221 Reference< XPropertySet > xPropertySet( rxDrawPage, UNO_QUERY_THROW );
222 xPropertySet->getPropertyValue( TKGet( TK_Width ) ) >>= aLogicalSize.Width;
223 xPropertySet->getPropertyValue( TKGet( TK_Height ) ) >>= aLogicalSize.Height;
225 Reference< XPropertySet > xBackgroundPropSet;
226 if ( xPropertySet->getPropertyValue( TKGet( TK_Background ) ) >>= xBackgroundPropSet )
227 ImpAddFillBitmapEntity( rxMSF, xBackgroundPropSet, aLogicalSize, rGraphicEntities, rGraphicSettings, xPropertySet );
229 catch( Exception& )
234 void ImpCollectGraphicObjects( const Reference< XComponentContext >& rxMSF, const Reference< XShapes >& rxShapes, const GraphicSettings& rGraphicSettings, std::vector< GraphicCollector::GraphicEntity >& rGraphicEntities )
236 for ( sal_Int32 i = 0; i < rxShapes->getCount(); i++ )
240 const OUString sGraphicObjectShape( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.GraphicObjectShape" ) );
241 const OUString sGroupShape( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.GroupShape" ) );
242 Reference< XShape > xShape( rxShapes->getByIndex( i ), UNO_QUERY_THROW );
243 const OUString sShapeType( xShape->getShapeType() );
244 if ( sShapeType == sGroupShape )
246 Reference< XShapes > xShapes( xShape, UNO_QUERY_THROW );
247 ImpCollectGraphicObjects( rxMSF, xShapes, rGraphicSettings, rGraphicEntities );
248 continue;
251 if ( sShapeType == sGraphicObjectShape )
252 ImpAddGraphicEntity( rxMSF, xShape, rGraphicSettings, rGraphicEntities );
254 // now check for a fillstyle
255 Reference< XPropertySet > xEmptyPagePropSet;
256 Reference< XPropertySet > xShapePropertySet( xShape, UNO_QUERY_THROW );
257 awt::Size aLogicalSize( xShape->getSize() );
258 ImpAddFillBitmapEntity( rxMSF, xShapePropertySet, aLogicalSize, rGraphicEntities, rGraphicSettings, xEmptyPagePropSet );
260 catch( Exception& )
266 awt::Size GraphicCollector::GetOriginalSize( const Reference< XComponentContext >& rxMSF, const Reference< XGraphic >& rxGraphic )
268 awt::Size aSize100thMM( 0, 0 );
269 Reference< XPropertySet > xGraphicPropertySet( rxGraphic, UNO_QUERY_THROW );
270 if ( xGraphicPropertySet->getPropertyValue( TKGet( TK_Size100thMM ) ) >>= aSize100thMM )
272 if ( !aSize100thMM.Width && !aSize100thMM.Height )
273 { // MAPMODE_PIXEL USED :-(
274 awt::Size aSourceSizePixel( 0, 0 );
275 if ( xGraphicPropertySet->getPropertyValue( TKGet( TK_SizePixel ) ) >>= aSourceSizePixel )
277 const DeviceInfo& rDeviceInfo( GraphicCollector::GetDeviceInfo( rxMSF ) );
278 if ( rDeviceInfo.PixelPerMeterX && rDeviceInfo.PixelPerMeterY )
280 aSize100thMM.Width = static_cast< sal_Int32 >( ( aSourceSizePixel.Width * 100000.0 ) / rDeviceInfo.PixelPerMeterX );
281 aSize100thMM.Height = static_cast< sal_Int32 >( ( aSourceSizePixel.Height * 100000.0 ) / rDeviceInfo.PixelPerMeterY );
286 return aSize100thMM;
289 void GraphicCollector::CollectGraphics( const Reference< XComponentContext >& rxMSF, const Reference< XModel >& rxModel,
290 const GraphicSettings& rGraphicSettings, std::vector< GraphicCollector::GraphicEntity >& rGraphicList )
294 sal_Int32 i;
295 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
296 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
297 for ( i = 0; i < xDrawPages->getCount(); i++ )
299 Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
300 ImpCollectBackgroundGraphic( rxMSF, xDrawPage, rGraphicSettings, rGraphicList );
301 Reference< XShapes > xDrawShapes( xDrawPage, UNO_QUERY_THROW );
302 ImpCollectGraphicObjects( rxMSF, xDrawShapes, rGraphicSettings, rGraphicList );
304 Reference< XPresentationPage > xPresentationPage( xDrawPage, UNO_QUERY_THROW );
305 Reference< XDrawPage > xNotesPage( xPresentationPage->getNotesPage() );
306 ImpCollectBackgroundGraphic( rxMSF, xNotesPage, rGraphicSettings, rGraphicList );
307 Reference< XShapes > xNotesShapes( xNotesPage, UNO_QUERY_THROW );
308 ImpCollectGraphicObjects( rxMSF, xNotesShapes, rGraphicSettings, rGraphicList );
310 Reference< XMasterPagesSupplier > xMasterPagesSupplier( rxModel, UNO_QUERY_THROW );
311 Reference< XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), UNO_QUERY_THROW );
312 for ( i = 0; i < xMasterPages->getCount(); i++ )
314 Reference< XDrawPage > xMasterPage( xMasterPages->getByIndex( i ), UNO_QUERY_THROW );
315 ImpCollectBackgroundGraphic( rxMSF, xMasterPage, rGraphicSettings, rGraphicList );
316 Reference< XShapes > xMasterPageShapes( xMasterPage, UNO_QUERY_THROW );
317 ImpCollectGraphicObjects( rxMSF, xMasterPageShapes, rGraphicSettings, rGraphicList );
320 std::vector< GraphicCollector::GraphicEntity >::iterator aGraphicIter( rGraphicList.begin() );
321 std::vector< GraphicCollector::GraphicEntity >::iterator aGraphicIEnd( rGraphicList.end() );
322 while( aGraphicIter != aGraphicIEnd )
324 // check if it is possible to remove the crop area
325 aGraphicIter->mbRemoveCropArea = rGraphicSettings.mbRemoveCropArea;
326 if ( aGraphicIter->mbRemoveCropArea )
328 std::vector< GraphicCollector::GraphicUser >::iterator aGUIter( aGraphicIter->maUser.begin() );
329 while( aGraphicIter->mbRemoveCropArea && ( aGUIter != aGraphicIter->maUser.end() ) )
331 if ( aGUIter->maGraphicCropLogic.Left || aGUIter->maGraphicCropLogic.Top
332 || aGUIter->maGraphicCropLogic.Right || aGUIter->maGraphicCropLogic.Bottom )
334 if ( aGUIter == aGraphicIter->maUser.begin() )
335 aGraphicIter->maGraphicCropLogic = aGUIter->maGraphicCropLogic;
336 else if ( ( aGraphicIter->maGraphicCropLogic.Left != aGUIter->maGraphicCropLogic.Left )
337 || ( aGraphicIter->maGraphicCropLogic.Top != aGUIter->maGraphicCropLogic.Top )
338 || ( aGraphicIter->maGraphicCropLogic.Right != aGUIter->maGraphicCropLogic.Right )
339 || ( aGraphicIter->maGraphicCropLogic.Bottom != aGUIter->maGraphicCropLogic.Bottom ) )
341 aGraphicIter->mbRemoveCropArea = sal_False;
344 else
345 aGraphicIter->mbRemoveCropArea = sal_False;
346 aGUIter++;
349 if ( !aGraphicIter->mbRemoveCropArea )
350 aGraphicIter->maGraphicCropLogic = text::GraphicCrop( 0, 0, 0, 0 );
351 aGraphicIter++;
354 catch ( Exception& )
359 void ImpCountGraphicObjects( const Reference< XComponentContext >& rxMSF, const Reference< XShapes >& rxShapes, const GraphicSettings& rGraphicSettings, sal_Int32& rnGraphics )
361 for ( sal_Int32 i = 0; i < rxShapes->getCount(); i++ )
365 const OUString sGraphicObjectShape( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.GraphicObjectShape" ) );
366 const OUString sGroupShape( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.GroupShape" ) );
367 Reference< XShape > xShape( rxShapes->getByIndex( i ), UNO_QUERY_THROW );
368 const OUString sShapeType( xShape->getShapeType() );
369 if ( sShapeType == sGroupShape )
371 Reference< XShapes > xShapes( xShape, UNO_QUERY_THROW );
372 ImpCountGraphicObjects( rxMSF, xShapes, rGraphicSettings, rnGraphics );
373 continue;
376 if ( sShapeType == sGraphicObjectShape )
378 rnGraphics++;
381 // now check for a fillstyle
382 Reference< XPropertySet > xEmptyPagePropSet;
383 Reference< XPropertySet > xShapePropertySet( xShape, UNO_QUERY_THROW );
384 awt::Size aLogicalSize( xShape->getSize() );
386 FillStyle eFillStyle;
387 if ( xShapePropertySet->getPropertyValue( TKGet( TK_FillStyle ) ) >>= eFillStyle )
389 if ( eFillStyle == FillStyle_BITMAP )
391 rnGraphics++;
395 catch( Exception& )
401 void ImpCountBackgroundGraphic( const Reference< XComponentContext >& /* rxMSF */, const Reference< XDrawPage >& rxDrawPage,
402 const GraphicSettings& /* rGraphicSettings */, sal_Int32& rnGraphics )
406 awt::Size aLogicalSize( 28000, 21000 );
407 Reference< XPropertySet > xPropertySet( rxDrawPage, UNO_QUERY_THROW );
408 xPropertySet->getPropertyValue( TKGet( TK_Width ) ) >>= aLogicalSize.Width;
409 xPropertySet->getPropertyValue( TKGet( TK_Height ) ) >>= aLogicalSize.Height;
411 Reference< XPropertySet > xBackgroundPropSet;
412 if ( xPropertySet->getPropertyValue( TKGet( TK_Background ) ) >>= xBackgroundPropSet )
414 FillStyle eFillStyle;
415 if ( xBackgroundPropSet->getPropertyValue( TKGet( TK_FillStyle ) ) >>= eFillStyle )
417 if ( eFillStyle == FillStyle_BITMAP )
419 rnGraphics++;
424 catch( Exception& )
429 void GraphicCollector::CountGraphics( const Reference< XComponentContext >& rxMSF, const Reference< XModel >& rxModel,
430 const GraphicSettings& rGraphicSettings, sal_Int32& rnGraphics )
434 sal_Int32 i;
435 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
436 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
437 for ( i = 0; i < xDrawPages->getCount(); i++ )
439 Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
440 ImpCountBackgroundGraphic( rxMSF, xDrawPage, rGraphicSettings, rnGraphics );
441 Reference< XShapes > xDrawShapes( xDrawPage, UNO_QUERY_THROW );
442 ImpCountGraphicObjects( rxMSF, xDrawShapes, rGraphicSettings, rnGraphics );
444 Reference< XPresentationPage > xPresentationPage( xDrawPage, UNO_QUERY_THROW );
445 Reference< XDrawPage > xNotesPage( xPresentationPage->getNotesPage() );
446 ImpCountBackgroundGraphic( rxMSF, xNotesPage, rGraphicSettings, rnGraphics );
447 Reference< XShapes > xNotesShapes( xNotesPage, UNO_QUERY_THROW );
448 ImpCountGraphicObjects( rxMSF, xNotesShapes, rGraphicSettings, rnGraphics );
450 Reference< XMasterPagesSupplier > xMasterPagesSupplier( rxModel, UNO_QUERY_THROW );
451 Reference< XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), UNO_QUERY_THROW );
452 for ( i = 0; i < xMasterPages->getCount(); i++ )
454 Reference< XDrawPage > xMasterPage( xMasterPages->getByIndex( i ), UNO_QUERY_THROW );
455 ImpCountBackgroundGraphic( rxMSF, xMasterPage, rGraphicSettings, rnGraphics );
456 Reference< XShapes > xMasterPageShapes( xMasterPage, UNO_QUERY_THROW );
457 ImpCountGraphicObjects( rxMSF, xMasterPageShapes, rGraphicSettings, rnGraphics );
460 catch ( Exception& )