blob: 69ec614bcf470f8c51a051f2342e33e41eb73b73 [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,
31 uint16_t firstGlypthID,
32 uint16_t lastGlypthID);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000033
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000034static void TestToUnicode(skiatest::Reporter* reporter) {
35 SkTDArray<SkUnichar> glyphToUnicode;
36 SkTDArray<uint16_t> glyphsInSubset;
37 SkPDFGlyphSet subset;
38
39 glyphToUnicode.push(0); // 0
40 glyphToUnicode.push(0); // 1
41 glyphToUnicode.push(0); // 2
42 glyphsInSubset.push(3);
43 glyphToUnicode.push(0x20); // 3
44 glyphsInSubset.push(4);
45 glyphToUnicode.push(0x25); // 4
46 glyphsInSubset.push(5);
47 glyphToUnicode.push(0x27); // 5
48 glyphsInSubset.push(6);
49 glyphToUnicode.push(0x28); // 6
50 glyphsInSubset.push(7);
51 glyphToUnicode.push(0x29); // 7
52 glyphsInSubset.push(8);
53 glyphToUnicode.push(0x2F); // 8
54 glyphsInSubset.push(9);
55 glyphToUnicode.push(0x33); // 9
56 glyphToUnicode.push(0); // 10
57 glyphsInSubset.push(11);
58 glyphToUnicode.push(0x35); // 11
59 glyphsInSubset.push(12);
60 glyphToUnicode.push(0x36); // 12
edisonn@google.com26d2e042013-09-18 19:29:08 +000061 glyphsInSubset.push(13);
62 glyphToUnicode.push(0x37); // 13
63 for (uint16_t i = 14; i < 0xFE; ++i) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000064 glyphToUnicode.push(0); // Zero from index 0x9 to 0xFD
65 }
66 glyphsInSubset.push(0xFE);
67 glyphToUnicode.push(0x1010);
68 glyphsInSubset.push(0xFF);
69 glyphToUnicode.push(0x1011);
70 glyphsInSubset.push(0x100);
71 glyphToUnicode.push(0x1012);
72 glyphsInSubset.push(0x101);
73 glyphToUnicode.push(0x1013);
74
75 SkDynamicMemoryWStream buffer;
76 subset.set(glyphsInSubset.begin(), glyphsInSubset.count());
edisonn@google.com26d2e042013-09-18 19:29:08 +000077 append_cmap_sections(glyphToUnicode, &subset, &buffer, 0, 0xFFFF);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000078
79 char expectedResult[] =
80"4 beginbfchar\n\
81<0003> <0020>\n\
82<0004> <0025>\n\
83<0008> <002F>\n\
84<0009> <0033>\n\
85endbfchar\n\
864 beginbfrange\n\
87<0005> <0007> <0027>\n\
edisonn@google.com26d2e042013-09-18 19:29:08 +000088<000B> <000D> <0035>\n\
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000089<00FE> <00FF> <1010>\n\
90<0100> <0101> <1012>\n\
91endbfrange\n";
92
93 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
94 buffer.getOffset()));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000095
edisonn@google.com26d2e042013-09-18 19:29:08 +000096 // Remove characters and ranges.
97 buffer.reset();
98
99 append_cmap_sections(glyphToUnicode, &subset, &buffer, 8, 0x00FF);
100
101 char expectedResultChop1[] =
102"2 beginbfchar\n\
103<0008> <002F>\n\
104<0009> <0033>\n\
105endbfchar\n\
1062 beginbfrange\n\
107<000B> <000D> <0035>\n\
108<00FE> <00FF> <1010>\n\
109endbfrange\n";
110
111 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1,
112 buffer.getOffset()));
113
114 // Remove characters from range to downdrade it to one char.
115 buffer.reset();
116
117 append_cmap_sections(glyphToUnicode, &subset, &buffer, 0x00D, 0x00FE);
118
119 char expectedResultChop2[] =
120"2 beginbfchar\n\
121<000D> <0037>\n\
122<00FE> <1010>\n\
123endbfchar\n";
124
125 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2,
126 buffer.getOffset()));
127
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000128 glyphToUnicode.reset();
129 glyphsInSubset.reset();
130 SkPDFGlyphSet subset2;
131
132 // Test mapping:
133 // I n s t a l
134 // Glyph id 2c 51 56 57 44 4f
135 // Unicode 49 6e 73 74 61 6c
136 for (size_t i = 0; i < 100; ++i) {
137 glyphToUnicode.push(i + 29);
138 }
139
140 glyphsInSubset.push(0x2C);
141 glyphsInSubset.push(0x44);
142 glyphsInSubset.push(0x4F);
143 glyphsInSubset.push(0x51);
144 glyphsInSubset.push(0x56);
145 glyphsInSubset.push(0x57);
146
147 SkDynamicMemoryWStream buffer2;
148 subset2.set(glyphsInSubset.begin(), glyphsInSubset.count());
edisonn@google.com26d2e042013-09-18 19:29:08 +0000149 append_cmap_sections(glyphToUnicode, &subset2, &buffer2, 0, 0xffff);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000150
151 char expectedResult2[] =
152"4 beginbfchar\n\
153<002C> <0049>\n\
154<0044> <0061>\n\
155<004F> <006C>\n\
156<0051> <006E>\n\
157endbfchar\n\
1581 beginbfrange\n\
159<0056> <0057> <0073>\n\
160endbfrange\n";
161
162 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
163 buffer2.getOffset()));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000164}
165
166#include "TestClassDef.h"
167DEFINE_TESTCLASS("ToUnicode", ToUnicodeTestClass, TestToUnicode)