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