Debian Codegraph @

podofo/test/unit/EncodingTest.cpp

raw symbols SCIP
1 /**
2 * Copyright (C) 2008 by Dominik Seichter <[email protected]>
3 * Copyright (C) 2021 by Francesco Pretto <[email protected]>
4 *
5 * Licensed under GNU Library General Public 2.0 or later.
6 * Some rights reserved. See COPYING, AUTHORS.
7 */
8
9 #include <PdfTest.h>
10
11 #include <ostream>
12 #include <iostream>
13
14 using namespace std;
15 using namespace PoDoFo;
16
17 static void outofRangeHelper(PdfEncoding& encoding);
18
19 inline ostream& operator<<(ostream& o, const PdfVariant& s)
20 {
21 string str;
22 s.ToString(str);
23 return o << str;
24 }
25
26 TEST_CASE("testDifferences")
27 {
28 PdfDifferenceList difference;
29
30 // Newly created encoding should be empty
31 REQUIRE(difference.GetCount() == 0);
32
33 // Adding 0 should work
34 difference.AddDifference(0, "A");
35 REQUIRE(difference.GetCount() == 1);
36
37 // Adding 255 should work
38 difference.AddDifference(255, "B");
39 REQUIRE(difference.GetCount() == 2);
40
41 // Convert to array
42 PdfArray data;
43 PdfArray expected;
44 expected.Add(static_cast<int64_t>(0));
45 expected.Add(PdfName("A"));
46 expected.Add(static_cast<int64_t>(255));
47 expected.Add(PdfName("B"));
48
49 difference.ToArray(data);
50
51 REQUIRE(expected.GetSize() == data.GetSize());
52 for (unsigned int i = 0; i < data.GetSize(); i++)
53 REQUIRE(expected[i] == data[i]);
54
55
56 // Test replace
57 expected.Clear();
58 expected.Add(static_cast<int64_t>(0));
59 expected.Add(PdfName("A"));
60 expected.Add(static_cast<int64_t>(255));
61 expected.Add(PdfName("X"));
62
63 difference.AddDifference(255, "X");
64
65 difference.ToArray(data);
66
67 REQUIRE(expected.GetSize() == data.GetSize());
68 for (unsigned int i = 0; i < data.GetSize(); i++)
69 REQUIRE(expected[i] == data[i]);
70
71
72 // Test more complicated array
73 expected.Clear();
74 expected.Add(static_cast<int64_t>(0));
75 expected.Add(PdfName("A"));
76 expected.Add(PdfName("B"));
77 expected.Add(PdfName("C"));
78 expected.Add(static_cast<int64_t>(4));
79 expected.Add(PdfName("D"));
80 expected.Add(PdfName("E"));
81 expected.Add(static_cast<int64_t>(9));
82 expected.Add(PdfName("F"));
83 expected.Add(static_cast<int64_t>(255));
84 expected.Add(PdfName("X"));
85
86 difference.AddDifference(1, "B");
87 difference.AddDifference(2, "C");
88 difference.AddDifference(4, "D");
89 difference.AddDifference(5, "E");
90 difference.AddDifference(9, "F");
91
92 difference.ToArray(data);
93
94 REQUIRE(expected.GetSize() == data.GetSize());
95 for (unsigned int i = 0; i < data.GetSize(); i++)
96 REQUIRE(expected[i] == data[i]);
97
98 // Test if contains works correctly
99 const PdfName* name;
100 char32_t value;
101 REQUIRE(difference.TryGetMappedName(0, name, value));
102 REQUIRE(*name == "A");
103 REQUIRE(static_cast<int>(value) == 0x41);
104
105 REQUIRE(difference.TryGetMappedName(9, name, value));
106 REQUIRE(*name == "F");
107 REQUIRE(static_cast<int>(value) == 0x46);
108
109 REQUIRE(difference.TryGetMappedName(255, name, value));
110 REQUIRE(*name == "X");
111 REQUIRE(static_cast<int>(value) == 0x58);
112
113 REQUIRE(!difference.TryGetMappedName(100, name, value));
114 }
115
116 TEST_CASE("testDifferencesObject")
117 {
118 PdfDifferenceList difference;
119 difference.AddDifference(1, "B");
120 difference.AddDifference(2, "C");
121 difference.AddDifference(4, "D");
122 difference.AddDifference(5, "E");
123 difference.AddDifference(9, "F");
124
125 PdfDifferenceEncoding encoding(difference, PdfEncodingMapFactory::MacRomanEncodingInstance());
126
127 // Check for encoding key
128 PdfMemDocument doc;
129 PdfName name;
130 PdfObject* encodingObj;
131 REQUIRE(encoding.TryGetExportObject(doc.GetObjects(), name, encodingObj));
132 REQUIRE(encodingObj != nullptr);
133
134 // Test BaseEncoding
135 PdfObject* baseObj = encodingObj->GetDictionary().GetKey("BaseEncoding");
136 REQUIRE(baseObj->GetName() == "MacRomanEncoding");
137
138 // Test differences
139 PdfObject* diff = encodingObj->GetDictionary().GetKey("Differences");
140 PdfArray expected;
141
142 expected.Add(static_cast<int64_t>(1));
143 expected.Add(PdfName("B"));
144 expected.Add(PdfName("C"));
145 expected.Add(static_cast<int64_t>(4));
146 expected.Add(PdfName("D"));
147 expected.Add(PdfName("E"));
148 expected.Add(static_cast<int64_t>(9));
149 expected.Add(PdfName("F"));
150
151 const PdfArray& data = diff->GetArray();
152 REQUIRE(expected.GetSize() == data.GetSize());
153 for (unsigned int i = 0; i < data.GetSize(); i++)
154 REQUIRE(expected[i] == data[i]);
155 }
156
157 TEST_CASE("testDifferencesEncoding")
158 {
159 // Create a differences encoding where A and B are exchanged
160 PdfDifferenceList difference;
161 difference.AddDifference((unsigned char)'A', "B");
162 difference.AddDifference((unsigned char)'B', "A");
163 difference.AddDifference((unsigned char)'C', "D");
164
165 PdfMemDocument doc;
166
167 PdfFontCreateParams params;
168 params.Encoding = PdfEncoding(std::make_shared<PdfDifferenceEncoding>(difference, PdfEncodingMapFactory::WinAnsiEncodingInstance()));
169 auto& font = doc.GetFonts().GetStandard14Font(PdfStandard14FontType::Helvetica, params);
170
171 charbuff encoded;
172 INFO("'C' in \"BAABC\" is already reserved for mapping in 'D'");
173 REQUIRE(!font.GetEncoding().TryConvertToEncoded("BAABC", encoded));
174 encoded = font.GetEncoding().ConvertToEncoded("BAABI");
175 REQUIRE(encoded == "ABBAI");
176 auto unicode = params.Encoding.ConvertToUtf8(PdfString::FromRaw(encoded));
177 REQUIRE(unicode == "BAABI");
178 }
179
180 // FIX-ME: This test passes but it's garbage and very slow
181 // Fix it the whole thing by handling properly the Adobe Glyph List
182 // in PdfDifferenceEncoding (or better a new separate function)
183 TEST_CASE("testUnicodeNames", "[.]")
184 {
185 // List of items which are defined twice and cause
186 // other ids to be returned than those which where send in
187 const char* duplicates[] = {
188 "Delta",
189 "fraction",
190 "hyphen",
191 "macron",
192 "mu",
193 "Omega",
194 "periodcentered",
195 "scedilla",
196 "Scedilla",
197 "space",
198 "tcommaaccent",
199 "Tcommaaccent",
200 "exclamsmall",
201 "dollaroldstyle",
202 "zerooldstyle",
203 "oneoldstyle",
204 "twooldstyle",
205 "threeoldstyle",
206 "fouroldstyle",
207 "fiveoldstyle",
208 "sixoldstyle",
209 "sevenoldstyle",
210 "eightoldstyle",
211 "nineoldstyle",
212 "ampersandsmall",
213 "questionsmall",
214 nullptr
215 };
216
217 unsigned duplicatesCount = 0;
218 unsigned codeCount = 0;
219 for (int i = 0; i < 0xFFFF; i++)
220 {
221 PdfName name = PdfDifferenceEncoding::CodePointToName(static_cast<char32_t>(i));
222 char32_t id = PdfDifferenceEncoding::NameToCodePoint(name);
223
224 bool duplicateFound = false;
225 const char** duplicate = duplicates;
226 while (*duplicate != nullptr)
227 {
228 if (name == *duplicate)
229 {
230 duplicateFound = true;
231 break;
232 }
233
234 duplicate++;
235 }
236
237 if (!duplicateFound)
238 {
239 if (static_cast<char32_t>(i) == id)
240 codeCount++;
241 }
242 else
243 {
244 duplicatesCount++;
245 }
246 }
247
248 // FIX-ME: This test is fishy. It's not clear what "codeCount"
249 // means and why (65535 - duplicatesCount) is different than codeCount
250 // Possibly there are more duplicates
251 INFO(utls::Format("Compared codes count: {}", codeCount));
252 INFO(utls::Format("Duplicate codes count: {}", duplicatesCount));
253 REQUIRE(codeCount == 65421);
254 }
255
256 TEST_CASE("testGetCharCode")
257 {
258 auto winAnsiEncoding = PdfEncodingFactory::CreateWinAnsiEncoding();
259 INFO("WinAnsiEncoding");
260 outofRangeHelper(winAnsiEncoding);
261
262 auto macRomanEncoding = PdfEncodingFactory::CreateMacRomanEncoding();
263 INFO("MacRomanEncoding");
264 outofRangeHelper(macRomanEncoding);
265
266 PdfDifferenceList difference;
267 difference.AddDifference((unsigned char)'A', "B");
268 difference.AddDifference((unsigned char)'B', "A");
269 PdfEncoding differenceEncoding(std::make_shared<PdfDifferenceEncoding>(difference, PdfEncodingMapFactory::WinAnsiEncodingInstance()));
270 outofRangeHelper(differenceEncoding);
271 }
272
273 TEST_CASE("testToUnicodeParse")
274 {
275 string_view toUnicode =
276 "3 beginbfrange\n"
277 "<0001> <0004> <1001>\n"
278 "<0005> <000A> [<000A> <0009> <0008> <0007> <0006> <0005>]\n"
279 "<000B> <000F> <100B>\n"
280 "endbfrange\n";
281 charbuff encodedStr("\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07\x00\x08\x00\x09\x00\x0A\x00\x0B\x00\x0C\x00\x0D\x00\x0E\x00\x0F"sv);
282 string expected("\xE1\x80\x81\xE1\x80\x82\xE1\x80\x83\xE1\x80\x84\x0A\x09\x08\x07\x06\x05\xE1\x80\x8B\xE1\x80\x8C\xE1\x80\x8D\xE1\x80\x8E\xE1\x80\x8F"sv);
283
284 PdfMemDocument doc;
285 auto& toUnicodeObj = doc.GetObjects().CreateDictionaryObject();
286 toUnicodeObj.GetOrCreateStream().SetData(toUnicode);
287
288 PdfEncoding encoding(std::make_shared<PdfIdentityEncoding>(2), PdfCMapEncoding::CreateFromObject(toUnicodeObj));
289
290 auto utf8str = encoding.ConvertToUtf8(PdfString::FromRaw(encodedStr));
291 REQUIRE(utf8str == expected);
292
293 const char* toUnicodeInvalidTests[] =
294 {
295 // missing object numbers
296 "beginbfrange\n",
297 "beginbfchar\n",
298
299 // invalid hex digits
300 "2 beginbfrange <WXYZ> endbfrange\n",
301 "2 beginbfrange <-123> endbfrange\n",
302 "2 beginbfrange <<00>> endbfrange\n",
303
304 // missing hex digits
305 "2 beginbfrange <> endbfrange\n",
306
307 // empty array
308 "2 beginbfrange [] endbfrange\n",
309
310 nullptr
311 };
312
313 for (size_t i = 0; toUnicodeInvalidTests[i] != nullptr; i++)
314 {
315 try
316 {
317 PdfIndirectObjectList invalidList;
318 auto& invalidObject = invalidList.CreateDictionaryObject();
319 invalidObject.GetOrCreateStream().SetData(bufferview(toUnicodeInvalidTests[i], char_traits<char>::length(toUnicodeInvalidTests[i])));
320
321 PdfEncoding encodingTestInvalid(std::make_shared<PdfIdentityEncoding>(2), PdfCMapEncoding::CreateFromObject(invalidObject));
322
323 auto unicodeStringTestInvalid = encodingTestInvalid.ConvertToUtf8(PdfString::FromRaw(encodedStr));
324
325 // exception not thrown - should never get here
326 // TODO not all invalid input throws an exception (e.g. no hex digits in <WXYZ>)
327 FAIL();
328 }
329 catch (PdfError&)
330 {
331 // parsing every invalid test string should throw an exception
332 }
333 catch (exception&)
334 {
335 FAIL("Unexpected exception type");
336 }
337 }
338 }
339
340 void outofRangeHelper(PdfEncoding& encoding)
341 {
342 (void)encoding.GetCodePoint(encoding.GetFirstChar());
343 REQUIRE(encoding.GetCodePoint(encoding.GetFirstChar().Code - 1) == U'\0');
344 (void)encoding.GetCodePoint(encoding.GetLastChar());
345 REQUIRE(encoding.GetCodePoint(encoding.GetLastChar().Code + 1) == U'\0');
346 }