1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/helper/graphichelper.hxx"
29 
30 #include <com/sun/star/awt/Point.hpp>
31 #include <com/sun/star/awt/Size.hpp>
32 #include <com/sun/star/awt/XDevice.hpp>
33 #include <com/sun/star/awt/XUnitConversion.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/frame/XFramesSupplier.hpp>
36 #include <com/sun/star/graphic/GraphicObject.hpp>
37 #include <com/sun/star/graphic/XGraphicProvider.hpp>
38 #include <com/sun/star/util/MeasureUnit.hpp>
39 #include <comphelper/seqstream.hxx>
40 #include "oox/helper/containerhelper.hxx"
41 #include "oox/helper/propertyset.hxx"
42 #include "oox/token/tokens.hxx"
43 
44 namespace oox {
45 
46 // ============================================================================
47 
48 using namespace ::com::sun::star::awt;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::frame;
51 using namespace ::com::sun::star::graphic;
52 using namespace ::com::sun::star::io;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::uno;
55 
56 using ::rtl::OUString;
57 
58 // ============================================================================
59 
60 namespace {
61 
62 inline sal_Int32 lclConvertScreenPixelToHmm( double fPixel, double fPixelPerHmm )
63 {
64     return static_cast< sal_Int32 >( (fPixelPerHmm > 0.0) ? (fPixel / fPixelPerHmm + 0.5) : 0.0 );
65 }
66 
67 } // namespace
68 
69 // ============================================================================
70 
71 GraphicHelper::GraphicHelper( const Reference< XComponentContext >& rxContext, const Reference< XFrame >& rxTargetFrame, const StorageRef& rxStorage ) :
72     mxContext( rxContext ),
73     mxStorage( rxStorage ),
74     maGraphicObjScheme( CREATE_OUSTRING( "vnd.sun.star.GraphicObject:" ) )
75 {
76     OSL_ENSURE( mxContext.is(), "GraphicHelper::GraphicHelper - missing component context" );
77     Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY );
78     OSL_ENSURE( xFactory.is(), "GraphicHelper::GraphicHelper - missing service factory" );
79     if( xFactory.is() )
80         mxGraphicProvider.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.graphic.GraphicProvider" ) ), UNO_QUERY );
81 
82     //! TODO: get colors from system
83     maSystemPalette[ XML_3dDkShadow ]               = 0x716F64;
84     maSystemPalette[ XML_3dLight ]                  = 0xF1EFE2;
85     maSystemPalette[ XML_activeBorder ]             = 0xD4D0C8;
86     maSystemPalette[ XML_activeCaption ]            = 0x0054E3;
87     maSystemPalette[ XML_appWorkspace ]             = 0x808080;
88     maSystemPalette[ XML_background ]               = 0x004E98;
89     maSystemPalette[ XML_btnFace ]                  = 0xECE9D8;
90     maSystemPalette[ XML_btnHighlight ]             = 0xFFFFFF;
91     maSystemPalette[ XML_btnShadow ]                = 0xACA899;
92     maSystemPalette[ XML_btnText ]                  = 0x000000;
93     maSystemPalette[ XML_captionText ]              = 0xFFFFFF;
94     maSystemPalette[ XML_gradientActiveCaption ]    = 0x3D95FF;
95     maSystemPalette[ XML_gradientInactiveCaption ]  = 0xD8E4F8;
96     maSystemPalette[ XML_grayText ]                 = 0xACA899;
97     maSystemPalette[ XML_highlight ]                = 0x316AC5;
98     maSystemPalette[ XML_highlightText ]            = 0xFFFFFF;
99     maSystemPalette[ XML_hotLight ]                 = 0x000080;
100     maSystemPalette[ XML_inactiveBorder ]           = 0xD4D0C8;
101     maSystemPalette[ XML_inactiveCaption ]          = 0x7A96DF;
102     maSystemPalette[ XML_inactiveCaptionText ]      = 0xD8E4F8;
103     maSystemPalette[ XML_infoBk ]                   = 0xFFFFE1;
104     maSystemPalette[ XML_infoText ]                 = 0x000000;
105     maSystemPalette[ XML_menu ]                     = 0xFFFFFF;
106     maSystemPalette[ XML_menuBar ]                  = 0xECE9D8;
107     maSystemPalette[ XML_menuHighlight ]            = 0x316AC5;
108     maSystemPalette[ XML_menuText ]                 = 0x000000;
109     maSystemPalette[ XML_scrollBar ]                = 0xD4D0C8;
110     maSystemPalette[ XML_window ]                   = 0xFFFFFF;
111     maSystemPalette[ XML_windowFrame ]              = 0x000000;
112     maSystemPalette[ XML_windowText ]               = 0x000000;
113 
114     // if no target frame has been passed (e.g. OLE objects), try to fallback to the active frame
115     // TODO: we need some mechanism to keep and pass the parent frame
116     Reference< XFrame > xFrame = rxTargetFrame;
117     if( !xFrame.is() && xFactory.is() ) try
118     {
119         Reference< XFramesSupplier > xFramesSupp( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.frame.Desktop" ) ), UNO_QUERY_THROW );
120         xFrame = xFramesSupp->getActiveFrame();
121     }
122     catch( Exception& )
123     {
124     }
125 
126     // get the metric of the output device
127     OSL_ENSURE( xFrame.is(), "GraphicHelper::GraphicHelper - cannot get target frame" );
128     maDeviceInfo.PixelPerMeterX = maDeviceInfo.PixelPerMeterY = 3500.0; // some default just in case
129     if( xFrame.is() ) try
130     {
131         Reference< XDevice > xDevice( xFrame->getContainerWindow(), UNO_QUERY_THROW );
132         mxUnitConversion.set( xDevice, UNO_QUERY );
133         OSL_ENSURE( mxUnitConversion.is(), "GraphicHelper::GraphicHelper - cannot get unit converter" );
134         maDeviceInfo = xDevice->getInfo();
135     }
136     catch( Exception& )
137     {
138         OSL_ENSURE( false, "GraphicHelper::GraphicHelper - cannot get output device info" );
139     }
140     mfPixelPerHmmX = maDeviceInfo.PixelPerMeterX / 100000.0;
141     mfPixelPerHmmY = maDeviceInfo.PixelPerMeterY / 100000.0;
142 }
143 
144 GraphicHelper::~GraphicHelper()
145 {
146 }
147 
148 // System colors and predefined colors ----------------------------------------
149 
150 sal_Int32 GraphicHelper::getSystemColor( sal_Int32 nToken, sal_Int32 nDefaultRgb ) const
151 {
152     return ContainerHelper::getMapElement( maSystemPalette, nToken, nDefaultRgb );
153 }
154 
155 sal_Int32 GraphicHelper::getSchemeColor( sal_Int32 /*nToken*/ ) const
156 {
157     OSL_ENSURE( false, "GraphicHelper::getSchemeColor - scheme colors not implemented" );
158     return API_RGB_TRANSPARENT;
159 }
160 
161 sal_Int32 GraphicHelper::getPaletteColor( sal_Int32 /*nPaletteIdx*/ ) const
162 {
163     OSL_ENSURE( false, "GraphicHelper::getPaletteColor - palette colors not implemented" );
164     return API_RGB_TRANSPARENT;
165 }
166 
167 // Device info and device dependent unit conversion ---------------------------
168 
169 const DeviceInfo& GraphicHelper::getDeviceInfo() const
170 {
171     return maDeviceInfo;
172 }
173 
174 sal_Int32 GraphicHelper::convertScreenPixelXToHmm( double fPixelX ) const
175 {
176     return lclConvertScreenPixelToHmm( fPixelX, mfPixelPerHmmX );
177 }
178 
179 sal_Int32 GraphicHelper::convertScreenPixelYToHmm( double fPixelY ) const
180 {
181     return lclConvertScreenPixelToHmm( fPixelY, mfPixelPerHmmY );
182 }
183 
184 Point GraphicHelper::convertScreenPixelToHmm( const Point& rPixel ) const
185 {
186     return Point( convertScreenPixelXToHmm( rPixel.X ), convertScreenPixelYToHmm( rPixel.Y ) );
187 }
188 
189 Size GraphicHelper::convertScreenPixelToHmm( const Size& rPixel ) const
190 {
191     return Size( convertScreenPixelXToHmm( rPixel.Width ), convertScreenPixelYToHmm( rPixel.Height ) );
192 }
193 
194 double GraphicHelper::convertHmmToScreenPixelX( sal_Int32 nHmmX ) const
195 {
196     return nHmmX * mfPixelPerHmmX;
197 }
198 
199 double GraphicHelper::convertHmmToScreenPixelY( sal_Int32 nHmmY ) const
200 {
201     return nHmmY * mfPixelPerHmmY;
202 }
203 
204 Point GraphicHelper::convertHmmToScreenPixel( const Point& rHmm ) const
205 {
206     return Point(
207         static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.X ) + 0.5 ),
208         static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Y ) + 0.5 ) );
209 }
210 
211 Size GraphicHelper::convertHmmToScreenPixel( const Size& rHmm ) const
212 {
213     return Size(
214         static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.Width ) + 0.5 ),
215         static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Height ) + 0.5 ) );
216 }
217 
218 Point GraphicHelper::convertAppFontToHmm( const Point& rAppFont ) const
219 {
220     if( mxUnitConversion.is() ) try
221     {
222         Point aPixel = mxUnitConversion->convertPointToPixel( rAppFont, ::com::sun::star::util::MeasureUnit::APPFONT );
223         return convertScreenPixelToHmm( aPixel );
224     }
225     catch( Exception& )
226     {
227     }
228     return Point( 0, 0 );
229 }
230 
231 Size GraphicHelper::convertAppFontToHmm( const Size& rAppFont ) const
232 {
233     if( mxUnitConversion.is() ) try
234     {
235         Size aPixel = mxUnitConversion->convertSizeToPixel( rAppFont, ::com::sun::star::util::MeasureUnit::APPFONT );
236         return convertScreenPixelToHmm( aPixel );
237     }
238     catch( Exception& )
239     {
240     }
241     return Size( 0, 0 );
242 }
243 
244 Point GraphicHelper::convertHmmToAppFont( const Point& rHmm ) const
245 {
246     if( mxUnitConversion.is() ) try
247     {
248         Point aPixel = convertHmmToScreenPixel( rHmm );
249         return mxUnitConversion->convertPointToLogic( aPixel, ::com::sun::star::util::MeasureUnit::APPFONT );
250     }
251     catch( Exception& )
252     {
253     }
254     return Point( 0, 0 );
255 }
256 
257 Size GraphicHelper::convertHmmToAppFont( const Size& rHmm ) const
258 {
259     if( mxUnitConversion.is() ) try
260     {
261         Size aPixel = convertHmmToScreenPixel( rHmm );
262         return mxUnitConversion->convertSizeToLogic( aPixel, ::com::sun::star::util::MeasureUnit::APPFONT );
263     }
264     catch( Exception& )
265     {
266     }
267     return Size( 0, 0 );
268 }
269 
270 // Graphics and graphic objects  ----------------------------------------------
271 
272 Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm ) const
273 {
274     Reference< XGraphic > xGraphic;
275     if( rxInStrm.is() && mxGraphicProvider.is() ) try
276     {
277         Sequence< PropertyValue > aArgs( 1 );
278         aArgs[ 0 ].Name = CREATE_OUSTRING( "InputStream" );
279         aArgs[ 0 ].Value <<= rxInStrm;
280         xGraphic = mxGraphicProvider->queryGraphic( aArgs );
281     }
282     catch( Exception& )
283     {
284     }
285     return xGraphic;
286 }
287 
288 Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rGraphicData ) const
289 {
290     Reference< XGraphic > xGraphic;
291     if( rGraphicData.hasElements() )
292     {
293         Reference< XInputStream > xInStrm( new ::comphelper::SequenceInputStream( rGraphicData ) );
294         xGraphic = importGraphic( xInStrm );
295     }
296     return xGraphic;
297 }
298 
299 Reference< XGraphic > GraphicHelper::importEmbeddedGraphic( const OUString& rStreamName ) const
300 {
301     Reference< XGraphic > xGraphic;
302     OSL_ENSURE( rStreamName.getLength() > 0, "GraphicHelper::importEmbeddedGraphic - empty stream name" );
303     if( rStreamName.getLength() > 0 )
304     {
305         EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find( rStreamName );
306         if( aIt == maEmbeddedGraphics.end() )
307         {
308             xGraphic = importGraphic( mxStorage->openInputStream( rStreamName ) );
309             if( xGraphic.is() )
310                 maEmbeddedGraphics[ rStreamName ] = xGraphic;
311         }
312         else
313             xGraphic = aIt->second;
314     }
315     return xGraphic;
316 }
317 
318 OUString GraphicHelper::createGraphicObject( const Reference< XGraphic >& rxGraphic ) const
319 {
320     OUString aGraphicObjUrl;
321     if( mxContext.is() && rxGraphic.is() ) try
322     {
323         Reference< XGraphicObject > xGraphicObj( GraphicObject::create( mxContext ), UNO_SET_THROW );
324         xGraphicObj->setGraphic( rxGraphic );
325         maGraphicObjects.push_back( xGraphicObj );
326         aGraphicObjUrl = maGraphicObjScheme + xGraphicObj->getUniqueID();
327     }
328     catch( Exception& )
329     {
330     }
331     return aGraphicObjUrl;
332 }
333 
334 OUString GraphicHelper::importGraphicObject( const Reference< XInputStream >& rxInStrm ) const
335 {
336     return createGraphicObject( importGraphic( rxInStrm ) );
337 }
338 
339 OUString GraphicHelper::importGraphicObject( const StreamDataSequence& rGraphicData ) const
340 {
341     return createGraphicObject( importGraphic( rGraphicData ) );
342 }
343 
344 OUString GraphicHelper::importEmbeddedGraphicObject( const OUString& rStreamName ) const
345 {
346     Reference< XGraphic > xGraphic = importEmbeddedGraphic( rStreamName );
347     return xGraphic.is() ? createGraphicObject( xGraphic ) : OUString();
348 }
349 
350 Size GraphicHelper::getOriginalSize( const Reference< XGraphic >& xGraphic ) const
351 {
352     Size aSizeHmm;
353     PropertySet aPropSet( xGraphic );
354     if( aPropSet.getProperty( aSizeHmm, PROP_Size100thMM ) && (aSizeHmm.Width == 0) && (aSizeHmm.Height == 0) )     // MAPMODE_PIXEL used?
355     {
356         Size aSizePixel( 0, 0 );
357         if( aPropSet.getProperty( aSizePixel, PROP_SizePixel ) )
358             aSizeHmm = convertScreenPixelToHmm( aSizePixel );
359     }
360     return aSizeHmm;
361 }
362 
363 // ============================================================================
364 
365 } // namespace oox
366