Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "descriptor.hxx"
21 :
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>
28 :
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>
33 :
34 : #include "vcl/graph.hxx"
35 : #include "vcl/svapp.hxx"
36 :
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
45 :
46 : using namespace ::com::sun::star;
47 :
48 : namespace unographic {
49 :
50 :
51 : // - GraphicDescriptor -
52 :
53 :
54 0 : GraphicDescriptor::GraphicDescriptor() :
55 : ::comphelper::PropertySetHelper( createPropertySetInfo(), SAL_NO_ACQUIRE ),
56 : mpGraphic( NULL ),
57 : meType( GRAPHIC_NONE ),
58 : mnBitsPerPixel ( 0 ),
59 : mbTransparent ( false ),
60 : mbAlpha( false ),
61 0 : mbAnimated( false )
62 : {
63 0 : }
64 :
65 :
66 :
67 0 : GraphicDescriptor::~GraphicDescriptor()
68 0 : throw()
69 : {
70 0 : }
71 :
72 :
73 :
74 0 : void GraphicDescriptor::init( const ::Graphic& rGraphic )
75 : throw()
76 : {
77 0 : mpGraphic = &rGraphic;
78 0 : }
79 :
80 :
81 :
82 0 : void GraphicDescriptor::init( const OUString& rURL )
83 : throw()
84 : {
85 0 : SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rURL, STREAM_READ );
86 :
87 0 : if( pIStm )
88 : {
89 0 : implCreate( *pIStm, &rURL );
90 0 : delete pIStm;
91 : }
92 0 : }
93 :
94 :
95 :
96 0 : void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL )
97 : throw()
98 : {
99 0 : SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rxIStm );
100 :
101 0 : if( pIStm )
102 : {
103 0 : implCreate( *pIStm, &rURL );
104 0 : delete pIStm;
105 : }
106 0 : }
107 :
108 :
109 :
110 0 : void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL )
111 : {
112 0 : OUString aURL;
113 0 : if( pURL )
114 0 : aURL = *pURL;
115 0 : ::GraphicDescriptor aDescriptor( rIStm, &aURL );
116 :
117 0 : mpGraphic = NULL;
118 0 : maMimeType = OUString();
119 0 : meType = GRAPHIC_NONE;
120 0 : mnBitsPerPixel = 0;
121 0 : mbTransparent = false;
122 :
123 0 : if( aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GFF_NOT )
124 : {
125 0 : const char* pMimeType = NULL;
126 0 : sal_uInt8 cType = graphic::GraphicType::EMPTY;
127 :
128 0 : switch( aDescriptor.GetFileFormat() )
129 : {
130 0 : case( GFF_BMP ): pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
131 0 : case( GFF_GIF ): pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
132 0 : case( GFF_JPG ): pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
133 0 : case( GFF_PCD ): pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
134 0 : case( GFF_PCX ): pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
135 0 : case( GFF_PNG ): pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
136 0 : case( GFF_TIF ): pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
137 0 : case( GFF_XBM ): pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
138 0 : case( GFF_XPM ): pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
139 0 : case( GFF_PBM ): pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
140 0 : case( GFF_PGM ): pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
141 0 : case( GFF_PPM ): pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
142 0 : case( GFF_RAS ): pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
143 0 : case( GFF_TGA ): pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
144 0 : case( GFF_PSD ): pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
145 :
146 0 : case( GFF_EPS ): pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
147 0 : case( GFF_DXF ): pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
148 0 : case( GFF_MET ): pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
149 0 : case( GFF_PCT ): pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
150 0 : case( GFF_SGF ): pMimeType = MIMETYPE_SGF; cType = graphic::GraphicType::VECTOR; break;
151 0 : case( GFF_SVM ): pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
152 0 : case( GFF_WMF ): pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
153 0 : case( GFF_SGV ): pMimeType = MIMETYPE_SGV; cType = graphic::GraphicType::VECTOR; break;
154 0 : case( GFF_EMF ): pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
155 0 : case( GFF_SVG ): pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
156 :
157 : default:
158 0 : break;
159 : }
160 :
161 0 : if( graphic::GraphicType::EMPTY != cType )
162 : {
163 0 : meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GRAPHIC_BITMAP : GRAPHIC_GDIMETAFILE );
164 0 : maMimeType = OUString( pMimeType, strlen(pMimeType), RTL_TEXTENCODING_ASCII_US );
165 0 : maSizePixel = aDescriptor.GetSizePixel();
166 0 : maSize100thMM = aDescriptor.GetSize_100TH_MM();
167 0 : mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
168 0 : mbTransparent = ( graphic::GraphicType::VECTOR == cType );
169 0 : mbAlpha = mbAnimated = false;
170 : }
171 0 : }
172 0 : }
173 :
174 :
175 :
176 0 : OUString GraphicDescriptor::getImplementationName_Static()
177 : throw()
178 : {
179 0 : return OUString( "com.sun.star.comp.graphic.GraphicDescriptor" );
180 : }
181 :
182 :
183 :
184 0 : uno::Sequence< OUString > GraphicDescriptor::getSupportedServiceNames_Static()
185 : throw( )
186 : {
187 0 : uno::Sequence< OUString > aSeq( 1 );
188 :
189 0 : aSeq.getArray()[ 0 ] = "com.sun.star.graphic.GraphicDescriptor";
190 :
191 0 : return aSeq;
192 : }
193 :
194 :
195 :
196 0 : uno::Any SAL_CALL GraphicDescriptor::queryAggregation( const uno::Type & rType )
197 : throw( uno::RuntimeException, std::exception )
198 : {
199 0 : uno::Any aAny;
200 :
201 0 : if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
202 0 : aAny <<= uno::Reference< lang::XServiceInfo >(this);
203 0 : else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
204 0 : aAny <<= uno::Reference< lang::XTypeProvider >(this);
205 0 : else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
206 0 : aAny <<= uno::Reference< beans::XPropertySet >(this);
207 0 : else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
208 0 : aAny <<= uno::Reference< beans::XPropertyState >(this);
209 0 : else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
210 0 : aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
211 : else
212 0 : aAny <<= OWeakAggObject::queryAggregation( rType );
213 :
214 0 : return aAny;
215 : }
216 :
217 :
218 :
219 0 : uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
220 : throw( uno::RuntimeException, std::exception )
221 : {
222 0 : return OWeakAggObject::queryInterface( rType );
223 : }
224 :
225 :
226 :
227 0 : void SAL_CALL GraphicDescriptor::acquire()
228 : throw()
229 : {
230 0 : OWeakAggObject::acquire();
231 0 : }
232 :
233 :
234 :
235 0 : void SAL_CALL GraphicDescriptor::release()
236 : throw()
237 : {
238 0 : OWeakAggObject::release();
239 0 : }
240 :
241 :
242 :
243 0 : OUString SAL_CALL GraphicDescriptor::getImplementationName()
244 : throw( uno::RuntimeException, std::exception )
245 : {
246 0 : return getImplementationName_Static();
247 : }
248 :
249 0 : sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
250 : throw( uno::RuntimeException, std::exception )
251 : {
252 0 : return cppu::supportsService(this, ServiceName);
253 : }
254 :
255 :
256 :
257 0 : uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
258 : throw( uno::RuntimeException, std::exception )
259 : {
260 0 : return getSupportedServiceNames_Static();
261 : }
262 :
263 :
264 :
265 0 : uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
266 : throw( uno::RuntimeException, std::exception )
267 : {
268 0 : uno::Sequence< uno::Type > aTypes( 6 );
269 0 : uno::Type* pTypes = aTypes.getArray();
270 :
271 0 : *pTypes++ = ::getCppuType((const uno::Reference< uno::XAggregation>*)0);
272 0 : *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
273 0 : *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
274 0 : *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
275 0 : *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
276 0 : *pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
277 :
278 0 : return aTypes;
279 : }
280 :
281 0 : uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
282 : throw( uno::RuntimeException, std::exception )
283 : {
284 0 : return css::uno::Sequence<sal_Int8>();
285 : }
286 :
287 :
288 :
289 0 : ::comphelper::PropertySetInfo* GraphicDescriptor::createPropertySetInfo()
290 : {
291 0 : SolarMutexGuard aGuard;
292 0 : ::comphelper::PropertySetInfo* pRet = new ::comphelper::PropertySetInfo();
293 :
294 : static ::comphelper::PropertyMapEntry const aEntries[] =
295 : {
296 0 : { OUString("GraphicType"), UNOGRAPHIC_GRAPHICTYPE, ::getCppuType( (const sal_Int8*)(0)), beans::PropertyAttribute::READONLY, 0 },
297 0 : { OUString("MimeType"), UNOGRAPHIC_MIMETYPE, ::getCppuType( (const OUString*)(0)), beans::PropertyAttribute::READONLY, 0 },
298 0 : { OUString("SizePixel"), UNOGRAPHIC_SIZEPIXEL, ::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
299 0 : { OUString("Size100thMM"), UNOGRAPHIC_SIZE100THMM, ::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
300 0 : { OUString("BitsPerPixel"), UNOGRAPHIC_BITSPERPIXEL, ::getCppuType( (const sal_uInt8*)(0)), beans::PropertyAttribute::READONLY, 0 },
301 0 : { OUString("Transparent"), UNOGRAPHIC_TRANSPARENT, ::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
302 0 : { OUString("Alpha"), UNOGRAPHIC_ALPHA, ::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
303 0 : { OUString("Animated"), UNOGRAPHIC_ANIMATED, ::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
304 : { OUString(), 0, css::uno::Type(), 0, 0 }
305 0 : };
306 :
307 0 : pRet->acquire();
308 0 : pRet->add( aEntries );
309 :
310 0 : return pRet;
311 : }
312 :
313 :
314 :
315 0 : void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
316 : throw( beans::UnknownPropertyException,
317 : beans::PropertyVetoException,
318 : lang::IllegalArgumentException,
319 : lang::WrappedTargetException )
320 : {
321 : // we only have readonly attributes
322 0 : }
323 :
324 :
325 :
326 0 : void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
327 : throw( beans::UnknownPropertyException, lang::WrappedTargetException )
328 : {
329 0 : SolarMutexGuard aGuard;
330 :
331 0 : while( *ppEntries )
332 : {
333 0 : switch( (*ppEntries)->mnHandle )
334 : {
335 : case( UNOGRAPHIC_GRAPHICTYPE ):
336 : {
337 0 : const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
338 :
339 0 : *pValues <<= ( ( eType == GRAPHIC_BITMAP ? graphic::GraphicType::PIXEL :
340 : ( eType == GRAPHIC_GDIMETAFILE ? graphic::GraphicType::VECTOR :
341 0 : graphic::GraphicType::EMPTY ) ) );
342 : }
343 0 : break;
344 :
345 : case( UNOGRAPHIC_MIMETYPE ):
346 : {
347 0 : OUString aMimeType;
348 :
349 0 : if( mpGraphic )
350 : {
351 0 : if( mpGraphic->IsLink() )
352 : {
353 : const char* pMimeType;
354 :
355 0 : switch( const_cast< Graphic* >( mpGraphic )->GetLink().GetType() )
356 : {
357 0 : case( GFX_LINK_TYPE_NATIVE_GIF ): pMimeType = MIMETYPE_GIF; break;
358 :
359 : // #i15508# added BMP type for better exports (checked, works)
360 0 : case( GFX_LINK_TYPE_NATIVE_BMP ): pMimeType = MIMETYPE_BMP; break;
361 :
362 0 : case( GFX_LINK_TYPE_NATIVE_JPG ): pMimeType = MIMETYPE_JPG; break;
363 0 : case( GFX_LINK_TYPE_NATIVE_PNG ): pMimeType = MIMETYPE_PNG; break;
364 0 : case( GFX_LINK_TYPE_NATIVE_WMF ): pMimeType = MIMETYPE_WMF; break;
365 0 : case( GFX_LINK_TYPE_NATIVE_MET ): pMimeType = MIMETYPE_MET; break;
366 0 : case( GFX_LINK_TYPE_NATIVE_PCT ): pMimeType = MIMETYPE_PCT; break;
367 :
368 : // added Svg mimetype support
369 0 : case( GFX_LINK_TYPE_NATIVE_SVG ): pMimeType = MIMETYPE_SVG; break;
370 :
371 : default:
372 0 : pMimeType = NULL;
373 0 : break;
374 : }
375 :
376 0 : if( pMimeType )
377 0 : aMimeType = OUString::createFromAscii( pMimeType );
378 : }
379 :
380 0 : if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GRAPHIC_NONE ) )
381 0 : aMimeType = MIMETYPE_VCLGRAPHIC;
382 : }
383 : else
384 0 : aMimeType = maMimeType;
385 :
386 0 : *pValues <<= aMimeType;
387 : }
388 0 : break;
389 :
390 : case( UNOGRAPHIC_SIZEPIXEL ):
391 : {
392 0 : awt::Size aAWTSize( 0, 0 );
393 :
394 0 : if( mpGraphic )
395 : {
396 0 : if( mpGraphic->GetType() == GRAPHIC_BITMAP )
397 : {
398 0 : const Size aSizePix( mpGraphic->GetBitmapEx().GetSizePixel() );
399 0 : aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
400 : }
401 : }
402 : else
403 0 : aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
404 :
405 0 : *pValues <<= aAWTSize;
406 : }
407 0 : break;
408 :
409 : case( UNOGRAPHIC_SIZE100THMM ):
410 : {
411 0 : awt::Size aAWTSize( 0, 0 );
412 :
413 0 : if( mpGraphic )
414 : {
415 0 : if( mpGraphic->GetPrefMapMode().GetMapUnit() != MAP_PIXEL )
416 : {
417 0 : const Size aSizeLog( OutputDevice::LogicToLogic( mpGraphic->GetPrefSize(), mpGraphic->GetPrefMapMode(), MAP_100TH_MM ) );
418 0 : aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
419 : }
420 : }
421 : else
422 0 : aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
423 :
424 0 : *pValues <<= aAWTSize;
425 : }
426 0 : break;
427 :
428 : case( UNOGRAPHIC_BITSPERPIXEL ):
429 : {
430 0 : sal_uInt16 nBitsPerPixel = 0;
431 :
432 0 : if( mpGraphic )
433 : {
434 0 : if( mpGraphic->GetType() == GRAPHIC_BITMAP )
435 0 : nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount();
436 : }
437 : else
438 0 : nBitsPerPixel = mnBitsPerPixel;
439 :
440 0 : *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
441 : }
442 0 : break;
443 :
444 : case( UNOGRAPHIC_TRANSPARENT ):
445 : {
446 0 : *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsTransparent() : mbTransparent );
447 : }
448 0 : break;
449 :
450 : case( UNOGRAPHIC_ALPHA ):
451 : {
452 0 : *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAlpha() : mbAlpha );
453 : }
454 0 : break;
455 :
456 : case( UNOGRAPHIC_ANIMATED ):
457 : {
458 0 : *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAnimated() : mbAnimated );
459 : }
460 0 : break;
461 : }
462 :
463 0 : ++ppEntries;
464 0 : ++pValues;
465 0 : }
466 0 : }
467 :
468 : }
469 :
470 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|