diff options
Diffstat (limited to 'oox/source/helper/graphichelper.cxx')
-rw-r--r-- | oox/source/helper/graphichelper.cxx | 236 |
1 files changed, 221 insertions, 15 deletions
diff --git a/oox/source/helper/graphichelper.cxx b/oox/source/helper/graphichelper.cxx index 3e54cd16c2bc..6b294f61abef 100644 --- a/oox/source/helper/graphichelper.cxx +++ b/oox/source/helper/graphichelper.cxx @@ -26,42 +26,248 @@ ************************************************************************/ #include "oox/helper/graphichelper.hxx" +#include <com/sun/star/awt/Point.hpp> +#include <com/sun/star/awt/Size.hpp> +#include <com/sun/star/awt/XDevice.hpp> +#include <com/sun/star/awt/XUnitConversion.hpp> +#include <com/sun/star/frame/XFramesSupplier.hpp> #include <com/sun/star/graphic/GraphicObject.hpp> #include <com/sun/star/graphic/XGraphicProvider.hpp> +#include <com/sun/star/util/MeasureUnit.hpp> #include <comphelper/componentcontext.hxx> #include <comphelper/seqstream.hxx> +#include "tokens.hxx" +#include "oox/helper/containerhelper.hxx" using ::rtl::OUString; -using ::com::sun::star::uno::Exception; -using ::com::sun::star::uno::Reference; -using ::com::sun::star::uno::Sequence; -using ::com::sun::star::uno::UNO_QUERY; -using ::com::sun::star::uno::UNO_SET_THROW; +using ::com::sun::star::awt::DeviceInfo; +using ::com::sun::star::awt::Point; +using ::com::sun::star::awt::Size; +using ::com::sun::star::awt::XDevice; +using ::com::sun::star::awt::XUnitConversion; using ::com::sun::star::beans::PropertyValue; -using ::com::sun::star::io::XInputStream; -using ::com::sun::star::lang::XMultiServiceFactory; +using ::com::sun::star::frame::XFrame; +using ::com::sun::star::frame::XFramesSupplier; using ::com::sun::star::graphic::GraphicObject; using ::com::sun::star::graphic::XGraphic; using ::com::sun::star::graphic::XGraphicObject; using ::com::sun::star::graphic::XGraphicProvider; +using ::com::sun::star::io::XInputStream; +using ::com::sun::star::lang::XMultiServiceFactory; +using ::com::sun::star::uno::Exception; +using ::com::sun::star::uno::Reference; +using ::com::sun::star::uno::Sequence; +using ::com::sun::star::uno::UNO_QUERY; +using ::com::sun::star::uno::UNO_QUERY_THROW; +using ::com::sun::star::uno::UNO_SET_THROW; namespace oox { // ============================================================================ -GraphicHelper::GraphicHelper( const Reference< XMultiServiceFactory >& rxFactory ) : - mxGraphicProvider( rxFactory->createInstance( CREATE_OUSTRING( "com.sun.star.graphic.GraphicProvider" ) ), UNO_QUERY ), +namespace { + +inline sal_Int32 lclConvertScreenPixelToHmm( double fPixel, double fPixelPerHmm ) +{ + return static_cast< sal_Int32 >( (fPixelPerHmm > 0.0) ? (fPixel / fPixelPerHmm + 0.5) : 0.0 ); +} + +} // namespace + +// ============================================================================ + +GraphicHelper::GraphicHelper( const Reference< XMultiServiceFactory >& rxGlobalFactory, const Reference< XFrame >& rxTargetFrame ) : + mxGraphicProvider( rxGlobalFactory->createInstance( CREATE_OUSTRING( "com.sun.star.graphic.GraphicProvider" ) ), UNO_QUERY ), maGraphicObjScheme( CREATE_OUSTRING( "vnd.sun.star.GraphicObject:" ) ) { - ::comphelper::ComponentContext aContext( rxFactory ); + ::comphelper::ComponentContext aContext( rxGlobalFactory ); mxCompContext = aContext.getUNOContext(); + + //! TODO: get colors from system + maSystemPalette[ XML_3dDkShadow ] = 0x716F64; + maSystemPalette[ XML_3dLight ] = 0xF1EFE2; + maSystemPalette[ XML_activeBorder ] = 0xD4D0C8; + maSystemPalette[ XML_activeCaption ] = 0x0054E3; + maSystemPalette[ XML_appWorkspace ] = 0x808080; + maSystemPalette[ XML_background ] = 0x004E98; + maSystemPalette[ XML_btnFace ] = 0xECE9D8; + maSystemPalette[ XML_btnHighlight ] = 0xFFFFFF; + maSystemPalette[ XML_btnShadow ] = 0xACA899; + maSystemPalette[ XML_btnText ] = 0x000000; + maSystemPalette[ XML_captionText ] = 0xFFFFFF; + maSystemPalette[ XML_gradientActiveCaption ] = 0x3D95FF; + maSystemPalette[ XML_gradientInactiveCaption ] = 0xD8E4F8; + maSystemPalette[ XML_grayText ] = 0xACA899; + maSystemPalette[ XML_highlight ] = 0x316AC5; + maSystemPalette[ XML_highlightText ] = 0xFFFFFF; + maSystemPalette[ XML_hotLight ] = 0x000080; + maSystemPalette[ XML_inactiveBorder ] = 0xD4D0C8; + maSystemPalette[ XML_inactiveCaption ] = 0x7A96DF; + maSystemPalette[ XML_inactiveCaptionText ] = 0xD8E4F8; + maSystemPalette[ XML_infoBk ] = 0xFFFFE1; + maSystemPalette[ XML_infoText ] = 0x000000; + maSystemPalette[ XML_menu ] = 0xFFFFFF; + maSystemPalette[ XML_menuBar ] = 0xECE9D8; + maSystemPalette[ XML_menuHighlight ] = 0x316AC5; + maSystemPalette[ XML_menuText ] = 0x000000; + maSystemPalette[ XML_scrollBar ] = 0xD4D0C8; + maSystemPalette[ XML_window ] = 0xFFFFFF; + maSystemPalette[ XML_windowFrame ] = 0x000000; + maSystemPalette[ XML_windowText ] = 0x000000; + + // if no target frame has been passed (e.g. OLE objects), try to fallback to the active frame + // TODO: we need some mechanism to keep and pass the parent frame + Reference< XFrame > xFrame = rxTargetFrame; + if( !xFrame.is() && rxGlobalFactory.is() ) try + { + Reference< XFramesSupplier > xFramesSupp( rxGlobalFactory->createInstance( CREATE_OUSTRING( "com.sun.star.frame.Desktop" ) ), UNO_QUERY_THROW ); + xFrame = xFramesSupp->getActiveFrame(); + } + catch( Exception& ) + { + } + + // get the metric of the output device + OSL_ENSURE( xFrame.is(), "GraphicHelper::GraphicHelper - cannot get target frame" ); + maDeviceInfo.PixelPerMeterX = maDeviceInfo.PixelPerMeterY = 3500.0; // some default just in case + if( xFrame.is() ) try + { + Reference< XDevice > xDevice( xFrame->getContainerWindow(), UNO_QUERY_THROW ); + mxUnitConversion.set( xDevice, UNO_QUERY ); + OSL_ENSURE( mxUnitConversion.is(), "GraphicHelper::GraphicHelper - cannot get unit converter" ); + maDeviceInfo = xDevice->getInfo(); + } + catch( Exception& ) + { + OSL_ENSURE( false, "GraphicHelper::GraphicHelper - cannot get output device info" ); + } + mfPixelPerHmmX = maDeviceInfo.PixelPerMeterX / 100000.0; + mfPixelPerHmmY = maDeviceInfo.PixelPerMeterY / 100000.0; } GraphicHelper::~GraphicHelper() { } -Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm ) +sal_Int32 GraphicHelper::getSystemColor( sal_Int32 nToken, sal_Int32 nDefaultRgb ) const +{ + return ContainerHelper::getMapElement( maSystemPalette, nToken, nDefaultRgb ); +} + +sal_Int32 GraphicHelper::getSchemeColor( sal_Int32 /*nToken*/ ) const +{ + OSL_ENSURE( false, "GraphicHelper::getSchemeColor - scheme colors not implemented" ); + return API_RGB_TRANSPARENT; +} + +sal_Int32 GraphicHelper::getPaletteColor( sal_Int32 /*nPaletteIdx*/ ) const +{ + OSL_ENSURE( false, "GraphicHelper::getPaletteColor - palette colors not implemented" ); + return API_RGB_TRANSPARENT; +} + +const DeviceInfo& GraphicHelper::getDeviceInfo() const +{ + return maDeviceInfo; +} + +sal_Int32 GraphicHelper::convertScreenPixelXToHmm( double fPixelX ) const +{ + return lclConvertScreenPixelToHmm( fPixelX, mfPixelPerHmmX ); +} + +sal_Int32 GraphicHelper::convertScreenPixelYToHmm( double fPixelY ) const +{ + return lclConvertScreenPixelToHmm( fPixelY, mfPixelPerHmmY ); +} + +Point GraphicHelper::convertScreenPixelToHmm( const Point& rPixel ) const +{ + return Point( convertScreenPixelXToHmm( rPixel.X ), convertScreenPixelYToHmm( rPixel.Y ) ); +} + +Size GraphicHelper::convertScreenPixelToHmm( const Size& rPixel ) const +{ + return Size( convertScreenPixelXToHmm( rPixel.Width ), convertScreenPixelYToHmm( rPixel.Height ) ); +} + +double GraphicHelper::convertHmmToScreenPixelX( sal_Int32 nHmmX ) const +{ + return nHmmX * mfPixelPerHmmX; +} + +double GraphicHelper::convertHmmToScreenPixelY( sal_Int32 nHmmY ) const +{ + return nHmmY * mfPixelPerHmmY; +} + +Point GraphicHelper::convertHmmToScreenPixel( const Point& rHmm ) const +{ + return Point( + static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.X ) + 0.5 ), + static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Y ) + 0.5 ) ); +} + +Size GraphicHelper::convertHmmToScreenPixel( const Size& rHmm ) const +{ + return Size( + static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.Width ) + 0.5 ), + static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Height ) + 0.5 ) ); +} + +Point GraphicHelper::convertAppFontToHmm( const Point& rAppFont ) const +{ + if( mxUnitConversion.is() ) try + { + Point aPixel = mxUnitConversion->convertPointToPixel( rAppFont, ::com::sun::star::util::MeasureUnit::APPFONT ); + return convertScreenPixelToHmm( aPixel ); + } + catch( Exception& ) + { + } + return Point( 0, 0 ); +} + +Size GraphicHelper::convertAppFontToHmm( const Size& rAppFont ) const +{ + if( mxUnitConversion.is() ) try + { + Size aPixel = mxUnitConversion->convertSizeToPixel( rAppFont, ::com::sun::star::util::MeasureUnit::APPFONT ); + return convertScreenPixelToHmm( aPixel ); + } + catch( Exception& ) + { + } + return Size( 0, 0 ); +} + +Point GraphicHelper::convertHmmToAppFont( const Point& rHmm ) const +{ + if( mxUnitConversion.is() ) try + { + Point aPixel = convertHmmToScreenPixel( rHmm ); + return mxUnitConversion->convertPointToLogic( aPixel, ::com::sun::star::util::MeasureUnit::APPFONT ); + } + catch( Exception& ) + { + } + return Point( 0, 0 ); +} + +Size GraphicHelper::convertHmmToAppFont( const Size& rHmm ) const +{ + if( mxUnitConversion.is() ) try + { + Size aPixel = convertHmmToScreenPixel( rHmm ); + return mxUnitConversion->convertSizeToLogic( aPixel, ::com::sun::star::util::MeasureUnit::APPFONT ); + } + catch( Exception& ) + { + } + return Size( 0, 0 ); +} + +Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm ) const { Reference< XGraphic > xGraphic; if( rxInStrm.is() && mxGraphicProvider.is() ) try @@ -77,7 +283,7 @@ Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStrea return xGraphic; } -Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rGraphicData ) +Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rGraphicData ) const { Reference< XGraphic > xGraphic; if( rGraphicData.hasElements() ) @@ -88,7 +294,7 @@ Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rG return xGraphic; } -OUString GraphicHelper::createGraphicObject( const Reference< XGraphic >& rxGraphic ) +OUString GraphicHelper::createGraphicObject( const Reference< XGraphic >& rxGraphic ) const { OUString aGraphicObjUrl; if( mxCompContext.is() && rxGraphic.is() ) try @@ -104,12 +310,12 @@ OUString GraphicHelper::createGraphicObject( const Reference< XGraphic >& rxGrap return aGraphicObjUrl; } -OUString GraphicHelper::importGraphicObject( const Reference< XInputStream >& rxInStrm ) +OUString GraphicHelper::importGraphicObject( const Reference< XInputStream >& rxInStrm ) const { return createGraphicObject( importGraphic( rxInStrm ) ); } -OUString GraphicHelper::importGraphicObject( const StreamDataSequence& rGraphicData ) +OUString GraphicHelper::importGraphicObject( const StreamDataSequence& rGraphicData ) const { return createGraphicObject( importGraphic( rGraphicData ) ); } |