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