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