diff options
author | Hal Canary <halcanary@google.com> | 2018-04-10 11:13:24 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-05-07 20:07:57 +0000 |
commit | 5bdc4d590b905b3d3fba0da80051ac576e705e2c (patch) | |
tree | 03a033f7bcf43ccab3ed5b3c857a78e930b5429f | |
parent | e0b7f5cb0c3ebc14eb1493217876d7243039a801 (diff) |
SkAdvancedTypefaceMetrics: simplify more
Change-Id: I80c28c50aa96c28181d2fef13daad4a80ec91a12
Reviewed-on: https://skia-review.googlesource.com/120160
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
-rw-r--r-- | include/core/SkTypeface.h | 4 | ||||
-rw-r--r-- | src/core/SkAdvancedTypefaceMetrics.h | 3 | ||||
-rw-r--r-- | src/pdf/SkPDFCanon.h | 4 | ||||
-rw-r--r-- | src/pdf/SkPDFFont.cpp | 25 | ||||
-rw-r--r-- | src/pdf/SkPDFFont.h | 1 | ||||
-rw-r--r-- | src/ports/SkFontHost_FreeType.cpp | 51 | ||||
-rw-r--r-- | src/ports/SkFontHost_FreeType_common.h | 1 |
7 files changed, 54 insertions, 35 deletions
diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h index b55ac4ecb3..37878cc390 100644 --- a/include/core/SkTypeface.h +++ b/include/core/SkTypeface.h @@ -313,6 +313,10 @@ protected: // Subclasses *must* override this method to work with the PDF backend. virtual std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const; + // For type1 postscript fonts only, set the glyph names for each glyph. + // destination array is non-null, and points to an array of size this->countGlyphs(). + // Backends that do not suport type1 fonts should not override. + virtual void getPostScriptGlyphNames(SkString*) const {} virtual SkStreamAsset* onOpenStream(int* ttcIndex) const = 0; // TODO: make pure virtual. diff --git a/src/core/SkAdvancedTypefaceMetrics.h b/src/core/SkAdvancedTypefaceMetrics.h index ea25340580..6be3d443bf 100644 --- a/src/core/SkAdvancedTypefaceMetrics.h +++ b/src/core/SkAdvancedTypefaceMetrics.h @@ -71,9 +71,6 @@ struct SkAdvancedTypefaceMetrics { SkIRect fBBox = {0, 0, 0, 0}; // The bounding box of all glyphs (in font units). - // The names of each glyph, only populated for postscript fonts. - SkTArray<SkString> fGlyphNames; - // The mapping from glyph to Unicode; array indices are glyph ids. SkTDArray<SkUnichar> fGlyphToUnicode; }; diff --git a/src/pdf/SkPDFCanon.h b/src/pdf/SkPDFCanon.h index 99d8ab8a0d..8d0e8f8fed 100644 --- a/src/pdf/SkPDFCanon.h +++ b/src/pdf/SkPDFCanon.h @@ -7,6 +7,8 @@ #ifndef SkPDFCanon_DEFINED #define SkPDFCanon_DEFINED +#include <vector> + #include "SkBitmapKey.h" #include "SkPDFGradientShader.h" #include "SkPDFGraphicState.h" @@ -14,6 +16,7 @@ #include "SkTDArray.h" #include "SkTHash.h" #include "SkTypeface.h" +#include "SkString.h" class SkPDFFont; struct SkAdvancedTypefaceMetrics; @@ -38,6 +41,7 @@ public: SkTHashMap<SkBitmapKey, sk_sp<SkPDFObject>> fPDFBitmapMap; SkTHashMap<uint32_t, std::unique_ptr<SkAdvancedTypefaceMetrics>> fTypefaceMetrics; + SkTHashMap<uint32_t, std::vector<SkString>> fType1GlyphNames; SkTHashMap<uint32_t, sk_sp<SkPDFDict>> fFontDescriptors; SkTHashMap<uint64_t, sk_sp<SkPDFFont>> fFontMap; diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp index c8b576afe6..9ba411495a 100644 --- a/src/pdf/SkPDFFont.cpp +++ b/src/pdf/SkPDFFont.cpp @@ -521,6 +521,7 @@ static sk_sp<SkPDFDict> make_type1_font_descriptor( static void populate_type_1_font(SkPDFDict* font, const SkAdvancedTypefaceMetrics& info, + const std::vector<SkString>& glyphNames, SkTypeface* typeface, SkGlyphID firstGlyphID, SkGlyphID lastGlyphID) { @@ -547,14 +548,12 @@ static void populate_type_1_font(SkPDFDict* font, auto encDiffs = sk_make_sp<SkPDFArray>(); encDiffs->reserve(lastGlyphID - firstGlyphID + 3); encDiffs->appendInt(0); - const SkTArray<SkString>& glyphNames = info.fGlyphNames; - SkASSERT(glyphNames.count() > lastGlyphID); - encDiffs->appendName(glyphNames[0].c_str()); + + SkASSERT(glyphNames.size() > lastGlyphID); const SkString unknown("UNKNOWN"); + encDiffs->appendName(glyphNames[0].isEmpty() ? unknown : glyphNames[0]); for (int gID = firstGlyphID; gID <= lastGlyphID; gID++) { - const bool valid = gID < glyphNames.count() && !glyphNames[gID].isEmpty(); - const SkString& name = valid ? glyphNames[gID] : unknown; - encDiffs->appendName(name); + encDiffs->appendName(glyphNames[gID].isEmpty() ? unknown : glyphNames[gID]); } auto encoding = sk_make_sp<SkPDFDict>("Encoding"); @@ -562,6 +561,10 @@ static void populate_type_1_font(SkPDFDict* font, font->insertObject("Encoding", std::move(encoding)); } +void SkPDFFont::GetType1GlyphNames(const SkTypeface& face, SkString* dst) { + face.getPostScriptGlyphNames(dst); +} + SkPDFType1Font::SkPDFType1Font(SkPDFFont::Info info, const SkAdvancedTypefaceMetrics& metrics, SkPDFCanon* canon) @@ -576,8 +579,16 @@ SkPDFType1Font::SkPDFType1Font(SkPDFFont::Info info, canon->fFontDescriptors.set(fontID, fontDescriptor); } this->insertObjRef("FontDescriptor", std::move(fontDescriptor)); + + std::vector<SkString>* glyphNames = canon->fType1GlyphNames.find(fontID); + if (!glyphNames) { + std::vector<SkString> names(this->typeface()->countGlyphs()); + SkPDFFont::GetType1GlyphNames(*this->typeface(), names.data()); + glyphNames = canon->fType1GlyphNames.set(fontID, std::move(names)); + } + SkASSERT(glyphNames); // TODO(halcanary): subset this (advances and names). - populate_type_1_font(this, metrics, this->typeface(), + populate_type_1_font(this, metrics, *glyphNames, this->typeface(), this->firstGlyphID(), this->lastGlyphID()); } diff --git a/src/pdf/SkPDFFont.h b/src/pdf/SkPDFFont.h index 746f4fab25..9dc4655501 100644 --- a/src/pdf/SkPDFFont.h +++ b/src/pdf/SkPDFFont.h @@ -42,6 +42,7 @@ public: SkAdvancedTypefaceMetrics::FontType getType() const { return fFontType; } static SkAdvancedTypefaceMetrics::FontType FontType(const SkAdvancedTypefaceMetrics&); + static void GetType1GlyphNames(const SkTypeface&, SkString*); static bool IsMultiByte(SkAdvancedTypefaceMetrics::FontType type) { return type == SkAdvancedTypefaceMetrics::kType1CID_Font || diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp index c5e8a65733..f1c1b555f2 100644 --- a/src/ports/SkFontHost_FreeType.cpp +++ b/src/ports/SkFontHost_FreeType.cpp @@ -528,6 +528,18 @@ static void populate_glyph_to_unicode(FT_Face& face, SkTDArray<SkUnichar>* glyph } } +static SkAdvancedTypefaceMetrics::FontType get_font_type(FT_Face face) { + const char* fontType = FT_Get_X11_Font_Format(face); + static struct { const char* s; SkAdvancedTypefaceMetrics::FontType t; } values[] = { + { "Type 1", SkAdvancedTypefaceMetrics::kType1_Font }, + { "CID Type 1", SkAdvancedTypefaceMetrics::kType1CID_Font }, + { "CFF", SkAdvancedTypefaceMetrics::kCFF_Font }, + { "TrueType", SkAdvancedTypefaceMetrics::kTrueType_Font }, + }; + for(const auto& v : values) { if (strcmp(fontType, v.s) == 0) { return v.t; } } + return SkAdvancedTypefaceMetrics::kOther_Font; +} + std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface_FreeType::onGetAdvancedMetrics() const { AutoFTAccess fta(this); FT_Face face = fta.face(); @@ -549,19 +561,7 @@ std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface_FreeType::onGetAdvancedMet info->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag; } - const char* fontType = FT_Get_X11_Font_Format(face); - if (strcmp(fontType, "Type 1") == 0) { - info->fType = SkAdvancedTypefaceMetrics::kType1_Font; - } else if (strcmp(fontType, "CID Type 1") == 0) { - info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font; - } else if (strcmp(fontType, "CFF") == 0) { - info->fType = SkAdvancedTypefaceMetrics::kCFF_Font; - } else if (strcmp(fontType, "TrueType") == 0) { - info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; - } else { - info->fType = SkAdvancedTypefaceMetrics::kOther_Font; - } - + info->fType = get_font_type(face); info->fStyle = (SkAdvancedTypefaceMetrics::StyleFlags)0; if (FT_IS_FIXED_WIDTH(face)) { info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; @@ -605,18 +605,6 @@ std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface_FreeType::onGetAdvancedMet bool perGlyphInfo = FT_IS_SCALABLE(face); - if (perGlyphInfo && info->fType == SkAdvancedTypefaceMetrics::kType1_Font) { - // Postscript fonts may contain more than 255 glyphs, so we end up - // using multiple font descriptions with a glyph ordering. Record - // the name of each glyph. - info->fGlyphNames.reset(face->num_glyphs); - for (int gID = 0; gID < face->num_glyphs; gID++) { - char glyphName[128]; // PS limit for names is 127 bytes. - FT_Get_Glyph_Name(face, gID, glyphName, 128); - info->fGlyphNames[gID].set(glyphName); - } - } - if (perGlyphInfo && info->fType != SkAdvancedTypefaceMetrics::kType1_Font && face->num_charmaps) @@ -627,6 +615,19 @@ std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface_FreeType::onGetAdvancedMet return info; } +void SkTypeface_FreeType::getPostScriptGlyphNames(SkString* dstArray) const { + SkASSERT(dstArray); + AutoFTAccess fta(this); + FT_Face face = fta.face(); + if (face && FT_HAS_GLYPH_NAMES(face)) { + for (int gID = 0; gID < face->num_glyphs; gID++) { + char glyphName[128]; // PS limit for names is 127 bytes. + FT_Get_Glyph_Name(face, gID, glyphName, 128); + dstArray[gID] = glyphName; + } + } +} + /////////////////////////////////////////////////////////////////////////// static bool bothZero(SkScalar a, SkScalar b) { diff --git a/src/ports/SkFontHost_FreeType_common.h b/src/ports/SkFontHost_FreeType_common.h index 3ef864bd78..3aabdde250 100644 --- a/src/ports/SkFontHost_FreeType_common.h +++ b/src/ports/SkFontHost_FreeType_common.h @@ -92,6 +92,7 @@ protected: const SkDescriptor*) const override; void onFilterRec(SkScalerContextRec*) const override; std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; + void getPostScriptGlyphNames(SkString* dstArray) const override; int onGetUPEM() const override; bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count, int32_t adjustments[]) const override; |