LibreOffice Module vcl (master) 1
UnoGraphicDescriptor.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
23#include <vcl/graphicfilter.hxx>
25
26#include <com/sun/star/beans/PropertyAttribute.hpp>
27#include <com/sun/star/awt/Size.hpp>
28#include <com/sun/star/graphic/GraphicType.hpp>
29#include <com/sun/star/io/XInputStream.hpp>
30
31#include <vcl/outdev.hxx>
32#include <vcl/graph.hxx>
33#include <vcl/svapp.hxx>
34#include <memory>
35
36namespace {
37
38enum class UnoGraphicProperty
39{
40 GraphicType = 1
41 , MimeType = 2
42 , SizePixel = 3
43 , Size100thMM = 4
44 , BitsPerPixel = 5
45 , Transparent = 6
46 , Alpha = 7
47 , Animated = 8
48 , Linked = 9
49 , OriginURL = 10
50};
51
52}
53
54using namespace ::com::sun::star;
55
56namespace unographic {
57
58
60 ::comphelper::PropertySetHelper( createPropertySetInfo() ),
61 mpGraphic( nullptr ),
63 mnBitsPerPixel ( 0 ),
64 mbTransparent ( false )
65{
66}
67
69 noexcept
70{
71}
72
73void GraphicDescriptor::init( const ::Graphic& rGraphic )
74{
75 mpGraphic = &rGraphic;
76}
77
78void GraphicDescriptor::init( const OUString& rURL )
79{
80 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rURL, StreamMode::READ ));
81
82 if( pIStm )
83 implCreate( *pIStm, &rURL );
84}
85
86void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL )
87{
88 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm ));
89
90 if( pIStm )
91 implCreate( *pIStm, &rURL );
92}
93
94void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL )
95{
96 OUString aURL;
97 if( pURL )
98 aURL = *pURL;
99 ::GraphicDescriptor aDescriptor( rIStm, &aURL );
100
101 mpGraphic = nullptr;
102 maMimeType.clear();
104 mnBitsPerPixel = 0;
105 mbTransparent = false;
106
107 if( !(aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GraphicFileFormat::NOT) )
108 return;
109
110 const char* pMimeType = nullptr;
111 sal_uInt8 cType = graphic::GraphicType::EMPTY;
112
113 switch( aDescriptor.GetFileFormat() )
114 {
115 case GraphicFileFormat::BMP: pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
116 case GraphicFileFormat::GIF: pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
117 case GraphicFileFormat::JPG: pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
118 case GraphicFileFormat::PCD: pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
119 case GraphicFileFormat::PCX: pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
120 case GraphicFileFormat::PNG: pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
121 case GraphicFileFormat::TIF: pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
122 case GraphicFileFormat::XBM: pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
123 case GraphicFileFormat::XPM: pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
124 case GraphicFileFormat::PBM: pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
125 case GraphicFileFormat::PGM: pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
126 case GraphicFileFormat::PPM: pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
127 case GraphicFileFormat::RAS: pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
128 case GraphicFileFormat::TGA: pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
129 case GraphicFileFormat::PSD: pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
130 case GraphicFileFormat::WEBP: pMimeType = MIMETYPE_WEBP; cType = graphic::GraphicType::PIXEL; break;
131
132 case GraphicFileFormat::EPS: pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
133 case GraphicFileFormat::DXF: pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
134 case GraphicFileFormat::MET: pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
135 case GraphicFileFormat::PCT: pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
136 case GraphicFileFormat::SVM: pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
137 case GraphicFileFormat::WMF: pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
138 case GraphicFileFormat::WMZ: pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
139 case GraphicFileFormat::EMF: pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
140 case GraphicFileFormat::EMZ: pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
141 case GraphicFileFormat::SVG: pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
142 case GraphicFileFormat::SVGZ: pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
143
144 default:
145 break;
146 }
147
148 if( graphic::GraphicType::EMPTY != cType )
149 {
150 meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GraphicType::Bitmap : GraphicType::GdiMetafile );
151 maMimeType = OUString( pMimeType, strlen(pMimeType), RTL_TEXTENCODING_ASCII_US );
152 maSizePixel = aDescriptor.GetSizePixel();
153 maSize100thMM = aDescriptor.GetSize_100TH_MM();
154 mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
155 mbTransparent = ( graphic::GraphicType::VECTOR == cType );
156 }
157}
158
159
161{
162 uno::Any aAny;
163
165 aAny <<= uno::Reference< lang::XServiceInfo >(this);
166 else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
167 aAny <<= uno::Reference< lang::XTypeProvider >(this);
168 else if( rType == cppu::UnoType<beans::XPropertySet>::get())
169 aAny <<= uno::Reference< beans::XPropertySet >(this);
171 aAny <<= uno::Reference< beans::XPropertyState >(this);
173 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
174 else
175 aAny = OWeakObject::queryInterface( rType );
176
177 return aAny;
178}
179
180
182 noexcept
183{
184 OWeakObject::acquire();
185}
186
187
189 noexcept
190{
191 OWeakObject::release();
192}
193
194
196{
197 return "com.sun.star.comp.graphic.GraphicDescriptor";
198}
199
200sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
201{
203}
204
205
206uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
207{
208 return { "com.sun.star.graphic.GraphicDescriptor" };
209}
210
211
212uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
213{
214 static const uno::Sequence< uno::Type > aTypes {
221 return aTypes;
222}
223
224uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
225{
226 return css::uno::Sequence<sal_Int8>();
227}
228
229
231{
232 static ::comphelper::PropertyMapEntry const aEntries[] =
233 {
234 { OUString( "GraphicType" ), static_cast< sal_Int32 >( UnoGraphicProperty::GraphicType ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
235 { OUString( "MimeType" ), static_cast< sal_Int32 >( UnoGraphicProperty::MimeType ), cppu::UnoType< OUString >::get(), beans::PropertyAttribute::READONLY, 0 },
236 { OUString( "SizePixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::SizePixel ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
237 { OUString( "Size100thMM" ), static_cast< sal_Int32 >( UnoGraphicProperty::Size100thMM ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
238 { OUString( "BitsPerPixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::BitsPerPixel ), cppu::UnoType< sal_uInt8 >::get(), beans::PropertyAttribute::READONLY, 0 },
239 { OUString( "Transparent" ), static_cast< sal_Int32 >( UnoGraphicProperty::Transparent ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
240 { OUString( "Alpha" ), static_cast< sal_Int32 >( UnoGraphicProperty::Alpha ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
241 { OUString( "Animated" ), static_cast< sal_Int32 >( UnoGraphicProperty::Animated ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
242 { OUString("Linked"), sal_Int32(UnoGraphicProperty::Linked), cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
243 { OUString("OriginURL"), sal_Int32(UnoGraphicProperty::OriginURL), cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
244 };
245
246 return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) );
247}
248
249
250void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
251{
252 // we only have readonly attributes
253}
254
255
257{
258 SolarMutexGuard aGuard;
259
260 while( *ppEntries )
261 {
262 UnoGraphicProperty theProperty = static_cast< UnoGraphicProperty >( (*ppEntries)->mnHandle );
263 switch( theProperty )
264 {
266 {
267 const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
268
269 *pValues <<= ( eType == GraphicType::Bitmap ? graphic::GraphicType::PIXEL :
270 ( eType == GraphicType::GdiMetafile ? graphic::GraphicType::VECTOR :
271 graphic::GraphicType::EMPTY ) );
272 }
273 break;
274
275 case UnoGraphicProperty::MimeType:
276 {
277 OUString aMimeType;
278
279 if( mpGraphic )
280 {
281 if( mpGraphic->IsGfxLink() )
282 {
283 const char* pMimeType;
284
285 switch( mpGraphic->GetGfxLink().GetType() )
286 {
287 case GfxLinkType::NativeGif: pMimeType = MIMETYPE_GIF; break;
288
289 // #i15508# added BMP type for better exports (checked, works)
290 case GfxLinkType::NativeBmp: pMimeType = MIMETYPE_BMP; break;
291
292 case GfxLinkType::NativeJpg: pMimeType = MIMETYPE_JPG; break;
293 case GfxLinkType::NativePng: pMimeType = MIMETYPE_PNG; break;
294 case GfxLinkType::NativeWmf: pMimeType = MIMETYPE_WMF; break;
295 case GfxLinkType::NativeMet: pMimeType = MIMETYPE_MET; break;
296 case GfxLinkType::NativePct: pMimeType = MIMETYPE_PCT; break;
297 case GfxLinkType::NativeWebp: pMimeType = MIMETYPE_WEBP; break;
298
299 // added Svg mimetype support
300 case GfxLinkType::NativeSvg: pMimeType = MIMETYPE_SVG; break;
301 case GfxLinkType::NativePdf: pMimeType = MIMETYPE_PDF; break;
302
303 default:
304 pMimeType = nullptr;
305 break;
306 }
307
308 if( pMimeType )
309 aMimeType = OUString::createFromAscii( pMimeType );
310 }
311
312 if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GraphicType::NONE ) )
314 }
315 else
317
318 *pValues <<= aMimeType;
319 }
320 break;
321
322 case UnoGraphicProperty::SizePixel:
323 {
324 awt::Size aAWTSize( 0, 0 );
325
326 if( mpGraphic )
327 {
328 if( mpGraphic->GetType() == GraphicType::Bitmap )
329 {
330 const Size aSizePix( mpGraphic->GetSizePixel() );
331 aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
332 }
333 }
334 else
335 aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
336
337 *pValues <<= aAWTSize;
338 }
339 break;
340
341 case UnoGraphicProperty::Size100thMM:
342 {
343 awt::Size aAWTSize( 0, 0 );
344
345 if( mpGraphic )
346 {
347 if( mpGraphic->GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel )
348 {
349 const Size aSizeLog( OutputDevice::LogicToLogic(
350 mpGraphic->GetPrefSize(),
351 mpGraphic->GetPrefMapMode(),
352 MapMode(MapUnit::Map100thMM)) );
353 aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
354 }
355 }
356 else
357 aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
358
359 *pValues <<= aAWTSize;
360 }
361 break;
362
363 case UnoGraphicProperty::BitsPerPixel:
364 {
365 sal_uInt16 nBitsPerPixel = 0;
366
367 if( mpGraphic )
368 {
369 if( mpGraphic->GetType() == GraphicType::Bitmap )
370 {
371 auto ePixelFormat = mpGraphic->GetBitmapEx().GetBitmap().getPixelFormat();
372 nBitsPerPixel = vcl::pixelFormatBitCount(ePixelFormat);
373 }
374 }
375 else
376 nBitsPerPixel = mnBitsPerPixel;
377
378 *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
379 }
380 break;
381
382 case UnoGraphicProperty::Transparent:
383 {
384 *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent;
385 }
386 break;
387
388 case UnoGraphicProperty::Alpha:
389 {
390 *pValues <<= mpGraphic && mpGraphic->IsAlpha();
391 }
392 break;
393
394 case UnoGraphicProperty::Animated:
395 {
396 *pValues <<= mpGraphic && mpGraphic->IsAnimated();
397 }
398 break;
399
400 case UnoGraphicProperty::Linked:
401 {
402 *pValues <<= mpGraphic && !mpGraphic->getOriginURL().isEmpty();
403 }
404 break;
405
406 case UnoGraphicProperty::OriginURL:
407 {
408 OUString aOriginURL;
409 if (mpGraphic)
410 aOriginURL = mpGraphic->getOriginURL();
411
412 *pValues <<= aOriginURL;
413 }
414 break;
415 }
416
417 ++ppEntries;
418 ++pValues;
419 }
420}
421
422}
423
424/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const PropertyValue * pValues
#define MIMETYPE_XBM
#define MIMETYPE_PGM
#define MIMETYPE_WMF
#define MIMETYPE_MET
#define MIMETYPE_DXF
#define MIMETYPE_JPG
#define MIMETYPE_PDF
#define MIMETYPE_PBM
constexpr OUStringLiteral MIMETYPE_VCLGRAPHIC
#define MIMETYPE_TGA
#define MIMETYPE_GIF
#define MIMETYPE_WEBP
#define MIMETYPE_EMF
#define MIMETYPE_PNG
#define MIMETYPE_PCX
#define MIMETYPE_PCT
#define MIMETYPE_BMP
#define MIMETYPE_PCD
#define MIMETYPE_PPM
#define MIMETYPE_RAS
#define MIMETYPE_XPM
#define MIMETYPE_EPS
#define MIMETYPE_SVM
#define MIMETYPE_TIF
#define MIMETYPE_PSD
#define MIMETYPE_SVG
sal_uInt16 GetBitsPerPixel() const
bool Detect(bool bExtendedInfo=false)
starts the detection
const Size & GetSize_100TH_MM() const
GraphicFileFormat GetFileFormat() const
const Size & GetSizePixel() const
SAL_WARN_UNUSED_RESULT Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
Definition: map.cxx:1593
constexpr tools::Long Height() const
constexpr tools::Long Width() const
css::uno::Type const & get()
virtual void _setPropertyValues(const comphelper::PropertyMapEntry **ppEntries, const css::uno::Any *pValues) override
static rtl::Reference<::comphelper::PropertySetInfo > createPropertySetInfo()
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
virtual void SAL_CALL release() noexcept override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
virtual ~GraphicDescriptor() noexcept override
void init(const ::Graphic &rGraphic)
virtual void _getPropertyValues(const comphelper::PropertyMapEntry **ppEntries, css::uno::Any *pValue) override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
virtual void SAL_CALL acquire() noexcept override
void implCreate(SvStream &rIStm, const OUString *pPath)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
URL aURL
ScXMLEditAttributeMap::Entry const aEntries[]
DocumentType eType
GraphicType
Definition: graph.hxx:35
NONE
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr sal_uInt16 pixelFormatBitCount(PixelFormat ePixelFormat)
Definition: BitmapTypes.hxx:34
const sal_Unicode *const aMimeType[]
unsigned char sal_uInt8
unsigned char sal_Bool
RedlineType meType
const SvXMLTokenMapEntry aTypes[]