/src/libreoffice/oox/source/helper/graphichelper.cxx
Line | Count | Source |
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 <oox/helper/graphichelper.hxx> |
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> |
29 | | #include <tools/mapunit.hxx> |
30 | | #include <comphelper/propertyvalue.hxx> |
31 | | #include <comphelper/seqstream.hxx> |
32 | | #include <utility> |
33 | | #include <vcl/wmfexternal.hxx> |
34 | | #include <vcl/svapp.hxx> |
35 | | #include <vcl/outdev.hxx> |
36 | | #include <tools/gen.hxx> |
37 | | #include <comphelper/diagnose_ex.hxx> |
38 | | #include <oox/helper/containerhelper.hxx> |
39 | | #include <oox/helper/propertyset.hxx> |
40 | | #include <oox/token/properties.hxx> |
41 | | #include <oox/token/tokens.hxx> |
42 | | |
43 | | namespace oox { |
44 | | |
45 | | using namespace ::com::sun::star; |
46 | | using namespace ::com::sun::star::beans; |
47 | | using namespace ::com::sun::star::frame; |
48 | | using namespace ::com::sun::star::graphic; |
49 | | using namespace ::com::sun::star::io; |
50 | | using namespace ::com::sun::star::uno; |
51 | | |
52 | | namespace { |
53 | | |
54 | | sal_Int32 lclConvertScreenPixelToHmm( double fPixel, double fPixelPerHmm ) |
55 | 1.39k | { |
56 | 1.39k | return static_cast< sal_Int32 >( (fPixelPerHmm > 0.0) ? (fPixel / fPixelPerHmm + 0.5) : 0.0 ); |
57 | 1.39k | } |
58 | | |
59 | | } // namespace |
60 | | |
61 | | GraphicHelper::GraphicHelper( const Reference< XComponentContext >& rxContext, const Reference< XFrame >& /*rxTargetFrame*/, StorageRef xStorage ) : |
62 | 29.3k | mxContext( rxContext ), |
63 | 29.3k | mxStorage(std::move( xStorage )) |
64 | 29.3k | { |
65 | 29.3k | OSL_ENSURE( mxContext.is(), "GraphicHelper::GraphicHelper - missing component context" ); |
66 | 29.3k | if( mxContext.is() ) |
67 | 29.3k | mxGraphicProvider.set( graphic::GraphicProvider::create( mxContext ), uno::UNO_QUERY ); |
68 | | |
69 | | //! TODO: get colors from system |
70 | 29.3k | maSystemPalette[ XML_3dDkShadow ] = Color(0x716F64); |
71 | 29.3k | maSystemPalette[ XML_3dLight ] = Color(0xF1EFE2); |
72 | 29.3k | maSystemPalette[ XML_activeBorder ] = Color(0xD4D0C8); |
73 | 29.3k | maSystemPalette[ XML_activeCaption ] = Color(0x0054E3); |
74 | 29.3k | maSystemPalette[ XML_appWorkspace ] = Color(0x808080); |
75 | 29.3k | maSystemPalette[ XML_background ] = Color(0x004E98); |
76 | 29.3k | maSystemPalette[ XML_btnFace ] = Color(0xECE9D8); |
77 | 29.3k | maSystemPalette[ XML_btnHighlight ] = Color(0xFFFFFF); |
78 | 29.3k | maSystemPalette[ XML_btnShadow ] = Color(0xACA899); |
79 | 29.3k | maSystemPalette[ XML_btnText ] = Color(0x000000); |
80 | 29.3k | maSystemPalette[ XML_captionText ] = Color(0xFFFFFF); |
81 | 29.3k | maSystemPalette[ XML_gradientActiveCaption ] = Color(0x3D95FF); |
82 | 29.3k | maSystemPalette[ XML_gradientInactiveCaption ] = Color(0xD8E4F8); |
83 | 29.3k | maSystemPalette[ XML_grayText ] = Color(0xACA899); |
84 | 29.3k | maSystemPalette[ XML_highlight ] = Color(0x316AC5); |
85 | 29.3k | maSystemPalette[ XML_highlightText ] = Color(0xFFFFFF); |
86 | 29.3k | maSystemPalette[ XML_hotLight ] = Color(0x000080); |
87 | 29.3k | maSystemPalette[ XML_inactiveBorder ] = Color(0xD4D0C8); |
88 | 29.3k | maSystemPalette[ XML_inactiveCaption ] = Color(0x7A96DF); |
89 | 29.3k | maSystemPalette[ XML_inactiveCaptionText ] = Color(0xD8E4F8); |
90 | 29.3k | maSystemPalette[ XML_infoBk ] = Color(0xFFFFE1); |
91 | 29.3k | maSystemPalette[ XML_infoText ] = Color(0x000000); |
92 | 29.3k | maSystemPalette[ XML_menu ] = Color(0xFFFFFF); |
93 | 29.3k | maSystemPalette[ XML_menuBar ] = Color(0xECE9D8); |
94 | 29.3k | maSystemPalette[ XML_menuHighlight ] = Color(0x316AC5); |
95 | 29.3k | maSystemPalette[ XML_menuText ] = Color(0x000000); |
96 | 29.3k | maSystemPalette[ XML_scrollBar ] = Color(0xD4D0C8); |
97 | 29.3k | maSystemPalette[ XML_window ] = Color(0xFFFFFF); |
98 | 29.3k | maSystemPalette[ XML_windowFrame ] = Color(0x000000); |
99 | 29.3k | 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 | 29.3k | mxDefaultOutputDevice = Application::GetDefaultDevice(); |
104 | 29.3k | 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 | 29.3k | Size aDefault = mxDefaultOutputDevice->LogicToPixel(Size(100000, 100000), MapMode(MapUnit::Map100thMM)); |
108 | 29.3k | maDeviceInfo.PixelPerMeterX = aDefault.Width(); |
109 | 29.3k | maDeviceInfo.PixelPerMeterY = aDefault.Height(); |
110 | 29.3k | mfPixelPerHmmX = maDeviceInfo.PixelPerMeterX / 100000.0; |
111 | 29.3k | mfPixelPerHmmY = maDeviceInfo.PixelPerMeterY / 100000.0; |
112 | 29.3k | } |
113 | | |
114 | | GraphicHelper::~GraphicHelper() |
115 | 29.3k | { |
116 | 29.3k | } |
117 | | |
118 | | // System colors and predefined colors ---------------------------------------- |
119 | | |
120 | | ::Color GraphicHelper::getSystemColor( sal_Int32 nToken, ::Color nDefaultRgb ) const |
121 | 2.97M | { |
122 | 2.97M | return ContainerHelper::getMapElement( maSystemPalette, nToken, nDefaultRgb ); |
123 | 2.97M | } |
124 | | |
125 | 4.15k | void GraphicHelper::getSchemeColorToken(sal_Int32& /*nToken*/) const {} |
126 | | |
127 | | ::Color GraphicHelper::getSchemeColor( sal_Int32 /*nToken*/ ) const |
128 | 0 | { |
129 | 0 | OSL_FAIL( "GraphicHelper::getSchemeColor - scheme colors not implemented" ); |
130 | 0 | return API_RGB_TRANSPARENT; |
131 | 0 | } |
132 | | |
133 | | ::Color GraphicHelper::getPaletteColor( sal_Int32 /*nPaletteIdx*/ ) const |
134 | 0 | { |
135 | 0 | OSL_FAIL( "GraphicHelper::getPaletteColor - palette colors not implemented" ); |
136 | 0 | return API_RGB_TRANSPARENT; |
137 | 0 | } |
138 | | |
139 | | sal_Int32 GraphicHelper::getDefaultChartAreaFillStyle() const |
140 | 0 | { |
141 | 0 | return XML_solidFill; |
142 | 0 | } |
143 | | |
144 | | sal_Int32 GraphicHelper::getDefaultChartAreaLineStyle() |
145 | 0 | { |
146 | 0 | return XML_solidFill; |
147 | 0 | } |
148 | | |
149 | | sal_Int16 GraphicHelper::getDefaultChartAreaLineWidth() |
150 | 0 | { |
151 | | // this value is what MSO 2016 writes fixing incomplete MSO 2010 documents (0.75 pt in emu) |
152 | 0 | return 9525; |
153 | 0 | } |
154 | | |
155 | | // Device info and device dependent unit conversion --------------------------- |
156 | | |
157 | | sal_Int32 GraphicHelper::convertScreenPixelXToHmm( double fPixelX ) const |
158 | 698 | { |
159 | 698 | return lclConvertScreenPixelToHmm( fPixelX, mfPixelPerHmmX ); |
160 | 698 | } |
161 | | |
162 | | sal_Int32 GraphicHelper::convertScreenPixelYToHmm( double fPixelY ) const |
163 | 699 | { |
164 | 699 | return lclConvertScreenPixelToHmm( fPixelY, mfPixelPerHmmY ); |
165 | 699 | } |
166 | | |
167 | | awt::Size GraphicHelper::convertScreenPixelToHmm( const awt::Size& rPixel ) const |
168 | 698 | { |
169 | 698 | return awt::Size( convertScreenPixelXToHmm( rPixel.Width ), convertScreenPixelYToHmm( rPixel.Height ) ); |
170 | 698 | } |
171 | | |
172 | | double GraphicHelper::convertHmmToScreenPixelX( sal_Int32 nHmmX ) const |
173 | 0 | { |
174 | 0 | return nHmmX * mfPixelPerHmmX; |
175 | 0 | } |
176 | | |
177 | | double GraphicHelper::convertHmmToScreenPixelY( sal_Int32 nHmmY ) const |
178 | 0 | { |
179 | 0 | return nHmmY * mfPixelPerHmmY; |
180 | 0 | } |
181 | | |
182 | | awt::Point GraphicHelper::convertHmmToScreenPixel( const awt::Point& rHmm ) const |
183 | 0 | { |
184 | 0 | return awt::Point( |
185 | 0 | static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.X ) + 0.5 ), |
186 | 0 | static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Y ) + 0.5 ) ); |
187 | 0 | } |
188 | | |
189 | | awt::Size GraphicHelper::convertHmmToScreenPixel( const awt::Size& rHmm ) const |
190 | 0 | { |
191 | 0 | return awt::Size( |
192 | 0 | static_cast< sal_Int32 >( convertHmmToScreenPixelX( rHmm.Width ) + 0.5 ), |
193 | 0 | static_cast< sal_Int32 >( convertHmmToScreenPixelY( rHmm.Height ) + 0.5 ) ); |
194 | 0 | } |
195 | | |
196 | | awt::Point GraphicHelper::convertHmmToAppFont( const awt::Point& rHmm ) const |
197 | 0 | { |
198 | 0 | try |
199 | 0 | { |
200 | 0 | awt::Point aPixel = convertHmmToScreenPixel( rHmm ); |
201 | 0 | MapMode aMode(MapUnit::MapAppFont); |
202 | 0 | ::Point aVCLPoint(aPixel.X, aPixel.Y); |
203 | 0 | ::Point aDevPoint = mxDefaultOutputDevice->PixelToLogic(aVCLPoint, aMode ); |
204 | 0 | return awt::Point(aDevPoint.X(), aDevPoint.Y()); |
205 | 0 | } |
206 | 0 | catch( Exception& ) |
207 | 0 | { |
208 | 0 | DBG_UNHANDLED_EXCEPTION("oox"); |
209 | 0 | } |
210 | 0 | return awt::Point( 0, 0 ); |
211 | 0 | } |
212 | | |
213 | | awt::Size GraphicHelper::convertHmmToAppFont( const awt::Size& rHmm ) const |
214 | 0 | { |
215 | 0 | try |
216 | 0 | { |
217 | 0 | awt::Size aPixel = convertHmmToScreenPixel( rHmm ); |
218 | 0 | MapMode aMode(MapUnit::MapAppFont); |
219 | 0 | ::Size aVCLSize(aPixel.Width, aPixel.Height); |
220 | 0 | ::Size aDevSz = mxDefaultOutputDevice->PixelToLogic(aVCLSize, aMode ); |
221 | 0 | return awt::Size(aDevSz.Width(), aDevSz.Height()); |
222 | 0 | } |
223 | 0 | catch( Exception& ) |
224 | 0 | { |
225 | 0 | DBG_UNHANDLED_EXCEPTION("oox"); |
226 | 0 | } |
227 | 0 | return awt::Size( 0, 0 ); |
228 | 0 | } |
229 | | |
230 | | |
231 | | // Graphics and graphic objects ---------------------------------------------- |
232 | | |
233 | | Reference< XGraphic > GraphicHelper::importGraphic( const Reference< XInputStream >& rxInStrm, |
234 | | const WmfExternal* pExtHeader, const bool bLazyLoad ) const |
235 | 1.90k | { |
236 | 1.90k | Reference< XGraphic > xGraphic; |
237 | 1.90k | if( rxInStrm.is() && mxGraphicProvider.is() ) try |
238 | 978 | { |
239 | 978 | Sequence< PropertyValue > aArgs{ comphelper::makePropertyValue(u"InputStream"_ustr, rxInStrm), |
240 | 978 | comphelper::makePropertyValue(u"LazyRead"_ustr, bLazyLoad) }; |
241 | | |
242 | 978 | if ( pExtHeader && pExtHeader->mapMode > 0 ) |
243 | 14 | { |
244 | 14 | aArgs.realloc( aArgs.getLength() + 1 ); |
245 | 14 | auto pArgs = aArgs.getArray(); |
246 | 14 | Sequence< PropertyValue > aFilterData{ |
247 | 14 | comphelper::makePropertyValue(u"ExternalWidth"_ustr, pExtHeader->xExt), |
248 | 14 | comphelper::makePropertyValue(u"ExternalHeight"_ustr, pExtHeader->yExt), |
249 | 14 | comphelper::makePropertyValue(u"ExternalMapMode"_ustr, pExtHeader->mapMode) |
250 | 14 | }; |
251 | 14 | pArgs[ 2 ].Name = "FilterData"; |
252 | 14 | pArgs[ 2 ].Value <<= aFilterData; |
253 | 14 | } |
254 | | |
255 | 978 | xGraphic = mxGraphicProvider->queryGraphic( aArgs ); |
256 | 978 | } |
257 | 978 | catch( Exception& ) |
258 | 978 | { |
259 | 0 | } |
260 | 1.90k | return xGraphic; |
261 | 1.90k | } |
262 | | |
263 | | Reference< XGraphic > GraphicHelper::importGraphic( const StreamDataSequence& rGraphicData ) const |
264 | 0 | { |
265 | 0 | Reference< XGraphic > xGraphic; |
266 | 0 | if( rGraphicData.hasElements() ) |
267 | 0 | { |
268 | 0 | Reference< XInputStream > xInStrm( new ::comphelper::SequenceInputStream( rGraphicData ) ); |
269 | 0 | xGraphic = importGraphic( xInStrm ); |
270 | 0 | } |
271 | 0 | return xGraphic; |
272 | 0 | } |
273 | | |
274 | | Reference< XGraphic > GraphicHelper::importEmbeddedGraphic( const OUString& rStreamName, const WmfExternal* pExtHeader ) const |
275 | 3.07k | { |
276 | 3.07k | Reference< XGraphic > xGraphic; |
277 | 3.07k | OSL_ENSURE( !rStreamName.isEmpty(), "GraphicHelper::importEmbeddedGraphic - empty stream name" ); |
278 | | |
279 | 3.07k | if( !rStreamName.isEmpty() ) |
280 | 3.07k | { |
281 | 3.07k | initializeGraphicMapperIfNeeded(); |
282 | | |
283 | 3.07k | SAL_WARN_IF(!mxGraphicMapper.is(), "oox", "GraphicHelper::importEmbeddedGraphic - graphic mapper not available"); |
284 | | |
285 | 3.07k | xGraphic = mxGraphicMapper->findGraphic(rStreamName); |
286 | 3.07k | if (!xGraphic.is()) |
287 | 1.88k | { |
288 | | // Lazy-loading doesn't work with cropped TIFF images, because in case of Lazy-load TIFF images |
289 | | // we are using MapUnit::MapPixel, but in case of cropped images we are using MapUnit::Map100thMM |
290 | | // and the crop values are relative to original bitmap size. |
291 | 1.88k | auto xStream = mxStorage->openInputStream(rStreamName); |
292 | 1.88k | bool bLazyLoad = !rStreamName.endsWithIgnoreAsciiCase(".tiff") |
293 | 1.88k | && !rStreamName.endsWithIgnoreAsciiCase(".tif"); |
294 | 1.88k | xGraphic = importGraphic(xStream, pExtHeader, bLazyLoad); |
295 | 1.88k | if (xGraphic.is()) |
296 | 805 | mxGraphicMapper->putGraphic(rStreamName, xGraphic); |
297 | 1.88k | } |
298 | 3.07k | } |
299 | 3.07k | return xGraphic; |
300 | 3.07k | } |
301 | | |
302 | | awt::Size GraphicHelper::getOriginalSize( const Reference< XGraphic >& xGraphic ) const |
303 | 759 | { |
304 | 759 | awt::Size aSizeHmm; |
305 | 759 | PropertySet aPropSet( xGraphic ); |
306 | 759 | if( aPropSet.getProperty( aSizeHmm, PROP_Size100thMM ) && (aSizeHmm.Width == 0) && (aSizeHmm.Height == 0) ) // MAPMODE_PIXEL used? |
307 | 687 | { |
308 | 687 | awt::Size aSizePixel( 0, 0 ); |
309 | 687 | if( aPropSet.getProperty( aSizePixel, PROP_SizePixel ) ) |
310 | 687 | aSizeHmm = convertScreenPixelToHmm( aSizePixel ); |
311 | 687 | } |
312 | 759 | return aSizeHmm; |
313 | 759 | } |
314 | | |
315 | | void GraphicHelper::setGraphicMapper(css::uno::Reference<css::graphic::XGraphicMapper> const & rGraphicMapper) |
316 | 1.00k | { |
317 | 1.00k | mxGraphicMapper = rGraphicMapper; |
318 | 1.00k | } |
319 | | |
320 | | void GraphicHelper::initializeGraphicMapperIfNeeded() const |
321 | 3.07k | { |
322 | 3.07k | if (!mxGraphicMapper.is()) |
323 | 228 | { |
324 | 228 | auto* pNonConstThis = const_cast<GraphicHelper*>(this); |
325 | 228 | pNonConstThis->mxGraphicMapper = graphic::GraphicMapper::create(mxContext); |
326 | 228 | } |
327 | 3.07k | } |
328 | | |
329 | | } // namespace oox |
330 | | |
331 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |