Branch data 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 : 624 : GraphicHelper::GraphicHelper( const Reference< XComponentContext >& rxContext, const Reference< XFrame >& rxTargetFrame, const StorageRef& rxStorage ) :
69 : : mxContext( rxContext ),
70 : : mxStorage( rxStorage ),
71 [ + - ][ + - ]: 627 : maGraphicObjScheme( CREATE_OUSTRING( "vnd.sun.star.GraphicObject:" ) )
[ + - ][ + - ]
[ + - ]
72 : : {
73 : : OSL_ENSURE( mxContext.is(), "GraphicHelper::GraphicHelper - missing component context" );
74 [ + - ][ + - ]: 624 : Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY );
[ + - ]
75 : : OSL_ENSURE( xFactory.is(), "GraphicHelper::GraphicHelper - missing service factory" );
76 [ + - ]: 624 : if( xFactory.is() )
77 [ + + ][ + - ]: 624 : mxGraphicProvider.set( graphic::GraphicProvider::create( mxContext ) );
78 : :
79 : : //! TODO: get colors from system
80 [ + - ]: 621 : maSystemPalette[ XML_3dDkShadow ] = 0x716F64;
81 [ + - ]: 621 : maSystemPalette[ XML_3dLight ] = 0xF1EFE2;
82 [ + - ]: 621 : maSystemPalette[ XML_activeBorder ] = 0xD4D0C8;
83 [ + - ]: 621 : maSystemPalette[ XML_activeCaption ] = 0x0054E3;
84 [ + - ]: 621 : maSystemPalette[ XML_appWorkspace ] = 0x808080;
85 [ + - ]: 621 : maSystemPalette[ XML_background ] = 0x004E98;
86 [ + - ]: 621 : maSystemPalette[ XML_btnFace ] = 0xECE9D8;
87 [ + - ]: 621 : maSystemPalette[ XML_btnHighlight ] = 0xFFFFFF;
88 [ + - ]: 621 : maSystemPalette[ XML_btnShadow ] = 0xACA899;
89 [ + - ]: 621 : maSystemPalette[ XML_btnText ] = 0x000000;
90 [ + - ]: 621 : maSystemPalette[ XML_captionText ] = 0xFFFFFF;
91 [ + - ]: 621 : maSystemPalette[ XML_gradientActiveCaption ] = 0x3D95FF;
92 [ + - ]: 621 : maSystemPalette[ XML_gradientInactiveCaption ] = 0xD8E4F8;
93 [ + - ]: 621 : maSystemPalette[ XML_grayText ] = 0xACA899;
94 [ + - ]: 621 : maSystemPalette[ XML_highlight ] = 0x316AC5;
95 [ + - ]: 621 : maSystemPalette[ XML_highlightText ] = 0xFFFFFF;
96 [ + - ]: 621 : maSystemPalette[ XML_hotLight ] = 0x000080;
97 [ + - ]: 621 : maSystemPalette[ XML_inactiveBorder ] = 0xD4D0C8;
98 [ + - ]: 621 : maSystemPalette[ XML_inactiveCaption ] = 0x7A96DF;
99 [ + - ]: 621 : maSystemPalette[ XML_inactiveCaptionText ] = 0xD8E4F8;
100 [ + - ]: 621 : maSystemPalette[ XML_infoBk ] = 0xFFFFE1;
101 [ + - ]: 621 : maSystemPalette[ XML_infoText ] = 0x000000;
102 [ + - ]: 621 : maSystemPalette[ XML_menu ] = 0xFFFFFF;
103 [ + - ]: 621 : maSystemPalette[ XML_menuBar ] = 0xECE9D8;
104 [ + - ]: 621 : maSystemPalette[ XML_menuHighlight ] = 0x316AC5;
105 [ + - ]: 621 : maSystemPalette[ XML_menuText ] = 0x000000;
106 [ + - ]: 621 : maSystemPalette[ XML_scrollBar ] = 0xD4D0C8;
107 [ + - ]: 621 : maSystemPalette[ XML_window ] = 0xFFFFFF;
108 [ + - ]: 621 : maSystemPalette[ XML_windowFrame ] = 0x000000;
109 [ + - ]: 621 : 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 : 621 : Reference< XFrame > xFrame = rxTargetFrame;
114 [ + - ][ + + ]: 621 : if( !xFrame.is() && xFactory.is() ) try
[ + + ]
115 : : {
116 [ + - ][ + - ]: 171 : Reference< XFramesSupplier > xFramesSupp( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.frame.Desktop" ) ), UNO_QUERY_THROW );
[ + - ][ + + ]
117 [ + - ][ + - ]: 171 : xFrame = xFramesSupp->getActiveFrame();
[ - + ][ + - ]
118 : : }
119 [ + - ]: 33 : 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 : 621 : maDeviceInfo.PixelPerMeterX = maDeviceInfo.PixelPerMeterY = 3500.0; // some default just in case
126 [ + + ]: 621 : if( xFrame.is() ) try
127 : : {
128 [ + - ][ + - ]: 469 : Reference< awt::XDevice > xDevice( xFrame->getContainerWindow(), UNO_QUERY_THROW );
[ + - ]
129 [ + - ]: 469 : mxUnitConversion.set( xDevice, UNO_QUERY );
130 : : OSL_ENSURE( mxUnitConversion.is(), "GraphicHelper::GraphicHelper - cannot get unit converter" );
131 [ + - ][ + - ]: 469 : maDeviceInfo = xDevice->getInfo();
[ # # ]
132 : : }
133 [ # # ]: 0 : catch( Exception& )
134 : : {
135 : : OSL_FAIL( "GraphicHelper::GraphicHelper - cannot get output device info" );
136 : : }
137 : 621 : mfPixelPerHmmX = maDeviceInfo.PixelPerMeterX / 100000.0;
138 : 624 : mfPixelPerHmmY = maDeviceInfo.PixelPerMeterY / 100000.0;
139 : 621 : }
140 : :
141 [ + - ]: 621 : GraphicHelper::~GraphicHelper()
142 : : {
143 [ - + ]: 957 : }
144 : :
145 : : // System colors and predefined colors ----------------------------------------
146 : :
147 : 262 : sal_Int32 GraphicHelper::getSystemColor( sal_Int32 nToken, sal_Int32 nDefaultRgb ) const
148 : : {
149 : 262 : 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 : 24 : const awt::DeviceInfo& GraphicHelper::getDeviceInfo() const
167 : : {
168 : 24 : 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 : 42 : Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm,
239 : : const WMF_EXTERNALHEADER* pExtHeader ) const
240 : : {
241 : 42 : Reference< XGraphic > xGraphic;
242 [ + - ][ + - ]: 42 : if( rxInStrm.is() && mxGraphicProvider.is() ) try
[ + - ]
243 : : {
244 [ + - ]: 42 : Sequence< PropertyValue > aArgs( 1 );
245 [ + - ][ + - ]: 42 : aArgs[ 0 ].Name = CREATE_OUSTRING( "InputStream" );
246 [ + - ][ + - ]: 42 : aArgs[ 0 ].Value <<= rxInStrm;
247 : :
248 [ + + ][ - + ]: 42 : if ( pExtHeader && pExtHeader->mapMode > 0 )
249 : : {
250 [ # # ]: 0 : aArgs.realloc( aArgs.getLength() + 1 );
251 [ # # ]: 0 : Sequence< PropertyValue > aFilterData( 3 );
252 [ # # ][ # # ]: 0 : aFilterData[ 0 ].Name = CREATE_OUSTRING( "ExternalWidth" );
253 [ # # ][ # # ]: 0 : aFilterData[ 0 ].Value <<= pExtHeader->xExt;
254 [ # # ][ # # ]: 0 : aFilterData[ 1 ].Name = CREATE_OUSTRING( "ExternalHeight" );
255 [ # # ][ # # ]: 0 : aFilterData[ 1 ].Value <<= pExtHeader->yExt;
256 [ # # ][ # # ]: 0 : aFilterData[ 2 ].Name = CREATE_OUSTRING( "ExternalMapMode" );
257 [ # # ][ # # ]: 0 : aFilterData[ 2 ].Value <<= pExtHeader->mapMode;
258 [ # # ][ # # ]: 0 : aArgs[ 1 ].Name = CREATE_OUSTRING( "FilterData" );
259 [ # # ][ # # ]: 0 : aArgs[ 1 ].Value <<= aFilterData;
[ # # ]
260 : : }
261 : :
262 [ + - ][ + - ]: 42 : xGraphic = mxGraphicProvider->queryGraphic( aArgs );
[ + - ][ + - ]
[ # # ]
263 : : }
264 [ # # ]: 0 : catch( Exception& )
265 : : {
266 : : }
267 : 42 : 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 : 27 : Reference< XGraphic > GraphicHelper::importEmbeddedGraphic( const OUString& rStreamName ) const
282 : : {
283 : 27 : Reference< XGraphic > xGraphic;
284 : : OSL_ENSURE( !rStreamName.isEmpty(), "GraphicHelper::importEmbeddedGraphic - empty stream name" );
285 [ + - ]: 27 : if( !rStreamName.isEmpty() )
286 : : {
287 [ + - ]: 27 : EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find( rStreamName );
288 [ + - ]: 27 : if( aIt == maEmbeddedGraphics.end() )
289 : : {
290 [ + - ][ + - ]: 27 : xGraphic = importGraphic( mxStorage->openInputStream( rStreamName ) );
[ + - ]
291 [ + - ]: 27 : if( xGraphic.is() )
292 [ + - ][ + - ]: 27 : maEmbeddedGraphics[ rStreamName ] = xGraphic;
293 : : }
294 : : else
295 [ # # ]: 27 : xGraphic = aIt->second;
296 : : }
297 : 27 : return xGraphic;
298 : : }
299 : :
300 : 42 : OUString GraphicHelper::createGraphicObject( const Reference< XGraphic >& rxGraphic ) const
301 : : {
302 : 42 : OUString aGraphicObjUrl;
303 [ + - ][ + - ]: 42 : if( mxContext.is() && rxGraphic.is() ) try
[ + - ]
304 : : {
305 [ + - ][ + - ]: 42 : Reference< XGraphicObject > xGraphicObj( GraphicObject::create( mxContext ), UNO_SET_THROW );
306 [ + - ][ + - ]: 42 : xGraphicObj->setGraphic( rxGraphic );
307 [ + - ]: 42 : maGraphicObjects.push_back( xGraphicObj );
308 [ + - ][ + - ]: 42 : aGraphicObjUrl = maGraphicObjScheme + xGraphicObj->getUniqueID();
[ # # ]
309 : : }
310 [ # # ]: 0 : catch( Exception& )
311 : : {
312 : : }
313 : 42 : return aGraphicObjUrl;
314 : : }
315 : :
316 : 15 : OUString GraphicHelper::importGraphicObject( const Reference< XInputStream >& rxInStrm,
317 : : const WMF_EXTERNALHEADER* pExtHeader ) const
318 : : {
319 [ + - ]: 15 : 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 : 12 : awt::Size GraphicHelper::getOriginalSize( const Reference< XGraphic >& xGraphic ) const
334 : : {
335 : 12 : awt::Size aSizeHmm;
336 [ + - ]: 12 : PropertySet aPropSet( xGraphic );
337 [ + - ][ + - ]: 12 : 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 [ + - ]: 12 : return aSizeHmm;
344 : : }
345 : :
346 : : // ============================================================================
347 : :
348 [ + - ][ + - ]: 285 : } // namespace oox
349 : :
350 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|