| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkData.h" | 8 #include "SkData.h" |
| 9 #include "SkGlyphCache.h" | 9 #include "SkGlyphCache.h" |
| 10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
| 11 #include "SkPDFCanon.h" | 11 #include "SkPDFCanon.h" |
| 12 #include "SkPDFConvertType1FontStream.h" | 12 #include "SkPDFConvertType1FontStream.h" |
| 13 #include "SkPDFDevice.h" | 13 #include "SkPDFDevice.h" |
| 14 #include "SkPDFMakeCIDGlyphWidthsArray.h" | 14 #include "SkPDFMakeCIDGlyphWidthsArray.h" |
| 15 #include "SkPDFMakeToUnicodeCmap.h" | 15 #include "SkPDFMakeToUnicodeCmap.h" |
| 16 #include "SkPDFFont.h" | 16 #include "SkPDFFont.h" |
| 17 #include "SkPDFUtils.h" | 17 #include "SkPDFUtils.h" |
| 18 #include "SkRefCnt.h" | 18 #include "SkRefCnt.h" |
| 19 #include "SkScalar.h" | 19 #include "SkScalar.h" |
| 20 #include "SkStream.h" | 20 #include "SkStream.h" |
| 21 #include "SkTypefacePriv.h" | 21 #include "SkTypefacePriv.h" |
| 22 #include "SkTypes.h" | 22 #include "SkTypes.h" |
| 23 #include "SkUtils.h" | 23 #include "SkUtils.h" |
| 24 | 24 |
| 25 #if defined (SK_SFNTLY_SUBSETTER) | 25 // TODO(halcanary): Switch Chrome from SK_SFNTLY_SUBSETTER to SK_PDF_USE_SFNTLY. |
| 26 #if defined (GOOGLE3) | 26 #if defined(SK_SFNTLY_SUBSETTER) && !defined(SK_PDF_USE_SFNTLY) |
| 27 // #including #defines doesn't work with this build system. | 27 #define SK_PDF_USE_SFNTLY |
| 28 #include "sample/chromium/font_subsetter.h" | 28 #endif |
| 29 #else | 29 |
| 30 #include SK_SFNTLY_SUBSETTER | 30 #ifdef SK_PDF_USE_SFNTLY |
| 31 #endif | 31 #include "sample/chromium/font_subsetter.h" |
| 32 #endif | 32 #endif |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 // PDF's notion of symbolic vs non-symbolic is related to the character set, not | 35 // PDF's notion of symbolic vs non-symbolic is related to the character set, not |
| 36 // symbols vs. characters. Rarely is a font the right character set to call it | 36 // symbols vs. characters. Rarely is a font the right character set to call it |
| 37 // non-symbolic, so always call it symbolic. (PDF 1.4 spec, section 5.7.1) | 37 // non-symbolic, so always call it symbolic. (PDF 1.4 spec, section 5.7.1) |
| 38 static const int kPdfSymbolic = 4; | 38 static const int kPdfSymbolic = 4; |
| 39 | 39 |
| 40 struct SkPDFType0Font final : public SkPDFFont { | 40 struct SkPDFType0Font final : public SkPDFFont { |
| 41 SkPDFType0Font(SkPDFFont::Info, const SkAdvancedTypefaceMetrics&); | 41 SkPDFType0Font(SkPDFFont::Info, const SkAdvancedTypefaceMetrics&); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 * page/pdf device. c) in the document, retrieve the per font glyph usage | 135 * page/pdf device. c) in the document, retrieve the per font glyph usage |
| 136 * from each page and combine it and ask for a resource with that subset. | 136 * from each page and combine it and ask for a resource with that subset. |
| 137 */ | 137 */ |
| 138 | 138 |
| 139 SkPDFFont::~SkPDFFont() {} | 139 SkPDFFont::~SkPDFFont() {} |
| 140 | 140 |
| 141 static bool can_embed(const SkAdvancedTypefaceMetrics& metrics) { | 141 static bool can_embed(const SkAdvancedTypefaceMetrics& metrics) { |
| 142 return !SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotEmbeddable_
FontFlag); | 142 return !SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotEmbeddable_
FontFlag); |
| 143 } | 143 } |
| 144 | 144 |
| 145 #ifdef SK_SFNTLY_SUBSETTER | 145 #ifdef SK_PDF_USE_SFNTLY |
| 146 static bool can_subset(const SkAdvancedTypefaceMetrics& metrics) { | 146 static bool can_subset(const SkAdvancedTypefaceMetrics& metrics) { |
| 147 return !SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotSubsettable
_FontFlag); | 147 return !SkToBool(metrics.fFlags & SkAdvancedTypefaceMetrics::kNotSubsettable
_FontFlag); |
| 148 } | 148 } |
| 149 #endif | 149 #endif |
| 150 | 150 |
| 151 int SkPDFFont::glyphsToPDFFontEncoding(SkGlyphID* glyphIDs, int numGlyphs) const
{ | 151 int SkPDFFont::glyphsToPDFFontEncoding(SkGlyphID* glyphIDs, int numGlyphs) const
{ |
| 152 // A font with multibyte glyphs will support all glyph IDs in a single font. | 152 // A font with multibyte glyphs will support all glyph IDs in a single font. |
| 153 if (this->multiByteGlyphs()) { | 153 if (this->multiByteGlyphs()) { |
| 154 return numGlyphs; | 154 return numGlyphs; |
| 155 } | 155 } |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 | 329 |
| 330 | 330 |
| 331 #ifdef SK_DEBUG | 331 #ifdef SK_DEBUG |
| 332 void SkPDFType0Font::emitObject(SkWStream* stream, | 332 void SkPDFType0Font::emitObject(SkWStream* stream, |
| 333 const SkPDFObjNumMap& objNumMap) const { | 333 const SkPDFObjNumMap& objNumMap) const { |
| 334 SkASSERT(fPopulated); | 334 SkASSERT(fPopulated); |
| 335 return INHERITED::emitObject(stream, objNumMap); | 335 return INHERITED::emitObject(stream, objNumMap); |
| 336 } | 336 } |
| 337 #endif | 337 #endif |
| 338 | 338 |
| 339 #ifdef SK_SFNTLY_SUBSETTER | 339 #ifdef SK_PDF_USE_SFNTLY |
| 340 // if possible, make no copy. | 340 // if possible, make no copy. |
| 341 static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) { | 341 static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) { |
| 342 SkASSERT(stream); | 342 SkASSERT(stream); |
| 343 (void)stream->rewind(); | 343 (void)stream->rewind(); |
| 344 SkASSERT(stream->hasLength()); | 344 SkASSERT(stream->hasLength()); |
| 345 size_t size = stream->getLength(); | 345 size_t size = stream->getLength(); |
| 346 if (const void* base = stream->getMemoryBase()) { | 346 if (const void* base = stream->getMemoryBase()) { |
| 347 SkData::ReleaseProc proc = | 347 SkData::ReleaseProc proc = |
| 348 [](const void*, void* ctx) { delete (SkStream*)ctx; }; | 348 [](const void*, void* ctx) { delete (SkStream*)ctx; }; |
| 349 return SkData::MakeWithProc(base, size, proc, stream.release()); | 349 return SkData::MakeWithProc(base, size, proc, stream.release()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 380 } | 380 } |
| 381 SkASSERT(subsetFont != nullptr); | 381 SkASSERT(subsetFont != nullptr); |
| 382 auto subsetStream = sk_make_sp<SkPDFStream>( | 382 auto subsetStream = sk_make_sp<SkPDFStream>( |
| 383 SkData::MakeWithProc( | 383 SkData::MakeWithProc( |
| 384 subsetFont, subsetFontSize, | 384 subsetFont, subsetFontSize, |
| 385 [](const void* p, void*) { delete[] (unsigned char*)p; }, | 385 [](const void* p, void*) { delete[] (unsigned char*)p; }, |
| 386 nullptr)); | 386 nullptr)); |
| 387 subsetStream->dict()->insertInt("Length1", subsetFontSize); | 387 subsetStream->dict()->insertInt("Length1", subsetFontSize); |
| 388 return subsetStream; | 388 return subsetStream; |
| 389 } | 389 } |
| 390 #endif // SK_SFNTLY_SUBSETTER | 390 #endif // SK_PDF_USE_SFNTLY |
| 391 | 391 |
| 392 void SkPDFType0Font::getFontSubset(SkPDFCanon* canon) { | 392 void SkPDFType0Font::getFontSubset(SkPDFCanon* canon) { |
| 393 const SkAdvancedTypefaceMetrics* metricsPtr = | 393 const SkAdvancedTypefaceMetrics* metricsPtr = |
| 394 SkPDFFont::GetMetrics(this->typeface(), canon); | 394 SkPDFFont::GetMetrics(this->typeface(), canon); |
| 395 SkASSERT(metricsPtr); | 395 SkASSERT(metricsPtr); |
| 396 if (!metricsPtr) { return; } | 396 if (!metricsPtr) { return; } |
| 397 const SkAdvancedTypefaceMetrics& metrics = *metricsPtr; | 397 const SkAdvancedTypefaceMetrics& metrics = *metricsPtr; |
| 398 SkASSERT(can_embed(metrics)); | 398 SkASSERT(can_embed(metrics)); |
| 399 SkAdvancedTypefaceMetrics::FontType type = this->getType(); | 399 SkAdvancedTypefaceMetrics::FontType type = this->getType(); |
| 400 SkTypeface* face = this->typeface(); | 400 SkTypeface* face = this->typeface(); |
| 401 SkASSERT(face); | 401 SkASSERT(face); |
| 402 const SkString& name = metrics.fFontName; | 402 const SkString& name = metrics.fFontName; |
| 403 | 403 |
| 404 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); | 404 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
| 405 add_common_font_descriptor_entries(descriptor.get(), metrics, 0); | 405 add_common_font_descriptor_entries(descriptor.get(), metrics, 0); |
| 406 switch (type) { | 406 switch (type) { |
| 407 case SkAdvancedTypefaceMetrics::kTrueType_Font: { | 407 case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
| 408 int ttcIndex; | 408 int ttcIndex; |
| 409 std::unique_ptr<SkStreamAsset> fontAsset(face->openStream(&ttcIndex)
); | 409 std::unique_ptr<SkStreamAsset> fontAsset(face->openStream(&ttcIndex)
); |
| 410 SkASSERT(fontAsset); | 410 SkASSERT(fontAsset); |
| 411 if (!fontAsset) { | 411 if (!fontAsset) { |
| 412 return; | 412 return; |
| 413 } | 413 } |
| 414 size_t fontSize = fontAsset->getLength(); | 414 size_t fontSize = fontAsset->getLength(); |
| 415 SkASSERT(fontSize > 0); | 415 SkASSERT(fontSize > 0); |
| 416 if (fontSize == 0) { | 416 if (fontSize == 0) { |
| 417 return; | 417 return; |
| 418 } | 418 } |
| 419 | 419 |
| 420 #ifdef SK_SFNTLY_SUBSETTER | 420 #ifdef SK_PDF_USE_SFNTLY |
| 421 if (can_subset(metrics)) { | 421 if (can_subset(metrics)) { |
| 422 // Generate glyph id array. in format needed by sfntly | 422 // Generate glyph id array. in format needed by sfntly |
| 423 SkTDArray<uint32_t> glyphIDs; | 423 SkTDArray<uint32_t> glyphIDs; |
| 424 if (!this->glyphUsage().has(0)) { | 424 if (!this->glyphUsage().has(0)) { |
| 425 glyphIDs.push(0); // Always include glyph 0. | 425 glyphIDs.push(0); // Always include glyph 0. |
| 426 } | 426 } |
| 427 this->glyphUsage().exportTo(&glyphIDs); | 427 this->glyphUsage().exportTo(&glyphIDs); |
| 428 sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( | 428 sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( |
| 429 std::move(fontAsset), glyphIDs, name.c_str()); | 429 std::move(fontAsset), glyphIDs, name.c_str()); |
| 430 if (subsetStream) { | 430 if (subsetStream) { |
| 431 descriptor->insertObjRef("FontFile2", std::move(subsetStream
)); | 431 descriptor->insertObjRef("FontFile2", std::move(subsetStream
)); |
| 432 break; | 432 break; |
| 433 } | 433 } |
| 434 // If subsetting fails, fall back to original font data. | 434 // If subsetting fails, fall back to original font data. |
| 435 fontAsset.reset(face->openStream(&ttcIndex)); | 435 fontAsset.reset(face->openStream(&ttcIndex)); |
| 436 } | 436 } |
| 437 #endif // SK_SFNTLY_SUBSETTER | 437 #endif // SK_PDF_USE_SFNTLY |
| 438 auto fontStream = sk_make_sp<SkPDFSharedStream>(std::move(fontAsset)
); | 438 auto fontStream = sk_make_sp<SkPDFSharedStream>(std::move(fontAsset)
); |
| 439 fontStream->dict()->insertInt("Length1", fontSize); | 439 fontStream->dict()->insertInt("Length1", fontSize); |
| 440 descriptor->insertObjRef("FontFile2", std::move(fontStream)); | 440 descriptor->insertObjRef("FontFile2", std::move(fontStream)); |
| 441 break; | 441 break; |
| 442 } | 442 } |
| 443 case SkAdvancedTypefaceMetrics::kType1CID_Font: { | 443 case SkAdvancedTypefaceMetrics::kType1CID_Font: { |
| 444 std::unique_ptr<SkStreamAsset> fontData(face->openStream(nullptr)); | 444 std::unique_ptr<SkStreamAsset> fontData(face->openStream(nullptr)); |
| 445 SkASSERT(fontData); | 445 SkASSERT(fontData); |
| 446 SkASSERT(fontData->getLength() > 0); | 446 SkASSERT(fontData->getLength() > 0); |
| 447 if (!fontData || 0 == fontData->getLength()) { | 447 if (!fontData || 0 == fontData->getLength()) { |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 | 755 |
| 756 bool SkPDFFont::CanEmbedTypeface(SkTypeface* typeface, SkPDFCanon* canon) { | 756 bool SkPDFFont::CanEmbedTypeface(SkTypeface* typeface, SkPDFCanon* canon) { |
| 757 const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, c
anon); | 757 const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, c
anon); |
| 758 return metrics && can_embed(*metrics); | 758 return metrics && can_embed(*metrics); |
| 759 } | 759 } |
| 760 | 760 |
| 761 void SkPDFFont::drop() { | 761 void SkPDFFont::drop() { |
| 762 fTypeface = nullptr; | 762 fTypeface = nullptr; |
| 763 this->SkPDFDict::drop(); | 763 this->SkPDFDict::drop(); |
| 764 } | 764 } |
| OLD | NEW |