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