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