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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 9 | |
| 10 | #ifdef SK_SUPPORT_PDF |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkData.h" |
| 13 | #include "include/core/SkStream.h" |
| 14 | #include "include/private/SkTo.h" |
| 15 | #include "src/pdf/SkPDFMakeToUnicodeCmap.h" |
| 16 | #include "src/utils/SkBitSet.h" |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 17 | |
Hal Canary | 3135598 | 2018-10-19 12:21:41 -0400 | [diff] [blame] | 18 | static constexpr SkGlyphID kMaximumGlyphIndex = UINT16_MAX; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 19 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 20 | static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 21 | const char* buffer, size_t len) { |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 22 | if (len != strlen(buffer)) { |
| 23 | return false; |
| 24 | } |
Mike Reed | 5adaf8b | 2016-12-15 13:02:33 -0500 | [diff] [blame] | 25 | |
| 26 | const size_t streamSize = stream.bytesWritten(); |
| 27 | |
| 28 | if (offset + len > streamSize) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | SkAutoTMalloc<char> data(streamSize); |
| 33 | stream.copyTo(data.get()); |
| 34 | return memcmp(data.get() + offset, buffer, len) == 0; |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 35 | } |
| 36 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 37 | DEF_TEST(SkPDF_ToUnicode, reporter) { |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 38 | SkTDArray<SkUnichar> glyphToUnicode; |
| 39 | SkTDArray<uint16_t> glyphsInSubset; |
Hal Canary | 3135598 | 2018-10-19 12:21:41 -0400 | [diff] [blame] | 40 | SkPDFGlyphUse subset(1, kMaximumGlyphIndex); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 41 | |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 42 | glyphToUnicode.push_back(0); // 0 |
| 43 | glyphToUnicode.push_back(0); // 1 |
| 44 | glyphToUnicode.push_back(0); // 2 |
| 45 | glyphsInSubset.push_back(3); |
| 46 | glyphToUnicode.push_back(0x20); // 3 |
| 47 | glyphsInSubset.push_back(4); |
| 48 | glyphToUnicode.push_back(0x25); // 4 |
| 49 | glyphsInSubset.push_back(5); |
| 50 | glyphToUnicode.push_back(0x27); // 5 |
| 51 | glyphsInSubset.push_back(6); |
| 52 | glyphToUnicode.push_back(0x28); // 6 |
| 53 | glyphsInSubset.push_back(7); |
| 54 | glyphToUnicode.push_back(0x29); // 7 |
| 55 | glyphsInSubset.push_back(8); |
| 56 | glyphToUnicode.push_back(0x2F); // 8 |
| 57 | glyphsInSubset.push_back(9); |
| 58 | glyphToUnicode.push_back(0x33); // 9 |
| 59 | glyphToUnicode.push_back(0); // 10 |
| 60 | glyphsInSubset.push_back(11); |
| 61 | glyphToUnicode.push_back(0x35); // 11 |
| 62 | glyphsInSubset.push_back(12); |
| 63 | glyphToUnicode.push_back(0x36); // 12 |
| 64 | glyphsInSubset.push_back(13); |
| 65 | glyphToUnicode.push_back(0x37); // 13 |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 66 | for (uint16_t i = 14; i < 0xFE; ++i) { |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 67 | glyphToUnicode.push_back(0); // Zero from index 0x9 to 0xFD |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 68 | } |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 69 | glyphsInSubset.push_back(0xFE); |
| 70 | glyphToUnicode.push_back(0x1010); |
| 71 | glyphsInSubset.push_back(0xFF); |
| 72 | glyphToUnicode.push_back(0x1011); |
| 73 | glyphsInSubset.push_back(0x100); |
| 74 | glyphToUnicode.push_back(0x1012); |
| 75 | glyphsInSubset.push_back(0x101); |
| 76 | glyphToUnicode.push_back(0x1013); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 77 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 78 | SkGlyphID lastGlyphID = SkToU16(glyphToUnicode.count() - 1); |
| 79 | |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 80 | SkDynamicMemoryWStream buffer; |
Hal Canary | 52514d5 | 2018-10-19 10:08:42 -0400 | [diff] [blame] | 81 | for (uint16_t v : glyphsInSubset) { |
| 82 | subset.set(v); |
| 83 | } |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 84 | SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 0, |
| 85 | SkTMin<SkGlyphID>(0xFFFF, lastGlyphID)); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 86 | |
| 87 | char expectedResult[] = |
| 88 | "4 beginbfchar\n\ |
| 89 | <0003> <0020>\n\ |
| 90 | <0004> <0025>\n\ |
| 91 | <0008> <002F>\n\ |
| 92 | <0009> <0033>\n\ |
| 93 | endbfchar\n\ |
| 94 | 4 beginbfrange\n\ |
| 95 | <0005> <0007> <0027>\n\ |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 96 | <000B> <000D> <0035>\n\ |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 97 | <00FE> <00FF> <1010>\n\ |
| 98 | <0100> <0101> <1012>\n\ |
| 99 | endbfrange\n"; |
| 100 | |
| 101 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 102 | buffer.bytesWritten())); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 103 | |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 104 | // Remove characters and ranges. |
| 105 | buffer.reset(); |
| 106 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 107 | SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 8, |
| 108 | SkTMin<SkGlyphID>(0x00FF, lastGlyphID)); |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 109 | |
| 110 | char expectedResultChop1[] = |
| 111 | "2 beginbfchar\n\ |
| 112 | <0008> <002F>\n\ |
| 113 | <0009> <0033>\n\ |
| 114 | endbfchar\n\ |
| 115 | 2 beginbfrange\n\ |
| 116 | <000B> <000D> <0035>\n\ |
| 117 | <00FE> <00FF> <1010>\n\ |
| 118 | endbfrange\n"; |
| 119 | |
| 120 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1, |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 121 | buffer.bytesWritten())); |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 122 | |
| 123 | // Remove characters from range to downdrade it to one char. |
| 124 | buffer.reset(); |
| 125 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 126 | SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 0x00D, |
| 127 | SkTMin<SkGlyphID>(0x00FE, lastGlyphID)); |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 128 | |
| 129 | char expectedResultChop2[] = |
| 130 | "2 beginbfchar\n\ |
| 131 | <000D> <0037>\n\ |
| 132 | <00FE> <1010>\n\ |
| 133 | endbfchar\n"; |
| 134 | |
| 135 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2, |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 136 | buffer.bytesWritten())); |
edisonn@google.com | 26d2e04 | 2013-09-18 19:29:08 +0000 | [diff] [blame] | 137 | |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 138 | buffer.reset(); |
| 139 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 140 | SkPDFAppendCmapSections(&glyphToUnicode[0], nullptr, &buffer, false, 0xFC, |
| 141 | SkTMin<SkGlyphID>(0x110, lastGlyphID)); |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 142 | |
| 143 | char expectedResultSingleBytes[] = |
| 144 | "2 beginbfchar\n\ |
halcanary | 3d01c62 | 2016-08-31 12:52:35 -0700 | [diff] [blame] | 145 | <01> <0000>\n\ |
| 146 | <02> <0000>\n\ |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 147 | endbfchar\n\ |
| 148 | 1 beginbfrange\n\ |
halcanary | 3d01c62 | 2016-08-31 12:52:35 -0700 | [diff] [blame] | 149 | <03> <06> <1010>\n\ |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 150 | endbfrange\n"; |
| 151 | |
| 152 | REPORTER_ASSERT(reporter, stream_equals(buffer, 0, |
| 153 | expectedResultSingleBytes, |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 154 | buffer.bytesWritten())); |
commit-bot@chromium.org | 1236d8e | 2013-12-11 18:47:11 +0000 | [diff] [blame] | 155 | |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 156 | glyphToUnicode.reset(); |
| 157 | glyphsInSubset.reset(); |
Hal Canary | 3135598 | 2018-10-19 12:21:41 -0400 | [diff] [blame] | 158 | SkPDFGlyphUse subset2(1, kMaximumGlyphIndex); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 159 | |
| 160 | // Test mapping: |
| 161 | // I n s t a l |
| 162 | // Glyph id 2c 51 56 57 44 4f |
| 163 | // Unicode 49 6e 73 74 61 6c |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 164 | for (SkUnichar i = 0; i < 100; ++i) { |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 165 | glyphToUnicode.push_back(i + 29); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 166 | } |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 167 | lastGlyphID = SkToU16(glyphToUnicode.count() - 1); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 168 | |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 169 | glyphsInSubset.push_back(0x2C); |
| 170 | glyphsInSubset.push_back(0x44); |
| 171 | glyphsInSubset.push_back(0x4F); |
| 172 | glyphsInSubset.push_back(0x51); |
| 173 | glyphsInSubset.push_back(0x56); |
| 174 | glyphsInSubset.push_back(0x57); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 175 | |
| 176 | SkDynamicMemoryWStream buffer2; |
Hal Canary | 52514d5 | 2018-10-19 10:08:42 -0400 | [diff] [blame] | 177 | for (uint16_t v : glyphsInSubset) { |
| 178 | subset2.set(v); |
| 179 | } |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 180 | SkPDFAppendCmapSections(&glyphToUnicode[0], &subset2, &buffer2, true, 0, |
| 181 | SkTMin<SkGlyphID>(0xFFFF, lastGlyphID)); |
vandebo@chromium.org | 9ad3599 | 2012-01-03 18:35:39 +0000 | [diff] [blame] | 182 | |
| 183 | char expectedResult2[] = |
| 184 | "4 beginbfchar\n\ |
| 185 | <002C> <0049>\n\ |
| 186 | <0044> <0061>\n\ |
| 187 | <004F> <006C>\n\ |
| 188 | <0051> <006E>\n\ |
| 189 | endbfchar\n\ |
| 190 | 1 beginbfrange\n\ |
| 191 | <0056> <0057> <0073>\n\ |
| 192 | endbfrange\n"; |
| 193 | |
| 194 | REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2, |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 195 | buffer2.bytesWritten())); |
vandebo@chromium.org | 04c643b | 2011-08-08 22:33:05 +0000 | [diff] [blame] | 196 | } |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 197 | |
| 198 | #endif |