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