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