vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 1 | |
| 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.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 10 | #include "Test.h" |
| 11 | #include "SkData.h" |
| 12 | #include "SkPDFTypes.h" |
| 13 | #include "SkPDFFont.h" |
| 14 | #include "SkStream.h" |
| 15 | |
| 16 | static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 17 | const char* buffer, size_t len) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 18 | SkAutoDataUnref data(stream.copyToData()); |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 19 | if (offset + len > data->size()) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 20 | return false; |
| 21 | } |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 22 | if (len != strlen(buffer)) { |
| 23 | return false; |
| 24 | } |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 25 | return memcmp(data->bytes() + offset, buffer, len) == 0; |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode, |
| 29 | const SkPDFGlyphSet* subset, |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 30 | SkDynamicMemoryWStream* cmap, |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame^] | 31 | bool multiByteGlyphs, |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 32 | uint16_t firstGlypthID, |
| 33 | uint16_t lastGlypthID); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 34 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 35 | static 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.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 62 | glyphsInSubset.push(13); |
| 63 | glyphToUnicode.push(0x37); // 13 |
| 64 | for (uint16_t i = 14; i < 0xFE; ++i) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 65 | 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.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame^] | 78 | append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 0, 0xFFFF); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 79 | |
| 80 | char expectedResult[] = |
| 81 | "4 beginbfchar\n\ |
| 82 | <0003> <0020>\n\ |
| 83 | <0004> <0025>\n\ |
| 84 | <0008> <002F>\n\ |
| 85 | <0009> <0033>\n\ |
| 86 | endbfchar\n\ |
| 87 | 4 beginbfrange\n\ |
| 88 | <0005> <0007> <0027>\n\ |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 89 | <000B> <000D> <0035>\n\ |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 90 | <00FE> <00FF> <1010>\n\ |
| 91 | <0100> <0101> <1012>\n\ |
| 92 | endbfrange\n"; |
| 93 | |
| 94 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, |
| 95 | buffer.getOffset())); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 96 | |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 97 | // Remove characters and ranges. |
| 98 | buffer.reset(); |
| 99 | |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame^] | 100 | append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 8, 0x00FF); |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 101 | |
| 102 | char expectedResultChop1[] = |
| 103 | "2 beginbfchar\n\ |
| 104 | <0008> <002F>\n\ |
| 105 | <0009> <0033>\n\ |
| 106 | endbfchar\n\ |
| 107 | 2 beginbfrange\n\ |
| 108 | <000B> <000D> <0035>\n\ |
| 109 | <00FE> <00FF> <1010>\n\ |
| 110 | endbfrange\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.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame^] | 118 | append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 0x00D, 0x00FE); |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 119 | |
| 120 | char expectedResultChop2[] = |
| 121 | "2 beginbfchar\n\ |
| 122 | <000D> <0037>\n\ |
| 123 | <00FE> <1010>\n\ |
| 124 | endbfchar\n"; |
| 125 | |
| 126 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2, |
| 127 | buffer.getOffset())); |
| 128 | |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame^] | 129 | 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\ |
| 137 | endbfchar\n\ |
| 138 | 1 beginbfrange\n\ |
| 139 | <0003> <0006> <1010>\n\ |
| 140 | endbfrange\n"; |
| 141 | |
| 142 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, |
| 143 | expectedResultSingleBytes, |
| 144 | buffer.getOffset())); |
| 145 | |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 146 | 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.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame^] | 167 | append_cmap_sections(glyphToUnicode, &subset2, &buffer2, true, 0, 0xffff); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 168 | |
| 169 | char expectedResult2[] = |
| 170 | "4 beginbfchar\n\ |
| 171 | <002C> <0049>\n\ |
| 172 | <0044> <0061>\n\ |
| 173 | <004F> <006C>\n\ |
| 174 | <0051> <006E>\n\ |
| 175 | endbfchar\n\ |
| 176 | 1 beginbfrange\n\ |
| 177 | <0056> <0057> <0073>\n\ |
| 178 | endbfrange\n"; |
| 179 | |
| 180 | REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2, |
| 181 | buffer2.getOffset())); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | #include "TestClassDef.h" |
| 185 | DEFINE_TESTCLASS("ToUnicode", ToUnicodeTestClass, TestToUnicode) |