gfx/skia/trunk/src/pdf/SkPDFFont.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial