LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/oox/source/helper - graphichelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 119 149 79.9 %
Date: 2013-07-09 Functions: 19 27 70.4 %
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             : #include <com/sun/star/container/XNameContainer.hpp>
      21             : #include "oox/helper/graphichelper.hxx"
      22             : 
      23             : #include <com/sun/star/awt/Point.hpp>
      24             : #include <com/sun/star/awt/Size.hpp>
      25             : #include <com/sun/star/awt/XDevice.hpp>
      26             : #include <com/sun/star/awt/XUnitConversion.hpp>
      27             : #include <com/sun/star/beans/XPropertySet.hpp>
      28             : #include <com/sun/star/frame/Desktop.hpp>
      29             : #include <com/sun/star/frame/XFramesSupplier.hpp>
      30             : #include <com/sun/star/graphic/GraphicObject.hpp>
      31             : #include <com/sun/star/graphic/GraphicProvider.hpp>
      32             : #include <com/sun/star/graphic/XGraphicProvider.hpp>
      33             : #include <com/sun/star/util/MeasureUnit.hpp>
      34             : #include <comphelper/seqstream.hxx>
      35             : #include <vcl/wmf.hxx>
      36             : #include "oox/helper/containerhelper.hxx"
      37             : #include "oox/helper/propertyset.hxx"
      38             : #include "oox/token/properties.hxx"
      39             : #include "oox/token/tokens.hxx"
      40             : 
      41             : namespace oox {
      42             : 
      43             : // ============================================================================
      44             : 
      45             : 
      46             : using namespace ::com::sun::star;
      47             : using namespace ::com::sun::star::beans;
      48             : using namespace ::com::sun::star::frame;
      49             : using namespace ::com::sun::star::graphic;
      50             : using namespace ::com::sun::star::io;
      51             : using namespace ::com::sun::star::lang;
      52             : using namespace ::com::sun::star::uno;
      53             : 
      54             : // ============================================================================
      55             : 
      56             : namespace {
      57             : 
      58           0 : inline sal_Int32 lclConvertScreenPixelToHmm( double fPixel, double fPixelPerHmm )
      59             : {
      60           0 :     return static_cast< sal_Int32 >( (fPixelPerHmm > 0.0) ? (fPixel / fPixelPerHmm + 0.5) : 0.0 );
      61             : }
      62             : 
      63             : } // namespace
      64             : 
      65             : // ============================================================================
      66             : 
      67         461 : GraphicHelper::GraphicHelper( const Reference< XComponentContext >& rxContext, const Reference< XFrame >& rxTargetFrame, const StorageRef& rxStorage ) :
      68             :     mxContext( rxContext ),
      69             :     mxStorage( rxStorage ),
      70         461 :     maGraphicObjScheme( "vnd.sun.star.GraphicObject:" )
      71             : {
      72             :     OSL_ENSURE( mxContext.is(), "GraphicHelper::GraphicHelper - missing component context" );
      73         461 :     if( mxContext.is() )
      74         461 :         mxGraphicProvider.set( graphic::GraphicProvider::create( mxContext ) );
      75             : 
      76             :     //! TODO: get colors from system
      77         461 :     maSystemPalette[ XML_3dDkShadow ]               = 0x716F64;
      78         461 :     maSystemPalette[ XML_3dLight ]                  = 0xF1EFE2;
      79         461 :     maSystemPalette[ XML_activeBorder ]             = 0xD4D0C8;
      80         461 :     maSystemPalette[ XML_activeCaption ]            = 0x0054E3;
      81         461 :     maSystemPalette[ XML_appWorkspace ]             = 0x808080;
      82         461 :     maSystemPalette[ XML_background ]               = 0x004E98;
      83         461 :     maSystemPalette[ XML_btnFace ]                  = 0xECE9D8;
      84         461 :     maSystemPalette[ XML_btnHighlight ]             = 0xFFFFFF;
      85         461 :     maSystemPalette[ XML_btnShadow ]                = 0xACA899;
      86         461 :     maSystemPalette[ XML_btnText ]                  = 0x000000;
      87         461 :     maSystemPalette[ XML_captionText ]              = 0xFFFFFF;
      88         461 :     maSystemPalette[ XML_gradientActiveCaption ]    = 0x3D95FF;
      89         461 :     maSystemPalette[ XML_gradientInactiveCaption ]  = 0xD8E4F8;
      90         461 :     maSystemPalette[ XML_grayText ]                 = 0xACA899;
      91         461 :     maSystemPalette[ XML_highlight ]                = 0x316AC5;
      92         461 :     maSystemPalette[ XML_highlightText ]            = 0xFFFFFF;
      93         461 :     maSystemPalette[ XML_hotLight ]                 = 0x000080;
      94         461 :     maSystemPalette[ XML_inactiveBorder ]           = 0xD4D0C8;
      95         461 :     maSystemPalette[ XML_inactiveCaption ]          = 0x7A96DF;
      96         461 :     maSystemPalette[ XML_inactiveCaptionText ]      = 0xD8E4F8;
      97         461 :     maSystemPalette[ XML_infoBk ]                   = 0xFFFFE1;
      98         461 :     maSystemPalette[ XML_infoText ]                 = 0x000000;
      99         461 :     maSystemPalette[ XML_menu ]                     = 0xFFFFFF;
     100         461 :     maSystemPalette[ XML_menuBar ]                  = 0xECE9D8;
     101         461 :     maSystemPalette[ XML_menuHighlight ]            = 0x316AC5;
     102         461 :     maSystemPalette[ XML_menuText ]                 = 0x000000;
     103         461 :     maSystemPalette[ XML_scrollBar ]                = 0xD4D0C8;
     104         461 :     maSystemPalette[ XML_window ]                   = 0xFFFFFF;
     105         461 :     maSystemPalette[ XML_windowFrame ]              = 0x000000;
     106         461 :     maSystemPalette[ XML_windowText ]               = 0x000000;
     107             : 
     108             :     // if no target frame has been passed (e.g. OLE objects), try to fallback to the active frame
     109             :     // TODO: we need some mechanism to keep and pass the parent frame
     110         461 :     Reference< XFrame > xFrame = rxTargetFrame;
     111         461 :     if( !xFrame.is() && mxContext.is() ) try
     112             :     {
     113         363 :         Reference< XDesktop2 > xFramesSupp = Desktop::create( mxContext );
     114         350 :         xFrame = xFramesSupp->getActiveFrame();
     115             :     }
     116          13 :     catch( Exception& )
     117             :     {
     118             :     }
     119             : 
     120             :     // get the metric of the output device
     121             :     OSL_ENSURE( xFrame.is(), "GraphicHelper::GraphicHelper - cannot get target frame" );
     122         461 :     maDeviceInfo.PixelPerMeterX = maDeviceInfo.PixelPerMeterY = 3500.0; // some default just in case
     123         461 :     if( xFrame.is() ) try
     124             :     {
     125         169 :         Reference< awt::XDevice > xDevice( xFrame->getContainerWindow(), UNO_QUERY_THROW );
     126         169 :         mxUnitConversion.set( xDevice, UNO_QUERY );
     127             :         OSL_ENSURE( mxUnitConversion.is(), "GraphicHelper::GraphicHelper - cannot get unit converter" );
     128         169 :         maDeviceInfo = xDevice->getInfo();
     129             :     }
     130           0 :     catch( Exception& )
     131             :     {
     132             :         OSL_FAIL( "GraphicHelper::GraphicHelper - cannot get output device info" );
     133             :     }
     134         461 :     mfPixelPerHmmX = maDeviceInfo.PixelPerMeterX / 100000.0;
     135         461 :     mfPixelPerHmmY = maDeviceInfo.PixelPerMeterY / 100000.0;
     136         461 : }
     137             : 
     138         667 : GraphicHelper::~GraphicHelper()
     139             : {
     140         667 : }
     141             : 
     142             : // System colors and predefined colors ----------------------------------------
     143             : 
     144         296 : sal_Int32 GraphicHelper::getSystemColor( sal_Int32 nToken, sal_Int32 nDefaultRgb ) const
     145             : {
     146         296 :     return ContainerHelper::getMapElement( maSystemPalette, nToken, nDefaultRgb );
     147             : }
     148             : 
     149           0 : sal_Int32 GraphicHelper::getSchemeColor( sal_Int32 /*nToken*/ ) const
     150             : {
     151             :     OSL_FAIL( "GraphicHelper::getSchemeColor - scheme colors not implemented" );
     152           0 :     return API_RGB_TRANSPARENT;
     153             : }
     154             : 
     155           0 : sal_Int32 GraphicHelper::getPaletteColor( sal_Int32 /*nPaletteIdx*/ ) const
     156             : {
     157             :     OSL_FAIL( "GraphicHelper::getPaletteColor - palette colors not implemented" );
     158           0 :     return API_RGB_TRANSPARENT;
     159             : }
     160             : 
     161             : // Device info and device dependent unit conversion ---------------------------
     162             : 
     163          21 : const awt::DeviceInfo& GraphicHelper::getDeviceInfo() const
     164             : {
     165          21 :     return maDeviceInfo;
     166             : }
     167             : 
     168           0 : sal_Int32 GraphicHelper::convertScreenPixelXToHmm( double fPixelX ) const
     169             : {
     170           0 :     return lclConvertScreenPixelToHmm( fPixelX, mfPixelPerHmmX );
     171             : }
     172             : 
     173           0 : sal_Int32 GraphicHelper::convertScreenPixelYToHmm( double fPixelY ) const
     174             : {
     175           0 :     return lclConvertScreenPixelToHmm( fPixelY, mfPixelPerHmmY );
     176             : }
     177             : 
     178           0 : awt::Size GraphicHelper::convertScreenPixelToHmm( const awt::Size& rPixel ) const
     179             : {
     180           0 :     return awt::Size( convertScreenPixelXToHmm( rPixel.Width ), convertScreenPixelYToHmm( rPixel.Height ) );
     181             : }
     182             : 
     183          18 : double GraphicHelper::convertHmmToScreenPixelX( sal_Int32 nHmmX ) const
     184             : {
     185          18 :     return nHmmX * mfPixelPerHmmX;
     186             : }
     187             : 
     188          18 : double GraphicHelper::convertHmmToScreenPixelY( sal_Int32 nHmmY ) const
     189             : {
     190          18 :     return nHmmY * mfPixelPerHmmY;
     191             : }
     192             : 
     193           8 : awt::Point GraphicHelper::convertHmmToScreenPixel( const awt::Point& rHmm ) const
     194             : {
     195             :     return awt::Point(
     196           8 :         static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.X ) + 0.5 ),
     197          16 :         static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Y ) + 0.5 ) );
     198             : }
     199             : 
     200          10 : awt::Size GraphicHelper::convertHmmToScreenPixel( const awt::Size& rHmm ) const
     201             : {
     202             :     return awt::Size(
     203          10 :         static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.Width ) + 0.5 ),
     204          20 :         static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Height ) + 0.5 ) );
     205             : }
     206             : 
     207           8 : awt::Point GraphicHelper::convertHmmToAppFont( const awt::Point& rHmm ) const
     208             : {
     209           8 :     if( mxUnitConversion.is() ) try
     210             :     {
     211           8 :         awt::Point aPixel = convertHmmToScreenPixel( rHmm );
     212           8 :         return mxUnitConversion->convertPointToLogic( aPixel, ::com::sun::star::util::MeasureUnit::APPFONT );
     213             :     }
     214           0 :     catch( Exception& )
     215             :     {
     216             :     }
     217           0 :     return awt::Point( 0, 0 );
     218             : }
     219             : 
     220          10 : awt::Size GraphicHelper::convertHmmToAppFont( const awt::Size& rHmm ) const
     221             : {
     222          10 :     if( mxUnitConversion.is() ) try
     223             :     {
     224          10 :         awt::Size aPixel = convertHmmToScreenPixel( rHmm );
     225          10 :         return mxUnitConversion->convertSizeToLogic( aPixel, ::com::sun::star::util::MeasureUnit::APPFONT );
     226             :     }
     227           0 :     catch( Exception& )
     228             :     {
     229             :     }
     230           0 :     return awt::Size( 0, 0 );
     231             : }
     232             : 
     233             : // Graphics and graphic objects  ----------------------------------------------
     234             : 
     235          38 : Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm,
     236             :         const WMF_EXTERNALHEADER* pExtHeader ) const
     237             : {
     238          38 :     Reference< XGraphic > xGraphic;
     239          38 :     if( rxInStrm.is() && mxGraphicProvider.is() ) try
     240             :     {
     241          38 :         Sequence< PropertyValue > aArgs( 1 );
     242          38 :         aArgs[ 0 ].Name = "InputStream";
     243          38 :         aArgs[ 0 ].Value <<= rxInStrm;
     244             : 
     245          38 :         if ( pExtHeader && pExtHeader->mapMode > 0 )
     246             :         {
     247           2 :             aArgs.realloc( aArgs.getLength() + 1 );
     248           2 :             Sequence< PropertyValue > aFilterData( 3 );
     249           2 :             aFilterData[ 0 ].Name = "ExternalWidth";
     250           2 :             aFilterData[ 0 ].Value <<= pExtHeader->xExt;
     251           2 :             aFilterData[ 1 ].Name = "ExternalHeight";
     252           2 :             aFilterData[ 1 ].Value <<= pExtHeader->yExt;
     253           2 :             aFilterData[ 2 ].Name = "ExternalMapMode";
     254           2 :             aFilterData[ 2 ].Value <<= pExtHeader->mapMode;
     255           2 :             aArgs[ 1 ].Name = "FilterData";
     256           2 :             aArgs[ 1 ].Value <<= aFilterData;
     257             :         }
     258             : 
     259          38 :         xGraphic = mxGraphicProvider->queryGraphic( aArgs );
     260             :     }
     261           0 :     catch( Exception& )
     262             :     {
     263             :     }
     264          38 :     return xGraphic;
     265             : }
     266             : 
     267           0 : Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rGraphicData ) const
     268             : {
     269           0 :     Reference< XGraphic > xGraphic;
     270           0 :     if( rGraphicData.hasElements() )
     271             :     {
     272           0 :         Reference< XInputStream > xInStrm( new ::comphelper::SequenceInputStream( rGraphicData ) );
     273           0 :         xGraphic = importGraphic( xInStrm );
     274             :     }
     275           0 :     return xGraphic;
     276             : }
     277             : 
     278          31 : Reference< XGraphic > GraphicHelper::importEmbeddedGraphic( const OUString& rStreamName ) const
     279             : {
     280          31 :     Reference< XGraphic > xGraphic;
     281             :     OSL_ENSURE( !rStreamName.isEmpty(), "GraphicHelper::importEmbeddedGraphic - empty stream name" );
     282          31 :     if( !rStreamName.isEmpty() )
     283             :     {
     284          31 :         EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find( rStreamName );
     285          31 :         if( aIt == maEmbeddedGraphics.end() )
     286             :         {
     287          24 :             xGraphic = importGraphic( mxStorage->openInputStream( rStreamName ) );
     288          24 :             if( xGraphic.is() )
     289          24 :                 maEmbeddedGraphics[ rStreamName ] = xGraphic;
     290             :         }
     291             :         else
     292           7 :             xGraphic = aIt->second;
     293             :     }
     294          31 :     return xGraphic;
     295             : }
     296             : 
     297          45 : OUString GraphicHelper::createGraphicObject( const Reference< XGraphic >& rxGraphic ) const
     298             : {
     299          45 :     OUString aGraphicObjUrl;
     300          45 :     if( mxContext.is() && rxGraphic.is() ) try
     301             :     {
     302          45 :         Reference< XGraphicObject > xGraphicObj( graphic::GraphicObject::create( mxContext ), UNO_SET_THROW );
     303          45 :         xGraphicObj->setGraphic( rxGraphic );
     304          45 :         maGraphicObjects.push_back( xGraphicObj );
     305          45 :         aGraphicObjUrl = maGraphicObjScheme + xGraphicObj->getUniqueID();
     306             :     }
     307           0 :     catch( Exception& )
     308             :     {
     309             :     }
     310          45 :     return aGraphicObjUrl;
     311             : }
     312             : 
     313          14 : OUString GraphicHelper::importGraphicObject( const Reference< XInputStream >& rxInStrm,
     314             :         const WMF_EXTERNALHEADER* pExtHeader ) const
     315             : {
     316          14 :     return createGraphicObject( importGraphic( rxInStrm, pExtHeader ) );
     317             : }
     318             : 
     319           0 : OUString GraphicHelper::importGraphicObject( const StreamDataSequence& rGraphicData ) const
     320             : {
     321           0 :     return createGraphicObject( importGraphic( rGraphicData ) );
     322             : }
     323             : 
     324          16 : OUString GraphicHelper::importEmbeddedGraphicObject( const OUString& rStreamName ) const
     325             : {
     326          16 :     Reference< XGraphic > xGraphic = importEmbeddedGraphic( rStreamName );
     327          16 :     return xGraphic.is() ? createGraphicObject( xGraphic ) : OUString();
     328             : }
     329             : 
     330           5 : awt::Size GraphicHelper::getOriginalSize( const Reference< XGraphic >& xGraphic ) const
     331             : {
     332           5 :     awt::Size aSizeHmm;
     333           5 :     PropertySet aPropSet( xGraphic );
     334           5 :     if( aPropSet.getProperty( aSizeHmm, PROP_Size100thMM ) && (aSizeHmm.Width == 0) && (aSizeHmm.Height == 0) )     // MAPMODE_PIXEL used?
     335             :     {
     336           0 :         awt::Size aSizePixel( 0, 0 );
     337           0 :         if( aPropSet.getProperty( aSizePixel, PROP_SizePixel ) )
     338           0 :             aSizeHmm = convertScreenPixelToHmm( aSizePixel );
     339             :     }
     340           5 :     return aSizeHmm;
     341             : }
     342             : 
     343             : // ============================================================================
     344             : 
     345         141 : } // namespace oox
     346             : 
     347             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10