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/. */
6 #ifndef NSFONTMETRICS__H__
7 #define NSFONTMETRICS__H__
9 #include <stdint.h> // for uint32_t
10 #include <sys/types.h> // for int32_t
11 #include "gfxFont.h" // for gfxFont, gfxFontGroup
12 #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
13 #include "nsAutoPtr.h" // for nsRefPtr
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
22 class gfxTextPerfMetrics
;
23 class nsDeviceContext
;
25 class nsRenderingContext
;
26 struct nsBoundingMetrics
;
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
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.
46 class nsFontMetrics MOZ_FINAL
51 NS_INLINE_DECL_REFCOUNTING(nsFontMetrics
)
54 * Initialize the font metrics. Call this after creating the font metrics.
55 * Font metrics you get from the font cache do NOT need to be initialized
57 * @see nsDeviceContext#GetMetricsFor()
59 nsresult
Init(const nsFont
& aFont
, nsIAtom
* aLanguage
,
60 nsDeviceContext
*aContext
,
61 gfxUserFontSet
*aUserFontSet
,
62 gfxTextPerfMetrics
*aTextPerf
);
65 * Destroy this font metrics. This breaks the association between
66 * the font metrics and the device context.
71 * Return the font's x-height.
76 * Return the font's superscript offset (the distance from the
77 * baseline to where a superscript's baseline should be placed).
78 * The value returned will be positive.
80 nscoord
SuperscriptOffset();
83 * Return the font's subscript offset (the distance from the
84 * baseline to where a subscript's baseline should be placed).
85 * The value returned will be positive.
87 nscoord
SubscriptOffset();
90 * Return the font's strikeout offset (the distance from the
91 * baseline to where a strikeout should be placed) and size.
92 * Positive values are above the baseline, negative below.
94 void GetStrikeout(nscoord
& aOffset
, nscoord
& aSize
);
97 * Return the font's underline offset (the distance from the
98 * baseline to where a underline should be placed) and size.
99 * Positive values are above the baseline, negative below.
101 void GetUnderline(nscoord
& aOffset
, nscoord
& aSize
);
104 * Returns the amount of internal leading for the font.
105 * This is normally the difference between the max ascent
108 nscoord
InternalLeading();
111 * Returns the amount of external leading for the font.
112 * em ascent(?) plus external leading is the font designer's
113 * recommended line-height for this font.
115 nscoord
ExternalLeading();
118 * Returns the height of the em square.
119 * This is em ascent plus em descent.
124 * Returns the ascent part of the em square.
129 * Returns the descent part of the em square.
134 * Returns the height of the bounding box.
135 * This is max ascent plus max descent.
140 * Returns the maximum distance characters in this font extend
141 * above the base line.
146 * Returns the maximum distance characters in this font extend
147 * below the base line.
149 nscoord
MaxDescent();
152 * Returns the maximum character advance for the font.
154 nscoord
MaxAdvance();
157 * Returns the average character width
159 nscoord
AveCharWidth();
162 * Returns the often needed width of the space character
164 nscoord
SpaceWidth();
167 * Returns the font associated with these metrics. The return value
168 * is only defined after Init() has been called.
170 const nsFont
&Font() { return mFont
; }
173 * Returns the language associated with these metrics
175 nsIAtom
* Language() { return mLanguage
; }
177 int32_t GetMaxStringLength();
179 // Get the width for this string. aWidth will be updated with the
180 // width in points, not twips. Callers must convert it if they
181 // want it in another format.
182 nscoord
GetWidth(const char* aString
, uint32_t aLength
,
183 nsRenderingContext
*aContext
);
184 nscoord
GetWidth(const char16_t
* aString
, uint32_t aLength
,
185 nsRenderingContext
*aContext
);
187 // Draw a string using this font handle on the surface passed in.
188 void DrawString(const char *aString
, uint32_t aLength
,
189 nscoord aX
, nscoord aY
,
190 nsRenderingContext
*aContext
);
191 void DrawString(const char16_t
* aString
, uint32_t aLength
,
192 nscoord aX
, nscoord aY
,
193 nsRenderingContext
*aContext
,
194 nsRenderingContext
*aTextRunConstructionContext
);
196 nsBoundingMetrics
GetBoundingMetrics(const char16_t
*aString
,
198 nsRenderingContext
*aContext
);
200 // Returns the LOOSE_INK_EXTENTS bounds of the text for determing the
201 // overflow area of the string.
202 nsBoundingMetrics
GetInkBoundsForVisualOverflow(const char16_t
*aString
,
204 nsRenderingContext
*aContext
);
206 void SetTextRunRTL(bool aIsRTL
) { mTextRunRTL
= aIsRTL
; }
207 bool GetTextRunRTL() { return mTextRunRTL
; }
209 gfxFontGroup
* GetThebesFontGroup() { return mFontGroup
; }
210 gfxUserFontSet
* GetUserFontSet() { return mFontGroup
->GetUserFontSet(); }
212 int32_t AppUnitsPerDevPixel() { return mP2A
; }
215 // Private destructor, to discourage deletion outside of Release():
218 const gfxFont::Metrics
& GetMetrics() const;
221 nsRefPtr
<gfxFontGroup
> mFontGroup
;
222 nsCOMPtr
<nsIAtom
> mLanguage
;
223 nsDeviceContext
*mDeviceContext
;
228 #endif /* NSFONTMETRICS__H__ */