blob: 33fcc80ef7648a6e3ac075eb8a7d6841577dd3e5 [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
vandebo@chromium.org04c643b2011-08-08 22:33:05 +00008#include "SkData.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +00009#include "SkPDFFont.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "SkPDFTypes.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
14static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000015 const char* buffer, size_t len) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000016 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000017 if (offset + len > data->size()) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000018 return false;
19 }
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000020 if (len != strlen(buffer)) {
21 return false;
22 }
robertphillips@google.com59f46b82012-07-10 17:30:58 +000023 return memcmp(data->bytes() + offset, buffer, len) == 0;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000024}
25
26void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
27 const SkPDFGlyphSet* subset,
edisonn@google.com26d2e042013-09-18 19:29:08 +000028 SkDynamicMemoryWStream* cmap,
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +000029 bool multiByteGlyphs,
edisonn@google.com26d2e042013-09-18 19:29:08 +000030 uint16_t firstGlypthID,
31 uint16_t lastGlypthID);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000032
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000033DEF_TEST(ToUnicode, reporter) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000034 SkTDArray<SkUnichar> glyphToUnicode;
35 SkTDArray<uint16_t> glyphsInSubset;
36 SkPDFGlyphSet subset;
37
38 glyphToUnicode.push(0); // 0
39 glyphToUnicode.push(0); // 1
40 glyphToUnicode.push(0); // 2
41 glyphsInSubset.push(3);
42 glyphToUnicode.push(0x20); // 3
43 glyphsInSubset.push(4);
44 glyphToUnicode.push(0x25); // 4
45 glyphsInSubset.push(5);
46 glyphToUnicode.push(0x27); // 5
47 glyphsInSubset.push(6);
48 glyphToUnicode.push(0x28); // 6
49 glyphsInSubset.push(7);
50 glyphToUnicode.push(0x29); // 7
51 glyphsInSubset.push(8);
52 glyphToUnicode.push(0x2F); // 8
53 glyphsInSubset.push(9);
54 glyphToUnicode.push(0x33); // 9
55 glyphToUnicode.push(0); // 10
56 glyphsInSubset.push(11);
57 glyphToUnicode.push(0x35); // 11
58 glyphsInSubset.push(12);
59 glyphToUnicode.push(0x36); // 12
edisonn@google.com26d2e042013-09-18 19:29:08 +000060 glyphsInSubset.push(13);
61 glyphToUnicode.push(0x37); // 13
62 for (uint16_t i = 14; i < 0xFE; ++i) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000063 glyphToUnicode.push(0); // Zero from index 0x9 to 0xFD
64 }
65 glyphsInSubset.push(0xFE);
66 glyphToUnicode.push(0x1010);
67 glyphsInSubset.push(0xFF);
68 glyphToUnicode.push(0x1011);
69 glyphsInSubset.push(0x100);
70 glyphToUnicode.push(0x1012);
71 glyphsInSubset.push(0x101);
72 glyphToUnicode.push(0x1013);
73
74 SkDynamicMemoryWStream buffer;
75 subset.set(glyphsInSubset.begin(), glyphsInSubset.count());
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +000076 append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 0, 0xFFFF);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000077
78 char expectedResult[] =
79"4 beginbfchar\n\
80<0003> <0020>\n\
81<0004> <0025>\n\
82<0008> <002F>\n\
83<0009> <0033>\n\
84endbfchar\n\
854 beginbfrange\n\
86<0005> <0007> <0027>\n\
edisonn@google.com26d2e042013-09-18 19:29:08 +000087<000B> <000D> <0035>\n\
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000088<00FE> <00FF> <1010>\n\
89<0100> <0101> <1012>\n\
90endbfrange\n";
91
92 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
93 buffer.getOffset()));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000094
edisonn@google.com26d2e042013-09-18 19:29:08 +000095 // Remove characters and ranges.
96 buffer.reset();
97
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +000098 append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 8, 0x00FF);
edisonn@google.com26d2e042013-09-18 19:29:08 +000099
100 char expectedResultChop1[] =
101"2 beginbfchar\n\
102<0008> <002F>\n\
103<0009> <0033>\n\
104endbfchar\n\
1052 beginbfrange\n\
106<000B> <000D> <0035>\n\
107<00FE> <00FF> <1010>\n\
108endbfrange\n";
109
110 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1,
111 buffer.getOffset()));
112
113 // Remove characters from range to downdrade it to one char.
114 buffer.reset();
115
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000116 append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 0x00D, 0x00FE);
edisonn@google.com26d2e042013-09-18 19:29:08 +0000117
118 char expectedResultChop2[] =
119"2 beginbfchar\n\
120<000D> <0037>\n\
121<00FE> <1010>\n\
122endbfchar\n";
123
124 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2,
125 buffer.getOffset()));
126
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000127 buffer.reset();
128
129 append_cmap_sections(glyphToUnicode, NULL, &buffer, false, 0xFC, 0x110);
130
131 char expectedResultSingleBytes[] =
132"2 beginbfchar\n\
133<0001> <0000>\n\
134<0002> <0000>\n\
135endbfchar\n\
1361 beginbfrange\n\
137<0003> <0006> <1010>\n\
138endbfrange\n";
139
140 REPORTER_ASSERT(reporter, stream_equals(buffer, 0,
141 expectedResultSingleBytes,
142 buffer.getOffset()));
143
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000144 glyphToUnicode.reset();
145 glyphsInSubset.reset();
146 SkPDFGlyphSet subset2;
147
148 // Test mapping:
149 // I n s t a l
150 // Glyph id 2c 51 56 57 44 4f
151 // Unicode 49 6e 73 74 61 6c
bsalomon98806072014-12-12 15:11:17 -0800152 for (SkUnichar i = 0; i < 100; ++i) {
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000153 glyphToUnicode.push(i + 29);
154 }
155
156 glyphsInSubset.push(0x2C);
157 glyphsInSubset.push(0x44);
158 glyphsInSubset.push(0x4F);
159 glyphsInSubset.push(0x51);
160 glyphsInSubset.push(0x56);
161 glyphsInSubset.push(0x57);
162
163 SkDynamicMemoryWStream buffer2;
164 subset2.set(glyphsInSubset.begin(), glyphsInSubset.count());
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000165 append_cmap_sections(glyphToUnicode, &subset2, &buffer2, true, 0, 0xffff);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000166
167 char expectedResult2[] =
168"4 beginbfchar\n\
169<002C> <0049>\n\
170<0044> <0061>\n\
171<004F> <006C>\n\
172<0051> <006E>\n\
173endbfchar\n\
1741 beginbfrange\n\
175<0056> <0057> <0073>\n\
176endbfrange\n";
177
178 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
179 buffer2.getOffset()));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000180}