Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "impoptimizer.hxx"
22 : #include "pppoptimizer.hxx"
23 : #include "graphiccollector.hxx"
24 : #include "pagecollector.hxx"
25 : #include "informationdialog.hxx"
26 :
27 : #include <vector>
28 : #include "com/sun/star/util/URL.hpp"
29 : #include "com/sun/star/util/XURLTransformer.hpp"
30 : #include <com/sun/star/beans/XPropertySet.hpp>
31 : #include <com/sun/star/awt/Rectangle.hpp>
32 : #include <com/sun/star/awt/Size.hpp>
33 : #include <com/sun/star/util/MeasureUnit.hpp>
34 : #include <com/sun/star/frame/XModel.hpp>
35 : #include <com/sun/star/frame/Desktop.hpp>
36 : #include <com/sun/star/awt/XWindow.hpp>
37 : #include <com/sun/star/frame/XStorable.hpp>
38 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
39 : #include <com/sun/star/frame/XDispatchProvider.hpp>
40 : #include <com/sun/star/graphic/GraphicProvider.hpp>
41 : #include <com/sun/star/graphic/XGraphicProvider.hpp>
42 : #include <com/sun/star/lang/XServiceInfo.hpp>
43 : #include <com/sun/star/container/XNamed.hpp>
44 : #include <com/sun/star/drawing/XShapes.hpp>
45 : #include <com/sun/star/drawing/XMasterPageTarget.hpp>
46 : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
47 : #include <com/sun/star/drawing/XMasterPagesSupplier.hpp>
48 : #include <com/sun/star/presentation/XPresentationSupplier.hpp>
49 : #include <com/sun/star/container/XNameAccess.hpp>
50 : #include <com/sun/star/presentation/XPresentation.hpp>
51 : #include <com/sun/star/presentation/XPresentationPage.hpp>
52 : #include <com/sun/star/document/XFilter.hpp>
53 : #include <com/sun/star/document/XExporter.hpp>
54 : #include <com/sun/star/uno/RuntimeException.hpp>
55 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
56 : #include <com/sun/star/graphic/GraphicType.hpp>
57 : #include <com/sun/star/io/XStream.hpp>
58 : #include <com/sun/star/io/XSeekable.hpp>
59 : #include <com/sun/star/io/TempFile.hpp>
60 : #include <com/sun/star/frame/XComponentLoader.hpp>
61 : #include <com/sun/star/util/URL.hpp>
62 : #include <com/sun/star/util/URLTransformer.hpp>
63 :
64 : using namespace ::std;
65 : using namespace ::rtl;
66 : using namespace ::com::sun::star;
67 : using namespace ::com::sun::star::io;
68 : using namespace ::com::sun::star::awt;
69 : using namespace ::com::sun::star::uno;
70 : using namespace ::com::sun::star::lang;
71 : using namespace ::com::sun::star::util;
72 : using namespace ::com::sun::star::frame;
73 : using namespace ::com::sun::star::beans;
74 : using namespace ::com::sun::star::drawing;
75 : using namespace ::com::sun::star::graphic;
76 : using namespace ::com::sun::star::document;
77 : using namespace ::com::sun::star::container;
78 : using namespace ::com::sun::star::presentation;
79 :
80 0 : void ImpExtractCustomShow( const Reference< XModel >& rxModel, const OUString& rCustomShowName )
81 : {
82 0 : vector< Reference< XDrawPage > > vNonUsedPageList;
83 : try
84 : {
85 0 : PageCollector::CollectNonCustomShowPages( rxModel, rCustomShowName, vNonUsedPageList );
86 0 : Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
87 0 : Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
88 0 : vector< Reference< XDrawPage > >::iterator aIter( vNonUsedPageList.begin() );
89 0 : while( aIter != vNonUsedPageList.end() )
90 0 : xDrawPages->remove( *aIter++ );
91 : }
92 0 : catch( Exception& )
93 : {
94 :
95 0 : }
96 0 : }
97 :
98 0 : void ImpDeleteUnusedMasterPages( const Reference< XModel >& rxModel )
99 : {
100 0 : vector< PageCollector::MasterPageEntity > aMasterPageList;
101 0 : PageCollector::CollectMasterPages( rxModel, aMasterPageList );
102 :
103 : // now master pages that are not marked can be deleted
104 0 : Reference< XMasterPagesSupplier > xMasterPagesSupplier( rxModel, UNO_QUERY_THROW );
105 0 : Reference< XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), UNO_QUERY_THROW );
106 0 : vector< PageCollector::MasterPageEntity >::iterator aIter( aMasterPageList.begin() );
107 0 : while( aIter != aMasterPageList.end() )
108 : {
109 0 : if ( !aIter->bUsed )
110 0 : xMasterPages->remove( aIter->xMasterPage );
111 0 : ++aIter;
112 0 : }
113 0 : }
114 :
115 0 : void ImpDeleteHiddenSlides( const Reference< XModel >& rxModel )
116 : {
117 : try
118 : {
119 0 : Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
120 0 : Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
121 0 : for( sal_Int32 i = 0; i < xDrawPages->getCount(); i++ )
122 : {
123 0 : Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
124 0 : Reference< XPropertySet > xPropSet( xDrawPage, UNO_QUERY_THROW );
125 :
126 0 : sal_Bool bVisible = sal_True;
127 0 : const OUString sVisible( "Visible" );
128 0 : if ( xPropSet->getPropertyValue( sVisible ) >>= bVisible )
129 : {
130 0 : if (!bVisible )
131 : {
132 0 : xDrawPages->remove( xDrawPage );
133 0 : i--;
134 : }
135 : }
136 0 : }
137 : }
138 0 : catch( Exception& )
139 : {
140 : }
141 0 : }
142 :
143 0 : void ImpDeleteNotesPages( const Reference< XModel >& rxModel )
144 : {
145 : try
146 : {
147 0 : Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
148 0 : Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
149 0 : sal_Int32 i, nPages = xDrawPages->getCount();
150 0 : for( i = 0; i < nPages; i++ )
151 : {
152 0 : Reference< XPresentationPage > xPresentationPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
153 0 : Reference< XPropertySet > xPropSet( xPresentationPage->getNotesPage(), UNO_QUERY_THROW );
154 0 : Reference< XShapes > xShapes( xPropSet, UNO_QUERY_THROW );
155 0 : while( xShapes->getCount() )
156 0 : xShapes->remove( Reference< XShape >( xShapes->getByIndex( xShapes->getCount() - 1 ), UNO_QUERY_THROW ) );
157 :
158 0 : const OUString sLayout( "Layout" );
159 0 : xPropSet->setPropertyValue( sLayout, Any( (sal_Int16)21 ) );
160 0 : }
161 : }
162 0 : catch( Exception& )
163 : {
164 : }
165 0 : }
166 :
167 0 : void ImpConvertOLE( const Reference< XModel >& rxModel, sal_Int32 nOLEOptimizationType )
168 : {
169 : try
170 : {
171 0 : Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
172 0 : Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
173 0 : for ( sal_Int32 i = 0; i < xDrawPages->getCount(); i++ )
174 : {
175 0 : Reference< XShapes > xShapes( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
176 0 : for ( sal_Int32 j = 0; j < xShapes->getCount(); j++ )
177 : {
178 0 : const OUString sOLE2Shape( "com.sun.star.drawing.OLE2Shape" );
179 0 : Reference< XShape > xShape( xShapes->getByIndex( j ), UNO_QUERY_THROW );
180 0 : if ( xShape->getShapeType() == sOLE2Shape )
181 : {
182 0 : Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
183 :
184 0 : sal_Bool bConvertOLE = nOLEOptimizationType == 0;
185 0 : if ( nOLEOptimizationType == 1 )
186 : {
187 0 : sal_Bool bIsInternal = sal_True;
188 0 : xPropSet->getPropertyValue( TKGet( TK_IsInternal ) ) >>= bIsInternal;
189 0 : bConvertOLE = !bIsInternal;
190 : }
191 0 : if ( bConvertOLE )
192 : {
193 0 : Reference< XGraphic > xGraphic;
194 0 : if ( xPropSet->getPropertyValue( TKGet( TK_Graphic ) ) >>= xGraphic )
195 : {
196 0 : const OUString sGraphicShape( "com.sun.star.drawing.GraphicObjectShape" );
197 0 : Reference< XMultiServiceFactory > xFact( rxModel, UNO_QUERY_THROW );
198 0 : Reference< XShape > xShape2( xFact->createInstance( sGraphicShape ), UNO_QUERY_THROW );
199 0 : xShapes->add( xShape2 );
200 0 : xShape2->setPosition( xShape->getPosition() );
201 0 : xShape2->setSize( xShape->getSize() );
202 0 : Reference< XPropertySet > xPropSet2( xShape2, UNO_QUERY_THROW );
203 0 : xPropSet2->setPropertyValue( TKGet( TK_Graphic ), Any( xGraphic ) );
204 0 : xShapes->remove( xShape );
205 0 : xPropSet2->setPropertyValue( TKGet( TK_ZOrder ), Any( j ) );
206 0 : }
207 0 : }
208 : }
209 0 : }
210 0 : }
211 : }
212 0 : catch( Exception& )
213 : {
214 : }
215 0 : }
216 :
217 0 : void ImpCompressGraphic( Reference< XGraphicProvider >& rxGraphicProvider, const Reference< XGraphic >& rxGraphic, Reference< XOutputStream >& rxOutputStream,
218 : const OUString& rDestMimeType, const awt::Size& rLogicalSize, sal_Int32 nJPEGQuality, sal_Int32 nImageResolution, sal_Bool bRemoveCropping, const text::GraphicCrop& rGraphicCropLogic )
219 : {
220 : try
221 : {
222 0 : if ( rxGraphicProvider.is() && rxOutputStream.is() )
223 : {
224 0 : Sequence< PropertyValue > aFilterData( 8 );
225 0 : aFilterData[ 0 ].Name = TKGet( TK_ImageResolution );
226 0 : aFilterData[ 0 ].Value <<= nImageResolution;
227 0 : aFilterData[ 1 ].Name = TKGet( TK_ColorMode ); // todo: jpeg color mode (0->true color, 1->greyscale)
228 0 : aFilterData[ 1 ].Value <<= (sal_Int32)0;
229 0 : aFilterData[ 2 ].Name = TKGet( TK_Quality ); // quality that is used if we export to jpeg
230 0 : aFilterData[ 2 ].Value <<= nJPEGQuality;
231 0 : aFilterData[ 3 ].Name = TKGet( TK_Compression ); // compression that is used if we export to png
232 0 : aFilterData[ 3 ].Value <<= (sal_Int32)6;
233 0 : aFilterData[ 4 ].Name = TKGet( TK_Interlaced ); // interlaced is turned off if we export to png
234 0 : aFilterData[ 4 ].Value <<= (sal_Int32)0;
235 0 : aFilterData[ 5 ].Name = TKGet( TK_LogicalSize );
236 0 : aFilterData[ 5 ].Value <<= rLogicalSize;
237 0 : aFilterData[ 6 ].Name = TKGet( TK_RemoveCropArea );
238 0 : aFilterData[ 6 ].Value <<= bRemoveCropping;
239 0 : aFilterData[ 7 ].Name = TKGet( TK_GraphicCropLogic );
240 0 : aFilterData[ 7 ].Value <<= rGraphicCropLogic;
241 :
242 0 : Sequence< PropertyValue > aArgs( 3 );
243 0 : aArgs[ 0 ].Name = TKGet( TK_MimeType ); // the GraphicProvider is using "MimeType", the GraphicExporter "MediaType"...
244 0 : aArgs[ 0 ].Value <<= rDestMimeType;
245 0 : aArgs[ 1 ].Name = TKGet( TK_OutputStream );
246 0 : aArgs[ 1 ].Value <<= rxOutputStream;
247 0 : aArgs[ 2 ].Name = TKGet( TK_FilterData );
248 0 : aArgs[ 2 ].Value <<= aFilterData;
249 :
250 0 : rxGraphicProvider->storeGraphic( rxGraphic, aArgs );
251 : }
252 : }
253 0 : catch( Exception& )
254 : {
255 : }
256 0 : }
257 :
258 0 : Reference< XGraphic > ImpCompressGraphic( const Reference< XComponentContext >& rxMSF,
259 : const Reference< XGraphic >& xGraphic, const awt::Size& aLogicalSize, const text::GraphicCrop& aGraphicCropLogic,
260 : const GraphicSettings& rGraphicSettings )
261 : {
262 0 : Reference< XGraphic > xNewGraphic;
263 : try
264 : {
265 0 : OUString aSourceMimeType;
266 0 : Reference< XPropertySet > xGraphicPropertySet( xGraphic, UNO_QUERY_THROW );
267 0 : if ( xGraphicPropertySet->getPropertyValue( TKGet( TK_MimeType ) ) >>= aSourceMimeType )
268 : {
269 0 : sal_Int8 nGraphicType( xGraphic->getType() );
270 0 : if ( nGraphicType == com::sun::star::graphic::GraphicType::PIXEL )
271 : {
272 0 : sal_Bool bTransparent = sal_False;
273 0 : sal_Bool bAlpha = sal_False;
274 0 : sal_Bool bAnimated = sal_False;
275 :
276 0 : awt::Size aSourceSizePixel( 0, 0 );
277 0 : text::GraphicCrop aGraphicCropPixel( 0, 0, 0, 0 );
278 :
279 0 : if ( ( xGraphicPropertySet->getPropertyValue( TKGet( TK_SizePixel ) ) >>= aSourceSizePixel ) &&
280 0 : ( xGraphicPropertySet->getPropertyValue( TKGet( TK_Transparent ) ) >>= bTransparent ) &&
281 0 : ( xGraphicPropertySet->getPropertyValue( TKGet( TK_Alpha ) ) >>= bAlpha ) &&
282 0 : ( xGraphicPropertySet->getPropertyValue( TKGet( TK_Animated ) ) >>= bAnimated ) )
283 : {
284 0 : awt::Size aDestSizePixel( aSourceSizePixel );
285 0 : if ( !bAnimated )
286 : {
287 0 : sal_Bool bNeedsOptimizing = sal_False;
288 0 : sal_Bool bRemoveCropArea( rGraphicSettings.mbRemoveCropArea );
289 :
290 : // cropping has to be removed from SourceSizePixel
291 0 : if ( aGraphicCropLogic.Left || aGraphicCropLogic.Top || aGraphicCropLogic.Right || aGraphicCropLogic.Bottom )
292 : {
293 0 : const awt::Size aSize100thMM( GraphicCollector::GetOriginalSize( rxMSF, xGraphic ) );
294 :
295 0 : if ( bRemoveCropArea )
296 0 : bNeedsOptimizing = sal_True;
297 :
298 0 : if ( aSize100thMM.Width && aSize100thMM.Height )
299 : {
300 0 : aGraphicCropPixel.Left = static_cast< sal_Int32 >( ( (double)aSourceSizePixel.Width * aGraphicCropLogic.Left ) / aSize100thMM.Width );
301 0 : aGraphicCropPixel.Top = static_cast< sal_Int32 >( ( (double)aSourceSizePixel.Height* aGraphicCropLogic.Top ) / aSize100thMM.Height );
302 0 : aGraphicCropPixel.Right = static_cast< sal_Int32 >( ( (double)aSourceSizePixel.Width * ( aSize100thMM.Width - aGraphicCropLogic.Right ) ) / aSize100thMM.Width );
303 0 : aGraphicCropPixel.Bottom = static_cast< sal_Int32 >( ( (double)aSourceSizePixel.Height* ( aSize100thMM.Height - aGraphicCropLogic.Bottom ) ) / aSize100thMM.Height );
304 :
305 : // first calculating new SourceSizePixel by removing the cropped area
306 0 : aSourceSizePixel.Width = aGraphicCropPixel.Right - aGraphicCropPixel.Left;
307 0 : aSourceSizePixel.Height= aGraphicCropPixel.Bottom - aGraphicCropPixel.Top;
308 : }
309 : else
310 : {
311 0 : bRemoveCropArea = sal_False;
312 : }
313 : }
314 0 : if ( ( aSourceSizePixel.Width > 0 ) && ( aSourceSizePixel.Height > 0 ) )
315 : {
316 0 : OUString aDestMimeType( "image/png" );
317 0 : if ( rGraphicSettings.mbJPEGCompression && !bTransparent && !bAlpha && !bAnimated )
318 : {
319 0 : aDestMimeType = OUString( "image/jpeg" );
320 : // if( aSourceMimeType != aDestMimeType )
321 0 : bNeedsOptimizing = sal_True;
322 : }
323 0 : if ( bRemoveCropArea )
324 0 : aDestSizePixel = aSourceSizePixel;
325 0 : if ( rGraphicSettings.mnImageResolution && aLogicalSize.Width && aLogicalSize.Height )
326 : {
327 0 : const double fSourceDPIX = ((double)aSourceSizePixel.Width / ((double)aLogicalSize.Width / 2540.0 ));
328 0 : const double fSourceDPIY = ((double)aSourceSizePixel.Height/ ((double)aLogicalSize.Height/ 2540.0 ));
329 :
330 : // check, if the bitmap DPI exceeds the maximum DPI
331 0 : if( ( fSourceDPIX > rGraphicSettings.mnImageResolution ) || ( fSourceDPIY > rGraphicSettings.mnImageResolution ) )
332 : {
333 0 : const double fNewSizePixelX = ((double)aDestSizePixel.Width * rGraphicSettings.mnImageResolution ) / fSourceDPIX;
334 0 : const double fNewSizePixelY = ((double)aDestSizePixel.Height* rGraphicSettings.mnImageResolution ) / fSourceDPIY;
335 :
336 0 : aDestSizePixel = awt::Size( (sal_Int32)fNewSizePixelX, (sal_Int32)fNewSizePixelY );
337 0 : bNeedsOptimizing = sal_True;
338 : }
339 : }
340 0 : if ( bNeedsOptimizing && aDestSizePixel.Width && aDestSizePixel.Height )
341 : {
342 0 : Reference< XStream > xTempFile( io::TempFile::create(rxMSF), UNO_QUERY_THROW );
343 0 : Reference< XOutputStream > xOutputStream( xTempFile->getOutputStream() );
344 0 : Reference< XGraphicProvider > xGraphicProvider( GraphicProvider::create( rxMSF ) );
345 :
346 0 : ImpCompressGraphic( xGraphicProvider, xGraphic, xOutputStream, aDestMimeType, aLogicalSize, rGraphicSettings.mnJPEGQuality, rGraphicSettings.mnImageResolution, bRemoveCropArea, aGraphicCropLogic );
347 0 : Reference< XInputStream > xInputStream( xTempFile->getInputStream() );
348 0 : Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY_THROW );
349 0 : xSeekable->seek( 0 );
350 0 : Sequence< PropertyValue > aArgs( 1 );
351 0 : aArgs[ 0 ].Name = TKGet( TK_InputStream );
352 0 : aArgs[ 0 ].Value <<= xInputStream;
353 0 : xNewGraphic = xGraphicProvider->queryGraphic( aArgs );
354 0 : }
355 : }
356 : }
357 : }
358 : }
359 : else // this is a metafile
360 : {
361 0 : rtl::OUString aDestMimeType( aSourceMimeType );
362 0 : Reference< XStream > xTempFile( io::TempFile::create(rxMSF), UNO_QUERY_THROW );
363 0 : Reference< XOutputStream > xOutputStream( xTempFile->getOutputStream() );
364 0 : Reference< XGraphicProvider > xGraphicProvider( GraphicProvider::create( rxMSF ) );
365 0 : ImpCompressGraphic( xGraphicProvider, xGraphic, xOutputStream, aDestMimeType, aLogicalSize, rGraphicSettings.mnJPEGQuality, rGraphicSettings.mnImageResolution, sal_False, aGraphicCropLogic );
366 0 : Reference< XInputStream > xInputStream( xTempFile->getInputStream() );
367 0 : Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY_THROW );
368 0 : xSeekable->seek( 0 );
369 0 : Sequence< PropertyValue > aArgs( 1 );
370 0 : aArgs[ 0 ].Name = TKGet( TK_InputStream );
371 0 : aArgs[ 0 ].Value <<= xInputStream;
372 0 : xNewGraphic = xGraphicProvider->queryGraphic( aArgs );
373 : }
374 0 : }
375 : }
376 0 : catch( Exception& )
377 : {
378 : }
379 0 : return xNewGraphic;
380 : }
381 :
382 0 : void CompressGraphics( ImpOptimizer& rOptimizer, const Reference< XComponentContext >& rxMSF, const GraphicSettings& rGraphicSettings,
383 : std::vector< GraphicCollector::GraphicEntity >& rGraphicList )
384 : {
385 : try
386 : {
387 0 : std::vector< GraphicCollector::GraphicEntity >::iterator aGraphicIter( rGraphicList.begin() );
388 0 : std::vector< GraphicCollector::GraphicEntity >::iterator aGraphicIEnd( rGraphicList.end() );
389 0 : double i = 0;
390 0 : while( aGraphicIter != aGraphicIEnd )
391 : {
392 0 : i++;
393 0 : sal_Int32 nProgress = static_cast< sal_Int32 >( 40.0 * ( i / static_cast< double >( rGraphicList.size() ) ) ) + 50;
394 0 : rOptimizer.SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( nProgress ) ) );
395 0 : rOptimizer.DispatchStatus();
396 :
397 0 : if ( aGraphicIter->maUser.size() )
398 : {
399 0 : GraphicSettings aGraphicSettings( rGraphicSettings );
400 0 : aGraphicSettings.mbRemoveCropArea = aGraphicIter->mbRemoveCropArea;
401 :
402 0 : Reference< XGraphic > xGraphic;
403 0 : if ( aGraphicIter->maUser[ 0 ].mbFillBitmap && aGraphicIter->maUser[ 0 ].mxPropertySet.is() )
404 : {
405 0 : Reference< XBitmap > xFillBitmap;
406 0 : if ( aGraphicIter->maUser[ 0 ].mxPropertySet->getPropertyValue( TKGet( TK_FillBitmap ) ) >>= xFillBitmap )
407 0 : xGraphic = Reference< XGraphic >( xFillBitmap, UNO_QUERY_THROW );
408 : }
409 0 : else if ( aGraphicIter->maUser[ 0 ].mxShape.is() )
410 : {
411 0 : Reference< XPropertySet > xShapePropertySet( aGraphicIter->maUser[ 0 ].mxShape, UNO_QUERY_THROW );
412 0 : xShapePropertySet->getPropertyValue( TKGet( TK_Graphic ) ) >>= xGraphic;
413 : }
414 0 : if ( xGraphic.is() )
415 : {
416 0 : Reference< XPropertySet > xNewGraphicPropertySet( xGraphic, UNO_QUERY_THROW );
417 0 : awt::Size aSize100thMM( GraphicCollector::GetOriginalSize( rxMSF, xGraphic ) );
418 0 : Reference< XGraphic > xNewGraphic( ImpCompressGraphic( rxMSF, xGraphic, aGraphicIter->maLogicalSize, aGraphicIter->maGraphicCropLogic, aGraphicSettings ) );
419 0 : if ( xNewGraphic.is() )
420 : {
421 : // applying graphic to each user
422 0 : std::vector< GraphicCollector::GraphicUser >::iterator aGraphicUserIter( aGraphicIter->maUser.begin() );
423 0 : while( aGraphicUserIter != aGraphicIter->maUser.end() )
424 : {
425 0 : if ( aGraphicUserIter->mxShape.is() )
426 : {
427 0 : rtl::OUString sEmptyGraphicURL;
428 0 : Reference< XPropertySet > xShapePropertySet( aGraphicUserIter->mxShape, UNO_QUERY_THROW );
429 0 : xShapePropertySet->setPropertyValue( TKGet( TK_GraphicURL ), Any( sEmptyGraphicURL ) );
430 0 : xShapePropertySet->setPropertyValue( TKGet( TK_Graphic ), Any( xNewGraphic ) );
431 :
432 0 : if ( aGraphicUserIter->maGraphicCropLogic.Left || aGraphicUserIter->maGraphicCropLogic.Top
433 0 : || aGraphicUserIter->maGraphicCropLogic.Right || aGraphicUserIter->maGraphicCropLogic.Bottom )
434 : { // removing crop area was not possible or should't been applied
435 0 : text::GraphicCrop aGraphicCropLogic( 0, 0, 0, 0 );
436 0 : if ( !aGraphicSettings.mbRemoveCropArea )
437 : {
438 0 : awt::Size aNewSize( GraphicCollector::GetOriginalSize( rxMSF, xNewGraphic ) );
439 0 : aGraphicCropLogic.Left = (sal_Int32)((double)aGraphicUserIter->maGraphicCropLogic.Left * ((double)aNewSize.Width / (double)aSize100thMM.Width));
440 0 : aGraphicCropLogic.Top = (sal_Int32)((double)aGraphicUserIter->maGraphicCropLogic.Top * ((double)aNewSize.Height / (double)aSize100thMM.Height));
441 0 : aGraphicCropLogic.Right = (sal_Int32)((double)aGraphicUserIter->maGraphicCropLogic.Right * ((double)aNewSize.Width / (double)aSize100thMM.Width));
442 0 : aGraphicCropLogic.Bottom = (sal_Int32)((double)aGraphicUserIter->maGraphicCropLogic.Bottom * ((double)aNewSize.Height / (double)aSize100thMM.Height));
443 : }
444 0 : xShapePropertySet->setPropertyValue( TKGet( TK_GraphicCrop ), Any( aGraphicCropLogic ) );
445 0 : }
446 : }
447 0 : else if ( aGraphicUserIter->mxPropertySet.is() )
448 : {
449 0 : Reference< XBitmap > xFillBitmap( xNewGraphic, UNO_QUERY );
450 0 : if ( xFillBitmap.is() )
451 : {
452 0 : awt::Size aSize;
453 : sal_Bool bLogicalSize;
454 :
455 0 : Reference< XPropertySet >& rxPropertySet( aGraphicUserIter->mxPropertySet );
456 0 : rxPropertySet->setPropertyValue( TKGet( TK_FillBitmap ), Any( xFillBitmap ) );
457 0 : if ( ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapLogicalSize ) ) >>= bLogicalSize )
458 0 : && ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapSizeX ) ) >>= aSize.Width )
459 0 : && ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapSizeY ) ) >>= aSize.Height ) )
460 : {
461 0 : if ( !aSize.Width || !aSize.Height )
462 : {
463 0 : rxPropertySet->setPropertyValue( TKGet( TK_FillBitmapLogicalSize ), Any( sal_True ) );
464 0 : rxPropertySet->setPropertyValue( TKGet( TK_FillBitmapSizeX ), Any( aGraphicUserIter->maLogicalSize.Width ) );
465 0 : rxPropertySet->setPropertyValue( TKGet( TK_FillBitmapSizeY ), Any( aGraphicUserIter->maLogicalSize.Height ) );
466 : }
467 : }
468 0 : if ( aGraphicUserIter->mxPagePropertySet.is() )
469 0 : aGraphicUserIter->mxPagePropertySet->setPropertyValue( TKGet( TK_Background ), Any( rxPropertySet ) );
470 0 : }
471 : }
472 0 : ++aGraphicUserIter;
473 : }
474 0 : }
475 0 : }
476 : }
477 0 : ++aGraphicIter;
478 : }
479 : }
480 0 : catch ( Exception& )
481 : {
482 : }
483 0 : }
484 :
485 : // ----------------
486 : // - ImpOptimizer -
487 : // ----------------
488 :
489 0 : ImpOptimizer::ImpOptimizer( const Reference< XComponentContext >& rxMSF, const Reference< XModel >& rxModel ) :
490 : mxMSF ( rxMSF ),
491 : mxModel ( rxModel ),
492 : mbJPEGCompression ( sal_False ),
493 : mnJPEGQuality ( 90 ),
494 : mbRemoveCropArea ( sal_False ),
495 : mnImageResolution ( 0 ),
496 : mbEmbedLinkedGraphics ( sal_True ),
497 : mbOLEOptimization ( sal_False ),
498 : mnOLEOptimizationType ( 0 ),
499 : mbDeleteUnusedMasterPages ( sal_False ),
500 : mbDeleteHiddenSlides ( sal_False ),
501 : mbDeleteNotesPages ( sal_False ),
502 0 : mbOpenNewDocument ( sal_True )
503 : {
504 0 : }
505 :
506 : // -----------------------------------------------------------------------------
507 :
508 0 : ImpOptimizer::~ImpOptimizer()
509 : {
510 0 : }
511 :
512 : // -----------------------------------------------------------------------------
513 :
514 0 : void ImpOptimizer::DispatchStatus()
515 : {
516 0 : if ( mxStatusDispatcher.is() )
517 : {
518 0 : URL aURL;
519 0 : aURL.Protocol = OUString( "vnd.com.sun.star.comp.SunPresentationMinimizer:" );
520 0 : aURL.Path = OUString( "statusupdate" );
521 0 : mxStatusDispatcher->dispatch( aURL, GetStatusSequence() );
522 : }
523 0 : }
524 :
525 : // -----------------------------------------------------------------------------
526 :
527 0 : sal_Bool ImpOptimizer::Optimize()
528 : {
529 :
530 0 : if ( !maCustomShowName.isEmpty() )
531 0 : ImpExtractCustomShow( mxModel, maCustomShowName );
532 :
533 0 : if ( mbDeleteUnusedMasterPages )
534 : {
535 0 : SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 40 ) ) );
536 0 : SetStatusValue( TK_Status, Any( TKGet( STR_DELETING_SLIDES ) ) );
537 0 : DispatchStatus();
538 0 : ImpDeleteUnusedMasterPages( mxModel );
539 : }
540 :
541 0 : if ( mbDeleteHiddenSlides )
542 : {
543 0 : SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 40 ) ) );
544 0 : SetStatusValue( TK_Status, Any( TKGet( STR_DELETING_SLIDES ) ) );
545 0 : DispatchStatus();
546 0 : ImpDeleteHiddenSlides( mxModel );
547 : }
548 :
549 0 : if ( mbDeleteNotesPages )
550 : {
551 0 : SetStatusValue( TK_Status, Any( TKGet( STR_DELETING_SLIDES ) ) );
552 0 : DispatchStatus();
553 0 : ImpDeleteNotesPages( mxModel );
554 : }
555 :
556 0 : if ( mbOLEOptimization )
557 : {
558 0 : SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 45 ) ) );
559 0 : SetStatusValue( TK_Status, Any( TKGet( STR_CREATING_OLE_REPLACEMENTS ) ) );
560 0 : DispatchStatus();
561 0 : ImpConvertOLE( mxModel, mnOLEOptimizationType );
562 : }
563 :
564 0 : if ( mbJPEGCompression || mbRemoveCropArea || mnImageResolution )
565 : {
566 0 : SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 50 ) ) );
567 0 : SetStatusValue( TK_Status, Any( TKGet( STR_OPTIMIZING_GRAPHICS ) ) );
568 0 : DispatchStatus();
569 :
570 0 : std::vector< GraphicCollector::GraphicEntity > aGraphicList;
571 0 : GraphicSettings aGraphicSettings( mbJPEGCompression, mnJPEGQuality, mbRemoveCropArea, mnImageResolution, mbEmbedLinkedGraphics );
572 0 : GraphicCollector::CollectGraphics( mxMSF, mxModel, aGraphicSettings, aGraphicList );
573 0 : CompressGraphics( *this, mxMSF, aGraphicSettings, aGraphicList );
574 : }
575 0 : SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 100 ) ) );
576 0 : DispatchStatus();
577 0 : return sal_True;
578 : }
579 :
580 0 : static void DispatchURL( Reference< XComponentContext > xContext, OUString sURL, Reference< XFrame > xFrame )
581 : {
582 : try
583 : {
584 0 : Reference< XURLTransformer > xURLTransformer( URLTransformer::create(xContext) );
585 0 : util::URL aUrl;
586 0 : aUrl.Complete = sURL;
587 0 : xURLTransformer->parseStrict( aUrl );
588 0 : Sequence< PropertyValue > aArgs;
589 0 : Reference< XDispatchProvider > xDispatchProvider( xFrame, UNO_QUERY_THROW );
590 0 : Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aUrl, OUString(), 0 ); // "_self"
591 0 : if ( xDispatch.is() )
592 0 : xDispatch->dispatch( aUrl, aArgs );
593 : }
594 0 : catch( Exception& )
595 : {
596 : }
597 0 : }
598 :
599 : // -----------------------------------------------------------------------------
600 :
601 0 : sal_Bool ImpOptimizer::Optimize( const Sequence< PropertyValue >& rArguments )
602 : {
603 0 : sal_Bool bRet = sal_True;
604 :
605 0 : if ( mxModel.is() )
606 : {
607 0 : sal_Int64 nEstimatedFileSize = 0;
608 0 : SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 0 ) ) );
609 0 : DispatchStatus();
610 :
611 : int i, nICount;
612 0 : for ( i = 0, nICount = rArguments.getLength(); i < nICount; i++ )
613 : {
614 0 : switch( TKGet( rArguments[ i ].Name ) )
615 : {
616 0 : case TK_StatusDispatcher : rArguments[ i ].Value >>= mxStatusDispatcher; break;
617 0 : case TK_InformationDialog: rArguments[ i ].Value >>= mxInformationDialog; break;
618 : case TK_Settings :
619 : {
620 0 : com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue > aSettings;
621 : int j, nJCount;
622 0 : rArguments[ i ].Value >>= aSettings;
623 0 : for ( j = 0, nJCount = aSettings.getLength(); j < nJCount; j++ )
624 : {
625 0 : switch( TKGet( aSettings[ j ].Name ) )
626 : {
627 0 : case TK_JPEGCompression : aSettings[ j ].Value >>= mbJPEGCompression; break;
628 0 : case TK_JPEGQuality : aSettings[ j ].Value >>= mnJPEGQuality; break;
629 0 : case TK_RemoveCropArea : aSettings[ j ].Value >>= mbRemoveCropArea; break;
630 0 : case TK_ImageResolution : aSettings[ j ].Value >>= mnImageResolution; break;
631 0 : case TK_EmbedLinkedGraphics : aSettings[ j ].Value >>= mbEmbedLinkedGraphics; break;
632 0 : case TK_OLEOptimization : aSettings[ j ].Value >>= mbOLEOptimization; break;
633 0 : case TK_OLEOptimizationType : aSettings[ j ].Value >>= mnOLEOptimizationType; break;
634 0 : case TK_CustomShowName : aSettings[ j ].Value >>= maCustomShowName; break;
635 0 : case TK_DeleteUnusedMasterPages : aSettings[ j ].Value >>= mbDeleteUnusedMasterPages; break;
636 0 : case TK_DeleteHiddenSlides : aSettings[ j ].Value >>= mbDeleteHiddenSlides; break;
637 0 : case TK_DeleteNotesPages : aSettings[ j ].Value >>= mbDeleteNotesPages; break;
638 0 : case TK_SaveAsURL : aSettings[ j ].Value >>= maSaveAsURL; break;
639 0 : case TK_FilterName : aSettings[ j ].Value >>= maFilterName; break;
640 0 : case TK_OpenNewDocument : aSettings[ j ].Value >>= mbOpenNewDocument; break;
641 0 : case TK_EstimatedFileSize : aSettings[ j ].Value >>= nEstimatedFileSize; break;
642 0 : default: break;
643 : }
644 0 : }
645 : }
646 0 : break;
647 0 : default: break;
648 : }
649 : }
650 :
651 0 : sal_Int64 nSourceSize = 0;
652 0 : sal_Int64 nDestSize = 0;
653 :
654 0 : Reference< XFrame > xSelf;
655 0 : if ( !maSaveAsURL.isEmpty() )
656 : {
657 :
658 0 : SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 10 ) ) );
659 0 : SetStatusValue( TK_Status, Any( TKGet( STR_DUPLICATING_PRESENTATION ) ) );
660 0 : DispatchStatus();
661 :
662 0 : Reference< XStorable >xStorable( mxModel, UNO_QUERY );
663 0 : if ( xStorable.is() )
664 : {
665 0 : if ( xStorable->hasLocation() )
666 0 : nSourceSize = PPPOptimizer::GetFileSize( xStorable->getLocation() );
667 :
668 0 : Sequence< PropertyValue > aArguments;
669 0 : if ( !maFilterName.isEmpty() )
670 : {
671 0 : int nLength = aArguments.getLength();
672 0 : aArguments.realloc( nLength + 1 );
673 0 : aArguments[ nLength ].Name = TKGet( TK_FilterName );
674 0 : aArguments[ nLength ].Value <<= maFilterName;
675 : }
676 0 : xStorable->storeToURL( maSaveAsURL, aArguments );
677 0 : if ( !nSourceSize )
678 0 : nSourceSize = PPPOptimizer::GetFileSize( maSaveAsURL );
679 :
680 0 : SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 30 ) ) );
681 0 : SetStatusValue( TK_Status, Any( TKGet( STR_DUPLICATING_PRESENTATION ) ) );
682 0 : DispatchStatus();
683 :
684 0 : Reference< XDesktop2 > xDesktop = Desktop::create( mxMSF );
685 0 : xSelf = xDesktop->findFrame( TKGet( TK__blank ), FrameSearchFlag::CREATE );
686 0 : Reference< XComponentLoader > xComponentLoader( xSelf, UNO_QUERY );
687 :
688 0 : Sequence< PropertyValue > aLoadProps( 1 );
689 0 : aLoadProps[ 0 ].Name = TKGet( TK_Hidden );
690 0 : aLoadProps[ 0 ].Value <<= (sal_Bool)( sal_True );
691 0 : mxModel = Reference< XModel >( xComponentLoader->loadComponentFromURL(
692 0 : maSaveAsURL, TKGet( TK__self ), 0, aLoadProps ), UNO_QUERY );
693 0 : }
694 : }
695 :
696 : // check if the document is ReadOnly -> error
697 0 : Reference< XStorable > xStorable( mxModel, UNO_QUERY );
698 0 : if ( xStorable.is() && !xStorable->isReadonly() )
699 : {
700 0 : mxModel->lockControllers();
701 0 : bRet = Optimize();
702 0 : mxModel->unlockControllers();
703 :
704 : // clearing undo stack:
705 0 : Reference< XFrame > xFrame( xSelf.is() ? xSelf : mxInformationDialog );
706 0 : if ( xFrame.is() )
707 : {
708 0 : const OUString sSlot( "slot:27115" );
709 0 : DispatchURL( mxMSF, sSlot, xFrame );
710 0 : }
711 : }
712 :
713 0 : if ( !maSaveAsURL.isEmpty() )
714 : {
715 0 : if ( xStorable.is() )
716 : {
717 0 : xStorable->store();
718 0 : nDestSize = PPPOptimizer::GetFileSize( maSaveAsURL );
719 : }
720 : }
721 :
722 0 : if ( mxInformationDialog.is() )
723 : {
724 0 : InformationDialog aInformationDialog( mxMSF, mxInformationDialog, maSaveAsURL, mbOpenNewDocument, nSourceSize, nDestSize, nEstimatedFileSize );
725 0 : aInformationDialog.execute();
726 0 : SetStatusValue( TK_OpenNewDocument, Any( mbOpenNewDocument ) );
727 0 : DispatchStatus();
728 : }
729 :
730 0 : if ( !maSaveAsURL.isEmpty() )
731 : {
732 0 : if ( mbOpenNewDocument && xSelf.is() )
733 : {
734 0 : Reference< awt::XWindow > xContainerWindow( xSelf->getContainerWindow() );
735 0 : xContainerWindow->setVisible( sal_True );
736 : }
737 : else
738 : {
739 0 : Reference< XComponent > xComponent( mxModel, UNO_QUERY );
740 0 : xComponent->dispose();
741 : }
742 : }
743 0 : if ( nSourceSize && nDestSize )
744 : {
745 0 : SetStatusValue( TK_FileSizeSource, Any( nSourceSize ) );
746 0 : SetStatusValue( TK_FileSizeDestination, Any( nDestSize ) );
747 0 : DispatchStatus();
748 0 : }
749 : }
750 : else
751 0 : bRet = sal_False;
752 0 : return bRet;
753 : }
754 :
755 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|