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>
72 std::vector< Reference< XDrawPage > > vNonUsedPageList;
76 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
77 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
78 for(
const auto& rxPage : vNonUsedPageList )
79 xDrawPages->remove( rxPage );
89 std::vector< PageCollector::MasterPageEntity > aMasterPageList;
93 Reference< XMasterPagesSupplier > xMasterPagesSupplier( rxModel, UNO_QUERY_THROW );
94 Reference< XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), UNO_SET_THROW );
95 for(
const auto& rMasterPage : aMasterPageList )
97 if ( !rMasterPage.bUsed )
98 xMasterPages->remove( rMasterPage.xMasterPage );
106 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
107 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
108 for( sal_Int32
i = 0;
i < xDrawPages->getCount();
i++ )
110 Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex(
i ), UNO_QUERY_THROW );
111 Reference< XPropertySet > xPropSet( xDrawPage, UNO_QUERY_THROW );
114 if ( xPropSet->getPropertyValue(
"Visible" ) >>=
bVisible )
118 xDrawPages->remove( xDrawPage );
133 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
134 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
135 sal_Int32
i, nPages = xDrawPages->getCount();
136 for(
i = 0;
i < nPages;
i++ )
138 Reference< XPresentationPage > xPresentationPage( xDrawPages->getByIndex(
i ), UNO_QUERY_THROW );
139 Reference< XPropertySet > xPropSet( xPresentationPage->getNotesPage(), UNO_QUERY_THROW );
140 Reference< XShapes > xShapes( xPropSet, UNO_QUERY_THROW );
141 while( xShapes->getCount() )
142 xShapes->remove( Reference< XShape >( xShapes->getByIndex( xShapes->getCount() - 1 ), UNO_QUERY_THROW ) );
144 xPropSet->setPropertyValue(
"Layout",
Any( sal_Int16(21) ) );
152static void ImpConvertOLE(
const Reference< XModel >& rxModel, sal_Int32 nOLEOptimizationType )
156 Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
157 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
158 for ( sal_Int32
i = 0;
i < xDrawPages->getCount();
i++ )
160 Reference< XShapes > xShapes( xDrawPages->getByIndex(
i ), UNO_QUERY_THROW );
161 for ( sal_Int32 j = 0; j < xShapes->getCount(); j++ )
163 Reference< XShape > xShape( xShapes->getByIndex( j ), UNO_QUERY_THROW );
164 if ( xShape->getShapeType() ==
"com.sun.star.drawing.OLE2Shape" )
166 Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
168 bool bConvertOLE = nOLEOptimizationType == 0;
169 if ( nOLEOptimizationType == 1 )
171 bool bIsInternal =
true;
172 xPropSet->getPropertyValue(
"IsInternal" ) >>= bIsInternal;
173 bConvertOLE = !bIsInternal;
177 Reference< XGraphic > xGraphic;
178 if ( xPropSet->getPropertyValue(
"Graphic" ) >>= xGraphic )
180 Reference< XMultiServiceFactory > xFact( rxModel, UNO_QUERY_THROW );
181 Reference< XShape > xShape2( xFact->createInstance(
"com.sun.star.drawing.GraphicObjectShape" ), UNO_QUERY_THROW );
182 xShapes->add( xShape2 );
183 xShape2->setPosition( xShape->getPosition() );
184 xShape2->setSize( xShape->getSize() );
185 Reference< XPropertySet > xPropSet2( xShape2, UNO_QUERY_THROW );
186 xPropSet2->setPropertyValue(
"Graphic",
Any( xGraphic ) );
187 xShapes->remove( xShape );
188 xPropSet2->setPropertyValue(
"ZOrder",
Any( j ) );
200static void ImpCompressGraphic( Reference< XGraphicProvider >
const & rxGraphicProvider,
const Reference< XGraphic >& rxGraphic, Reference< XOutputStream >
const & rxOutputStream,
201 const OUString& rDestMimeType,
const awt::Size& rLogicalSize, sal_Int32 nJPEGQuality, sal_Int32 nImageResolution,
bool bRemoveCropping,
const text::GraphicCrop& rGraphicCropLogic )
205 if ( rxGraphicProvider.is() && rxOutputStream.is() )
207 Sequence< PropertyValue > aFilterData{
218 Sequence< PropertyValue > aArgs{
224 rxGraphicProvider->storeGraphic( rxGraphic, aArgs );
233 const Reference< XGraphic >& xGraphic,
const awt::Size& aLogicalSize,
const text::GraphicCrop& aGraphicCropLogic,
236 Reference< XGraphic > xNewGraphic;
239 OUString aSourceMimeType;
240 Reference< XPropertySet > xGraphicPropertySet( xGraphic, UNO_QUERY_THROW );
241 if ( xGraphicPropertySet->getPropertyValue(
"MimeType" ) >>= aSourceMimeType )
243 sal_Int8 nGraphicType( xGraphic->getType() );
244 if ( nGraphicType == css::graphic::GraphicType::PIXEL )
246 bool bTransparent =
false;
248 bool bAnimated =
false;
250 awt::Size aSourceSizePixel( 0, 0 );
251 text::GraphicCrop aGraphicCropPixel( 0, 0, 0, 0 );
253 if ( ( xGraphicPropertySet->getPropertyValue(
"SizePixel" ) >>= aSourceSizePixel ) &&
254 ( xGraphicPropertySet->getPropertyValue(
"Transparent" ) >>= bTransparent ) &&
255 ( xGraphicPropertySet->getPropertyValue(
"Alpha" ) >>= bAlpha ) &&
256 ( xGraphicPropertySet->getPropertyValue(
"Animated" ) >>= bAnimated ) )
258 awt::Size aDestSizePixel( aSourceSizePixel );
261 bool bNeedsOptimizing =
false;
265 if ( aGraphicCropLogic.Left || aGraphicCropLogic.Top || aGraphicCropLogic.Right || aGraphicCropLogic.Bottom )
269 if ( bRemoveCropArea )
270 bNeedsOptimizing =
true;
272 if ( aSize100thMM.Width && aSize100thMM.Height )
274 aGraphicCropPixel.Left =
static_cast< sal_Int32
>( (
static_cast<double>(aSourceSizePixel.Width) * aGraphicCropLogic.Left ) / aSize100thMM.Width );
275 aGraphicCropPixel.Top =
static_cast< sal_Int32
>( (
static_cast<double>(aSourceSizePixel.Height)* aGraphicCropLogic.Top ) / aSize100thMM.Height );
276 aGraphicCropPixel.Right =
static_cast< sal_Int32
>( (
static_cast<double>(aSourceSizePixel.Width) * ( aSize100thMM.Width - aGraphicCropLogic.Right ) ) / aSize100thMM.Width );
277 aGraphicCropPixel.Bottom =
static_cast< sal_Int32
>( (
static_cast<double>(aSourceSizePixel.Height)* ( aSize100thMM.Height - aGraphicCropLogic.Bottom ) ) / aSize100thMM.Height );
280 aSourceSizePixel.Width = aGraphicCropPixel.Right - aGraphicCropPixel.Left;
281 aSourceSizePixel.Height= aGraphicCropPixel.Bottom - aGraphicCropPixel.Top;
285 bRemoveCropArea =
false;
288 if ( ( aSourceSizePixel.Width > 0 ) && ( aSourceSizePixel.Height > 0 ) )
290 OUString aDestMimeType(
"image/png" );
293 aDestMimeType =
"image/jpeg";
295 bNeedsOptimizing =
true;
297 if ( bRemoveCropArea )
298 aDestSizePixel = aSourceSizePixel;
299 if ( rGraphicSettings.
mnImageResolution && aLogicalSize.Width && aLogicalSize.Height )
301 const double fSourceDPIX =
static_cast<double>(aSourceSizePixel.Width) / (
static_cast<double>(aLogicalSize.Width) / 2540.0 );
302 const double fSourceDPIY =
static_cast<double>(aSourceSizePixel.Height)/ (
static_cast<double>(aLogicalSize.Height)/ 2540.0 );
307 const double fNewSizePixelX = (
static_cast<double>(aDestSizePixel.Width) * rGraphicSettings.
mnImageResolution ) / fSourceDPIX;
308 const double fNewSizePixelY = (
static_cast<double>(aDestSizePixel.Height)* rGraphicSettings.
mnImageResolution ) / fSourceDPIY;
310 aDestSizePixel = awt::Size(
static_cast<sal_Int32
>(fNewSizePixelX),
static_cast<sal_Int32
>(fNewSizePixelY) );
311 bNeedsOptimizing =
true;
314 if ( bNeedsOptimizing && aDestSizePixel.Width && aDestSizePixel.Height )
316 Reference< XStream > xTempFile( io::TempFile::create(rxContext), UNO_QUERY_THROW );
317 Reference< XOutputStream > xOutputStream( xTempFile->getOutputStream() );
318 Reference< XGraphicProvider > xGraphicProvider( GraphicProvider::create( rxContext ) );
321 Reference< XInputStream > xInputStream( xTempFile->getInputStream() );
322 Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY_THROW );
323 xSeekable->seek( 0 );
325 "InputStream", xInputStream) };
326 xNewGraphic = xGraphicProvider->queryGraphic( aArgs );
334 const OUString& aDestMimeType( aSourceMimeType );
335 Reference< XStream > xTempFile( io::TempFile::create(rxContext), UNO_QUERY_THROW );
336 Reference< XOutputStream > xOutputStream( xTempFile->getOutputStream() );
337 Reference< XGraphicProvider > xGraphicProvider( GraphicProvider::create( rxContext ) );
339 Reference< XInputStream > xInputStream( xTempFile->getInputStream() );
340 Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY_THROW );
341 xSeekable->seek( 0 );
344 xNewGraphic = xGraphicProvider->queryGraphic( aArgs );
355 std::vector< GraphicCollector::GraphicEntity >& rGraphicList )
360 for(
auto& rGraphic : rGraphicList )
363 sal_Int32 nProgress =
static_cast< sal_Int32
>( 40.0 * (
i /
static_cast< double >( rGraphicList.size() ) ) ) + 50;
367 if ( !rGraphic.maUser.empty() )
372 Reference< XGraphic > xGraphic;
373 if ( rGraphic.maUser[ 0 ].mbFillBitmap && rGraphic.maUser[ 0 ].mxPropertySet.is() )
375 Reference< rendering::XBitmap > xFillBitmap;
376 if ( rGraphic.maUser[ 0 ].mxPropertySet->getPropertyValue(
"FillBitmap" ) >>= xFillBitmap )
377 xGraphic.set( xFillBitmap, UNO_QUERY_THROW );
379 else if ( rGraphic.maUser[ 0 ].mxShape.is() )
381 Reference< XPropertySet > xShapePropertySet( rGraphic.maUser[ 0 ].mxShape, UNO_QUERY_THROW );
382 xShapePropertySet->getPropertyValue(
"Graphic" ) >>= xGraphic;
386 Reference< XPropertySet > xNewGraphicPropertySet( xGraphic, UNO_QUERY_THROW );
388 Reference< XGraphic > xNewGraphic(
ImpCompressGraphic( rxContext, xGraphic, rGraphic.maLogicalSize, rGraphic.maGraphicCropLogic, aGraphicSettings ) );
389 if ( xNewGraphic.is() )
392 for(
auto& rGraphicUser : rGraphic.maUser )
394 if ( rGraphicUser.mxShape.is() )
396 Reference< XPropertySet > xShapePropertySet( rGraphicUser.mxShape, UNO_QUERY_THROW );
397 xShapePropertySet->setPropertyValue(
"Graphic",
Any( xNewGraphic ) );
399 if ( rGraphicUser.maGraphicCropLogic.Left || rGraphicUser.maGraphicCropLogic.Top
400 || rGraphicUser.maGraphicCropLogic.Right || rGraphicUser.maGraphicCropLogic.Bottom )
402 text::GraphicCrop aGraphicCropLogic( 0, 0, 0, 0 );
406 aGraphicCropLogic.Left =
static_cast<sal_Int32
>(
static_cast<double>(rGraphicUser.maGraphicCropLogic.Left) * (
static_cast<double>(aNewSize.Width) /
static_cast<double>(aSize100thMM.Width)));
407 aGraphicCropLogic.Top =
static_cast<sal_Int32
>(
static_cast<double>(rGraphicUser.maGraphicCropLogic.Top) * (
static_cast<double>(aNewSize.Height) /
static_cast<double>(aSize100thMM.Height)));
408 aGraphicCropLogic.Right =
static_cast<sal_Int32
>(
static_cast<double>(rGraphicUser.maGraphicCropLogic.Right) * (
static_cast<double>(aNewSize.Width) /
static_cast<double>(aSize100thMM.Width)));
409 aGraphicCropLogic.Bottom =
static_cast<sal_Int32
>(
static_cast<double>(rGraphicUser.maGraphicCropLogic.Bottom) * (
static_cast<double>(aNewSize.Height) /
static_cast<double>(aSize100thMM.Height)));
411 xShapePropertySet->setPropertyValue(
"GraphicCrop",
Any( aGraphicCropLogic ) );
414 else if ( rGraphicUser.mxPropertySet.is() )
416 Reference< rendering::XBitmap > xFillBitmap( xNewGraphic, UNO_QUERY );
417 if ( xFillBitmap.is() )
422 Reference< XPropertySet >& rxPropertySet( rGraphicUser.mxPropertySet );
423 rxPropertySet->setPropertyValue(
"FillBitmap",
Any( xFillBitmap ) );
424 if ( ( rxPropertySet->getPropertyValue(
"FillBitmapLogicalSize" ) >>= bLogicalSize )
425 && ( rxPropertySet->getPropertyValue(
"FillBitmapSizeX" ) >>= aSize.Width )
426 && ( rxPropertySet->getPropertyValue(
"FillBitmapSizeY" ) >>= aSize.Height ) )
428 if ( !aSize.Width || !aSize.Height )
430 rxPropertySet->setPropertyValue(
"FillBitmapLogicalSize",
Any(
true ) );
431 rxPropertySet->setPropertyValue(
"FillBitmapSizeX",
Any( rGraphicUser.maLogicalSize.Width ) );
432 rxPropertySet->setPropertyValue(
"FillBitmapSizeY",
Any( rGraphicUser.maLogicalSize.Height ) );
435 if ( rGraphicUser.mxPagePropertySet.is() )
436 rGraphicUser.mxPagePropertySet->setPropertyValue(
"Background",
Any( rxPropertySet ) );
454 mbJPEGCompression ( false ),
455 mnJPEGQuality ( 90 ),
456 mbRemoveCropArea ( false ),
457 mnImageResolution ( 0 ),
458 mbEmbedLinkedGraphics ( true ),
459 mbOLEOptimization ( false ),
460 mnOLEOptimizationType ( 0 ),
461 mbDeleteUnusedMasterPages ( false ),
462 mbDeleteHiddenSlides ( false ),
463 mbDeleteNotesPages ( false ),
464 mbOpenNewDocument ( true )
479 aURL.Protocol =
"vnd.com.sun.star.comp.PresentationMinimizer:";
480 aURL.Path =
"statusupdate";
529 std::vector< GraphicCollector::GraphicEntity > aGraphicList;
538static void DispatchURL(
const Reference< XComponentContext >& xContext,
const OUString& sURL,
const Reference< XFrame >& xFrame )
542 Reference< XURLTransformer > xURLTransformer( URLTransformer::create(xContext) );
544 aUrl.Complete = sURL;
545 xURLTransformer->parseStrict( aUrl );
546 Sequence< PropertyValue > aArgs;
547 Reference< XDispatchProvider > xDispatchProvider(
xFrame, UNO_QUERY_THROW );
548 Reference< XDispatch >
xDispatch = xDispatchProvider->queryDispatch( aUrl, OUString(), 0 );
563 sal_Int64 nEstimatedFileSize = 0;
567 for (
const auto& rArgument : rArguments )
569 switch(
TKGet( rArgument.Name ) )
576 css::uno::Sequence< css::beans::PropertyValue > aSettings;
577 rArgument.Value >>= aSettings;
578 for (
const auto& rSetting : std::as_const(aSettings) )
580 switch(
TKGet( rSetting.Name ) )
606 sal_Int64 nSourceSize = 0;
607 sal_Int64 nDestSize = 0;
609 Reference< XFrame > xSelf;
617 Reference< XStorable >xStorable(
mxModel, UNO_QUERY );
618 if ( xStorable.is() )
620 if ( xStorable->hasLocation() )
629 pArguments[
nLength ].Name =
"FilterName";
640 Reference< XDesktop2 > xDesktop = Desktop::create(
mxContext );
641 xSelf = xDesktop->findFrame(
"_blank", FrameSearchFlag::CREATE );
642 Reference< XComponentLoader > xComponentLoader( xSelf, UNO_QUERY );
645 mxModel.set( xComponentLoader->loadComponentFromURL(
646 maSaveAsURL,
"_self", 0, aLoadProps ), UNO_QUERY );
651 Reference< XStorable > xStorable(
mxModel, UNO_QUERY );
652 if ( xStorable.is() && !xStorable->isReadonly() )
668 if ( xStorable.is() )
678 aInformationDialog.execute();
687 Reference< awt::XWindow > xContainerWindow( xSelf->getContainerWindow() );
688 xContainerWindow->setVisible(
true );
695 if ( nSourceSize && nDestSize )
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
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
css::uno::Reference< css::awt::XWindow > mxDialogParentWindow
css::uno::Reference< css::frame::XModel > mxModel
css::uno::Reference< css::frame::XFrame > mxDocumentFrame
bool mbEmbedLinkedGraphics
sal_Int32 mnImageResolution
css::uno::Reference< css::uno::XComponentContext > mxContext
OUString maCustomShowName
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
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
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
PPPOptimizerTokenEnum TKGet(std::u16string_view rToken)
@ TK_DeleteUnusedMasterPages
sal_Int32 mnImageResolution
static sal_Int64 GetFileSize(const OUString &rURL)
Reference< XFrame > xFrame