blob: 2aeedf0330f2fecb1da82d50213476ad93575d6d [file] [log] [blame]
vandebo@chromium.org04c643b2011-08-08 22:33:05 +00001/*
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 Canary43fb7a02016-12-30 13:09:03 -05008#include "Test.h"
9
10#ifdef SK_SUPPORT_PDF
11
halcanary530032a2016-08-18 14:22:52 -070012#include "SkBitSet.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000013#include "SkData.h"
halcanary8eccc302016-08-09 13:04:34 -070014#include "SkPDFMakeToUnicodeCmap.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000015#include "SkStream.h"
16
halcanary530032a2016-08-18 14:22:52 -070017static const int kMaximumGlyphCount = SK_MaxU16 + 1;
18
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000019static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000020 const char* buffer, size_t len) {
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000021 if (len != strlen(buffer)) {
22 return false;
23 }
Mike Reed5adaf8b2016-12-15 13:02:33 -050024
25 const size_t streamSize = stream.bytesWritten();
26
27 if (offset + len > streamSize) {
28 return false;
29 }
30
31 SkAutoTMalloc<char> data(streamSize);
32 stream.copyTo(data.get());
33 return memcmp(data.get() + offset, buffer, len) == 0;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000034}
35
halcanary57f744e2016-09-09 11:41:59 -070036DEF_TEST(SkPDF_ToUnicode, reporter) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000037 SkTDArray<SkUnichar> glyphToUnicode;
38 SkTDArray<uint16_t> glyphsInSubset;
halcanary530032a2016-08-18 14:22:52 -070039 SkBitSet subset(kMaximumGlyphCount);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000040
41 glyphToUnicode.push(0); // 0
42 glyphToUnicode.push(0); // 1
43 glyphToUnicode.push(0); // 2
44 glyphsInSubset.push(3);
45 glyphToUnicode.push(0x20); // 3
46 glyphsInSubset.push(4);
47 glyphToUnicode.push(0x25); // 4
48 glyphsInSubset.push(5);
49 glyphToUnicode.push(0x27); // 5
50 glyphsInSubset.push(6);
51 glyphToUnicode.push(0x28); // 6
52 glyphsInSubset.push(7);
53 glyphToUnicode.push(0x29); // 7
54 glyphsInSubset.push(8);
55 glyphToUnicode.push(0x2F); // 8
56 glyphsInSubset.push(9);
57 glyphToUnicode.push(0x33); // 9
58 glyphToUnicode.push(0); // 10
59 glyphsInSubset.push(11);
60 glyphToUnicode.push(0x35); // 11
61 glyphsInSubset.push(12);
62 glyphToUnicode.push(0x36); // 12
edisonn@google.com26d2e042013-09-18 19:29:08 +000063 glyphsInSubset.push(13);
64 glyphToUnicode.push(0x37); // 13
65 for (uint16_t i = 14; i < 0xFE; ++i) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000066 glyphToUnicode.push(0); // Zero from index 0x9 to 0xFD
67 }
68 glyphsInSubset.push(0xFE);
69 glyphToUnicode.push(0x1010);
70 glyphsInSubset.push(0xFF);
71 glyphToUnicode.push(0x1011);
72 glyphsInSubset.push(0x100);
73 glyphToUnicode.push(0x1012);
74 glyphsInSubset.push(0x101);
75 glyphToUnicode.push(0x1013);
76
Hal Canary46cc3da2018-05-09 11:50:34 -040077 SkGlyphID lastGlyphID = SkToU16(glyphToUnicode.count() - 1);
78
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000079 SkDynamicMemoryWStream buffer;
halcanary530032a2016-08-18 14:22:52 -070080 subset.setAll(glyphsInSubset.begin(), glyphsInSubset.count());
Hal Canary46cc3da2018-05-09 11:50:34 -040081 SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 0,
82 SkTMin<SkGlyphID>(0xFFFF, lastGlyphID));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000083
84 char expectedResult[] =
85"4 beginbfchar\n\
86<0003> <0020>\n\
87<0004> <0025>\n\
88<0008> <002F>\n\
89<0009> <0033>\n\
90endbfchar\n\
914 beginbfrange\n\
92<0005> <0007> <0027>\n\
edisonn@google.com26d2e042013-09-18 19:29:08 +000093<000B> <000D> <0035>\n\
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000094<00FE> <00FF> <1010>\n\
95<0100> <0101> <1012>\n\
96endbfrange\n";
97
98 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
Ben Wagner884300d2016-12-16 16:51:41 +000099 buffer.bytesWritten()));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000100
edisonn@google.com26d2e042013-09-18 19:29:08 +0000101 // Remove characters and ranges.
102 buffer.reset();
103
Hal Canary46cc3da2018-05-09 11:50:34 -0400104 SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 8,
105 SkTMin<SkGlyphID>(0x00FF, lastGlyphID));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000106
107 char expectedResultChop1[] =
108"2 beginbfchar\n\
109<0008> <002F>\n\
110<0009> <0033>\n\
111endbfchar\n\
1122 beginbfrange\n\
113<000B> <000D> <0035>\n\
114<00FE> <00FF> <1010>\n\
115endbfrange\n";
116
117 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1,
Ben Wagner884300d2016-12-16 16:51:41 +0000118 buffer.bytesWritten()));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000119
120 // Remove characters from range to downdrade it to one char.
121 buffer.reset();
122
Hal Canary46cc3da2018-05-09 11:50:34 -0400123 SkPDFAppendCmapSections(&glyphToUnicode[0], &subset, &buffer, true, 0x00D,
124 SkTMin<SkGlyphID>(0x00FE, lastGlyphID));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000125
126 char expectedResultChop2[] =
127"2 beginbfchar\n\
128<000D> <0037>\n\
129<00FE> <1010>\n\
130endbfchar\n";
131
132 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2,
Ben Wagner884300d2016-12-16 16:51:41 +0000133 buffer.bytesWritten()));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000134
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000135 buffer.reset();
136
Hal Canary46cc3da2018-05-09 11:50:34 -0400137 SkPDFAppendCmapSections(&glyphToUnicode[0], nullptr, &buffer, false, 0xFC,
138 SkTMin<SkGlyphID>(0x110, lastGlyphID));
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000139
140 char expectedResultSingleBytes[] =
141"2 beginbfchar\n\
halcanary3d01c622016-08-31 12:52:35 -0700142<01> <0000>\n\
143<02> <0000>\n\
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000144endbfchar\n\
1451 beginbfrange\n\
halcanary3d01c622016-08-31 12:52:35 -0700146<03> <06> <1010>\n\
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000147endbfrange\n";
148
149 REPORTER_ASSERT(reporter, stream_equals(buffer, 0,
150 expectedResultSingleBytes,
Ben Wagner884300d2016-12-16 16:51:41 +0000151 buffer.bytesWritten()));
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000152
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000153 glyphToUnicode.reset();
154 glyphsInSubset.reset();
halcanary530032a2016-08-18 14:22:52 -0700155 SkBitSet subset2(kMaximumGlyphCount);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000156
157 // Test mapping:
158 // I n s t a l
159 // Glyph id 2c 51 56 57 44 4f
160 // Unicode 49 6e 73 74 61 6c
bsalomon98806072014-12-12 15:11:17 -0800161 for (SkUnichar i = 0; i < 100; ++i) {
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000162 glyphToUnicode.push(i + 29);
163 }
Hal Canary46cc3da2018-05-09 11:50:34 -0400164 lastGlyphID = SkToU16(glyphToUnicode.count() - 1);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000165
166 glyphsInSubset.push(0x2C);
167 glyphsInSubset.push(0x44);
168 glyphsInSubset.push(0x4F);
169 glyphsInSubset.push(0x51);
170 glyphsInSubset.push(0x56);
171 glyphsInSubset.push(0x57);
172
173 SkDynamicMemoryWStream buffer2;
halcanary530032a2016-08-18 14:22:52 -0700174 subset2.setAll(glyphsInSubset.begin(), glyphsInSubset.count());
Hal Canary46cc3da2018-05-09 11:50:34 -0400175 SkPDFAppendCmapSections(&glyphToUnicode[0], &subset2, &buffer2, true, 0,
176 SkTMin<SkGlyphID>(0xFFFF, lastGlyphID));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000177
178 char expectedResult2[] =
179"4 beginbfchar\n\
180<002C> <0049>\n\
181<0044> <0061>\n\
182<004F> <006C>\n\
183<0051> <006E>\n\
184endbfchar\n\
1851 beginbfrange\n\
186<0056> <0057> <0073>\n\
187endbfrange\n";
188
189 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
Ben Wagner884300d2016-12-16 16:51:41 +0000190 buffer2.bytesWritten()));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000191}
Hal Canary43fb7a02016-12-30 13:09:03 -0500192
193#endif