xref: /trunk/main/svtools/source/graphic/descriptor.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
211     uno::Any aAny;
212 
213     if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
214         aAny <<= uno::Reference< lang::XServiceInfo >(this);
215     else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
216         aAny <<= uno::Reference< lang::XTypeProvider >(this);
217     else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
218         aAny <<= uno::Reference< beans::XPropertySet >(this);
219     else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
220         aAny <<= uno::Reference< beans::XPropertyState >(this);
221     else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
222         aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
223     else
224         aAny <<= OWeakAggObject::queryAggregation( rType );
225 
226     return aAny;
227 }
228 
229 // ------------------------------------------------------------------------------
230 
queryInterface(const uno::Type & rType)231 uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
232 {
233     return OWeakAggObject::queryInterface( rType );
234 }
235 
236 // ------------------------------------------------------------------------------
237 
acquire()238 void SAL_CALL GraphicDescriptor::acquire()
239     throw()
240 {
241     OWeakAggObject::acquire();
242 }
243 
244 // ------------------------------------------------------------------------------
245 
release()246 void SAL_CALL GraphicDescriptor::release()
247     throw()
248 {
249     OWeakAggObject::release();
250 }
251 
252 // ------------------------------------------------------------------------------
253 
getImplementationName()254 ::rtl::OUString SAL_CALL GraphicDescriptor::getImplementationName()
255 {
256     return getImplementationName_Static();
257 }
258 
259 // ------------------------------------------------------------------------------
260 
supportsService(const rtl::OUString & ServiceName)261 sal_Bool SAL_CALL GraphicDescriptor::supportsService( const rtl::OUString& ServiceName )
262 {
263     uno::Sequence< ::rtl::OUString >    aSNL( getSupportedServiceNames() );
264     const ::rtl::OUString*              pArray = aSNL.getConstArray();
265 
266     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
267         if( pArray[i] == ServiceName )
268             return true;
269 
270     return false;
271 }
272 
273 // ------------------------------------------------------------------------------
274 
getSupportedServiceNames()275 uno::Sequence< rtl::OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
276 {
277     return getSupportedServiceNames_Static();
278 }
279 
280 // ------------------------------------------------------------------------------
281 
getTypes()282 uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
283 {
284     uno::Sequence< uno::Type >  aTypes( 6 );
285     uno::Type*                  pTypes = aTypes.getArray();
286 
287     *pTypes++ = ::getCppuType((const uno::Reference< uno::XAggregation>*)0);
288     *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
289     *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
290     *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
291     *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
292     *pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
293 
294     return aTypes;
295 }
296 
297 // ------------------------------------------------------------------------------
298 
getImplementationId()299 uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
300 {
301     vos::OGuard                         aGuard( Application::GetSolarMutex() );
302     static uno::Sequence< sal_Int8 >    aId;
303 
304     if( aId.getLength() == 0 )
305     {
306         aId.realloc( 16 );
307         rtl_createUuid( reinterpret_cast< sal_uInt8* >( aId.getArray() ), 0, sal_True );
308     }
309 
310     return aId;
311 }
312 
313 // ------------------------------------------------------------------------------
314 
createPropertySetInfo()315 ::comphelper::PropertySetInfo* GraphicDescriptor::createPropertySetInfo()
316 {
317     vos::OGuard                     aGuard( Application::GetSolarMutex() );
318     ::comphelper::PropertySetInfo*  pRet = new ::comphelper::PropertySetInfo();
319 
320     static ::comphelper::PropertyMapEntry aEntries[] =
321     {
322         { MAP_CHAR_LEN( "GraphicType" ), UNOGRAPHIC_GRAPHICTYPE, &::getCppuType( (const sal_Int8*)(0)), beans::PropertyAttribute::READONLY, 0 },
323         { MAP_CHAR_LEN( "MimeType" ), UNOGRAPHIC_MIMETYPE, &::getCppuType( (const ::rtl::OUString*)(0)), beans::PropertyAttribute::READONLY, 0 },
324         { MAP_CHAR_LEN( "SizePixel" ), UNOGRAPHIC_SIZEPIXEL, &::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
325         { MAP_CHAR_LEN( "Size100thMM" ), UNOGRAPHIC_SIZE100THMM,    &::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
326         { MAP_CHAR_LEN( "BitsPerPixel" ), UNOGRAPHIC_BITSPERPIXEL, &::getCppuType( (const sal_uInt8*)(0)), beans::PropertyAttribute::READONLY, 0 },
327         { MAP_CHAR_LEN( "Transparent" ), UNOGRAPHIC_TRANSPARENT, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
328         { MAP_CHAR_LEN( "Alpha" ), UNOGRAPHIC_ALPHA, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
329         { MAP_CHAR_LEN( "Animated" ), UNOGRAPHIC_ANIMATED, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
330 
331         { 0,0,0,0,0,0 }
332     };
333 
334     pRet->acquire();
335     pRet->add( aEntries );
336 
337     return pRet;
338 }
339 
340 // ------------------------------------------------------------------------------
341 
_setPropertyValues(const comphelper::PropertyMapEntry **,const uno::Any *)342 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
343 {
344     // we only have readonly attributes
345 }
346 
347 // ------------------------------------------------------------------------------
348 
_getPropertyValues(const comphelper::PropertyMapEntry ** ppEntries,uno::Any * pValues)349 void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
350 {
351     ::vos::OGuard aGuard( Application::GetSolarMutex() );
352 
353     while( *ppEntries )
354     {
355         switch( (*ppEntries)->mnHandle )
356         {
357             case( UNOGRAPHIC_GRAPHICTYPE ):
358             {
359                 const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
360 
361                 *pValues <<= ( ( eType == GRAPHIC_BITMAP ? graphic::GraphicType::PIXEL :
362                                 ( eType == GRAPHIC_GDIMETAFILE ? graphic::GraphicType::VECTOR :
363                                 graphic::GraphicType::EMPTY ) ) );
364             }
365             break;
366 
367             case( UNOGRAPHIC_MIMETYPE ):
368             {
369                 ::rtl::OUString aMimeType;
370 
371                 if( mpGraphic )
372                 {
373                     if( mpGraphic->IsLink() )
374                     {
375                         const char* pMimeType;
376 
377                         switch( const_cast< Graphic* >( mpGraphic )->GetLink().GetType() )
378                         {
379                             case( GFX_LINK_TYPE_NATIVE_GIF ): pMimeType = MIMETYPE_GIF; break;
380 
381                             // #15508# added BMP type for better exports (checked, works)
382                             case( GFX_LINK_TYPE_NATIVE_BMP ): pMimeType = MIMETYPE_BMP; break;
383 
384                             case( GFX_LINK_TYPE_NATIVE_JPG ): pMimeType = MIMETYPE_JPG; break;
385                             case( GFX_LINK_TYPE_NATIVE_PNG ): pMimeType = MIMETYPE_PNG; break;
386                             case( GFX_LINK_TYPE_NATIVE_WMF ): pMimeType = MIMETYPE_WMF; break;
387                             case( GFX_LINK_TYPE_NATIVE_MET ): pMimeType = MIMETYPE_MET; break;
388                             case( GFX_LINK_TYPE_NATIVE_PCT ): pMimeType = MIMETYPE_PCT; break;
389 
390                             // added Svg mimetype support
391                             case( GFX_LINK_TYPE_NATIVE_SVG ): pMimeType = MIMETYPE_SVG; break;
392 
393                             default:
394                                 pMimeType = NULL;
395                             break;
396                         }
397 
398                         if( pMimeType )
399                             aMimeType = ::rtl::OUString::createFromAscii( pMimeType );
400                     }
401 
402                     if( !aMimeType.getLength() && ( mpGraphic->GetType() != GRAPHIC_NONE ) )
403                         aMimeType = ::rtl::OUString::createFromAscii( MIMETYPE_VCLGRAPHIC );
404                 }
405                 else
406                     aMimeType = maMimeType;
407 
408                 *pValues <<= aMimeType;
409             }
410             break;
411 
412             case( UNOGRAPHIC_SIZEPIXEL ):
413             {
414                 awt::Size aAWTSize( 0, 0 );
415 
416                 if( mpGraphic )
417                 {
418                     if( mpGraphic->GetType() == GRAPHIC_BITMAP )
419                     {
420                         const Size aSizePix( mpGraphic->GetBitmapEx().GetSizePixel() );
421                         aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
422                     }
423                 }
424                 else
425                     aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
426 
427                 *pValues <<= aAWTSize;
428             }
429             break;
430 
431             case( UNOGRAPHIC_SIZE100THMM ):
432             {
433                 awt::Size aAWTSize( 0, 0 );
434 
435                 if( mpGraphic )
436                 {
437                     if( mpGraphic->GetPrefMapMode().GetMapUnit() != MAP_PIXEL )
438                     {
439                         const Size aSizeLog( OutputDevice::LogicToLogic( mpGraphic->GetPrefSize(), mpGraphic->GetPrefMapMode(), MAP_100TH_MM ) );
440                         aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
441                     }
442                 }
443                 else
444                     aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
445 
446                 *pValues <<= aAWTSize;
447             }
448             break;
449 
450             case( UNOGRAPHIC_BITSPERPIXEL ):
451             {
452                 sal_uInt16 nBitsPerPixel = 0;
453 
454                 if( mpGraphic )
455                 {
456                     if( mpGraphic->GetType() == GRAPHIC_BITMAP )
457                         nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount();
458                 }
459                 else
460                     nBitsPerPixel = mnBitsPerPixel;
461 
462                 *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
463             }
464             break;
465 
466             case( UNOGRAPHIC_TRANSPARENT ):
467             {
468                 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsTransparent() : mbTransparent );
469             }
470             break;
471 
472             case( UNOGRAPHIC_ALPHA ):
473             {
474                 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAlpha() : mbAlpha );
475             }
476             break;
477 
478             case( UNOGRAPHIC_ANIMATED ):
479             {
480                 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAnimated() : mbAnimated );
481             }
482             break;
483         }
484 
485         ++ppEntries;
486         ++pValues;
487     }
488 }
489 
490 }
491