blob: 0d87cd7df28afd833e6359e4de5e79dbdb28d893 [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
halcanary530032a2016-08-18 14:22:52 -07008#include "SkBitSet.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +00009#include "SkData.h"
halcanary8eccc302016-08-09 13:04:34 -070010#include "SkPDFMakeToUnicodeCmap.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000011#include "SkStream.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.h"
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000013
halcanary530032a2016-08-18 14:22:52 -070014static const int kMaximumGlyphCount = SK_MaxU16 + 1;
15
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000016static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000017 const char* buffer, size_t len) {
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000018 if (len != strlen(buffer)) {
19 return false;
20 }
Mike Reed5adaf8b2016-12-15 13:02:33 -050021
22 const size_t streamSize = stream.bytesWritten();
23
24 if (offset + len > streamSize) {
25 return false;
26 }
27
28 SkAutoTMalloc<char> data(streamSize);
29 stream.copyTo(data.get());
30 return memcmp(data.get() + offset, buffer, len) == 0;
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000031}
32
halcanary57f744e2016-09-09 11:41:59 -070033DEF_TEST(SkPDF_ToUnicode, reporter) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000034 SkTDArray<SkUnichar> glyphToUnicode;
35 SkTDArray<uint16_t> glyphsInSubset;
halcanary530032a2016-08-18 14:22:52 -070036 SkBitSet subset(kMaximumGlyphCount);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000037
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.com26d2e042013-09-18 19:29:08 +000060 glyphsInSubset.push(13);
61 glyphToUnicode.push(0x37); // 13
62 for (uint16_t i = 14; i < 0xFE; ++i) {
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000063 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;
halcanary530032a2016-08-18 14:22:52 -070075 subset.setAll(glyphsInSubset.begin(), glyphsInSubset.count());
halcanary8eccc302016-08-09 13:04:34 -070076 SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 0, 0xFFFF);
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000077
78 char expectedResult[] =
79"4 beginbfchar\n\
80<0003> <0020>\n\
81<0004> <0025>\n\
82<0008> <002F>\n\
83<0009> <0033>\n\
84endbfchar\n\
854 beginbfrange\n\
86<0005> <0007> <0027>\n\
edisonn@google.com26d2e042013-09-18 19:29:08 +000087<000B> <000D> <0035>\n\
vandebo@chromium.org04c643b2011-08-08 22:33:05 +000088<00FE> <00FF> <1010>\n\
89<0100> <0101> <1012>\n\
90endbfrange\n";
91
92 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
Ben Wagner884300d2016-12-16 16:51:41 +000093 buffer.bytesWritten()));
vandebo@chromium.org9ad35992012-01-03 18:35:39 +000094
edisonn@google.com26d2e042013-09-18 19:29:08 +000095 // Remove characters and ranges.
96 buffer.reset();
97
halcanary8eccc302016-08-09 13:04:34 -070098 SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 8, 0x00FF);
edisonn@google.com26d2e042013-09-18 19:29:08 +000099
100 char expectedResultChop1[] =
101"2 beginbfchar\n\
102<0008> <002F>\n\
103<0009> <0033>\n\
104endbfchar\n\
1052 beginbfrange\n\
106<000B> <000D> <0035>\n\
107<00FE> <00FF> <1010>\n\
108endbfrange\n";
109
110 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1,
Ben Wagner884300d2016-12-16 16:51:41 +0000111 buffer.bytesWritten()));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000112
113 // Remove characters from range to downdrade it to one char.
114 buffer.reset();
115
halcanary8eccc302016-08-09 13:04:34 -0700116 SkPDFAppendCmapSections(glyphToUnicode, &subset, &buffer, true, 0x00D, 0x00FE);
edisonn@google.com26d2e042013-09-18 19:29:08 +0000117
118 char expectedResultChop2[] =
119"2 beginbfchar\n\
120<000D> <0037>\n\
121<00FE> <1010>\n\
122endbfchar\n";
123
124 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2,
Ben Wagner884300d2016-12-16 16:51:41 +0000125 buffer.bytesWritten()));
edisonn@google.com26d2e042013-09-18 19:29:08 +0000126
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000127 buffer.reset();
128
halcanary8eccc302016-08-09 13:04:34 -0700129 SkPDFAppendCmapSections(glyphToUnicode, nullptr, &buffer, false, 0xFC, 0x110);
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000130
131 char expectedResultSingleBytes[] =
132"2 beginbfchar\n\
halcanary3d01c622016-08-31 12:52:35 -0700133<01> <0000>\n\
134<02> <0000>\n\
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000135endbfchar\n\
1361 beginbfrange\n\
halcanary3d01c622016-08-31 12:52:35 -0700137<03> <06> <1010>\n\
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000138endbfrange\n";
139
140 REPORTER_ASSERT(reporter, stream_equals(buffer, 0,
141 expectedResultSingleBytes,
Ben Wagner884300d2016-12-16 16:51:41 +0000142 buffer.bytesWritten()));
commit-bot@chromium.org1236d8e2013-12-11 18:47:11 +0000143
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000144 glyphToUnicode.reset();
145 glyphsInSubset.reset();
halcanary530032a2016-08-18 14:22:52 -0700146 SkBitSet subset2(kMaximumGlyphCount);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000147
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
bsalomon98806072014-12-12 15:11:17 -0800152 for (SkUnichar i = 0; i < 100; ++i) {
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000153 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;
halcanary530032a2016-08-18 14:22:52 -0700164 subset2.setAll(glyphsInSubset.begin(), glyphsInSubset.count());
halcanary8eccc302016-08-09 13:04:34 -0700165 SkPDFAppendCmapSections(glyphToUnicode, &subset2, &buffer2, true, 0, 0xffff);
vandebo@chromium.org9ad35992012-01-03 18:35:39 +0000166
167 char expectedResult2[] =
168"4 beginbfchar\n\
169<002C> <0049>\n\
170<0044> <0061>\n\
171<004F> <006C>\n\
172<0051> <006E>\n\
173endbfchar\n\
1741 beginbfrange\n\
175<0056> <0057> <0073>\n\
176endbfrange\n";
177
178 REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
Ben Wagner884300d2016-12-16 16:51:41 +0000179 buffer2.bytesWritten()));
vandebo@chromium.org04c643b2011-08-08 22:33:05 +0000180}