OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 The Android Open Source Project | 3 * Copyright 2010 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "Test.h" | 10 #include "Test.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkPDFTypes.h" | 12 #include "SkPDFTypes.h" |
13 #include "SkPDFFont.h" | 13 #include "SkPDFFont.h" |
14 #include "SkStream.h" | 14 #include "SkStream.h" |
15 | 15 |
16 static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, | 16 static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, |
17 const char* buffer, size_t len) { | 17 const char* buffer, size_t len) { |
18 SkAutoDataUnref data(stream.copyToData()); | 18 SkAutoDataUnref data(stream.copyToData()); |
19 if (offset + len > data->size()) { | 19 if (offset + len > data->size()) { |
20 return false; | 20 return false; |
21 } | 21 } |
22 if (len != strlen(buffer)) { | 22 if (len != strlen(buffer)) { |
23 return false; | 23 return false; |
24 } | 24 } |
25 return memcmp(data->bytes() + offset, buffer, len) == 0; | 25 return memcmp(data->bytes() + offset, buffer, len) == 0; |
26 } | 26 } |
27 | 27 |
28 void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode, | 28 void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode, |
29 const SkPDFGlyphSet* subset, | 29 const SkPDFGlyphSet* subset, |
30 SkDynamicMemoryWStream* cmap); | 30 SkDynamicMemoryWStream* cmap, |
| 31 uint16_t firstGlypthID, |
| 32 uint16_t lastGlypthID); |
31 | 33 |
32 static void TestToUnicode(skiatest::Reporter* reporter) { | 34 static void TestToUnicode(skiatest::Reporter* reporter) { |
33 SkTDArray<SkUnichar> glyphToUnicode; | 35 SkTDArray<SkUnichar> glyphToUnicode; |
34 SkTDArray<uint16_t> glyphsInSubset; | 36 SkTDArray<uint16_t> glyphsInSubset; |
35 SkPDFGlyphSet subset; | 37 SkPDFGlyphSet subset; |
36 | 38 |
37 glyphToUnicode.push(0); // 0 | 39 glyphToUnicode.push(0); // 0 |
38 glyphToUnicode.push(0); // 1 | 40 glyphToUnicode.push(0); // 1 |
39 glyphToUnicode.push(0); // 2 | 41 glyphToUnicode.push(0); // 2 |
40 glyphsInSubset.push(3); | 42 glyphsInSubset.push(3); |
41 glyphToUnicode.push(0x20); // 3 | 43 glyphToUnicode.push(0x20); // 3 |
42 glyphsInSubset.push(4); | 44 glyphsInSubset.push(4); |
43 glyphToUnicode.push(0x25); // 4 | 45 glyphToUnicode.push(0x25); // 4 |
44 glyphsInSubset.push(5); | 46 glyphsInSubset.push(5); |
45 glyphToUnicode.push(0x27); // 5 | 47 glyphToUnicode.push(0x27); // 5 |
46 glyphsInSubset.push(6); | 48 glyphsInSubset.push(6); |
47 glyphToUnicode.push(0x28); // 6 | 49 glyphToUnicode.push(0x28); // 6 |
48 glyphsInSubset.push(7); | 50 glyphsInSubset.push(7); |
49 glyphToUnicode.push(0x29); // 7 | 51 glyphToUnicode.push(0x29); // 7 |
50 glyphsInSubset.push(8); | 52 glyphsInSubset.push(8); |
51 glyphToUnicode.push(0x2F); // 8 | 53 glyphToUnicode.push(0x2F); // 8 |
52 glyphsInSubset.push(9); | 54 glyphsInSubset.push(9); |
53 glyphToUnicode.push(0x33); // 9 | 55 glyphToUnicode.push(0x33); // 9 |
54 glyphToUnicode.push(0); // 10 | 56 glyphToUnicode.push(0); // 10 |
55 glyphsInSubset.push(11); | 57 glyphsInSubset.push(11); |
56 glyphToUnicode.push(0x35); // 11 | 58 glyphToUnicode.push(0x35); // 11 |
57 glyphsInSubset.push(12); | 59 glyphsInSubset.push(12); |
58 glyphToUnicode.push(0x36); // 12 | 60 glyphToUnicode.push(0x36); // 12 |
59 for (uint16_t i = 13; i < 0xFE; ++i) { | 61 glyphsInSubset.push(13); |
| 62 glyphToUnicode.push(0x37); // 13 |
| 63 for (uint16_t i = 14; i < 0xFE; ++i) { |
60 glyphToUnicode.push(0); // Zero from index 0x9 to 0xFD | 64 glyphToUnicode.push(0); // Zero from index 0x9 to 0xFD |
61 } | 65 } |
62 glyphsInSubset.push(0xFE); | 66 glyphsInSubset.push(0xFE); |
63 glyphToUnicode.push(0x1010); | 67 glyphToUnicode.push(0x1010); |
64 glyphsInSubset.push(0xFF); | 68 glyphsInSubset.push(0xFF); |
65 glyphToUnicode.push(0x1011); | 69 glyphToUnicode.push(0x1011); |
66 glyphsInSubset.push(0x100); | 70 glyphsInSubset.push(0x100); |
67 glyphToUnicode.push(0x1012); | 71 glyphToUnicode.push(0x1012); |
68 glyphsInSubset.push(0x101); | 72 glyphsInSubset.push(0x101); |
69 glyphToUnicode.push(0x1013); | 73 glyphToUnicode.push(0x1013); |
70 | 74 |
71 SkDynamicMemoryWStream buffer; | 75 SkDynamicMemoryWStream buffer; |
72 subset.set(glyphsInSubset.begin(), glyphsInSubset.count()); | 76 subset.set(glyphsInSubset.begin(), glyphsInSubset.count()); |
73 append_cmap_sections(glyphToUnicode, &subset, &buffer); | 77 append_cmap_sections(glyphToUnicode, &subset, &buffer, 0, 0xFFFF); |
74 | 78 |
75 char expectedResult[] = | 79 char expectedResult[] = |
76 "4 beginbfchar\n\ | 80 "4 beginbfchar\n\ |
77 <0003> <0020>\n\ | 81 <0003> <0020>\n\ |
78 <0004> <0025>\n\ | 82 <0004> <0025>\n\ |
79 <0008> <002F>\n\ | 83 <0008> <002F>\n\ |
80 <0009> <0033>\n\ | 84 <0009> <0033>\n\ |
81 endbfchar\n\ | 85 endbfchar\n\ |
82 4 beginbfrange\n\ | 86 4 beginbfrange\n\ |
83 <0005> <0007> <0027>\n\ | 87 <0005> <0007> <0027>\n\ |
84 <000B> <000C> <0035>\n\ | 88 <000B> <000D> <0035>\n\ |
85 <00FE> <00FF> <1010>\n\ | 89 <00FE> <00FF> <1010>\n\ |
86 <0100> <0101> <1012>\n\ | 90 <0100> <0101> <1012>\n\ |
87 endbfrange\n"; | 91 endbfrange\n"; |
88 | 92 |
89 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, | 93 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, |
90 buffer.getOffset())); | 94 buffer.getOffset())); |
91 | 95 |
| 96 // 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\ |
| 105 endbfchar\n\ |
| 106 2 beginbfrange\n\ |
| 107 <000B> <000D> <0035>\n\ |
| 108 <00FE> <00FF> <1010>\n\ |
| 109 endbfrange\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\ |
| 123 endbfchar\n"; |
| 124 |
| 125 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2, |
| 126 buffer.getOffset())); |
| 127 |
92 glyphToUnicode.reset(); | 128 glyphToUnicode.reset(); |
93 glyphsInSubset.reset(); | 129 glyphsInSubset.reset(); |
94 SkPDFGlyphSet subset2; | 130 SkPDFGlyphSet subset2; |
95 | 131 |
96 // Test mapping: | 132 // Test mapping: |
97 // I n s t a l | 133 // I n s t a l |
98 // Glyph id 2c 51 56 57 44 4f | 134 // Glyph id 2c 51 56 57 44 4f |
99 // Unicode 49 6e 73 74 61 6c | 135 // Unicode 49 6e 73 74 61 6c |
100 for (size_t i = 0; i < 100; ++i) { | 136 for (size_t i = 0; i < 100; ++i) { |
101 glyphToUnicode.push(i + 29); | 137 glyphToUnicode.push(i + 29); |
102 } | 138 } |
103 | 139 |
104 glyphsInSubset.push(0x2C); | 140 glyphsInSubset.push(0x2C); |
105 glyphsInSubset.push(0x44); | 141 glyphsInSubset.push(0x44); |
106 glyphsInSubset.push(0x4F); | 142 glyphsInSubset.push(0x4F); |
107 glyphsInSubset.push(0x51); | 143 glyphsInSubset.push(0x51); |
108 glyphsInSubset.push(0x56); | 144 glyphsInSubset.push(0x56); |
109 glyphsInSubset.push(0x57); | 145 glyphsInSubset.push(0x57); |
110 | 146 |
111 SkDynamicMemoryWStream buffer2; | 147 SkDynamicMemoryWStream buffer2; |
112 subset2.set(glyphsInSubset.begin(), glyphsInSubset.count()); | 148 subset2.set(glyphsInSubset.begin(), glyphsInSubset.count()); |
113 append_cmap_sections(glyphToUnicode, &subset2, &buffer2); | 149 append_cmap_sections(glyphToUnicode, &subset2, &buffer2, 0, 0xffff); |
114 | 150 |
115 char expectedResult2[] = | 151 char expectedResult2[] = |
116 "4 beginbfchar\n\ | 152 "4 beginbfchar\n\ |
117 <002C> <0049>\n\ | 153 <002C> <0049>\n\ |
118 <0044> <0061>\n\ | 154 <0044> <0061>\n\ |
119 <004F> <006C>\n\ | 155 <004F> <006C>\n\ |
120 <0051> <006E>\n\ | 156 <0051> <006E>\n\ |
121 endbfchar\n\ | 157 endbfchar\n\ |
122 1 beginbfrange\n\ | 158 1 beginbfrange\n\ |
123 <0056> <0057> <0073>\n\ | 159 <0056> <0057> <0073>\n\ |
124 endbfrange\n"; | 160 endbfrange\n"; |
125 | 161 |
126 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2, | 162 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2, |
127 buffer2.getOffset())); | 163 buffer2.getOffset())); |
128 } | 164 } |
129 | 165 |
130 #include "TestClassDef.h" | 166 #include "TestClassDef.h" |
131 DEFINE_TESTCLASS("ToUnicode", ToUnicodeTestClass, TestToUnicode) | 167 DEFINE_TESTCLASS("ToUnicode", ToUnicodeTestClass, TestToUnicode) |
OLD | NEW |