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 <o3tl/hash_combine.hxx>
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();
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 ) const
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  case GfxLinkType::NativeWebp: aShortName = WEBP_SHORTNAME; break;
138  default: break;
139  }
140  if (!aShortName.isEmpty())
141  {
143  sal_uInt16 nFormat = rFilter.GetImportFormatNumberForShortName(aShortName);
144  ErrCode nResult = rFilter.ImportGraphic(rGraphic, u"", aMemoryStream, nFormat);
145  if (nResult == ERRCODE_NONE)
146  bRet = true;
147  }
148  }
149  }
150 
151  return bRet;
152 }
153 
154 bool GfxLink::ExportNative( SvStream& rOStream ) const
155 {
156  if( GetDataSize() )
157  {
158  auto pData = GetData();
159  if (pData)
160  rOStream.WriteBytes(pData, GetDataSize());
161  }
162 
163  return ( rOStream.GetError() == ERRCODE_NONE );
164 }
165 
166 bool GfxLink::IsEMF() const
167 {
168  const sal_uInt8* pGraphicAry = GetData();
169  if ((GetType() == GfxLinkType::NativeWmf) && pGraphicAry && (GetDataSize() > 0x2c))
170  {
171  // check the magic number
172  if ((pGraphicAry[0x28] == 0x20) && (pGraphicAry[0x29] == 0x45)
173  && (pGraphicAry[0x2a] == 0x4d) && (pGraphicAry[0x2b] == 0x46))
174  {
175  //emf detected
176  return true;
177  }
178  }
179  return false;
180 }
181 
182 /* 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)
constexpr OUStringLiteral MET_SHORTNAME
constexpr OUStringLiteral PCT_SHORTNAME
constexpr OUStringLiteral SVG_SHORTNAME
std::unique_ptr< sal_Int32[]> pData
Class to import and export graphic formats.
NONE
ErrCode GetError() const
Container for the binary data, whose responsibility is to manage the make it as simple as possible to...
constexpr OUStringLiteral WEBP_SHORTNAME
size_t calculateHash() const
std::size_t WriteBytes(const void *pData, std::size_t nSize)
float u
const sal_uInt8 * getData() const
std::enable_if_t<(sizeof(N)==4)> hash_combine(N &nSeed, T const *pValue, size_t nCount)
constexpr OUStringLiteral PDF_SHORTNAME
#define ERRCODE_NONE
Definition: errcode.hxx:196
unsigned char sal_uInt8
constexpr OUStringLiteral JPG_SHORTNAME
constexpr OUStringLiteral GIF_SHORTNAME
static GraphicFilter & GetGraphicFilter()
constexpr OUStringLiteral PNG_SHORTNAME
RedlineType meType
constexpr OUStringLiteral TIF_SHORTNAME
constexpr OUStringLiteral BMP_SHORTNAME
sal_uInt16 GetImportFormatNumberForShortName(std::u16string_view rShortName)
bool m_bDetectedRangeSegmentation false
constexpr OUStringLiteral WMF_SHORTNAME