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