update credits
[LibreOffice.git] / svtools / source / graphic / descriptor.cxx
blob9c8ca91dc7694ce9d20f6226562c5daffdc9434f
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>
28 #include <com/sun/star/beans/PropertyState.hpp>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/awt/Size.hpp>
31 #include <com/sun/star/graphic/GraphicType.hpp>
33 #include "vcl/graph.hxx"
34 #include "vcl/svapp.hxx"
36 #define UNOGRAPHIC_GRAPHICTYPE 1
37 #define UNOGRAPHIC_MIMETYPE 2
38 #define UNOGRAPHIC_SIZEPIXEL 3
39 #define UNOGRAPHIC_SIZE100THMM 4
40 #define UNOGRAPHIC_BITSPERPIXEL 5
41 #define UNOGRAPHIC_TRANSPARENT 6
42 #define UNOGRAPHIC_ALPHA 7
43 #define UNOGRAPHIC_ANIMATED 8
45 using namespace ::com::sun::star;
47 namespace unographic {
49 // ---------------------
50 // - GraphicDescriptor -
51 // ---------------------
53 GraphicDescriptor::GraphicDescriptor() :
54 ::comphelper::PropertySetHelper( createPropertySetInfo(), SAL_NO_ACQUIRE ),
55 mpGraphic( NULL ),
56 meType( GRAPHIC_NONE ),
57 mnBitsPerPixel ( 0 ),
58 mbTransparent ( false ),
59 mbAlpha( false ),
60 mbAnimated( false )
64 // ------------------------------------------------------------------------------
66 GraphicDescriptor::~GraphicDescriptor()
67 throw()
71 // ------------------------------------------------------------------------------
73 void GraphicDescriptor::init( const ::Graphic& rGraphic )
74 throw()
76 mpGraphic = &rGraphic;
79 // ------------------------------------------------------------------------------
81 void GraphicDescriptor::init( const OUString& rURL )
82 throw()
84 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rURL, STREAM_READ );
86 if( pIStm )
88 implCreate( *pIStm, &rURL );
89 delete pIStm;
93 // ------------------------------------------------------------------------------
95 void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL )
96 throw()
98 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rxIStm );
100 if( pIStm )
102 implCreate( *pIStm, &rURL );
103 delete pIStm;
107 // ------------------------------------------------------------------------------
109 void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL )
111 String aURL;
112 if( pURL )
113 aURL = *pURL;
114 ::GraphicDescriptor aDescriptor( rIStm, &aURL );
116 mpGraphic = NULL;
117 maMimeType = OUString();
118 meType = GRAPHIC_NONE;
119 mnBitsPerPixel = 0;
120 mbTransparent = false;
122 if( aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GFF_NOT )
124 const char* pMimeType = NULL;
125 sal_uInt8 cType = graphic::GraphicType::EMPTY;
127 switch( aDescriptor.GetFileFormat() )
129 case( GFF_BMP ): pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
130 case( GFF_GIF ): pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
131 case( GFF_JPG ): pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
132 case( GFF_PCD ): pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
133 case( GFF_PCX ): pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
134 case( GFF_PNG ): pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
135 case( GFF_TIF ): pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
136 case( GFF_XBM ): pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
137 case( GFF_XPM ): pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
138 case( GFF_PBM ): pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
139 case( GFF_PGM ): pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
140 case( GFF_PPM ): pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
141 case( GFF_RAS ): pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
142 case( GFF_TGA ): pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
143 case( GFF_PSD ): pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
145 case( GFF_EPS ): pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
146 case( GFF_DXF ): pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
147 case( GFF_MET ): pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
148 case( GFF_PCT ): pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
149 case( GFF_SGF ): pMimeType = MIMETYPE_SGF; cType = graphic::GraphicType::VECTOR; break;
150 case( GFF_SVM ): pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
151 case( GFF_WMF ): pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
152 case( GFF_SGV ): pMimeType = MIMETYPE_SGV; cType = graphic::GraphicType::VECTOR; break;
153 case( GFF_EMF ): pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
154 case( GFF_SVG ): pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
156 default:
157 break;
160 if( graphic::GraphicType::EMPTY != cType )
162 meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GRAPHIC_BITMAP : GRAPHIC_GDIMETAFILE );
163 maMimeType = String( pMimeType, RTL_TEXTENCODING_ASCII_US );
164 maSizePixel = aDescriptor.GetSizePixel();
165 maSize100thMM = aDescriptor.GetSize_100TH_MM();
166 mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
167 mbTransparent = ( graphic::GraphicType::VECTOR == cType );
168 mbAlpha = mbAnimated = false;
173 // ------------------------------------------------------------------------------
175 OUString GraphicDescriptor::getImplementationName_Static()
176 throw()
178 return OUString( "com.sun.star.comp.graphic.GraphicDescriptor" );
181 // ------------------------------------------------------------------------------
183 uno::Sequence< OUString > GraphicDescriptor::getSupportedServiceNames_Static()
184 throw( )
186 uno::Sequence< OUString > aSeq( 1 );
188 aSeq.getArray()[ 0 ] = OUString( "com.sun.star.graphic.GraphicDescriptor" );
190 return aSeq;
193 // ------------------------------------------------------------------------------
195 uno::Any SAL_CALL GraphicDescriptor::queryAggregation( const uno::Type & rType )
196 throw( uno::RuntimeException )
198 uno::Any aAny;
200 if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
201 aAny <<= uno::Reference< lang::XServiceInfo >(this);
202 else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
203 aAny <<= uno::Reference< lang::XTypeProvider >(this);
204 else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
205 aAny <<= uno::Reference< beans::XPropertySet >(this);
206 else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
207 aAny <<= uno::Reference< beans::XPropertyState >(this);
208 else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
209 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
210 else
211 aAny <<= OWeakAggObject::queryAggregation( rType );
213 return aAny;
216 // ------------------------------------------------------------------------------
218 uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
219 throw( uno::RuntimeException )
221 return OWeakAggObject::queryInterface( rType );
224 // ------------------------------------------------------------------------------
226 void SAL_CALL GraphicDescriptor::acquire()
227 throw()
229 OWeakAggObject::acquire();
232 // ------------------------------------------------------------------------------
234 void SAL_CALL GraphicDescriptor::release()
235 throw()
237 OWeakAggObject::release();
240 // ------------------------------------------------------------------------------
242 OUString SAL_CALL GraphicDescriptor::getImplementationName()
243 throw( uno::RuntimeException )
245 return getImplementationName_Static();
248 // ------------------------------------------------------------------------------
250 sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
251 throw( uno::RuntimeException )
253 uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
254 const OUString* pArray = aSNL.getConstArray();
256 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
257 if( pArray[i] == ServiceName )
258 return true;
260 return false;
263 // ------------------------------------------------------------------------------
265 uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
266 throw( uno::RuntimeException )
268 return getSupportedServiceNames_Static();
271 // ------------------------------------------------------------------------------
273 uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
274 throw( uno::RuntimeException )
276 uno::Sequence< uno::Type > aTypes( 6 );
277 uno::Type* pTypes = aTypes.getArray();
279 *pTypes++ = ::getCppuType((const uno::Reference< uno::XAggregation>*)0);
280 *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
281 *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
282 *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
283 *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
284 *pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
286 return aTypes;
289 namespace
291 class theGraphicDescriptorUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theGraphicDescriptorUnoTunnelId > {};
294 uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
295 throw( uno::RuntimeException )
297 return theGraphicDescriptorUnoTunnelId::get().getSeq();
300 // ------------------------------------------------------------------------------
302 ::comphelper::PropertySetInfo* GraphicDescriptor::createPropertySetInfo()
304 SolarMutexGuard aGuard;
305 ::comphelper::PropertySetInfo* pRet = new ::comphelper::PropertySetInfo();
307 static ::comphelper::PropertyMapEntry aEntries[] =
309 { MAP_CHAR_LEN( "GraphicType" ), UNOGRAPHIC_GRAPHICTYPE, &::getCppuType( (const sal_Int8*)(0)), beans::PropertyAttribute::READONLY, 0 },
310 { MAP_CHAR_LEN( "MimeType" ), UNOGRAPHIC_MIMETYPE, &::getCppuType( (const OUString*)(0)), beans::PropertyAttribute::READONLY, 0 },
311 { MAP_CHAR_LEN( "SizePixel" ), UNOGRAPHIC_SIZEPIXEL, &::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
312 { MAP_CHAR_LEN( "Size100thMM" ), UNOGRAPHIC_SIZE100THMM, &::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
313 { MAP_CHAR_LEN( "BitsPerPixel" ), UNOGRAPHIC_BITSPERPIXEL, &::getCppuType( (const sal_uInt8*)(0)), beans::PropertyAttribute::READONLY, 0 },
314 { MAP_CHAR_LEN( "Transparent" ), UNOGRAPHIC_TRANSPARENT, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
315 { MAP_CHAR_LEN( "Alpha" ), UNOGRAPHIC_ALPHA, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
316 { MAP_CHAR_LEN( "Animated" ), UNOGRAPHIC_ANIMATED, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
318 { 0,0,0,0,0,0 }
321 pRet->acquire();
322 pRet->add( aEntries );
324 return pRet;
327 // ------------------------------------------------------------------------------
329 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
330 throw( beans::UnknownPropertyException,
331 beans::PropertyVetoException,
332 lang::IllegalArgumentException,
333 lang::WrappedTargetException )
335 // we only have readonly attributes
338 // ------------------------------------------------------------------------------
340 void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
341 throw( beans::UnknownPropertyException, lang::WrappedTargetException )
343 SolarMutexGuard aGuard;
345 while( *ppEntries )
347 switch( (*ppEntries)->mnHandle )
349 case( UNOGRAPHIC_GRAPHICTYPE ):
351 const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
353 *pValues <<= ( ( eType == GRAPHIC_BITMAP ? graphic::GraphicType::PIXEL :
354 ( eType == GRAPHIC_GDIMETAFILE ? graphic::GraphicType::VECTOR :
355 graphic::GraphicType::EMPTY ) ) );
357 break;
359 case( UNOGRAPHIC_MIMETYPE ):
361 OUString aMimeType;
363 if( mpGraphic )
365 if( mpGraphic->IsLink() )
367 const char* pMimeType;
369 switch( const_cast< Graphic* >( mpGraphic )->GetLink().GetType() )
371 case( GFX_LINK_TYPE_NATIVE_GIF ): pMimeType = MIMETYPE_GIF; break;
372 case( GFX_LINK_TYPE_NATIVE_JPG ): pMimeType = MIMETYPE_JPG; break;
373 case( GFX_LINK_TYPE_NATIVE_PNG ): pMimeType = MIMETYPE_PNG; break;
374 case( GFX_LINK_TYPE_NATIVE_WMF ): pMimeType = MIMETYPE_WMF; break;
375 case( GFX_LINK_TYPE_NATIVE_MET ): pMimeType = MIMETYPE_MET; break;
376 case( GFX_LINK_TYPE_NATIVE_PCT ): pMimeType = MIMETYPE_PCT; break;
378 // added Svg mimetype support
379 case( GFX_LINK_TYPE_NATIVE_SVG ): pMimeType = MIMETYPE_SVG; break;
381 default:
382 pMimeType = NULL;
383 break;
386 if( pMimeType )
387 aMimeType = OUString::createFromAscii( pMimeType );
390 if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GRAPHIC_NONE ) )
391 aMimeType = OUString(MIMETYPE_VCLGRAPHIC );
393 else
394 aMimeType = maMimeType;
396 *pValues <<= aMimeType;
398 break;
400 case( UNOGRAPHIC_SIZEPIXEL ):
402 awt::Size aAWTSize( 0, 0 );
404 if( mpGraphic )
406 if( mpGraphic->GetType() == GRAPHIC_BITMAP )
408 const Size aSizePix( mpGraphic->GetBitmapEx().GetSizePixel() );
409 aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
412 else
413 aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
415 *pValues <<= aAWTSize;
417 break;
419 case( UNOGRAPHIC_SIZE100THMM ):
421 awt::Size aAWTSize( 0, 0 );
423 if( mpGraphic )
425 if( mpGraphic->GetPrefMapMode().GetMapUnit() != MAP_PIXEL )
427 const Size aSizeLog( OutputDevice::LogicToLogic( mpGraphic->GetPrefSize(), mpGraphic->GetPrefMapMode(), MAP_100TH_MM ) );
428 aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
431 else
432 aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
434 *pValues <<= aAWTSize;
436 break;
438 case( UNOGRAPHIC_BITSPERPIXEL ):
440 sal_uInt16 nBitsPerPixel = 0;
442 if( mpGraphic )
444 if( mpGraphic->GetType() == GRAPHIC_BITMAP )
445 nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount();
447 else
448 nBitsPerPixel = mnBitsPerPixel;
450 *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
452 break;
454 case( UNOGRAPHIC_TRANSPARENT ):
456 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsTransparent() : mbTransparent );
458 break;
460 case( UNOGRAPHIC_ALPHA ):
462 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAlpha() : mbAlpha );
464 break;
466 case( UNOGRAPHIC_ANIMATED ):
468 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAnimated() : mbAnimated );
470 break;
473 ++ppEntries;
474 ++pValues;
480 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */