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 NSFONTMETRICS__H__
7 : #define NSFONTMETRICS__H__
8 :
9 : #include <stdint.h> // for uint32_t
10 : #include <sys/types.h> // for int32_t
11 : #include "gfxTextRun.h" // for gfxFont, gfxFontGroup
12 : #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
13 : #include "mozilla/RefPtr.h" // for RefPtr
14 : #include "nsCOMPtr.h" // for nsCOMPtr
15 : #include "nsCoord.h" // for nscoord
16 : #include "nsError.h" // for nsresult
17 : #include "nsFont.h" // for nsFont
18 : #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING
19 : #include "nscore.h" // for char16_t
20 :
21 : class gfxContext;
22 : class gfxUserFontSet;
23 : class gfxTextPerfMetrics;
24 : class nsDeviceContext;
25 : class nsIAtom;
26 : struct nsBoundingMetrics;
27 :
28 : /**
29 : * Font metrics
30 : *
31 : * This class may be somewhat misnamed. A better name might be
32 : * nsFontList. The style system uses the nsFont struct for various
33 : * font properties, one of which is font-family, which can contain a
34 : * *list* of font names. The nsFont struct is "realized" by asking the
35 : * device context to cough up an nsFontMetrics object, which contains
36 : * a list of real font handles, one for each font mentioned in
37 : * font-family (and for each fallback when we fall off the end of that
38 : * list).
39 : *
40 : * The style system needs to have access to certain metrics, such as
41 : * the em height (for the CSS "em" unit), and we use the first Western
42 : * font's metrics for that purpose. The platform-specific
43 : * implementations are expected to select non-Western fonts that "fit"
44 : * reasonably well with the Western font that is loaded at Init time.
45 : */
46 : class nsFontMetrics final
47 : {
48 : public:
49 : typedef gfxTextRun::Range Range;
50 : typedef mozilla::gfx::DrawTarget DrawTarget;
51 :
52 : struct Params
53 : {
54 : nsIAtom* language = nullptr;
55 : bool explicitLanguage = false;
56 : gfxFont::Orientation orientation = gfxFont::eHorizontal;
57 : gfxUserFontSet* userFontSet = nullptr;
58 : gfxTextPerfMetrics* textPerf = nullptr;
59 : };
60 :
61 : nsFontMetrics(const nsFont& aFont, const Params& aParams,
62 : nsDeviceContext *aContext);
63 :
64 : // Used by stylo
65 838 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsFontMetrics)
66 :
67 : /**
68 : * Destroy this font metrics. This breaks the association between
69 : * the font metrics and the device context.
70 : */
71 : void Destroy();
72 :
73 : /**
74 : * Return the font's x-height.
75 : */
76 : nscoord XHeight();
77 :
78 : /**
79 : * Return the font's cap-height.
80 : */
81 : nscoord CapHeight();
82 :
83 : /**
84 : * Return the font's superscript offset (the distance from the
85 : * baseline to where a superscript's baseline should be placed).
86 : * The value returned will be positive.
87 : */
88 : nscoord SuperscriptOffset();
89 :
90 : /**
91 : * Return the font's subscript offset (the distance from the
92 : * baseline to where a subscript's baseline should be placed).
93 : * The value returned will be positive.
94 : */
95 : nscoord SubscriptOffset();
96 :
97 : /**
98 : * Return the font's strikeout offset (the distance from the
99 : * baseline to where a strikeout should be placed) and size.
100 : * Positive values are above the baseline, negative below.
101 : */
102 : void GetStrikeout(nscoord& aOffset, nscoord& aSize);
103 :
104 : /**
105 : * Return the font's underline offset (the distance from the
106 : * baseline to where a underline should be placed) and size.
107 : * Positive values are above the baseline, negative below.
108 : */
109 : void GetUnderline(nscoord& aOffset, nscoord& aSize);
110 :
111 : /**
112 : * Returns the amount of internal leading for the font.
113 : * This is normally the difference between the max ascent
114 : * and the em ascent.
115 : */
116 : nscoord InternalLeading();
117 :
118 : /**
119 : * Returns the amount of external leading for the font.
120 : * em ascent(?) plus external leading is the font designer's
121 : * recommended line-height for this font.
122 : */
123 : nscoord ExternalLeading();
124 :
125 : /**
126 : * Returns the height of the em square.
127 : * This is em ascent plus em descent.
128 : */
129 : nscoord EmHeight();
130 :
131 : /**
132 : * Returns the ascent part of the em square.
133 : */
134 : nscoord EmAscent();
135 :
136 : /**
137 : * Returns the descent part of the em square.
138 : */
139 : nscoord EmDescent();
140 :
141 : /**
142 : * Returns the height of the bounding box.
143 : * This is max ascent plus max descent.
144 : */
145 : nscoord MaxHeight();
146 :
147 : /**
148 : * Returns the maximum distance characters in this font extend
149 : * above the base line.
150 : */
151 : nscoord MaxAscent();
152 :
153 : /**
154 : * Returns the maximum distance characters in this font extend
155 : * below the base line.
156 : */
157 : nscoord MaxDescent();
158 :
159 : /**
160 : * Returns the maximum character advance for the font.
161 : */
162 : nscoord MaxAdvance();
163 :
164 : /**
165 : * Returns the average character width
166 : */
167 : nscoord AveCharWidth();
168 :
169 : /**
170 : * Returns the often needed width of the space character
171 : */
172 : nscoord SpaceWidth();
173 :
174 : /**
175 : * Returns the font associated with these metrics. The return value
176 : * is only defined after Init() has been called.
177 : */
178 409 : const nsFont &Font() const { return mFont; }
179 :
180 : /**
181 : * Returns the language associated with these metrics
182 : */
183 409 : nsIAtom* Language() const { return mLanguage; }
184 :
185 : /**
186 : * Returns the orientation (horizontal/vertical) of these metrics.
187 : */
188 417 : gfxFont::Orientation Orientation() const { return mOrientation; }
189 :
190 : int32_t GetMaxStringLength();
191 :
192 : // Get the width for this string. aWidth will be updated with the
193 : // width in points, not twips. Callers must convert it if they
194 : // want it in another format.
195 : nscoord GetWidth(const char* aString, uint32_t aLength,
196 : DrawTarget* aDrawTarget);
197 : nscoord GetWidth(const char16_t* aString, uint32_t aLength,
198 : DrawTarget* aDrawTarget);
199 :
200 : // Draw a string using this font handle on the surface passed in.
201 : void DrawString(const char *aString, uint32_t aLength,
202 : nscoord aX, nscoord aY,
203 : gfxContext *aContext);
204 : void DrawString(const char16_t* aString, uint32_t aLength,
205 : nscoord aX, nscoord aY,
206 : gfxContext *aContext,
207 : DrawTarget* aTextRunConstructionDrawTarget);
208 :
209 : nsBoundingMetrics GetBoundingMetrics(const char16_t *aString,
210 : uint32_t aLength,
211 : DrawTarget* aDrawTarget);
212 :
213 : // Returns the LOOSE_INK_EXTENTS bounds of the text for determing the
214 : // overflow area of the string.
215 : nsBoundingMetrics GetInkBoundsForVisualOverflow(const char16_t *aString,
216 : uint32_t aLength,
217 : DrawTarget* aDrawTarget);
218 :
219 43 : void SetTextRunRTL(bool aIsRTL) { mTextRunRTL = aIsRTL; }
220 62 : bool GetTextRunRTL() const { return mTextRunRTL; }
221 :
222 42 : void SetVertical(bool aVertical) { mVertical = aVertical; }
223 62 : bool GetVertical() const { return mVertical; }
224 :
225 42 : void SetTextOrientation(uint8_t aTextOrientation)
226 : {
227 42 : mTextOrientation = aTextOrientation;
228 42 : }
229 0 : uint8_t GetTextOrientation() const { return mTextOrientation; }
230 :
231 542 : gfxFontGroup* GetThebesFontGroup() const { return mFontGroup; }
232 409 : gfxUserFontSet* GetUserFontSet() const
233 : {
234 409 : return mFontGroup->GetUserFontSet();
235 : }
236 :
237 62 : int32_t AppUnitsPerDevPixel() const { return mP2A; }
238 :
239 : private:
240 : // Private destructor, to discourage deletion outside of Release():
241 : ~nsFontMetrics();
242 :
243 938 : const gfxFont::Metrics& GetMetrics() const {
244 938 : return GetMetrics(mOrientation);
245 : }
246 :
247 : const gfxFont::Metrics&
248 : GetMetrics(const gfxFont::Orientation aFontOrientation) const;
249 :
250 : nsFont mFont;
251 : RefPtr<gfxFontGroup> mFontGroup;
252 : nsCOMPtr<nsIAtom> mLanguage;
253 : nsDeviceContext* mDeviceContext;
254 : int32_t mP2A;
255 :
256 : // The font orientation (horizontal or vertical) for which these metrics
257 : // have been initialized. This determines which line metrics (ascent and
258 : // descent) they will return.
259 : gfxFont::Orientation mOrientation;
260 :
261 : // These fields may be set by clients to control the behavior of methods
262 : // like GetWidth and DrawString according to the writing mode, direction
263 : // and text-orientation desired.
264 : bool mTextRunRTL;
265 : bool mVertical;
266 : uint8_t mTextOrientation;
267 : };
268 :
269 : #endif /* NSFONTMETRICS__H__ */
|