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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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(const SkAdvancedTypefaceMetrics* info, |
60 SkTypeface* typeface); | 60 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(const SkAdvancedTypefaceMetrics* info, |
81 SkTypeface* typeface, | 81 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(const SkAdvancedTypefaceMetrics* info, |
95 SkTypeface* typeface, | 95 SkTypeface* typeface, |
96 uint16_t glyphID, | 96 uint16_t glyphID, |
97 SkPDFDict* relatedFontDescriptor); | 97 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(const SkAdvancedTypefaceMetrics* info, |
109 SkTypeface* typeface, | 108 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
532 SkASSERT(typeface); | 523 SkASSERT(typeface); |
533 int glyphCount = typeface->countGlyphs(); | 524 int glyphCount = typeface->countGlyphs(); |
534 if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. | 525 if (glyphCount < 1 || // typeface lacks even a NOTDEF glyph. |
535 glyphCount > 1 + SK_MaxU16 || // invalid glyphCount | 526 glyphCount > 1 + SK_MaxU16 || // invalid glyphCount |
536 glyphID >= glyphCount) { // invalid glyph | 527 glyphID >= glyphCount) { // invalid glyph |
537 return nullptr; | 528 return nullptr; |
538 } | 529 } |
539 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics; | 530 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics; |
540 SkPDFDict* relatedFontDescriptor = nullptr; | 531 SkPDFDict* relatedFontDescriptor = nullptr; |
541 if (relatedFont) { | 532 if (relatedFont) { |
542 fontMetrics.reset(SkSafeRef(relatedFont->fontInfo())); | 533 fontMetrics = relatedFont->fontInfo(); |
543 relatedFontDescriptor = relatedFont->getFontDescriptor(); | 534 relatedFontDescriptor = relatedFont->getFontDescriptor().get(); |
544 | 535 |
545 // This only is to catch callers who pass invalid glyph ids. | 536 // This only is to catch callers who pass invalid glyph ids. |
546 // If glyph id is invalid, then we will create duplicate entries | 537 // If glyph id is invalid, then we will create duplicate entries |
547 // for TrueType fonts. | 538 // for TrueType fonts. |
548 SkAdvancedTypefaceMetrics::FontType fontType = | 539 SkAdvancedTypefaceMetrics::FontType fontType = |
549 fontMetrics.get() ? fontMetrics.get()->fType : | 540 fontMetrics.get() ? fontMetrics.get()->fType : |
550 SkAdvancedTypefaceMetrics::kOther_Font; | 541 SkAdvancedTypefaceMetrics::kOther_Font; |
551 | 542 |
552 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font || | 543 if (fontType == SkAdvancedTypefaceMetrics::kType1CID_Font || |
553 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) { | 544 fontType == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
554 return SkRef(relatedFont); | 545 return SkRef(relatedFont); |
555 } | 546 } |
556 } else { | 547 } else { |
557 SkTypeface::PerGlyphInfo info = | 548 auto info = (SkTypeface::PerGlyphInfo)( |
bungeman-skia
2016/08/15 16:49:58
This is a really gross use of a c style cast and a
hal.canary
2016/08/15 18:05:23
Done.
| |
558 SkTBitOr(SkTypeface::kGlyphNames_PerGlyphInfo, | 549 SkTypeface::kGlyphNames_PerGlyphInfo | |
559 SkTypeface::kToUnicode_PerGlyphInfo); | 550 SkTypeface::kToUnicode_PerGlyphInfo); |
560 fontMetrics.reset( | 551 fontMetrics.reset( |
561 typeface->getAdvancedTypefaceMetrics(info, nullptr, 0)); | 552 typeface->getAdvancedTypefaceMetrics(info, nullptr, 0)); |
562 } | 553 } |
563 | 554 |
564 SkPDFFont* font = SkPDFFont::Create(canon, fontMetrics.get(), typeface, | 555 SkPDFFont* font = SkPDFFont::Create(canon, fontMetrics.get(), typeface, |
565 glyphID, relatedFontDescriptor); | 556 glyphID, relatedFontDescriptor); |
566 canon->addFont(font, fontID, font->fFirstGlyphID); | 557 canon->addFont(font, fontID, font->fFirstGlyphID); |
567 return font; | 558 return font; |
568 } | 559 } |
569 | 560 |
570 sk_sp<SkPDFObject> SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { | 561 sk_sp<SkPDFObject> SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { |
571 return nullptr; // Default: no support. | 562 return nullptr; // Default: no support. |
572 } | 563 } |
573 | 564 |
574 // TODO: take a sk_sp<SkAdvancedTypefaceMetrics> and sk_sp<SkTypeface> | 565 // TODO: take a sk_sp<SkAdvancedTypefaceMetrics> and sk_sp<SkTypeface> |
575 SkPDFFont::SkPDFFont(const SkAdvancedTypefaceMetrics* info, | 566 SkPDFFont::SkPDFFont(const SkAdvancedTypefaceMetrics* info, |
576 SkTypeface* typeface, | 567 SkTypeface* typeface, |
577 SkPDFDict* relatedFontDescriptor) | 568 SkPDFDict* relatedFontDescriptor, |
569 SkAdvancedTypefaceMetrics::FontType fontType, | |
570 bool multiByteGlyphs) | |
578 : SkPDFDict("Font") | 571 : SkPDFDict("Font") |
579 , fTypeface(ref_or_default(typeface)) | 572 , fTypeface(ref_or_default(typeface)) |
573 , fFontInfo(SkSafeRef(info)) | |
574 , fDescriptor(SkSafeRef(relatedFontDescriptor)) | |
580 , fFirstGlyphID(1) | 575 , fFirstGlyphID(1) |
581 , fLastGlyphID(info ? info->fLastGlyphID : 0) | 576 , fLastGlyphID(info ? info->fLastGlyphID : 0) |
582 , fFontInfo(SkSafeRef(info)) | 577 , fFontType(fontType) |
583 , fDescriptor(SkSafeRef(relatedFontDescriptor)) | 578 , fMultiByteGlyphs(multiByteGlyphs) { |
584 , fFontType((!info || info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster _FontFlag) | |
585 ? SkAdvancedTypefaceMetrics::kOther_Font | |
586 : info->fType) { | |
587 SkASSERT(fTypeface); | 579 SkASSERT(fTypeface); |
588 if (0 == fLastGlyphID) { | 580 if (0 == fLastGlyphID) { |
589 fLastGlyphID = SkToU16(fTypeface->countGlyphs() - 1); | 581 fLastGlyphID = SkToU16(fTypeface->countGlyphs() - 1); |
590 } | 582 } |
591 } | 583 } |
592 | 584 |
593 // static | 585 // static |
594 SkPDFFont* SkPDFFont::Create(SkPDFCanon* canon, | 586 SkPDFFont* SkPDFFont::Create(SkPDFCanon* canon, |
595 const SkAdvancedTypefaceMetrics* info, | 587 const SkAdvancedTypefaceMetrics* info, |
596 SkTypeface* typeface, | 588 SkTypeface* typeface, |
597 uint16_t glyphID, | 589 uint16_t glyphID, |
598 SkPDFDict* relatedFontDescriptor) { | 590 SkPDFDict* relatedFontDescriptor) { |
599 SkAdvancedTypefaceMetrics::FontType type = | 591 SkAdvancedTypefaceMetrics::FontType type = |
600 info ? info->fType : SkAdvancedTypefaceMetrics::kOther_Font; | 592 info ? info->fType : SkAdvancedTypefaceMetrics::kOther_Font; |
601 SkAdvancedTypefaceMetrics::FontFlags flags = | 593 uint8_t flags = info ? info->fFlags : 0; |
bungeman-skia
2016/08/15 16:49:58
This uint8_t is really opaque. Before the type was
hal.canary
2016/08/15 18:05:23
Done.
| |
602 info ? info->fFlags : SkAdvancedTypefaceMetrics::kEmpty_FontFlag; | |
603 if (SkToBool(flags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { | 594 if (SkToBool(flags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { |
604 return new SkPDFType3Font(info, typeface, glyphID); | 595 return new SkPDFType3Font(info, typeface, type, glyphID); |
605 } | 596 } |
606 switch (type) { | 597 switch (type) { |
607 case SkAdvancedTypefaceMetrics::kType1CID_Font: | 598 case SkAdvancedTypefaceMetrics::kType1CID_Font: |
608 case SkAdvancedTypefaceMetrics::kTrueType_Font: | 599 case SkAdvancedTypefaceMetrics::kTrueType_Font: |
609 SkASSERT(relatedFontDescriptor == nullptr); | 600 SkASSERT(relatedFontDescriptor == nullptr); |
610 SkASSERT(info != nullptr); | 601 SkASSERT(info != nullptr); |
611 return new SkPDFType0Font(info, typeface); | 602 return new SkPDFType0Font(info, typeface, type); |
612 case SkAdvancedTypefaceMetrics::kType1_Font: | 603 case SkAdvancedTypefaceMetrics::kType1_Font: |
613 SkASSERT(info != nullptr); | 604 SkASSERT(info != nullptr); |
614 return new SkPDFType1Font(info, typeface, glyphID, relatedFontDescri ptor); | 605 return new SkPDFType1Font(info, typeface, glyphID, relatedFontDescri ptor); |
615 case SkAdvancedTypefaceMetrics::kCFF_Font: | 606 case SkAdvancedTypefaceMetrics::kCFF_Font: |
616 SkASSERT(info != nullptr); | 607 SkASSERT(info != nullptr); |
617 // fallthrough | 608 // fallthrough |
618 case SkAdvancedTypefaceMetrics::kOther_Font: | 609 case SkAdvancedTypefaceMetrics::kOther_Font: |
619 return new SkPDFType3Font(info, typeface, glyphID); | 610 return new SkPDFType3Font(info, typeface, type, glyphID); |
620 } | 611 } |
621 SkDEBUGFAIL("invalid SkAdvancedTypefaceMetrics::FontType"); | 612 SkDEBUGFAIL("invalid SkAdvancedTypefaceMetrics::FontType"); |
622 return nullptr; | 613 return nullptr; |
623 } | 614 } |
624 | 615 |
625 const SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { | 616 void SkPDFFont::setFontInfo(sk_sp<const SkAdvancedTypefaceMetrics> info) { |
626 return fFontInfo.get(); | 617 if (info) { |
627 } | 618 fFontInfo = std::move(info); |
628 | |
629 void SkPDFFont::setFontInfo(const SkAdvancedTypefaceMetrics* info) { | |
630 if (info == nullptr || info == fFontInfo.get()) { | |
631 return; | |
632 } | 619 } |
633 fFontInfo.reset(info); | |
634 SkSafeRef(info); | |
635 } | |
636 | |
637 uint16_t SkPDFFont::firstGlyphID() const { | |
638 return fFirstGlyphID; | |
639 } | |
640 | |
641 uint16_t SkPDFFont::lastGlyphID() const { | |
642 return fLastGlyphID; | |
643 } | 620 } |
644 | 621 |
645 void SkPDFFont::setLastGlyphID(uint16_t glyphID) { | 622 void SkPDFFont::setLastGlyphID(uint16_t glyphID) { |
646 fLastGlyphID = glyphID; | 623 fLastGlyphID = glyphID; |
647 } | 624 } |
648 | 625 |
649 SkPDFDict* SkPDFFont::getFontDescriptor() { | 626 void SkPDFFont::setFontDescriptor(sk_sp<SkPDFDict> descriptor) { |
650 return fDescriptor.get(); | 627 fDescriptor = std::move(descriptor); |
651 } | |
652 | |
653 void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) { | |
654 fDescriptor.reset(descriptor); | |
655 SkSafeRef(descriptor); | |
656 } | 628 } |
657 | 629 |
658 bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { | 630 bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { |
659 if (fDescriptor.get() == nullptr) { | 631 if (fDescriptor.get() == nullptr) { |
660 return false; | 632 return false; |
661 } | 633 } |
662 | 634 |
663 const uint16_t emSize = fFontInfo->fEmSize; | 635 const uint16_t emSize = fFontInfo->fEmSize; |
664 | 636 |
665 fDescriptor->insertName("FontName", fFontInfo->fFontName); | 637 fDescriptor->insertName("FontName", fFontInfo->fFontName); |
666 fDescriptor->insertInt("Flags", fFontInfo->fStyle | kPdfSymbolic); | 638 fDescriptor->insertInt("Flags", (int32_t)(fFontInfo->fStyle | kPdfSymbolic)) ; |
bungeman-skia
2016/08/15 16:49:58
Much prefer to write an operator '|' and static_ca
hal.canary
2016/08/15 18:05:23
Done.
| |
667 fDescriptor->insertScalar("Ascent", | 639 fDescriptor->insertScalar("Ascent", |
668 scaleFromFontUnits(fFontInfo->fAscent, emSize)); | 640 scaleFromFontUnits(fFontInfo->fAscent, emSize)); |
669 fDescriptor->insertScalar("Descent", | 641 fDescriptor->insertScalar("Descent", |
670 scaleFromFontUnits(fFontInfo->fDescent, emSize)); | 642 scaleFromFontUnits(fFontInfo->fDescent, emSize)); |
671 fDescriptor->insertScalar("StemV", | 643 fDescriptor->insertScalar("StemV", |
672 scaleFromFontUnits(fFontInfo->fStemV, emSize)); | 644 scaleFromFontUnits(fFontInfo->fStemV, emSize)); |
673 | 645 |
674 fDescriptor->insertScalar("CapHeight", | 646 fDescriptor->insertScalar("CapHeight", |
675 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)); | 647 scaleFromFontUnits(fFontInfo->fCapHeight, emSize)); |
676 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle); | 648 fDescriptor->insertInt("ItalicAngle", fFontInfo->fItalicAngle); |
(...skipping 24 matching lines...) Expand all Loading... | |
701 subset, | 673 subset, |
702 multiByteGlyphs(), | 674 multiByteGlyphs(), |
703 firstGlyphID(), | 675 firstGlyphID(), |
704 lastGlyphID())); | 676 lastGlyphID())); |
705 } | 677 } |
706 | 678 |
707 /////////////////////////////////////////////////////////////////////////////// | 679 /////////////////////////////////////////////////////////////////////////////// |
708 // class SkPDFType0Font | 680 // class SkPDFType0Font |
709 /////////////////////////////////////////////////////////////////////////////// | 681 /////////////////////////////////////////////////////////////////////////////// |
710 | 682 |
711 SkPDFType0Font::SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, SkTypeface * typeface) | 683 SkPDFType0Font::SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, |
712 : SkPDFFont(info, typeface, nullptr) { | 684 SkTypeface* typeface, |
685 SkAdvancedTypefaceMetrics::FontType fontType) | |
686 : SkPDFFont(info, typeface, nullptr, fontType, true) { | |
713 SkDEBUGCODE(fPopulated = false); | 687 SkDEBUGCODE(fPopulated = false); |
714 if (!canSubset()) { | 688 if (!canSubset()) { |
715 this->populate(nullptr); | 689 this->populate(nullptr); |
716 } | 690 } |
717 } | 691 } |
718 | 692 |
719 SkPDFType0Font::~SkPDFType0Font() {} | 693 SkPDFType0Font::~SkPDFType0Font() {} |
720 | 694 |
721 sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { | 695 sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { |
722 if (!canSubset()) { | 696 if (!canSubset()) { |
723 return nullptr; | 697 return nullptr; |
724 } | 698 } |
725 auto newSubset = sk_make_sp<SkPDFType0Font>(fontInfo(), typeface()); | 699 auto newSubset = sk_make_sp<SkPDFType0Font>(fontInfo().get(), typeface(), ge tType()); |
726 newSubset->populate(subset); | 700 newSubset->populate(subset); |
727 return newSubset; | 701 return newSubset; |
728 } | 702 } |
729 | 703 |
730 #ifdef SK_DEBUG | 704 #ifdef SK_DEBUG |
731 void SkPDFType0Font::emitObject(SkWStream* stream, | 705 void SkPDFType0Font::emitObject(SkWStream* stream, |
732 const SkPDFObjNumMap& objNumMap, | 706 const SkPDFObjNumMap& objNumMap, |
733 const SkPDFSubstituteMap& substitutes) const { | 707 const SkPDFSubstituteMap& substitutes) const { |
734 SkASSERT(fPopulated); | 708 SkASSERT(fPopulated); |
735 return INHERITED::emitObject(stream, objNumMap, substitutes); | 709 return INHERITED::emitObject(stream, objNumMap, substitutes); |
736 } | 710 } |
737 #endif | 711 #endif |
738 | 712 |
739 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { | 713 bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { |
740 insertName("Subtype", "Type0"); | 714 insertName("Subtype", "Type0"); |
741 insertName("BaseFont", fontInfo()->fFontName); | 715 insertName("BaseFont", fontInfo()->fFontName); |
742 insertName("Encoding", "Identity-H"); | 716 insertName("Encoding", "Identity-H"); |
743 | 717 |
744 sk_sp<SkPDFCIDFont> newCIDFont( | 718 sk_sp<SkPDFCIDFont> newCIDFont( |
745 new SkPDFCIDFont(fontInfo(), typeface(), subset)); | 719 new SkPDFCIDFont(this->fontInfo().get(), |
720 this->typeface(), | |
721 this->getType(), | |
722 subset)); | |
746 auto descendantFonts = sk_make_sp<SkPDFArray>(); | 723 auto descendantFonts = sk_make_sp<SkPDFArray>(); |
747 descendantFonts->appendObjRef(std::move(newCIDFont)); | 724 descendantFonts->appendObjRef(std::move(newCIDFont)); |
748 this->insertObject("DescendantFonts", std::move(descendantFonts)); | 725 this->insertObject("DescendantFonts", std::move(descendantFonts)); |
749 | 726 |
750 this->populateToUnicodeTable(subset); | 727 this->populateToUnicodeTable(subset); |
751 | 728 |
752 SkDEBUGCODE(fPopulated = true); | 729 SkDEBUGCODE(fPopulated = true); |
753 return true; | 730 return true; |
754 } | 731 } |
755 | 732 |
756 /////////////////////////////////////////////////////////////////////////////// | 733 /////////////////////////////////////////////////////////////////////////////// |
757 // class SkPDFCIDFont | 734 // class SkPDFCIDFont |
758 /////////////////////////////////////////////////////////////////////////////// | 735 /////////////////////////////////////////////////////////////////////////////// |
759 | 736 |
760 SkPDFCIDFont::SkPDFCIDFont(const SkAdvancedTypefaceMetrics* info, | 737 SkPDFCIDFont::SkPDFCIDFont(const SkAdvancedTypefaceMetrics* info, |
761 SkTypeface* typeface, | 738 SkTypeface* typeface, |
739 SkAdvancedTypefaceMetrics::FontType fontType, | |
762 const SkPDFGlyphSet* subset) | 740 const SkPDFGlyphSet* subset) |
763 : SkPDFFont(info, typeface, nullptr) { | 741 : SkPDFFont(info, typeface, nullptr, fontType , /* multiByteGlyphs = */ true ) { |
764 this->populate(subset); | 742 this->populate(subset); |
765 } | 743 } |
766 | 744 |
767 SkPDFCIDFont::~SkPDFCIDFont() {} | 745 SkPDFCIDFont::~SkPDFCIDFont() {} |
768 | 746 |
769 #ifdef SK_SFNTLY_SUBSETTER | 747 #ifdef SK_SFNTLY_SUBSETTER |
770 // if possible, make no copy. | 748 // if possible, make no copy. |
771 static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) { | 749 static sk_sp<SkData> stream_to_data(std::unique_ptr<SkStreamAsset> stream) { |
772 SkASSERT(stream); | 750 SkASSERT(stream); |
773 (void)stream->rewind(); | 751 (void)stream->rewind(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
815 [](const void* p, void*) { delete[] (unsigned char*)p; }, | 793 [](const void* p, void*) { delete[] (unsigned char*)p; }, |
816 nullptr)); | 794 nullptr)); |
817 subsetStream->dict()->insertInt("Length1", subsetFontSize); | 795 subsetStream->dict()->insertInt("Length1", subsetFontSize); |
818 return subsetStream; | 796 return subsetStream; |
819 } | 797 } |
820 #endif // SK_SFNTLY_SUBSETTER | 798 #endif // SK_SFNTLY_SUBSETTER |
821 | 799 |
822 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, | 800 bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, |
823 const SkTDArray<uint32_t>* subset) { | 801 const SkTDArray<uint32_t>* subset) { |
824 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); | 802 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
825 setFontDescriptor(descriptor.get()); | 803 setFontDescriptor(descriptor); |
826 if (!addCommonFontDescriptorEntries(defaultWidth)) { | 804 if (!addCommonFontDescriptorEntries(defaultWidth)) { |
827 this->insertObjRef("FontDescriptor", std::move(descriptor)); | 805 this->insertObjRef("FontDescriptor", std::move(descriptor)); |
828 return false; | 806 return false; |
829 } | 807 } |
830 SkASSERT(this->canEmbed()); | 808 SkASSERT(this->canEmbed()); |
831 | 809 |
832 switch (getType()) { | 810 switch (getType()) { |
833 case SkAdvancedTypefaceMetrics::kTrueType_Font: { | 811 case SkAdvancedTypefaceMetrics::kTrueType_Font: { |
834 int ttcIndex; | 812 int ttcIndex; |
835 std::unique_ptr<SkStreamAsset> fontAsset( | 813 std::unique_ptr<SkStreamAsset> fontAsset( |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
925 if (!subset->has(0)) { | 903 if (!subset->has(0)) { |
926 glyphIDs.push(0); // Always include glyph 0. | 904 glyphIDs.push(0); // Always include glyph 0. |
927 } | 905 } |
928 subset->exportTo(&glyphIDs); | 906 subset->exportTo(&glyphIDs); |
929 } | 907 } |
930 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { | 908 if (fontInfo()->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { |
931 uint32_t* glyphs = (glyphIDs.count() == 0) ? nullptr : glyphIDs.begin(); | 909 uint32_t* glyphs = (glyphIDs.count() == 0) ? nullptr : glyphIDs.begin(); |
932 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; | 910 uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; |
933 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics = | 911 sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics = |
934 SkPDFFont::GetFontMetricsWithGlyphNames(this->typeface(), glyphs, gl yphsCount); | 912 SkPDFFont::GetFontMetricsWithGlyphNames(this->typeface(), glyphs, gl yphsCount); |
935 this->setFontInfo(fontMetrics.get()); | 913 this->setFontInfo(std::move(fontMetrics)); |
936 this->addFontDescriptor(0, &glyphIDs); | 914 this->addFontDescriptor(0, &glyphIDs); |
937 } else { | 915 } else { |
938 // Other CID fonts | 916 // Other CID fonts |
939 addFontDescriptor(0, nullptr); | 917 addFontDescriptor(0, nullptr); |
940 } | 918 } |
941 | 919 |
942 insertName("BaseFont", fontInfo()->fFontName); | 920 insertName("BaseFont", fontInfo()->fFontName); |
943 | 921 |
944 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) { | 922 if (getType() == SkAdvancedTypefaceMetrics::kType1CID_Font) { |
945 insertName("Subtype", "CIDFontType0"); | 923 insertName("Subtype", "CIDFontType0"); |
(...skipping 25 matching lines...) Expand all Loading... | |
971 } | 949 } |
972 | 950 |
973 /////////////////////////////////////////////////////////////////////////////// | 951 /////////////////////////////////////////////////////////////////////////////// |
974 // class SkPDFType1Font | 952 // class SkPDFType1Font |
975 /////////////////////////////////////////////////////////////////////////////// | 953 /////////////////////////////////////////////////////////////////////////////// |
976 | 954 |
977 SkPDFType1Font::SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, | 955 SkPDFType1Font::SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, |
978 SkTypeface* typeface, | 956 SkTypeface* typeface, |
979 uint16_t glyphID, | 957 uint16_t glyphID, |
980 SkPDFDict* relatedFontDescriptor) | 958 SkPDFDict* relatedFontDescriptor) |
981 : SkPDFFont(info, typeface, relatedFontDescriptor) { | 959 : SkPDFFont(info, |
982 this->populate(glyphID); | 960 typeface, |
961 relatedFontDescriptor, | |
962 SkAdvancedTypefaceMetrics::kType1_Font, | |
963 /* multiByteGlyphs = */ false) { | |
964 this->populate(glyphID); // TODO(halcanary): subset this. | |
983 } | 965 } |
984 | 966 |
985 SkPDFType1Font::~SkPDFType1Font() {} | 967 SkPDFType1Font::~SkPDFType1Font() {} |
986 | 968 |
987 bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { | 969 bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { |
988 if (SkPDFDict* descriptor = getFontDescriptor()) { | 970 if (sk_sp<SkPDFDict> descriptor = getFontDescriptor()) { |
989 this->insertObjRef("FontDescriptor", | 971 this->insertObjRef("FontDescriptor", |
990 sk_ref_sp(descriptor)); | 972 std::move(descriptor)); |
bungeman-skia
2016/08/15 16:49:58
nit: fits on previous line?
hal.canary
2016/08/15 18:05:23
Done.
| |
991 return true; | 973 return true; |
992 } | 974 } |
993 | 975 |
994 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); | 976 auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
995 setFontDescriptor(descriptor.get()); | 977 setFontDescriptor(descriptor); |
996 | 978 |
997 int ttcIndex; | 979 int ttcIndex; |
998 size_t header SK_INIT_TO_AVOID_WARNING; | 980 size_t header SK_INIT_TO_AVOID_WARNING; |
999 size_t data SK_INIT_TO_AVOID_WARNING; | 981 size_t data SK_INIT_TO_AVOID_WARNING; |
1000 size_t trailer SK_INIT_TO_AVOID_WARNING; | 982 size_t trailer SK_INIT_TO_AVOID_WARNING; |
1001 std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex) ); | 983 std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex) ); |
1002 sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), | 984 sk_sp<SkData> fontData = SkPDFConvertType1FontStream(std::move(rawFontData), |
1003 &header, &data, &traile r); | 985 &header, &data, &traile r); |
1004 if (!fontData) { | 986 if (!fontData) { |
1005 return false; | 987 return false; |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1216 firstGlyphID, | 1198 firstGlyphID, |
1217 lastGlyphID)); | 1199 lastGlyphID)); |
1218 } | 1200 } |
1219 font->insertObject("Widths", std::move(widthArray)); | 1201 font->insertObject("Widths", std::move(widthArray)); |
1220 font->insertObject("Encoding", std::move(encoding)); | 1202 font->insertObject("Encoding", std::move(encoding)); |
1221 font->insertObject("CharProcs", std::move(charProcs)); | 1203 font->insertObject("CharProcs", std::move(charProcs)); |
1222 } | 1204 } |
1223 | 1205 |
1224 SkPDFType3Font::SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, | 1206 SkPDFType3Font::SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, |
1225 SkTypeface* typeface, | 1207 SkTypeface* typeface, |
1208 SkAdvancedTypefaceMetrics::FontType fontType, | |
1226 uint16_t glyphID) | 1209 uint16_t glyphID) |
1227 : SkPDFFont(info, typeface, nullptr) { | 1210 : SkPDFFont(info, typeface, nullptr, fontType, /* multiByteGlyphs = */ false ) { |
1228 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. | 1211 // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. |
1229 this->setLastGlyphID(SkToU16(typeface->countGlyphs() - 1)); | 1212 this->setLastGlyphID(SkToU16(typeface->countGlyphs() - 1)); |
1230 this->adjustGlyphRangeForSingleByteEncoding(glyphID); | 1213 this->adjustGlyphRangeForSingleByteEncoding(glyphID); |
1231 } | 1214 } |
1232 | 1215 |
1233 sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(const SkPDFGlyphSet* usage) { | 1216 sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(const SkPDFGlyphSet* usage) { |
1234 // All fonts are subset before serialization. | 1217 // All fonts are subset before serialization. |
1235 // TODO(halcanary): all fonts should follow this pattern. | 1218 // TODO(halcanary): all fonts should follow this pattern. |
1236 auto font = sk_make_sp<SkPDFDict>("Font"); | 1219 auto font = sk_make_sp<SkPDFDict>("Font"); |
1237 const SkAdvancedTypefaceMetrics* info = this->fontInfo(); | 1220 const SkAdvancedTypefaceMetrics* info = this->fontInfo().get(); |
1238 uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; | 1221 uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; |
1239 add_type3_font_info(font.get(), this->typeface(), (SkScalar)emSize, usage, | 1222 add_type3_font_info(font.get(), this->typeface(), (SkScalar)emSize, usage, |
1240 this->firstGlyphID(), this->lastGlyphID()); | 1223 this->firstGlyphID(), this->lastGlyphID()); |
1241 return font; | 1224 return font; |
1242 } | 1225 } |
1243 | 1226 |
1244 | 1227 |
1245 //////////////////////////////////////////////////////////////////////////////// | 1228 //////////////////////////////////////////////////////////////////////////////// |
1246 | 1229 |
1247 SkPDFFont::Match SkPDFFont::IsMatch(SkPDFFont* existingFont, | 1230 SkPDFFont::Match SkPDFFont::IsMatch(SkPDFFont* existingFont, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1283 } | 1266 } |
1284 return *canon->fCanEmbedTypeface.set(id, canEmbed); | 1267 return *canon->fCanEmbedTypeface.set(id, canEmbed); |
1285 } | 1268 } |
1286 | 1269 |
1287 void SkPDFFont::drop() { | 1270 void SkPDFFont::drop() { |
1288 fTypeface = nullptr; | 1271 fTypeface = nullptr; |
1289 fFontInfo = nullptr; | 1272 fFontInfo = nullptr; |
1290 fDescriptor = nullptr; | 1273 fDescriptor = nullptr; |
1291 this->SkPDFDict::drop(); | 1274 this->SkPDFDict::drop(); |
1292 } | 1275 } |
OLD | NEW |