Use COMReference to handle COM pointers in CreateShortcut
[LibreOffice.git] / vcl / source / graphic / UnoGraphicDescriptor.cxx
blobf36d04988be8f85aba5d605d15a060fbb0f49157
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <graphic/UnoGraphicDescriptor.hxx>
22 #include <cppuhelper/weakagg.hxx>
23 #include <unotools/ucbstreamhelper.hxx>
24 #include <vcl/graphicfilter.hxx>
25 #include <cppuhelper/supportsservice.hxx>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/awt/Size.hpp>
29 #include <com/sun/star/graphic/GraphicType.hpp>
30 #include <com/sun/star/io/XInputStream.hpp>
32 #include <vcl/outdev.hxx>
33 #include <vcl/graph.hxx>
34 #include <vcl/svapp.hxx>
35 #include <memory>
37 namespace {
39 enum class UnoGraphicProperty
41 GraphicType = 1
42 , MimeType = 2
43 , SizePixel = 3
44 , Size100thMM = 4
45 , BitsPerPixel = 5
46 , Transparent = 6
47 , Alpha = 7
48 , Animated = 8
49 , Linked = 9
50 , OriginURL = 10
55 using namespace ::com::sun::star;
57 namespace unographic {
60 GraphicDescriptor::GraphicDescriptor() :
61 ::comphelper::PropertySetHelper( createPropertySetInfo() ),
62 mpGraphic( nullptr ),
63 meType( GraphicType::NONE ),
64 mnBitsPerPixel ( 0 ),
65 mbTransparent ( false )
69 GraphicDescriptor::~GraphicDescriptor()
70 noexcept
74 void GraphicDescriptor::init( const ::Graphic& rGraphic )
76 mpGraphic = &rGraphic;
79 void GraphicDescriptor::init( const OUString& rURL )
81 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rURL, StreamMode::READ ));
83 if( pIStm )
84 implCreate( *pIStm, &rURL );
87 void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL )
89 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm ));
91 if( pIStm )
92 implCreate( *pIStm, &rURL );
95 void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL )
97 OUString aURL;
98 if( pURL )
99 aURL = *pURL;
100 ::GraphicDescriptor aDescriptor( rIStm, &aURL );
102 mpGraphic = nullptr;
103 maMimeType.clear();
104 meType = GraphicType::NONE;
105 mnBitsPerPixel = 0;
106 mbTransparent = false;
108 if( !(aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GraphicFileFormat::NOT) )
109 return;
111 OUString aMimeType;
112 sal_uInt8 cType = graphic::GraphicType::EMPTY;
114 switch( aDescriptor.GetFileFormat() )
116 case GraphicFileFormat::BMP: aMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
117 case GraphicFileFormat::GIF: aMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
118 case GraphicFileFormat::JPG: aMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
119 case GraphicFileFormat::PCD: aMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
120 case GraphicFileFormat::PCX: aMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
121 case GraphicFileFormat::PNG: aMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
122 case GraphicFileFormat::TIF: aMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
123 case GraphicFileFormat::XBM: aMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
124 case GraphicFileFormat::XPM: aMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
125 case GraphicFileFormat::PBM: aMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
126 case GraphicFileFormat::PGM: aMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
127 case GraphicFileFormat::PPM: aMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
128 case GraphicFileFormat::RAS: aMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
129 case GraphicFileFormat::TGA: aMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
130 case GraphicFileFormat::PSD: aMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
131 case GraphicFileFormat::WEBP: aMimeType = MIMETYPE_WEBP; cType = graphic::GraphicType::PIXEL; break;
133 case GraphicFileFormat::EPS: aMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
134 case GraphicFileFormat::DXF: aMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
135 case GraphicFileFormat::MET: aMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
136 case GraphicFileFormat::PCT: aMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
137 case GraphicFileFormat::SVM: aMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
138 case GraphicFileFormat::WMF: aMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
139 case GraphicFileFormat::WMZ: aMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
140 case GraphicFileFormat::EMF: aMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
141 case GraphicFileFormat::EMZ: aMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
142 case GraphicFileFormat::SVG: aMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
143 case GraphicFileFormat::SVGZ: aMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
145 default:
146 break;
149 if( graphic::GraphicType::EMPTY != cType )
151 meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GraphicType::Bitmap : GraphicType::GdiMetafile );
152 maMimeType = aMimeType;
153 maSizePixel = aDescriptor.GetSizePixel();
154 maSize100thMM = aDescriptor.GetSize_100TH_MM();
155 mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
156 mbTransparent = ( graphic::GraphicType::VECTOR == cType );
161 uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
163 uno::Any aAny;
165 if( rType == cppu::UnoType<lang::XServiceInfo>::get())
166 aAny <<= uno::Reference< lang::XServiceInfo >(this);
167 else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
168 aAny <<= uno::Reference< lang::XTypeProvider >(this);
169 else if( rType == cppu::UnoType<beans::XPropertySet>::get())
170 aAny <<= uno::Reference< beans::XPropertySet >(this);
171 else if( rType == cppu::UnoType<beans::XPropertyState>::get())
172 aAny <<= uno::Reference< beans::XPropertyState >(this);
173 else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get())
174 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
175 else
176 aAny = OWeakObject::queryInterface( rType );
178 return aAny;
182 void SAL_CALL GraphicDescriptor::acquire()
183 noexcept
185 OWeakObject::acquire();
189 void SAL_CALL GraphicDescriptor::release()
190 noexcept
192 OWeakObject::release();
196 OUString SAL_CALL GraphicDescriptor::getImplementationName()
198 return u"com.sun.star.comp.graphic.GraphicDescriptor"_ustr;
201 sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
203 return cppu::supportsService(this, ServiceName);
207 uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
209 return { u"com.sun.star.graphic.GraphicDescriptor"_ustr };
213 uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
215 static const uno::Sequence< uno::Type > aTypes {
216 cppu::UnoType<uno::XAggregation>::get(),
217 cppu::UnoType<lang::XServiceInfo>::get(),
218 cppu::UnoType<lang::XTypeProvider>::get(),
219 cppu::UnoType<beans::XPropertySet>::get(),
220 cppu::UnoType<beans::XPropertyState>::get(),
221 cppu::UnoType<beans::XMultiPropertySet>::get() };
222 return aTypes;
225 uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
227 return css::uno::Sequence<sal_Int8>();
231 rtl::Reference<::comphelper::PropertySetInfo> GraphicDescriptor::createPropertySetInfo()
233 static ::comphelper::PropertyMapEntry const aEntries[] =
235 { u"GraphicType"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::GraphicType ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
236 { u"MimeType"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::MimeType ), cppu::UnoType< OUString >::get(), beans::PropertyAttribute::READONLY, 0 },
237 { u"SizePixel"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::SizePixel ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
238 { u"Size100thMM"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Size100thMM ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
239 { u"BitsPerPixel"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::BitsPerPixel ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
240 { u"Transparent"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Transparent ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
241 { u"Alpha"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Alpha ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
242 { u"Animated"_ustr, static_cast< sal_Int32 >( UnoGraphicProperty::Animated ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
243 { u"Linked"_ustr, sal_Int32(UnoGraphicProperty::Linked), cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
244 { u"OriginURL"_ustr, sal_Int32(UnoGraphicProperty::OriginURL), cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
247 return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) );
251 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
253 // we only have readonly attributes
257 void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
259 SolarMutexGuard aGuard;
261 while( *ppEntries )
263 UnoGraphicProperty theProperty = static_cast< UnoGraphicProperty >( (*ppEntries)->mnHandle );
264 switch( theProperty )
266 case UnoGraphicProperty::GraphicType:
268 const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
270 *pValues <<= ( eType == GraphicType::Bitmap ? graphic::GraphicType::PIXEL :
271 ( eType == GraphicType::GdiMetafile ? graphic::GraphicType::VECTOR :
272 graphic::GraphicType::EMPTY ) );
274 break;
276 case UnoGraphicProperty::MimeType:
278 OUString aMimeType;
280 if( mpGraphic )
282 if( mpGraphic->IsGfxLink() )
284 GfxLink aLink = mpGraphic->GetGfxLink();
285 switch (aLink.GetType())
287 case GfxLinkType::NativeGif: aMimeType = MIMETYPE_GIF; break;
289 // #i15508# added BMP type for better exports (checked, works)
290 case GfxLinkType::NativeBmp: aMimeType = MIMETYPE_BMP; break;
292 case GfxLinkType::NativeJpg: aMimeType = MIMETYPE_JPG; break;
293 case GfxLinkType::NativePng: aMimeType = MIMETYPE_PNG; break;
294 case GfxLinkType::NativeTif: aMimeType = MIMETYPE_TIF; break;
295 case GfxLinkType::NativeWmf: aMimeType = aLink.IsEMF() ? MIMETYPE_EMF : MIMETYPE_WMF; break;
296 case GfxLinkType::NativeMet: aMimeType = MIMETYPE_MET; break;
297 case GfxLinkType::NativePct: aMimeType = MIMETYPE_PCT; break;
298 case GfxLinkType::NativeWebp: aMimeType = MIMETYPE_WEBP; break;
300 // added Svg mimetype support
301 case GfxLinkType::NativeSvg: aMimeType = MIMETYPE_SVG; break;
302 case GfxLinkType::NativePdf: aMimeType = MIMETYPE_PDF; break;
304 default:
305 break;
309 if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GraphicType::NONE ) )
310 aMimeType = MIMETYPE_VCLGRAPHIC;
312 else
313 aMimeType = maMimeType;
315 *pValues <<= aMimeType;
317 break;
319 case UnoGraphicProperty::SizePixel:
321 awt::Size aAWTSize( 0, 0 );
323 if( mpGraphic )
325 if( mpGraphic->GetType() == GraphicType::Bitmap )
327 const Size aSizePix( mpGraphic->GetSizePixel() );
328 aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
331 else
332 aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
334 *pValues <<= aAWTSize;
336 break;
338 case UnoGraphicProperty::Size100thMM:
340 awt::Size aAWTSize( 0, 0 );
342 if( mpGraphic )
344 if( mpGraphic->GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel )
346 const Size aSizeLog( OutputDevice::LogicToLogic(
347 mpGraphic->GetPrefSize(),
348 mpGraphic->GetPrefMapMode(),
349 MapMode(MapUnit::Map100thMM)) );
350 aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
353 else
354 aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
356 *pValues <<= aAWTSize;
358 break;
360 case UnoGraphicProperty::BitsPerPixel:
362 sal_uInt16 nBitsPerPixel = 0;
364 if( mpGraphic )
366 if( mpGraphic->GetType() == GraphicType::Bitmap )
368 auto ePixelFormat = mpGraphic->GetBitmapEx().GetBitmap().getPixelFormat();
369 nBitsPerPixel = vcl::pixelFormatBitCount(ePixelFormat);
372 else
373 nBitsPerPixel = mnBitsPerPixel;
375 *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
377 break;
379 case UnoGraphicProperty::Transparent:
381 *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent;
383 break;
385 case UnoGraphicProperty::Alpha:
387 *pValues <<= mpGraphic && mpGraphic->IsAlpha();
389 break;
391 case UnoGraphicProperty::Animated:
393 *pValues <<= mpGraphic && mpGraphic->IsAnimated();
395 break;
397 case UnoGraphicProperty::Linked:
399 *pValues <<= mpGraphic && !mpGraphic->getOriginURL().isEmpty();
401 break;
403 case UnoGraphicProperty::OriginURL:
405 OUString aOriginURL;
406 if (mpGraphic)
407 aOriginURL = mpGraphic->getOriginURL();
409 *pValues <<= aOriginURL;
411 break;
414 ++ppEntries;
415 ++pValues;
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */