LCOV - code coverage report
Current view: top level - sdext/source/minimizer - impoptimizer.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 410 0.0 %
Date: 2014-04-14 Functions: 0 14 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10