Home | History | Annotate | Download | only in tests
      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 
      8 #include "SkData.h"
      9 #include "SkPDFFont.h"
     10 #include "SkPDFTypes.h"
     11 #include "SkStream.h"
     12 #include "Test.h"
     13 
     14 static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset,
     15                           const char* buffer, size_t len) {
     16     SkAutoDataUnref data(stream.copyToData());
     17     if (offset + len > data->size()) {
     18         return false;
     19     }
     20     if (len != strlen(buffer)) {
     21         return false;
     22     }
     23     return memcmp(data->bytes() + offset, buffer, len) == 0;
     24 }
     25 
     26 void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
     27                           const SkPDFGlyphSet* subset,
     28                           SkDynamicMemoryWStream* cmap,
     29                           bool multiByteGlyphs,
     30                           uint16_t firstGlypthID,
     31                           uint16_t lastGlypthID);
     32 
     33 DEF_TEST(ToUnicode, reporter) {
     34     SkTDArray<SkUnichar> glyphToUnicode;
     35     SkTDArray<uint16_t> glyphsInSubset;
     36     SkPDFGlyphSet subset;
     37 
     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
     60     glyphsInSubset.push(13);
     61     glyphToUnicode.push(0x37);  // 13
     62     for (uint16_t i = 14; i < 0xFE; ++i) {
     63         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;
     75     subset.set(glyphsInSubset.begin(), glyphsInSubset.count());
     76     append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 0, 0xFFFF);
     77 
     78     char expectedResult[] =
     79 "4 beginbfchar\n\
     80 <0003> <0020>\n\
     81 <0004> <0025>\n\
     82 <0008> <002F>\n\
     83 <0009> <0033>\n\
     84 endbfchar\n\
     85 4 beginbfrange\n\
     86 <0005> <0007> <0027>\n\
     87 <000B> <000D> <0035>\n\
     88 <00FE> <00FF> <1010>\n\
     89 <0100> <0101> <1012>\n\
     90 endbfrange\n";
     91 
     92     REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
     93                                             buffer.getOffset()));
     94 
     95     // Remove characters and ranges.
     96     buffer.reset();
     97 
     98     append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 8, 0x00FF);
     99 
    100     char expectedResultChop1[] =
    101 "2 beginbfchar\n\
    102 <0008> <002F>\n\
    103 <0009> <0033>\n\
    104 endbfchar\n\
    105 2 beginbfrange\n\
    106 <000B> <000D> <0035>\n\
    107 <00FE> <00FF> <1010>\n\
    108 endbfrange\n";
    109 
    110     REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop1,
    111                                             buffer.getOffset()));
    112 
    113     // Remove characters from range to downdrade it to one char.
    114     buffer.reset();
    115 
    116     append_cmap_sections(glyphToUnicode, &subset, &buffer, true, 0x00D, 0x00FE);
    117 
    118     char expectedResultChop2[] =
    119 "2 beginbfchar\n\
    120 <000D> <0037>\n\
    121 <00FE> <1010>\n\
    122 endbfchar\n";
    123 
    124     REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResultChop2,
    125                                             buffer.getOffset()));
    126 
    127     buffer.reset();
    128 
    129     append_cmap_sections(glyphToUnicode, nullptr, &buffer, false, 0xFC, 0x110);
    130 
    131     char expectedResultSingleBytes[] =
    132 "2 beginbfchar\n\
    133 <0001> <0000>\n\
    134 <0002> <0000>\n\
    135 endbfchar\n\
    136 1 beginbfrange\n\
    137 <0003> <0006> <1010>\n\
    138 endbfrange\n";
    139 
    140     REPORTER_ASSERT(reporter, stream_equals(buffer, 0,
    141                                             expectedResultSingleBytes,
    142                                             buffer.getOffset()));
    143 
    144     glyphToUnicode.reset();
    145     glyphsInSubset.reset();
    146     SkPDFGlyphSet subset2;
    147 
    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
    152     for (SkUnichar i = 0; i < 100; ++i) {
    153       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;
    164     subset2.set(glyphsInSubset.begin(), glyphsInSubset.count());
    165     append_cmap_sections(glyphToUnicode, &subset2, &buffer2, true, 0, 0xffff);
    166 
    167     char expectedResult2[] =
    168 "4 beginbfchar\n\
    169 <002C> <0049>\n\
    170 <0044> <0061>\n\
    171 <004F> <006C>\n\
    172 <0051> <006E>\n\
    173 endbfchar\n\
    174 1 beginbfrange\n\
    175 <0056> <0057> <0073>\n\
    176 endbfrange\n";
    177 
    178     REPORTER_ASSERT(reporter, stream_equals(buffer2, 0, expectedResult2,
    179                                             buffer2.getOffset()));
    180 }
    181