blob: d518432b8ad0ef4664dc2a3b0f18aac300d5898e [file] [log] [blame]
vandebo@chromium.org04c643b2011-08-08 22:33:05 +00001
2/*
3 * Copyright 2010 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000010#include "Test.h"
11#include "SkData.h"
12#include "SkPDFTypes.h"
13#include "SkPDFFont.h"
14#include "SkStream.h"
15
16static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000017 const char* buffer, size_t len) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000018 SkAutoDataUnref data(stream.copyToData());
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
28void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
29 const SkPDFGlyphSet* subset,
edisonn@google.com26d2e042013-09-18 19:29:08 +000030 SkDynamicMemoryWStream* cmap,
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +000031 bool multiByteGlyphs,
edisonn@google.com26d2e042013-09-18 19:29:08 +000032 uint16_t firstGlypthID,
33 uint16_t lastGlypthID);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000034
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000035static void TestToUnicode(skiatest::Reporter* reporter) {
36 SkTDArray<SkUnichar> glyphToUnicode;
37 SkTDArray<uint16_t> glyphsInSubset;
38 SkPDFGlyphSet subset;
39
40 glyphToUnicode.push(0); // 0
41 glyphToUnicode.push(0); // 1
42 glyphToUnicode.push(0); // 2
43 glyphsInSubset.push(3);
44 glyphToUnicode.push(0x20); // 3
45 glyphsInSubset.push(4);
46 glyphToUnicode.push(0x25); // 4
47 glyphsInSubset.push(5);
48 glyphToUnicode.push(0x27); // 5
49 glyphsInSubset.push(6);
50 glyphToUnicode.push(0x28); // 6
51 glyphsInSubset.push(7);
52 glyphToUnicode.push(0x29); // 7
53 glyphsInSubset.push(8);
54 glyphToUnicode.push(0x2F); // 8
55 glyphsInSubset.push(9);
56 glyphToUnicode.push(0x33); // 9
57 glyphToUnicode.push(0); // 10
58 glyphsInSubset.push(11);
59 glyphToUnicode.push(0x35); // 11
60 glyphsInSubset.push(12);
61 glyphToUnicode.push(0x36); // 12
edisonn@google.com26d2e042013-09-18 19:29:08 +000062 glyphsInSubset.push(13);
63 glyphToUnicode.push(0x37); // 13
64 for (uint16_t i = 14; i < 0xFE; ++i) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000065 glyphToUnicode.push(0); // Zero from index 0x9 to 0xFD
66 }
67 glyphsInSubset.push(0xFE);
68 glyphToUnicode.push(0x1010);
69 glyphsInSubset.push(0xFF);
70 glyphToUnicode.push(0x1011);
71 glyphsInSubset.push(0x100);
72 glyphToUnicode.push(0x1012);
73 glyphsInSubset.push(0x101);
74 glyphToUnicode.push(0x1013);
75
76 SkDynamicMemoryWStream buffer;
77 subset.set(glyphsInSubset.begin(), glyphsInSubset.count());
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +000078 append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 0, 0xFFFF);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000079
80 char expectedResult[] =
81"4 beginbfchar\n\
82<0003> <0020>\n\
83<0004> <0025>\n\
84<0008> <002F>\n\
85<0009> <0033>\n\
86endbfchar\n\
874 beginbfrange\n\
88<0005> <0007> <0027>\n\
edisonn@google.com26d2e042013-09-18 19:29:08 +000089<000B> <000D> <0035>\n\
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000090<00FE> <00FF> <1010>\n\
91<0100> <0101> <1012>\n\
92endbfrange\n";
93
94 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
95 buffer.getOffset()));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000096
edisonn@google.com26d2e042013-09-18 19:29:08 +000097 // Remove characters and ranges.
98 buffer.reset();
99
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000100 append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 8, 0x00FF);
edisonn@google.com26d2e042013-09-18 19:29:08 +0000101
102 char expectedResultChop1[] =
103"2 beginbfchar\n\
104<0008> <002F>\n\
105<0009> <0033>\n\
106endbfchar\n\
1072 beginbfrange\n\
108<000B> <000D> <0035>\n\
109<00FE> <00FF> <1010>\n\
110endbfrange\n";
111
112 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1,
113 buffer.getOffset()));
114
115 // Remove characters from range to downdrade it to one char.
116 buffer.reset();
117
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000118 append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 0x00D, 0x00FE);
edisonn@google.com26d2e042013-09-18 19:29:08 +0000119
120 char expectedResultChop2[] =
121"2 beginbfchar\n\
122<000D> <0037>\n\
123<00FE> <1010>\n\
124endbfchar\n";
125
126 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2,
127 buffer.getOffset()));
128
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000129 buffer.reset();
130
131 append_cmap_sections(glyphToUnicode, NULL, &buffer, false, 0xFC, 0x110);
132
133 char expectedResultSingleBytes[] =
134"2 beginbfchar\n\
135<0001> <0000>\n\
136<0002> <0000>\n\
137endbfchar\n\
1381 beginbfrange\n\
139<0003> <0006> <1010>\n\
140endbfrange\n";
141
142 REPORTER_ASSERT(reporter, stream_equals(buffer, 0,
143 expectedResultSingleBytes,
144 buffer.getOffset()));
145
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000146 glyphToUnicode.reset();
147 glyphsInSubset.reset();
148 SkPDFGlyphSet subset2;
149
150 // Test mapping:
151 // I n s t a l
152 // Glyph id 2c 51 56 57 44 4f
153 // Unicode 49 6e 73 74 61 6c
154 for (size_t i = 0; i < 100; ++i) {
155 glyphToUnicode.push(i + 29);
156 }
157
158 glyphsInSubset.push(0x2C);
159 glyphsInSubset.push(0x44);
160 glyphsInSubset.push(0x4F);
161 glyphsInSubset.push(0x51);
162 glyphsInSubset.push(0x56);
163 glyphsInSubset.push(0x57);
164
165 SkDynamicMemoryWStream buffer2;
166 subset2.set(glyphsInSubset.begin(), glyphsInSubset.count());
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000167 append_cmap_sections(glyphToUnicode, &subset2, &buffer2, true, 0, 0xffff);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000168
169 char expectedResult2[] =
170"4 beginbfchar\n\
171<002C> <0049>\n\
172<0044> <0061>\n\
173<004F> <006C>\n\
174<0051> <006E>\n\
175endbfchar\n\
1761 beginbfrange\n\
177<0056> <0057> <0073>\n\
178endbfrange\n";
179
180 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
181 buffer2.getOffset()));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000182}
183
184#include "TestClassDef.h"
185DEFINE_TESTCLASS("ToUnicode", ToUnicodeTestClass, TestToUnicode)