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