1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
37 #define UNOGRAPHIC_GRAPHICTYPE 1
38 #define UNOGRAPHIC_MIMETYPE 2
39 #define UNOGRAPHIC_SIZEPIXEL 3
40 #define UNOGRAPHIC_SIZE100THMM 4
41 #define UNOGRAPHIC_BITSPERPIXEL 5
42 #define UNOGRAPHIC_TRANSPARENT 6
43 #define UNOGRAPHIC_ALPHA 7
44 #define UNOGRAPHIC_ANIMATED 8
46 using namespace ::com::sun::star
;
48 namespace unographic
{
50 // ---------------------
51 // - GraphicDescriptor -
52 // ---------------------
54 GraphicDescriptor::GraphicDescriptor() :
55 ::comphelper::PropertySetHelper( createPropertySetInfo(), SAL_NO_ACQUIRE
),
57 meType( GRAPHIC_NONE
),
59 mbTransparent ( false ),
65 // ------------------------------------------------------------------------------
67 GraphicDescriptor::~GraphicDescriptor()
72 // ------------------------------------------------------------------------------
74 void GraphicDescriptor::init( const ::Graphic
& rGraphic
)
77 mpGraphic
= &rGraphic
;
80 // ------------------------------------------------------------------------------
82 void GraphicDescriptor::init( const OUString
& rURL
)
85 SvStream
* pIStm
= ::utl::UcbStreamHelper::CreateStream( rURL
, STREAM_READ
);
89 implCreate( *pIStm
, &rURL
);
94 // ------------------------------------------------------------------------------
96 void GraphicDescriptor::init( const uno::Reference
< io::XInputStream
>& rxIStm
, const OUString
& rURL
)
99 SvStream
* pIStm
= ::utl::UcbStreamHelper::CreateStream( rxIStm
);
103 implCreate( *pIStm
, &rURL
);
108 // ------------------------------------------------------------------------------
110 void GraphicDescriptor::implCreate( SvStream
& rIStm
, const OUString
* pURL
)
115 ::GraphicDescriptor
aDescriptor( rIStm
, &aURL
);
118 maMimeType
= OUString();
119 meType
= GRAPHIC_NONE
;
121 mbTransparent
= false;
123 if( aDescriptor
.Detect( true ) && aDescriptor
.GetFileFormat() != GFF_NOT
)
125 const char* pMimeType
= NULL
;
126 sal_uInt8 cType
= graphic::GraphicType::EMPTY
;
128 switch( aDescriptor
.GetFileFormat() )
130 case( GFF_BMP
): pMimeType
= MIMETYPE_BMP
; cType
= graphic::GraphicType::PIXEL
; break;
131 case( GFF_GIF
): pMimeType
= MIMETYPE_GIF
; cType
= graphic::GraphicType::PIXEL
; break;
132 case( GFF_JPG
): pMimeType
= MIMETYPE_JPG
; cType
= graphic::GraphicType::PIXEL
; break;
133 case( GFF_PCD
): pMimeType
= MIMETYPE_PCD
; cType
= graphic::GraphicType::PIXEL
; break;
134 case( GFF_PCX
): pMimeType
= MIMETYPE_PCX
; cType
= graphic::GraphicType::PIXEL
; break;
135 case( GFF_PNG
): pMimeType
= MIMETYPE_PNG
; cType
= graphic::GraphicType::PIXEL
; break;
136 case( GFF_TIF
): pMimeType
= MIMETYPE_TIF
; cType
= graphic::GraphicType::PIXEL
; break;
137 case( GFF_XBM
): pMimeType
= MIMETYPE_XBM
; cType
= graphic::GraphicType::PIXEL
; break;
138 case( GFF_XPM
): pMimeType
= MIMETYPE_XPM
; cType
= graphic::GraphicType::PIXEL
; break;
139 case( GFF_PBM
): pMimeType
= MIMETYPE_PBM
; cType
= graphic::GraphicType::PIXEL
; break;
140 case( GFF_PGM
): pMimeType
= MIMETYPE_PGM
; cType
= graphic::GraphicType::PIXEL
; break;
141 case( GFF_PPM
): pMimeType
= MIMETYPE_PPM
; cType
= graphic::GraphicType::PIXEL
; break;
142 case( GFF_RAS
): pMimeType
= MIMETYPE_RAS
; cType
= graphic::GraphicType::PIXEL
; break;
143 case( GFF_TGA
): pMimeType
= MIMETYPE_TGA
; cType
= graphic::GraphicType::PIXEL
; break;
144 case( GFF_PSD
): pMimeType
= MIMETYPE_PSD
; cType
= graphic::GraphicType::PIXEL
; break;
146 case( GFF_EPS
): pMimeType
= MIMETYPE_EPS
; cType
= graphic::GraphicType::VECTOR
; break;
147 case( GFF_DXF
): pMimeType
= MIMETYPE_DXF
; cType
= graphic::GraphicType::VECTOR
; break;
148 case( GFF_MET
): pMimeType
= MIMETYPE_MET
; cType
= graphic::GraphicType::VECTOR
; break;
149 case( GFF_PCT
): pMimeType
= MIMETYPE_PCT
; cType
= graphic::GraphicType::VECTOR
; break;
150 case( GFF_SGF
): pMimeType
= MIMETYPE_SGF
; cType
= graphic::GraphicType::VECTOR
; break;
151 case( GFF_SVM
): pMimeType
= MIMETYPE_SVM
; cType
= graphic::GraphicType::VECTOR
; break;
152 case( GFF_WMF
): pMimeType
= MIMETYPE_WMF
; cType
= graphic::GraphicType::VECTOR
; break;
153 case( GFF_SGV
): pMimeType
= MIMETYPE_SGV
; cType
= graphic::GraphicType::VECTOR
; break;
154 case( GFF_EMF
): pMimeType
= MIMETYPE_EMF
; cType
= graphic::GraphicType::VECTOR
; break;
155 case( GFF_SVG
): pMimeType
= MIMETYPE_SVG
; cType
= graphic::GraphicType::VECTOR
; break;
161 if( graphic::GraphicType::EMPTY
!= cType
)
163 meType
= ( ( graphic::GraphicType::PIXEL
== cType
) ? GRAPHIC_BITMAP
: GRAPHIC_GDIMETAFILE
);
164 maMimeType
= OUString( pMimeType
, strlen(pMimeType
), RTL_TEXTENCODING_ASCII_US
);
165 maSizePixel
= aDescriptor
.GetSizePixel();
166 maSize100thMM
= aDescriptor
.GetSize_100TH_MM();
167 mnBitsPerPixel
= aDescriptor
.GetBitsPerPixel();
168 mbTransparent
= ( graphic::GraphicType::VECTOR
== cType
);
169 mbAlpha
= mbAnimated
= false;
174 // ------------------------------------------------------------------------------
176 OUString
GraphicDescriptor::getImplementationName_Static()
179 return OUString( "com.sun.star.comp.graphic.GraphicDescriptor" );
182 // ------------------------------------------------------------------------------
184 uno::Sequence
< OUString
> GraphicDescriptor::getSupportedServiceNames_Static()
187 uno::Sequence
< OUString
> aSeq( 1 );
189 aSeq
.getArray()[ 0 ] = "com.sun.star.graphic.GraphicDescriptor";
194 // ------------------------------------------------------------------------------
196 uno::Any SAL_CALL
GraphicDescriptor::queryAggregation( const uno::Type
& rType
)
197 throw( uno::RuntimeException
)
201 if( rType
== ::getCppuType((const uno::Reference
< lang::XServiceInfo
>*)0) )
202 aAny
<<= uno::Reference
< lang::XServiceInfo
>(this);
203 else if( rType
== ::getCppuType((const uno::Reference
< lang::XTypeProvider
>*)0) )
204 aAny
<<= uno::Reference
< lang::XTypeProvider
>(this);
205 else if( rType
== ::getCppuType((const uno::Reference
< beans::XPropertySet
>*)0) )
206 aAny
<<= uno::Reference
< beans::XPropertySet
>(this);
207 else if( rType
== ::getCppuType((const uno::Reference
< beans::XPropertyState
>*)0) )
208 aAny
<<= uno::Reference
< beans::XPropertyState
>(this);
209 else if( rType
== ::getCppuType((const uno::Reference
< beans::XMultiPropertySet
>*)0) )
210 aAny
<<= uno::Reference
< beans::XMultiPropertySet
>(this);
212 aAny
<<= OWeakAggObject::queryAggregation( rType
);
217 // ------------------------------------------------------------------------------
219 uno::Any SAL_CALL
GraphicDescriptor::queryInterface( const uno::Type
& rType
)
220 throw( uno::RuntimeException
)
222 return OWeakAggObject::queryInterface( rType
);
225 // ------------------------------------------------------------------------------
227 void SAL_CALL
GraphicDescriptor::acquire()
230 OWeakAggObject::acquire();
233 // ------------------------------------------------------------------------------
235 void SAL_CALL
GraphicDescriptor::release()
238 OWeakAggObject::release();
241 // ------------------------------------------------------------------------------
243 OUString SAL_CALL
GraphicDescriptor::getImplementationName()
244 throw( uno::RuntimeException
)
246 return getImplementationName_Static();
249 sal_Bool SAL_CALL
GraphicDescriptor::supportsService( const OUString
& ServiceName
)
250 throw( uno::RuntimeException
)
252 return cppu::supportsService(this, ServiceName
);
255 // ------------------------------------------------------------------------------
257 uno::Sequence
< OUString
> SAL_CALL
GraphicDescriptor::getSupportedServiceNames()
258 throw( uno::RuntimeException
)
260 return getSupportedServiceNames_Static();
263 // ------------------------------------------------------------------------------
265 uno::Sequence
< uno::Type
> SAL_CALL
GraphicDescriptor::getTypes()
266 throw( uno::RuntimeException
)
268 uno::Sequence
< uno::Type
> aTypes( 6 );
269 uno::Type
* pTypes
= aTypes
.getArray();
271 *pTypes
++ = ::getCppuType((const uno::Reference
< uno::XAggregation
>*)0);
272 *pTypes
++ = ::getCppuType((const uno::Reference
< lang::XServiceInfo
>*)0);
273 *pTypes
++ = ::getCppuType((const uno::Reference
< lang::XTypeProvider
>*)0);
274 *pTypes
++ = ::getCppuType((const uno::Reference
< beans::XPropertySet
>*)0);
275 *pTypes
++ = ::getCppuType((const uno::Reference
< beans::XPropertyState
>*)0);
276 *pTypes
++ = ::getCppuType((const uno::Reference
< beans::XMultiPropertySet
>*)0);
283 class theGraphicDescriptorUnoTunnelId
: public rtl::Static
< UnoTunnelIdInit
, theGraphicDescriptorUnoTunnelId
> {};
286 uno::Sequence
< sal_Int8
> SAL_CALL
GraphicDescriptor::getImplementationId()
287 throw( uno::RuntimeException
)
289 return theGraphicDescriptorUnoTunnelId::get().getSeq();
292 // ------------------------------------------------------------------------------
294 ::comphelper::PropertySetInfo
* GraphicDescriptor::createPropertySetInfo()
296 SolarMutexGuard aGuard
;
297 ::comphelper::PropertySetInfo
* pRet
= new ::comphelper::PropertySetInfo();
299 static ::comphelper::PropertyMapEntry aEntries
[] =
301 { MAP_CHAR_LEN( "GraphicType" ), UNOGRAPHIC_GRAPHICTYPE
, &::getCppuType( (const sal_Int8
*)(0)), beans::PropertyAttribute::READONLY
, 0 },
302 { MAP_CHAR_LEN( "MimeType" ), UNOGRAPHIC_MIMETYPE
, &::getCppuType( (const OUString
*)(0)), beans::PropertyAttribute::READONLY
, 0 },
303 { MAP_CHAR_LEN( "SizePixel" ), UNOGRAPHIC_SIZEPIXEL
, &::getCppuType( (const awt::Size
*)(0)), beans::PropertyAttribute::READONLY
, 0 },
304 { MAP_CHAR_LEN( "Size100thMM" ), UNOGRAPHIC_SIZE100THMM
, &::getCppuType( (const awt::Size
*)(0)), beans::PropertyAttribute::READONLY
, 0 },
305 { MAP_CHAR_LEN( "BitsPerPixel" ), UNOGRAPHIC_BITSPERPIXEL
, &::getCppuType( (const sal_uInt8
*)(0)), beans::PropertyAttribute::READONLY
, 0 },
306 { MAP_CHAR_LEN( "Transparent" ), UNOGRAPHIC_TRANSPARENT
, &::getCppuType( (const sal_Bool
*)(0)), beans::PropertyAttribute::READONLY
, 0 },
307 { MAP_CHAR_LEN( "Alpha" ), UNOGRAPHIC_ALPHA
, &::getCppuType( (const sal_Bool
*)(0)), beans::PropertyAttribute::READONLY
, 0 },
308 { MAP_CHAR_LEN( "Animated" ), UNOGRAPHIC_ANIMATED
, &::getCppuType( (const sal_Bool
*)(0)), beans::PropertyAttribute::READONLY
, 0 },
314 pRet
->add( aEntries
);
319 // ------------------------------------------------------------------------------
321 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry
** /*ppEntries*/, const uno::Any
* /*pValues*/ )
322 throw( beans::UnknownPropertyException
,
323 beans::PropertyVetoException
,
324 lang::IllegalArgumentException
,
325 lang::WrappedTargetException
)
327 // we only have readonly attributes
330 // ------------------------------------------------------------------------------
332 void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry
** ppEntries
, uno::Any
* pValues
)
333 throw( beans::UnknownPropertyException
, lang::WrappedTargetException
)
335 SolarMutexGuard aGuard
;
339 switch( (*ppEntries
)->mnHandle
)
341 case( UNOGRAPHIC_GRAPHICTYPE
):
343 const GraphicType
eType( mpGraphic
? mpGraphic
->GetType() : meType
);
345 *pValues
<<= ( ( eType
== GRAPHIC_BITMAP
? graphic::GraphicType::PIXEL
:
346 ( eType
== GRAPHIC_GDIMETAFILE
? graphic::GraphicType::VECTOR
:
347 graphic::GraphicType::EMPTY
) ) );
351 case( UNOGRAPHIC_MIMETYPE
):
357 if( mpGraphic
->IsLink() )
359 const char* pMimeType
;
361 switch( const_cast< Graphic
* >( mpGraphic
)->GetLink().GetType() )
363 case( GFX_LINK_TYPE_NATIVE_GIF
): pMimeType
= MIMETYPE_GIF
; break;
364 case( GFX_LINK_TYPE_NATIVE_JPG
): pMimeType
= MIMETYPE_JPG
; break;
365 case( GFX_LINK_TYPE_NATIVE_PNG
): pMimeType
= MIMETYPE_PNG
; break;
366 case( GFX_LINK_TYPE_NATIVE_WMF
): pMimeType
= MIMETYPE_WMF
; break;
367 case( GFX_LINK_TYPE_NATIVE_MET
): pMimeType
= MIMETYPE_MET
; break;
368 case( GFX_LINK_TYPE_NATIVE_PCT
): pMimeType
= MIMETYPE_PCT
; break;
370 // added Svg mimetype support
371 case( GFX_LINK_TYPE_NATIVE_SVG
): pMimeType
= MIMETYPE_SVG
; break;
379 aMimeType
= OUString::createFromAscii( pMimeType
);
382 if( aMimeType
.isEmpty() && ( mpGraphic
->GetType() != GRAPHIC_NONE
) )
383 aMimeType
= MIMETYPE_VCLGRAPHIC
;
386 aMimeType
= maMimeType
;
388 *pValues
<<= aMimeType
;
392 case( UNOGRAPHIC_SIZEPIXEL
):
394 awt::Size
aAWTSize( 0, 0 );
398 if( mpGraphic
->GetType() == GRAPHIC_BITMAP
)
400 const Size
aSizePix( mpGraphic
->GetBitmapEx().GetSizePixel() );
401 aAWTSize
= awt::Size( aSizePix
.Width(), aSizePix
.Height() );
405 aAWTSize
= awt::Size( maSizePixel
.Width(), maSizePixel
.Height() );
407 *pValues
<<= aAWTSize
;
411 case( UNOGRAPHIC_SIZE100THMM
):
413 awt::Size
aAWTSize( 0, 0 );
417 if( mpGraphic
->GetPrefMapMode().GetMapUnit() != MAP_PIXEL
)
419 const Size
aSizeLog( OutputDevice::LogicToLogic( mpGraphic
->GetPrefSize(), mpGraphic
->GetPrefMapMode(), MAP_100TH_MM
) );
420 aAWTSize
= awt::Size( aSizeLog
.Width(), aSizeLog
.Height() );
424 aAWTSize
= awt::Size( maSize100thMM
.Width(), maSize100thMM
.Height() );
426 *pValues
<<= aAWTSize
;
430 case( UNOGRAPHIC_BITSPERPIXEL
):
432 sal_uInt16 nBitsPerPixel
= 0;
436 if( mpGraphic
->GetType() == GRAPHIC_BITMAP
)
437 nBitsPerPixel
= mpGraphic
->GetBitmapEx().GetBitmap().GetBitCount();
440 nBitsPerPixel
= mnBitsPerPixel
;
442 *pValues
<<= sal::static_int_cast
< sal_Int8
>(nBitsPerPixel
);
446 case( UNOGRAPHIC_TRANSPARENT
):
448 *pValues
<<= static_cast< sal_Bool
>( mpGraphic
? mpGraphic
->IsTransparent() : mbTransparent
);
452 case( UNOGRAPHIC_ALPHA
):
454 *pValues
<<= static_cast< sal_Bool
>( mpGraphic
? mpGraphic
->IsAlpha() : mbAlpha
);
458 case( UNOGRAPHIC_ANIMATED
):
460 *pValues
<<= static_cast< sal_Bool
>( mpGraphic
? mpGraphic
->IsAnimated() : mbAnimated
);
472 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */