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/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 <osl/diagnose.h>
34 #include <sal/log.hxx>
35 #include <comphelper/seqstream.hxx>
36 #include <vcl/wmf.hxx>
37 #include <vcl/wmfexternal.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/outdev.hxx>
40 #include <tools/gen.hxx>
41 #include <comphelper/propertysequence.hxx>
42 #include <comphelper/sequence.hxx>
43 #include <oox/helper/containerhelper.hxx>
44 #include <oox/helper/propertyset.hxx>
45 #include <oox/token/properties.hxx>
46 #include <oox/token/tokens.hxx>
47 
48 namespace oox {
49 
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::frame;
53 using namespace ::com::sun::star::graphic;
54 using namespace ::com::sun::star::io;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::uno;
57 
58 namespace {
59 
lclConvertScreenPixelToHmm(double fPixel,double fPixelPerHmm)60 sal_Int32 lclConvertScreenPixelToHmm( double fPixel, double fPixelPerHmm )
61 {
62     return static_cast< sal_Int32 >( (fPixelPerHmm > 0.0) ? (fPixel / fPixelPerHmm + 0.5) : 0.0 );
63 }
64 
65 } // namespace
66 
GraphicHelper(const Reference<XComponentContext> & rxContext,const Reference<XFrame> & rxTargetFrame,const StorageRef & rxStorage)67 GraphicHelper::GraphicHelper( const Reference< XComponentContext >& rxContext, const Reference< XFrame >& rxTargetFrame, const StorageRef& rxStorage ) :
68     mxContext( rxContext ),
69     mxStorage( rxStorage )
70 {
71     OSL_ENSURE( mxContext.is(), "GraphicHelper::GraphicHelper - missing component context" );
72     if( mxContext.is() )
73         mxGraphicProvider.set( graphic::GraphicProvider::create( mxContext ), uno::UNO_QUERY );
74 
75     //! TODO: get colors from system
76     maSystemPalette[ XML_3dDkShadow ]               = Color(0x716F64);
77     maSystemPalette[ XML_3dLight ]                  = Color(0xF1EFE2);
78     maSystemPalette[ XML_activeBorder ]             = Color(0xD4D0C8);
79     maSystemPalette[ XML_activeCaption ]            = Color(0x0054E3);
80     maSystemPalette[ XML_appWorkspace ]             = Color(0x808080);
81     maSystemPalette[ XML_background ]               = Color(0x004E98);
82     maSystemPalette[ XML_btnFace ]                  = Color(0xECE9D8);
83     maSystemPalette[ XML_btnHighlight ]             = Color(0xFFFFFF);
84     maSystemPalette[ XML_btnShadow ]                = Color(0xACA899);
85     maSystemPalette[ XML_btnText ]                  = Color(0x000000);
86     maSystemPalette[ XML_captionText ]              = Color(0xFFFFFF);
87     maSystemPalette[ XML_gradientActiveCaption ]    = Color(0x3D95FF);
88     maSystemPalette[ XML_gradientInactiveCaption ]  = Color(0xD8E4F8);
89     maSystemPalette[ XML_grayText ]                 = Color(0xACA899);
90     maSystemPalette[ XML_highlight ]                = Color(0x316AC5);
91     maSystemPalette[ XML_highlightText ]            = Color(0xFFFFFF);
92     maSystemPalette[ XML_hotLight ]                 = Color(0x000080);
93     maSystemPalette[ XML_inactiveBorder ]           = Color(0xD4D0C8);
94     maSystemPalette[ XML_inactiveCaption ]          = Color(0x7A96DF);
95     maSystemPalette[ XML_inactiveCaptionText ]      = Color(0xD8E4F8);
96     maSystemPalette[ XML_infoBk ]                   = Color(0xFFFFE1);
97     maSystemPalette[ XML_infoText ]                 = Color(0x000000);
98     maSystemPalette[ XML_menu ]                     = Color(0xFFFFFF);
99     maSystemPalette[ XML_menuBar ]                  = Color(0xECE9D8);
100     maSystemPalette[ XML_menuHighlight ]            = Color(0x316AC5);
101     maSystemPalette[ XML_menuText ]                 = Color(0x000000);
102     maSystemPalette[ XML_scrollBar ]                = Color(0xD4D0C8);
103     maSystemPalette[ XML_window ]                   = Color(0xFFFFFF);
104     maSystemPalette[ XML_windowFrame ]              = Color(0x000000);
105     maSystemPalette[ XML_windowText ]               = Color(0x000000);
106 
107     // if no target frame has been passed (e.g. OLE objects), try to fallback to the active frame
108     // TODO: we need some mechanism to keep and pass the parent frame
109     Reference< XFrame > xFrame = rxTargetFrame;
110     if( !xFrame.is() && mxContext.is() ) try
111     {
112         Reference< XDesktop2 > xFramesSupp = Desktop::create( mxContext );
113         xFrame = xFramesSupp->getActiveFrame();
114     }
115     catch( Exception& )
116     {
117     }
118 
119     // get the metric of the output device
120     OSL_ENSURE( xFrame.is(), "GraphicHelper::GraphicHelper - cannot get target frame" );
121     // some default just in case, 100 000 is 1 meter in MM100
122     Size aDefault = Application::GetDefaultDevice()->LogicToPixel(Size(100000, 100000), MapMode(MapUnit::Map100thMM));
123     maDeviceInfo.PixelPerMeterX = aDefault.Width();
124     maDeviceInfo.PixelPerMeterY = aDefault.Height();
125     if( xFrame.is() ) try
126     {
127         Reference< awt::XDevice > xDevice( xFrame->getContainerWindow(), UNO_QUERY_THROW );
128         mxUnitConversion.set( xDevice, UNO_QUERY );
129         OSL_ENSURE( mxUnitConversion.is(), "GraphicHelper::GraphicHelper - cannot get unit converter" );
130         maDeviceInfo = xDevice->getInfo();
131     }
132     catch( Exception& )
133     {
134         OSL_FAIL( "GraphicHelper::GraphicHelper - cannot get output device info" );
135     }
136     mfPixelPerHmmX = maDeviceInfo.PixelPerMeterX / 100000.0;
137     mfPixelPerHmmY = maDeviceInfo.PixelPerMeterY / 100000.0;
138 }
139 
~GraphicHelper()140 GraphicHelper::~GraphicHelper()
141 {
142 }
143 
144 // System colors and predefined colors ----------------------------------------
145 
getSystemColor(sal_Int32 nToken,::Color nDefaultRgb) const146 ::Color GraphicHelper::getSystemColor( sal_Int32 nToken, ::Color nDefaultRgb ) const
147 {
148     return ContainerHelper::getMapElement( maSystemPalette, nToken, nDefaultRgb );
149 }
150 
getSchemeColor(sal_Int32) const151 ::Color GraphicHelper::getSchemeColor( sal_Int32 /*nToken*/ ) const
152 {
153     OSL_FAIL( "GraphicHelper::getSchemeColor - scheme colors not implemented" );
154     return API_RGB_TRANSPARENT;
155 }
156 
getPaletteColor(sal_Int32) const157 ::Color GraphicHelper::getPaletteColor( sal_Int32 /*nPaletteIdx*/ ) const
158 {
159     OSL_FAIL( "GraphicHelper::getPaletteColor - palette colors not implemented" );
160     return API_RGB_TRANSPARENT;
161 }
162 
getDefaultChartAreaFillStyle() const163 sal_Int32 GraphicHelper::getDefaultChartAreaFillStyle() const
164 {
165     return XML_solidFill;
166 }
167 
getDefaultChartAreaLineStyle()168 sal_Int32 GraphicHelper::getDefaultChartAreaLineStyle()
169 {
170     return XML_solidFill;
171 }
172 
getDefaultChartAreaLineWidth()173 sal_Int16 GraphicHelper::getDefaultChartAreaLineWidth()
174 {
175     // this value is what MSO 2016 writes fixing incomplete MSO 2010 documents (0.75 pt in emu)
176     return 9525;
177 }
178 
179 // Device info and device dependent unit conversion ---------------------------
180 
convertScreenPixelXToHmm(double fPixelX) const181 sal_Int32 GraphicHelper::convertScreenPixelXToHmm( double fPixelX ) const
182 {
183     return lclConvertScreenPixelToHmm( fPixelX, mfPixelPerHmmX );
184 }
185 
convertScreenPixelYToHmm(double fPixelY) const186 sal_Int32 GraphicHelper::convertScreenPixelYToHmm( double fPixelY ) const
187 {
188     return lclConvertScreenPixelToHmm( fPixelY, mfPixelPerHmmY );
189 }
190 
convertScreenPixelToHmm(const awt::Size & rPixel) const191 awt::Size GraphicHelper::convertScreenPixelToHmm( const awt::Size& rPixel ) const
192 {
193     return awt::Size( convertScreenPixelXToHmm( rPixel.Width ), convertScreenPixelYToHmm( rPixel.Height ) );
194 }
195 
convertHmmToScreenPixelX(sal_Int32 nHmmX) const196 double GraphicHelper::convertHmmToScreenPixelX( sal_Int32 nHmmX ) const
197 {
198     return nHmmX * mfPixelPerHmmX;
199 }
200 
convertHmmToScreenPixelY(sal_Int32 nHmmY) const201 double GraphicHelper::convertHmmToScreenPixelY( sal_Int32 nHmmY ) const
202 {
203     return nHmmY * mfPixelPerHmmY;
204 }
205 
convertHmmToScreenPixel(const awt::Point & rHmm) const206 awt::Point GraphicHelper::convertHmmToScreenPixel( const awt::Point& rHmm ) const
207 {
208     return awt::Point(
209         static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.X ) + 0.5 ),
210         static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Y ) + 0.5 ) );
211 }
212 
convertHmmToScreenPixel(const awt::Size & rHmm) const213 awt::Size GraphicHelper::convertHmmToScreenPixel( const awt::Size& rHmm ) const
214 {
215     return awt::Size(
216         static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.Width ) + 0.5 ),
217         static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Height ) + 0.5 ) );
218 }
219 
convertHmmToAppFont(const awt::Point & rHmm) const220 awt::Point GraphicHelper::convertHmmToAppFont( const awt::Point& rHmm ) const
221 {
222     if( mxUnitConversion.is() ) try
223     {
224         awt::Point aPixel = convertHmmToScreenPixel( rHmm );
225         return mxUnitConversion->convertPointToLogic( aPixel, css::util::MeasureUnit::APPFONT );
226     }
227     catch( Exception& )
228     {
229     }
230     return awt::Point( 0, 0 );
231 }
232 
convertHmmToAppFont(const awt::Size & rHmm) const233 awt::Size GraphicHelper::convertHmmToAppFont( const awt::Size& rHmm ) const
234 {
235     if( mxUnitConversion.is() ) try
236     {
237         awt::Size aPixel = convertHmmToScreenPixel( rHmm );
238         return mxUnitConversion->convertSizeToLogic( aPixel, css::util::MeasureUnit::APPFONT );
239     }
240     catch( Exception& )
241     {
242     }
243     return awt::Size( 0, 0 );
244 }
245 
246 // Graphics and graphic objects  ----------------------------------------------
247 
importGraphic(const Reference<XInputStream> & rxInStrm,const WmfExternal * pExtHeader) const248 Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm,
249         const WmfExternal* pExtHeader ) const
250 {
251     Reference< XGraphic > xGraphic;
252     if( rxInStrm.is() && mxGraphicProvider.is() ) try
253     {
254         Sequence< PropertyValue > aArgs( 2 );
255         aArgs[ 0 ].Name = "InputStream";
256         aArgs[ 0 ].Value <<= rxInStrm;
257         aArgs[ 1 ].Name = "LazyRead";
258         bool bLazyRead = !pExtHeader;
259         aArgs[ 1 ].Value <<= bLazyRead;
260 
261         if ( pExtHeader && pExtHeader->mapMode > 0 )
262         {
263             aArgs.realloc( aArgs.getLength() + 1 );
264             Sequence< PropertyValue > aFilterData( 3 );
265             aFilterData[ 0 ].Name = "ExternalWidth";
266             aFilterData[ 0 ].Value <<= pExtHeader->xExt;
267             aFilterData[ 1 ].Name = "ExternalHeight";
268             aFilterData[ 1 ].Value <<= pExtHeader->yExt;
269             aFilterData[ 2 ].Name = "ExternalMapMode";
270             aFilterData[ 2 ].Value <<= pExtHeader->mapMode;
271             aArgs[ 2 ].Name = "FilterData";
272             aArgs[ 2 ].Value <<= aFilterData;
273         }
274 
275         xGraphic = mxGraphicProvider->queryGraphic( aArgs );
276     }
277     catch( Exception& )
278     {
279     }
280     return xGraphic;
281 }
282 
importGraphics(const std::vector<uno::Reference<io::XInputStream>> & rStreams) const283 std::vector< uno::Reference<graphic::XGraphic> > GraphicHelper::importGraphics(const std::vector< uno::Reference<io::XInputStream> >& rStreams) const
284 {
285     std::vector< uno::Sequence<beans::PropertyValue> > aArgsVec;
286 
287     for (const auto& rStream : rStreams)
288     {
289         uno::Sequence<beans::PropertyValue > aArgs = comphelper::InitPropertySequence(
290         {
291             {"InputStream", uno::makeAny(rStream)}
292         });
293         aArgsVec.push_back(aArgs);
294     }
295 
296     std::vector< uno::Reference<graphic::XGraphic> > aRet;
297 
298     if (mxGraphicProvider.is())
299         aRet = comphelper::sequenceToContainer< std::vector< uno::Reference<graphic::XGraphic> > >(mxGraphicProvider->queryGraphics(comphelper::containerToSequence(aArgsVec)));
300 
301     return aRet;
302 }
303 
importGraphic(const StreamDataSequence & rGraphicData) const304 Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rGraphicData ) const
305 {
306     Reference< XGraphic > xGraphic;
307     if( rGraphicData.hasElements() )
308     {
309         Reference< XInputStream > xInStrm( new ::comphelper::SequenceInputStream( rGraphicData ) );
310         xGraphic = importGraphic( xInStrm );
311     }
312     return xGraphic;
313 }
314 
importEmbeddedGraphics(const std::vector<OUString> & rStreamNames) const315 void GraphicHelper::importEmbeddedGraphics(const std::vector<OUString>& rStreamNames) const
316 {
317     // Don't actually return anything, just fill maEmbeddedGraphics.
318 
319     // Stream names and streams to be imported.
320     std::vector<OUString> aMissingStreamNames;
321     std::vector< uno::Reference<io::XInputStream> > aMissingStreams;
322 
323     for (const auto& rStreamName : rStreamNames)
324     {
325         if(rStreamName.isEmpty())
326         {
327             SAL_WARN("oox", "GraphicHelper::importEmbeddedGraphics - empty stream name");
328             continue;
329         }
330 
331         EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find(rStreamName);
332         if (aIt == maEmbeddedGraphics.end())
333         {
334             aMissingStreamNames.push_back(rStreamName);
335             aMissingStreams.push_back(mxStorage->openInputStream(rStreamName));
336         }
337     }
338 
339     std::vector< uno::Reference<graphic::XGraphic> > aGraphics = importGraphics(aMissingStreams);
340 
341     assert(aGraphics.size() == aMissingStreamNames.size());
342     for (size_t i = 0; i < aGraphics.size(); ++i)
343     {
344         if (aGraphics[i].is())
345             maEmbeddedGraphics[aMissingStreamNames[i]] = aGraphics[i];
346     }
347 }
348 
importEmbeddedGraphic(const OUString & rStreamName,const WmfExternal * pExtHeader) const349 Reference< XGraphic > GraphicHelper::importEmbeddedGraphic( const OUString& rStreamName, const WmfExternal* pExtHeader ) const
350 {
351     Reference< XGraphic > xGraphic;
352     OSL_ENSURE( !rStreamName.isEmpty(), "GraphicHelper::importEmbeddedGraphic - empty stream name" );
353     if( !rStreamName.isEmpty() )
354     {
355         EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find( rStreamName );
356         if( aIt == maEmbeddedGraphics.end() )
357         {
358             // Lazy-loading doesn't work with TIFF or WMF at the moment.
359             WmfExternal aHeader;
360             if ( (rStreamName.endsWith(".tiff") || rStreamName.endsWith(".wmf") ) && !pExtHeader)
361                 pExtHeader = &aHeader;
362 
363             xGraphic = importGraphic(mxStorage->openInputStream(rStreamName), pExtHeader);
364             if( xGraphic.is() )
365                 maEmbeddedGraphics[ rStreamName ] = xGraphic;
366         }
367         else
368             xGraphic = aIt->second;
369     }
370     return xGraphic;
371 }
372 
getOriginalSize(const Reference<XGraphic> & xGraphic) const373 awt::Size GraphicHelper::getOriginalSize( const Reference< XGraphic >& xGraphic ) const
374 {
375     awt::Size aSizeHmm;
376     PropertySet aPropSet( xGraphic );
377     if( aPropSet.getProperty( aSizeHmm, PROP_Size100thMM ) && (aSizeHmm.Width == 0) && (aSizeHmm.Height == 0) )     // MAPMODE_PIXEL used?
378     {
379         awt::Size aSizePixel( 0, 0 );
380         if( aPropSet.getProperty( aSizePixel, PROP_SizePixel ) )
381             aSizeHmm = convertScreenPixelToHmm( aSizePixel );
382     }
383     return aSizeHmm;
384 }
385 
386 } // namespace oox
387 
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
389