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
27class SkAdvancedTypefaceMetrics : public SkRefCnt {
28public:
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        kScript_Style      = 0x00008,
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
116private:
117    typedef SkRefCnt INHERITED;
118};
119
120namespace skia_advanced_typeface_metrics_utils {
121
122template <typename Data>
123void resetRange(SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range,
124                       int startId);
125
126template <typename Data>
127SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* appendRange(
128        SkTScopedPtr<SkAdvancedTypefaceMetrics::AdvanceMetric<Data> >* nextSlot,
129        int startId);
130
131template <typename Data>
132void finishRange(
133        SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range,
134        int endId,
135        typename SkAdvancedTypefaceMetrics::AdvanceMetric<Data>::MetricType
136                type);
137
138/** Retrieve advance data for glyphs. Used by the PDF backend. It calls
139    underlying platform dependent API getAdvance to acquire the data.
140    @param num_glyphs    Total number of glyphs in the given font.
141    @param glyphIDs      For per-glyph info, specify subset of the font by
142                         giving glyph ids.  Each integer represents a glyph
143                         id.  Passing NULL means all glyphs in the font.
144    @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if
145                         glyphIDs is NULL.
146*/
147template <typename Data, typename FontHandle>
148SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* getAdvanceData(
149        FontHandle fontHandle,
150        int num_glyphs,
151        const uint32_t* glyphIDs,
152        uint32_t glyphIDsCount,
153        bool (*getAdvance)(FontHandle fontHandle, int gId, Data* data));
154
155} // namespace skia_advanced_typeface_metrics_utils
156
157#endif
158