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
40 : meType(nType)
41 , mnUserId(0)
42 , maDataContainer(std::move(aDataConainer))
43 , maHash(0)
44 , mbPrefMapModeValid(false)
45 , mbPrefSizeValid(false)
46{
47}
48
49size_t GfxLink::GetHash() const
50{
51 if (!maHash)
52 {
53 std::size_t seed = maDataContainer.calculateHash();
55 maHash = seed;
56 }
57 return maHash;
58}
59
60bool GfxLink::operator==( const GfxLink& rGfxLink ) const
61{
62 if (GetDataSize() != rGfxLink.GetDataSize()
63 || meType != rGfxLink.meType
64 || GetHash() != rGfxLink.GetHash())
65 return false;
66
67 const sal_uInt8* pSource = GetData();
68 const sal_uInt8* pDestination = rGfxLink.GetData();
69
70 if (pSource == pDestination)
71 return true;
72
73 sal_uInt32 nSourceSize = GetDataSize();
74 sal_uInt32 nDestSize = rGfxLink.GetDataSize();
75
76 if (pSource && pDestination && (nSourceSize == nDestSize))
77 return memcmp(pSource, pDestination, nSourceSize) == 0;
78
79 return false;
80}
81
83{
85}
86
88{
89 return maDataContainer.getData();
90}
91
92void GfxLink::SetPrefSize( const Size& rPrefSize )
93{
94 maPrefSize = rPrefSize;
95 mbPrefSizeValid = true;
96}
97
98void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode )
99{
100 maPrefMapMode = rPrefMapMode;
101 mbPrefMapModeValid = true;
102}
103
104bool GfxLink::LoadNative( Graphic& rGraphic ) const
105{
106 bool bRet = false;
107
108 if (IsNative() && !maDataContainer.isEmpty())
109 {
110 const sal_uInt8* pData = GetData();
111 if (pData)
112 {
113 SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), GetDataSize(), StreamMode::READ | StreamMode::WRITE);
114 OUString aShortName;
115
116 switch (meType)
117 {
118 case GfxLinkType::NativeGif: aShortName = GIF_SHORTNAME; break;
119 case GfxLinkType::NativeJpg: aShortName = JPG_SHORTNAME; break;
120 case GfxLinkType::NativePng: aShortName = PNG_SHORTNAME; break;
121 case GfxLinkType::NativeTif: aShortName = TIF_SHORTNAME; break;
122 case GfxLinkType::NativeWmf: aShortName = WMF_SHORTNAME; break;
123 case GfxLinkType::NativeMet: aShortName = MET_SHORTNAME; break;
124 case GfxLinkType::NativePct: aShortName = PCT_SHORTNAME; break;
125 case GfxLinkType::NativeSvg: aShortName = SVG_SHORTNAME; break;
126 case GfxLinkType::NativeBmp: aShortName = BMP_SHORTNAME; break;
127 case GfxLinkType::NativePdf: aShortName = PDF_SHORTNAME; break;
128 case GfxLinkType::NativeWebp: aShortName = WEBP_SHORTNAME; break;
129 default: break;
130 }
131 if (!aShortName.isEmpty())
132 {
134 sal_uInt16 nFormat = rFilter.GetImportFormatNumberForShortName(aShortName);
135 ErrCode nResult = rFilter.ImportGraphic(rGraphic, u"", aMemoryStream, nFormat);
136 if (nResult == ERRCODE_NONE)
137 bRet = true;
138 }
139 }
140 }
141
142 return bRet;
143}
144
145bool GfxLink::ExportNative( SvStream& rOStream ) const
146{
147 if( GetDataSize() )
148 {
149 auto pData = GetData();
150 if (pData)
151 rOStream.WriteBytes(pData, GetDataSize());
152 }
153
154 return ( rOStream.GetError() == ERRCODE_NONE );
155}
156
157bool GfxLink::IsEMF() const
158{
159 const sal_uInt8* pGraphicAry = GetData();
160 if ((GetType() == GfxLinkType::NativeWmf) && pGraphicAry && (GetDataSize() > 0x2c))
161 {
162 // check the magic number
163 if ((pGraphicAry[0x28] == 0x20) && (pGraphicAry[0x29] == 0x45)
164 && (pGraphicAry[0x2a] == 0x4d) && (pGraphicAry[0x2b] == 0x46))
165 {
166 //emf detected
167 return true;
168 }
169 }
170 return false;
171}
172
173/* 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)
QPRO_FUNC_TYPE nType
unsigned char sal_uInt8
RedlineType meType