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