blob: b3ee2d86f2cf20a83a84a97f877d76b255fb8439 [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
halcanary530032a2016-08-18 14:22:52 -07008#include "SkBitSet.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +00009#include "SkData.h"
halcanary8eccc302016-08-09 13:04:34 -070010#include "SkPDFMakeToUnicodeCmap.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000011#include "SkStream.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000013
halcanary530032a2016-08-18 14:22:52 -070014static const int kMaximumGlyphCount = SK_MaxU16 + 1;
15
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000016static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000017 const char* buffer, size_t len) {
reed42943c82016-09-12 12:01:44 -070018 sk_sp<SkData> data = stream.snapshotAsData();
robertphillips@google.com59f46b82012-07-10 17:30:58 +000019 if (offset + len > data->size()) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000020 return false;
21 }
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000022 if (len != strlen(buffer)) {
23 return false;
24 }
robertphillips@google.com59f46b82012-07-10 17:30:58 +000025 return memcmp(data->bytes() + offset, buffer, len) == 0;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000026}
27
halcanary57f744e2016-09-09 11:41:59 -070028DEF_TEST(SkPDF_ToUnicode, reporter) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000029 SkTDArray<SkUnichar> glyphToUnicode;
30 SkTDArray<uint16_t> glyphsInSubset;
halcanary530032a2016-08-18 14:22:52 -070031 SkBitSet subset(kMaximumGlyphCount);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000032
33 glyphToUnicode.push(0); // 0
34 glyphToUnicode.push(0); // 1
35 glyphToUnicode.push(0); // 2
36 glyphsInSubset.push(3);
37 glyphToUnicode.push(0x20); // 3
38 glyphsInSubset.push(4);
39 glyphToUnicode.push(0x25); // 4
40 glyphsInSubset.push(5);
41 glyphToUnicode.push(0x27); // 5
42 glyphsInSubset.push(6);
43 glyphToUnicode.push(0x28); // 6
44 glyphsInSubset.push(7);
45 glyphToUnicode.push(0x29); // 7
46 glyphsInSubset.push(8);
47 glyphToUnicode.push(0x2F); // 8
48 glyphsInSubset.push(9);
49 glyphToUnicode.push(0x33); // 9
50 glyphToUnicode.push(0); // 10
51 glyphsInSubset.push(11);
52 glyphToUnicode.push(0x35); // 11
53 glyphsInSubset.push(12);
54 glyphToUnicode.push(0x36); // 12
edisonn@google.com26d2e042013-09-18 19:29:08 +000055 glyphsInSubset.push(13);
56 glyphToUnicode.push(0x37); // 13
57 for (uint16_t i = 14; i < 0xFE; ++i) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000058 glyphToUnicode.push(0); // Zero from index 0x9 to 0xFD
59 }
60 glyphsInSubset.push(0xFE);
61 glyphToUnicode.push(0x1010);
62 glyphsInSubset.push(0xFF);
63 glyphToUnicode.push(0x1011);
64 glyphsInSubset.push(0x100);
65 glyphToUnicode.push(0x1012);
66 glyphsInSubset.push(0x101);
67 glyphToUnicode.push(0x1013);
68
69 SkDynamicMemoryWStream buffer;
halcanary530032a2016-08-18 14:22:52 -070070 subset.setAll(glyphsInSubset.begin(), glyphsInSubset.count());
halcanary8eccc302016-08-09 13:04:34 -070071 SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 0, 0xFFFF);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000072
73 char expectedResult[] =
74"4 beginbfchar\n\
75<0003> <0020>\n\
76<0004> <0025>\n\
77<0008> <002F>\n\
78<0009> <0033>\n\
79endbfchar\n\
804 beginbfrange\n\
81<0005> <0007> <0027>\n\
edisonn@google.com26d2e042013-09-18 19:29:08 +000082<000B> <000D> <0035>\n\
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000083<00FE> <00FF> <1010>\n\
84<0100> <0101> <1012>\n\
85endbfrange\n";
86
87 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
88 buffer.getOffset()));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000089
edisonn@google.com26d2e042013-09-18 19:29:08 +000090 // Remove characters and ranges.
91 buffer.reset();
92
halcanary8eccc302016-08-09 13:04:34 -070093 SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 8, 0x00FF);
edisonn@google.com26d2e042013-09-18 19:29:08 +000094
95 char expectedResultChop1[] =
96"2 beginbfchar\n\
97<0008> <002F>\n\
98<0009> <0033>\n\
99endbfchar\n\
1002 beginbfrange\n\
101<000B> <000D> <0035>\n\
102<00FE> <00FF> <1010>\n\
103endbfrange\n";
104
105 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1,
106 buffer.getOffset()));
107
108 // Remove characters from range to downdrade it to one char.
109 buffer.reset();
110
halcanary8eccc302016-08-09 13:04:34 -0700111 SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 0x00D, 0x00FE);
edisonn@google.com26d2e042013-09-18 19:29:08 +0000112
113 char expectedResultChop2[] =
114"2 beginbfchar\n\
115<000D> <0037>\n\
116<00FE> <1010>\n\
117endbfchar\n";
118
119 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2,
120 buffer.getOffset()));
121
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000122 buffer.reset();
123
halcanary8eccc302016-08-09 13:04:34 -0700124 SkPDFAppendCmapSections(glyphToUnicode, nullptr, &buffer, false, 0xFC, 0x110);
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000125
126 char expectedResultSingleBytes[] =
127"2 beginbfchar\n\
halcanary3d01c622016-08-31 12:52:35 -0700128<01> <0000>\n\
129<02> <0000>\n\
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000130endbfchar\n\
1311 beginbfrange\n\
halcanary3d01c622016-08-31 12:52:35 -0700132<03> <06> <1010>\n\
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000133endbfrange\n";
134
135 REPORTER_ASSERT(reporter, stream_equals(buffer, 0,
136 expectedResultSingleBytes,
137 buffer.getOffset()));
138
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000139 glyphToUnicode.reset();
140 glyphsInSubset.reset();
halcanary530032a2016-08-18 14:22:52 -0700141 SkBitSet subset2(kMaximumGlyphCount);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000142
143 // Test mapping:
144 // I n s t a l
145 // Glyph id 2c 51 56 57 44 4f
146 // Unicode 49 6e 73 74 61 6c
bsalomon98806072014-12-12 15:11:17 -0800147 for (SkUnichar i = 0; i < 100; ++i) {
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000148 glyphToUnicode.push(i + 29);
149 }
150
151 glyphsInSubset.push(0x2C);
152 glyphsInSubset.push(0x44);
153 glyphsInSubset.push(0x4F);
154 glyphsInSubset.push(0x51);
155 glyphsInSubset.push(0x56);
156 glyphsInSubset.push(0x57);
157
158 SkDynamicMemoryWStream buffer2;
halcanary530032a2016-08-18 14:22:52 -0700159 subset2.setAll(glyphsInSubset.begin(), glyphsInSubset.count());
halcanary8eccc302016-08-09 13:04:34 -0700160 SkPDFAppendCmapSections(glyphToUnicode, &subset2, &buffer2, true, 0, 0xffff);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000161
162 char expectedResult2[] =
163"4 beginbfchar\n\
164<002C> <0049>\n\
165<0044> <0061>\n\
166<004F> <006C>\n\
167<0051> <006E>\n\
168endbfchar\n\
1691 beginbfrange\n\
170<0056> <0057> <0073>\n\
171endbfrange\n";
172
173 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
174 buffer2.getOffset()));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000175}