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