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