LibreOffice Module vcl (master)  1
UnoGraphicDescriptor.cxx
Go to the documentation of this file.
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 
21 
23 #include <vcl/graphicfilter.hxx>
25 
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/awt/Size.hpp>
28 #include <com/sun/star/graphic/GraphicType.hpp>
29 
30 #include <vcl/outdev.hxx>
31 #include <vcl/graph.hxx>
32 #include <vcl/svapp.hxx>
33 #include <memory>
34 
36 {
37  GraphicType = 1
38  , MimeType = 2
39  , SizePixel = 3
40  , Size100thMM = 4
41  , BitsPerPixel = 5
42  , Transparent = 6
43  , Alpha = 7
44  , Animated = 8
45  , Linked = 9
46  , OriginURL = 10
47 };
48 
49 
50 using namespace ::com::sun::star;
51 
52 namespace unographic {
53 
54 
56  ::comphelper::PropertySetHelper( createPropertySetInfo() ),
57  mpGraphic( nullptr ),
59  mnBitsPerPixel ( 0 ),
60  mbTransparent ( false )
61 {
62 }
63 
65  throw()
66 {
67 }
68 
69 void GraphicDescriptor::init( const ::Graphic& rGraphic )
70 {
71  mpGraphic = &rGraphic;
72 }
73 
74 void GraphicDescriptor::init( const OUString& rURL )
75 {
76  std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rURL, StreamMode::READ ));
77 
78  if( pIStm )
79  implCreate( *pIStm, &rURL );
80 }
81 
82 void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL )
83 {
84  std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm ));
85 
86  if( pIStm )
87  implCreate( *pIStm, &rURL );
88 }
89 
90 void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL )
91 {
92  OUString aURL;
93  if( pURL )
94  aURL = *pURL;
95  ::GraphicDescriptor aDescriptor( rIStm, &aURL );
96 
97  mpGraphic = nullptr;
98  maMimeType.clear();
100  mnBitsPerPixel = 0;
101  mbTransparent = false;
102 
103  if( !(aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GraphicFileFormat::NOT) )
104  return;
105 
106  const char* pMimeType = nullptr;
107  sal_uInt8 cType = graphic::GraphicType::EMPTY;
108 
109  switch( aDescriptor.GetFileFormat() )
110  {
111  case GraphicFileFormat::BMP: pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
112  case GraphicFileFormat::GIF: pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
113  case GraphicFileFormat::JPG: pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
114  case GraphicFileFormat::PCD: pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
115  case GraphicFileFormat::PCX: pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
116  case GraphicFileFormat::PNG: pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
117  case GraphicFileFormat::TIF: pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
118  case GraphicFileFormat::XBM: pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
119  case GraphicFileFormat::XPM: pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
120  case GraphicFileFormat::PBM: pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
121  case GraphicFileFormat::PGM: pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
122  case GraphicFileFormat::PPM: pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
123  case GraphicFileFormat::RAS: pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
124  case GraphicFileFormat::TGA: pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
125  case GraphicFileFormat::PSD: pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
126 
127  case GraphicFileFormat::EPS: pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
128  case GraphicFileFormat::DXF: pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
129  case GraphicFileFormat::MET: pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
130  case GraphicFileFormat::PCT: pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
131  case GraphicFileFormat::SVM: pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
132  case GraphicFileFormat::WMF: pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
133  case GraphicFileFormat::EMF: pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
134  case GraphicFileFormat::SVG: pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break;
135 
136  default:
137  break;
138  }
139 
140  if( graphic::GraphicType::EMPTY != cType )
141  {
142  meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GraphicType::Bitmap : GraphicType::GdiMetafile );
143  maMimeType = OUString( pMimeType, strlen(pMimeType), RTL_TEXTENCODING_ASCII_US );
144  maSizePixel = aDescriptor.GetSizePixel();
145  maSize100thMM = aDescriptor.GetSize_100TH_MM();
146  mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
147  mbTransparent = ( graphic::GraphicType::VECTOR == cType );
148  }
149 }
150 
151 
153 {
154  uno::Any aAny;
155 
157  aAny <<= uno::Reference< lang::XServiceInfo >(this);
158  else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
159  aAny <<= uno::Reference< lang::XTypeProvider >(this);
160  else if( rType == cppu::UnoType<beans::XPropertySet>::get())
161  aAny <<= uno::Reference< beans::XPropertySet >(this);
162  else if( rType == cppu::UnoType<beans::XPropertyState>::get())
163  aAny <<= uno::Reference< beans::XPropertyState >(this);
165  aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
166  else
167  aAny = OWeakAggObject::queryAggregation( rType );
168 
169  return aAny;
170 }
171 
172 
174 {
175  return OWeakAggObject::queryInterface( rType );
176 }
177 
178 
180  throw()
181 {
182  OWeakAggObject::acquire();
183 }
184 
185 
187  throw()
188 {
189  OWeakAggObject::release();
190 }
191 
192 
194 {
195  return "com.sun.star.comp.graphic.GraphicDescriptor";
196 }
197 
198 sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName )
199 {
200  return cppu::supportsService(this, ServiceName);
201 }
202 
203 
204 uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
205 {
206  return { "com.sun.star.graphic.GraphicDescriptor" };
207 }
208 
209 
210 uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
211 {
212  static const uno::Sequence< uno::Type > aTypes {
219  return aTypes;
220 }
221 
222 uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
223 {
224  return css::uno::Sequence<sal_Int8>();
225 }
226 
227 
229 {
230  static ::comphelper::PropertyMapEntry const aEntries[] =
231  {
232  { OUString( "GraphicType" ), static_cast< sal_Int32 >( UnoGraphicProperty::GraphicType ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 },
233  { OUString( "MimeType" ), static_cast< sal_Int32 >( UnoGraphicProperty::MimeType ), cppu::UnoType< OUString >::get(), beans::PropertyAttribute::READONLY, 0 },
234  { OUString( "SizePixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::SizePixel ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
235  { OUString( "Size100thMM" ), static_cast< sal_Int32 >( UnoGraphicProperty::Size100thMM ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 },
236  { OUString( "BitsPerPixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::BitsPerPixel ), cppu::UnoType< sal_uInt8 >::get(), beans::PropertyAttribute::READONLY, 0 },
237  { OUString( "Transparent" ), static_cast< sal_Int32 >( UnoGraphicProperty::Transparent ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
238  { OUString( "Alpha" ), static_cast< sal_Int32 >( UnoGraphicProperty::Alpha ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
239  { OUString( "Animated" ), static_cast< sal_Int32 >( UnoGraphicProperty::Animated ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 },
240  { OUString("Linked"), sal_Int32(UnoGraphicProperty::Linked), cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY, 0 },
241  { OUString("OriginURL"), sal_Int32(UnoGraphicProperty::OriginURL), cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
242 
243  { OUString(), 0, css::uno::Type(), 0, 0 }
244  };
245 
246  return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) );
247 }
248 
249 
250 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
251 {
252  // we only have readonly attributes
253 }
254 
255 
257 {
258  SolarMutexGuard aGuard;
259 
260  while( *ppEntries )
261  {
262  UnoGraphicProperty theProperty = static_cast< UnoGraphicProperty >( (*ppEntries)->mnHandle );
263  switch( theProperty )
264  {
266  {
267  const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
268 
269  *pValues <<= ( eType == GraphicType::Bitmap ? graphic::GraphicType::PIXEL :
270  ( eType == GraphicType::GdiMetafile ? graphic::GraphicType::VECTOR :
271  graphic::GraphicType::EMPTY ) );
272  }
273  break;
274 
276  {
277  OUString aMimeType;
278 
279  if( mpGraphic )
280  {
281  if( mpGraphic->IsGfxLink() )
282  {
283  const char* pMimeType;
284 
285  switch( mpGraphic->GetGfxLink().GetType() )
286  {
287  case GfxLinkType::NativeGif: pMimeType = MIMETYPE_GIF; break;
288 
289  // #i15508# added BMP type for better exports (checked, works)
290  case GfxLinkType::NativeBmp: pMimeType = MIMETYPE_BMP; break;
291 
292  case GfxLinkType::NativeJpg: pMimeType = MIMETYPE_JPG; break;
293  case GfxLinkType::NativePng: pMimeType = MIMETYPE_PNG; break;
294  case GfxLinkType::NativeWmf: pMimeType = MIMETYPE_WMF; break;
295  case GfxLinkType::NativeMet: pMimeType = MIMETYPE_MET; break;
296  case GfxLinkType::NativePct: pMimeType = MIMETYPE_PCT; break;
297 
298  // added Svg mimetype support
299  case GfxLinkType::NativeSvg: pMimeType = MIMETYPE_SVG; break;
300  case GfxLinkType::NativePdf: pMimeType = MIMETYPE_PDF; break;
301 
302  default:
303  pMimeType = nullptr;
304  break;
305  }
306 
307  if( pMimeType )
308  aMimeType = OUString::createFromAscii( pMimeType );
309  }
310 
311  if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GraphicType::NONE ) )
312  aMimeType = MIMETYPE_VCLGRAPHIC;
313  }
314  else
315  aMimeType = maMimeType;
316 
317  *pValues <<= aMimeType;
318  }
319  break;
320 
322  {
323  awt::Size aAWTSize( 0, 0 );
324 
325  if( mpGraphic )
326  {
327  if( mpGraphic->GetType() == GraphicType::Bitmap )
328  {
329  const Size aSizePix( mpGraphic->GetSizePixel() );
330  aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
331  }
332  }
333  else
334  aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
335 
336  *pValues <<= aAWTSize;
337  }
338  break;
339 
341  {
342  awt::Size aAWTSize( 0, 0 );
343 
344  if( mpGraphic )
345  {
346  if( mpGraphic->GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel )
347  {
348  const Size aSizeLog( OutputDevice::LogicToLogic(
349  mpGraphic->GetPrefSize(),
350  mpGraphic->GetPrefMapMode(),
351  MapMode(MapUnit::Map100thMM)) );
352  aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
353  }
354  }
355  else
356  aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
357 
358  *pValues <<= aAWTSize;
359  }
360  break;
361 
363  {
364  sal_uInt16 nBitsPerPixel = 0;
365 
366  if( mpGraphic )
367  {
368  if( mpGraphic->GetType() == GraphicType::Bitmap )
369  nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount();
370  }
371  else
372  nBitsPerPixel = mnBitsPerPixel;
373 
374  *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
375  }
376  break;
377 
379  {
380  *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent;
381  }
382  break;
383 
385  {
386  *pValues <<= mpGraphic && mpGraphic->IsAlpha();
387  }
388  break;
389 
391  {
392  *pValues <<= mpGraphic && mpGraphic->IsAnimated();
393  }
394  break;
395 
397  {
398  *pValues <<= mpGraphic && !mpGraphic->getOriginURL().isEmpty();
399  }
400  break;
401 
403  {
404  OUString aOriginURL;
405  if (mpGraphic)
406  aOriginURL = mpGraphic->getOriginURL();
407 
408  *pValues <<= aOriginURL;
409  }
410  break;
411  }
412 
413  ++ppEntries;
414  ++pValues;
415  }
416 }
417 
418 }
419 
420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define MIMETYPE_BMP
long Width() const
#define MIMETYPE_XPM
#define MIMETYPE_PCT
#define MIMETYPE_EMF
virtual void SAL_CALL release() override
#define MIMETYPE_TGA
#define MIMETYPE_PDF
PrimitiveType meType
Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
Definition: map.cxx:1674
long Height() const
virtual OUString SAL_CALL getImplementationName() override
GraphicType
Definition: graph.hxx:36
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &rType) override
#define MIMETYPE_PGM
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
#define MIMETYPE_XBM
#define MIMETYPE_PNG
UnoGraphicProperty
#define MIMETYPE_PPM
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
#define MIMETYPE_WMF
#define MIMETYPE_SVG
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
NONE
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
#define MIMETYPE_EPS
virtual void _setPropertyValues(const comphelper::PropertyMapEntry **ppEntries, const css::uno::Any *pValues) override
virtual void SAL_CALL acquire() override
const char *const aMimeType[]
#define MIMETYPE_PCX
unsigned char sal_Bool
css::uno::Type const & get()
#define MIMETYPE_GIF
DocumentType const eType
#define MIMETYPE_TIF
#define MIMETYPE_SVM
#define MIMETYPE_DXF
#define MIMETYPE_RAS
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
#define MIMETYPE_PBM
void implCreate(SvStream &rIStm, const OUString *pPath)
virtual void _getPropertyValues(const comphelper::PropertyMapEntry **ppEntries, css::uno::Any *pValue) override
unsigned char sal_uInt8
void init(const ::Graphic &rGraphic)
#define MIMETYPE_PSD
#define MIMETYPE_JPG
#define MIMETYPE_VCLGRAPHIC
ScXMLEditAttributeMap::Entry const aEntries[]
#define MIMETYPE_MET
#define MIMETYPE_PCD
static rtl::Reference<::comphelper::PropertySetInfo > createPropertySetInfo()
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override