/* * Copyright 2011 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SkAdvancedTypefaceMetrics_DEFINED #define SkAdvancedTypefaceMetrics_DEFINED #include "SkRect.h" #include "SkRefCnt.h" #include "SkString.h" #include "SkTDArray.h" #include "SkTemplates.h" // Whatever std::unique_ptr Clank's using doesn't seem to work with AdvanceMetric's // style of forward-declaration. Probably just a bug in an old libc++ / libstdc++. // For now, hack around it with our own smart pointer. It'd be nice to clean up. template class SkHackyAutoTDelete : SkNoncopyable { public: explicit SkHackyAutoTDelete(T* ptr = nullptr) : fPtr(ptr) {} ~SkHackyAutoTDelete() { delete fPtr; } T* get() const { return fPtr; } T* operator->() const { return fPtr; } void reset(T* ptr = nullptr) { if (ptr != fPtr) { delete fPtr; fPtr = ptr; } } T* release() { T* ptr = fPtr; fPtr = nullptr; return ptr; } private: T* fPtr; }; /** \class SkAdvancedTypefaceMetrics The SkAdvancedTypefaceMetrics class is used by the PDF backend to correctly embed typefaces. This class is created and filled in with information by SkTypeface::getAdvancedTypefaceMetrics. */ class SkAdvancedTypefaceMetrics : public SkRefCnt { public: SkAdvancedTypefaceMetrics() : fType(SkAdvancedTypefaceMetrics::kOther_Font) , fFlags(SkAdvancedTypefaceMetrics::kEmpty_FontFlag) , fLastGlyphID(0) , fEmSize(0) , fStyle(0) , fItalicAngle(0) , fAscent(0) , fDescent(0) , fStemV(0) , fCapHeight(0) , fBBox(SkIRect::MakeEmpty()) {} SkString fFontName; enum FontType { kType1_Font, kType1CID_Font, kCFF_Font, kTrueType_Font, kOther_Font, }; // The type of the underlying font program. This field determines which // of the following fields are valid. If it is kOther_Font the per glyph // information will never be populated. FontType fType; enum FontFlags { kEmpty_FontFlag = 0x0, //! struct AdvanceMetric { enum MetricType { kDefault, // Default advance: fAdvance.count = 1 kRange, // Advances for a range: fAdvance.count = fEndID-fStartID kRun // fStartID-fEndID have same advance: fAdvance.count = 1 }; MetricType fType; uint16_t fStartId; uint16_t fEndId; SkTDArray fAdvance; SkHackyAutoTDelete > fNext; }; struct VerticalMetric { int16_t fVerticalAdvance; int16_t fOriginXDisp; // Horiz. displacement of the secondary origin. int16_t fOriginYDisp; // Vert. displacement of the secondary origin. }; typedef AdvanceMetric WidthRange; typedef AdvanceMetric VerticalAdvanceRange; // This is indexed by glyph id. SkAutoTDelete fGlyphWidths; // Only used for Vertical CID fonts. SkAutoTDelete fVerticalMetrics; // The names of each glyph, only populated for postscript fonts. SkAutoTDelete > fGlyphNames; // The mapping from glyph to Unicode, only populated if // kToUnicode_PerGlyphInfo is passed to GetAdvancedTypefaceMetrics. SkTDArray fGlyphToUnicode; private: typedef SkRefCnt INHERITED; }; namespace skia_advanced_typeface_metrics_utils { template void resetRange(SkAdvancedTypefaceMetrics::AdvanceMetric* range, int startId); template class AutoTDelete> SkAdvancedTypefaceMetrics::AdvanceMetric* appendRange( AutoTDelete >* nextSlot, int startId); template void finishRange( SkAdvancedTypefaceMetrics::AdvanceMetric* range, int endId, typename SkAdvancedTypefaceMetrics::AdvanceMetric::MetricType type); /** Retrieve advance data for glyphs. Used by the PDF backend. It calls underlying platform dependent API getAdvance to acquire the data. @param num_glyphs Total number of glyphs in the given font. @param glyphIDs For per-glyph info, specify subset of the font by giving glyph ids. Each integer represents a glyph id. Passing nullptr means all glyphs in the font. @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if glyphIDs is nullptr. */ template SkAdvancedTypefaceMetrics::AdvanceMetric* getAdvanceData( FontHandle fontHandle, int num_glyphs, const uint32_t* glyphIDs, uint32_t glyphIDsCount, bool (*getAdvance)(FontHandle fontHandle, int gId, Data* data)); } // namespace skia_advanced_typeface_metrics_utils #endif