tdf#42949 Fix IWYU warnings in vcl/source/[f-i]*
[LibreOffice.git] / vcl / source / graphic / UnoGraphicDescriptor.cxx
blob331e24167affa709a971b4226dd2ded99a0e7689
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 <unotools/ucbstreamhelper.hxx>
23 #include <vcl/graphicfilter.hxx>
24 #include <cppuhelper/supportsservice.hxx>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/awt/Size.hpp>
28 #include <com/sun/star/graphic/GraphicType.hpp>
30 #include <vcl/outdev.hxx>
31 #include <vcl/graph.hxx>
32 #include <vcl/svapp.hxx>
33 #include <memory>
35 enum class UnoGraphicProperty
37 GraphicType = 1
38 , MimeType = 2
39 , SizePixel = 3
40 , Size100thMM = 4
41 , BitsPerPixel = 5
42 , Transparent = 6
43 , Alpha = 7
44 , Animated = 8
45 , Linked = 9
46 , OriginURL = 10
50 using namespace ::com::sun::star;
52 namespace unographic {
55 GraphicDescriptor::GraphicDescriptor() :
56 ::comphelper::PropertySetHelper( createPropertySetInfo() ),
57 mpGraphic( nullptr ),
58 meType( GraphicType::NONE ),
59 mnBitsPerPixel ( 0 ),
60 mbTransparent ( false )
64 GraphicDescriptor::~GraphicDescriptor()
65 throw()
69 void GraphicDescriptor::init( const ::Graphic& rGraphic )
71 mpGraphic = &rGraphic;
74 void GraphicDescriptor::init( const OUString& rURL )
76 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rURL, StreamMode::READ ));
78 if( pIStm )
79 implCreate( *pIStm, &rURL );
82 void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL )
84 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm ));
86 if( pIStm )
87 implCreate( *pIStm, &rURL );
90 void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL )
92 OUString aURL;
93 if( pURL )
94 aURL = *pURL;
95 ::GraphicDescriptor aDescriptor( rIStm, &aURL );
97 mpGraphic = nullptr;
98 maMimeType.clear();
99 meType = GraphicType::NONE;
100 mnBitsPerPixel = 0;
101 mbTransparent = false;
103 if( !(aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GraphicFileFormat::NOT) )
104 return;
106 const char* pMimeType = nullptr;
107 sal_uInt8 cType = graphic::GraphicType::EMPTY;
109 switch( aDescriptor.GetFileFormat() )
111 case GraphicFileFormat::BMP: pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
112 case GraphicFileFormat::GIF: pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
113 case GraphicFileFormat::JPG: pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
114 case GraphicFileFormat::PCD: pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
115 case GraphicFileFormat::PCX: pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
116 case GraphicFileFormat::PNG: pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
117 case GraphicFileFormat::TIF: pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
118 case GraphicFileFormat::XBM: pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
119 case GraphicFileFormat::XPM: pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
120 case GraphicFileFormat::PBM: pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
121 case GraphicFileFormat::PGM: pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
122 case GraphicFileFormat::PPM: pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
123 case GraphicFileFormat::RAS: pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
124 case GraphicFileFormat::TGA: pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
125 case GraphicFileFormat::PSD: pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
127 case GraphicFileFormat::EPS: pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
128 case GraphicFileFormat::DXF: pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
129 case GraphicFileFormat::MET: pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
130 case GraphicFileFormat::PCT: pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
131 case GraphicFileFormat::SVM: pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
132 case GraphicFileFormat::WMF: pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
133 case GraphicFileFormat::EMF: pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
134 case GraphicFileFormat::SVG: pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
136 default:
137 break;
140 if( graphic::GraphicType::EMPTY != cType )
142 meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GraphicType::Bitmap : GraphicType::GdiMetafile );
143 maMimeType = OUString( pMimeType, strlen(pMimeType), RTL_TEXTENCODING_ASCII_US );
144 maSizePixel = aDescriptor.GetSizePixel();
145 maSize100thMM = aDescriptor.GetSize_100TH_MM();
146 mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
147 mbTransparent = ( graphic::GraphicType::VECTOR == cType );
152 uno::Any SAL_CALL GraphicDescriptor::queryAggregation( const uno::Type & rType )
154 uno::Any aAny;
156 if( rType == cppu::UnoType<lang::XServiceInfo>::get())
157 aAny <<= uno::Reference< lang::XServiceInfo >(this);
158 else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
159 aAny <<= uno::Reference< lang::XTypeProvider >(this);
160 else if( rType == cppu::UnoType<beans::XPropertySet>::get())
161 aAny <<= uno::Reference< beans::XPropertySet >(this);
162 else if( rType == cppu::UnoType<beans::XPropertyState>::get())
163 aAny <<= uno::Reference< beans::XPropertyState >(this);
164 else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get())
165 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
166 else
167 aAny = OWeakAggObject::queryAggregation( rType );
169 return aAny;
173 uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
175 return OWeakAggObject::queryInterface( rType );
179 void SAL_CALL GraphicDescriptor::acquire()
180 throw()
182 OWeakAggObject::acquire();
186 void SAL_CALL GraphicDescriptor::release()
187 throw()
189 OWeakAggObject::release();
193 OUString SAL_CALL GraphicDescriptor::getImplementationName()
195 return OUString( "com.sun.star.comp.graphic.GraphicDescriptor" );
198 sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
200 return cppu::supportsService(this, ServiceName);
204 uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
206 return { "com.sun.star.graphic.GraphicDescriptor" };
210 uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
212 static const uno::Sequence< uno::Type > aTypes {
213 cppu::UnoType<uno::XAggregation>::get(),
214 cppu::UnoType<lang::XServiceInfo>::get(),
215 cppu::UnoType<lang::XTypeProvider>::get(),
216 cppu::UnoType<beans::XPropertySet>::get(),
217 cppu::UnoType<beans::XPropertyState>::get(),
218 cppu::UnoType<beans::XMultiPropertySet>::get() };
219 return aTypes;
222 uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
224 return css::uno::Sequence<sal_Int8>();
228 rtl::Reference<::comphelper::PropertySetInfo> GraphicDescriptor::createPropertySetInfo()
230 static ::comphelper::PropertyMapEntry const aEntries[] =
232 { OUString( "GraphicType" ), static_cast< sal_Int32 >( UnoGraphicProperty::GraphicType ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
233 { OUString( "MimeType" ), static_cast< sal_Int32 >( UnoGraphicProperty::MimeType ), cppu::UnoType< OUString >::get(), beans::PropertyAttribute::READONLY, 0 },
234 { OUString( "SizePixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::SizePixel ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
235 { OUString( "Size100thMM" ), static_cast< sal_Int32 >( UnoGraphicProperty::Size100thMM ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
236 { OUString( "BitsPerPixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::BitsPerPixel ), cppu::UnoType< sal_uInt8 >::get(), beans::PropertyAttribute::READONLY, 0 },
237 { OUString( "Transparent" ), static_cast< sal_Int32 >( UnoGraphicProperty::Transparent ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
238 { OUString( "Alpha" ), static_cast< sal_Int32 >( UnoGraphicProperty::Alpha ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
239 { OUString( "Animated" ), static_cast< sal_Int32 >( UnoGraphicProperty::Animated ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
240 { OUString("Linked"), sal_Int32(UnoGraphicProperty::Linked), cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
241 { OUString("OriginURL"), sal_Int32(UnoGraphicProperty::OriginURL), cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
243 { OUString(), 0, css::uno::Type(), 0, 0 }
246 return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) );
250 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
252 // we only have readonly attributes
256 void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
258 SolarMutexGuard aGuard;
260 while( *ppEntries )
262 UnoGraphicProperty theProperty = static_cast< UnoGraphicProperty >( (*ppEntries)->mnHandle );
263 switch( theProperty )
265 case UnoGraphicProperty::GraphicType:
267 const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
269 *pValues <<= ( eType == GraphicType::Bitmap ? graphic::GraphicType::PIXEL :
270 ( eType == GraphicType::GdiMetafile ? graphic::GraphicType::VECTOR :
271 graphic::GraphicType::EMPTY ) );
273 break;
275 case UnoGraphicProperty::MimeType:
277 OUString aMimeType;
279 if( mpGraphic )
281 if( mpGraphic->IsGfxLink() )
283 const char* pMimeType;
285 switch( mpGraphic->GetGfxLink().GetType() )
287 case GfxLinkType::NativeGif: pMimeType = MIMETYPE_GIF; break;
289 // #i15508# added BMP type for better exports (checked, works)
290 case GfxLinkType::NativeBmp: pMimeType = MIMETYPE_BMP; break;
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;
298 // added Svg mimetype support
299 case GfxLinkType::NativeSvg: pMimeType = MIMETYPE_SVG; break;
300 case GfxLinkType::NativePdf: pMimeType = MIMETYPE_PDF; break;
302 default:
303 pMimeType = nullptr;
304 break;
307 if( pMimeType )
308 aMimeType = OUString::createFromAscii( pMimeType );
311 if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GraphicType::NONE ) )
312 aMimeType = MIMETYPE_VCLGRAPHIC;
314 else
315 aMimeType = maMimeType;
317 *pValues <<= aMimeType;
319 break;
321 case UnoGraphicProperty::SizePixel:
323 awt::Size aAWTSize( 0, 0 );
325 if( mpGraphic )
327 if( mpGraphic->GetType() == GraphicType::Bitmap )
329 const Size aSizePix( mpGraphic->GetSizePixel() );
330 aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
333 else
334 aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
336 *pValues <<= aAWTSize;
338 break;
340 case UnoGraphicProperty::Size100thMM:
342 awt::Size aAWTSize( 0, 0 );
344 if( mpGraphic )
346 if( mpGraphic->GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel )
348 const Size aSizeLog( OutputDevice::LogicToLogic(
349 mpGraphic->GetPrefSize(),
350 mpGraphic->GetPrefMapMode(),
351 MapMode(MapUnit::Map100thMM)) );
352 aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
355 else
356 aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
358 *pValues <<= aAWTSize;
360 break;
362 case UnoGraphicProperty::BitsPerPixel:
364 sal_uInt16 nBitsPerPixel = 0;
366 if( mpGraphic )
368 if( mpGraphic->GetType() == GraphicType::Bitmap )
369 nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount();
371 else
372 nBitsPerPixel = mnBitsPerPixel;
374 *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
376 break;
378 case UnoGraphicProperty::Transparent:
380 *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent;
382 break;
384 case UnoGraphicProperty::Alpha:
386 *pValues <<= mpGraphic && mpGraphic->IsAlpha();
388 break;
390 case UnoGraphicProperty::Animated:
392 *pValues <<= mpGraphic && mpGraphic->IsAnimated();
394 break;
396 case UnoGraphicProperty::Linked:
398 *pValues <<= mpGraphic && !mpGraphic->getOriginURL().isEmpty();
400 break;
402 case UnoGraphicProperty::OriginURL:
404 OUString aOriginURL;
405 if (mpGraphic)
406 aOriginURL = mpGraphic->getOriginURL();
408 *pValues <<= aOriginURL;
410 break;
413 ++ppEntries;
414 ++pValues;
420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */