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 <boost/functional/hash.hpp>
28 
31  , mnUserId(0)
32  , maHash(0)
33  , mnSwapInDataSize(0)
34  , mbPrefMapModeValid(false)
35  , mbPrefSizeValid(false)
36 {
37 }
38 
39 
40 
41 GfxLink::GfxLink(std::unique_ptr<sal_uInt8[]> pBuf, sal_uInt32 nSize, GfxLinkType nType)
42  : meType(nType)
43  , mnUserId(0)
44  , mpSwapInData(std::shared_ptr<sal_uInt8>(pBuf.release(), pBuf.get_deleter())) // std::move(pBuf) does not compile on Jenkins MacOSX (24 May 2016)
45  , maHash(0)
46  , mnSwapInDataSize(nSize)
47  , mbPrefMapModeValid(false)
48  , mbPrefSizeValid(false)
49 {
50  SAL_WARN_IF(mpSwapInData == nullptr || mnSwapInDataSize <= 0, "vcl",
51  "GfxLink::GfxLink(): empty/NULL buffer given");
52 }
53 
54 size_t GfxLink::GetHash() const
55 {
56  if (!maHash)
57  {
58  std::size_t seed = 0;
59  boost::hash_combine(seed, mnSwapInDataSize);
60  boost::hash_combine(seed, meType);
61  const sal_uInt8* pData = GetData();
62  if (pData)
63  seed += boost::hash_range(pData, pData + GetDataSize());
64  maHash = seed;
65 
66  }
67  return maHash;
68 }
69 
70 bool GfxLink::operator==( const GfxLink& rGfxLink ) const
71 {
72  if (GetHash() != rGfxLink.GetHash())
73  return false;
74 
75  if ( mnSwapInDataSize != rGfxLink.mnSwapInDataSize ||
76  meType != rGfxLink.meType )
77  return false;
78 
79  const sal_uInt8* pSource = GetData();
80  const sal_uInt8* pDest = rGfxLink.GetData();
81  if ( pSource == pDest )
82  return true;
83  sal_uInt32 nSourceSize = GetDataSize();
84  sal_uInt32 nDestSize = rGfxLink.GetDataSize();
85  if ( pSource && pDest && ( nSourceSize == nDestSize ) )
86  return (memcmp( pSource, pDest, nSourceSize ) == 0);
87  return false;
88 }
89 
90 bool GfxLink::IsNative() const
91 {
93 }
94 
95 
97 {
98  return mpSwapInData.get();
99 }
100 
101 
102 void GfxLink::SetPrefSize( const Size& rPrefSize )
103 {
104  maPrefSize = rPrefSize;
105  mbPrefSizeValid = true;
106 }
107 
108 
109 void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
110 {
111  maPrefMapMode = rPrefMapMode;
112  mbPrefMapModeValid = true;
113 }
114 
115 
116 bool GfxLink::LoadNative( Graphic& rGraphic )
117 {
118  bool bRet = false;
119 
120  if( IsNative() && mnSwapInDataSize )
121  {
122  const sal_uInt8* pData = GetData();
123  if (pData)
124  {
125  SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), mnSwapInDataSize, StreamMode::READ | StreamMode::WRITE);
126  OUString aShortName;
127 
128  switch (meType)
129  {
130  case GfxLinkType::NativeGif: aShortName = GIF_SHORTNAME; break;
131  case GfxLinkType::NativeJpg: aShortName = JPG_SHORTNAME; break;
132  case GfxLinkType::NativePng: aShortName = PNG_SHORTNAME; break;
133  case GfxLinkType::NativeTif: aShortName = TIF_SHORTNAME; break;
134  case GfxLinkType::NativeWmf: aShortName = WMF_SHORTNAME; break;
135  case GfxLinkType::NativeMet: aShortName = MET_SHORTNAME; break;
136  case GfxLinkType::NativePct: aShortName = PCT_SHORTNAME; break;
137  case GfxLinkType::NativeSvg: aShortName = SVG_SHORTNAME; break;
138  case GfxLinkType::NativeBmp: aShortName = BMP_SHORTNAME; break;
139  case GfxLinkType::NativePdf: aShortName = PDF_SHORTNAME; break;
140  default: break;
141  }
142  if (!aShortName.isEmpty())
143  {
145  sal_uInt16 nFormat = rFilter.GetImportFormatNumberForShortName(aShortName);
146  ErrCode nResult = rFilter.ImportGraphic(rGraphic, OUString(), aMemoryStream, nFormat);
147  if (nResult == ERRCODE_NONE)
148  bRet = true;
149  }
150  }
151  }
152 
153  return bRet;
154 }
155 
156 bool GfxLink::ExportNative( SvStream& rOStream ) const
157 {
158  if( GetDataSize() )
159  {
160  auto pData = GetSwapInData();
161  if (pData)
162  rOStream.WriteBytes( pData.get(), mnSwapInDataSize );
163  }
164 
165  return ( rOStream.GetError() == ERRCODE_NONE );
166 }
167 
168 std::shared_ptr<sal_uInt8> GfxLink::GetSwapInData() const
169 {
170  return mpSwapInData;
171 }
172 
173 bool GfxLink::IsEMF() const
174 {
175  const sal_uInt8* pGraphicAry = GetData();
176  if ((GetType() == GfxLinkType::NativeWmf) && pGraphicAry && (GetDataSize() > 0x2c))
177  {
178  // check the magic number
179  if ((pGraphicAry[0x28] == 0x20) && (pGraphicAry[0x29] == 0x45)
180  && (pGraphicAry[0x2a] == 0x4d) && (pGraphicAry[0x2b] == 0x46))
181  {
182  //emf detected
183  return true;
184  }
185  }
186  return false;
187 }
188 
189 /* 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)
std::unique_ptr< ContentProperties > pData
Class to import and export graphic formats.
NONE
#define TIF_SHORTNAME
#define JPG_SHORTNAME
ErrCode GetError() const
#define PNG_SHORTNAME
#define BMP_SHORTNAME
std::size_t WriteBytes(const void *pData, std::size_t nSize)
sal_uInt16 GetImportFormatNumberForShortName(const OUString &rShortName)
#define PDF_SHORTNAME
#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 SVG_SHORTNAME
#define GIF_SHORTNAME
RedlineType meType
#define PCT_SHORTNAME
#define WMF_SHORTNAME