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 | |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 8 | #include "SkBitSet.h" |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 10 | #include "SkPDFMakeToUnicodeCmap.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 | |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 14 | static const int kMaximumGlyphCount = SK_MaxU16 + 1; |
| 15 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 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) { |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 18 | sk_sp<SkData> data = stream.snapshotAsData(); |
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 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 28 | DEF_TEST(SkPDF_ToUnicode, reporter) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 29 | SkTDArray<SkUnichar> glyphToUnicode; |
| 30 | SkTDArray<uint16_t> glyphsInSubset; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 31 | SkBitSet subset(kMaximumGlyphCount); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 32 | |
| 33 | glyphToUnicode.push(0); // 0 |
| 34 | glyphToUnicode.push(0); // 1 |
| 35 | glyphToUnicode.push(0); // 2 |
| 36 | glyphsInSubset.push(3); |
| 37 | glyphToUnicode.push(0x20); // 3 |
| 38 | glyphsInSubset.push(4); |
| 39 | glyphToUnicode.push(0x25); // 4 |
| 40 | glyphsInSubset.push(5); |
| 41 | glyphToUnicode.push(0x27); // 5 |
| 42 | glyphsInSubset.push(6); |
| 43 | glyphToUnicode.push(0x28); // 6 |
| 44 | glyphsInSubset.push(7); |
| 45 | glyphToUnicode.push(0x29); // 7 |
| 46 | glyphsInSubset.push(8); |
| 47 | glyphToUnicode.push(0x2F); // 8 |
| 48 | glyphsInSubset.push(9); |
| 49 | glyphToUnicode.push(0x33); // 9 |
| 50 | glyphToUnicode.push(0); // 10 |
| 51 | glyphsInSubset.push(11); |
| 52 | glyphToUnicode.push(0x35); // 11 |
| 53 | glyphsInSubset.push(12); |
| 54 | glyphToUnicode.push(0x36); // 12 |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 55 | glyphsInSubset.push(13); |
| 56 | glyphToUnicode.push(0x37); // 13 |
| 57 | for (uint16_t i = 14; i < 0xFE; ++i) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 58 | glyphToUnicode.push(0); // Zero from index 0x9 to 0xFD |
| 59 | } |
| 60 | glyphsInSubset.push(0xFE); |
| 61 | glyphToUnicode.push(0x1010); |
| 62 | glyphsInSubset.push(0xFF); |
| 63 | glyphToUnicode.push(0x1011); |
| 64 | glyphsInSubset.push(0x100); |
| 65 | glyphToUnicode.push(0x1012); |
| 66 | glyphsInSubset.push(0x101); |
| 67 | glyphToUnicode.push(0x1013); |
| 68 | |
| 69 | SkDynamicMemoryWStream buffer; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 70 | subset.setAll(glyphsInSubset.begin(), glyphsInSubset.count()); |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 71 | SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 0, 0xFFFF); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 72 | |
| 73 | char expectedResult[] = |
| 74 | "4 beginbfchar\n\ |
| 75 | <0003> <0020>\n\ |
| 76 | <0004> <0025>\n\ |
| 77 | <0008> <002F>\n\ |
| 78 | <0009> <0033>\n\ |
| 79 | endbfchar\n\ |
| 80 | 4 beginbfrange\n\ |
| 81 | <0005> <0007> <0027>\n\ |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 82 | <000B> <000D> <0035>\n\ |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 83 | <00FE> <00FF> <1010>\n\ |
| 84 | <0100> <0101> <1012>\n\ |
| 85 | endbfrange\n"; |
| 86 | |
| 87 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, |
| 88 | buffer.getOffset())); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 89 | |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 90 | // Remove characters and ranges. |
| 91 | buffer.reset(); |
| 92 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 93 | SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 8, 0x00FF); |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 94 | |
| 95 | char expectedResultChop1[] = |
| 96 | "2 beginbfchar\n\ |
| 97 | <0008> <002F>\n\ |
| 98 | <0009> <0033>\n\ |
| 99 | endbfchar\n\ |
| 100 | 2 beginbfrange\n\ |
| 101 | <000B> <000D> <0035>\n\ |
| 102 | <00FE> <00FF> <1010>\n\ |
| 103 | endbfrange\n"; |
| 104 | |
| 105 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1, |
| 106 | buffer.getOffset())); |
| 107 | |
| 108 | // Remove characters from range to downdrade it to one char. |
| 109 | buffer.reset(); |
| 110 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 111 | SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 0x00D, 0x00FE); |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 112 | |
| 113 | char expectedResultChop2[] = |
| 114 | "2 beginbfchar\n\ |
| 115 | <000D> <0037>\n\ |
| 116 | <00FE> <1010>\n\ |
| 117 | endbfchar\n"; |
| 118 | |
| 119 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2, |
| 120 | buffer.getOffset())); |
| 121 | |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 122 | buffer.reset(); |
| 123 | |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 124 | SkPDFAppendCmapSections(glyphToUnicode, nullptr, &buffer, false, 0xFC, 0x110); |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 125 | |
| 126 | char expectedResultSingleBytes[] = |
| 127 | "2 beginbfchar\n\ |
halcanary | 3d01c62 | 2016-08-31 12:52:35 -0700 | [diff] [blame] | 128 | <01> <0000>\n\ |
| 129 | <02> <0000>\n\ |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 130 | endbfchar\n\ |
| 131 | 1 beginbfrange\n\ |
halcanary | 3d01c62 | 2016-08-31 12:52:35 -0700 | [diff] [blame] | 132 | <03> <06> <1010>\n\ |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 133 | endbfrange\n"; |
| 134 | |
| 135 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, |
| 136 | expectedResultSingleBytes, |
| 137 | buffer.getOffset())); |
| 138 | |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 139 | glyphToUnicode.reset(); |
| 140 | glyphsInSubset.reset(); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 141 | SkBitSet subset2(kMaximumGlyphCount); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 142 | |
| 143 | // Test mapping: |
| 144 | // I n s t a l |
| 145 | // Glyph id 2c 51 56 57 44 4f |
| 146 | // Unicode 49 6e 73 74 61 6c |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 147 | for (SkUnichar i = 0; i < 100; ++i) { |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 148 | glyphToUnicode.push(i + 29); |
| 149 | } |
| 150 | |
| 151 | glyphsInSubset.push(0x2C); |
| 152 | glyphsInSubset.push(0x44); |
| 153 | glyphsInSubset.push(0x4F); |
| 154 | glyphsInSubset.push(0x51); |
| 155 | glyphsInSubset.push(0x56); |
| 156 | glyphsInSubset.push(0x57); |
| 157 | |
| 158 | SkDynamicMemoryWStream buffer2; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 159 | subset2.setAll(glyphsInSubset.begin(), glyphsInSubset.count()); |
halcanary | 8eccc30 | 2016-08-09 13:04:34 -0700 | [diff] [blame] | 160 | SkPDFAppendCmapSections(glyphToUnicode, &subset2, &buffer2, true, 0, 0xffff); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 161 | |
| 162 | char expectedResult2[] = |
| 163 | "4 beginbfchar\n\ |
| 164 | <002C> <0049>\n\ |
| 165 | <0044> <0061>\n\ |
| 166 | <004F> <006C>\n\ |
| 167 | <0051> <006E>\n\ |
| 168 | endbfchar\n\ |
| 169 | 1 beginbfrange\n\ |
| 170 | <0056> <0057> <0073>\n\ |
| 171 | endbfrange\n"; |
| 172 | |
| 173 | REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2, |
| 174 | buffer2.getOffset())); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 175 | } |