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     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     template <typename Data>
     78     struct AdvanceMetric {
     79         enum MetricType {
     80             kDefault,  // Default advance: fAdvance.count = 1
     81             kRange,    // Advances for a range: fAdvance.count = fEndID-fStartID
     82             kRun       // fStartID-fEndID have same advance: fAdvance.count = 1
     83         };
     84         MetricType fType;
     85         uint16_t fStartId;
     86         uint16_t fEndId;
     87         SkTDArray<Data> fAdvance;
     88         SkAutoTDelete<AdvanceMetric<Data> > fNext;
     89     };
     90 
     91     struct VerticalMetric {
     92         int16_t fVerticalAdvance;
     93         int16_t fOriginXDisp;  // Horiz. displacement of the secondary origin.
     94         int16_t fOriginYDisp;  // Vert. displacement of the secondary origin.
     95     };
     96     typedef AdvanceMetric<int16_t> WidthRange;
     97     typedef AdvanceMetric<VerticalMetric> VerticalAdvanceRange;
     98 
     99     // This is indexed by glyph id.
    100     SkAutoTDelete<WidthRange> fGlyphWidths;
    101     // Only used for Vertical CID fonts.
    102     SkAutoTDelete<VerticalAdvanceRange> fVerticalMetrics;
    103 
    104     // The names of each glyph, only populated for postscript fonts.
    105     SkAutoTDelete<SkAutoTArray<SkString> > fGlyphNames;
    106 
    107     // The mapping from glyph to Unicode, only populated if
    108     // kToUnicode_PerGlyphInfo is passed to GetAdvancedTypefaceMetrics.
    109     SkTDArray<SkUnichar> fGlyphToUnicode;
    110 
    111 private:
    112     typedef SkRefCnt INHERITED;
    113 };
    114 
    115 namespace skia_advanced_typeface_metrics_utils {
    116 
    117 template <typename Data>
    118 void resetRange(SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range,
    119                        int startId);
    120 
    121 template <typename Data>
    122 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* appendRange(
    123         SkAutoTDelete<SkAdvancedTypefaceMetrics::AdvanceMetric<Data> >* nextSlot,
    124         int startId);
    125 
    126 template <typename Data>
    127 void finishRange(
    128         SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range,
    129         int endId,
    130         typename SkAdvancedTypefaceMetrics::AdvanceMetric<Data>::MetricType
    131                 type);
    132 
    133 /** Retrieve advance data for glyphs. Used by the PDF backend. It calls
    134     underlying platform dependent API getAdvance to acquire the data.
    135     @param num_glyphs    Total number of glyphs in the given font.
    136     @param glyphIDs      For per-glyph info, specify subset of the font by
    137                          giving glyph ids.  Each integer represents a glyph
    138                          id.  Passing NULL means all glyphs in the font.
    139     @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if
    140                          glyphIDs is NULL.
    141 */
    142 template <typename Data, typename FontHandle>
    143 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* getAdvanceData(
    144         FontHandle fontHandle,
    145         int num_glyphs,
    146         const uint32_t* glyphIDs,
    147         uint32_t glyphIDsCount,
    148         bool (*getAdvance)(FontHandle fontHandle, int gId, Data* data));
    149 
    150 } // namespace skia_advanced_typeface_metrics_utils
    151 
    152 #endif
    153