SkPDFFont.cpp revision 5b073680ecba631da68e33d0d2f28f10a07110ce
1561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/* 2561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Copyright (C) 2011 Google Inc. 3561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 4561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * you may not use this file except in compliance with the License. 6561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * You may obtain a copy of the License at 7561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 8561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 10561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * See the License for the specific language governing permissions and 14561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * limitations under the License. 15561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 16561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 17561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include <ctype.h> 18561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 198d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath#include "SkFontHost.h" 208d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath#include "SkGlyphCache.h" 21561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkPaint.h" 22561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkPDFDevice.h" 23561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkPDFFont.h" 24561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkPDFStream.h" 25561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkPDFTypes.h" 26561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkPDFUtils.h" 27561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkScalar.h" 28561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkStream.h" 29561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkTypeface.h" 30561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes#include "SkUtils.h" 31561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 32561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesnamespace { 33561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 348d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamathbool parsePFBSection(const uint8_t** src, size_t* len, int sectionType, 35561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes size_t* size) { 36561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // PFB sections have a two or six bytes header. 0x80 and a one byte 37561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // section type followed by a four byte section length. Type one is 38561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // an ASCII section (includes a length), type two is a binary section 39561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // (includes a length) and type three is an EOF marker with no length. 40561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes const uint8_t* buf = *src; 41561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (*len < 2 || buf[0] != 0x80 || buf[1] != sectionType) 42561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes return false; 438d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath if (buf[1] == 3) 44561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes return true; 45561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (*len < 6) 46561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes return false; 47561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 48561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *size = buf[2] | (buf[3] << 8) | (buf[4] << 16) | (buf[5] << 24); 49561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes size_t consumed = *size + 6; 50561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (consumed > *len) 51 return false; 52 *src = *src + consumed; 53 *len = *len - consumed; 54 return true; 55} 56 57bool parsePFB(const uint8_t* src, size_t size, size_t* headerLen, 58 size_t* dataLen, size_t* trailerLen) { 59 const uint8_t* srcPtr = src; 60 size_t remaining = size; 61 62 return parsePFBSection(&srcPtr, &remaining, 1, headerLen) && 63 parsePFBSection(&srcPtr, &remaining, 2, dataLen) && 64 parsePFBSection(&srcPtr, &remaining, 1, trailerLen) && 65 parsePFBSection(&srcPtr, &remaining, 3, NULL); 66} 67 68/* The sections of a PFA file are implicitly defined. The body starts 69 * after the line containing "eexec," and the trailer starts with 512 70 * literal 0's followed by "cleartomark" (plus arbitrary white space). 71 * 72 * This function assumes that src is NUL terminated, but the NUL 73 * termination is not included in size. 74 * 75 */ 76bool parsePFA(const char* src, size_t size, size_t* headerLen, 77 size_t* hexDataLen, size_t* dataLen, size_t* trailerLen) { 78 const char* end = src + size; 79 80 const char* dataPos = strstr(src, "eexec"); 81 if (!dataPos) 82 return false; 83 dataPos += strlen("eexec"); 84 while ((*dataPos == '\n' || *dataPos == '\r' || *dataPos == ' ') && 85 dataPos < end) 86 dataPos++; 87 *headerLen = dataPos - src; 88 89 const char* trailerPos = strstr(dataPos, "cleartomark"); 90 if (!trailerPos) 91 return false; 92 int zeroCount = 0; 93 for (trailerPos--; trailerPos > dataPos && zeroCount < 512; trailerPos--) { 94 if (*trailerPos == '\n' || *trailerPos == '\r' || *trailerPos == ' ') { 95 continue; 96 } else if (*trailerPos == '0') { 97 zeroCount++; 98 } else { 99 return false; 100 } 101 } 102 if (zeroCount != 512) 103 return false; 104 105 *hexDataLen = trailerPos - src - *headerLen; 106 *trailerLen = size - *headerLen - *hexDataLen; 107 108 // Verify that the data section is hex encoded and count the bytes. 109 int nibbles = 0; 110 for (; dataPos < trailerPos; dataPos++) { 111 if (isspace(*dataPos)) 112 continue; 113 if (!isxdigit(*dataPos)) 114 return false; 115 nibbles++; 116 } 117 *dataLen = (nibbles + 1) / 2; 118 119 return true; 120} 121 122int8_t hexToBin(uint8_t c) { 123 if (!isxdigit(c)) 124 return -1; 125 if (c <= '9') return c - '0'; 126 if (c <= 'F') return c - 'A' + 10; 127 if (c <= 'f') return c - 'a' + 10; 128 return -1; 129} 130 131SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen, 132 size_t* dataLen, size_t* trailerLen) { 133 // srcStream may be backed by a file or a unseekable fd, so we may not be 134 // able to use skip(), rewind(), or getMemoryBase(). read()ing through 135 // the input only once is doable, but very ugly. Furthermore, it'd be nice 136 // if the data was NUL terminated so that we can use strstr() to search it. 137 // Make as few copies as possible given these constraints. 138 SkDynamicMemoryWStream dynamicStream; 139 SkRefPtr<SkMemoryStream> staticStream; 140 const uint8_t* src; 141 size_t srcLen; 142 if ((srcLen = srcStream->getLength()) > 0) { 143 staticStream = new SkMemoryStream(srcLen + 1); 144 staticStream->unref(); // new and SkRefPtr both took a ref. 145 src = (const uint8_t*)staticStream->getMemoryBase(); 146 if (srcStream->getMemoryBase() != NULL) { 147 memcpy((void *)src, srcStream->getMemoryBase(), srcLen); 148 } else { 149 size_t read = 0; 150 while (read < srcLen) { 151 size_t got = srcStream->read((void *)staticStream->getAtPos(), 152 srcLen - read); 153 if (got == 0) 154 return NULL; 155 read += got; 156 staticStream->seek(read); 157 } 158 } 159 ((uint8_t *)src)[srcLen] = 0; 160 } else { 161 static const size_t bufSize = 4096; 162 uint8_t buf[bufSize]; 163 size_t amount; 164 while ((amount = srcStream->read(buf, bufSize)) > 0) 165 dynamicStream.write(buf, amount); 166 amount = 0; 167 dynamicStream.write(&amount, 1); // NULL terminator. 168 // getStream makes another copy, but we couldn't do any better. 169 src = (const uint8_t*)dynamicStream.getStream(); 170 srcLen = dynamicStream.getOffset() - 1; 171 } 172 173 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) { 174 SkMemoryStream* result = 175 new SkMemoryStream(*headerLen + *dataLen + *trailerLen); 176 memcpy((char*)result->getAtPos(), src + 6, *headerLen); 177 result->seek(*headerLen); 178 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen); 179 result->seek(*headerLen + *dataLen); 180 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen, 181 *trailerLen); 182 result->rewind(); 183 return result; 184 } 185 186 // A PFA has to be converted for PDF. 187 size_t hexDataLen; 188 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen, 189 trailerLen)) { 190 SkMemoryStream* result = 191 new SkMemoryStream(*headerLen + *dataLen + *trailerLen); 192 memcpy((char*)result->getAtPos(), src, *headerLen); 193 result->seek(*headerLen); 194 195 const uint8_t* hexData = src + *headerLen; 196 const uint8_t* trailer = hexData + hexDataLen; 197 size_t outputOffset = 0; 198 uint8_t dataByte = 0; // To hush compiler. 199 bool highNibble = true; 200 for (; hexData < trailer; hexData++) { 201 char curNibble = hexToBin(*hexData); 202 if (curNibble < 0) 203 continue; 204 if (highNibble) { 205 dataByte = curNibble << 4; 206 highNibble = false; 207 } else { 208 dataByte |= curNibble; 209 highNibble = true; 210 ((char *)result->getAtPos())[outputOffset++] = dataByte; 211 } 212 } 213 if (!highNibble) 214 ((char *)result->getAtPos())[outputOffset++] = dataByte; 215 SkASSERT(outputOffset == *dataLen); 216 result->seek(*headerLen + outputOffset); 217 218 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen, 219 *trailerLen); 220 result->rewind(); 221 return result; 222 } 223 224 return NULL; 225} 226 227SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { 228 if (emSize == 1000) 229 return SkIntToScalar(val); 230 int intVal = ((int)val) * 1000; 231 return SkIntToScalar(intVal) * SkScalarInvert(SkIntToScalar(emSize)); 232} 233 234void setGlyphWidthAndBoundingBox(SkScalar width, SkIRect box, 235 SkString* content) { 236 // Specify width and bounding box for the glyph. 237 SkPDFScalar::Append(width, content); 238 content->appendf(" 0 %d %d %d %d d1\n", box.fLeft, box.fTop, 239 box.fRight, box.fBottom); 240} 241 242SkPDFArray* makeFontBBox( 243 SkIRect glyphBBox, uint16_t emSize, 244 SkPDFDevice::OriginTransform flipOrigin = 245 SkPDFDevice::kNoFlip_OriginTransform) { 246 if (flipOrigin == SkPDFDevice::kFlip_OriginTransform) { 247 int32_t temp = -glyphBBox.fTop; 248 glyphBBox.fTop = -glyphBBox.fBottom; 249 glyphBBox.fBottom = temp; 250 } 251 SkPDFArray* bbox = new SkPDFArray; 252 bbox->reserve(4); 253 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fLeft, 254 emSize)))->unref(); 255 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fBottom, 256 emSize)))->unref(); 257 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fRight, 258 emSize)))->unref(); 259 bbox->append(new SkPDFScalar(scaleFromFontUnits(glyphBBox.fTop, 260 emSize)))->unref(); 261 return bbox; 262} 263 264SkPDFArray* appendWidth(const int16_t& width, uint16_t emSize, 265 SkPDFArray* array) { 266 array->append(new SkPDFScalar(scaleFromFontUnits(width, emSize)))->unref(); 267 return array; 268} 269 270SkPDFArray* appendVerticalAdvance( 271 const SkAdvancedTypefaceMetrics::VerticalMetric& advance, 272 uint16_t emSize, SkPDFArray* array) { 273 appendWidth(advance.fVerticalAdvance, emSize, array); 274 appendWidth(advance.fOriginXDisp, emSize, array); 275 appendWidth(advance.fOriginYDisp, emSize, array); 276 return array; 277} 278 279template <typename Data> 280SkPDFArray* composeAdvanceData( 281 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* advanceInfo, 282 uint16_t emSize, 283 SkPDFArray* (*appendAdvance)(const Data& advance, uint16_t emSize, 284 SkPDFArray* array), 285 Data* defaultAdvance) { 286 SkPDFArray* result = new SkPDFArray(); 287 for (; advanceInfo != NULL; advanceInfo = advanceInfo->fNext.get()) { 288 switch (advanceInfo->fType) { 289 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: { 290 SkASSERT(advanceInfo->fAdvance.count() == 1); 291 *defaultAdvance = advanceInfo->fAdvance[0]; 292 break; 293 } 294 case SkAdvancedTypefaceMetrics::WidthRange::kRange: { 295 SkRefPtr<SkPDFArray> advanceArray = new SkPDFArray(); 296 advanceArray->unref(); // SkRefPtr and new both took a ref. 297 for (int j = 0; j < advanceInfo->fAdvance.count(); j++) 298 appendAdvance(advanceInfo->fAdvance[j], emSize, 299 advanceArray.get()); 300 result->append(new SkPDFInt(advanceInfo->fStartId))->unref(); 301 result->append(advanceArray.get()); 302 break; 303 } 304 case SkAdvancedTypefaceMetrics::WidthRange::kRun: { 305 SkASSERT(advanceInfo->fAdvance.count() == 1); 306 result->append(new SkPDFInt(advanceInfo->fStartId))->unref(); 307 result->append(new SkPDFInt(advanceInfo->fEndId))->unref(); 308 appendAdvance(advanceInfo->fAdvance[0], emSize, result); 309 break; 310 } 311 } 312 } 313 return result; 314} 315 316} // namespace 317 318/* Font subset design: It would be nice to be able to subset fonts 319 * (particularly type 3 fonts), but it's a lot of work and not a priority. 320 * 321 * Resources are canonicalized and uniqueified by pointer so there has to be 322 * some additional state indicating which subset of the font is used. It 323 * must be maintained at the page granularity and then combined at the document 324 * granularity. a) change SkPDFFont to fill in its state on demand, kind of 325 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each 326 * page/pdf device. c) in the document, retrieve the per font glyph usage 327 * from each page and combine it and ask for a resource with that subset. 328 */ 329 330SkPDFFont::~SkPDFFont() { 331 SkAutoMutexAcquire lock(canonicalFontsMutex()); 332 int index; 333 if (find(SkTypeface::UniqueID(fTypeface.get()), fFirstGlyphID, &index)) { 334 canonicalFonts().removeShuffle(index); 335#ifdef SK_DEBUG 336 SkASSERT(!fDescendant); 337 } else { 338 SkASSERT(fDescendant); 339#endif 340 } 341 fResources.unrefAll(); 342} 343 344void SkPDFFont::getResources(SkTDArray<SkPDFObject*>* resourceList) { 345 resourceList->setReserve(resourceList->count() + fResources.count()); 346 for (int i = 0; i < fResources.count(); i++) { 347 resourceList->push(fResources[i]); 348 fResources[i]->ref(); 349 fResources[i]->getResources(resourceList); 350 } 351} 352 353SkTypeface* SkPDFFont::typeface() { 354 return fTypeface.get(); 355} 356 357bool SkPDFFont::hasGlyph(uint16_t id) { 358 return (id >= fFirstGlyphID && id <= fLastGlyphID) || id == 0; 359} 360 361bool SkPDFFont::multiByteGlyphs() { 362 return fMultiByteGlyphs; 363} 364 365size_t SkPDFFont::glyphsToPDFFontEncoding(uint16_t* glyphIDs, 366 size_t numGlyphs) { 367 // A font with multibyte glyphs will support all glyph IDs in a single font. 368 if (fMultiByteGlyphs) { 369 return numGlyphs; 370 } 371 372 for (size_t i = 0; i < numGlyphs; i++) { 373 if (glyphIDs[i] == 0) { 374 continue; 375 } 376 if (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID) { 377 return i; 378 } 379 glyphIDs[i] -= (fFirstGlyphID - 1); 380 } 381 382 return numGlyphs; 383} 384 385// static 386SkPDFFont* SkPDFFont::getFontResource(SkTypeface* typeface, uint16_t glyphID) { 387 SkAutoMutexAcquire lock(canonicalFontsMutex()); 388 const uint32_t fontID = SkTypeface::UniqueID(typeface); 389 int index; 390 if (find(fontID, glyphID, &index)) { 391 canonicalFonts()[index].fFont->ref(); 392 return canonicalFonts()[index].fFont; 393 } 394 395 SkRefPtr<SkAdvancedTypefaceMetrics> fontInfo; 396 SkPDFDict* fontDescriptor = NULL; 397 if (index >= 0) { 398 SkPDFFont* relatedFont = canonicalFonts()[index].fFont; 399 SkASSERT(relatedFont->fFontInfo.get()); 400 fontInfo = relatedFont->fFontInfo; 401 fontDescriptor = relatedFont->fDescriptor.get(); 402 } else { 403 fontInfo = SkFontHost::GetAdvancedTypefaceMetrics(fontID, true); 404 fontInfo->unref(); // SkRefPtr and get info both took a reference. 405 } 406 407 SkPDFFont* font = new SkPDFFont(fontInfo.get(), typeface, glyphID, false, 408 fontDescriptor); 409 FontRec newEntry(font, fontID, font->fFirstGlyphID); 410 index = canonicalFonts().count(); 411 canonicalFonts().push(newEntry); 412 return font; // Return the reference new SkPDFFont() created. 413} 414 415// static 416SkTDArray<SkPDFFont::FontRec>& SkPDFFont::canonicalFonts() { 417 // This initialization is only thread safe with gcc. 418 static SkTDArray<FontRec> gCanonicalFonts; 419 return gCanonicalFonts; 420} 421 422// static 423SkMutex& SkPDFFont::canonicalFontsMutex() { 424 // This initialization is only thread safe with gcc. 425 static SkMutex gCanonicalFontsMutex; 426 return gCanonicalFontsMutex; 427} 428 429// static 430bool SkPDFFont::find(uint32_t fontID, uint16_t glyphID, int* index) { 431 // TODO(vandebo) optimize this, do only one search? 432 FontRec search(NULL, fontID, glyphID); 433 *index = canonicalFonts().find(search); 434 if (*index >= 0) 435 return true; 436 search.fGlyphID = 0; 437 *index = canonicalFonts().find(search); 438 return false; 439} 440 441SkPDFFont::SkPDFFont(class SkAdvancedTypefaceMetrics* fontInfo, 442 SkTypeface* typeface, 443 uint16_t glyphID, 444 bool descendantFont, 445 SkPDFDict* fontDescriptor) 446 : SkPDFDict("Font"), 447 fTypeface(typeface), 448#ifdef SK_DEBUG 449 fDescendant(descendantFont), 450#endif 451 fMultiByteGlyphs(false), 452 fFirstGlyphID(1), 453 fLastGlyphID(fontInfo->fLastGlyphID), 454 fFontInfo(fontInfo), 455 fDescriptor(fontDescriptor) { 456 457 if (fontInfo->fMultiMaster) { 458 SkASSERT(false); // Not supported yet. 459 fontInfo->fType = SkAdvancedTypefaceMetrics::kOther_Font; 460 } 461 if (fontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font || 462 fontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { 463 if (descendantFont) { 464 populateCIDFont(); 465 } else { 466 populateType0Font(); 467 } 468 // No need to hold onto the font info for fonts types that 469 // support multibyte glyphs. 470 fFontInfo = NULL; 471 return; 472 } 473 474 // Single byte glyph encoding supports a max of 255 glyphs. 475 fFirstGlyphID = glyphID - (glyphID - 1) % 255; 476 if (fLastGlyphID > fFirstGlyphID + 255 - 1) { 477 fLastGlyphID = fFirstGlyphID + 255 - 1; 478 } 479 480 if (fontInfo->fType == SkAdvancedTypefaceMetrics::kType1_Font && 481 populateType1Font()) { 482 return; 483 } 484 485 SkASSERT(fontInfo->fType == SkAdvancedTypefaceMetrics::kType1_Font || 486 fontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font || 487 fontInfo->fType == SkAdvancedTypefaceMetrics::kOther_Font || 488 fontInfo->fType == SkAdvancedTypefaceMetrics::kNotEmbeddable_Font); 489 populateType3Font(); 490} 491 492void SkPDFFont::populateType0Font() { 493 // TODO(vandebo) add a ToUnicode mapping. 494 fMultiByteGlyphs = true; 495 496 insert("Subtype", new SkPDFName("Type0"))->unref(); 497 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref(); 498 insert("Encoding", new SkPDFName("Identity-H"))->unref(); 499 500 SkRefPtr<SkPDFArray> descendantFonts = new SkPDFArray(); 501 descendantFonts->unref(); // SkRefPtr and new took a reference. 502 503 // Pass ref new created to fResources. 504 fResources.push( 505 new SkPDFFont(fFontInfo.get(), fTypeface.get(), 1, true, NULL)); 506 descendantFonts->append(new SkPDFObjRef(fResources.top()))->unref(); 507 insert("DescendantFonts", descendantFonts.get()); 508} 509 510void SkPDFFont::populateCIDFont() { 511 fMultiByteGlyphs = true; 512 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref(); 513 514 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kType1CID_Font) { 515 insert("Subtype", new SkPDFName("CIDFontType0"))->unref(); 516 } else if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { 517 insert("Subtype", new SkPDFName("CIDFontType2"))->unref(); 518 } else { 519 SkASSERT(false); 520 } 521 522 SkRefPtr<SkPDFDict> sysInfo = new SkPDFDict; 523 sysInfo->unref(); // SkRefPtr and new both took a reference. 524 sysInfo->insert("Registry", new SkPDFString("Adobe"))->unref(); 525 sysInfo->insert("Ordering", new SkPDFString("Identity"))->unref(); 526 sysInfo->insert("Supplement", new SkPDFInt(0))->unref(); 527 insert("CIDSystemInfo", sysInfo.get()); 528 529 addFontDescriptor(0); 530 531 if (fFontInfo->fGlyphWidths.get()) { 532 int16_t defaultWidth = 0; 533 SkRefPtr<SkPDFArray> widths = 534 composeAdvanceData(fFontInfo->fGlyphWidths.get(), 535 fFontInfo->fEmSize, &appendWidth, &defaultWidth); 536 widths->unref(); // SkRefPtr and compose both took a reference. 537 if (widths->size()) 538 insert("W", widths.get()); 539 if (defaultWidth != 0) { 540 insert("DW", new SkPDFScalar(scaleFromFontUnits( 541 defaultWidth, fFontInfo->fEmSize)))->unref(); 542 } 543 } 544 if (fFontInfo->fVerticalMetrics.get()) { 545 struct SkAdvancedTypefaceMetrics::VerticalMetric defaultAdvance; 546 defaultAdvance.fVerticalAdvance = 0; 547 defaultAdvance.fOriginXDisp = 0; 548 defaultAdvance.fOriginYDisp = 0; 549 SkRefPtr<SkPDFArray> advances = 550 composeAdvanceData(fFontInfo->fVerticalMetrics.get(), 551 fFontInfo->fEmSize, &appendVerticalAdvance, 552 &defaultAdvance); 553 advances->unref(); // SkRefPtr and compose both took a ref. 554 if (advances->size()) 555 insert("W2", advances.get()); 556 if (defaultAdvance.fVerticalAdvance || 557 defaultAdvance.fOriginXDisp || 558 defaultAdvance.fOriginYDisp) { 559 insert("DW2", appendVerticalAdvance(defaultAdvance, 560 fFontInfo->fEmSize, 561 new SkPDFArray))->unref(); 562 } 563 } 564} 565 566bool SkPDFFont::populateType1Font() { 567 SkASSERT(!fFontInfo->fVerticalMetrics.get()); 568 SkASSERT(fFontInfo->fGlyphWidths.get()); 569 570 int16_t defaultWidth = 0; 571 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry = NULL; 572 const SkAdvancedTypefaceMetrics::WidthRange* widthEntry; 573 for (widthEntry = fFontInfo.get()->fGlyphWidths.get(); 574 widthEntry != NULL; 575 widthEntry = widthEntry->fNext.get()) { 576 switch (widthEntry->fType) { 577 case SkAdvancedTypefaceMetrics::WidthRange::kDefault: 578 defaultWidth = widthEntry->fAdvance[0]; 579 break; 580 case SkAdvancedTypefaceMetrics::WidthRange::kRun: 581 SkASSERT(false); 582 break; 583 case SkAdvancedTypefaceMetrics::WidthRange::kRange: 584 SkASSERT(widthRangeEntry == NULL); 585 widthRangeEntry = widthEntry; 586 break; 587 } 588 } 589 590 if (!addFontDescriptor(defaultWidth)) 591 return false; 592 593 insert("Subtype", new SkPDFName("Type1"))->unref(); 594 insert("BaseFont", new SkPDFName(fFontInfo->fFontName))->unref(); 595 596 addWidthInfoFromRange(defaultWidth, widthRangeEntry); 597 598 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding"); 599 encoding->unref(); // SkRefPtr and new both took a reference. 600 insert("Encoding", encoding.get()); 601 602 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray; 603 encDiffs->unref(); // SkRefPtr and new both took a reference. 604 encoding->insert("Differences", encDiffs.get()); 605 606 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2); 607 encDiffs->append(new SkPDFInt(1))->unref(); 608 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) { 609 encDiffs->append( 610 new SkPDFName(fFontInfo->fGlyphNames->get()[gID]))->unref(); 611 } 612 613 if (fFontInfo->fLastGlyphID <= 255) 614 fFontInfo = NULL; 615 return true; 616} 617 618void SkPDFFont::populateType3Font() { 619 insert("Subtype", new SkPDFName("Type3"))->unref(); 620 // Flip about the x-axis and scale by 1/1000. 621 SkMatrix fontMatrix; 622 fontMatrix.setScale(SkScalarInvert(1000), -SkScalarInvert(1000)); 623 insert("FontMatrix", SkPDFUtils::MatrixToArray(fontMatrix))->unref(); 624 625 SkRefPtr<SkPDFDict> charProcs = new SkPDFDict; 626 charProcs->unref(); // SkRefPtr and new both took a reference. 627 insert("CharProcs", charProcs.get()); 628 629 SkRefPtr<SkPDFDict> encoding = new SkPDFDict("Encoding"); 630 encoding->unref(); // SkRefPtr and new both took a reference. 631 insert("Encoding", encoding.get()); 632 633 SkRefPtr<SkPDFArray> encDiffs = new SkPDFArray; 634 encDiffs->unref(); // SkRefPtr and new both took a reference. 635 encoding->insert("Differences", encDiffs.get()); 636 encDiffs->reserve(fLastGlyphID - fFirstGlyphID + 2); 637 encDiffs->append(new SkPDFInt(1))->unref(); 638 639 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray(); 640 widthArray->unref(); // SkRefPtr and new both took a ref. 641 642 SkPaint paint; 643 paint.setTypeface(fTypeface.get()); 644 paint.setTextSize(1000); 645 SkAutoGlyphCache autoCache(paint, NULL); 646 SkGlyphCache* cache = autoCache.getCache(); 647 648 SkIRect bbox = SkIRect::MakeEmpty(); 649 for (int gID = fFirstGlyphID; gID <= fLastGlyphID; gID++) { 650 SkString characterName; 651 characterName.printf("gid%d", gID); 652 encDiffs->append(new SkPDFName(characterName))->unref(); 653 654 const SkGlyph glyph = cache->getGlyphIDMetrics(gID); 655 appendWidth(SkFixedToFloat(glyph.fAdvanceX), 1000, widthArray.get()); 656 SkIRect glyphBBox = SkIRect::MakeXYWH(glyph.fLeft, glyph.fTop, 657 glyph.fWidth, glyph.fHeight); 658 bbox.join(glyphBBox); 659 660 SkString content; 661 setGlyphWidthAndBoundingBox(SkFixedToScalar(glyph.fAdvanceX), glyphBBox, 662 &content); 663 const SkPath* path = cache->findPath(glyph); 664 if (path) { 665 SkPDFUtils::EmitPath(*path, &content); 666 SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(), 667 &content); 668 } 669 SkRefPtr<SkStream> glyphStream = 670 new SkMemoryStream(content.c_str(), content.size(), true); 671 glyphStream->unref(); // SkRefPtr and new both took a ref. 672 SkRefPtr<SkPDFStream> glyphDescription = 673 new SkPDFStream(glyphStream.get()); 674 // SkRefPtr and new both ref()'d charProcs, pass one. 675 fResources.push(glyphDescription.get()); 676 charProcs->insert(characterName.c_str(), 677 new SkPDFObjRef(glyphDescription.get()))->unref(); 678 } 679 680 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); 681 insert("FirstChar", new SkPDFInt(fFirstGlyphID))->unref(); 682 insert("LastChar", new SkPDFInt(fLastGlyphID))->unref(); 683 insert("Widths", widthArray.get()); 684 685 if (fFontInfo->fLastGlyphID <= 255) 686 fFontInfo = NULL; 687} 688 689bool SkPDFFont::addFontDescriptor(int16_t defaultWidth) { 690 if (fDescriptor.get() != NULL) { 691 fResources.push(fDescriptor.get()); 692 fDescriptor->ref(); 693 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref(); 694 return true; 695 } 696 697 fDescriptor = new SkPDFDict("FontDescriptor"); 698 fDescriptor->unref(); // SkRefPtr and new both took a ref. 699 700 switch (fFontInfo->fType) { 701 case SkAdvancedTypefaceMetrics::kType1_Font: { 702 size_t header, data, trailer; 703 SkRefPtr<SkStream> rawFontData = 704 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get())); 705 rawFontData->unref(); // SkRefPtr and OpenStream both took a ref. 706 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, 707 &data, &trailer); 708 if (fontData == NULL) 709 return false; 710 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData); 711 // SkRefPtr and new both ref()'d fontStream, pass one. 712 fResources.push(fontStream.get()); 713 fontStream->insert("Length1", new SkPDFInt(header))->unref(); 714 fontStream->insert("Length2", new SkPDFInt(data))->unref(); 715 fontStream->insert("Length3", new SkPDFInt(trailer))->unref(); 716 fDescriptor->insert("FontFile", 717 new SkPDFObjRef(fontStream.get()))->unref(); 718 break; 719 } 720 case SkAdvancedTypefaceMetrics::kTrueType_Font: { 721 SkRefPtr<SkStream> fontData = 722 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get())); 723 fontData->unref(); // SkRefPtr and OpenStream both took a ref. 724 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get()); 725 // SkRefPtr and new both ref()'d fontStream, pass one. 726 fResources.push(fontStream.get()); 727 728 fontStream->insert("Length1", 729 new SkPDFInt(fontData->getLength()))->unref(); 730 fDescriptor->insert("FontFile2", 731 new SkPDFObjRef(fontStream.get()))->unref(); 732 break; 733 } 734 case SkAdvancedTypefaceMetrics::kCFF_Font: 735 case SkAdvancedTypefaceMetrics::kType1CID_Font: { 736 SkRefPtr<SkStream> fontData = 737 SkFontHost::OpenStream(SkTypeface::UniqueID(fTypeface.get())); 738 fontData->unref(); // SkRefPtr and OpenStream both took a ref. 739 SkRefPtr<SkPDFStream> fontStream = new SkPDFStream(fontData.get()); 740 // SkRefPtr and new both ref()'d fontStream, pass one. 741 fResources.push(fontStream.get()); 742 743 if (fFontInfo->fType == SkAdvancedTypefaceMetrics::kCFF_Font) { 744 fontStream->insert("Subtype", new SkPDFName("Type1C"))->unref(); 745 } else { 746 fontStream->insert("Subtype", 747 new SkPDFName("CIDFontType0c"))->unref(); 748 } 749 fDescriptor->insert("FontFile3", 750 new SkPDFObjRef(fontStream.get()))->unref(); 751 break; 752 } 753 default: 754 SkASSERT(false); 755 } 756 757 const uint16_t emSize = fFontInfo->fEmSize; 758 fResources.push(fDescriptor.get()); 759 fDescriptor->ref(); 760 insert("FontDescriptor", new SkPDFObjRef(fDescriptor.get()))->unref(); 761 762 fDescriptor->insert("FontName", new SkPDFName( 763 fFontInfo->fFontName))->unref(); 764 fDescriptor->insert("Flags", new SkPDFInt(fFontInfo->fStyle))->unref(); 765 fDescriptor->insert("Ascent", new SkPDFScalar( 766 scaleFromFontUnits(fFontInfo->fAscent, emSize)))->unref(); 767 fDescriptor->insert("Descent", new SkPDFScalar( 768 scaleFromFontUnits(fFontInfo->fDescent, emSize)))->unref(); 769 fDescriptor->insert("StemV", new SkPDFScalar( 770 scaleFromFontUnits(fFontInfo->fStemV, emSize)))->unref(); 771 fDescriptor->insert("CapHeight", new SkPDFScalar( 772 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)))->unref(); 773 fDescriptor->insert("ItalicAngle", new SkPDFInt( 774 fFontInfo->fItalicAngle))->unref(); 775 fDescriptor->insert("FontBBox", makeFontBBox(fFontInfo->fBBox, 776 fFontInfo->fEmSize))->unref(); 777 778 if (defaultWidth > 0) { 779 fDescriptor->insert("MissingWidth", new SkPDFScalar( 780 scaleFromFontUnits(defaultWidth, emSize)))->unref(); 781 } 782 return true; 783} 784void SkPDFFont::addWidthInfoFromRange( 785 int16_t defaultWidth, 786 const SkAdvancedTypefaceMetrics::WidthRange* widthRangeEntry) { 787 SkRefPtr<SkPDFArray> widthArray = new SkPDFArray(); 788 widthArray->unref(); // SkRefPtr and new both took a ref. 789 int firstChar = 0; 790 if (widthRangeEntry) { 791 const uint16_t emSize = fFontInfo->fEmSize; 792 int startIndex = fFirstGlyphID - widthRangeEntry->fStartId; 793 int endIndex = startIndex + fLastGlyphID - fFirstGlyphID + 1; 794 if (startIndex < 0) 795 startIndex = 0; 796 if (endIndex > widthRangeEntry->fAdvance.count()) 797 endIndex = widthRangeEntry->fAdvance.count(); 798 if (widthRangeEntry->fStartId == 0) { 799 appendWidth(widthRangeEntry->fAdvance[0], emSize, widthArray.get()); 800 } else { 801 firstChar = startIndex + widthRangeEntry->fStartId; 802 } 803 for (int i = startIndex; i < endIndex; i++) 804 appendWidth(widthRangeEntry->fAdvance[i], emSize, widthArray.get()); 805 } else { 806 appendWidth(defaultWidth, 1000, widthArray.get()); 807 } 808 insert("FirstChar", new SkPDFInt(firstChar))->unref(); 809 insert("LastChar", 810 new SkPDFInt(firstChar + widthArray->size() - 1))->unref(); 811 insert("Widths", widthArray.get()); 812} 813 814bool SkPDFFont::FontRec::operator==(const SkPDFFont::FontRec& b) const { 815 if (fFontID != b.fFontID) 816 return false; 817 if (fFont != NULL && b.fFont != NULL) { 818 return fFont->fFirstGlyphID == b.fFont->fFirstGlyphID && 819 fFont->fLastGlyphID == b.fFont->fLastGlyphID; 820 } 821 if (fGlyphID == 0 || b.fGlyphID == 0) 822 return true; 823 824 if (fFont != NULL) { 825 return fFont->fFirstGlyphID <= b.fGlyphID && 826 b.fGlyphID <= fFont->fLastGlyphID; 827 } else if (b.fFont != NULL) { 828 return b.fFont->fFirstGlyphID <= fGlyphID && 829 fGlyphID <= b.fFont->fLastGlyphID; 830 } 831 return fGlyphID == b.fGlyphID; 832} 833 834SkPDFFont::FontRec::FontRec(SkPDFFont* font, uint32_t fontID, uint16_t glyphID) 835 : fFont(font), 836 fFontID(fontID), 837 fGlyphID(glyphID) { 838} 839