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