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  , mbPrefMapModeValid(false)
34  , mbPrefSizeValid(false)
35 {
36 }
37 
38 GfxLink::GfxLink(std::unique_ptr<sal_uInt8[]> pBuf, sal_uInt32 nSize, GfxLinkType nType)
39  : meType(nType)
40  , mnUserId(0)
41  , maDataContainer(pBuf.get(), nSize)
42  , maHash(0)
43  , mbPrefMapModeValid(false)
44  , mbPrefSizeValid(false)
45 {
46 }
47 
48 GfxLink::GfxLink(BinaryDataContainer const & rDataConainer, GfxLinkType nType)
49  : meType(nType)
50  , mnUserId(0)
51  , maDataContainer(rDataConainer)
52  , maHash(0)
53  , mbPrefMapModeValid(false)
54  , mbPrefSizeValid(false)
55 {
56 }
57 
58 size_t GfxLink::GetHash() const
59 {
60  if (!maHash)
61  {
62  std::size_t seed = maDataContainer.calculateHash();
63  boost::hash_combine(seed, meType);
64  maHash = seed;
65  }
66  return maHash;
67 }
68 
69 bool GfxLink::operator==( const GfxLink& rGfxLink ) const
70 {
71  if (GetDataSize() != rGfxLink.GetDataSize()
72  || meType != rGfxLink.meType
73  || GetHash() != rGfxLink.GetHash())
74  return false;
75 
76  const sal_uInt8* pSource = GetData();
77  const sal_uInt8* pDestination = rGfxLink.GetData();
78 
79  if (pSource == pDestination)
80  return true;
81 
82  sal_uInt32 nSourceSize = GetDataSize();
83  sal_uInt32 nDestSize = rGfxLink.GetDataSize();
84 
85  if (pSource && pDestination && (nSourceSize == nDestSize))
86  return memcmp(pSource, pDestination, nSourceSize) == 0;
87 
88  return false;
89 }
90 
91 bool GfxLink::IsNative() const
92 {
94 }
95 
97 {
98  return maDataContainer.getData();
99 }
100 
101 void GfxLink::SetPrefSize( const Size& rPrefSize )
102 {
103  maPrefSize = rPrefSize;
104  mbPrefSizeValid = true;
105 }
106 
107 void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
108 {
109  maPrefMapMode = rPrefMapMode;
110  mbPrefMapModeValid = true;
111 }
112 
113 bool GfxLink::LoadNative( Graphic& rGraphic )
114 {
115  bool bRet = false;
116 
117  if (IsNative() && !maDataContainer.isEmpty())
118  {
119  const sal_uInt8* pData = GetData();
120  if (pData)
121  {
122  SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), GetDataSize(), StreamMode::READ | StreamMode::WRITE);
123  OUString aShortName;
124 
125  switch (meType)
126  {
127  case GfxLinkType::NativeGif: aShortName = GIF_SHORTNAME; break;
128  case GfxLinkType::NativeJpg: aShortName = JPG_SHORTNAME; break;
129  case GfxLinkType::NativePng: aShortName = PNG_SHORTNAME; break;
130  case GfxLinkType::NativeTif: aShortName = TIF_SHORTNAME; break;
131  case GfxLinkType::NativeWmf: aShortName = WMF_SHORTNAME; break;
132  case GfxLinkType::NativeMet: aShortName = MET_SHORTNAME; break;
133  case GfxLinkType::NativePct: aShortName = PCT_SHORTNAME; break;
134  case GfxLinkType::NativeSvg: aShortName = SVG_SHORTNAME; break;
135  case GfxLinkType::NativeBmp: aShortName = BMP_SHORTNAME; break;
136  case GfxLinkType::NativePdf: aShortName = PDF_SHORTNAME; break;
137  default: break;
138  }
139  if (!aShortName.isEmpty())
140  {
142  sal_uInt16 nFormat = rFilter.GetImportFormatNumberForShortName(aShortName);
143  ErrCode nResult = rFilter.ImportGraphic(rGraphic, OUString(), aMemoryStream, nFormat);
144  if (nResult == ERRCODE_NONE)
145  bRet = true;
146  }
147  }
148  }
149 
150  return bRet;
151 }
152 
153 bool GfxLink::ExportNative( SvStream& rOStream ) const
154 {
155  if( GetDataSize() )
156  {
157  auto pData = GetData();
158  if (pData)
159  rOStream.WriteBytes(pData, GetDataSize());
160  }
161 
162  return ( rOStream.GetError() == ERRCODE_NONE );
163 }
164 
165 bool GfxLink::IsEMF() const
166 {
167  const sal_uInt8* pGraphicAry = GetData();
168  if ((GetType() == GfxLinkType::NativeWmf) && pGraphicAry && (GetDataSize() > 0x2c))
169  {
170  // check the magic number
171  if ((pGraphicAry[0x28] == 0x20) && (pGraphicAry[0x29] == 0x45)
172  && (pGraphicAry[0x2a] == 0x4d) && (pGraphicAry[0x2b] == 0x46))
173  {
174  //emf detected
175  return true;
176  }
177  }
178  return false;
179 }
180 
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
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
Container for the binary data, whose responsibility is to manage the make it as simple as possible to...
#define PNG_SHORTNAME
#define BMP_SHORTNAME
size_t calculateHash() const
std::size_t WriteBytes(const void *pData, std::size_t nSize)
const sal_uInt8 * getData() const
#define PDF_SHORTNAME
#define ERRCODE_NONE
Definition: errcode.hxx:196
unsigned char sal_uInt8
static GraphicFilter & GetGraphicFilter()
#define MET_SHORTNAME
#define SVG_SHORTNAME
#define GIF_SHORTNAME
RedlineType meType
#define PCT_SHORTNAME
sal_uInt16 GetImportFormatNumberForShortName(std::u16string_view rShortName)
#define WMF_SHORTNAME