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