Chromium Code Reviews| Index: src/pdf/SkPDFFont.cpp |
| diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp |
| index 769771ab236032e20cacbce780f2a842e0529a10..66df1bf894d383b5641e227339a51f6a789a700f 100644 |
| --- a/src/pdf/SkPDFFont.cpp |
| +++ b/src/pdf/SkPDFFont.cpp |
| @@ -57,9 +57,9 @@ struct AdvanceMetric { |
| class SkPDFType0Font final : public SkPDFFont { |
| public: |
| SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, |
| - SkTypeface* typeface); |
| + SkTypeface* typeface, |
| + SkAdvancedTypefaceMetrics::FontType type); |
| virtual ~SkPDFType0Font(); |
| - bool multiByteGlyphs() const override { return true; } |
| sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; |
| #ifdef SK_DEBUG |
| void emitObject(SkWStream*, |
| @@ -79,9 +79,9 @@ class SkPDFCIDFont final : public SkPDFFont { |
| public: |
| SkPDFCIDFont(const SkAdvancedTypefaceMetrics* info, |
| SkTypeface* typeface, |
| + SkAdvancedTypefaceMetrics::FontType fontType, |
| const SkPDFGlyphSet* subset); |
| virtual ~SkPDFCIDFont(); |
| - bool multiByteGlyphs() const override { return true; } |
| private: |
| bool populate(const SkPDFGlyphSet* subset); |
| @@ -96,7 +96,6 @@ public: |
| uint16_t glyphID, |
| SkPDFDict* relatedFontDescriptor); |
| virtual ~SkPDFType1Font(); |
| - bool multiByteGlyphs() const override { return false; } |
| private: |
| bool populate(int16_t glyphID); |
| @@ -107,6 +106,7 @@ class SkPDFType3Font final : public SkPDFFont { |
| public: |
| SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, |
| SkTypeface* typeface, |
| + SkAdvancedTypefaceMetrics::FontType fontType, |
| uint16_t glyphID); |
| virtual ~SkPDFType3Font() {} |
| void emitObject(SkWStream*, |
| @@ -115,7 +115,6 @@ public: |
| SkDEBUGFAIL("should call getFontSubset!"); |
| } |
| sk_sp<SkPDFObject> getFontSubset(const SkPDFGlyphSet* usage) override; |
| - bool multiByteGlyphs() const override { return false; } |
| }; |
| /////////////////////////////////////////////////////////////////////////////// |
| @@ -468,14 +467,6 @@ SkPDFGlyphSet* SkPDFGlyphSetMap::getGlyphSetForFont(SkPDFFont* font) { |
| SkPDFFont::~SkPDFFont() {} |
| -SkTypeface* SkPDFFont::typeface() { |
| - return fTypeface.get(); |
| -} |
| - |
| -SkAdvancedTypefaceMetrics::FontType SkPDFFont::getType() { |
| - return fFontType; |
| -} |
| - |
| bool SkPDFFont::canEmbed() const { |
| if (!fFontInfo.get()) { |
| SkASSERT(fFontType == SkAdvancedTypefaceMetrics::kOther_Font); |
| @@ -539,8 +530,8 @@ SkPDFFont* SkPDFFont::GetFontResource(SkPDFCanon* canon, |
| sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics; |
| SkPDFDict* relatedFontDescriptor = nullptr; |
| if (relatedFont) { |
| - fontMetrics.reset(SkSafeRef(relatedFont->fontInfo())); |
| - relatedFontDescriptor = relatedFont->getFontDescriptor(); |
| + fontMetrics = relatedFont->fontInfo(); |
| + relatedFontDescriptor = relatedFont->getFontDescriptor().get(); |
| // This only is to catch callers who pass invalid glyph ids. |
| // If glyph id is invalid, then we will create duplicate entries |
| @@ -554,9 +545,9 @@ SkPDFFont* SkPDFFont::GetFontResource(SkPDFCanon* canon, |
| return SkRef(relatedFont); |
| } |
| } else { |
| - SkTypeface::PerGlyphInfo info = |
| - SkTBitOr(SkTypeface::kGlyphNames_PerGlyphInfo, |
| - SkTypeface::kToUnicode_PerGlyphInfo); |
| + 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.
|
| + SkTypeface::kGlyphNames_PerGlyphInfo | |
| + SkTypeface::kToUnicode_PerGlyphInfo); |
| fontMetrics.reset( |
| typeface->getAdvancedTypefaceMetrics(info, nullptr, 0)); |
| } |
| @@ -574,16 +565,17 @@ sk_sp<SkPDFObject> SkPDFFont::getFontSubset(const SkPDFGlyphSet*) { |
| // TODO: take a sk_sp<SkAdvancedTypefaceMetrics> and sk_sp<SkTypeface> |
| SkPDFFont::SkPDFFont(const SkAdvancedTypefaceMetrics* info, |
| SkTypeface* typeface, |
| - SkPDFDict* relatedFontDescriptor) |
| + SkPDFDict* relatedFontDescriptor, |
| + SkAdvancedTypefaceMetrics::FontType fontType, |
| + bool multiByteGlyphs) |
| : SkPDFDict("Font") |
| , fTypeface(ref_or_default(typeface)) |
| - , fFirstGlyphID(1) |
| - , fLastGlyphID(info ? info->fLastGlyphID : 0) |
| , fFontInfo(SkSafeRef(info)) |
| , fDescriptor(SkSafeRef(relatedFontDescriptor)) |
| - , fFontType((!info || info->fFlags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag) |
| - ? SkAdvancedTypefaceMetrics::kOther_Font |
| - : info->fType) { |
| + , fFirstGlyphID(1) |
| + , fLastGlyphID(info ? info->fLastGlyphID : 0) |
| + , fFontType(fontType) |
| + , fMultiByteGlyphs(multiByteGlyphs) { |
| SkASSERT(fTypeface); |
| if (0 == fLastGlyphID) { |
| fLastGlyphID = SkToU16(fTypeface->countGlyphs() - 1); |
| @@ -598,17 +590,16 @@ SkPDFFont* SkPDFFont::Create(SkPDFCanon* canon, |
| SkPDFDict* relatedFontDescriptor) { |
| SkAdvancedTypefaceMetrics::FontType type = |
| info ? info->fType : SkAdvancedTypefaceMetrics::kOther_Font; |
| - SkAdvancedTypefaceMetrics::FontFlags flags = |
| - info ? info->fFlags : SkAdvancedTypefaceMetrics::kEmpty_FontFlag; |
| + 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.
|
| if (SkToBool(flags & SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag)) { |
| - return new SkPDFType3Font(info, typeface, glyphID); |
| + return new SkPDFType3Font(info, typeface, type, glyphID); |
| } |
| switch (type) { |
| case SkAdvancedTypefaceMetrics::kType1CID_Font: |
| case SkAdvancedTypefaceMetrics::kTrueType_Font: |
| SkASSERT(relatedFontDescriptor == nullptr); |
| SkASSERT(info != nullptr); |
| - return new SkPDFType0Font(info, typeface); |
| + return new SkPDFType0Font(info, typeface, type); |
| case SkAdvancedTypefaceMetrics::kType1_Font: |
| SkASSERT(info != nullptr); |
| return new SkPDFType1Font(info, typeface, glyphID, relatedFontDescriptor); |
| @@ -616,43 +607,24 @@ SkPDFFont* SkPDFFont::Create(SkPDFCanon* canon, |
| SkASSERT(info != nullptr); |
| // fallthrough |
| case SkAdvancedTypefaceMetrics::kOther_Font: |
| - return new SkPDFType3Font(info, typeface, glyphID); |
| + return new SkPDFType3Font(info, typeface, type, glyphID); |
| } |
| SkDEBUGFAIL("invalid SkAdvancedTypefaceMetrics::FontType"); |
| return nullptr; |
| } |
| -const SkAdvancedTypefaceMetrics* SkPDFFont::fontInfo() { |
| - return fFontInfo.get(); |
| -} |
| - |
| -void SkPDFFont::setFontInfo(const SkAdvancedTypefaceMetrics* info) { |
| - if (info == nullptr || info == fFontInfo.get()) { |
| - return; |
| +void SkPDFFont::setFontInfo(sk_sp<const SkAdvancedTypefaceMetrics> info) { |
| + if (info) { |
| + fFontInfo = std::move(info); |
| } |
| - fFontInfo.reset(info); |
| - SkSafeRef(info); |
| -} |
| - |
| -uint16_t SkPDFFont::firstGlyphID() const { |
| - return fFirstGlyphID; |
| -} |
| - |
| -uint16_t SkPDFFont::lastGlyphID() const { |
| - return fLastGlyphID; |
| } |
| void SkPDFFont::setLastGlyphID(uint16_t glyphID) { |
| fLastGlyphID = glyphID; |
| } |
| -SkPDFDict* SkPDFFont::getFontDescriptor() { |
| - return fDescriptor.get(); |
| -} |
| - |
| -void SkPDFFont::setFontDescriptor(SkPDFDict* descriptor) { |
| - fDescriptor.reset(descriptor); |
| - SkSafeRef(descriptor); |
| +void SkPDFFont::setFontDescriptor(sk_sp<SkPDFDict> descriptor) { |
| + fDescriptor = std::move(descriptor); |
| } |
| bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { |
| @@ -663,7 +635,7 @@ bool SkPDFFont::addCommonFontDescriptorEntries(int16_t defaultWidth) { |
| const uint16_t emSize = fFontInfo->fEmSize; |
| fDescriptor->insertName("FontName", fFontInfo->fFontName); |
| - fDescriptor->insertInt("Flags", fFontInfo->fStyle | kPdfSymbolic); |
| + 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.
|
| fDescriptor->insertScalar("Ascent", |
| scaleFromFontUnits(fFontInfo->fAscent, emSize)); |
| fDescriptor->insertScalar("Descent", |
| @@ -708,8 +680,10 @@ void SkPDFFont::populateToUnicodeTable(const SkPDFGlyphSet* subset) { |
| // class SkPDFType0Font |
| /////////////////////////////////////////////////////////////////////////////// |
| -SkPDFType0Font::SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, SkTypeface* typeface) |
| - : SkPDFFont(info, typeface, nullptr) { |
| +SkPDFType0Font::SkPDFType0Font(const SkAdvancedTypefaceMetrics* info, |
| + SkTypeface* typeface, |
| + SkAdvancedTypefaceMetrics::FontType fontType) |
| + : SkPDFFont(info, typeface, nullptr, fontType, true) { |
| SkDEBUGCODE(fPopulated = false); |
| if (!canSubset()) { |
| this->populate(nullptr); |
| @@ -722,7 +696,7 @@ sk_sp<SkPDFObject> SkPDFType0Font::getFontSubset(const SkPDFGlyphSet* subset) { |
| if (!canSubset()) { |
| return nullptr; |
| } |
| - auto newSubset = sk_make_sp<SkPDFType0Font>(fontInfo(), typeface()); |
| + auto newSubset = sk_make_sp<SkPDFType0Font>(fontInfo().get(), typeface(), getType()); |
| newSubset->populate(subset); |
| return newSubset; |
| } |
| @@ -742,7 +716,10 @@ bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { |
| insertName("Encoding", "Identity-H"); |
| sk_sp<SkPDFCIDFont> newCIDFont( |
| - new SkPDFCIDFont(fontInfo(), typeface(), subset)); |
| + new SkPDFCIDFont(this->fontInfo().get(), |
| + this->typeface(), |
| + this->getType(), |
| + subset)); |
| auto descendantFonts = sk_make_sp<SkPDFArray>(); |
| descendantFonts->appendObjRef(std::move(newCIDFont)); |
| this->insertObject("DescendantFonts", std::move(descendantFonts)); |
| @@ -759,8 +736,9 @@ bool SkPDFType0Font::populate(const SkPDFGlyphSet* subset) { |
| SkPDFCIDFont::SkPDFCIDFont(const SkAdvancedTypefaceMetrics* info, |
| SkTypeface* typeface, |
| + SkAdvancedTypefaceMetrics::FontType fontType, |
| const SkPDFGlyphSet* subset) |
| - : SkPDFFont(info, typeface, nullptr) { |
| + : SkPDFFont(info, typeface, nullptr, fontType , /* multiByteGlyphs = */ true) { |
| this->populate(subset); |
| } |
| @@ -822,7 +800,7 @@ static sk_sp<SkPDFObject> get_subset_font_stream( |
| bool SkPDFCIDFont::addFontDescriptor(int16_t defaultWidth, |
| const SkTDArray<uint32_t>* subset) { |
| auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
| - setFontDescriptor(descriptor.get()); |
| + setFontDescriptor(descriptor); |
| if (!addCommonFontDescriptorEntries(defaultWidth)) { |
| this->insertObjRef("FontDescriptor", std::move(descriptor)); |
| return false; |
| @@ -932,7 +910,7 @@ bool SkPDFCIDFont::populate(const SkPDFGlyphSet* subset) { |
| uint32_t glyphsCount = glyphs ? glyphIDs.count() : 0; |
| sk_sp<const SkAdvancedTypefaceMetrics> fontMetrics = |
| SkPDFFont::GetFontMetricsWithGlyphNames(this->typeface(), glyphs, glyphsCount); |
| - this->setFontInfo(fontMetrics.get()); |
| + this->setFontInfo(std::move(fontMetrics)); |
| this->addFontDescriptor(0, &glyphIDs); |
| } else { |
| // Other CID fonts |
| @@ -978,21 +956,25 @@ SkPDFType1Font::SkPDFType1Font(const SkAdvancedTypefaceMetrics* info, |
| SkTypeface* typeface, |
| uint16_t glyphID, |
| SkPDFDict* relatedFontDescriptor) |
| - : SkPDFFont(info, typeface, relatedFontDescriptor) { |
| - this->populate(glyphID); |
| + : SkPDFFont(info, |
| + typeface, |
| + relatedFontDescriptor, |
| + SkAdvancedTypefaceMetrics::kType1_Font, |
| + /* multiByteGlyphs = */ false) { |
| + this->populate(glyphID); // TODO(halcanary): subset this. |
| } |
| SkPDFType1Font::~SkPDFType1Font() {} |
| bool SkPDFType1Font::addFontDescriptor(int16_t defaultWidth) { |
| - if (SkPDFDict* descriptor = getFontDescriptor()) { |
| + if (sk_sp<SkPDFDict> descriptor = getFontDescriptor()) { |
| this->insertObjRef("FontDescriptor", |
| - sk_ref_sp(descriptor)); |
| + 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.
|
| return true; |
| } |
| auto descriptor = sk_make_sp<SkPDFDict>("FontDescriptor"); |
| - setFontDescriptor(descriptor.get()); |
| + setFontDescriptor(descriptor); |
| int ttcIndex; |
| size_t header SK_INIT_TO_AVOID_WARNING; |
| @@ -1223,8 +1205,9 @@ static void add_type3_font_info(SkPDFDict* font, |
| SkPDFType3Font::SkPDFType3Font(const SkAdvancedTypefaceMetrics* info, |
| SkTypeface* typeface, |
| + SkAdvancedTypefaceMetrics::FontType fontType, |
| uint16_t glyphID) |
| - : SkPDFFont(info, typeface, nullptr) { |
| + : SkPDFFont(info, typeface, nullptr, fontType, /* multiByteGlyphs = */ false) { |
| // If fLastGlyphID isn't set (because there is not fFontInfo), look it up. |
| this->setLastGlyphID(SkToU16(typeface->countGlyphs() - 1)); |
| this->adjustGlyphRangeForSingleByteEncoding(glyphID); |
| @@ -1234,7 +1217,7 @@ sk_sp<SkPDFObject> SkPDFType3Font::getFontSubset(const SkPDFGlyphSet* usage) { |
| // All fonts are subset before serialization. |
| // TODO(halcanary): all fonts should follow this pattern. |
| auto font = sk_make_sp<SkPDFDict>("Font"); |
| - const SkAdvancedTypefaceMetrics* info = this->fontInfo(); |
| + const SkAdvancedTypefaceMetrics* info = this->fontInfo().get(); |
| uint16_t emSize = info && info->fEmSize > 0 ? info->fEmSize : 1000; |
| add_type3_font_info(font.get(), this->typeface(), (SkScalar)emSize, usage, |
| this->firstGlyphID(), this->lastGlyphID()); |