1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <oox/helper/graphichelper.hxx>
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 <comphelper/propertyvalue.hxx>
30 #include <comphelper/seqstream.hxx>
32 #include <vcl/wmfexternal.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/outdev.hxx>
35 #include <tools/gen.hxx>
36 #include <comphelper/diagnose_ex.hxx>
37 #include <oox/helper/containerhelper.hxx>
38 #include <oox/helper/propertyset.hxx>
39 #include <oox/token/properties.hxx>
40 #include <oox/token/tokens.hxx>
44 using namespace ::com::sun::star
;
45 using namespace ::com::sun::star::beans
;
46 using namespace ::com::sun::star::frame
;
47 using namespace ::com::sun::star::graphic
;
48 using namespace ::com::sun::star::io
;
49 using namespace ::com::sun::star::uno
;
53 sal_Int32
lclConvertScreenPixelToHmm( double fPixel
, double fPixelPerHmm
)
55 return static_cast< sal_Int32
>( (fPixelPerHmm
> 0.0) ? (fPixel
/ fPixelPerHmm
+ 0.5) : 0.0 );
60 GraphicHelper::GraphicHelper( const Reference
< XComponentContext
>& rxContext
, const Reference
< XFrame
>& /*rxTargetFrame*/, StorageRef xStorage
) :
61 mxContext( rxContext
),
62 mxStorage(std::move( xStorage
))
64 OSL_ENSURE( mxContext
.is(), "GraphicHelper::GraphicHelper - missing component context" );
66 mxGraphicProvider
.set( graphic::GraphicProvider::create( mxContext
), uno::UNO_QUERY
);
68 //! TODO: get colors from system
69 maSystemPalette
[ XML_3dDkShadow
] = Color(0x716F64);
70 maSystemPalette
[ XML_3dLight
] = Color(0xF1EFE2);
71 maSystemPalette
[ XML_activeBorder
] = Color(0xD4D0C8);
72 maSystemPalette
[ XML_activeCaption
] = Color(0x0054E3);
73 maSystemPalette
[ XML_appWorkspace
] = Color(0x808080);
74 maSystemPalette
[ XML_background
] = Color(0x004E98);
75 maSystemPalette
[ XML_btnFace
] = Color(0xECE9D8);
76 maSystemPalette
[ XML_btnHighlight
] = Color(0xFFFFFF);
77 maSystemPalette
[ XML_btnShadow
] = Color(0xACA899);
78 maSystemPalette
[ XML_btnText
] = Color(0x000000);
79 maSystemPalette
[ XML_captionText
] = Color(0xFFFFFF);
80 maSystemPalette
[ XML_gradientActiveCaption
] = Color(0x3D95FF);
81 maSystemPalette
[ XML_gradientInactiveCaption
] = Color(0xD8E4F8);
82 maSystemPalette
[ XML_grayText
] = Color(0xACA899);
83 maSystemPalette
[ XML_highlight
] = Color(0x316AC5);
84 maSystemPalette
[ XML_highlightText
] = Color(0xFFFFFF);
85 maSystemPalette
[ XML_hotLight
] = Color(0x000080);
86 maSystemPalette
[ XML_inactiveBorder
] = Color(0xD4D0C8);
87 maSystemPalette
[ XML_inactiveCaption
] = Color(0x7A96DF);
88 maSystemPalette
[ XML_inactiveCaptionText
] = Color(0xD8E4F8);
89 maSystemPalette
[ XML_infoBk
] = Color(0xFFFFE1);
90 maSystemPalette
[ XML_infoText
] = Color(0x000000);
91 maSystemPalette
[ XML_menu
] = Color(0xFFFFFF);
92 maSystemPalette
[ XML_menuBar
] = Color(0xECE9D8);
93 maSystemPalette
[ XML_menuHighlight
] = Color(0x316AC5);
94 maSystemPalette
[ XML_menuText
] = Color(0x000000);
95 maSystemPalette
[ XML_scrollBar
] = Color(0xD4D0C8);
96 maSystemPalette
[ XML_window
] = Color(0xFFFFFF);
97 maSystemPalette
[ XML_windowFrame
] = Color(0x000000);
98 maSystemPalette
[ XML_windowText
] = Color(0x000000);
100 // Note that we cannot try to get DeviceInfo from the current frame here,
101 // because there might not be a current frame yet
102 mxDefaultOutputDevice
= Application::GetDefaultDevice();
103 maDeviceInfo
= mxDefaultOutputDevice
->GetDeviceInfo();
104 // 100 000 is 1 meter in MM100.
105 // various unit tests rely on these values being exactly this and not the "true" values
106 Size aDefault
= mxDefaultOutputDevice
->LogicToPixel(Size(100000, 100000), MapMode(MapUnit::Map100thMM
));
107 maDeviceInfo
.PixelPerMeterX
= aDefault
.Width();
108 maDeviceInfo
.PixelPerMeterY
= aDefault
.Height();
109 mfPixelPerHmmX
= maDeviceInfo
.PixelPerMeterX
/ 100000.0;
110 mfPixelPerHmmY
= maDeviceInfo
.PixelPerMeterY
/ 100000.0;
113 GraphicHelper::~GraphicHelper()
117 // System colors and predefined colors ----------------------------------------
119 ::Color
GraphicHelper::getSystemColor( sal_Int32 nToken
, ::Color nDefaultRgb
) const
121 return ContainerHelper::getMapElement( maSystemPalette
, nToken
, nDefaultRgb
);
124 void GraphicHelper::getSchemeColorToken(sal_Int32
& /*nToken*/) const {}
126 ::Color
GraphicHelper::getSchemeColor( sal_Int32
/*nToken*/ ) const
128 OSL_FAIL( "GraphicHelper::getSchemeColor - scheme colors not implemented" );
129 return API_RGB_TRANSPARENT
;
132 ::Color
GraphicHelper::getPaletteColor( sal_Int32
/*nPaletteIdx*/ ) const
134 OSL_FAIL( "GraphicHelper::getPaletteColor - palette colors not implemented" );
135 return API_RGB_TRANSPARENT
;
138 sal_Int32
GraphicHelper::getDefaultChartAreaFillStyle() const
140 return XML_solidFill
;
143 sal_Int32
GraphicHelper::getDefaultChartAreaLineStyle()
145 return XML_solidFill
;
148 sal_Int16
GraphicHelper::getDefaultChartAreaLineWidth()
150 // this value is what MSO 2016 writes fixing incomplete MSO 2010 documents (0.75 pt in emu)
154 // Device info and device dependent unit conversion ---------------------------
156 sal_Int32
GraphicHelper::convertScreenPixelXToHmm( double fPixelX
) const
158 return lclConvertScreenPixelToHmm( fPixelX
, mfPixelPerHmmX
);
161 sal_Int32
GraphicHelper::convertScreenPixelYToHmm( double fPixelY
) const
163 return lclConvertScreenPixelToHmm( fPixelY
, mfPixelPerHmmY
);
166 awt::Size
GraphicHelper::convertScreenPixelToHmm( const awt::Size
& rPixel
) const
168 return awt::Size( convertScreenPixelXToHmm( rPixel
.Width
), convertScreenPixelYToHmm( rPixel
.Height
) );
171 double GraphicHelper::convertHmmToScreenPixelX( sal_Int32 nHmmX
) const
173 return nHmmX
* mfPixelPerHmmX
;
176 double GraphicHelper::convertHmmToScreenPixelY( sal_Int32 nHmmY
) const
178 return nHmmY
* mfPixelPerHmmY
;
181 awt::Point
GraphicHelper::convertHmmToScreenPixel( const awt::Point
& rHmm
) const
184 static_cast< sal_Int32
>( convertHmmToScreenPixelX( rHmm
.X
) + 0.5 ),
185 static_cast< sal_Int32
>( convertHmmToScreenPixelY( rHmm
.Y
) + 0.5 ) );
188 awt::Size
GraphicHelper::convertHmmToScreenPixel( const awt::Size
& rHmm
) const
191 static_cast< sal_Int32
>( convertHmmToScreenPixelX( rHmm
.Width
) + 0.5 ),
192 static_cast< sal_Int32
>( convertHmmToScreenPixelY( rHmm
.Height
) + 0.5 ) );
195 awt::Point
GraphicHelper::convertHmmToAppFont( const awt::Point
& rHmm
) const
199 awt::Point aPixel
= convertHmmToScreenPixel( rHmm
);
200 MapMode
aMode(MapUnit::MapAppFont
);
201 ::Point
aVCLPoint(aPixel
.X
, aPixel
.Y
);
202 ::Point aDevPoint
= mxDefaultOutputDevice
->PixelToLogic(aVCLPoint
, aMode
);
203 return awt::Point(aDevPoint
.X(), aDevPoint
.Y());
207 DBG_UNHANDLED_EXCEPTION("oox");
209 return awt::Point( 0, 0 );
212 awt::Size
GraphicHelper::convertHmmToAppFont( const awt::Size
& rHmm
) const
216 awt::Size aPixel
= convertHmmToScreenPixel( rHmm
);
217 MapMode
aMode(MapUnit::MapAppFont
);
218 ::Size
aVCLSize(aPixel
.Width
, aPixel
.Height
);
219 ::Size aDevSz
= mxDefaultOutputDevice
->PixelToLogic(aVCLSize
, aMode
);
220 return awt::Size(aDevSz
.Width(), aDevSz
.Height());
224 DBG_UNHANDLED_EXCEPTION("oox");
226 return awt::Size( 0, 0 );
230 // Graphics and graphic objects ----------------------------------------------
232 Reference
< XGraphic
> GraphicHelper::importGraphic( const Reference
< XInputStream
>& rxInStrm
,
233 const WmfExternal
* pExtHeader
, const bool bLazyLoad
) const
235 Reference
< XGraphic
> xGraphic
;
236 if( rxInStrm
.is() && mxGraphicProvider
.is() ) try
238 Sequence
< PropertyValue
> aArgs
{ comphelper::makePropertyValue(u
"InputStream"_ustr
, rxInStrm
),
239 comphelper::makePropertyValue(u
"LazyRead"_ustr
, bLazyLoad
) };
241 if ( pExtHeader
&& pExtHeader
->mapMode
> 0 )
243 aArgs
.realloc( aArgs
.getLength() + 1 );
244 auto pArgs
= aArgs
.getArray();
245 Sequence
< PropertyValue
> aFilterData
{
246 comphelper::makePropertyValue(u
"ExternalWidth"_ustr
, pExtHeader
->xExt
),
247 comphelper::makePropertyValue(u
"ExternalHeight"_ustr
, pExtHeader
->yExt
),
248 comphelper::makePropertyValue(u
"ExternalMapMode"_ustr
, pExtHeader
->mapMode
)
250 pArgs
[ 2 ].Name
= "FilterData";
251 pArgs
[ 2 ].Value
<<= aFilterData
;
254 xGraphic
= mxGraphicProvider
->queryGraphic( aArgs
);
262 Reference
< XGraphic
> GraphicHelper::importGraphic( const StreamDataSequence
& rGraphicData
) const
264 Reference
< XGraphic
> xGraphic
;
265 if( rGraphicData
.hasElements() )
267 Reference
< XInputStream
> xInStrm( new ::comphelper::SequenceInputStream( rGraphicData
) );
268 xGraphic
= importGraphic( xInStrm
);
273 Reference
< XGraphic
> GraphicHelper::importEmbeddedGraphic( const OUString
& rStreamName
, const WmfExternal
* pExtHeader
) const
275 Reference
< XGraphic
> xGraphic
;
276 OSL_ENSURE( !rStreamName
.isEmpty(), "GraphicHelper::importEmbeddedGraphic - empty stream name" );
278 if( !rStreamName
.isEmpty() )
280 initializeGraphicMapperIfNeeded();
282 SAL_WARN_IF(!mxGraphicMapper
.is(), "oox", "GraphicHelper::importEmbeddedGraphic - graphic mapper not available");
284 xGraphic
= mxGraphicMapper
->findGraphic(rStreamName
);
287 // Lazy-loading doesn't work with cropped TIFF images, because in case of Lazy-load TIFF images
288 // we are using MapUnit::MapPixel, but in case of cropped images we are using MapUnit::Map100thMM
289 // and the crop values are relative to original bitmap size.
290 auto xStream
= mxStorage
->openInputStream(rStreamName
);
291 xGraphic
= importGraphic(xStream
, pExtHeader
, !rStreamName
.endsWith(".tiff"));
293 mxGraphicMapper
->putGraphic(rStreamName
, xGraphic
);
299 awt::Size
GraphicHelper::getOriginalSize( const Reference
< XGraphic
>& xGraphic
) const
302 PropertySet
aPropSet( xGraphic
);
303 if( aPropSet
.getProperty( aSizeHmm
, PROP_Size100thMM
) && (aSizeHmm
.Width
== 0) && (aSizeHmm
.Height
== 0) ) // MAPMODE_PIXEL used?
305 awt::Size
aSizePixel( 0, 0 );
306 if( aPropSet
.getProperty( aSizePixel
, PROP_SizePixel
) )
307 aSizeHmm
= convertScreenPixelToHmm( aSizePixel
);
312 void GraphicHelper::setGraphicMapper(css::uno::Reference
<css::graphic::XGraphicMapper
> const & rGraphicMapper
)
314 mxGraphicMapper
= rGraphicMapper
;
317 void GraphicHelper::initializeGraphicMapperIfNeeded() const
319 if (!mxGraphicMapper
.is())
321 auto* pNonConstThis
= const_cast<GraphicHelper
*>(this);
322 pNonConstThis
->mxGraphicMapper
= graphic::GraphicMapper::create(mxContext
);
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */