1
2/*
3 * Copyright 2010 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "Test.h"
11#include "SkData.h"
12#include "SkPDFTypes.h"
13#include "SkPDFFont.h"
14#include "SkStream.h"
15
16static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
17                          const char* buffer, size_t len) {
18    SkAutoDataUnref data(stream.copyToData());
19    if (offset + len > data->size()) {
20        return false;
21    }
22    if (len != strlen(buffer)) {
23        return false;
24    }
25    return memcmp(data->bytes() + offset, buffer, len) == 0;
26}
27
28void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
29                          const SkPDFGlyphSet* subset,
30                          SkDynamicMemoryWStream* cmap);
31
32static void TestToUnicode(skiatest::Reporter* reporter) {
33    SkTDArray<SkUnichar> glyphToUnicode;
34    SkTDArray<uint16_t> glyphsInSubset;
35    SkPDFGlyphSet subset;
36
37    glyphToUnicode.push(0);  // 0
38    glyphToUnicode.push(0);  // 1
39    glyphToUnicode.push(0);  // 2
40    glyphsInSubset.push(3);
41    glyphToUnicode.push(0x20);  // 3
42    glyphsInSubset.push(4);
43    glyphToUnicode.push(0x25);  // 4
44    glyphsInSubset.push(5);
45    glyphToUnicode.push(0x27);  // 5
46    glyphsInSubset.push(6);
47    glyphToUnicode.push(0x28);  // 6
48    glyphsInSubset.push(7);
49    glyphToUnicode.push(0x29);  // 7
50    glyphsInSubset.push(8);
51    glyphToUnicode.push(0x2F);  // 8
52    glyphsInSubset.push(9);
53    glyphToUnicode.push(0x33);  // 9
54    glyphToUnicode.push(0);  // 10
55    glyphsInSubset.push(11);
56    glyphToUnicode.push(0x35);  // 11
57    glyphsInSubset.push(12);
58    glyphToUnicode.push(0x36);  // 12
59    for (uint16_t i = 13; i < 0xFE; ++i) {
60        glyphToUnicode.push(0);  // Zero from index 0x9 to 0xFD
61    }
62    glyphsInSubset.push(0xFE);
63    glyphToUnicode.push(0x1010);
64    glyphsInSubset.push(0xFF);
65    glyphToUnicode.push(0x1011);
66    glyphsInSubset.push(0x100);
67    glyphToUnicode.push(0x1012);
68    glyphsInSubset.push(0x101);
69    glyphToUnicode.push(0x1013);
70
71    SkDynamicMemoryWStream buffer;
72    subset.set(glyphsInSubset.begin(), glyphsInSubset.count());
73    append_cmap_sections(glyphToUnicode, &subset, &buffer);
74
75    char expectedResult[] =
76"4 beginbfchar\n\
77<0003> <0020>\n\
78<0004> <0025>\n\
79<0008> <002F>\n\
80<0009> <0033>\n\
81endbfchar\n\
824 beginbfrange\n\
83<0005> <0007> <0027>\n\
84<000B> <000C> <0035>\n\
85<00FE> <00FF> <1010>\n\
86<0100> <0101> <1012>\n\
87endbfrange\n";
88
89    REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
90                                            buffer.getOffset()));
91
92    glyphToUnicode.reset();
93    glyphsInSubset.reset();
94    SkPDFGlyphSet subset2;
95
96    // Test mapping:
97    //           I  n  s  t  a  l
98    // Glyph id 2c 51 56 57 44 4f
99    // Unicode  49 6e 73 74 61 6c
100    for (size_t i = 0; i < 100; ++i) {
101      glyphToUnicode.push(i + 29);
102    }
103
104    glyphsInSubset.push(0x2C);
105    glyphsInSubset.push(0x44);
106    glyphsInSubset.push(0x4F);
107    glyphsInSubset.push(0x51);
108    glyphsInSubset.push(0x56);
109    glyphsInSubset.push(0x57);
110
111    SkDynamicMemoryWStream buffer2;
112    subset2.set(glyphsInSubset.begin(), glyphsInSubset.count());
113    append_cmap_sections(glyphToUnicode, &subset2, &buffer2);
114
115    char expectedResult2[] =
116"4 beginbfchar\n\
117<002C> <0049>\n\
118<0044> <0061>\n\
119<004F> <006C>\n\
120<0051> <006E>\n\
121endbfchar\n\
1221 beginbfrange\n\
123<0056> <0057> <0073>\n\
124endbfrange\n";
125
126    REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
127                                            buffer2.getOffset()));
128}
129
130#include "TestClassDef.h"
131DEFINE_TESTCLASS("ToUnicode", ToUnicodeTestClass, TestToUnicode)
132