Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkAdvancedTypefaceMetrics_DEFINED
     11 #define SkAdvancedTypefaceMetrics_DEFINED
     12 
     13 #include "SkRect.h"
     14 #include "SkRefCnt.h"
     15 #include "SkString.h"
     16 #include "SkTDArray.h"
     17 #include "SkTemplates.h"
     18 
     19 /** \class SkAdvancedTypefaceMetrics
     20 
     21     The SkAdvancedTypefaceMetrics class is used by the PDF backend to correctly
     22     embed typefaces. This class is created and filled in with information by
     23     SkTypeface::getAdvancedTypefaceMetrics.
     24 */
     25 
     26 class SkAdvancedTypefaceMetrics : public SkRefCnt {
     27 public:
     28 
     29     SkAdvancedTypefaceMetrics()
     30         : fType(SkAdvancedTypefaceMetrics::kOther_Font)
     31         , fFlags(SkAdvancedTypefaceMetrics::kEmpty_FontFlag)
     32         , fLastGlyphID(0)
     33         , fEmSize(0)
     34         , fStyle(0)
     35         , fItalicAngle(0)
     36         , fAscent(0)
     37         , fDescent(0)
     38         , fStemV(0)
     39         , fCapHeight(0)
     40         , fBBox(SkIRect::MakeEmpty()) {}
     41 
     42     SkString fFontName;
     43 
     44     enum FontType {
     45         kType1_Font,
     46         kType1CID_Font,
     47         kCFF_Font,
     48         kTrueType_Font,
     49         kOther_Font,
     50     };
     51     // The type of the underlying font program.  This field determines which
     52     // of the following fields are valid.  If it is kOther_Font the per glyph
     53     // information will never be populated.
     54     FontType fType;
     55 
     56     enum FontFlags {
     57         kEmpty_FontFlag          = 0x0,  //!<No flags set
     58         kMultiMaster_FontFlag    = 0x1,  //!<May be true for Type1, CFF, or TrueType fonts.
     59         kNotEmbeddable_FontFlag  = 0x2,  //!<May not be embedded.
     60         kNotSubsettable_FontFlag = 0x4,  //!<May not be subset.
     61     };
     62     // Global font flags.
     63     FontFlags fFlags;
     64 
     65     uint16_t fLastGlyphID; // The last valid glyph ID in the font.
     66     uint16_t fEmSize;  // The size of the em box (defines font units).
     67 
     68     // These enum values match the values used in the PDF file format.
     69     enum StyleFlags {
     70         kFixedPitch_Style  = 0x00001,
     71         kSerif_Style       = 0x00002,
     72         kScript_Style      = 0x00008,
     73         kItalic_Style      = 0x00040,
     74         kAllCaps_Style     = 0x10000,
     75         kSmallCaps_Style   = 0x20000,
     76         kForceBold_Style   = 0x40000
     77     };
     78     uint16_t fStyle;        // Font style characteristics.
     79     int16_t fItalicAngle;   // Counterclockwise degrees from vertical of the
     80                             // dominant vertical stroke for an Italic face.
     81     // The following fields are all in font units.
     82     int16_t fAscent;       // Max height above baseline, not including accents.
     83     int16_t fDescent;      // Max depth below baseline (negative).
     84     int16_t fStemV;        // Thickness of dominant vertical stem.
     85     int16_t fCapHeight;    // Height (from baseline) of top of flat capitals.
     86 
     87     SkIRect fBBox;  // The bounding box of all glyphs (in font units).
     88 
     89     template <typename Data>
     90     struct AdvanceMetric {
     91         enum MetricType {
     92             kDefault,  // Default advance: fAdvance.count = 1
     93             kRange,    // Advances for a range: fAdvance.count = fEndID-fStartID
     94             kRun       // fStartID-fEndID have same advance: fAdvance.count = 1
     95         };
     96         MetricType fType;
     97         uint16_t fStartId;
     98         uint16_t fEndId;
     99         SkTDArray<Data> fAdvance;
    100         SkAutoTDelete<AdvanceMetric<Data> > fNext;
    101     };
    102 
    103     struct VerticalMetric {
    104         int16_t fVerticalAdvance;
    105         int16_t fOriginXDisp;  // Horiz. displacement of the secondary origin.
    106         int16_t fOriginYDisp;  // Vert. displacement of the secondary origin.
    107     };
    108     typedef AdvanceMetric<int16_t> WidthRange;
    109     typedef AdvanceMetric<VerticalMetric> VerticalAdvanceRange;
    110 
    111     // This is indexed by glyph id.
    112     SkAutoTDelete<WidthRange> fGlyphWidths;
    113     // Only used for Vertical CID fonts.
    114     SkAutoTDelete<VerticalAdvanceRange> fVerticalMetrics;
    115 
    116     // The names of each glyph, only populated for postscript fonts.
    117     SkAutoTDelete<SkAutoTArray<SkString> > fGlyphNames;
    118 
    119     // The mapping from glyph to Unicode, only populated if
    120     // kToUnicode_PerGlyphInfo is passed to GetAdvancedTypefaceMetrics.
    121     SkTDArray<SkUnichar> fGlyphToUnicode;
    122 
    123 private:
    124     typedef SkRefCnt INHERITED;
    125 };
    126 
    127 namespace skia_advanced_typeface_metrics_utils {
    128 
    129 template <typename Data>
    130 void resetRange(SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range,
    131                        int startId);
    132 
    133 template <typename Data>
    134 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* appendRange(
    135         SkAutoTDelete<SkAdvancedTypefaceMetrics::AdvanceMetric<Data> >* nextSlot,
    136         int startId);
    137 
    138 template <typename Data>
    139 void finishRange(
    140         SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range,
    141         int endId,
    142         typename SkAdvancedTypefaceMetrics::AdvanceMetric<Data>::MetricType
    143                 type);
    144 
    145 /** Retrieve advance data for glyphs. Used by the PDF backend. It calls
    146     underlying platform dependent API getAdvance to acquire the data.
    147     @param num_glyphs    Total number of glyphs in the given font.
    148     @param glyphIDs      For per-glyph info, specify subset of the font by
    149                          giving glyph ids.  Each integer represents a glyph
    150                          id.  Passing nullptr means all glyphs in the font.
    151     @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if
    152                          glyphIDs is nullptr.
    153 */
    154 template <typename Data, typename FontHandle>
    155 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* getAdvanceData(
    156         FontHandle fontHandle,
    157         int num_glyphs,
    158         const uint32_t* glyphIDs,
    159         uint32_t glyphIDsCount,
    160         bool (*getAdvance)(FontHandle fontHandle, int gId, Data* data));
    161 
    162 } // namespace skia_advanced_typeface_metrics_utils
    163 
    164 #endif
    165