Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/thebes/gfxHarfBuzzShaper.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 * This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef GFX_HARFBUZZSHAPER_H
7
#define GFX_HARFBUZZSHAPER_H
8
9
#include "gfxFont.h"
10
11
#include "harfbuzz/hb.h"
12
#include "nsUnicodeProperties.h"
13
#include "mozilla/gfx/2D.h"
14
15
class gfxHarfBuzzShaper : public gfxFontShaper {
16
public:
17
    explicit gfxHarfBuzzShaper(gfxFont *aFont);
18
    virtual ~gfxHarfBuzzShaper();
19
20
    /*
21
     * For HarfBuzz font callback functions, font_data is a ptr to a
22
     * FontCallbackData struct
23
     */
24
    struct FontCallbackData {
25
        gfxHarfBuzzShaper* mShaper;
26
        // initialized to a DrawTarget owned by our caller on every call to
27
        // ShapeText
28
        mozilla::gfx::DrawTarget* MOZ_NON_OWNING_REF mDrawTarget;
29
    };
30
31
    bool Initialize();
32
33
    bool ShapeText(DrawTarget      *aDrawTarget,
34
                   const char16_t *aText,
35
                   uint32_t         aOffset,
36
                   uint32_t         aLength,
37
                   Script           aScript,
38
                   bool             aVertical,
39
                   RoundingFlags    aRounding,
40
                   gfxShapedText   *aShapedText) override;
41
42
    // get a given font table in harfbuzz blob form
43
    hb_blob_t * GetFontTable(hb_tag_t aTag) const;
44
45
    // map unicode character to glyph ID
46
    hb_codepoint_t GetNominalGlyph(hb_codepoint_t unicode) const;
47
    hb_codepoint_t GetVariationGlyph(hb_codepoint_t unicode,
48
                                     hb_codepoint_t variation_selector) const;
49
50
    // get harfbuzz glyph advance, in font design units
51
    hb_position_t GetGlyphHAdvance(hb_codepoint_t glyph) const;
52
53
    hb_position_t GetGlyphVAdvance(hb_codepoint_t glyph) const;
54
55
    void GetGlyphVOrigin(mozilla::gfx::DrawTarget& aDT,
56
                         hb_codepoint_t aGlyph,
57
                         hb_position_t *aX, hb_position_t *aY) const;
58
59
    // get harfbuzz horizontal advance in 16.16 fixed point format.
60
    static hb_position_t
61
    HBGetGlyphHAdvance(hb_font_t *font, void *font_data,
62
                       hb_codepoint_t glyph, void *user_data);
63
64
    // get harfbuzz vertical advance in 16.16 fixed point format.
65
    static hb_position_t
66
    HBGetGlyphVAdvance(hb_font_t *font, void *font_data,
67
                       hb_codepoint_t glyph, void *user_data);
68
69
    static hb_bool_t
70
    HBGetGlyphVOrigin(hb_font_t *font, void *font_data,
71
                      hb_codepoint_t glyph,
72
                      hb_position_t *x, hb_position_t *y,
73
                      void *user_data);
74
75
    hb_position_t GetHKerning(uint16_t aFirstGlyph,
76
                              uint16_t aSecondGlyph) const;
77
78
    hb_bool_t GetGlyphExtents(hb_codepoint_t aGlyph,
79
                              hb_glyph_extents_t *aExtents) const;
80
81
    bool UseVerticalPresentationForms() const
82
0
    {
83
0
        return mUseVerticalPresentationForms;
84
0
    }
85
86
    static hb_script_t
87
0
    GetHBScriptUsedForShaping(Script aScript) {
88
0
        // Decide what harfbuzz script code will be used for shaping
89
0
        hb_script_t hbScript;
90
0
        if (aScript <= Script::INHERITED) {
91
0
            // For unresolved "common" or "inherited" runs,
92
0
            // default to Latin for now.
93
0
            hbScript = HB_SCRIPT_LATIN;
94
0
        } else {
95
0
            hbScript =
96
0
                hb_script_t(mozilla::unicode::GetScriptTagForCode(aScript));
97
0
        }
98
0
        return hbScript;
99
0
    }
100
101
    static hb_codepoint_t
102
    GetVerticalPresentationForm(hb_codepoint_t aUnicode);
103
104
protected:
105
    nsresult SetGlyphsFromRun(gfxShapedText  *aShapedText,
106
                              uint32_t        aOffset,
107
                              uint32_t        aLength,
108
                              const char16_t *aText,
109
                              bool            aVertical,
110
                              RoundingFlags   aRounding);
111
112
    // retrieve glyph positions, applying advance adjustments and attachments
113
    // returns results in appUnits
114
    nscoord GetGlyphPositions(gfxContext *aContext,
115
                              nsTArray<nsPoint>& aPositions,
116
                              uint32_t aAppUnitsPerDevUnit);
117
118
    void InitializeVertical();
119
    bool LoadHmtxTable();
120
121
    struct Glyf { // we only need the bounding-box at the beginning
122
                  // of the glyph record, not the actual outline data
123
        mozilla::AutoSwap_PRInt16 numberOfContours;
124
        mozilla::AutoSwap_PRInt16 xMin;
125
        mozilla::AutoSwap_PRInt16 yMin;
126
        mozilla::AutoSwap_PRInt16 xMax;
127
        mozilla::AutoSwap_PRInt16 yMax;
128
    };
129
130
    const Glyf *FindGlyf(hb_codepoint_t aGlyph, bool *aEmptyGlyf) const;
131
132
    // harfbuzz face object: we acquire a reference from the font entry
133
    // on shaper creation, and release it in our destructor
134
    hb_face_t         *mHBFace;
135
136
    // size-specific font object, owned by the gfxHarfBuzzShaper
137
    hb_font_t         *mHBFont;
138
139
    // harfbuzz buffer for the shaping process
140
    hb_buffer_t       *mBuffer;
141
142
    FontCallbackData   mCallbackData;
143
144
    // Following table references etc are declared "mutable" because the
145
    // harfbuzz callback functions take a const ptr to the shaper, but
146
    // wish to cache tables here to avoid repeatedly looking them up
147
    // in the font.
148
149
    // Old-style TrueType kern table, if we're not doing GPOS kerning
150
    mutable hb_blob_t *mKernTable;
151
152
    // Cached copy of the hmtx table.
153
    mutable hb_blob_t *mHmtxTable;
154
155
    // For vertical fonts, cached vmtx and VORG table, if present.
156
    mutable hb_blob_t *mVmtxTable;
157
    mutable hb_blob_t *mVORGTable;
158
    // And for vertical TrueType (not CFF) fonts that have vmtx,
159
    // we also use loca and glyf to get glyph bounding boxes.
160
    mutable hb_blob_t *mLocaTable;
161
    mutable hb_blob_t *mGlyfTable;
162
163
    // Cached pointer to cmap subtable to be used for char-to-glyph mapping.
164
    // This comes from GetFontTablePtr; if it is non-null, our destructor
165
    // must call ReleaseFontTablePtr to avoid permanently caching the table.
166
    mutable hb_blob_t *mCmapTable;
167
    mutable int32_t    mCmapFormat;
168
    mutable uint32_t   mSubtableOffset;
169
    mutable uint32_t   mUVSTableOffset;
170
171
    // Cached copy of numLongMetrics field from the hhea table,
172
    // for use when looking up glyph metrics; initialized to 0 by the
173
    // constructor so we can tell it hasn't been set yet.
174
    // This is a signed value so that we can use -1 to indicate
175
    // an error (if the hhea table was not available).
176
    mutable int32_t    mNumLongHMetrics;
177
    // Similarly for vhea if it's a vertical font.
178
    mutable int32_t    mNumLongVMetrics;
179
180
    // Default y-coordinate for glyph vertical origin, used if the font
181
    // does not actually have vertical-layout metrics.
182
    mutable gfxFloat mDefaultVOrg;
183
184
    // Whether the font implements GetGlyph, or we should read tables
185
    // directly
186
    bool mUseFontGetGlyph;
187
    // Whether the font implements GetGlyphWidth, or we should read tables
188
    // directly to get ideal widths
189
    bool mUseFontGlyphWidths;
190
191
    bool mInitialized;
192
    bool mVerticalInitialized;
193
194
    // Whether to use vertical presentation forms for CJK characters
195
    // when available (only set if the 'vert' feature is not available).
196
    bool mUseVerticalPresentationForms;
197
198
    // these are set from the FindGlyf callback on first use of the glyf data
199
    mutable bool mLoadedLocaGlyf;
200
    mutable bool mLocaLongOffsets;
201
};
202
203
#endif /* GFX_HARFBUZZSHAPER_H */