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" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 SkTDArray<int16_t> fAdvance; | 49 SkTDArray<int16_t> fAdvance; |
50 AdvanceMetric(uint16_t startId) : fStartId(startId) {} | 50 AdvanceMetric(uint16_t startId) : fStartId(startId) {} |
51 AdvanceMetric(AdvanceMetric&&) = default; | 51 AdvanceMetric(AdvanceMetric&&) = default; |
52 AdvanceMetric& operator=(AdvanceMetric&& other) = default; | 52 AdvanceMetric& operator=(AdvanceMetric&& other) = default; |
53 AdvanceMetric(const AdvanceMetric&) = delete; | 53 AdvanceMetric(const AdvanceMetric&) = delete; |
54 AdvanceMetric& operator=(const AdvanceMetric&) = delete; | 54 AdvanceMetric& operator=(const AdvanceMetric&) = delete; |
55 }; | 55 }; |
56 | 56 |
57 class SkPDFType0Font final : public SkPDFFont { | 57 class SkPDFType0Font final : public SkPDFFont { |
58 public: | 58 public: |
59 SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, | 59 SkPDFType0Font(sk_sp<const SkAdvancedTypefaceMetrics> info, |
60 SkTypeface* typeface); | 60 sk_sp<SkTypeface> typeface, |
| 61 SkAdvancedTypefaceMetrics::FontType type); |
61 virtual ~SkPDFType0Font(); | 62 virtual ~SkPDFType0Font(); |
62 bool multiByteGlyphs() const override { return true; } | |
63 sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; | 63 sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; |
64 #ifdef SK_DEBUG | 64 #ifdef SK_DEBUG |
65 void emitObject(SkWStream*, | 65 void emitObject(SkWStream*, |
66 const SkPDFObjNumMap&, | 66 const SkPDFObjNumMap&, |
67 const SkPDFSubstituteMap&) const override; | 67 const SkPDFSubstituteMap&) const override; |
68 #endif | 68 #endif |
69 | 69 |
70 private: | 70 private: |
71 #ifdef SK_DEBUG | 71 #ifdef SK_DEBUG |
72 bool fPopulated; | 72 bool fPopulated; |
73 #endif | 73 #endif |
74 bool populate(const SkPDFGlyphSet* subset); | 74 bool populate(const SkPDFGlyphSet* subset); |
75 typedef SkPDFDict INHERITED; | 75 typedef SkPDFDict INHERITED; |
76 }; | 76 }; |
77 | 77 |
78 class SkPDFCIDFont final : public SkPDFFont { | 78 class SkPDFCIDFont final : public SkPDFFont { |
79 public: | 79 public: |
80 SkPDFCIDFont(const SkAdvancedTypefaceMetrics* info, | 80 SkPDFCIDFont(sk_sp<const SkAdvancedTypefaceMetrics> info, |
81 SkTypeface* typeface, | 81 sk_sp<SkTypeface> typeface, |
| 82 SkAdvancedTypefaceMetrics::FontType fontType, |
82 const SkPDFGlyphSet* subset); | 83 const SkPDFGlyphSet* subset); |
83 virtual ~SkPDFCIDFont(); | 84 virtual ~SkPDFCIDFont(); |
84 bool multiByteGlyphs() const override { return true; } | |
85 | 85 |
86 private: | 86 private: |
87 bool populate(const SkPDFGlyphSet* subset); | 87 bool populate(const SkPDFGlyphSet* subset); |
88 bool addFontDescriptor(int16_t defaultWidth, | 88 bool addFontDescriptor(int16_t defaultWidth, |
89 const SkTDArray<uint32_t>* subset); | 89 const SkTDArray<uint32_t>* subset); |
90 }; | 90 }; |
91 | 91 |
92 class SkPDFType1Font final : public SkPDFFont { | 92 class SkPDFType1Font final : public SkPDFFont { |
93 public: | 93 public: |
94 SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, | 94 SkPDFType1Font(sk_sp<const SkAdvancedTypefaceMetrics> info, |
95 SkTypeface* typeface, | 95 sk_sp<SkTypeface> typeface, |
96 uint16_t glyphID, | 96 uint16_t glyphID, |
97 SkPDFDict* relatedFontDescriptor); | 97 sk_sp<SkPDFDict> relatedFontDescriptor); |
98 virtual ~SkPDFType1Font(); | 98 virtual ~SkPDFType1Font(); |
99 bool multiByteGlyphs() const override { return false; } | |
100 | 99 |
101 private: | 100 private: |
102 bool populate(int16_t glyphID); | 101 bool populate(int16_t glyphID); |
103 bool addFontDescriptor(int16_t defaultWidth); | 102 bool addFontDescriptor(int16_t defaultWidth); |
104 }; | 103 }; |
105 | 104 |
106 class SkPDFType3Font final : public SkPDFFont { | 105 class SkPDFType3Font final : public SkPDFFont { |
107 public: | 106 public: |
108 SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, | 107 SkPDFType3Font(sk_sp<const SkAdvancedTypefaceMetrics> info, |
109 SkTypeface* typeface, | 108 sk_sp<SkTypeface> typeface, |
| 109 SkAdvancedTypefaceMetrics::FontType fontType, |
110 uint16_t glyphID); | 110 uint16_t glyphID); |
111 virtual ~SkPDFType3Font() {} | 111 virtual ~SkPDFType3Font() {} |
112 void emitObject(SkWStream*, | 112 void emitObject(SkWStream*, |
113 const SkPDFObjNumMap&, | 113 const SkPDFObjNumMap&, |
114 const SkPDFSubstituteMap&) const override { | 114 const SkPDFSubstituteMap&) const override { |
115 SkDEBUGFAIL("should call getFontSubset!"); | 115 SkDEBUGFAIL("should call getFontSubset!"); |
116 } | 116 } |
117 sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; | 117 sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; |
118 bool multiByteGlyphs() const override { return false; } | |
119 }; | 118 }; |
120 | 119 |
121 /////////////////////////////////////////////////////////////////////////////// | 120 /////////////////////////////////////////////////////////////////////////////// |
122 // File-Local Functions | 121 // File-Local Functions |
123 /////////////////////////////////////////////////////////////////////////////// | 122 /////////////////////////////////////////////////////////////////////////////// |
124 | 123 |
125 const int16_t kInvalidAdvance = SK_MinS16; | 124 const int16_t kInvalidAdvance = SK_MinS16; |
126 const int16_t kDontCareAdvance = SK_MinS16 + 1; | 125 const int16_t kDontCareAdvance = SK_MinS16 + 1; |
127 | 126 |
128 static void stripUninterestingTrailingAdvancesFromRange( | 127 static void stripUninterestingTrailingAdvancesFromRange( |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 * some additional state indicating which subset of the font is used. It | 460 * some additional state indicating which subset of the font is used. It |
462 * must be maintained at the page granularity and then combined at the document | 461 * must be maintained at the page granularity and then combined at the document |
463 * granularity. a) change SkPDFFont to fill in its state on demand, kind of | 462 * granularity. a) change SkPDFFont to fill in its state on demand, kind of |
464 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each | 463 * like SkPDFGraphicState. b) maintain a per font glyph usage class in each |
465 * page/pdf device. c) in the document, retrieve the per font glyph usage | 464 * page/pdf device. c) in the document, retrieve the per font glyph usage |
466 * from each page and combine it and ask for a resource with that subset. | 465 * from each page and combine it and ask for a resource with that subset. |
467 */ | 466 */ |
468 | 467 |
469 SkPDFFont::~SkPDFFont() {} | 468 SkPDFFont::~SkPDFFont() {} |
470 | 469 |
471 SkTypeface* SkPDFFont::typeface() { | |
472 return fTypeface.get(); | |
473 } | |
474 | |
475 SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() { | |
476 return fFontType; | |
477 } | |
478 | |
479 bool SkPDFFont::canEmbed() const { | 470 bool SkPDFFont::canEmbed() const { |
480 if (!fFontInfo.get()) { | 471 if (!fFontInfo.get()) { |
481 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); | 472 SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); |
482 return true; | 473 return true; |
483 } | 474 } |
484 return (fFontInfo->fFlags & | 475 return (fFontInfo->fFlags & |
485 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag) == 0; | 476 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag) == 0; |
486 } | 477 } |
487 | 478 |
488 bool SkPDFFont::canSubset() const { | 479 bool SkPDFFont::canSubset() const { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 if (glyphIDs[i] != 0 && | 517 if (glyphIDs[i] != 0 && |
527 (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID)) { | 518 (glyphIDs[i] < fFirstGlyphID || glyphIDs[i] > fLastGlyphID)) { |
528 return i; | 519 return i; |
529 } | 520 } |
530 } | 521 } |
531 return numGlyphs; | 522 return numGlyphs; |
532 } | 523 } |
533 | 524 |
534 // static | 525 // static |
535 SkPDFFont* SkPDFFont::GetFontResource(SkPDFCanon* canon, | 526 SkPDFFont* SkPDFFont::GetFontResource(SkPDFCanon* canon, |
536 SkTypeface* typeface, | 527 SkTypeface* face, |
537 uint16_t glyphID) { | 528 uint16_t glyphID) { |
538 SkASSERT(canon); | 529 SkASSERT(canon); |
539 const uint32_t fontID = SkTypeface::UniqueID(typeface); | 530 const uint32_t fontID = SkTypeface::UniqueID(face); |
540 SkPDFFont* relatedFont; | 531 SkPDFFont* relatedFont; |
541 if (SkPDFFont* pdfFont = canon->findFont(fontID, glyphID, &relatedFont)) { | 532 if (SkPDFFont* pdfFont = canon->findFont(fontID, glyphID, &relatedFont)) { |
542 return SkRef(pdfFont); | 533 return SkRef(pdfFont); |
543 } | 534 } |
544 SkAutoResolveDefaultTypeface autoResolve(typeface); | 535 sk_sp<SkTypeface> typeface(face ? sk_ref_sp(face) : SkTypeface::MakeDefault(
)); |
545 typeface = autoResolve.get(); | |
546 SkASSERT(typeface); | 536 SkASSERT(typeface); |
547 int glyphCount = typeface->countGlyphs(); | 537 int glyphCount = typeface->countGlyphs(); |
548 if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. | 538 if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. |
549 glyphCount > 1 + SK_MaxU16 || // invalid glyphCount | 539 glyphCount > 1 + SK_MaxU16 || // invalid glyphCount |
550 glyphID >= glyphCount) { // invalid glyph | 540 glyphID >= glyphCount) { // invalid glyph |
551 return nullptr; | 541 return nullptr; |
552 } | 542 } |
553 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics; | 543 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics; |
554 SkPDFDict* relatedFontDescriptor = nullptr; | 544 sk_sp<SkPDFDict> relatedFontDescriptor; |
555 if (relatedFont) { | 545 if (relatedFont) { |
556 fontMetrics.reset(SkSafeRef(relatedFont->fontInfo())); | 546 fontMetrics = relatedFont->refFontInfo(); |
557 relatedFontDescriptor = relatedFont->getFontDescriptor(); | 547 relatedFontDescriptor = relatedFont->refFontDescriptor(); |
558 | 548 |
559 // This only is to catch callers who pass invalid glyph ids. | 549 // This only is to catch callers who pass invalid glyph ids. |
560 // If glyph id is invalid, then we will create duplicate entries | 550 // If glyph id is invalid, then we will create duplicate entries |
561 // for TrueType fonts. | 551 // for TrueType fonts. |
562 SkAdvancedTypefaceMetrics::FontType fontType = | 552 SkDEBUGCODE(SkAdvancedTypefaceMetrics::FontType fontType = relatedFont->
getType()); |
563 fontMetrics.get() ? fontMetrics.get()->fType : | 553 SkASSERT(fontType != SkAdvancedTypefaceMetrics::kType1CID_Font); |
564 SkAdvancedTypefaceMetrics::kOther_Font; | 554 SkASSERT(fontType != SkAdvancedTypefaceMetrics::kTrueType_Font); |
565 | |
566 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font || | |
567 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) { | |
568 return SkRef(relatedFont); | |
569 } | |
570 } else { | 555 } else { |
571 SkTypeface::PerGlyphInfo info = | 556 SkTypeface::PerGlyphInfo info = (SkTypeface::PerGlyphInfo)( |
572 SkTBitOr(SkTypeface::kGlyphNames_PerGlyphInfo, | 557 SkTypeface::kGlyphNames_PerGlyphInfo | |
573 SkTypeface::kToUnicode_PerGlyphInfo); | 558 SkTypeface::kToUnicode_PerGlyphInfo); |
574 fontMetrics.reset( | 559 fontMetrics.reset( |
575 typeface->getAdvancedTypefaceMetrics(info, nullptr, 0)); | 560 typeface->getAdvancedTypefaceMetrics(info, nullptr, 0)); |
576 } | 561 } |
577 | 562 |
578 SkPDFFont* font = SkPDFFont::Create(canon, fontMetrics.get(), typeface, | 563 SkAdvancedTypefaceMetrics::FontType type = |
579 glyphID, relatedFontDescriptor); | 564 fontMetrics ? fontMetrics->fType : SkAdvancedTypefaceMetrics::kOther_Fon
t; |
580 canon->addFont(font, fontID, font->fFirstGlyphID); | 565 if (fontMetrics && |
581 return font; | 566 SkToBool(fontMetrics->fFlags & |
| 567 SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { |
| 568 // force Type3 fallback. |
| 569 type = SkAdvancedTypefaceMetrics::kOther_Font; |
| 570 } |
| 571 |
| 572 sk_sp<SkPDFFont> font; |
| 573 switch (type) { |
| 574 case SkAdvancedTypefaceMetrics::kType1CID_Font: |
| 575 case SkAdvancedTypefaceMetrics::kTrueType_Font: |
| 576 SkASSERT(relatedFontDescriptor == nullptr); |
| 577 SkASSERT(fontMetrics != nullptr); |
| 578 font = sk_make_sp<SkPDFType0Font>(std::move(fontMetrics), |
| 579 std::move(typeface), |
| 580 type); |
| 581 break; |
| 582 case SkAdvancedTypefaceMetrics::kType1_Font: |
| 583 SkASSERT(fontMetrics != nullptr); |
| 584 font = sk_make_sp<SkPDFType1Font>(std::move(fontMetrics), |
| 585 std::move(typeface), |
| 586 glyphID, |
| 587 std::move(relatedFontDescriptor)); |
| 588 break; |
| 589 case SkAdvancedTypefaceMetrics::kCFF_Font: |
| 590 SkASSERT(fontMetrics != nullptr); |
| 591 // fallthrough |
| 592 case SkAdvancedTypefaceMetrics::kOther_Font: |
| 593 font = sk_make_sp<SkPDFType3Font>(std::move(fontMetrics), |
| 594 std::move(typeface), |
| 595 type, |
| 596 glyphID); |
| 597 break; |
| 598 default: |
| 599 SkDEBUGFAIL("invalid SkAdvancedTypefaceMetrics::FontType"); |
| 600 return nullptr; |
| 601 } |
| 602 // When firstGlyphID==0, SkFont::IsMatch() matches all glyphs in font. |
| 603 SkGlyphID firstGlyphID = font->multiByteGlyphs() ? 0 : font->fFirstGlyphID; |
| 604 // TODO(halcanary) Make SkCanon::addFont take sk_sp<SkPDFFont>. |
| 605 canon->addFont(font.get(), fontID, firstGlyphID); |
| 606 return font.release(); // TODO(halcanary) return sk_sp<SkPDFFont>. |
582 } | 607 } |
583 | 608 |
584 sk_sp<SkPDFObject> SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { | 609 sk_sp<SkPDFObject> SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { |
585 return nullptr; // Default: no support. | 610 return nullptr; // Default: no support. |
586 } | 611 } |
587 | 612 |
588 // TODO: take a sk_sp<SkAdvancedTypefaceMetrics> and sk_sp<SkTypeface> | 613 // TODO: take a sk_sp<SkAdvancedTypefaceMetrics> and sk_sp<SkTypeface> |
589 SkPDFFont::SkPDFFont(const SkAdvancedTypefaceMetrics* info, | 614 SkPDFFont::SkPDFFont(sk_sp<const SkAdvancedTypefaceMetrics> info, |
590 SkTypeface* typeface, | 615 sk_sp<SkTypeface> typeface, |
591 SkPDFDict* relatedFontDescriptor) | 616 sk_sp<SkPDFDict> relatedFontDescriptor, |
| 617 SkAdvancedTypefaceMetrics::FontType fontType, |
| 618 bool multiByteGlyphs) |
592 : SkPDFDict("Font") | 619 : SkPDFDict("Font") |
593 , fTypeface(ref_or_default(typeface)) | 620 , fTypeface(std::move(typeface)) |
| 621 , fFontInfo(std::move(info)) |
| 622 , fDescriptor(std::move(relatedFontDescriptor)) |
594 , fFirstGlyphID(1) | 623 , fFirstGlyphID(1) |
595 , fLastGlyphID(info ? info->fLastGlyphID : 0) | 624 , fFontType(fontType) |
596 , fFontInfo(SkSafeRef(info)) | 625 , fMultiByteGlyphs(multiByteGlyphs) { |
597 , fDescriptor(SkSafeRef(relatedFontDescriptor)) | |
598 , fFontType((!info || info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster
_FontFlag) | |
599 ? SkAdvancedTypefaceMetrics::kOther_Font | |
600 : info->fType) { | |
601 SkASSERT(fTypeface); | 626 SkASSERT(fTypeface); |
| 627 fLastGlyphID = fFontInfo ? fFontInfo->fLastGlyphID : 0; |
602 if (0 == fLastGlyphID) { | 628 if (0 == fLastGlyphID) { |
603 fLastGlyphID = SkToU16(fTypeface->countGlyphs() - 1); | 629 fLastGlyphID = SkToU16(fTypeface->countGlyphs() - 1); |
604 } | 630 } |
605 } | 631 } |
606 | 632 |
607 // static | 633 void SkPDFFont::setFontInfo(sk_sp<const SkAdvancedTypefaceMetrics> info) { |
608 SkPDFFont* SkPDFFont::Create(SkPDFCanon* canon, | 634 if (info) { |
609 const SkAdvancedTypefaceMetrics* info, | 635 fFontInfo = std::move(info); |
610 SkTypeface* typeface, | |
611 uint16_t glyphID, | |
612 SkPDFDict* relatedFontDescriptor) { | |
613 SkAdvancedTypefaceMetrics::FontType type = | |
614 info ? info->fType : SkAdvancedTypefaceMetrics::kOther_Font; | |
615 SkAdvancedTypefaceMetrics::FontFlags flags = | |
616 info ? info->fFlags : SkAdvancedTypefaceMetrics::kEmpty_FontFlag; | |
617 if (SkToBool(flags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { | |
618 return new SkPDFType3Font(info, typeface, glyphID); | |
619 } | 636 } |
620 switch (type) { | |
621 case SkAdvancedTypefaceMetrics::kType1CID_Font: | |
622 case SkAdvancedTypefaceMetrics::kTrueType_Font: | |
623 SkASSERT(relatedFontDescriptor == nullptr); | |
624 SkASSERT(info != nullptr); | |
625 return new SkPDFType0Font(info, typeface); | |
626 case SkAdvancedTypefaceMetrics::kType1_Font: | |
627 SkASSERT(info != nullptr); | |
628 return new SkPDFType1Font(info, typeface, glyphID, relatedFontDescri
ptor); | |
629 case SkAdvancedTypefaceMetrics::kCFF_Font: | |
630 SkASSERT(info != nullptr); | |
631 // fallthrough | |
632 case SkAdvancedTypefaceMetrics::kOther_Font: | |
633 return new SkPDFType3Font(info, typeface, glyphID); | |
634 } | |
635 SkDEBUGFAIL("invalid SkAdvancedTypefaceMetrics::FontType"); | |
636 return nullptr; | |
637 } | |
638 | |
639 const SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { | |
640 return fFontInfo.get(); | |
641 } | |
642 | |
643 void SkPDFFont::setFontInfo(const SkAdvancedTypefaceMetrics* info) { | |
644 if (info == nullptr || info == fFontInfo.get()) { | |
645 return; | |
646 } | |
647 fFontInfo.reset(info); | |
648 SkSafeRef(info); | |
649 } | |
650 | |
651 uint16_t SkPDFFont::firstGlyphID() const { | |
652 return fFirstGlyphID; | |
653 } | |
654 | |
655 uint16_t SkPDFFont::lastGlyphID() const { | |
656 return fLastGlyphID; | |
657 } | 637 } |
658 | 638 |
659 void SkPDFFont::setLastGlyphID(uint16_t glyphID) { | 639 void SkPDFFont::setLastGlyphID(uint16_t glyphID) { |
660 fLastGlyphID = glyphID; | 640 fLastGlyphID = glyphID; |
661 } | 641 } |
662 | 642 |
663 SkPDFDict* SkPDFFont::getFontDescriptor() { | 643 void SkPDFFont::setFontDescriptor(sk_sp<SkPDFDict> descriptor) { |
664 return fDescriptor.get(); | 644 fDescriptor = std::move(descriptor); |
665 } | |
666 | |
667 void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) { | |
668 fDescriptor.reset(descriptor); | |
669 SkSafeRef(descriptor); | |
670 } | 645 } |
671 | 646 |
672 bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { | 647 bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { |
673 if (fDescriptor.get() == nullptr) { | 648 if (fDescriptor.get() == nullptr) { |
674 return false; | 649 return false; |
675 } | 650 } |
676 | 651 |
677 const uint16_t emSize = fFontInfo->fEmSize; | 652 const uint16_t emSize = fFontInfo->fEmSize; |
678 | 653 |
679 fDescriptor->insertName("FontName", fFontInfo->fFontName); | 654 fDescriptor->insertName("FontName", fFontInfo->fFontName); |
680 fDescriptor->insertInt("Flags", fFontInfo->fStyle | kPdfSymbolic); | 655 fDescriptor->insertInt("Flags", (size_t)(fFontInfo->fStyle | kPdfSymbolic)); |
681 fDescriptor->insertScalar("Ascent", | 656 fDescriptor->insertScalar("Ascent", |
682 scaleFromFontUnits(fFontInfo->fAscent, emSize)); | 657 scaleFromFontUnits(fFontInfo->fAscent, emSize)); |
683 fDescriptor->insertScalar("Descent", | 658 fDescriptor->insertScalar("Descent", |
684 scaleFromFontUnits(fFontInfo->fDescent, emSize)); | 659 scaleFromFontUnits(fFontInfo->fDescent, emSize)); |
685 fDescriptor->insertScalar("StemV", | 660 fDescriptor->insertScalar("StemV", |
686 scaleFromFontUnits(fFontInfo->fStemV, emSize)); | 661 scaleFromFontUnits(fFontInfo->fStemV, emSize)); |
687 | 662 |
688 fDescriptor->insertScalar("CapHeight", | 663 fDescriptor->insertScalar("CapHeight", |
689 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)); | 664 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)); |
690 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle); | 665 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle); |
(...skipping 24 matching lines...) Expand all Loading... |
715 subset, | 690 subset, |
716 multiByteGlyphs(), | 691 multiByteGlyphs(), |
717 firstGlyphID(), | 692 firstGlyphID(), |
718 lastGlyphID())); | 693 lastGlyphID())); |
719 } | 694 } |
720 | 695 |
721 /////////////////////////////////////////////////////////////////////////////// | 696 /////////////////////////////////////////////////////////////////////////////// |
722 // class SkPDFType0Font | 697 // class SkPDFType0Font |
723 /////////////////////////////////////////////////////////////////////////////// | 698 /////////////////////////////////////////////////////////////////////////////// |
724 | 699 |
725 SkPDFType0Font::SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, SkTypeface
* typeface) | 700 SkPDFType0Font::SkPDFType0Font(sk_sp<const SkAdvancedTypefaceMetrics> info, |
726 : SkPDFFont(info, typeface, nullptr) { | 701 sk_sp<SkTypeface> typeface, |
| 702 SkAdvancedTypefaceMetrics::FontType fontType) |
| 703 : SkPDFFont(std::move(info), std::move(typeface), nullptr, fontType, true) { |
727 SkDEBUGCODE(fPopulated = false); | 704 SkDEBUGCODE(fPopulated = false); |
728 if (!canSubset()) { | 705 if (!canSubset()) { |
729 this->populate(nullptr); | 706 this->populate(nullptr); |
730 } | 707 } |
731 } | 708 } |
732 | 709 |
733 SkPDFType0Font::~SkPDFType0Font() {} | 710 SkPDFType0Font::~SkPDFType0Font() {} |
734 | 711 |
735 sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { | 712 sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { |
736 if (!canSubset()) { | 713 if (!canSubset()) { |
737 return nullptr; | 714 return nullptr; |
738 } | 715 } |
739 auto newSubset = sk_make_sp<SkPDFType0Font>(fontInfo(), typeface()); | 716 auto newSubset = sk_make_sp<SkPDFType0Font>(refFontInfo(), refTypeface(), ge
tType()); |
740 newSubset->populate(subset); | 717 newSubset->populate(subset); |
741 return newSubset; | 718 return newSubset; |
742 } | 719 } |
743 | 720 |
744 #ifdef SK_DEBUG | 721 #ifdef SK_DEBUG |
745 void SkPDFType0Font::emitObject(SkWStream* stream, | 722 void SkPDFType0Font::emitObject(SkWStream* stream, |
746 const SkPDFObjNumMap& objNumMap, | 723 const SkPDFObjNumMap& objNumMap, |
747 const SkPDFSubstituteMap& substitutes) const { | 724 const SkPDFSubstituteMap& substitutes) const { |
748 SkASSERT(fPopulated); | 725 SkASSERT(fPopulated); |
749 return INHERITED::emitObject(stream, objNumMap, substitutes); | 726 return INHERITED::emitObject(stream, objNumMap, substitutes); |
750 } | 727 } |
751 #endif | 728 #endif |
752 | 729 |
753 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { | 730 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { |
754 insertName("Subtype", "Type0"); | 731 insertName("Subtype", "Type0"); |
755 insertName("BaseFont", fontInfo()->fFontName); | 732 insertName("BaseFont", this->getFontInfo()->fFontName); |
756 insertName("Encoding", "Identity-H"); | 733 insertName("Encoding", "Identity-H"); |
757 | 734 |
758 sk_sp<SkPDFCIDFont> newCIDFont( | 735 sk_sp<SkPDFCIDFont> newCIDFont( |
759 new SkPDFCIDFont(fontInfo(), typeface(), subset)); | 736 sk_make_sp<SkPDFCIDFont>(this->refFontInfo(), |
| 737 this->refTypeface(), |
| 738 this->getType(), |
| 739 subset)); |
760 auto descendantFonts = sk_make_sp<SkPDFArray>(); | 740 auto descendantFonts = sk_make_sp<SkPDFArray>(); |
761 descendantFonts->appendObjRef(std::move(newCIDFont)); | 741 descendantFonts->appendObjRef(std::move(newCIDFont)); |
762 this->insertObject("DescendantFonts", std::move(descendantFonts)); | 742 this->insertObject("DescendantFonts", std::move(descendantFonts)); |
763 | 743 |
764 this->populateToUnicodeTable(subset); | 744 this->populateToUnicodeTable(subset); |
765 | 745 |
766 SkDEBUGCODE(fPopulated = true); | 746 SkDEBUGCODE(fPopulated = true); |
767 return true; | 747 return true; |
768 } | 748 } |
769 | 749 |
770 /////////////////////////////////////////////////////////////////////////////// | 750 /////////////////////////////////////////////////////////////////////////////// |
771 // class SkPDFCIDFont | 751 // class SkPDFCIDFont |
772 /////////////////////////////////////////////////////////////////////////////// | 752 /////////////////////////////////////////////////////////////////////////////// |
773 | 753 |
774 SkPDFCIDFont::SkPDFCIDFont(const SkAdvancedTypefaceMetrics* info, | 754 SkPDFCIDFont::SkPDFCIDFont(sk_sp<const SkAdvancedTypefaceMetrics> info, |
775 SkTypeface* typeface, | 755 sk_sp<SkTypeface> typeface, |
| 756 SkAdvancedTypefaceMetrics::FontType fontType, |
776 const SkPDFGlyphSet* subset) | 757 const SkPDFGlyphSet* subset) |
777 : SkPDFFont(info, typeface, nullptr) { | 758 : SkPDFFont(std::move(info), std::move(typeface), nullptr, |
| 759 fontType , /* multiByteGlyphs = */ true) { |
778 this->populate(subset); | 760 this->populate(subset); |
779 } | 761 } |
780 | 762 |
781 SkPDFCIDFont::~SkPDFCIDFont() {} | 763 SkPDFCIDFont::~SkPDFCIDFont() {} |
782 | 764 |
783 #ifdef SK_SFNTLY_SUBSETTER | 765 #ifdef SK_SFNTLY_SUBSETTER |
784 // if possible, make no copy. | 766 // if possible, make no copy. |
785 static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) { | 767 static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) { |
786 SkASSERT(stream); | 768 SkASSERT(stream); |
787 (void)stream->rewind(); | 769 (void)stream->rewind(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 [](const void* p, void*) { delete[] (unsigned char*)p; }, | 811 [](const void* p, void*) { delete[] (unsigned char*)p; }, |
830 nullptr)); | 812 nullptr)); |
831 subsetStream->dict()->insertInt("Length1", subsetFontSize); | 813 subsetStream->dict()->insertInt("Length1", subsetFontSize); |
832 return subsetStream; | 814 return subsetStream; |
833 } | 815 } |
834 #endif // SK_SFNTLY_SUBSETTER | 816 #endif // SK_SFNTLY_SUBSETTER |
835 | 817 |
836 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, | 818 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, |
837 const SkTDArray<uint32_t>* subset) { | 819 const SkTDArray<uint32_t>* subset) { |
838 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); | 820 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
839 setFontDescriptor(descriptor.get()); | 821 setFontDescriptor(descriptor); |
840 if (!addCommonFontDescriptorEntries(defaultWidth)) { | 822 if (!addCommonFontDescriptorEntries(defaultWidth)) { |
841 this->insertObjRef("FontDescriptor", std::move(descriptor)); | 823 this->insertObjRef("FontDescriptor", std::move(descriptor)); |
842 return false; | 824 return false; |
843 } | 825 } |
844 SkASSERT(this->canEmbed()); | 826 SkASSERT(this->canEmbed()); |
845 | 827 |
846 switch (getType()) { | 828 switch (getType()) { |
847 case SkAdvancedTypefaceMetrics::kTrueType_Font: { | 829 case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
848 int ttcIndex; | 830 int ttcIndex; |
849 std::unique_ptr<SkStreamAsset> fontAsset( | 831 std::unique_ptr<SkStreamAsset> fontAsset( |
850 this->typeface()->openStream(&ttcIndex)); | 832 this->typeface()->openStream(&ttcIndex)); |
851 SkASSERT(fontAsset); | 833 SkASSERT(fontAsset); |
852 if (!fontAsset) { | 834 if (!fontAsset) { |
853 return false; | 835 return false; |
854 } | 836 } |
855 size_t fontSize = fontAsset->getLength(); | 837 size_t fontSize = fontAsset->getLength(); |
856 SkASSERT(fontSize > 0); | 838 SkASSERT(fontSize > 0); |
857 if (fontSize == 0) { | 839 if (fontSize == 0) { |
858 return false; | 840 return false; |
859 } | 841 } |
860 | 842 |
861 #ifdef SK_SFNTLY_SUBSETTER | 843 #ifdef SK_SFNTLY_SUBSETTER |
862 if (this->canSubset() && subset) { | 844 if (this->canSubset() && subset) { |
| 845 const char* name = this->getFontInfo()->fFontName.c_str(); |
863 sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( | 846 sk_sp<SkPDFObject> subsetStream = get_subset_font_stream( |
864 std::move(fontAsset), *subset, fontInfo()->fFontName.c_s
tr()); | 847 std::move(fontAsset), *subset, name); |
865 if (subsetStream) { | 848 if (subsetStream) { |
866 descriptor->insertObjRef("FontFile2", std::move(subsetStream
)); | 849 descriptor->insertObjRef("FontFile2", std::move(subsetStream
)); |
867 break; | 850 break; |
868 } | 851 } |
869 // If subsetting fails, fall back to original font data. | 852 // If subsetting fails, fall back to original font data. |
870 fontAsset.reset(this->typeface()->openStream(&ttcIndex)); | 853 fontAsset.reset(this->typeface()->openStream(&ttcIndex)); |
871 } | 854 } |
872 #endif // SK_SFNTLY_SUBSETTER | 855 #endif // SK_SFNTLY_SUBSETTER |
873 auto fontStream = sk_make_sp<SkPDFSharedStream>(std::move(fontAsset)
); | 856 auto fontStream = sk_make_sp<SkPDFSharedStream>(std::move(fontAsset)
); |
874 fontStream->dict()->insertInt("Length1", fontSize); | 857 fontStream->dict()->insertInt("Length1", fontSize); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
934 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { | 917 bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { |
935 // Generate new font metrics with advance info for true type fonts. | 918 // Generate new font metrics with advance info for true type fonts. |
936 // Generate glyph id array. | 919 // Generate glyph id array. |
937 SkTDArray<uint32_t> glyphIDs; | 920 SkTDArray<uint32_t> glyphIDs; |
938 if (subset) { | 921 if (subset) { |
939 if (!subset->has(0)) { | 922 if (!subset->has(0)) { |
940 glyphIDs.push(0); // Always include glyph 0. | 923 glyphIDs.push(0); // Always include glyph 0. |
941 } | 924 } |
942 subset->exportTo(&glyphIDs); | 925 subset->exportTo(&glyphIDs); |
943 } | 926 } |
944 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { | 927 if (this->getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
945 uint32_t* glyphs = (glyphIDs.count() == 0) ? nullptr : glyphIDs.begin(); | 928 uint32_t* glyphs = (glyphIDs.count() == 0) ? nullptr : glyphIDs.begin(); |
946 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; | 929 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; |
947 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics = | 930 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics = |
948 SkPDFFont::GetFontMetricsWithGlyphNames(this->typeface(), glyphs, gl
yphsCount); | 931 SkPDFFont::GetFontMetricsWithGlyphNames(this->typeface(), glyphs, gl
yphsCount); |
949 this->setFontInfo(fontMetrics.get()); | 932 this->setFontInfo(std::move(fontMetrics)); |
950 this->addFontDescriptor(0, &glyphIDs); | 933 this->addFontDescriptor(0, &glyphIDs); |
951 } else { | 934 } else { |
952 // Other CID fonts | 935 // Other CID fonts |
953 addFontDescriptor(0, nullptr); | 936 addFontDescriptor(0, nullptr); |
954 } | 937 } |
955 | 938 |
956 insertName("BaseFont", fontInfo()->fFontName); | 939 insertName("BaseFont", this->getFontInfo()->fFontName); |
957 | 940 |
958 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) { | 941 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) { |
959 insertName("Subtype", "CIDFontType0"); | 942 insertName("Subtype", "CIDFontType0"); |
960 } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) { | 943 } else if (getType() == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
961 insertName("Subtype", "CIDFontType2"); | 944 insertName("Subtype", "CIDFontType2"); |
962 insertName("CIDToGIDMap", "Identity"); | 945 insertName("CIDToGIDMap", "Identity"); |
963 } else { | 946 } else { |
964 SkASSERT(false); | 947 SkASSERT(false); |
965 } | 948 } |
966 | 949 |
967 auto sysInfo = sk_make_sp<SkPDFDict>(); | 950 auto sysInfo = sk_make_sp<SkPDFDict>(); |
968 sysInfo->insertString("Registry", "Adobe"); | 951 sysInfo->insertString("Registry", "Adobe"); |
969 sysInfo->insertString("Ordering", "Identity"); | 952 sysInfo->insertString("Ordering", "Identity"); |
970 sysInfo->insertInt("Supplement", 0); | 953 sysInfo->insertInt("Supplement", 0); |
971 this->insertObject("CIDSystemInfo", std::move(sysInfo)); | 954 this->insertObject("CIDSystemInfo", std::move(sysInfo)); |
972 | 955 |
973 SkSinglyLinkedList<AdvanceMetric> tmpMetrics; | 956 SkSinglyLinkedList<AdvanceMetric> tmpMetrics; |
974 set_glyph_widths(this->typeface(), &glyphIDs, &tmpMetrics); | 957 set_glyph_widths(this->typeface(), &glyphIDs, &tmpMetrics); |
975 int16_t defaultWidth = 0; | 958 int16_t defaultWidth = 0; |
976 uint16_t emSize = (uint16_t)this->fontInfo()->fEmSize; | 959 uint16_t emSize = (uint16_t)this->getFontInfo()->fEmSize; |
977 sk_sp<SkPDFArray> widths = composeAdvanceData(tmpMetrics, emSize, &defaultWi
dth); | 960 sk_sp<SkPDFArray> widths = composeAdvanceData(tmpMetrics, emSize, &defaultWi
dth); |
978 if (widths->size()) { | 961 if (widths->size()) { |
979 this->insertObject("W", std::move(widths)); | 962 this->insertObject("W", std::move(widths)); |
980 } | 963 } |
981 | 964 |
982 this->insertScalar( | 965 this->insertScalar( |
983 "DW", scaleFromFontUnits(defaultWidth, emSize)); | 966 "DW", scaleFromFontUnits(defaultWidth, emSize)); |
984 return true; | 967 return true; |
985 } | 968 } |
986 | 969 |
987 /////////////////////////////////////////////////////////////////////////////// | 970 /////////////////////////////////////////////////////////////////////////////// |
988 // class SkPDFType1Font | 971 // class SkPDFType1Font |
989 /////////////////////////////////////////////////////////////////////////////// | 972 /////////////////////////////////////////////////////////////////////////////// |
990 | 973 |
991 SkPDFType1Font::SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, | 974 SkPDFType1Font::SkPDFType1Font(sk_sp<const SkAdvancedTypefaceMetrics> info, |
992 SkTypeface* typeface, | 975 sk_sp<SkTypeface> typeface, |
993 uint16_t glyphID, | 976 uint16_t glyphID, |
994 SkPDFDict* relatedFontDescriptor) | 977 sk_sp<SkPDFDict> relatedFontDescriptor) |
995 : SkPDFFont(info, typeface, relatedFontDescriptor) { | 978 : SkPDFFont(std::move(info), |
996 this->populate(glyphID); | 979 std::move(typeface), |
| 980 std::move(relatedFontDescriptor), |
| 981 SkAdvancedTypefaceMetrics::kType1_Font, |
| 982 /* multiByteGlyphs = */ false) { |
| 983 this->populate(glyphID); // TODO(halcanary): subset this. |
997 } | 984 } |
998 | 985 |
999 SkPDFType1Font::~SkPDFType1Font() {} | 986 SkPDFType1Font::~SkPDFType1Font() {} |
1000 | 987 |
1001 bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { | 988 bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { |
1002 if (SkPDFDict* descriptor = getFontDescriptor()) { | 989 if (sk_sp<SkPDFDict> descriptor = this->refFontDescriptor()) { |
1003 this->insertObjRef("FontDescriptor", | 990 this->insertObjRef("FontDescriptor", std::move(descriptor)); |
1004 sk_ref_sp(descriptor)); | |
1005 return true; | 991 return true; |
1006 } | 992 } |
1007 | 993 |
1008 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); | 994 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
1009 setFontDescriptor(descriptor.get()); | 995 setFontDescriptor(descriptor); |
1010 | 996 |
1011 int ttcIndex; | 997 int ttcIndex; |
1012 size_t header SK_INIT_TO_AVOID_WARNING; | 998 size_t header SK_INIT_TO_AVOID_WARNING; |
1013 size_t data SK_INIT_TO_AVOID_WARNING; | 999 size_t data SK_INIT_TO_AVOID_WARNING; |
1014 size_t trailer SK_INIT_TO_AVOID_WARNING; | 1000 size_t trailer SK_INIT_TO_AVOID_WARNING; |
1015 std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex)
); | 1001 std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex)
); |
1016 sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), | 1002 sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), |
1017 &header, &data, &traile
r); | 1003 &header, &data, &traile
r); |
1018 if (!fontData) { | 1004 if (!fontData) { |
1019 return false; | 1005 return false; |
1020 } | 1006 } |
1021 SkASSERT(this->canEmbed()); | 1007 SkASSERT(this->canEmbed()); |
1022 auto fontStream = sk_make_sp<SkPDFStream>(std::move(fontData)); | 1008 auto fontStream = sk_make_sp<SkPDFStream>(std::move(fontData)); |
1023 fontStream->dict()->insertInt("Length1", header); | 1009 fontStream->dict()->insertInt("Length1", header); |
1024 fontStream->dict()->insertInt("Length2", data); | 1010 fontStream->dict()->insertInt("Length2", data); |
1025 fontStream->dict()->insertInt("Length3", trailer); | 1011 fontStream->dict()->insertInt("Length3", trailer); |
1026 descriptor->insertObjRef("FontFile", std::move(fontStream)); | 1012 descriptor->insertObjRef("FontFile", std::move(fontStream)); |
1027 | 1013 |
1028 this->insertObjRef("FontDescriptor", std::move(descriptor)); | 1014 this->insertObjRef("FontDescriptor", std::move(descriptor)); |
1029 | 1015 |
1030 return addCommonFontDescriptorEntries(defaultWidth); | 1016 return addCommonFontDescriptorEntries(defaultWidth); |
1031 } | 1017 } |
1032 | 1018 |
1033 bool SkPDFType1Font::populate(int16_t glyphID) { | 1019 bool SkPDFType1Font::populate(int16_t glyphID) { |
1034 this->insertName("Subtype", "Type1"); | 1020 this->insertName("Subtype", "Type1"); |
1035 this->insertName("BaseFont", fontInfo()->fFontName); | 1021 this->insertName("BaseFont", this->getFontInfo()->fFontName); |
1036 adjustGlyphRangeForSingleByteEncoding(glyphID); | 1022 adjustGlyphRangeForSingleByteEncoding(glyphID); |
1037 SkGlyphID firstGlyphID = this->firstGlyphID(); | 1023 SkGlyphID firstGlyphID = this->firstGlyphID(); |
1038 SkGlyphID lastGlyphID = this->lastGlyphID(); | 1024 SkGlyphID lastGlyphID = this->lastGlyphID(); |
1039 | 1025 |
1040 // glyphCount not including glyph 0 | 1026 // glyphCount not including glyph 0 |
1041 unsigned glyphCount = 1 + lastGlyphID - firstGlyphID; | 1027 unsigned glyphCount = 1 + lastGlyphID - firstGlyphID; |
1042 SkASSERT(glyphCount > 0 && glyphCount <= 255); | 1028 SkASSERT(glyphCount > 0 && glyphCount <= 255); |
1043 this->insertInt("FirstChar", (size_t)0); | 1029 this->insertInt("FirstChar", (size_t)0); |
1044 this->insertInt("LastChar", (size_t)glyphCount); | 1030 this->insertInt("LastChar", (size_t)glyphCount); |
1045 { | 1031 { |
1046 SkPaint tmpPaint; | 1032 SkPaint tmpPaint; |
1047 tmpPaint.setHinting(SkPaint::kNo_Hinting); | 1033 tmpPaint.setHinting(SkPaint::kNo_Hinting); |
1048 tmpPaint.setTypeface(sk_ref_sp(this->typeface())); | 1034 tmpPaint.setTypeface(sk_ref_sp(this->typeface())); |
1049 tmpPaint.setTextSize((SkScalar)this->typeface()->getUnitsPerEm()); | 1035 tmpPaint.setTextSize((SkScalar)this->typeface()->getUnitsPerEm()); |
1050 const SkSurfaceProps props(0, kUnknown_SkPixelGeometry); | 1036 const SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
1051 SkAutoGlyphCache glyphCache(tmpPaint, &props, nullptr); | 1037 SkAutoGlyphCache glyphCache(tmpPaint, &props, nullptr); |
1052 auto widths = sk_make_sp<SkPDFArray>(); | 1038 auto widths = sk_make_sp<SkPDFArray>(); |
1053 SkScalar advance = glyphCache->getGlyphIDAdvance(0).fAdvanceX; | 1039 SkScalar advance = glyphCache->getGlyphIDAdvance(0).fAdvanceX; |
1054 const uint16_t emSize = this->fontInfo()->fEmSize; | 1040 const uint16_t emSize = this->getFontInfo()->fEmSize; |
1055 widths->appendScalar(from_font_units(advance, emSize)); | 1041 widths->appendScalar(from_font_units(advance, emSize)); |
1056 for (unsigned gID = firstGlyphID; gID <= lastGlyphID; gID++) { | 1042 for (unsigned gID = firstGlyphID; gID <= lastGlyphID; gID++) { |
1057 advance = glyphCache->getGlyphIDAdvance(gID).fAdvanceX; | 1043 advance = glyphCache->getGlyphIDAdvance(gID).fAdvanceX; |
1058 widths->appendScalar(from_font_units(advance, emSize)); | 1044 widths->appendScalar(from_font_units(advance, emSize)); |
1059 } | 1045 } |
1060 this->insertObject("Widths", std::move(widths)); | 1046 this->insertObject("Widths", std::move(widths)); |
1061 } | 1047 } |
1062 if (!addFontDescriptor(0)) { | 1048 if (!addFontDescriptor(0)) { |
1063 return false; | 1049 return false; |
1064 } | 1050 } |
1065 auto encDiffs = sk_make_sp<SkPDFArray>(); | 1051 auto encDiffs = sk_make_sp<SkPDFArray>(); |
1066 encDiffs->reserve(lastGlyphID - firstGlyphID + 3); | 1052 encDiffs->reserve(lastGlyphID - firstGlyphID + 3); |
1067 encDiffs->appendInt(0); | 1053 encDiffs->appendInt(0); |
1068 const SkTArray<SkString>& glyphNames = this->fontInfo()->fGlyphNames; | 1054 const SkTArray<SkString>& glyphNames = this->getFontInfo()->fGlyphNames; |
1069 SkASSERT(glyphNames.count() > lastGlyphID); | 1055 SkASSERT(glyphNames.count() > lastGlyphID); |
1070 encDiffs->appendName(glyphNames[0].c_str()); | 1056 encDiffs->appendName(glyphNames[0].c_str()); |
1071 const SkString unknown("UNKNOWN"); | 1057 const SkString unknown("UNKNOWN"); |
1072 for (int gID = firstGlyphID; gID <= lastGlyphID; gID++) { | 1058 for (int gID = firstGlyphID; gID <= lastGlyphID; gID++) { |
1073 const bool valid = gID < glyphNames.count() && !glyphNames[gID].isEmpty(
); | 1059 const bool valid = gID < glyphNames.count() && !glyphNames[gID].isEmpty(
); |
1074 const SkString& name = valid ? glyphNames[gID] : unknown; | 1060 const SkString& name = valid ? glyphNames[gID] : unknown; |
1075 encDiffs->appendName(name); | 1061 encDiffs->appendName(name); |
1076 } | 1062 } |
1077 | 1063 |
1078 auto encoding = sk_make_sp<SkPDFDict>("Encoding"); | 1064 auto encoding = sk_make_sp<SkPDFDict>("Encoding"); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1228 subset, | 1214 subset, |
1229 false, | 1215 false, |
1230 firstGlyphID, | 1216 firstGlyphID, |
1231 lastGlyphID)); | 1217 lastGlyphID)); |
1232 } | 1218 } |
1233 font->insertObject("Widths", std::move(widthArray)); | 1219 font->insertObject("Widths", std::move(widthArray)); |
1234 font->insertObject("Encoding", std::move(encoding)); | 1220 font->insertObject("Encoding", std::move(encoding)); |
1235 font->insertObject("CharProcs", std::move(charProcs)); | 1221 font->insertObject("CharProcs", std::move(charProcs)); |
1236 } | 1222 } |
1237 | 1223 |
1238 SkPDFType3Font::SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, | 1224 SkPDFType3Font::SkPDFType3Font(sk_sp<const SkAdvancedTypefaceMetrics> info, |
1239 SkTypeface* typeface, | 1225 sk_sp<SkTypeface> typeface, |
| 1226 SkAdvancedTypefaceMetrics::FontType fontType, |
1240 uint16_t glyphID) | 1227 uint16_t glyphID) |
1241 : SkPDFFont(info, typeface, nullptr) { | 1228 : SkPDFFont(std::move(info), std::move(typeface), nullptr, |
| 1229 fontType, /* multiByteGlyphs = */ false) { |
1242 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. | 1230 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. |
1243 this->setLastGlyphID(SkToU16(typeface->countGlyphs() - 1)); | 1231 this->setLastGlyphID(SkToU16(this->typeface()->countGlyphs() - 1)); |
1244 this->adjustGlyphRangeForSingleByteEncoding(glyphID); | 1232 this->adjustGlyphRangeForSingleByteEncoding(glyphID); |
1245 } | 1233 } |
1246 | 1234 |
1247 sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(const SkPDFGlyphSet* usage) { | 1235 sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(const SkPDFGlyphSet* usage) { |
1248 // All fonts are subset before serialization. | 1236 // All fonts are subset before serialization. |
1249 // TODO(halcanary): all fonts should follow this pattern. | 1237 // TODO(halcanary): all fonts should follow this pattern. |
1250 auto font = sk_make_sp<SkPDFDict>("Font"); | 1238 auto font = sk_make_sp<SkPDFDict>("Font"); |
1251 const SkAdvancedTypefaceMetrics* info = this->fontInfo(); | 1239 const SkAdvancedTypefaceMetrics* info = this->getFontInfo(); |
1252 uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; | 1240 uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; |
1253 add_type3_font_info(font.get(), this->typeface(), (SkScalar)emSize, usage, | 1241 add_type3_font_info(font.get(), this->typeface(), (SkScalar)emSize, usage, |
1254 this->firstGlyphID(), this->lastGlyphID()); | 1242 this->firstGlyphID(), this->lastGlyphID()); |
1255 return font; | 1243 return font; |
1256 } | 1244 } |
1257 | 1245 |
1258 | 1246 |
1259 //////////////////////////////////////////////////////////////////////////////// | 1247 //////////////////////////////////////////////////////////////////////////////// |
1260 | 1248 |
1261 SkPDFFont::Match SkPDFFont::IsMatch(SkPDFFont* existingFont, | 1249 SkPDFFont::Match SkPDFFont::IsMatch(SkPDFFont* existingFont, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1297 } | 1285 } |
1298 return *canon->fCanEmbedTypeface.set(id, canEmbed); | 1286 return *canon->fCanEmbedTypeface.set(id, canEmbed); |
1299 } | 1287 } |
1300 | 1288 |
1301 void SkPDFFont::drop() { | 1289 void SkPDFFont::drop() { |
1302 fTypeface = nullptr; | 1290 fTypeface = nullptr; |
1303 fFontInfo = nullptr; | 1291 fFontInfo = nullptr; |
1304 fDescriptor = nullptr; | 1292 fDescriptor = nullptr; |
1305 this->SkPDFDict::drop(); | 1293 this->SkPDFDict::drop(); |
1306 } | 1294 } |
OLD | NEW |