1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svtools.hxx"
26
27 #include "descriptor.hxx"
28
29 #include <rtl/uuid.h>
30 #include <vos/mutex.hxx>
31 #include <unotools/ucbstreamhelper.hxx>
32 #include <svtools/filter.hxx>
33 #include <svl/itemprop.hxx>
34
35 #include <com/sun/star/beans/PropertyState.hpp>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/awt/Size.hpp>
38 #include <com/sun/star/graphic/GraphicType.hpp>
39
40 #include "vcl/graph.hxx"
41 #include "vcl/svapp.hxx"
42
43 #define UNOGRAPHIC_GRAPHICTYPE 1
44 #define UNOGRAPHIC_MIMETYPE 2
45 #define UNOGRAPHIC_SIZEPIXEL 3
46 #define UNOGRAPHIC_SIZE100THMM 4
47 #define UNOGRAPHIC_BITSPERPIXEL 5
48 #define UNOGRAPHIC_TRANSPARENT 6
49 #define UNOGRAPHIC_ALPHA 7
50 #define UNOGRAPHIC_ANIMATED 8
51
52 using namespace ::com::sun::star;
53
54 namespace unographic {
55
56 // ---------------------
57 // - GraphicDescriptor -
58 // ---------------------
59
GraphicDescriptor()60 GraphicDescriptor::GraphicDescriptor() :
61 ::comphelper::PropertySetHelper( createPropertySetInfo(), SAL_NO_ACQUIRE ),
62 mpGraphic( NULL ),
63 meType( GRAPHIC_NONE ),
64 mnBitsPerPixel ( 0 ),
65 mbTransparent ( false ),
66 mbAlpha( false ),
67 mbAnimated( false )
68 {
69 }
70
71 // ------------------------------------------------------------------------------
72
~GraphicDescriptor()73 GraphicDescriptor::~GraphicDescriptor()
74 throw()
75 {
76 }
77
78 // ------------------------------------------------------------------------------
79
init(const::Graphic & rGraphic)80 void GraphicDescriptor::init( const ::Graphic& rGraphic )
81 throw()
82 {
83 mpGraphic = &rGraphic;
84 }
85
86 // ------------------------------------------------------------------------------
87
init(const::rtl::OUString & rURL)88 void GraphicDescriptor::init( const ::rtl::OUString& rURL )
89 throw()
90 {
91 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rURL, STREAM_READ );
92
93 if( pIStm )
94 {
95 implCreate( *pIStm, &rURL );
96 delete pIStm;
97 }
98 }
99
100 // ------------------------------------------------------------------------------
101
init(const uno::Reference<io::XInputStream> & rxIStm,const::rtl::OUString & rURL)102 void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const ::rtl::OUString& rURL )
103 throw()
104 {
105 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rxIStm );
106
107 if( pIStm )
108 {
109 implCreate( *pIStm, &rURL );
110 delete pIStm;
111 }
112 }
113
114 // ------------------------------------------------------------------------------
115
isValid() const116 bool GraphicDescriptor::isValid() const
117 {
118 return( mpGraphic ? ( mpGraphic->GetType() != GRAPHIC_NONE ) : ( meType != GRAPHIC_NONE ) );
119 }
120
121 // ------------------------------------------------------------------------------
122
implCreate(SvStream & rIStm,const::rtl::OUString * pURL)123 void GraphicDescriptor::implCreate( SvStream& rIStm, const ::rtl::OUString* pURL )
124 {
125 String aURL;
126 if( pURL )
127 aURL = *pURL;
128 ::GraphicDescriptor aDescriptor( rIStm, &aURL );
129
130 mpGraphic = NULL;
131 maMimeType = ::rtl::OUString();
132 meType = GRAPHIC_NONE;
133 mnBitsPerPixel = 0;
134 mbTransparent = false;
135
136 if( aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GFF_NOT )
137 {
138 const char* pMimeType = NULL;
139 sal_uInt8 cType = graphic::GraphicType::EMPTY;
140
141 switch( aDescriptor.GetFileFormat() )
142 {
143 case( GFF_BMP ): pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
144 case( GFF_GIF ): pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
145 case( GFF_JPG ): pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
146 case( GFF_PCD ): pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
147 case( GFF_PCX ): pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
148 case( GFF_PNG ): pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
149 case( GFF_TIF ): pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
150 case( GFF_XBM ): pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
151 case( GFF_XPM ): pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
152 case( GFF_PBM ): pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
153 case( GFF_PGM ): pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
154 case( GFF_PPM ): pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
155 case( GFF_RAS ): pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
156 case( GFF_TGA ): pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
157 case( GFF_PSD ): pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
158
159 case( GFF_EPS ): pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
160 case( GFF_DXF ): pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
161 case( GFF_MET ): pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
162 case( GFF_PCT ): pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
163 case( GFF_SGF ): pMimeType = MIMETYPE_SGF; cType = graphic::GraphicType::VECTOR; break;
164 case( GFF_SVM ): pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
165 case( GFF_WMF ): pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
166 case( GFF_SGV ): pMimeType = MIMETYPE_SGV; cType = graphic::GraphicType::VECTOR; break;
167 case( GFF_EMF ): pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
168 case( GFF_SVG ): pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
169
170 default:
171 break;
172 }
173
174 if( graphic::GraphicType::EMPTY != cType )
175 {
176 meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GRAPHIC_BITMAP : GRAPHIC_GDIMETAFILE );
177 maMimeType = String( pMimeType, RTL_TEXTENCODING_ASCII_US );
178 maSizePixel = aDescriptor.GetSizePixel();
179 maSize100thMM = aDescriptor.GetSize_100TH_MM();
180 mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
181 mbTransparent = ( graphic::GraphicType::VECTOR == cType );
182 mbAlpha = mbAnimated = false;
183 }
184 }
185 }
186
187 // ------------------------------------------------------------------------------
188
getImplementationName_Static()189 ::rtl::OUString GraphicDescriptor::getImplementationName_Static()
190 throw()
191 {
192 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.graphic.GraphicDescriptor" ) );
193 }
194
195 // ------------------------------------------------------------------------------
196
getSupportedServiceNames_Static()197 uno::Sequence< ::rtl::OUString > GraphicDescriptor::getSupportedServiceNames_Static()
198 throw( )
199 {
200 uno::Sequence< ::rtl::OUString > aSeq( 1 );
201
202 aSeq.getArray()[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicDescriptor" ) );
203
204 return aSeq;
205 }
206
207 // ------------------------------------------------------------------------------
208
queryAggregation(const uno::Type & rType)209 uno::Any SAL_CALL GraphicDescriptor::queryAggregation( const uno::Type & rType )
210 throw( uno::RuntimeException )
211 {
212 uno::Any aAny;
213
214 if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
215 aAny <<= uno::Reference< lang::XServiceInfo >(this);
216 else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
217 aAny <<= uno::Reference< lang::XTypeProvider >(this);
218 else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
219 aAny <<= uno::Reference< beans::XPropertySet >(this);
220 else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
221 aAny <<= uno::Reference< beans::XPropertyState >(this);
222 else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
223 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
224 else
225 aAny <<= OWeakAggObject::queryAggregation( rType );
226
227 return aAny;
228 }
229
230 // ------------------------------------------------------------------------------
231
queryInterface(const uno::Type & rType)232 uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
233 throw( uno::RuntimeException )
234 {
235 return OWeakAggObject::queryInterface( rType );
236 }
237
238 // ------------------------------------------------------------------------------
239
acquire()240 void SAL_CALL GraphicDescriptor::acquire()
241 throw()
242 {
243 OWeakAggObject::acquire();
244 }
245
246 // ------------------------------------------------------------------------------
247
release()248 void SAL_CALL GraphicDescriptor::release()
249 throw()
250 {
251 OWeakAggObject::release();
252 }
253
254 // ------------------------------------------------------------------------------
255
getImplementationName()256 ::rtl::OUString SAL_CALL GraphicDescriptor::getImplementationName()
257 throw( uno::RuntimeException )
258 {
259 return getImplementationName_Static();
260 }
261
262 // ------------------------------------------------------------------------------
263
supportsService(const rtl::OUString & ServiceName)264 sal_Bool SAL_CALL GraphicDescriptor::supportsService( const rtl::OUString& ServiceName )
265 throw( uno::RuntimeException )
266 {
267 uno::Sequence< ::rtl::OUString > aSNL( getSupportedServiceNames() );
268 const ::rtl::OUString* pArray = aSNL.getConstArray();
269
270 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
271 if( pArray[i] == ServiceName )
272 return true;
273
274 return false;
275 }
276
277 // ------------------------------------------------------------------------------
278
getSupportedServiceNames()279 uno::Sequence< rtl::OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
280 throw( uno::RuntimeException )
281 {
282 return getSupportedServiceNames_Static();
283 }
284
285 // ------------------------------------------------------------------------------
286
getTypes()287 uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
288 throw( uno::RuntimeException )
289 {
290 uno::Sequence< uno::Type > aTypes( 6 );
291 uno::Type* pTypes = aTypes.getArray();
292
293 *pTypes++ = ::getCppuType((const uno::Reference< uno::XAggregation>*)0);
294 *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
295 *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
296 *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
297 *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
298 *pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
299
300 return aTypes;
301 }
302
303 // ------------------------------------------------------------------------------
304
getImplementationId()305 uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
306 throw( uno::RuntimeException )
307 {
308 vos::OGuard aGuard( Application::GetSolarMutex() );
309 static uno::Sequence< sal_Int8 > aId;
310
311 if( aId.getLength() == 0 )
312 {
313 aId.realloc( 16 );
314 rtl_createUuid( reinterpret_cast< sal_uInt8* >( aId.getArray() ), 0, sal_True );
315 }
316
317 return aId;
318 }
319
320 // ------------------------------------------------------------------------------
321
createPropertySetInfo()322 ::comphelper::PropertySetInfo* GraphicDescriptor::createPropertySetInfo()
323 {
324 vos::OGuard aGuard( Application::GetSolarMutex() );
325 ::comphelper::PropertySetInfo* pRet = new ::comphelper::PropertySetInfo();
326
327 static ::comphelper::PropertyMapEntry aEntries[] =
328 {
329 { MAP_CHAR_LEN( "GraphicType" ), UNOGRAPHIC_GRAPHICTYPE, &::getCppuType( (const sal_Int8*)(0)), beans::PropertyAttribute::READONLY, 0 },
330 { MAP_CHAR_LEN( "MimeType" ), UNOGRAPHIC_MIMETYPE, &::getCppuType( (const ::rtl::OUString*)(0)), beans::PropertyAttribute::READONLY, 0 },
331 { MAP_CHAR_LEN( "SizePixel" ), UNOGRAPHIC_SIZEPIXEL, &::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
332 { MAP_CHAR_LEN( "Size100thMM" ), UNOGRAPHIC_SIZE100THMM, &::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
333 { MAP_CHAR_LEN( "BitsPerPixel" ), UNOGRAPHIC_BITSPERPIXEL, &::getCppuType( (const sal_uInt8*)(0)), beans::PropertyAttribute::READONLY, 0 },
334 { MAP_CHAR_LEN( "Transparent" ), UNOGRAPHIC_TRANSPARENT, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
335 { MAP_CHAR_LEN( "Alpha" ), UNOGRAPHIC_ALPHA, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
336 { MAP_CHAR_LEN( "Animated" ), UNOGRAPHIC_ANIMATED, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
337
338 { 0,0,0,0,0,0 }
339 };
340
341 pRet->acquire();
342 pRet->add( aEntries );
343
344 return pRet;
345 }
346
347 // ------------------------------------------------------------------------------
348
_setPropertyValues(const comphelper::PropertyMapEntry **,const uno::Any *)349 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
350 throw( beans::UnknownPropertyException,
351 beans::PropertyVetoException,
352 lang::IllegalArgumentException,
353 lang::WrappedTargetException )
354 {
355 // we only have readonly attributes
356 }
357
358 // ------------------------------------------------------------------------------
359
_getPropertyValues(const comphelper::PropertyMapEntry ** ppEntries,uno::Any * pValues)360 void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
361 throw( beans::UnknownPropertyException, lang::WrappedTargetException )
362 {
363 ::vos::OGuard aGuard( Application::GetSolarMutex() );
364
365 while( *ppEntries )
366 {
367 switch( (*ppEntries)->mnHandle )
368 {
369 case( UNOGRAPHIC_GRAPHICTYPE ):
370 {
371 const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
372
373 *pValues <<= ( ( eType == GRAPHIC_BITMAP ? graphic::GraphicType::PIXEL :
374 ( eType == GRAPHIC_GDIMETAFILE ? graphic::GraphicType::VECTOR :
375 graphic::GraphicType::EMPTY ) ) );
376 }
377 break;
378
379 case( UNOGRAPHIC_MIMETYPE ):
380 {
381 ::rtl::OUString aMimeType;
382
383 if( mpGraphic )
384 {
385 if( mpGraphic->IsLink() )
386 {
387 const char* pMimeType;
388
389 switch( const_cast< Graphic* >( mpGraphic )->GetLink().GetType() )
390 {
391 case( GFX_LINK_TYPE_NATIVE_GIF ): pMimeType = MIMETYPE_GIF; break;
392
393 // #15508# added BMP type for better exports (checked, works)
394 case( GFX_LINK_TYPE_NATIVE_BMP ): pMimeType = MIMETYPE_BMP; break;
395
396 case( GFX_LINK_TYPE_NATIVE_JPG ): pMimeType = MIMETYPE_JPG; break;
397 case( GFX_LINK_TYPE_NATIVE_PNG ): pMimeType = MIMETYPE_PNG; break;
398 case( GFX_LINK_TYPE_NATIVE_WMF ): pMimeType = MIMETYPE_WMF; break;
399 case( GFX_LINK_TYPE_NATIVE_MET ): pMimeType = MIMETYPE_MET; break;
400 case( GFX_LINK_TYPE_NATIVE_PCT ): pMimeType = MIMETYPE_PCT; break;
401
402 // added Svg mimetype support
403 case( GFX_LINK_TYPE_NATIVE_SVG ): pMimeType = MIMETYPE_SVG; break;
404
405 default:
406 pMimeType = NULL;
407 break;
408 }
409
410 if( pMimeType )
411 aMimeType = ::rtl::OUString::createFromAscii( pMimeType );
412 }
413
414 if( !aMimeType.getLength() && ( mpGraphic->GetType() != GRAPHIC_NONE ) )
415 aMimeType = ::rtl::OUString::createFromAscii( MIMETYPE_VCLGRAPHIC );
416 }
417 else
418 aMimeType = maMimeType;
419
420 *pValues <<= aMimeType;
421 }
422 break;
423
424 case( UNOGRAPHIC_SIZEPIXEL ):
425 {
426 awt::Size aAWTSize( 0, 0 );
427
428 if( mpGraphic )
429 {
430 if( mpGraphic->GetType() == GRAPHIC_BITMAP )
431 {
432 const Size aSizePix( mpGraphic->GetBitmapEx().GetSizePixel() );
433 aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
434 }
435 }
436 else
437 aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
438
439 *pValues <<= aAWTSize;
440 }
441 break;
442
443 case( UNOGRAPHIC_SIZE100THMM ):
444 {
445 awt::Size aAWTSize( 0, 0 );
446
447 if( mpGraphic )
448 {
449 if( mpGraphic->GetPrefMapMode().GetMapUnit() != MAP_PIXEL )
450 {
451 const Size aSizeLog( OutputDevice::LogicToLogic( mpGraphic->GetPrefSize(), mpGraphic->GetPrefMapMode(), MAP_100TH_MM ) );
452 aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
453 }
454 }
455 else
456 aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
457
458 *pValues <<= aAWTSize;
459 }
460 break;
461
462 case( UNOGRAPHIC_BITSPERPIXEL ):
463 {
464 sal_uInt16 nBitsPerPixel = 0;
465
466 if( mpGraphic )
467 {
468 if( mpGraphic->GetType() == GRAPHIC_BITMAP )
469 nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount();
470 }
471 else
472 nBitsPerPixel = mnBitsPerPixel;
473
474 *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
475 }
476 break;
477
478 case( UNOGRAPHIC_TRANSPARENT ):
479 {
480 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsTransparent() : mbTransparent );
481 }
482 break;
483
484 case( UNOGRAPHIC_ALPHA ):
485 {
486 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAlpha() : mbAlpha );
487 }
488 break;
489
490 case( UNOGRAPHIC_ANIMATED ):
491 {
492 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAnimated() : mbAnimated );
493 }
494 break;
495 }
496
497 ++ppEntries;
498 ++pValues;
499 }
500 }
501
502 }
503