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