LibreOffice Module vcl (master)  1
gfxlink.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 
20 #include <sal/log.hxx>
21 #include <tools/stream.hxx>
22 #include <tools/vcompat.hxx>
23 #include <vcl/graph.hxx>
24 #include <vcl/gfxlink.hxx>
25 #include <vcl/graphicfilter.hxx>
26 #include <memory>
27 #include <TypeSerializer.hxx>
28 
29 
32  , mnUserId(0)
33  , mnSwapInDataSize(0)
34  , mbPrefMapModeValid(false)
35  , mbPrefSizeValid(false)
36 {
37 }
38 
39 GfxLink::GfxLink(std::unique_ptr<sal_uInt8[]> pBuf, sal_uInt32 nSize, GfxLinkType nType)
40  : meType(nType)
41  , mnUserId(0)
42  , mpSwapInData(std::shared_ptr<sal_uInt8>(pBuf.release(), pBuf.get_deleter())) // std::move(pBuf) does not compile on Jenkins MacOSX (24 May 2016)
43  , mnSwapInDataSize(nSize)
44  , mbPrefMapModeValid(false)
45  , mbPrefSizeValid(false)
46 {
47  SAL_WARN_IF(mpSwapInData == nullptr || mnSwapInDataSize <= 0, "vcl",
48  "GfxLink::GfxLink(): empty/NULL buffer given");
49 }
50 
51 bool GfxLink::operator==( const GfxLink& rGfxLink ) const
52 {
53  bool bIsEqual = false;
54 
55  if ( ( mnSwapInDataSize == rGfxLink.mnSwapInDataSize ) && ( meType == rGfxLink.meType ) )
56  {
57  const sal_uInt8* pSource = GetData();
58  const sal_uInt8* pDest = rGfxLink.GetData();
59  sal_uInt32 nSourceSize = GetDataSize();
60  sal_uInt32 nDestSize = rGfxLink.GetDataSize();
61  if ( pSource && pDest && ( nSourceSize == nDestSize ) )
62  {
63  bIsEqual = memcmp( pSource, pDest, nSourceSize ) == 0;
64  }
65  else if ( ( pSource == nullptr ) && ( pDest == nullptr ) )
66  bIsEqual = true;
67  }
68  return bIsEqual;
69 }
70 
71 
72 bool GfxLink::IsNative() const
73 {
75 }
76 
77 
79 {
80  return mpSwapInData.get();
81 }
82 
83 
84 void GfxLink::SetPrefSize( const Size& rPrefSize )
85 {
86  maPrefSize = rPrefSize;
87  mbPrefSizeValid = true;
88 }
89 
90 
91 void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
92 {
93  maPrefMapMode = rPrefMapMode;
94  mbPrefMapModeValid = true;
95 }
96 
97 
98 bool GfxLink::LoadNative( Graphic& rGraphic )
99 {
100  bool bRet = false;
101 
102  if( IsNative() && mnSwapInDataSize )
103  {
104  const sal_uInt8* pData = GetData();
105  if (pData)
106  {
107  SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), mnSwapInDataSize, StreamMode::READ | StreamMode::WRITE);
108  OUString aShortName;
109 
110  switch (meType)
111  {
112  case GfxLinkType::NativeGif: aShortName = GIF_SHORTNAME; break;
113  case GfxLinkType::NativeJpg: aShortName = JPG_SHORTNAME; break;
114  case GfxLinkType::NativePng: aShortName = PNG_SHORTNAME; break;
115  case GfxLinkType::NativeTif: aShortName = TIF_SHORTNAME; break;
116  case GfxLinkType::NativeWmf: aShortName = WMF_SHORTNAME; break;
117  case GfxLinkType::NativeMet: aShortName = MET_SHORTNAME; break;
118  case GfxLinkType::NativePct: aShortName = PCT_SHORTNAME; break;
119  case GfxLinkType::NativeSvg: aShortName = SVG_SHORTNAME; break;
120  case GfxLinkType::NativeBmp: aShortName = BMP_SHORTNAME; break;
121  case GfxLinkType::NativePdf: aShortName = PDF_SHORTNAME; break;
122  default: break;
123  }
124  if (!aShortName.isEmpty())
125  {
127  sal_uInt16 nFormat = rFilter.GetImportFormatNumberForShortName(aShortName);
128  ErrCode nResult = rFilter.ImportGraphic(rGraphic, OUString(), aMemoryStream, nFormat);
129  if (nResult == ERRCODE_NONE)
130  bRet = true;
131  }
132  }
133  }
134 
135  return bRet;
136 }
137 
138 bool GfxLink::ExportNative( SvStream& rOStream ) const
139 {
140  if( GetDataSize() )
141  {
142  auto pData = GetSwapInData();
143  if (pData)
144  rOStream.WriteBytes( pData.get(), mnSwapInDataSize );
145  }
146 
147  return ( rOStream.GetError() == ERRCODE_NONE );
148 }
149 
150 SvStream& WriteGfxLink( SvStream& rOStream, const GfxLink& rGfxLink )
151 {
152  std::unique_ptr<VersionCompat> pCompat(new VersionCompat( rOStream, StreamMode::WRITE, 2 ));
153  TypeSerializer aSerializer(rOStream);
154 
155  // Version 1
156  rOStream.WriteUInt16( static_cast<sal_uInt16>(rGfxLink.GetType()) ).WriteUInt32( rGfxLink.GetDataSize() ).WriteUInt32( rGfxLink.GetUserId() );
157 
158  // Version 2
159  aSerializer.writeSize(rGfxLink.GetPrefSize());
160  WriteMapMode( rOStream, rGfxLink.GetPrefMapMode() );
161 
162  pCompat.reset(); // destructor writes stuff into the header
163 
164  if( rGfxLink.GetDataSize() )
165  {
166  auto pData = rGfxLink.GetSwapInData();
167  if (pData)
168  rOStream.WriteBytes( pData.get(), rGfxLink.mnSwapInDataSize );
169  }
170 
171  return rOStream;
172 }
173 
174 SvStream& ReadGfxLink( SvStream& rIStream, GfxLink& rGfxLink)
175 {
176  Size aSize;
177  MapMode aMapMode;
178  bool bMapAndSizeValid( false );
179  std::unique_ptr<VersionCompat> pCompat(new VersionCompat( rIStream, StreamMode::READ ));
180 
181  TypeSerializer aSerializer(rIStream);
182 
183  // Version 1
184  sal_uInt16 nType(0);
185  sal_uInt32 nSize(0), nUserId(0);
186  rIStream.ReadUInt16(nType).ReadUInt32(nSize).ReadUInt32(nUserId);
187 
188  if( pCompat->GetVersion() >= 2 )
189  {
190  aSerializer.readSize(aSize);
191  ReadMapMode( rIStream, aMapMode );
192  bMapAndSizeValid = true;
193  }
194 
195  pCompat.reset(); // destructor writes stuff into the header
196 
197  auto nRemainingData = rIStream.remainingSize();
198  if (nSize > nRemainingData)
199  {
200  SAL_WARN("vcl", "graphic link stream is smaller than requested size");
201  nSize = nRemainingData;
202  }
203 
204  std::unique_ptr<sal_uInt8[]> pBuf(new sal_uInt8[ nSize ]);
205  rIStream.ReadBytes( pBuf.get(), nSize );
206 
207  rGfxLink = GfxLink( std::move(pBuf), nSize, static_cast<GfxLinkType>(nType) );
208  rGfxLink.SetUserId( nUserId );
209 
210  if( bMapAndSizeValid )
211  {
212  rGfxLink.SetPrefSize( aSize );
213  rGfxLink.SetPrefMapMode( aMapMode );
214  }
215 
216  return rIStream;
217 }
218 
219 std::shared_ptr<sal_uInt8> GfxLink::GetSwapInData() const
220 {
221  return mpSwapInData;
222 }
223 
224 bool GfxLink::IsEMF() const
225 {
226  const sal_uInt8* pGraphicAry = GetData();
227  if ((GetType() == GfxLinkType::NativeWmf) && pGraphicAry && (GetDataSize() > 0x2c))
228  {
229  // check the magic number
230  if ((pGraphicAry[0x28] == 0x20) && (pGraphicAry[0x29] == 0x45)
231  && (pGraphicAry[0x2a] == 0x4d) && (pGraphicAry[0x2b] == 0x46))
232  {
233  //emf detected
234  return true;
235  }
236  }
237  return false;
238 }
239 
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ErrCode ImportGraphic(Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat=GRFILTER_FORMAT_DONTKNOW, sal_uInt16 *pDeterminedFormat=nullptr, GraphicFilterImportFlags nImportFlags=GraphicFilterImportFlags::NONE)
SvStream & WriteUInt16(sal_uInt16 nUInt16)
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
PrimitiveType meType
SvStream & WriteMapMode(SvStream &rOStm, const MapMode &rMapMode)
Definition: mapmod.cxx:152
void writeSize(const Size &rSize)
Class to import and export graphic formats.
NONE
#define TIF_SHORTNAME
#define JPG_SHORTNAME
ErrCode GetError() const
#define PNG_SHORTNAME
#define BMP_SHORTNAME
SvStream & WriteUInt32(sal_uInt32 nUInt32)
sal_uInt64 remainingSize()
void readSize(Size &rSize)
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
QPRO_FUNC_TYPE const nType
std::size_t WriteBytes(const void *pData, std::size_t nSize)
sal_uInt16 GetImportFormatNumberForShortName(const OUString &rShortName)
#define PDF_SHORTNAME
std::size_t ReadBytes(void *pData, std::size_t nSize)
#define SAL_WARN_IF(condition, area, stream)
#define ERRCODE_NONE
Definition: errcode.hxx:198
unsigned char sal_uInt8
static GraphicFilter & GetGraphicFilter()
#define MET_SHORTNAME
#define SAL_WARN(area, stream)
SvStream & ReadMapMode(SvStream &rIStm, MapMode &rMapMode)
Definition: mapmod.cxx:136
#define SVG_SHORTNAME
#define GIF_SHORTNAME
#define PCT_SHORTNAME
#define WMF_SHORTNAME