Home | History | Annotate | Download | only in pdf
      1 /*
      2  * Copyright 2011 Google Inc.
      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 <ctype.h>
      9 
     10 #include "SkData.h"
     11 #include "SkFontHost.h"
     12 #include "SkGlyphCache.h"
     13 #include "SkPaint.h"
     14 #include "SkPDFCatalog.h"
     15 #include "SkPDFDevice.h"
     16 #include "SkPDFFont.h"
     17 #include "SkPDFFontImpl.h"
     18 #include "SkPDFStream.h"
     19 #include "SkPDFTypes.h"
     20 #include "SkPDFUtils.h"
     21 #include "SkRefCnt.h"
     22 #include "SkScalar.h"
     23 #include "SkStream.h"
     24 #include "SkTypefacePriv.h"
     25 #include "SkTypes.h"
     26 #include "SkUtils.h"
     27 
     28 #if defined (SK_SFNTLY_SUBSETTER)
     29 #include SK_SFNTLY_SUBSETTER
     30 #endif
     31 
     32 // PDF's notion of symbolic vs non-symbolic is related to the character set, not
     33 // symbols vs. characters.  Rarely is a font the right character set to call it
     34 // non-symbolic, so always call it symbolic.  (PDF 1.4 spec, section 5.7.1)
     35 static const int kPdfSymbolic = 4;
     36 
     37 namespace {
     38 
     39 ///////////////////////////////////////////////////////////////////////////////
     40 // File-Local Functions
     41 ///////////////////////////////////////////////////////////////////////////////
     42 
     43 bool parsePFBSection(const uint8_t** src, size_t* len, int sectionType,
     44                      size_t* size) {
     45     // PFB sections have a two or six bytes header. 0x80 and a one byte
     46     // section type followed by a four byte section length.  Type one is
     47     // an ASCII section (includes a length), type two is a binary section
     48     // (includes a length) and type three is an EOF marker with no length.
     49     const uint8_t* buf = *src;
     50     if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType) {
     51         return false;
     52     } else if (buf[1] == 3) {
     53         return true;
     54     } else if (*len < 6) {
     55         return false;
     56     }
     57 
     58     *size = (size_t)buf[2] | ((size_t)buf[3] << 8) | ((size_t)buf[4] << 16) |
     59             ((size_t)buf[5] << 24);
     60     size_t consumed = *size + 6;
     61     if (consumed > *len) {
     62         return false;
     63     }
     64     *src = *src + consumed;
     65     *len = *len - consumed;
     66     return true;
     67 }
     68 
     69 bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen,
     70               size_t* dataLen, size_t* trailerLen) {
     71     const uint8_t* srcPtr = src;
     72     size_t remaining = size;
     73 
     74     return parsePFBSection(&srcPtr, &remaining, 1, headerLen) &&
     75            parsePFBSection(&srcPtr, &remaining, 2, dataLen) &&
     76            parsePFBSection(&srcPtr, &remaining, 1, trailerLen) &&
     77            parsePFBSection(&srcPtr, &remaining, 3, NULL);
     78 }
     79 
     80 /* The sections of a PFA file are implicitly defined.  The body starts
     81  * after the line containing "eexec," and the trailer starts with 512
     82  * literal 0's followed by "cleartomark" (plus arbitrary white space).
     83  *
     84  * This function assumes that src is NUL terminated, but the NUL
     85  * termination is not included in size.
     86  *
     87  */
     88 bool parsePFA(const char* src, size_t size, size_t* headerLen,
     89               size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) {
     90     const char* end = src + size;
     91 
     92     const char* dataPos = strstr(src, "eexec");
     93     if (!dataPos) {
     94         return false;
     95     }
     96     dataPos += strlen("eexec");
     97     while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') &&
     98             dataPos < end) {
     99         dataPos++;
    100     }
    101     *headerLen = dataPos - src;
    102 
    103     const char* trailerPos = strstr(dataPos, "cleartomark");
    104     if (!trailerPos) {
    105         return false;
    106     }
    107     int zeroCount = 0;
    108     for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) {
    109         if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') {
    110             continue;
    111         } else if (*trailerPos == '0') {
    112             zeroCount++;
    113         } else {
    114             return false;
    115         }
    116     }
    117     if (zeroCount != 512) {
    118         return false;
    119     }
    120 
    121     *hexDataLen = trailerPos - src - *headerLen;
    122     *trailerLen = size - *headerLen - *hexDataLen;
    123 
    124     // Verify that the data section is hex encoded and count the bytes.
    125     int nibbles = 0;
    126     for (; dataPos < trailerPos; dataPos++) {
    127         if (isspace(*dataPos)) {
    128             continue;
    129         }
    130         if (!isxdigit(*dataPos)) {
    131             return false;
    132         }
    133         nibbles++;
    134     }
    135     *dataLen = (nibbles + 1) / 2;
    136 
    137     return true;
    138 }
    139 
    140 int8_t hexToBin(uint8_t c) {
    141     if (!isxdigit(c)) {
    142         return -1;
    143     } else if (c <= '9') {
    144         return c - '0';
    145     } else if (c <= 'F') {
    146         return c - 'A' + 10;
    147     } else if (c <= 'f') {
    148         return c - 'a' + 10;
    149     }
    150     return -1;
    151 }
    152 
    153 SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen,
    154                             size_t* dataLen, size_t* trailerLen) {
    155     // srcStream may be backed by a file or a unseekable fd, so we may not be
    156     // able to use skip(), rewind(), or getMemoryBase().  read()ing through
    157     // the input only once is doable, but very ugly. Furthermore, it'd be nice
    158     // if the data was NUL terminated so that we can use strstr() to search it.
    159     // Make as few copies as possible given these constraints.
    160     SkDynamicMemoryWStream dynamicStream;
    161     SkAutoTUnref<SkMemoryStream> staticStream;
    162     SkData* data = NULL;
    163     const uint8_t* src;
    164     size_t srcLen;
    165     if ((srcLen = srcStream->getLength()) > 0) {
    166         staticStream.reset(new SkMemoryStream(srcLen + 1));
    167         src = (const uint8_t*)staticStream->getMemoryBase();
    168         if (srcStream->getMemoryBase() != NULL) {
    169             memcpy((void *)src, srcStream->getMemoryBase(), srcLen);
    170         } else {
    171             size_t read = 0;
    172             while (read < srcLen) {
    173                 size_t got = srcStream->read((void *)staticStream->getAtPos(),
    174                                              srcLen - read);
    175                 if (got == 0) {
    176                     return NULL;
    177                 }
    178                 read += got;
    179                 staticStream->seek(read);
    180             }
    181         }
    182         ((uint8_t *)src)[srcLen] = 0;
    183     } else {
    184         static const size_t kBufSize = 4096;
    185         uint8_t buf[kBufSize];
    186         size_t amount;
    187         while ((amount = srcStream->read(buf, kBufSize)) > 0) {
    188             dynamicStream.write(buf, amount);
    189         }
    190         amount = 0;
    191         dynamicStream.write(&amount, 1);  // NULL terminator.
    192         data = dynamicStream.copyToData();
    193         src = data->bytes();
    194         srcLen = data->size() - 1;
    195     }
    196 
    197     // this handles releasing the data we may have gotten from dynamicStream.
    198     // if data is null, it is a no-op
    199     SkAutoDataUnref aud(data);
    200 
    201     if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) {
    202         SkMemoryStream* result =
    203             new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
    204         memcpy((char*)result->getAtPos(), src + 6, *headerLen);
    205         result->seek(*headerLen);
    206         memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen);
    207         result->seek(*headerLen + *dataLen);
    208         memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen,
    209                *trailerLen);
    210         result->rewind();
    211         return result;
    212     }
    213 
    214     // A PFA has to be converted for PDF.
    215     size_t hexDataLen;
    216     if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
    217                  trailerLen)) {
    218         SkMemoryStream* result =
    219             new SkMemoryStream(*headerLen + *dataLen + *trailerLen);
    220         memcpy((char*)result->getAtPos(), src, *headerLen);
    221         result->seek(*headerLen);
    222 
    223         const uint8_t* hexData = src + *headerLen;
    224         const uint8_t* trailer = hexData + hexDataLen;
    225         size_t outputOffset = 0;
    226         uint8_t dataByte = 0;  // To hush compiler.
    227         bool highNibble = true;
    228         for (; hexData < trailer; hexData++) {
    229             int8_t curNibble = hexToBin(*hexData);
    230             if (curNibble < 0) {
    231                 continue;
    232             }
    233             if (highNibble) {
    234                 dataByte = curNibble << 4;
    235                 highNibble = false;
    236             } else {
    237                 dataByte |= curNibble;
    238                 highNibble = true;
    239                 ((char *)result->getAtPos())[outputOffset++] = dataByte;
    240             }
    241         }
    242         if (!highNibble) {
    243             ((char *)result->getAtPos())[outputOffset++] = dataByte;
    244         }
    245         SkASSERT(outputOffset == *dataLen);
    246         result->seek(*headerLen + outputOffset);
    247 
    248         memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen,
    249                *trailerLen);
    250         result->rewind();
    251         return result;
    252     }
    253 
    254     return NULL;
    255 }
    256 
    257 // scale from em-units to base-1000, returning as a SkScalar
    258 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
    259     SkScalar scaled = SkIntToScalar(val);
    260     if (emSize == 1000) {
    261         return scaled;
    262     } else {
    263         return SkScalarMulDiv(scaled, 1000, emSize);
    264     }
    265 }
    266 
    267 void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box,
    268                                  SkWStream* content) {
    269     // Specify width and bounding box for the glyph.
    270     SkPDFScalar::Append(width, content);
    271     content->writeText(" 0 ");
    272     content->writeDecAsText(box.fLeft);
    273     content->writeText(" ");
    274     content->writeDecAsText(box.fTop);
    275     content->writeText(" ");
    276     content->writeDecAsText(box.fRight);
    277     content->writeText(" ");
    278     content->writeDecAsText(box.fBottom);
    279     content->writeText(" d1\n");
    280 }
    281 
    282 SkPDFArray* makeFontBBox(SkIRect glyphBBox, uint16_t emSize) {
    283     SkPDFArray* bbox = new SkPDFArray;
    284     bbox->reserve(4);
    285     bbox->appendScalar(scaleFromFontUnits(glyphBBox.fLeft, emSize));
    286     bbox->appendScalar(scaleFromFontUnits(glyphBBox.fBottom, emSize));
    287     bbox->appendScalar(scaleFromFontUnits(glyphBBox.fRight, emSize));
    288     bbox->appendScalar(scaleFromFontUnits(glyphBBox.fTop, emSize));
    289     return bbox;
    290 }
    291 
    292 SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize,
    293                         SkPDFArray* array) {
    294     array->appendScalar(scaleFromFontUnits(width, emSize));
    295     return array;
    296 }
    297 
    298 SkPDFArray* appendVerticalAdvance(
    299         const SkAdvancedTypefaceMetrics::VerticalMetric& advance,
    300         uint16_t emSize, SkPDFArray* array) {
    301     appendWidth(advance.fVerticalAdvance, emSize, array);
    302     appendWidth(advance.fOriginXDisp, emSize, array);
    303     appendWidth(advance.fOriginYDisp, emSize, array);
    304     return array;
    305 }
    306 
    307 template <typename Data>
    308 SkPDFArray* composeAdvanceData(
    309         SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo,
    310         uint16_t emSize,
    311         SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize,
    312                                      SkPDFArray* array),
    313         Data* defaultAdvance) {
    314     SkPDFArray* result = new SkPDFArray();
    315     for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) {
    316         switch (advanceInfo->fType) {
    317             case SkAdvancedTypefaceMetrics::WidthRange::kDefault: {
    318                 SkASSERT(advanceInfo->fAdvance.count() == 1);
    319                 *defaultAdvance = advanceInfo->fAdvance[0];
    320                 break;
    321             }
    322             case SkAdvancedTypefaceMetrics::WidthRange::kRange: {
    323                 SkAutoTUnref<SkPDFArray> advanceArray(new SkPDFArray());
    324                 for (int j = 0; j < advanceInfo->fAdvance.count(); j++)
    325                     appendAdvance(advanceInfo->fAdvance[j], emSize,
    326                                   advanceArray.get());
    327                 result->appendInt(advanceInfo->fStartId);
    328                 result->append(advanceArray.get());
    329                 break;
    330             }
    331             case SkAdvancedTypefaceMetrics::WidthRange::kRun: {
    332                 SkASSERT(advanceInfo->fAdvance.count() == 1);
    333                 result->appendInt(advanceInfo->fStartId);
    334                 result->appendInt(advanceInfo->fEndId);
    335                 appendAdvance(advanceInfo->fAdvance[0], emSize, result);
    336                 break;
    337             }
    338         }
    339     }
    340     return result;
    341 }
    342 
    343 }  // namespace
    344 
    345 static void append_tounicode_header(SkDynamicMemoryWStream* cmap,
    346                                     uint16_t firstGlyphID,
    347                                     uint16_t lastGlyphID) {
    348     // 12 dict begin: 12 is an Adobe-suggested value. Shall not change.
    349     // It's there to prevent old version Adobe Readers from malfunctioning.
    350     const char* kHeader =
    351         "/CIDInit /ProcSet findresource begin\n"
    352         "12 dict begin\n"
    353         "begincmap\n";
    354     cmap->writeText(kHeader);
    355 
    356     // The /CIDSystemInfo must be consistent to the one in
    357     // SkPDFFont::populateCIDFont().
    358     // We can not pass over the system info object here because the format is
    359     // different. This is not a reference object.
    360     const char* kSysInfo =
    361         "/CIDSystemInfo\n"
    362         "<<  /Registry (Adobe)\n"
    363         "/Ordering (UCS)\n"
    364         "/Supplement 0\n"
    365         ">> def\n";
    366     cmap->writeText(kSysInfo);
    367 
    368     // The CMapName must be consistent to /CIDSystemInfo above.
    369     // /CMapType 2 means ToUnicode.
    370     // Codespace range just tells the PDF processor the valid range.
    371     const char* kTypeInfoHeader =
    372         "/CMapName /Adobe-Identity-UCS def\n"
    373         "/CMapType 2 def\n"
    374         "1 begincodespacerange\n";
    375     cmap->writeText(kTypeInfoHeader);
    376 
    377     // e.g.     "<0000> <FFFF>\n"
    378     SkString range;
    379     range.appendf("<%04X> <%04X>\n", firstGlyphID, lastGlyphID);
    380     cmap->writeText(range.c_str());
    381 
    382     const char* kTypeInfoFooter = "endcodespacerange\n";
    383     cmap->writeText(kTypeInfoFooter);
    384 }
    385 
    386 static void append_cmap_footer(SkDynamicMemoryWStream* cmap) {
    387     const char* kFooter =
    388         "endcmap\n"
    389         "CMapName currentdict /CMap defineresource pop\n"
    390         "end\n"
    391         "end";
    392     cmap->writeText(kFooter);
    393 }
    394 
    395 struct BFChar {
    396     uint16_t fGlyphId;
    397     SkUnichar fUnicode;
    398 };
    399 
    400 struct BFRange {
    401     uint16_t fStart;
    402     uint16_t fEnd;
    403     SkUnichar fUnicode;
    404 };
    405 
    406 static void append_bfchar_section(const SkTDArray<BFChar>& bfchar,
    407                                   SkDynamicMemoryWStream* cmap) {
    408     // PDF spec defines that every bf* list can have at most 100 entries.
    409     for (int i = 0; i < bfchar.count(); i += 100) {
    410         int count = bfchar.count() - i;
    411         count = SkMin32(count, 100);
    412         cmap->writeDecAsText(count);
    413         cmap->writeText(" beginbfchar\n");
    414         for (int j = 0; j < count; ++j) {
    415             cmap->writeText("<");
    416             cmap->writeHexAsText(bfchar[i + j].fGlyphId, 4);
    417             cmap->writeText("> <");
    418             cmap->writeHexAsText(bfchar[i + j].fUnicode, 4);
    419             cmap->writeText(">\n");
    420         }
    421         cmap->writeText("endbfchar\n");
    422     }
    423 }
    424 
    425 static void append_bfrange_section(const SkTDArray<BFRange>& bfrange,
    426                                    SkDynamicMemoryWStream* cmap) {
    427     // PDF spec defines that every bf* list can have at most 100 entries.
    428     for (int i = 0; i < bfrange.count(); i += 100) {
    429         int count = bfrange.count() - i;
    430         count = SkMin32(count, 100);
    431         cmap->writeDecAsText(count);
    432         cmap->writeText(" beginbfrange\n");
    433         for (int j = 0; j < count; ++j) {
    434             cmap->writeText("<");
    435             cmap->writeHexAsText(bfrange[i + j].fStart, 4);
    436             cmap->writeText("> <");
    437             cmap->writeHexAsText(bfrange[i + j].fEnd, 4);
    438             cmap->writeText("> <");
    439             cmap->writeHexAsText(bfrange[i + j].fUnicode, 4);
    440             cmap->writeText(">\n");
    441         }
    442         cmap->writeText("endbfrange\n");
    443     }
    444 }
    445 
    446 // Generate <bfchar> and <bfrange> table according to PDF spec 1.4 and Adobe
    447 // Technote 5014.
    448 // The function is not static so we can test it in unit tests.
    449 //
    450 // Current implementation guarantees bfchar and bfrange entries do not overlap.
    451 //
    452 // Current implementation does not attempt aggresive optimizations against
    453 // following case because the specification is not clear.
    454 //
    455 // 4 beginbfchar          1 beginbfchar
    456 // <0003> <0013>          <0020> <0014>
    457 // <0005> <0015>    to    endbfchar
    458 // <0007> <0017>          1 beginbfrange
    459 // <0020> <0014>          <0003> <0007> <0013>
    460 // endbfchar              endbfrange
    461 //
    462 // Adobe Technote 5014 said: "Code mappings (unlike codespace ranges) may
    463 // overlap, but succeeding maps supersede preceding maps."
    464 //
    465 // In case of searching text in PDF, bfrange will have higher precedence so
    466 // typing char id 0x0014 in search box will get glyph id 0x0004 first.  However,
    467 // the spec does not mention how will this kind of conflict being resolved.
    468 //
    469 // For the worst case (having 65536 continuous unicode and we use every other
    470 // one of them), the possible savings by aggressive optimization is 416KB
    471 // pre-compressed and does not provide enough motivation for implementation.
    472 
    473 // FIXME: this should be in a header so that it is separately testable
    474 // ( see caller in tests/ToUnicode.cpp )
    475 void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
    476                           const SkPDFGlyphSet* subset,
    477                           SkDynamicMemoryWStream* cmap,
    478                           bool multiByteGlyphs,
    479                           uint16_t firstGlyphID,
    480                           uint16_t lastGlyphID);
    481 
    482 void append_cmap_sections(const SkTDArray<SkUnichar>& glyphToUnicode,
    483                           const SkPDFGlyphSet* subset,
    484                           SkDynamicMemoryWStream* cmap,
    485                           bool multiByteGlyphs,
    486                           uint16_t firstGlyphID,
    487                           uint16_t lastGlyphID) {
    488     if (glyphToUnicode.isEmpty()) {
    489         return;
    490     }
    491     int glyphOffset = 0;
    492     if (!multiByteGlyphs) {
    493         glyphOffset = firstGlyphID - 1;
    494     }
    495 
    496     SkTDArray<BFChar> bfcharEntries;
    497     SkTDArray<BFRange> bfrangeEntries;
    498 
    499     BFRange currentRangeEntry = {0, 0, 0};
    500     bool rangeEmpty = true;
    501     const int limit =
    502             SkMin32(lastGlyphID + 1, glyphToUnicode.count()) - glyphOffset;
    503 
    504     for (int i = firstGlyphID - glyphOffset; i < limit + 1; ++i) {
    505         bool inSubset = i < limit &&
    506                         (subset == NULL || subset->has(i + glyphOffset));
    507         if (!rangeEmpty) {
    508             // PDF spec requires bfrange not changing the higher byte,
    509             // e.g. <1035> <10FF> <2222> is ok, but
    510             //      <1035> <1100> <2222> is no good
    511             bool inRange =
    512                 i == currentRangeEntry.fEnd + 1 &&
    513                 i >> 8 == currentRangeEntry.fStart >> 8 &&
    514                 i < limit &&
    515                 glyphToUnicode[i + glyphOffset] ==
    516                     currentRangeEntry.fUnicode + i - currentRangeEntry.fStart;
    517             if (!inSubset || !inRange) {
    518                 if (currentRangeEntry.fEnd > currentRangeEntry.fStart) {
    519                     bfrangeEntries.push(currentRangeEntry);
    520                 } else {
    521                     BFChar* entry = bfcharEntries.append();
    522                     entry->fGlyphId = currentRangeEntry.fStart;
    523                     entry->fUnicode = currentRangeEntry.fUnicode;
    524                 }
    525                 rangeEmpty = true;
    526             }
    527         }
    528         if (inSubset) {
    529             currentRangeEntry.fEnd = i;
    530             if (rangeEmpty) {
    531               currentRangeEntry.fStart = i;
    532               currentRangeEntry.fUnicode = glyphToUnicode[i + glyphOffset];
    533               rangeEmpty = false;
    534             }
    535         }
    536     }
    537 
    538     // The spec requires all bfchar entries for a font must come before bfrange
    539     // entries.
    540     append_bfchar_section(bfcharEntries, cmap);
    541     append_bfrange_section(bfrangeEntries, cmap);
    542 }
    543 
    544 static SkPDFStream* generate_tounicode_cmap(
    545         const SkTDArray<SkUnichar>& glyphToUnicode,
    546         const SkPDFGlyphSet* subset,
    547         bool multiByteGlyphs,
    548         uint16_t firstGlyphID,
    549         uint16_t lastGlyphID) {
    550     SkDynamicMemoryWStream cmap;
    551     if (multiByteGlyphs) {
    552         append_tounicode_header(&cmap, firstGlyphID, lastGlyphID);
    553     } else {
    554         append_tounicode_header(&cmap, 1, lastGlyphID - firstGlyphID + 1);
    555     }
    556     append_cmap_sections(glyphToUnicode, subset, &cmap, multiByteGlyphs,
    557                          firstGlyphID, lastGlyphID);
    558     append_cmap_footer(&cmap);
    559     SkAutoTUnref<SkMemoryStream> cmapStream(new SkMemoryStream());
    560     cmapStream->setData(cmap.copyToData())->unref();
    561     return new SkPDFStream(cmapStream.get());
    562 }
    563 
    564 #if defined (SK_SFNTLY_SUBSETTER)
    565 static void sk_delete_array(const void* ptr, size_t, void*) {
    566     // Use C-style cast to cast away const and cast type simultaneously.
    567     delete[] (unsigned char*)ptr;
    568 }
    569 #endif
    570 
    571 static size_t get_subset_font_stream(const char* fontName,
    572                                      const SkTypeface* typeface,
    573                                      const SkTDArray<uint32_t>& subset,
    574                                      SkPDFStream** fontStream) {
    575     int ttcIndex;
    576     SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex));
    577 
    578     size_t fontSize = fontData->getLength();
    579 
    580 #if defined (SK_SFNTLY_SUBSETTER)
    581     // Read font into buffer.
    582     SkPDFStream* subsetFontStream = NULL;
    583     SkTDArray<unsigned char> originalFont;
    584     originalFont.setCount(SkToInt(fontSize));
    585     if (fontData->read(originalFont.begin(), fontSize) == fontSize) {
    586         unsigned char* subsetFont = NULL;
    587         // sfntly requires unsigned int* to be passed in, as far as we know,
    588         // unsigned int is equivalent to uint32_t on all platforms.
    589         SK_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t),
    590                           unsigned_int_not_32_bits);
    591         int subsetFontSize = SfntlyWrapper::SubsetFont(fontName,
    592                                                        originalFont.begin(),
    593                                                        fontSize,
    594                                                        subset.begin(),
    595                                                        subset.count(),
    596                                                        &subsetFont);
    597         if (subsetFontSize > 0 && subsetFont != NULL) {
    598             SkAutoDataUnref data(SkData::NewWithProc(subsetFont,
    599                                                      subsetFontSize,
    600                                                      sk_delete_array,
    601                                                      NULL));
    602             subsetFontStream = new SkPDFStream(data.get());
    603             fontSize = subsetFontSize;
    604         }
    605     }
    606     if (subsetFontStream) {
    607         *fontStream = subsetFontStream;
    608         return fontSize;
    609     }
    610     fontData->rewind();
    611 #else
    612     sk_ignore_unused_variable(fontName);
    613     sk_ignore_unused_variable(subset);
    614 #endif
    615 
    616     // Fail over: just embed the whole font.
    617     *fontStream = new SkPDFStream(fontData.get());
    618     return fontSize;
    619 }
    620 
    621 ///////////////////////////////////////////////////////////////////////////////
    622 // class SkPDFGlyphSet
    623 ///////////////////////////////////////////////////////////////////////////////
    624 
    625 SkPDFGlyphSet::SkPDFGlyphSet() : fBitSet(SK_MaxU16 + 1) {
    626 }
    627 
    628 void SkPDFGlyphSet::set(const uint16_t* glyphIDs, int numGlyphs) {
    629     for (int i = 0; i < numGlyphs; ++i) {
    630         fBitSet.setBit(glyphIDs[i], true);
    631     }
    632 }
    633 
    634 bool SkPDFGlyphSet::has(uint16_t glyphID) const {
    635     return fBitSet.isBitSet(glyphID);
    636 }
    637 
    638 void SkPDFGlyphSet::merge(const SkPDFGlyphSet& usage) {
    639     fBitSet.orBits(usage.fBitSet);
    640 }
    641 
    642 void SkPDFGlyphSet::exportTo(SkTDArray<unsigned int>* glyphIDs) const {
    643     fBitSet.exportTo(glyphIDs);
    644 }
    645 
    646 ///////////////////////////////////////////////////////////////////////////////
    647 // class SkPDFGlyphSetMap
    648 ///////////////////////////////////////////////////////////////////////////////
    649 SkPDFGlyphSetMap::FontGlyphSetPair::FontGlyphSetPair(SkPDFFont* font,
    650                                                      SkPDFGlyphSet* glyphSet)
    651         : fFont(font),
    652           fGlyphSet(glyphSet) {
    653 }
    654 
    655 SkPDFGlyphSetMap::F2BIter::F2BIter(const SkPDFGlyphSetMap& map) {
    656     reset(map);
    657 }
    658 
    659 const SkPDFGlyphSetMap::FontGlyphSetPair* SkPDFGlyphSetMap::F2BIter::next() const {
    660     if (fIndex >= fMap->count()) {
    661         return NULL;
    662     }
    663     return &((*fMap)[fIndex++]);
    664 }
    665 
    666 void SkPDFGlyphSetMap::F2BIter::reset(const SkPDFGlyphSetMap& map) {
    667     fMap = &(map.fMap);
    668     fIndex = 0;
    669 }
    670 
    671 SkPDFGlyphSetMap::SkPDFGlyphSetMap() {
    672 }
    673 
    674 SkPDFGlyphSetMap::~SkPDFGlyphSetMap() {
    675     reset();
    676 }
    677 
    678 void SkPDFGlyphSetMap::merge(const SkPDFGlyphSetMap& usage) {
    679     for (int i = 0; i < usage.fMap.count(); ++i) {
    680         SkPDFGlyphSet* myUsage = getGlyphSetForFont(usage.fMap[i].fFont);
    681         myUsage->merge(*(usage.fMap[i].fGlyphSet));
    682     }
    683 }
    684 
    685 void SkPDFGlyphSetMap::reset() {
    686     for (int i = 0; i < fMap.count(); ++i) {
    687         delete fMap[i].fGlyphSet;  // Should not be NULL.
    688     }
    689     fMap.reset();
    690 }
    691 
    692 void SkPDFGlyphSetMap::noteGlyphUsage(SkPDFFont* font, const uint16_t* glyphIDs,
    693                                       int numGlyphs) {
    694     SkPDFGlyphSet* subset = getGlyphSetForFont(font);
    695     if (subset) {
    696         subset->set(glyphIDs, numGlyphs);
    697     }
    698 }
    699 
    700 SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) {
    701     int index = fMap.count();
    702     for (int i = 0; i < index; ++i) {
    703         if (fMap[i].fFont == font) {
    704             return fMap[i].fGlyphSet;
    705         }
    706     }
    707     fMap.append();
    708     index = fMap.count() - 1;
    709     fMap[index].fFont = font;
    710     fMap[index].fGlyphSet = new SkPDFGlyphSet();
    711     return fMap[index].fGlyphSet;
    712 }
    713 
    714 ///////////////////////////////////////////////////////////////////////////////
    715 // class SkPDFFont
    716 ///////////////////////////////////////////////////////////////////////////////
    717 
    718 /* Font subset design: It would be nice to be able to subset fonts
    719  * (particularly type 3 fonts), but it's a lot of work and not a priority.
    720  *
    721  * Resources are canonicalized and uniqueified by pointer so there has to be
    722  * some additional state indicating which subset of the font is used.  It
    723  * must be maintained at the page granularity and then combined at the document
    724  * granularity. a) change SkPDFFont to fill in its state on demand, kind of
    725  * like SkPDFGraphicState.  b) maintain a per font glyph usage class in each
    726  * page/pdf device. c) in the document, retrieve the per font glyph usage
    727  * from each page and combine it and ask for a resource with that subset.
    728  */
    729 
    730 SkPDFFont::~SkPDFFont() {
    731     SkAutoMutexAcquire lock(CanonicalFontsMutex());
    732     int index = -1;
    733     for (int i = 0 ; i < CanonicalFonts().count() ; i++) {
    734         if (CanonicalFonts()[i].fFont == this) {
    735             index = i;
    736         }
    737     }
    738 
    739     SkDEBUGCODE(int indexFound;)
    740     SkASSERT(index == -1 ||
    741              (Find(fTypeface->uniqueID(),
    742                    fFirstGlyphID,
    743                    &indexFound) &&
    744              index == indexFound));
    745     if (index >= 0) {
    746         CanonicalFonts().removeShuffle(index);
    747     }
    748     fResources.unrefAll();
    749 }
    750 
    751 void SkPDFFont::getResources(const SkTSet<SkPDFObject*>& knownResourceObjects,
    752                              SkTSet<SkPDFObject*>* newResourceObjects) {
    753     GetResourcesHelper(&fResources, knownResourceObjects, newResourceObjects);
    754 }
    755 
    756 SkTypeface* SkPDFFont::typeface() {
    757     return fTypeface.get();
    758 }
    759 
    760 SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() {
    761     return fFontType;
    762 }
    763 
    764 bool SkPDFFont::canEmbed() const {
    765     if (!fFontInfo.get()) {
    766         SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font);
    767         return true;
    768     }
    769     return (fFontInfo->fFlags &
    770             SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag) == 0;
    771 }
    772 
    773 bool SkPDFFont::canSubset() const {
    774     if (!fFontInfo.get()) {
    775         SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font);
    776         return true;
    777     }
    778     return (fFontInfo->fFlags &
    779             SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag) == 0;
    780 }
    781 
    782 bool SkPDFFont::hasGlyph(uint16_t id) {
    783     return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0;
    784 }
    785 
    786 int SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs, int numGlyphs) {
    787     // A font with multibyte glyphs will support all glyph IDs in a single font.
    788     if (this->multiByteGlyphs()) {
    789         return numGlyphs;
    790     }
    791 
    792     for (int i = 0; i < numGlyphs; i++) {
    793         if (glyphIDs[i] == 0) {
    794             continue;
    795         }
    796         if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) {
    797             return i;
    798         }
    799         glyphIDs[i] -= (fFirstGlyphID - 1);
    800     }
    801 
    802     return numGlyphs;
    803 }
    804 
    805 // static
    806 SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) {
    807     SkAutoMutexAcquire lock(CanonicalFontsMutex());
    808 
    809     SkAutoResolveDefaultTypeface autoResolve(typeface);
    810     typeface = autoResolve.get();
    811 
    812     const uint32_t fontID = typeface->uniqueID();
    813     int relatedFontIndex;
    814     if (Find(fontID, glyphID, &relatedFontIndex)) {
    815         CanonicalFonts()[relatedFontIndex].fFont->ref();
    816         return CanonicalFonts()[relatedFontIndex].fFont;
    817     }
    818 
    819     SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics;
    820     SkPDFDict* relatedFontDescriptor = NULL;
    821     if (relatedFontIndex >= 0) {
    822         SkPDFFont* relatedFont = CanonicalFonts()[relatedFontIndex].fFont;
    823         fontMetrics.reset(relatedFont->fontInfo());
    824         SkSafeRef(fontMetrics.get());
    825         relatedFontDescriptor = relatedFont->getFontDescriptor();
    826 
    827         // This only is to catch callers who pass invalid glyph ids.
    828         // If glyph id is invalid, then we will create duplicate entries
    829         // for TrueType fonts.
    830         SkAdvancedTypefaceMetrics::FontType fontType =
    831             fontMetrics.get() ? fontMetrics.get()->fType :
    832                                 SkAdvancedTypefaceMetrics::kOther_Font;
    833 
    834         if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font ||
    835             fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
    836             CanonicalFonts()[relatedFontIndex].fFont->ref();
    837             return CanonicalFonts()[relatedFontIndex].fFont;
    838         }
    839     } else {
    840         SkAdvancedTypefaceMetrics::PerGlyphInfo info;
    841         info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
    842         info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
    843                   info, SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo);
    844 #if !defined (SK_SFNTLY_SUBSETTER)
    845         info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
    846                   info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
    847 #endif
    848         fontMetrics.reset(
    849             typeface->getAdvancedTypefaceMetrics(info, NULL, 0));
    850 #if defined (SK_SFNTLY_SUBSETTER)
    851         if (fontMetrics.get() &&
    852             fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) {
    853             // Font does not support subsetting, get new info with advance.
    854             info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
    855                       info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
    856             fontMetrics.reset(
    857                 typeface->getAdvancedTypefaceMetrics(info, NULL, 0));
    858         }
    859 #endif
    860     }
    861 
    862     SkPDFFont* font = Create(fontMetrics.get(), typeface, glyphID,
    863                              relatedFontDescriptor);
    864     FontRec newEntry(font, fontID, font->fFirstGlyphID);
    865     CanonicalFonts().push(newEntry);
    866     return font;  // Return the reference new SkPDFFont() created.
    867 }
    868 
    869 SkPDFFont* SkPDFFont::getFontSubset(const SkPDFGlyphSet*) {
    870     return NULL;  // Default: no support.
    871 }
    872 
    873 // static
    874 SkTDArray<SkPDFFont::FontRec>& SkPDFFont::CanonicalFonts() {
    875     SkPDFFont::CanonicalFontsMutex().assertHeld();
    876     static SkTDArray<FontRec> gCanonicalFonts;
    877     return gCanonicalFonts;
    878 }
    879 
    880 // static
    881 SkBaseMutex& SkPDFFont::CanonicalFontsMutex() {
    882     SK_DECLARE_STATIC_MUTEX(gCanonicalFontsMutex);
    883     return gCanonicalFontsMutex;
    884 }
    885 
    886 // static
    887 bool SkPDFFont::Find(uint32_t fontID, uint16_t glyphID, int* index) {
    888     // TODO(vandebo): Optimize this, do only one search?
    889     FontRec search(NULL, fontID, glyphID);
    890     *index = CanonicalFonts().find(search);
    891     if (*index >= 0) {
    892         return true;
    893     }
    894     search.fGlyphID = 0;
    895     *index = CanonicalFonts().find(search);
    896     return false;
    897 }
    898 
    899 SkPDFFont::SkPDFFont(SkAdvancedTypefaceMetrics* info, SkTypeface* typeface,
    900                      SkPDFDict* relatedFontDescriptor)
    901         : SkPDFDict("Font"),
    902           fTypeface(ref_or_default(typeface)),
    903           fFirstGlyphID(1),
    904           fLastGlyphID(info ? info->fLastGlyphID : 0),
    905           fFontInfo(SkSafeRef(info)),
    906           fDescriptor(SkSafeRef(relatedFontDescriptor)) {
    907     if (info == NULL ||
    908             info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag) {
    909         fFontType = SkAdvancedTypefaceMetrics::kOther_Font;
    910     } else {
    911         fFontType = info->fType;
    912     }
    913 }
    914 
    915 // static
    916 SkPDFFont* SkPDFFont::Create(SkAdvancedTypefaceMetrics* info,
    917                              SkTypeface* typeface, uint16_t glyphID,
    918                              SkPDFDict* relatedFontDescriptor) {
    919     SkAdvancedTypefaceMetrics::FontType type =
    920         info ? info->fType : SkAdvancedTypefaceMetrics::kOther_Font;
    921 
    922     if (info &&
    923             (info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) {
    924         NOT_IMPLEMENTED(true, true);
    925         return new SkPDFType3Font(info,
    926                                   typeface,
    927                                   glyphID);
    928     }
    929     if (type == SkAdvancedTypefaceMetrics::kType1CID_Font ||
    930         type == SkAdvancedTypefaceMetrics::kTrueType_Font) {
    931         SkASSERT(relatedFontDescriptor == NULL);
    932         return new SkPDFType0Font(info, typeface);
    933     }
    934     if (type == SkAdvancedTypefaceMetrics::kType1_Font) {
    935         return new SkPDFType1Font(info,
    936                                   typeface,
    937                                   glyphID,
    938                                   relatedFontDescriptor);
    939     }
    940 
    941     SkASSERT(type == SkAdvancedTypefaceMetrics::kCFF_Font ||
    942              type == SkAdvancedTypefaceMetrics::kOther_Font);
    943 
    944     return new SkPDFType3Font(info, typeface, glyphID);
    945 }
    946 
    947 SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() {
    948     return fFontInfo.get();
    949 }
    950 
    951 void SkPDFFont::setFontInfo(SkAdvancedTypefaceMetrics* info) {
    952     if (info == NULL || info == fFontInfo.get()) {
    953         return;
    954     }
    955     fFontInfo.reset(info);
    956     SkSafeRef(info);
    957 }
    958 
    959 uint16_t SkPDFFont::firstGlyphID() const {
    960     return fFirstGlyphID;
    961 }
    962 
    963 uint16_t SkPDFFont::lastGlyphID() const {
    964     return fLastGlyphID;
    965 }
    966 
    967 void SkPDFFont::setLastGlyphID(uint16_t glyphID) {
    968     fLastGlyphID = glyphID;
    969 }
    970 
    971 void SkPDFFont::addResource(SkPDFObject* object) {
    972     SkASSERT(object != NULL);
    973     fResources.push(object);
    974     object->ref();
    975 }
    976 
    977 SkPDFDict* SkPDFFont::getFontDescriptor() {
    978     return fDescriptor.get();
    979 }
    980 
    981 void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) {
    982     fDescriptor.reset(descriptor);
    983     SkSafeRef(descriptor);
    984 }
    985 
    986 bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) {
    987     if (fDescriptor.get() == NULL) {
    988         return false;
    989     }
    990 
    991     const uint16_t emSize = fFontInfo->fEmSize;
    992 
    993     fDescriptor->insertName("FontName", fFontInfo->fFontName);
    994     fDescriptor->insertInt("Flags", fFontInfo->fStyle | kPdfSymbolic);
    995     fDescriptor->insertScalar("Ascent",
    996             scaleFromFontUnits(fFontInfo->fAscent, emSize));
    997     fDescriptor->insertScalar("Descent",
    998             scaleFromFontUnits(fFontInfo->fDescent, emSize));
    999     fDescriptor->insertScalar("StemV",
   1000             scaleFromFontUnits(fFontInfo->fStemV, emSize));
   1001     fDescriptor->insertScalar("CapHeight",
   1002             scaleFromFontUnits(fFontInfo->fCapHeight, emSize));
   1003     fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle);
   1004     fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox,
   1005                                                  fFontInfo->fEmSize))->unref();
   1006 
   1007     if (defaultWidth > 0) {
   1008         fDescriptor->insertScalar("MissingWidth",
   1009                 scaleFromFontUnits(defaultWidth, emSize));
   1010     }
   1011     return true;
   1012 }
   1013 
   1014 void SkPDFFont::adjustGlyphRangeForSingleByteEncoding(int16_t glyphID) {
   1015     // Single byte glyph encoding supports a max of 255 glyphs.
   1016     fFirstGlyphID = glyphID - (glyphID - 1) % 255;
   1017     if (fLastGlyphID > fFirstGlyphID + 255 - 1) {
   1018         fLastGlyphID = fFirstGlyphID + 255 - 1;
   1019     }
   1020 }
   1021 
   1022 bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const {
   1023     if (fFontID != b.fFontID) {
   1024         return false;
   1025     }
   1026     if (fFont != NULL && b.fFont != NULL) {
   1027         return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID &&
   1028             fFont->fLastGlyphID == b.fFont->fLastGlyphID;
   1029     }
   1030     if (fGlyphID == 0 || b.fGlyphID == 0) {
   1031         return true;
   1032     }
   1033 
   1034     if (fFont != NULL) {
   1035         return fFont->fFirstGlyphID <= b.fGlyphID &&
   1036             b.fGlyphID <= fFont->fLastGlyphID;
   1037     } else if (b.fFont != NULL) {
   1038         return b.fFont->fFirstGlyphID <= fGlyphID &&
   1039             fGlyphID <= b.fFont->fLastGlyphID;
   1040     }
   1041     return fGlyphID == b.fGlyphID;
   1042 }
   1043 
   1044 SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID)
   1045     : fFont(font),
   1046       fFontID(fontID),
   1047       fGlyphID(glyphID) {
   1048 }
   1049 
   1050 void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) {
   1051     if (fFontInfo == NULL || fFontInfo->fGlyphToUnicode.begin() == NULL) {
   1052         return;
   1053     }
   1054     SkAutoTUnref<SkPDFStream> pdfCmap(
   1055         generate_tounicode_cmap(fFontInfo->fGlyphToUnicode, subset,
   1056                                 multiByteGlyphs(), firstGlyphID(),
   1057                                 lastGlyphID()));
   1058     addResource(pdfCmap.get());
   1059     insert("ToUnicode", new SkPDFObjRef(pdfCmap.get()))->unref();
   1060 }
   1061 
   1062 ///////////////////////////////////////////////////////////////////////////////
   1063 // class SkPDFType0Font
   1064 ///////////////////////////////////////////////////////////////////////////////
   1065 
   1066 SkPDFType0Font::SkPDFType0Font(SkAdvancedTypefaceMetrics* info,
   1067                                SkTypeface* typeface)
   1068         : SkPDFFont(info, typeface, NULL) {
   1069     SkDEBUGCODE(fPopulated = false);
   1070     if (!canSubset()) {
   1071         populate(NULL);
   1072     }
   1073 }
   1074 
   1075 SkPDFType0Font::~SkPDFType0Font() {}
   1076 
   1077 SkPDFFont* SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) {
   1078     if (!canSubset()) {
   1079         return NULL;
   1080     }
   1081     SkPDFType0Font* newSubset = new SkPDFType0Font(fontInfo(), typeface());
   1082     newSubset->populate(subset);
   1083     return newSubset;
   1084 }
   1085 
   1086 #ifdef SK_DEBUG
   1087 void SkPDFType0Font::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
   1088                                 bool indirect) {
   1089     SkASSERT(fPopulated);
   1090     return INHERITED::emitObject(stream, catalog, indirect);
   1091 }
   1092 #endif
   1093 
   1094 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) {
   1095     insertName("Subtype", "Type0");
   1096     insertName("BaseFont", fontInfo()->fFontName);
   1097     insertName("Encoding", "Identity-H");
   1098 
   1099     SkAutoTUnref<SkPDFCIDFont> newCIDFont(
   1100             new SkPDFCIDFont(fontInfo(), typeface(), subset));
   1101     addResource(newCIDFont.get());
   1102     SkAutoTUnref<SkPDFArray> descendantFonts(new SkPDFArray());
   1103     descendantFonts->append(new SkPDFObjRef(newCIDFont.get()))->unref();
   1104     insert("DescendantFonts", descendantFonts.get());
   1105 
   1106     populateToUnicodeTable(subset);
   1107 
   1108     SkDEBUGCODE(fPopulated = true);
   1109     return true;
   1110 }
   1111 
   1112 ///////////////////////////////////////////////////////////////////////////////
   1113 // class SkPDFCIDFont
   1114 ///////////////////////////////////////////////////////////////////////////////
   1115 
   1116 SkPDFCIDFont::SkPDFCIDFont(SkAdvancedTypefaceMetrics* info,
   1117                            SkTypeface* typeface, const SkPDFGlyphSet* subset)
   1118         : SkPDFFont(info, typeface, NULL) {
   1119     populate(subset);
   1120 }
   1121 
   1122 SkPDFCIDFont::~SkPDFCIDFont() {}
   1123 
   1124 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth,
   1125                                      const SkTDArray<uint32_t>* subset) {
   1126     SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor"));
   1127     setFontDescriptor(descriptor.get());
   1128     addResource(descriptor.get());
   1129     insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
   1130     if (!addCommonFontDescriptorEntries(defaultWidth)) {
   1131         return false;
   1132     }
   1133     if (!canEmbed()) {
   1134         return true;
   1135     }
   1136 
   1137     switch (getType()) {
   1138         case SkAdvancedTypefaceMetrics::kTrueType_Font: {
   1139             SkAutoTUnref<SkPDFStream> fontStream;
   1140             size_t fontSize = 0;
   1141             if (canSubset()) {
   1142                 SkPDFStream* rawStream = NULL;
   1143                 fontSize = get_subset_font_stream(fontInfo()->fFontName.c_str(),
   1144                                                   typeface(),
   1145                                                   *subset,
   1146                                                   &rawStream);
   1147                 fontStream.reset(rawStream);
   1148             } else {
   1149                 int ttcIndex;
   1150                 SkAutoTUnref<SkStream> fontData(
   1151                         typeface()->openStream(&ttcIndex));
   1152                 fontStream.reset(new SkPDFStream(fontData.get()));
   1153                 fontSize = fontData->getLength();
   1154             }
   1155             SkASSERT(fontSize);
   1156             SkASSERT(fontStream.get());
   1157             addResource(fontStream.get());
   1158 
   1159             fontStream->insertInt("Length1", fontSize);
   1160             descriptor->insert("FontFile2",
   1161                                new SkPDFObjRef(fontStream.get()))->unref();
   1162             break;
   1163         }
   1164         case SkAdvancedTypefaceMetrics::kCFF_Font:
   1165         case SkAdvancedTypefaceMetrics::kType1CID_Font: {
   1166             int ttcIndex;
   1167             SkAutoTUnref<SkStream> fontData(typeface()->openStream(&ttcIndex));
   1168             SkAutoTUnref<SkPDFStream> fontStream(
   1169                 new SkPDFStream(fontData.get()));
   1170             addResource(fontStream.get());
   1171 
   1172             if (getType() == SkAdvancedTypefaceMetrics::kCFF_Font) {
   1173                 fontStream->insertName("Subtype", "Type1C");
   1174             } else {
   1175                 fontStream->insertName("Subtype", "CIDFontType0c");
   1176             }
   1177             descriptor->insert("FontFile3",
   1178                                 new SkPDFObjRef(fontStream.get()))->unref();
   1179             break;
   1180         }
   1181         default:
   1182             SkASSERT(false);
   1183     }
   1184     return true;
   1185 }
   1186 
   1187 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) {
   1188     // Generate new font metrics with advance info for true type fonts.
   1189     if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
   1190         // Generate glyph id array.
   1191         SkTDArray<uint32_t> glyphIDs;
   1192         if (subset) {
   1193             // Always include glyph 0.
   1194             if (!subset->has(0)) {
   1195                 glyphIDs.push(0);
   1196             }
   1197             subset->exportTo(&glyphIDs);
   1198         }
   1199 
   1200         SkAdvancedTypefaceMetrics::PerGlyphInfo info;
   1201         info = SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo;
   1202         info = SkTBitOr<SkAdvancedTypefaceMetrics::PerGlyphInfo>(
   1203                   info, SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo);
   1204         uint32_t* glyphs = (glyphIDs.count() == 0) ? NULL : glyphIDs.begin();
   1205         uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0;
   1206         SkAutoTUnref<SkAdvancedTypefaceMetrics> fontMetrics(
   1207             typeface()->getAdvancedTypefaceMetrics(info, glyphs, glyphsCount));
   1208         setFontInfo(fontMetrics.get());
   1209         addFontDescriptor(0, &glyphIDs);
   1210     } else {
   1211         // Other CID fonts
   1212         addFontDescriptor(0, NULL);
   1213     }
   1214 
   1215     insertName("BaseFont", fontInfo()->fFontName);
   1216 
   1217     if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) {
   1218         insertName("Subtype", "CIDFontType0");
   1219     } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) {
   1220         insertName("Subtype", "CIDFontType2");
   1221         insertName("CIDToGIDMap", "Identity");
   1222     } else {
   1223         SkASSERT(false);
   1224     }
   1225 
   1226     SkAutoTUnref<SkPDFDict> sysInfo(new SkPDFDict);
   1227     sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref();
   1228     sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref();
   1229     sysInfo->insertInt("Supplement", 0);
   1230     insert("CIDSystemInfo", sysInfo.get());
   1231 
   1232     if (fontInfo()->fGlyphWidths.get()) {
   1233         int16_t defaultWidth = 0;
   1234         SkAutoTUnref<SkPDFArray> widths(
   1235             composeAdvanceData(fontInfo()->fGlyphWidths.get(),
   1236                                fontInfo()->fEmSize, &appendWidth,
   1237                                &defaultWidth));
   1238         if (widths->size())
   1239             insert("W", widths.get());
   1240         if (defaultWidth != 0) {
   1241             insertScalar("DW", scaleFromFontUnits(defaultWidth,
   1242                                                   fontInfo()->fEmSize));
   1243         }
   1244     }
   1245     if (fontInfo()->fVerticalMetrics.get()) {
   1246         struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance;
   1247         defaultAdvance.fVerticalAdvance = 0;
   1248         defaultAdvance.fOriginXDisp = 0;
   1249         defaultAdvance.fOriginYDisp = 0;
   1250         SkAutoTUnref<SkPDFArray> advances(
   1251             composeAdvanceData(fontInfo()->fVerticalMetrics.get(),
   1252                                fontInfo()->fEmSize, &appendVerticalAdvance,
   1253                                &defaultAdvance));
   1254         if (advances->size())
   1255             insert("W2", advances.get());
   1256         if (defaultAdvance.fVerticalAdvance ||
   1257                 defaultAdvance.fOriginXDisp ||
   1258                 defaultAdvance.fOriginYDisp) {
   1259             insert("DW2", appendVerticalAdvance(defaultAdvance,
   1260                                                 fontInfo()->fEmSize,
   1261                                                 new SkPDFArray))->unref();
   1262         }
   1263     }
   1264 
   1265     return true;
   1266 }
   1267 
   1268 ///////////////////////////////////////////////////////////////////////////////
   1269 // class SkPDFType1Font
   1270 ///////////////////////////////////////////////////////////////////////////////
   1271 
   1272 SkPDFType1Font::SkPDFType1Font(SkAdvancedTypefaceMetrics* info,
   1273                                SkTypeface* typeface,
   1274                                uint16_t glyphID,
   1275                                SkPDFDict* relatedFontDescriptor)
   1276         : SkPDFFont(info, typeface, relatedFontDescriptor) {
   1277     populate(glyphID);
   1278 }
   1279 
   1280 SkPDFType1Font::~SkPDFType1Font() {}
   1281 
   1282 bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) {
   1283     if (getFontDescriptor() != NULL) {
   1284         SkPDFDict* descriptor = getFontDescriptor();
   1285         addResource(descriptor);
   1286         insert("FontDescriptor", new SkPDFObjRef(descriptor))->unref();
   1287         return true;
   1288     }
   1289 
   1290     SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor"));
   1291     setFontDescriptor(descriptor.get());
   1292 
   1293     int ttcIndex;
   1294     size_t header SK_INIT_TO_AVOID_WARNING;
   1295     size_t data SK_INIT_TO_AVOID_WARNING;
   1296     size_t trailer SK_INIT_TO_AVOID_WARNING;
   1297     SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex));
   1298     SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data,
   1299                                            &trailer);
   1300     if (fontData == NULL) {
   1301         return false;
   1302     }
   1303     if (canEmbed()) {
   1304         SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData));
   1305         addResource(fontStream.get());
   1306         fontStream->insertInt("Length1", header);
   1307         fontStream->insertInt("Length2", data);
   1308         fontStream->insertInt("Length3", trailer);
   1309         descriptor->insert("FontFile",
   1310                            new SkPDFObjRef(fontStream.get()))->unref();
   1311     }
   1312 
   1313     addResource(descriptor.get());
   1314     insert("FontDescriptor", new SkPDFObjRef(descriptor.get()))->unref();
   1315 
   1316     return addCommonFontDescriptorEntries(defaultWidth);
   1317 }
   1318 
   1319 bool SkPDFType1Font::populate(int16_t glyphID) {
   1320     SkASSERT(!fontInfo()->fVerticalMetrics.get());
   1321     SkASSERT(fontInfo()->fGlyphWidths.get());
   1322 
   1323     adjustGlyphRangeForSingleByteEncoding(glyphID);
   1324 
   1325     int16_t defaultWidth = 0;
   1326     const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL;
   1327     const SkAdvancedTypefaceMetrics::WidthRange* widthEntry;
   1328     for (widthEntry = fontInfo()->fGlyphWidths.get();
   1329             widthEntry != NULL;
   1330             widthEntry = widthEntry->fNext.get()) {
   1331         switch (widthEntry->fType) {
   1332             case SkAdvancedTypefaceMetrics::WidthRange::kDefault:
   1333                 defaultWidth = widthEntry->fAdvance[0];
   1334                 break;
   1335             case SkAdvancedTypefaceMetrics::WidthRange::kRun:
   1336                 SkASSERT(false);
   1337                 break;
   1338             case SkAdvancedTypefaceMetrics::WidthRange::kRange:
   1339                 SkASSERT(widthRangeEntry == NULL);
   1340                 widthRangeEntry = widthEntry;
   1341                 break;
   1342         }
   1343     }
   1344 
   1345     if (!addFontDescriptor(defaultWidth)) {
   1346         return false;
   1347     }
   1348 
   1349     insertName("Subtype", "Type1");
   1350     insertName("BaseFont", fontInfo()->fFontName);
   1351 
   1352     addWidthInfoFromRange(defaultWidth, widthRangeEntry);
   1353 
   1354     SkAutoTUnref<SkPDFDict> encoding(new SkPDFDict("Encoding"));
   1355     insert("Encoding", encoding.get());
   1356 
   1357     SkAutoTUnref<SkPDFArray> encDiffs(new SkPDFArray);
   1358     encoding->insert("Differences", encDiffs.get());
   1359 
   1360     encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
   1361     encDiffs->appendInt(1);
   1362     for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
   1363         encDiffs->appendName(fontInfo()->fGlyphNames->get()[gID].c_str());
   1364     }
   1365 
   1366     return true;
   1367 }
   1368 
   1369 void SkPDFType1Font::addWidthInfoFromRange(
   1370         int16_t defaultWidth,
   1371         const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) {
   1372     SkAutoTUnref<SkPDFArray> widthArray(new SkPDFArray());
   1373     int firstChar = 0;
   1374     if (widthRangeEntry) {
   1375         const uint16_t emSize = fontInfo()->fEmSize;
   1376         int startIndex = firstGlyphID() - widthRangeEntry->fStartId;
   1377         int endIndex = startIndex + lastGlyphID() - firstGlyphID() + 1;
   1378         if (startIndex < 0)
   1379             startIndex = 0;
   1380         if (endIndex > widthRangeEntry->fAdvance.count())
   1381             endIndex = widthRangeEntry->fAdvance.count();
   1382         if (widthRangeEntry->fStartId == 0) {
   1383             appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get());
   1384         } else {
   1385             firstChar = startIndex + widthRangeEntry->fStartId;
   1386         }
   1387         for (int i = startIndex; i < endIndex; i++) {
   1388             appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get());
   1389         }
   1390     } else {
   1391         appendWidth(defaultWidth, 1000, widthArray.get());
   1392     }
   1393     insertInt("FirstChar", firstChar);
   1394     insertInt("LastChar", firstChar + widthArray->size() - 1);
   1395     insert("Widths", widthArray.get());
   1396 }
   1397 
   1398 ///////////////////////////////////////////////////////////////////////////////
   1399 // class SkPDFType3Font
   1400 ///////////////////////////////////////////////////////////////////////////////
   1401 
   1402 SkPDFType3Font::SkPDFType3Font(SkAdvancedTypefaceMetrics* info,
   1403                                SkTypeface* typeface,
   1404                                uint16_t glyphID)
   1405         : SkPDFFont(info, typeface, NULL) {
   1406     populate(glyphID);
   1407 }
   1408 
   1409 SkPDFType3Font::~SkPDFType3Font() {}
   1410 
   1411 bool SkPDFType3Font::populate(int16_t glyphID) {
   1412     SkPaint paint;
   1413     paint.setTypeface(typeface());
   1414     paint.setTextSize(1000);
   1415     SkAutoGlyphCache autoCache(paint, NULL, NULL);
   1416     SkGlyphCache* cache = autoCache.getCache();
   1417     // If fLastGlyphID isn't set (because there is not fFontInfo), look it up.
   1418     if (lastGlyphID() == 0) {
   1419         setLastGlyphID(cache->getGlyphCount() - 1);
   1420     }
   1421 
   1422     adjustGlyphRangeForSingleByteEncoding(glyphID);
   1423 
   1424     insertName("Subtype", "Type3");
   1425     // Flip about the x-axis and scale by 1/1000.
   1426     SkMatrix fontMatrix;
   1427     fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000));
   1428     insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref();
   1429 
   1430     SkAutoTUnref<SkPDFDict> charProcs(new SkPDFDict);
   1431     insert("CharProcs", charProcs.get());
   1432 
   1433     SkAutoTUnref<SkPDFDict> encoding(new SkPDFDict("Encoding"));
   1434     insert("Encoding", encoding.get());
   1435 
   1436     SkAutoTUnref<SkPDFArray> encDiffs(new SkPDFArray);
   1437     encoding->insert("Differences", encDiffs.get());
   1438     encDiffs->reserve(lastGlyphID() - firstGlyphID() + 2);
   1439     encDiffs->appendInt(1);
   1440 
   1441     SkAutoTUnref<SkPDFArray> widthArray(new SkPDFArray());
   1442 
   1443     SkIRect bbox = SkIRect::MakeEmpty();
   1444     for (int gID = firstGlyphID(); gID <= lastGlyphID(); gID++) {
   1445         SkString characterName;
   1446         characterName.printf("gid%d", gID);
   1447         encDiffs->appendName(characterName.c_str());
   1448 
   1449         const SkGlyph& glyph = cache->getGlyphIDMetrics(gID);
   1450         widthArray->appendScalar(SkFixedToScalar(glyph.fAdvanceX));
   1451         SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop,
   1452                                               glyph.fWidth, glyph.fHeight);
   1453         bbox.join(glyphBBox);
   1454 
   1455         SkDynamicMemoryWStream content;
   1456         setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox,
   1457                                     &content);
   1458         const SkPath* path = cache->findPath(glyph);
   1459         if (path) {
   1460             SkPDFUtils::EmitPath(*path, paint.getStyle(), &content);
   1461             SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
   1462                                   &content);
   1463         }
   1464         SkAutoTUnref<SkMemoryStream> glyphStream(new SkMemoryStream());
   1465         glyphStream->setData(content.copyToData())->unref();
   1466 
   1467         SkAutoTUnref<SkPDFStream> glyphDescription(
   1468             new SkPDFStream(glyphStream.get()));
   1469         addResource(glyphDescription.get());
   1470         charProcs->insert(characterName.c_str(),
   1471                           new SkPDFObjRef(glyphDescription.get()))->unref();
   1472     }
   1473 
   1474     insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
   1475     insertInt("FirstChar", 1);
   1476     insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1);
   1477     insert("Widths", widthArray.get());
   1478     insertName("CIDToGIDMap", "Identity");
   1479 
   1480     populateToUnicodeTable(NULL);
   1481     return true;
   1482 }
   1483