blob: 0814c76a4db53ae26c215c79245c314a8e0f28cf [file] [log] [blame]
vandebo@chromium.org04c643b2011-08-08 22:33:05 +00001/*
2 * Copyright 2010 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Hal Canary43fb7a02016-12-30 13:09:03 -05009
10#ifdef SK_SUPPORT_PDF
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkData.h"
13#include "include/core/SkStream.h"
14#include "include/private/SkTo.h"
15#include "src/pdf/SkPDFMakeToUnicodeCmap.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000016
Hal Canary31355982018-10-19 12:21:41 -040017static constexpr SkGlyphID kMaximumGlyphIndex = UINT16_MAX;
halcanary530032a2016-08-18 14:22:52 -070018
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000019static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000020 const char* buffer, size_t len) {
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000021 if (len != strlen(buffer)) {
22 return false;
23 }
Mike Reed5adaf8b2016-12-15 13:02:33 -050024
25 const size_t streamSize = stream.bytesWritten();
26
27 if (offset + len > streamSize) {
28 return false;
29 }
30
31 SkAutoTMalloc<char> data(streamSize);
32 stream.copyTo(data.get());
33 return memcmp(data.get() + offset, buffer, len) == 0;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000034}
35
halcanary57f744e2016-09-09 11:41:59 -070036DEF_TEST(SkPDF_ToUnicode, reporter) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000037 SkTDArray<SkUnichar> glyphToUnicode;
38 SkTDArray<uint16_t> glyphsInSubset;
Hal Canary31355982018-10-19 12:21:41 -040039 SkPDFGlyphUse subset(1, kMaximumGlyphIndex);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000040
Mike Reed5edcd312018-08-08 11:23:41 -040041 glyphToUnicode.push_back(0); // 0
42 glyphToUnicode.push_back(0); // 1
43 glyphToUnicode.push_back(0); // 2
44 glyphsInSubset.push_back(3);
45 glyphToUnicode.push_back(0x20); // 3
46 glyphsInSubset.push_back(4);
47 glyphToUnicode.push_back(0x25); // 4
48 glyphsInSubset.push_back(5);
49 glyphToUnicode.push_back(0x27); // 5
50 glyphsInSubset.push_back(6);
51 glyphToUnicode.push_back(0x28); // 6
52 glyphsInSubset.push_back(7);
53 glyphToUnicode.push_back(0x29); // 7
54 glyphsInSubset.push_back(8);
55 glyphToUnicode.push_back(0x2F); // 8
56 glyphsInSubset.push_back(9);
57 glyphToUnicode.push_back(0x33); // 9
58 glyphToUnicode.push_back(0); // 10
59 glyphsInSubset.push_back(11);
60 glyphToUnicode.push_back(0x35); // 11
61 glyphsInSubset.push_back(12);
62 glyphToUnicode.push_back(0x36); // 12
63 glyphsInSubset.push_back(13);
64 glyphToUnicode.push_back(0x37); // 13
edisonn@google.com26d2e042013-09-18 19:29:08 +000065 for (uint16_t i = 14; i < 0xFE; ++i) {
Mike Reed5edcd312018-08-08 11:23:41 -040066 glyphToUnicode.push_back(0); // Zero from index 0x9 to 0xFD
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000067 }
Mike Reed5edcd312018-08-08 11:23:41 -040068 glyphsInSubset.push_back(0xFE);
69 glyphToUnicode.push_back(0x1010);
70 glyphsInSubset.push_back(0xFF);
71 glyphToUnicode.push_back(0x1011);
72 glyphsInSubset.push_back(0x100);
73 glyphToUnicode.push_back(0x1012);
74 glyphsInSubset.push_back(0x101);
75 glyphToUnicode.push_back(0x1013);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000076
Hal Canary46cc3da2018-05-09 11:50:34 -040077 SkGlyphID lastGlyphID = SkToU16(glyphToUnicode.count() - 1);
78
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000079 SkDynamicMemoryWStream buffer;
Hal Canary52514d52018-10-19 10:08:42 -040080 for (uint16_t v : glyphsInSubset) {
81 subset.set(v);
82 }
Hal Canary46cc3da2018-05-09 11:50:34 -040083 SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 0,
Brian Osman788b9162020-02-07 10:36:46 -050084 std::min<SkGlyphID>(0xFFFF, lastGlyphID));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000085
86 char expectedResult[] =
87"4 beginbfchar\n\
88<0003> <0020>\n\
89<0004> <0025>\n\
90<0008> <002F>\n\
91<0009> <0033>\n\
92endbfchar\n\
934 beginbfrange\n\
94<0005> <0007> <0027>\n\
edisonn@google.com26d2e042013-09-18 19:29:08 +000095<000B> <000D> <0035>\n\
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000096<00FE> <00FF> <1010>\n\
97<0100> <0101> <1012>\n\
98endbfrange\n";
99
100 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
Ben Wagner884300d2016-12-16 16:51:41 +0000101 buffer.bytesWritten()));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000102
edisonn@google.com26d2e042013-09-18 19:29:08 +0000103 // Remove characters and ranges.
104 buffer.reset();
105
Hal Canary46cc3da2018-05-09 11:50:34 -0400106 SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 8,
Brian Osman788b9162020-02-07 10:36:46 -0500107 std::min<SkGlyphID>(0x00FF, lastGlyphID));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000108
109 char expectedResultChop1[] =
110"2 beginbfchar\n\
111<0008> <002F>\n\
112<0009> <0033>\n\
113endbfchar\n\
1142 beginbfrange\n\
115<000B> <000D> <0035>\n\
116<00FE> <00FF> <1010>\n\
117endbfrange\n";
118
119 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1,
Ben Wagner884300d2016-12-16 16:51:41 +0000120 buffer.bytesWritten()));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000121
122 // Remove characters from range to downdrade it to one char.
123 buffer.reset();
124
Hal Canary46cc3da2018-05-09 11:50:34 -0400125 SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 0x00D,
Brian Osman788b9162020-02-07 10:36:46 -0500126 std::min<SkGlyphID>(0x00FE, lastGlyphID));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000127
128 char expectedResultChop2[] =
129"2 beginbfchar\n\
130<000D> <0037>\n\
131<00FE> <1010>\n\
132endbfchar\n";
133
134 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2,
Ben Wagner884300d2016-12-16 16:51:41 +0000135 buffer.bytesWritten()));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000136
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000137 buffer.reset();
138
Hal Canary46cc3da2018-05-09 11:50:34 -0400139 SkPDFAppendCmapSections(&glyphToUnicode[0], nullptr, &buffer, false, 0xFC,
Brian Osman788b9162020-02-07 10:36:46 -0500140 std::min<SkGlyphID>(0x110, lastGlyphID));
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000141
142 char expectedResultSingleBytes[] =
143"2 beginbfchar\n\
halcanary3d01c622016-08-31 12:52:35 -0700144<01> <0000>\n\
145<02> <0000>\n\
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000146endbfchar\n\
1471 beginbfrange\n\
halcanary3d01c622016-08-31 12:52:35 -0700148<03> <06> <1010>\n\
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000149endbfrange\n";
150
151 REPORTER_ASSERT(reporter, stream_equals(buffer, 0,
152 expectedResultSingleBytes,
Ben Wagner884300d2016-12-16 16:51:41 +0000153 buffer.bytesWritten()));
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000154
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000155 glyphToUnicode.reset();
156 glyphsInSubset.reset();
Hal Canary31355982018-10-19 12:21:41 -0400157 SkPDFGlyphUse subset2(1, kMaximumGlyphIndex);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000158
159 // Test mapping:
160 // I n s t a l
161 // Glyph id 2c 51 56 57 44 4f
162 // Unicode 49 6e 73 74 61 6c
bsalomon98806072014-12-12 15:11:17 -0800163 for (SkUnichar i = 0; i < 100; ++i) {
Mike Reed5edcd312018-08-08 11:23:41 -0400164 glyphToUnicode.push_back(i + 29);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000165 }
Hal Canary46cc3da2018-05-09 11:50:34 -0400166 lastGlyphID = SkToU16(glyphToUnicode.count() - 1);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000167
Mike Reed5edcd312018-08-08 11:23:41 -0400168 glyphsInSubset.push_back(0x2C);
169 glyphsInSubset.push_back(0x44);
170 glyphsInSubset.push_back(0x4F);
171 glyphsInSubset.push_back(0x51);
172 glyphsInSubset.push_back(0x56);
173 glyphsInSubset.push_back(0x57);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000174
175 SkDynamicMemoryWStream buffer2;
Hal Canary52514d52018-10-19 10:08:42 -0400176 for (uint16_t v : glyphsInSubset) {
177 subset2.set(v);
178 }
Hal Canary46cc3da2018-05-09 11:50:34 -0400179 SkPDFAppendCmapSections(&glyphToUnicode[0], &subset2, &buffer2, true, 0,
Brian Osman788b9162020-02-07 10:36:46 -0500180 std::min<SkGlyphID>(0xFFFF, lastGlyphID));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000181
182 char expectedResult2[] =
183"4 beginbfchar\n\
184<002C> <0049>\n\
185<0044> <0061>\n\
186<004F> <006C>\n\
187<0051> <006E>\n\
188endbfchar\n\
1891 beginbfrange\n\
190<0056> <0057> <0073>\n\
191endbfrange\n";
192
193 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
Ben Wagner884300d2016-12-16 16:51:41 +0000194 buffer2.bytesWritten()));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000195}
Hal Canary43fb7a02016-12-30 13:09:03 -0500196
197#endif