Bump version to 5.0-14
[LibreOffice.git] / svtools / source / graphic / descriptor.cxx
blobf0020618878cee56868d4e33a746e6528fde3189
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 "descriptor.hxx"
22 #include <osl/mutex.hxx>
23 #include <unotools/ucbstreamhelper.hxx>
24 #include <vcl/graphicfilter.hxx>
25 #include <svl/itemprop.hxx>
26 #include <comphelper/servicehelper.hxx>
27 #include <cppuhelper/supportsservice.hxx>
29 #include <com/sun/star/beans/PropertyState.hpp>
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <com/sun/star/awt/Size.hpp>
32 #include <com/sun/star/graphic/GraphicType.hpp>
34 #include <vcl/graph.hxx>
35 #include <vcl/svapp.hxx>
36 #include <boost/scoped_ptr.hpp>
38 #define UNOGRAPHIC_GRAPHICTYPE 1
39 #define UNOGRAPHIC_MIMETYPE 2
40 #define UNOGRAPHIC_SIZEPIXEL 3
41 #define UNOGRAPHIC_SIZE100THMM 4
42 #define UNOGRAPHIC_BITSPERPIXEL 5
43 #define UNOGRAPHIC_TRANSPARENT 6
44 #define UNOGRAPHIC_ALPHA 7
45 #define UNOGRAPHIC_ANIMATED 8
47 using namespace ::com::sun::star;
49 namespace unographic {
52 // - GraphicDescriptor -
55 GraphicDescriptor::GraphicDescriptor() :
56 ::comphelper::PropertySetHelper( createPropertySetInfo(), SAL_NO_ACQUIRE ),
57 mpGraphic( NULL ),
58 meType( GRAPHIC_NONE ),
59 mnBitsPerPixel ( 0 ),
60 mbTransparent ( false ),
61 mbAlpha( false ),
62 mbAnimated( false )
66 GraphicDescriptor::~GraphicDescriptor()
67 throw()
71 void GraphicDescriptor::init( const ::Graphic& rGraphic )
73 mpGraphic = &rGraphic;
76 void GraphicDescriptor::init( const OUString& rURL )
78 boost::scoped_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rURL, StreamMode::READ ));
80 if( pIStm )
81 implCreate( *pIStm, &rURL );
84 void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL )
86 boost::scoped_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm ));
88 if( pIStm )
89 implCreate( *pIStm, &rURL );
92 void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL )
94 OUString aURL;
95 if( pURL )
96 aURL = *pURL;
97 ::GraphicDescriptor aDescriptor( rIStm, &aURL );
99 mpGraphic = NULL;
100 maMimeType.clear();
101 meType = GRAPHIC_NONE;
102 mnBitsPerPixel = 0;
103 mbTransparent = false;
105 if( aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GraphicFileFormat::NOT )
107 const char* pMimeType = NULL;
108 sal_uInt8 cType = graphic::GraphicType::EMPTY;
110 switch( aDescriptor.GetFileFormat() )
112 case( GraphicFileFormat::BMP ): pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
113 case( GraphicFileFormat::GIF ): pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
114 case( GraphicFileFormat::JPG ): pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
115 case( GraphicFileFormat::PCD ): pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
116 case( GraphicFileFormat::PCX ): pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
117 case( GraphicFileFormat::PNG ): pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
118 case( GraphicFileFormat::TIF ): pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
119 case( GraphicFileFormat::XBM ): pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
120 case( GraphicFileFormat::XPM ): pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
121 case( GraphicFileFormat::PBM ): pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
122 case( GraphicFileFormat::PGM ): pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
123 case( GraphicFileFormat::PPM ): pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
124 case( GraphicFileFormat::RAS ): pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
125 case( GraphicFileFormat::TGA ): pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
126 case( GraphicFileFormat::PSD ): pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
128 case( GraphicFileFormat::EPS ): pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
129 case( GraphicFileFormat::DXF ): pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
130 case( GraphicFileFormat::MET ): pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
131 case( GraphicFileFormat::PCT ): pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
132 case( GraphicFileFormat::SGF ): pMimeType = MIMETYPE_SGF; cType = graphic::GraphicType::VECTOR; break;
133 case( GraphicFileFormat::SVM ): pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
134 case( GraphicFileFormat::WMF ): pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
135 case( GraphicFileFormat::SGV ): pMimeType = MIMETYPE_SGV; cType = graphic::GraphicType::VECTOR; break;
136 case( GraphicFileFormat::EMF ): pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
137 case( GraphicFileFormat::SVG ): pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
139 default:
140 break;
143 if( graphic::GraphicType::EMPTY != cType )
145 meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GRAPHIC_BITMAP : GRAPHIC_GDIMETAFILE );
146 maMimeType = OUString( pMimeType, strlen(pMimeType), RTL_TEXTENCODING_ASCII_US );
147 maSizePixel = aDescriptor.GetSizePixel();
148 maSize100thMM = aDescriptor.GetSize_100TH_MM();
149 mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
150 mbTransparent = ( graphic::GraphicType::VECTOR == cType );
151 mbAlpha = mbAnimated = false;
158 OUString GraphicDescriptor::getImplementationName_Static()
159 throw()
161 return OUString( "com.sun.star.comp.graphic.GraphicDescriptor" );
166 uno::Sequence< OUString > GraphicDescriptor::getSupportedServiceNames_Static()
167 throw( )
169 uno::Sequence< OUString > aSeq( 1 );
171 aSeq.getArray()[ 0 ] = "com.sun.star.graphic.GraphicDescriptor";
173 return aSeq;
178 uno::Any SAL_CALL GraphicDescriptor::queryAggregation( const uno::Type & rType )
179 throw( uno::RuntimeException, std::exception )
181 uno::Any aAny;
183 if( rType == cppu::UnoType<lang::XServiceInfo>::get())
184 aAny <<= uno::Reference< lang::XServiceInfo >(this);
185 else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
186 aAny <<= uno::Reference< lang::XTypeProvider >(this);
187 else if( rType == cppu::UnoType<beans::XPropertySet>::get())
188 aAny <<= uno::Reference< beans::XPropertySet >(this);
189 else if( rType == cppu::UnoType<beans::XPropertyState>::get())
190 aAny <<= uno::Reference< beans::XPropertyState >(this);
191 else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get())
192 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
193 else
194 aAny <<= OWeakAggObject::queryAggregation( rType );
196 return aAny;
201 uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
202 throw( uno::RuntimeException, std::exception )
204 return OWeakAggObject::queryInterface( rType );
209 void SAL_CALL GraphicDescriptor::acquire()
210 throw()
212 OWeakAggObject::acquire();
217 void SAL_CALL GraphicDescriptor::release()
218 throw()
220 OWeakAggObject::release();
225 OUString SAL_CALL GraphicDescriptor::getImplementationName()
226 throw( uno::RuntimeException, std::exception )
228 return getImplementationName_Static();
231 sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
232 throw( uno::RuntimeException, std::exception )
234 return cppu::supportsService(this, ServiceName);
239 uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
240 throw( uno::RuntimeException, std::exception )
242 return getSupportedServiceNames_Static();
247 uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
248 throw( uno::RuntimeException, std::exception )
250 uno::Sequence< uno::Type > aTypes( 6 );
251 uno::Type* pTypes = aTypes.getArray();
253 *pTypes++ = cppu::UnoType<uno::XAggregation>::get();
254 *pTypes++ = cppu::UnoType<lang::XServiceInfo>::get();
255 *pTypes++ = cppu::UnoType<lang::XTypeProvider>::get();
256 *pTypes++ = cppu::UnoType<beans::XPropertySet>::get();
257 *pTypes++ = cppu::UnoType<beans::XPropertyState>::get();
258 *pTypes++ = cppu::UnoType<beans::XMultiPropertySet>::get();
260 return aTypes;
263 uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
264 throw( uno::RuntimeException, std::exception )
266 return css::uno::Sequence<sal_Int8>();
271 ::comphelper::PropertySetInfo* GraphicDescriptor::createPropertySetInfo()
273 SolarMutexGuard aGuard;
274 ::comphelper::PropertySetInfo* pRet = new ::comphelper::PropertySetInfo();
276 static ::comphelper::PropertyMapEntry const aEntries[] =
278 { OUString("GraphicType"), UNOGRAPHIC_GRAPHICTYPE, cppu::UnoType<sal_Int8>::get(), beans::PropertyAttribute::READONLY, 0 },
279 { OUString("MimeType"), UNOGRAPHIC_MIMETYPE, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
280 { OUString("SizePixel"), UNOGRAPHIC_SIZEPIXEL, cppu::UnoType<awt::Size>::get(), beans::PropertyAttribute::READONLY, 0 },
281 { OUString("Size100thMM"), UNOGRAPHIC_SIZE100THMM, cppu::UnoType<awt::Size>::get(), beans::PropertyAttribute::READONLY, 0 },
282 { OUString("BitsPerPixel"), UNOGRAPHIC_BITSPERPIXEL, cppu::UnoType<sal_uInt8>::get(), beans::PropertyAttribute::READONLY, 0 },
283 { OUString("Transparent"), UNOGRAPHIC_TRANSPARENT, cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
284 { OUString("Alpha"), UNOGRAPHIC_ALPHA, cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
285 { OUString("Animated"), UNOGRAPHIC_ANIMATED, cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
286 { OUString(), 0, css::uno::Type(), 0, 0 }
289 pRet->acquire();
290 pRet->add( aEntries );
292 return pRet;
297 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
298 throw( beans::UnknownPropertyException,
299 beans::PropertyVetoException,
300 lang::IllegalArgumentException,
301 lang::WrappedTargetException )
303 // we only have readonly attributes
308 void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
309 throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException, std::exception )
311 SolarMutexGuard aGuard;
313 while( *ppEntries )
315 switch( (*ppEntries)->mnHandle )
317 case( UNOGRAPHIC_GRAPHICTYPE ):
319 const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
321 *pValues <<= ( ( eType == GRAPHIC_BITMAP ? graphic::GraphicType::PIXEL :
322 ( eType == GRAPHIC_GDIMETAFILE ? graphic::GraphicType::VECTOR :
323 graphic::GraphicType::EMPTY ) ) );
325 break;
327 case( UNOGRAPHIC_MIMETYPE ):
329 OUString aMimeType;
331 if( mpGraphic )
333 if( mpGraphic->IsLink() )
335 const char* pMimeType;
337 switch( mpGraphic->GetLink().GetType() )
339 case( GFX_LINK_TYPE_NATIVE_GIF ): pMimeType = MIMETYPE_GIF; break;
341 // #i15508# added BMP type for better exports (checked, works)
342 case( GFX_LINK_TYPE_NATIVE_BMP ): pMimeType = MIMETYPE_BMP; break;
344 case( GFX_LINK_TYPE_NATIVE_JPG ): pMimeType = MIMETYPE_JPG; break;
345 case( GFX_LINK_TYPE_NATIVE_PNG ): pMimeType = MIMETYPE_PNG; break;
346 case( GFX_LINK_TYPE_NATIVE_WMF ): pMimeType = MIMETYPE_WMF; break;
347 case( GFX_LINK_TYPE_NATIVE_MET ): pMimeType = MIMETYPE_MET; break;
348 case( GFX_LINK_TYPE_NATIVE_PCT ): pMimeType = MIMETYPE_PCT; break;
350 // added Svg mimetype support
351 case( GFX_LINK_TYPE_NATIVE_SVG ): pMimeType = MIMETYPE_SVG; break;
353 default:
354 pMimeType = NULL;
355 break;
358 if( pMimeType )
359 aMimeType = OUString::createFromAscii( pMimeType );
362 if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GRAPHIC_NONE ) )
363 aMimeType = MIMETYPE_VCLGRAPHIC;
365 else
366 aMimeType = maMimeType;
368 *pValues <<= aMimeType;
370 break;
372 case( UNOGRAPHIC_SIZEPIXEL ):
374 awt::Size aAWTSize( 0, 0 );
376 if( mpGraphic )
378 if( mpGraphic->GetType() == GRAPHIC_BITMAP )
380 const Size aSizePix( mpGraphic->GetBitmapEx().GetSizePixel() );
381 aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
384 else
385 aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
387 *pValues <<= aAWTSize;
389 break;
391 case( UNOGRAPHIC_SIZE100THMM ):
393 awt::Size aAWTSize( 0, 0 );
395 if( mpGraphic )
397 if( mpGraphic->GetPrefMapMode().GetMapUnit() != MAP_PIXEL )
399 const Size aSizeLog( OutputDevice::LogicToLogic( mpGraphic->GetPrefSize(), mpGraphic->GetPrefMapMode(), MAP_100TH_MM ) );
400 aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
403 else
404 aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
406 *pValues <<= aAWTSize;
408 break;
410 case( UNOGRAPHIC_BITSPERPIXEL ):
412 sal_uInt16 nBitsPerPixel = 0;
414 if( mpGraphic )
416 if( mpGraphic->GetType() == GRAPHIC_BITMAP )
417 nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount();
419 else
420 nBitsPerPixel = mnBitsPerPixel;
422 *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
424 break;
426 case( UNOGRAPHIC_TRANSPARENT ):
428 *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent;
430 break;
432 case( UNOGRAPHIC_ALPHA ):
434 *pValues <<= mpGraphic ? mpGraphic->IsAlpha() : mbAlpha;
436 break;
438 case( UNOGRAPHIC_ANIMATED ):
440 *pValues <<= mpGraphic ? mpGraphic->IsAnimated() : mbAnimated;
442 break;
445 ++ppEntries;
446 ++pValues;
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */