357 lines
13 KiB
C++
357 lines
13 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
/*************************************************************************
|
|
*
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
*
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
|
*
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
|
*
|
|
* This file is part of OpenOffice.org.
|
|
*
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
* only, as published by the Free Software Foundation.
|
|
*
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License version 3 for more details
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
* <http://www.openoffice.org/license.html>
|
|
* for a copy of the LGPLv3 License.
|
|
*
|
|
************************************************************************/
|
|
|
|
#include <com/sun/star/container/XNameContainer.hpp>
|
|
#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/beans/XPropertySet.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/seqstream.hxx>
|
|
#include <svtools/wmf.hxx>
|
|
#include "oox/helper/containerhelper.hxx"
|
|
#include "oox/helper/propertyset.hxx"
|
|
#include "oox/token/tokens.hxx"
|
|
|
|
namespace oox {
|
|
|
|
// ============================================================================
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
using namespace ::com::sun::star::beans;
|
|
using namespace ::com::sun::star::frame;
|
|
using namespace ::com::sun::star::graphic;
|
|
using namespace ::com::sun::star::io;
|
|
using namespace ::com::sun::star::lang;
|
|
using namespace ::com::sun::star::uno;
|
|
|
|
using ::rtl::OUString;
|
|
|
|
// ============================================================================
|
|
|
|
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< XComponentContext >& rxContext, const Reference< XFrame >& rxTargetFrame, const StorageRef& rxStorage ) :
|
|
mxContext( rxContext ),
|
|
mxStorage( rxStorage ),
|
|
maGraphicObjScheme( CREATE_OUSTRING( "vnd.sun.star.GraphicObject:" ) )
|
|
{
|
|
OSL_ENSURE( mxContext.is(), "GraphicHelper::GraphicHelper - missing component context" );
|
|
Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY );
|
|
OSL_ENSURE( xFactory.is(), "GraphicHelper::GraphicHelper - missing service factory" );
|
|
if( xFactory.is() )
|
|
mxGraphicProvider.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.graphic.GraphicProvider" ) ), UNO_QUERY );
|
|
|
|
//! 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() && xFactory.is() ) try
|
|
{
|
|
Reference< XFramesSupplier > xFramesSupp( xFactory->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< awt::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_FAIL( "GraphicHelper::GraphicHelper - cannot get output device info" );
|
|
}
|
|
mfPixelPerHmmX = maDeviceInfo.PixelPerMeterX / 100000.0;
|
|
mfPixelPerHmmY = maDeviceInfo.PixelPerMeterY / 100000.0;
|
|
}
|
|
|
|
GraphicHelper::~GraphicHelper()
|
|
{
|
|
}
|
|
|
|
// System colors and predefined colors ----------------------------------------
|
|
|
|
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_FAIL( "GraphicHelper::getSchemeColor - scheme colors not implemented" );
|
|
return API_RGB_TRANSPARENT;
|
|
}
|
|
|
|
sal_Int32 GraphicHelper::getPaletteColor( sal_Int32 /*nPaletteIdx*/ ) const
|
|
{
|
|
OSL_FAIL( "GraphicHelper::getPaletteColor - palette colors not implemented" );
|
|
return API_RGB_TRANSPARENT;
|
|
}
|
|
|
|
// Device info and device dependent unit conversion ---------------------------
|
|
|
|
const awt::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 );
|
|
}
|
|
|
|
awt::Size GraphicHelper::convertScreenPixelToHmm( const awt::Size& rPixel ) const
|
|
{
|
|
return awt::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;
|
|
}
|
|
|
|
awt::Point GraphicHelper::convertHmmToScreenPixel( const awt::Point& rHmm ) const
|
|
{
|
|
return awt::Point(
|
|
static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.X ) + 0.5 ),
|
|
static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Y ) + 0.5 ) );
|
|
}
|
|
|
|
awt::Size GraphicHelper::convertHmmToScreenPixel( const awt::Size& rHmm ) const
|
|
{
|
|
return awt::Size(
|
|
static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.Width ) + 0.5 ),
|
|
static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Height ) + 0.5 ) );
|
|
}
|
|
|
|
awt::Point GraphicHelper::convertHmmToAppFont( const awt::Point& rHmm ) const
|
|
{
|
|
if( mxUnitConversion.is() ) try
|
|
{
|
|
awt::Point aPixel = convertHmmToScreenPixel( rHmm );
|
|
return mxUnitConversion->convertPointToLogic( aPixel, ::com::sun::star::util::MeasureUnit::APPFONT );
|
|
}
|
|
catch( Exception& )
|
|
{
|
|
}
|
|
return awt::Point( 0, 0 );
|
|
}
|
|
|
|
awt::Size GraphicHelper::convertHmmToAppFont( const awt::Size& rHmm ) const
|
|
{
|
|
if( mxUnitConversion.is() ) try
|
|
{
|
|
awt::Size aPixel = convertHmmToScreenPixel( rHmm );
|
|
return mxUnitConversion->convertSizeToLogic( aPixel, ::com::sun::star::util::MeasureUnit::APPFONT );
|
|
}
|
|
catch( Exception& )
|
|
{
|
|
}
|
|
return awt::Size( 0, 0 );
|
|
}
|
|
|
|
// Graphics and graphic objects ----------------------------------------------
|
|
|
|
Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm,
|
|
const WMF_EXTERNALHEADER* pExtHeader ) const
|
|
{
|
|
Reference< XGraphic > xGraphic;
|
|
if( rxInStrm.is() && mxGraphicProvider.is() ) try
|
|
{
|
|
Sequence< PropertyValue > aArgs( 1 );
|
|
aArgs[ 0 ].Name = CREATE_OUSTRING( "InputStream" );
|
|
aArgs[ 0 ].Value <<= rxInStrm;
|
|
|
|
if ( pExtHeader && pExtHeader->mapMode > 0 )
|
|
{
|
|
aArgs.realloc( aArgs.getLength() + 1 );
|
|
Sequence< PropertyValue > aFilterData( 3 );
|
|
aFilterData[ 0 ].Name = CREATE_OUSTRING( "ExternalWidth" );
|
|
aFilterData[ 0 ].Value <<= pExtHeader->xExt;
|
|
aFilterData[ 1 ].Name = CREATE_OUSTRING( "ExternalHeight" );
|
|
aFilterData[ 1 ].Value <<= pExtHeader->yExt;
|
|
aFilterData[ 2 ].Name = CREATE_OUSTRING( "ExternalMapMode" );
|
|
aFilterData[ 2 ].Value <<= pExtHeader->mapMode;
|
|
aArgs[ 1 ].Name = CREATE_OUSTRING( "FilterData" );
|
|
aArgs[ 1 ].Value <<= aFilterData;
|
|
}
|
|
|
|
xGraphic = mxGraphicProvider->queryGraphic( aArgs );
|
|
}
|
|
catch( Exception& )
|
|
{
|
|
}
|
|
return xGraphic;
|
|
}
|
|
|
|
Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rGraphicData ) const
|
|
{
|
|
Reference< XGraphic > xGraphic;
|
|
if( rGraphicData.hasElements() )
|
|
{
|
|
Reference< XInputStream > xInStrm( new ::comphelper::SequenceInputStream( rGraphicData ) );
|
|
xGraphic = importGraphic( xInStrm );
|
|
}
|
|
return xGraphic;
|
|
}
|
|
|
|
Reference< XGraphic > GraphicHelper::importEmbeddedGraphic( const OUString& rStreamName ) const
|
|
{
|
|
Reference< XGraphic > xGraphic;
|
|
OSL_ENSURE( !rStreamName.isEmpty(), "GraphicHelper::importEmbeddedGraphic - empty stream name" );
|
|
if( !rStreamName.isEmpty() )
|
|
{
|
|
EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find( rStreamName );
|
|
if( aIt == maEmbeddedGraphics.end() )
|
|
{
|
|
xGraphic = importGraphic( mxStorage->openInputStream( rStreamName ) );
|
|
if( xGraphic.is() )
|
|
maEmbeddedGraphics[ rStreamName ] = xGraphic;
|
|
}
|
|
else
|
|
xGraphic = aIt->second;
|
|
}
|
|
return xGraphic;
|
|
}
|
|
|
|
OUString GraphicHelper::createGraphicObject( const Reference< XGraphic >& rxGraphic ) const
|
|
{
|
|
OUString aGraphicObjUrl;
|
|
if( mxContext.is() && rxGraphic.is() ) try
|
|
{
|
|
Reference< XGraphicObject > xGraphicObj( GraphicObject::create( mxContext ), UNO_SET_THROW );
|
|
xGraphicObj->setGraphic( rxGraphic );
|
|
maGraphicObjects.push_back( xGraphicObj );
|
|
aGraphicObjUrl = maGraphicObjScheme + xGraphicObj->getUniqueID();
|
|
}
|
|
catch( Exception& )
|
|
{
|
|
}
|
|
return aGraphicObjUrl;
|
|
}
|
|
|
|
OUString GraphicHelper::importGraphicObject( const Reference< XInputStream >& rxInStrm,
|
|
const WMF_EXTERNALHEADER* pExtHeader ) const
|
|
{
|
|
return createGraphicObject( importGraphic( rxInStrm, pExtHeader ) );
|
|
}
|
|
|
|
OUString GraphicHelper::importGraphicObject( const StreamDataSequence& rGraphicData ) const
|
|
{
|
|
return createGraphicObject( importGraphic( rGraphicData ) );
|
|
}
|
|
|
|
OUString GraphicHelper::importEmbeddedGraphicObject( const OUString& rStreamName ) const
|
|
{
|
|
Reference< XGraphic > xGraphic = importEmbeddedGraphic( rStreamName );
|
|
return xGraphic.is() ? createGraphicObject( xGraphic ) : OUString();
|
|
}
|
|
|
|
awt::Size GraphicHelper::getOriginalSize( const Reference< XGraphic >& xGraphic ) const
|
|
{
|
|
awt::Size aSizeHmm;
|
|
PropertySet aPropSet( xGraphic );
|
|
if( aPropSet.getProperty( aSizeHmm, PROP_Size100thMM ) && (aSizeHmm.Width == 0) && (aSizeHmm.Height == 0) ) // MAPMODE_PIXEL used?
|
|
{
|
|
awt::Size aSizePixel( 0, 0 );
|
|
if( aPropSet.getProperty( aSizePixel, PROP_SizePixel ) )
|
|
aSizeHmm = convertScreenPixelToHmm( aSizePixel );
|
|
}
|
|
return aSizeHmm;
|
|
}
|
|
|
|
// ============================================================================
|
|
|
|
} // namespace oox
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|