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