LibreOffice Module oox (master) 1
graphichelper.cxx
Go to the documentation of this file.
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
21
22#include <com/sun/star/awt/Point.hpp>
23#include <com/sun/star/awt/Size.hpp>
24#include <com/sun/star/beans/XPropertySet.hpp>
25#include <com/sun/star/graphic/GraphicProvider.hpp>
26#include <com/sun/star/graphic/GraphicMapper.hpp>
27#include <osl/diagnose.h>
28#include <sal/log.hxx>
31#include <utility>
32#include <vcl/wmfexternal.hxx>
33#include <vcl/svapp.hxx>
34#include <vcl/outdev.hxx>
35#include <tools/gen.hxx>
39#include <oox/token/properties.hxx>
40#include <oox/token/tokens.hxx>
41
42namespace oox {
43
44using namespace ::com::sun::star;
45using namespace ::com::sun::star::beans;
46using namespace ::com::sun::star::frame;
47using namespace ::com::sun::star::graphic;
48using namespace ::com::sun::star::io;
49using namespace ::com::sun::star::lang;
50using namespace ::com::sun::star::uno;
51
52namespace {
53
54sal_Int32 lclConvertScreenPixelToHmm( double fPixel, double fPixelPerHmm )
55{
56 return static_cast< sal_Int32 >( (fPixelPerHmm > 0.0) ? (fPixel / fPixelPerHmm + 0.5) : 0.0 );
57}
58
59} // namespace
60
61GraphicHelper::GraphicHelper( const Reference< XComponentContext >& rxContext, const Reference< XFrame >& /*rxTargetFrame*/, StorageRef xStorage ) :
62 mxContext( rxContext ),
63 mxStorage(std::move( xStorage ))
64{
65 OSL_ENSURE( mxContext.is(), "GraphicHelper::GraphicHelper - missing component context" );
66 if( mxContext.is() )
67 mxGraphicProvider.set( graphic::GraphicProvider::create( mxContext ), uno::UNO_QUERY );
68
70 maSystemPalette[ XML_3dDkShadow ] = Color(0x716F64);
71 maSystemPalette[ XML_3dLight ] = Color(0xF1EFE2);
72 maSystemPalette[ XML_activeBorder ] = Color(0xD4D0C8);
73 maSystemPalette[ XML_activeCaption ] = Color(0x0054E3);
74 maSystemPalette[ XML_appWorkspace ] = Color(0x808080);
75 maSystemPalette[ XML_background ] = Color(0x004E98);
76 maSystemPalette[ XML_btnFace ] = Color(0xECE9D8);
77 maSystemPalette[ XML_btnHighlight ] = Color(0xFFFFFF);
78 maSystemPalette[ XML_btnShadow ] = Color(0xACA899);
79 maSystemPalette[ XML_btnText ] = Color(0x000000);
80 maSystemPalette[ XML_captionText ] = Color(0xFFFFFF);
81 maSystemPalette[ XML_gradientActiveCaption ] = Color(0x3D95FF);
82 maSystemPalette[ XML_gradientInactiveCaption ] = Color(0xD8E4F8);
83 maSystemPalette[ XML_grayText ] = Color(0xACA899);
84 maSystemPalette[ XML_highlight ] = Color(0x316AC5);
85 maSystemPalette[ XML_highlightText ] = Color(0xFFFFFF);
86 maSystemPalette[ XML_hotLight ] = Color(0x000080);
87 maSystemPalette[ XML_inactiveBorder ] = Color(0xD4D0C8);
88 maSystemPalette[ XML_inactiveCaption ] = Color(0x7A96DF);
89 maSystemPalette[ XML_inactiveCaptionText ] = Color(0xD8E4F8);
90 maSystemPalette[ XML_infoBk ] = Color(0xFFFFE1);
91 maSystemPalette[ XML_infoText ] = Color(0x000000);
92 maSystemPalette[ XML_menu ] = Color(0xFFFFFF);
93 maSystemPalette[ XML_menuBar ] = Color(0xECE9D8);
94 maSystemPalette[ XML_menuHighlight ] = Color(0x316AC5);
95 maSystemPalette[ XML_menuText ] = Color(0x000000);
96 maSystemPalette[ XML_scrollBar ] = Color(0xD4D0C8);
97 maSystemPalette[ XML_window ] = Color(0xFFFFFF);
98 maSystemPalette[ XML_windowFrame ] = Color(0x000000);
99 maSystemPalette[ XML_windowText ] = Color(0x000000);
100
101 // Note that we cannot try to get DeviceInfo from the current frame here,
102 // because there might not be a current frame yet
103 mxDefaultOutputDevice = Application::GetDefaultDevice();
104 maDeviceInfo = mxDefaultOutputDevice->GetDeviceInfo();
105 // 100 000 is 1 meter in MM100.
106 // various unit tests rely on these values being exactly this and not the "true" values
107 Size aDefault = mxDefaultOutputDevice->LogicToPixel(Size(100000, 100000), MapMode(MapUnit::Map100thMM));
108 maDeviceInfo.PixelPerMeterX = aDefault.Width();
109 maDeviceInfo.PixelPerMeterY = aDefault.Height();
110 mfPixelPerHmmX = maDeviceInfo.PixelPerMeterX / 100000.0;
111 mfPixelPerHmmY = maDeviceInfo.PixelPerMeterY / 100000.0;
112}
113
114GraphicHelper::~GraphicHelper()
115{
116}
117
118// System colors and predefined colors ----------------------------------------
119
120::Color GraphicHelper::getSystemColor( sal_Int32 nToken, ::Color nDefaultRgb ) const
121{
122 return ContainerHelper::getMapElement( maSystemPalette, nToken, nDefaultRgb );
123}
124
125::Color GraphicHelper::getSchemeColor( sal_Int32 /*nToken*/ ) const
126{
127 OSL_FAIL( "GraphicHelper::getSchemeColor - scheme colors not implemented" );
128 return API_RGB_TRANSPARENT;
129}
130
131::Color GraphicHelper::getPaletteColor( sal_Int32 /*nPaletteIdx*/ ) const
132{
133 OSL_FAIL( "GraphicHelper::getPaletteColor - palette colors not implemented" );
134 return API_RGB_TRANSPARENT;
135}
136
137sal_Int32 GraphicHelper::getDefaultChartAreaFillStyle() const
138{
139 return XML_solidFill;
140}
141
142sal_Int32 GraphicHelper::getDefaultChartAreaLineStyle()
143{
144 return XML_solidFill;
145}
146
147sal_Int16 GraphicHelper::getDefaultChartAreaLineWidth()
148{
149 // this value is what MSO 2016 writes fixing incomplete MSO 2010 documents (0.75 pt in emu)
150 return 9525;
151}
152
153// Device info and device dependent unit conversion ---------------------------
154
155sal_Int32 GraphicHelper::convertScreenPixelXToHmm( double fPixelX ) const
156{
157 return lclConvertScreenPixelToHmm( fPixelX, mfPixelPerHmmX );
158}
159
160sal_Int32 GraphicHelper::convertScreenPixelYToHmm( double fPixelY ) const
161{
162 return lclConvertScreenPixelToHmm( fPixelY, mfPixelPerHmmY );
163}
164
165awt::Size GraphicHelper::convertScreenPixelToHmm( const awt::Size& rPixel ) const
166{
167 return awt::Size( convertScreenPixelXToHmm( rPixel.Width ), convertScreenPixelYToHmm( rPixel.Height ) );
168}
169
170double GraphicHelper::convertHmmToScreenPixelX( sal_Int32 nHmmX ) const
171{
172 return nHmmX * mfPixelPerHmmX;
173}
174
175double GraphicHelper::convertHmmToScreenPixelY( sal_Int32 nHmmY ) const
176{
177 return nHmmY * mfPixelPerHmmY;
178}
179
180awt::Point GraphicHelper::convertHmmToScreenPixel( const awt::Point& rHmm ) const
181{
182 return awt::Point(
183 static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.X ) + 0.5 ),
184 static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Y ) + 0.5 ) );
185}
186
187awt::Size GraphicHelper::convertHmmToScreenPixel( const awt::Size& rHmm ) const
188{
189 return awt::Size(
190 static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.Width ) + 0.5 ),
191 static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Height ) + 0.5 ) );
192}
193
194awt::Point GraphicHelper::convertHmmToAppFont( const awt::Point& rHmm ) const
195{
196 try
197 {
198 awt::Point aPixel = convertHmmToScreenPixel( rHmm );
199 MapMode aMode(MapUnit::MapAppFont);
200 ::Point aVCLPoint(aPixel.X, aPixel.Y);
201 ::Point aDevPoint = mxDefaultOutputDevice->PixelToLogic(aVCLPoint, aMode );
202 return awt::Point(aDevPoint.X(), aDevPoint.Y());
203 }
204 catch( Exception& )
205 {
207 }
208 return awt::Point( 0, 0 );
209}
210
211awt::Size GraphicHelper::convertHmmToAppFont( const awt::Size& rHmm ) const
212{
213 try
214 {
215 awt::Size aPixel = convertHmmToScreenPixel( rHmm );
216 MapMode aMode(MapUnit::MapAppFont);
217 ::Size aVCLSize(aPixel.Width, aPixel.Height);
218 ::Size aDevSz = mxDefaultOutputDevice->PixelToLogic(aVCLSize, aMode );
219 return awt::Size(aDevSz.Width(), aDevSz.Height());
220 }
221 catch( Exception& )
222 {
224 }
225 return awt::Size( 0, 0 );
226}
227
228
229// Graphics and graphic objects ----------------------------------------------
230
231Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm,
232 const WmfExternal* pExtHeader ) const
233{
234 Reference< XGraphic > xGraphic;
235 if( rxInStrm.is() && mxGraphicProvider.is() ) try
236 {
237 Sequence< PropertyValue > aArgs{ comphelper::makePropertyValue("InputStream", rxInStrm),
238 comphelper::makePropertyValue("LazyRead", true) };
239
240 if ( pExtHeader && pExtHeader->mapMode > 0 )
241 {
242 aArgs.realloc( aArgs.getLength() + 1 );
243 auto pArgs = aArgs.getArray();
244 Sequence< PropertyValue > aFilterData{
245 comphelper::makePropertyValue("ExternalWidth", pExtHeader->xExt),
246 comphelper::makePropertyValue("ExternalHeight", pExtHeader->yExt),
247 comphelper::makePropertyValue("ExternalMapMode", pExtHeader->mapMode)
248 };
249 pArgs[ 2 ].Name = "FilterData";
250 pArgs[ 2 ].Value <<= aFilterData;
251 }
252
253 xGraphic = mxGraphicProvider->queryGraphic( aArgs );
254 }
255 catch( Exception& )
256 {
257 }
258 return xGraphic;
259}
260
261Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rGraphicData ) const
262{
263 Reference< XGraphic > xGraphic;
264 if( rGraphicData.hasElements() )
265 {
266 Reference< XInputStream > xInStrm( new ::comphelper::SequenceInputStream( rGraphicData ) );
267 xGraphic = importGraphic( xInStrm );
268 }
269 return xGraphic;
270}
271
272Reference< XGraphic > GraphicHelper::importEmbeddedGraphic( const OUString& rStreamName, const WmfExternal* pExtHeader ) const
273{
274 Reference< XGraphic > xGraphic;
275 OSL_ENSURE( !rStreamName.isEmpty(), "GraphicHelper::importEmbeddedGraphic - empty stream name" );
276
277 if( !rStreamName.isEmpty() )
278 {
279 initializeGraphicMapperIfNeeded();
280
281 SAL_WARN_IF(!mxGraphicMapper.is(), "oox", "GraphicHelper::importEmbeddedGraphic - graphic mapper not available");
282
283 xGraphic = mxGraphicMapper->findGraphic(rStreamName);
284 if (!xGraphic.is())
285 {
286 auto xStream = mxStorage->openInputStream(rStreamName);
287 xGraphic = importGraphic(xStream, pExtHeader);
288 if (xGraphic.is())
289 mxGraphicMapper->putGraphic(rStreamName, xGraphic);
290 }
291 }
292 return xGraphic;
293}
294
295awt::Size GraphicHelper::getOriginalSize( const Reference< XGraphic >& xGraphic ) const
296{
297 awt::Size aSizeHmm;
298 PropertySet aPropSet( xGraphic );
299 if( aPropSet.getProperty( aSizeHmm, PROP_Size100thMM ) && (aSizeHmm.Width == 0) && (aSizeHmm.Height == 0) ) // MAPMODE_PIXEL used?
300 {
301 awt::Size aSizePixel( 0, 0 );
302 if( aPropSet.getProperty( aSizePixel, PROP_SizePixel ) )
303 aSizeHmm = convertScreenPixelToHmm( aSizePixel );
304 }
305 return aSizeHmm;
306}
307
308void GraphicHelper::setGraphicMapper(css::uno::Reference<css::graphic::XGraphicMapper> const & rGraphicMapper)
309{
310 mxGraphicMapper = rGraphicMapper;
311}
312
313void GraphicHelper::initializeGraphicMapperIfNeeded() const
314{
315 if (!mxGraphicMapper.is())
316 {
317 auto* pNonConstThis = const_cast<GraphicHelper*>(this);
318 pNonConstThis->mxGraphicMapper = graphic::GraphicMapper::create(mxContext);
319 }
320}
321
322} // namespace oox
323
324/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XInputStream > xStream
static OutputDevice * GetDefaultDevice()
constexpr tools::Long Y() const
constexpr tools::Long X() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
Provides helper functions for colors, device measurement conversion, graphics, and graphic objects ha...
css::uno::Reference< css::graphic::XGraphicMapper > mxGraphicMapper
GraphicHelper(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::frame::XFrame > &rxTargetFrame, StorageRef xStorage)
A wrapper for a UNO property set.
Definition: propertyset.hxx:58
bool getProperty(Type &orValue, sal_Int32 nPropId) const
Gets the specified property from the property set.
Definition: propertyset.hxx:94
#define DBG_UNHANDLED_EXCEPTION(...)
uno::Reference< uno::XComponentContext > mxContext
#define SAL_WARN_IF(condition, area, stream)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
std::shared_ptr< StorageBase > StorageRef
Definition: storagebase.hxx:42
css::uno::Sequence< sal_Int8 > StreamDataSequence
const ::Color API_RGB_TRANSPARENT(ColorTransparency, 0xffffffff)
Transparent color for API calls.
Reference< XNameContainer > mxStorage
Definition: olestorage.cxx:76
DefTokenId nToken
sal_uInt16 yExt
sal_uInt16 mapMode
sal_uInt16 xExt