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