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