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/document/XFilter.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/graphic/GraphicType.hpp>
47 #include <com/sun/star/io/XStream.hpp>
48 #include <com/sun/star/io/XSeekable.hpp>
49 #include <com/sun/star/io/TempFile.hpp>
50 #include <com/sun/star/frame/XComponentLoader.hpp>
51 #include <com/sun/star/util/URLTransformer.hpp>
53 using namespace ::
std;
68 static void ImpExtractCustomShow(
const Reference< XModel >& rxModel, std::u16string_view rCustomShowName )
70 vector< Reference< XDrawPage > > vNonUsedPageList;
74 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
75 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
76 for(
const auto& rxPage : vNonUsedPageList )
77 xDrawPages->remove( rxPage );
87 vector< PageCollector::MasterPageEntity > aMasterPageList;
91 Reference< XMasterPagesSupplier > xMasterPagesSupplier( rxModel, UNO_QUERY_THROW );
92 Reference< XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), UNO_SET_THROW );
93 for(
const auto& rMasterPage : aMasterPageList )
95 if ( !rMasterPage.bUsed )
96 xMasterPages->remove( rMasterPage.xMasterPage );
104 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
105 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
106 for( sal_Int32
i = 0;
i < xDrawPages->getCount();
i++ )
108 Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex(
i ), UNO_QUERY_THROW );
112 if ( xPropSet->getPropertyValue(
"Visible" ) >>= bVisible )
116 xDrawPages->remove( xDrawPage );
131 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
132 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
133 sal_Int32
i, nPages = xDrawPages->getCount();
134 for( i = 0; i < nPages; i++ )
136 Reference< XPresentationPage > xPresentationPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
138 Reference< XShapes > xShapes( xPropSet, UNO_QUERY_THROW );
139 while( xShapes->getCount() )
140 xShapes->remove( Reference< XShape >( xShapes->getByIndex( xShapes->getCount() - 1 ), UNO_QUERY_THROW ) );
142 xPropSet->setPropertyValue(
"Layout",
Any( sal_Int16(21) ) );
150 static void ImpConvertOLE(
const Reference< XModel >& rxModel, sal_Int32 nOLEOptimizationType )
154 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
155 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
156 for ( sal_Int32
i = 0;
i < xDrawPages->getCount();
i++ )
158 Reference< XShapes > xShapes( xDrawPages->getByIndex(
i ), UNO_QUERY_THROW );
159 for ( sal_Int32 j = 0; j < xShapes->getCount(); j++ )
161 Reference< XShape > xShape( xShapes->getByIndex( j ), UNO_QUERY_THROW );
162 if ( xShape->getShapeType() ==
"com.sun.star.drawing.OLE2Shape" )
166 bool bConvertOLE = nOLEOptimizationType == 0;
167 if ( nOLEOptimizationType == 1 )
169 bool bIsInternal =
true;
170 xPropSet->getPropertyValue(
"IsInternal" ) >>= bIsInternal;
171 bConvertOLE = !bIsInternal;
176 if ( xPropSet->getPropertyValue(
"Graphic" ) >>= xGraphic )
178 Reference< XMultiServiceFactory > xFact( rxModel, UNO_QUERY_THROW );
179 Reference< XShape > xShape2( xFact->createInstance(
"com.sun.star.drawing.GraphicObjectShape" ), UNO_QUERY_THROW );
180 xShapes->add( xShape2 );
181 xShape2->setPosition( xShape->getPosition() );
182 xShape2->setSize( xShape->getSize() );
184 xPropSet2->setPropertyValue(
"Graphic",
Any( xGraphic ) );
185 xShapes->remove( xShape );
186 xPropSet2->setPropertyValue(
"ZOrder",
Any( j ) );
198 static void ImpCompressGraphic( Reference< XGraphicProvider >
const & rxGraphicProvider,
const Reference< XGraphic >& rxGraphic, Reference< XOutputStream >
const & rxOutputStream,
199 const OUString& rDestMimeType,
const awt::Size& rLogicalSize, sal_Int32 nJPEGQuality, sal_Int32 nImageResolution,
bool bRemoveCropping,
const text::GraphicCrop& rGraphicCropLogic )
203 if ( rxGraphicProvider.is() && rxOutputStream.is() )
205 Sequence< PropertyValue > aFilterData( 8 );
206 aFilterData[ 0 ].Name =
"ImageResolution";
207 aFilterData[ 0 ].Value <<= nImageResolution;
208 aFilterData[ 1 ].Name =
"ColorMode";
209 aFilterData[ 1 ].Value <<= sal_Int32(0);
210 aFilterData[ 2 ].Name =
"Quality";
211 aFilterData[ 2 ].Value <<= nJPEGQuality;
212 aFilterData[ 3 ].Name =
"Compression";
213 aFilterData[ 3 ].Value <<= sal_Int32(6);
214 aFilterData[ 4 ].Name =
"Interlaced";
215 aFilterData[ 4 ].Value <<= sal_Int32(0);
216 aFilterData[ 5 ].Name =
"LogicalSize";
217 aFilterData[ 5 ].Value <<= rLogicalSize;
218 aFilterData[ 6 ].Name =
"RemoveCropArea";
219 aFilterData[ 6 ].Value <<= bRemoveCropping;
220 aFilterData[ 7 ].Name =
"GraphicCropLogic";
221 aFilterData[ 7 ].Value <<= rGraphicCropLogic;
223 Sequence< PropertyValue > aArgs( 3 );
224 aArgs[ 0 ].Name =
"MimeType";
225 aArgs[ 0 ].Value <<= rDestMimeType;
226 aArgs[ 1 ].Name =
"OutputStream";
227 aArgs[ 1 ].Value <<= rxOutputStream;
228 aArgs[ 2 ].Name =
"FilterData";
229 aArgs[ 2 ].Value <<= aFilterData;
231 rxGraphicProvider->storeGraphic( rxGraphic, aArgs );
240 const Reference< XGraphic >& xGraphic,
const awt::Size& aLogicalSize,
const text::GraphicCrop& aGraphicCropLogic,
243 Reference< XGraphic > xNewGraphic;
246 OUString aSourceMimeType;
248 if ( xGraphicPropertySet->getPropertyValue(
"MimeType" ) >>= aSourceMimeType )
250 sal_Int8 nGraphicType( xGraphic->getType() );
251 if ( nGraphicType == css::graphic::GraphicType::PIXEL )
253 bool bTransparent =
false;
255 bool bAnimated =
false;
257 awt::Size aSourceSizePixel( 0, 0 );
258 text::GraphicCrop aGraphicCropPixel( 0, 0, 0, 0 );
260 if ( ( xGraphicPropertySet->getPropertyValue(
"SizePixel" ) >>= aSourceSizePixel ) &&
261 ( xGraphicPropertySet->getPropertyValue(
"Transparent" ) >>= bTransparent ) &&
262 ( xGraphicPropertySet->getPropertyValue(
"Alpha" ) >>= bAlpha ) &&
263 ( xGraphicPropertySet->getPropertyValue(
"Animated" ) >>= bAnimated ) )
265 awt::Size aDestSizePixel( aSourceSizePixel );
268 bool bNeedsOptimizing =
false;
272 if ( aGraphicCropLogic.Left || aGraphicCropLogic.Top || aGraphicCropLogic.Right || aGraphicCropLogic.Bottom )
276 if ( bRemoveCropArea )
277 bNeedsOptimizing =
true;
279 if ( aSize100thMM.Width && aSize100thMM.Height )
281 aGraphicCropPixel.Left =
static_cast< sal_Int32
>( (
static_cast<double>(aSourceSizePixel.Width) * aGraphicCropLogic.Left ) / aSize100thMM.Width );
282 aGraphicCropPixel.Top =
static_cast< sal_Int32
>( (
static_cast<double>(aSourceSizePixel.Height)* aGraphicCropLogic.Top ) / aSize100thMM.Height );
283 aGraphicCropPixel.Right =
static_cast< sal_Int32
>( (
static_cast<double>(aSourceSizePixel.Width) * ( aSize100thMM.Width - aGraphicCropLogic.Right ) ) / aSize100thMM.Width );
284 aGraphicCropPixel.Bottom =
static_cast< sal_Int32
>( (
static_cast<double>(aSourceSizePixel.Height)* ( aSize100thMM.Height - aGraphicCropLogic.Bottom ) ) / aSize100thMM.Height );
287 aSourceSizePixel.Width = aGraphicCropPixel.Right - aGraphicCropPixel.Left;
288 aSourceSizePixel.Height= aGraphicCropPixel.Bottom - aGraphicCropPixel.Top;
292 bRemoveCropArea =
false;
295 if ( ( aSourceSizePixel.Width > 0 ) && ( aSourceSizePixel.Height > 0 ) )
297 OUString aDestMimeType(
"image/png" );
300 aDestMimeType =
"image/jpeg";
302 bNeedsOptimizing =
true;
304 if ( bRemoveCropArea )
305 aDestSizePixel = aSourceSizePixel;
306 if ( rGraphicSettings.
mnImageResolution && aLogicalSize.Width && aLogicalSize.Height )
308 const double fSourceDPIX =
static_cast<double>(aSourceSizePixel.Width) / (static_cast<double>(aLogicalSize.Width) / 2540.0 );
309 const double fSourceDPIY =
static_cast<double>(aSourceSizePixel.Height)/ (static_cast<double>(aLogicalSize.Height)/ 2540.0 );
314 const double fNewSizePixelX = (
static_cast<double>(aDestSizePixel.Width) * rGraphicSettings.
mnImageResolution ) / fSourceDPIX;
315 const double fNewSizePixelY = (
static_cast<double>(aDestSizePixel.Height)* rGraphicSettings.
mnImageResolution ) / fSourceDPIY;
317 aDestSizePixel = awt::Size( static_cast<sal_Int32>(fNewSizePixelX), static_cast<sal_Int32>(fNewSizePixelY) );
318 bNeedsOptimizing =
true;
321 if ( bNeedsOptimizing && aDestSizePixel.Width && aDestSizePixel.Height )
323 Reference< XStream > xTempFile( io::TempFile::create(rxContext), UNO_QUERY_THROW );
324 Reference< XOutputStream > xOutputStream( xTempFile->getOutputStream() );
325 Reference< XGraphicProvider > xGraphicProvider( GraphicProvider::create( rxContext ) );
329 Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY_THROW );
330 xSeekable->seek( 0 );
331 Sequence< PropertyValue > aArgs( 1 );
332 aArgs[ 0 ].Name =
"InputStream";
333 aArgs[ 0 ].Value <<= xInputStream;
334 xNewGraphic = xGraphicProvider->queryGraphic( aArgs );
342 const OUString& aDestMimeType( aSourceMimeType );
343 Reference< XStream > xTempFile( io::TempFile::create(rxContext), UNO_QUERY_THROW );
344 Reference< XOutputStream > xOutputStream( xTempFile->getOutputStream() );
345 Reference< XGraphicProvider > xGraphicProvider( GraphicProvider::create( rxContext ) );
348 Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY_THROW );
349 xSeekable->seek( 0 );
350 Sequence< PropertyValue > aArgs( 1 );
351 aArgs[ 0 ].Name =
"InputStream";
352 aArgs[ 0 ].Value <<= xInputStream;
353 xNewGraphic = xGraphicProvider->queryGraphic( aArgs );
364 std::vector< GraphicCollector::GraphicEntity >& rGraphicList )
369 for(
auto& rGraphic : rGraphicList )
372 sal_Int32 nProgress =
static_cast< sal_Int32
>( 40.0 * ( i /
static_cast< double >( rGraphicList.size() ) ) ) + 50;
376 if ( !rGraphic.maUser.empty() )
382 if ( rGraphic.maUser[ 0 ].mbFillBitmap && rGraphic.maUser[ 0 ].mxPropertySet.is() )
384 Reference< XBitmap > xFillBitmap;
385 if ( rGraphic.maUser[ 0 ].mxPropertySet->getPropertyValue(
"FillBitmap" ) >>= xFillBitmap )
386 xGraphic.set( xFillBitmap, UNO_QUERY_THROW );
388 else if ( rGraphic.maUser[ 0 ].mxShape.is() )
391 xShapePropertySet->getPropertyValue(
"Graphic" ) >>= xGraphic;
397 Reference< XGraphic > xNewGraphic(
ImpCompressGraphic( rxContext, xGraphic, rGraphic.maLogicalSize, rGraphic.maGraphicCropLogic, aGraphicSettings ) );
398 if ( xNewGraphic.is() )
401 for(
auto& rGraphicUser : rGraphic.maUser )
403 if ( rGraphicUser.mxShape.is() )
406 xShapePropertySet->setPropertyValue(
"Graphic",
Any( xNewGraphic ) );
408 if ( rGraphicUser.maGraphicCropLogic.Left || rGraphicUser.maGraphicCropLogic.Top
409 || rGraphicUser.maGraphicCropLogic.Right || rGraphicUser.maGraphicCropLogic.Bottom )
411 text::GraphicCrop aGraphicCropLogic( 0, 0, 0, 0 );
415 aGraphicCropLogic.Left =
static_cast<sal_Int32
>(
static_cast<double>(rGraphicUser.maGraphicCropLogic.Left) * (static_cast<double>(aNewSize.Width) /
static_cast<double>(aSize100thMM.Width)));
416 aGraphicCropLogic.Top =
static_cast<sal_Int32
>(
static_cast<double>(rGraphicUser.maGraphicCropLogic.Top) * (static_cast<double>(aNewSize.Height) /
static_cast<double>(aSize100thMM.Height)));
417 aGraphicCropLogic.Right =
static_cast<sal_Int32
>(
static_cast<double>(rGraphicUser.maGraphicCropLogic.Right) * (static_cast<double>(aNewSize.Width) /
static_cast<double>(aSize100thMM.Width)));
418 aGraphicCropLogic.Bottom =
static_cast<sal_Int32
>(
static_cast<double>(rGraphicUser.maGraphicCropLogic.Bottom) * (static_cast<double>(aNewSize.Height) /
static_cast<double>(aSize100thMM.Height)));
420 xShapePropertySet->setPropertyValue(
"GraphicCrop",
Any( aGraphicCropLogic ) );
423 else if ( rGraphicUser.mxPropertySet.is() )
425 Reference< XBitmap > xFillBitmap( xNewGraphic, UNO_QUERY );
426 if ( xFillBitmap.is() )
432 rxPropertySet->setPropertyValue(
"FillBitmap",
Any( xFillBitmap ) );
433 if ( ( rxPropertySet->getPropertyValue(
"FillBitmapLogicalSize" ) >>= bLogicalSize )
434 && ( rxPropertySet->getPropertyValue(
"FillBitmapSizeX" ) >>= aSize.Width )
435 && ( rxPropertySet->getPropertyValue(
"FillBitmapSizeY" ) >>= aSize.Height ) )
437 if ( !aSize.Width || !aSize.Height )
439 rxPropertySet->setPropertyValue(
"FillBitmapLogicalSize",
Any(
true ) );
440 rxPropertySet->setPropertyValue(
"FillBitmapSizeX",
Any( rGraphicUser.maLogicalSize.Width ) );
441 rxPropertySet->setPropertyValue(
"FillBitmapSizeY",
Any( rGraphicUser.maLogicalSize.Height ) );
444 if ( rGraphicUser.mxPagePropertySet.is() )
445 rGraphicUser.mxPagePropertySet->setPropertyValue(
"Background",
Any( rxPropertySet ) );
463 mbJPEGCompression ( false ),
464 mnJPEGQuality ( 90 ),
465 mbRemoveCropArea ( false ),
466 mnImageResolution ( 0 ),
467 mbEmbedLinkedGraphics ( true ),
468 mbOLEOptimization ( false ),
469 mnOLEOptimizationType ( 0 ),
470 mbDeleteUnusedMasterPages ( false ),
471 mbDeleteHiddenSlides ( false ),
472 mbDeleteNotesPages ( false ),
473 mbOpenNewDocument ( true )
488 aURL.Protocol =
"vnd.com.sun.star.comp.PresentationMinimizer:";
489 aURL.Path =
"statusupdate";
538 std::vector< GraphicCollector::GraphicEntity > aGraphicList;
551 Reference< XURLTransformer > xURLTransformer( URLTransformer::create(xContext) );
553 aUrl.Complete = sURL;
554 xURLTransformer->parseStrict( aUrl );
555 Sequence< PropertyValue > aArgs;
556 Reference< XDispatchProvider > xDispatchProvider( xFrame, UNO_QUERY_THROW );
557 Reference< XDispatch >
xDispatch = xDispatchProvider->queryDispatch( aUrl, OUString(), 0 );
558 if ( xDispatch.is() )
559 xDispatch->dispatch( aUrl, aArgs );
572 sal_Int64 nEstimatedFileSize = 0;
576 for (
const auto& rArgument : rArguments )
578 switch(
TKGet( rArgument.Name ) )
584 css::uno::Sequence< css::beans::PropertyValue > aSettings;
585 rArgument.Value >>= aSettings;
586 for (
const auto& rSetting : std::as_const(aSettings) )
588 switch(
TKGet( rSetting.Name ) )
614 sal_Int64 nSourceSize = 0;
615 sal_Int64 nDestSize = 0;
625 Reference< XStorable >xStorable(
mxModel, UNO_QUERY );
626 if ( xStorable.is() )
628 if ( xStorable->hasLocation() )
634 int nLength = aArguments.getLength();
635 aArguments.realloc( nLength + 1 );
636 aArguments[ nLength ].Name =
"FilterName";
647 Reference< XDesktop2 > xDesktop = Desktop::create(
mxContext );
648 xSelf = xDesktop->findFrame(
"_blank", FrameSearchFlag::CREATE );
649 Reference< XComponentLoader > xComponentLoader( xSelf, UNO_QUERY );
651 Sequence< PropertyValue > aLoadProps( 1 );
652 aLoadProps[ 0 ].Name =
"Hidden";
653 aLoadProps[ 0 ].Value <<=
true;
654 mxModel.set( xComponentLoader->loadComponentFromURL(
655 maSaveAsURL,
"_self", 0, aLoadProps ), UNO_QUERY );
660 Reference< XStorable > xStorable(
mxModel, UNO_QUERY );
661 if ( xStorable.is() && !xStorable->isReadonly() )
677 if ( xStorable.is() )
687 aInformationDialog.execute();
696 Reference< awt::XWindow > xContainerWindow( xSelf->getContainerWindow() );
697 xContainerWindow->setVisible(
true );
704 if ( nSourceSize && nDestSize )
css::uno::Reference< css::uno::XComponentContext > mxContext
static void ImpDeleteNotesPages(const Reference< XModel > &rxModel)
static void ImpConvertOLE(const Reference< XModel > &rxModel, sal_Int32 nOLEOptimizationType)
Reference< XFrame > xFrame
void SetStatusValue(const PPPOptimizerTokenEnum eStat, const css::uno::Any &rStatValue)
css::uno::Reference< css::frame::XModel2 > mxModel
static void DispatchURL(const Reference< XComponentContext > &xContext, const OUString &sURL, const Reference< XFrame > &xFrame)
static void ImpExtractCustomShow(const Reference< XModel > &rxModel, std::u16string_view rCustomShowName)
static sal_Int64 GetFileSize(const OUString &rURL)
Sequence< PropertyValue > aArguments
static void CompressGraphics(ImpOptimizer &rOptimizer, const Reference< XComponentContext > &rxContext, const GraphicSettings &rGraphicSettings, std::vector< GraphicCollector::GraphicEntity > &rGraphicList)
bool mbEmbedLinkedGraphics
static css::awt::Size GetOriginalSize(const css::uno::Reference< css::uno::XComponentContext > &rxMSF, const css::uno::Reference< css::graphic::XGraphic > &rxGraphic)
static void ImpDeleteHiddenSlides(const Reference< XModel > &rxModel)
static void ImpDeleteUnusedMasterPages(const Reference< XModel > &rxModel)
static void CollectNonCustomShowPages(const css::uno::Reference< css::frame::XModel > &, std::u16string_view rCustomShow, std::vector< css::uno::Reference< css::drawing::XDrawPage > > &)
css::uno::Reference< css::frame::XFrame > mxInformationDialog
ImpOptimizer(const css::uno::Reference< css::uno::XComponentContext > &rXFactory, const css::uno::Reference< css::frame::XModel > &rxModel)
static void CollectMasterPages(const css::uno::Reference< css::frame::XModel > &, std::vector< MasterPageEntity > &)
Reference< XDispatch > xDispatch
sal_Int32 mnImageResolution
OUString maCustomShowName
css::uno::Reference< css::frame::XDispatch > mxStatusDispatcher
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)
PPPOptimizerTokenEnum TKGet(const OUString &rToken)
css::beans::PropertyValues GetStatusSequence()
sal_Int32 mnImageResolution
bool mbDeleteUnusedMasterPages
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)
Reference< XGraphic > xGraphic
bool mbDeleteHiddenSlides
sal_Int32 mnOLEOptimizationType
css::uno::Reference< css::frame::XModel > mxModel