1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * mozilla.org.
19 : * Portions created by the Initial Developer are Copyright (C) 2005
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Stuart Parmenter <pavlov@pavlov.net>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef NSFONTMETRICS__H__
40 : #define NSFONTMETRICS__H__
41 :
42 : #include "nsCOMPtr.h"
43 : #include "nsCoord.h"
44 : #include "nsFont.h"
45 : #include "gfxFont.h"
46 :
47 : class nsIAtom;
48 : class nsDeviceContext;
49 : class nsRenderingContext;
50 : struct nsBoundingMetrics;
51 :
52 : /**
53 : * Font metrics
54 : *
55 : * This class may be somewhat misnamed. A better name might be
56 : * nsFontList. The style system uses the nsFont struct for various
57 : * font properties, one of which is font-family, which can contain a
58 : * *list* of font names. The nsFont struct is "realized" by asking the
59 : * device context to cough up an nsFontMetrics object, which contains
60 : * a list of real font handles, one for each font mentioned in
61 : * font-family (and for each fallback when we fall off the end of that
62 : * list).
63 : *
64 : * The style system needs to have access to certain metrics, such as
65 : * the em height (for the CSS "em" unit), and we use the first Western
66 : * font's metrics for that purpose. The platform-specific
67 : * implementations are expected to select non-Western fonts that "fit"
68 : * reasonably well with the Western font that is loaded at Init time.
69 : */
70 : class nsFontMetrics
71 : {
72 : public:
73 : nsFontMetrics();
74 : ~nsFontMetrics();
75 :
76 0 : NS_INLINE_DECL_REFCOUNTING(nsFontMetrics)
77 :
78 : /**
79 : * Initialize the font metrics. Call this after creating the font metrics.
80 : * Font metrics you get from the font cache do NOT need to be initialized
81 : *
82 : * @see nsDeviceContext#GetMetricsFor()
83 : */
84 : nsresult Init(const nsFont& aFont, nsIAtom* aLanguage,
85 : nsDeviceContext *aContext,
86 : gfxUserFontSet *aUserFontSet = nsnull);
87 :
88 : /**
89 : * Destroy this font metrics. This breaks the association between
90 : * the font metrics and the device context.
91 : */
92 : void Destroy();
93 :
94 : /**
95 : * Return the font's x-height.
96 : */
97 : nscoord XHeight();
98 :
99 : /**
100 : * Return the font's superscript offset (the distance from the
101 : * baseline to where a superscript's baseline should be placed).
102 : * The value returned will be positive.
103 : */
104 : nscoord SuperscriptOffset();
105 :
106 : /**
107 : * Return the font's subscript offset (the distance from the
108 : * baseline to where a subscript's baseline should be placed).
109 : * The value returned will be positive.
110 : */
111 : nscoord SubscriptOffset();
112 :
113 : /**
114 : * Return the font's strikeout offset (the distance from the
115 : * baseline to where a strikeout should be placed) and size.
116 : * Positive values are above the baseline, negative below.
117 : */
118 : void GetStrikeout(nscoord& aOffset, nscoord& aSize);
119 :
120 : /**
121 : * Return the font's underline offset (the distance from the
122 : * baseline to where a underline should be placed) and size.
123 : * Positive values are above the baseline, negative below.
124 : */
125 : void GetUnderline(nscoord& aOffset, nscoord& aSize);
126 :
127 : /**
128 : * Returns the amount of internal leading for the font.
129 : * This is normally the difference between the max ascent
130 : * and the em ascent.
131 : */
132 : nscoord InternalLeading();
133 :
134 : /**
135 : * Returns the amount of external leading for the font.
136 : * em ascent(?) plus external leading is the font designer's
137 : * recommended line-height for this font.
138 : */
139 : nscoord ExternalLeading();
140 :
141 : /**
142 : * Returns the height of the em square.
143 : * This is em ascent plus em descent.
144 : */
145 : nscoord EmHeight();
146 :
147 : /**
148 : * Returns the ascent part of the em square.
149 : */
150 : nscoord EmAscent();
151 :
152 : /**
153 : * Returns the descent part of the em square.
154 : */
155 : nscoord EmDescent();
156 :
157 : /**
158 : * Returns the height of the bounding box.
159 : * This is max ascent plus max descent.
160 : */
161 : nscoord MaxHeight();
162 :
163 : /**
164 : * Returns the maximum distance characters in this font extend
165 : * above the base line.
166 : */
167 : nscoord MaxAscent();
168 :
169 : /**
170 : * Returns the maximum distance characters in this font extend
171 : * below the base line.
172 : */
173 : nscoord MaxDescent();
174 :
175 : /**
176 : * Returns the maximum character advance for the font.
177 : */
178 : nscoord MaxAdvance();
179 :
180 : /**
181 : * Returns the average character width
182 : */
183 : nscoord AveCharWidth();
184 :
185 : /**
186 : * Returns the often needed width of the space character
187 : */
188 : nscoord SpaceWidth();
189 :
190 : /**
191 : * Returns the font associated with these metrics. The return value
192 : * is only defined after Init() has been called.
193 : */
194 0 : const nsFont &Font() { return mFont; }
195 :
196 : /**
197 : * Returns the language associated with these metrics
198 : */
199 0 : nsIAtom* Language() { return mLanguage; }
200 :
201 : PRInt32 GetMaxStringLength();
202 :
203 : // Get the width for this string. aWidth will be updated with the
204 : // width in points, not twips. Callers must convert it if they
205 : // want it in another format.
206 : nscoord GetWidth(const char* aString, PRUint32 aLength,
207 : nsRenderingContext *aContext);
208 : nscoord GetWidth(const PRUnichar* aString, PRUint32 aLength,
209 : nsRenderingContext *aContext);
210 :
211 : // Draw a string using this font handle on the surface passed in.
212 : void DrawString(const char *aString, PRUint32 aLength,
213 : nscoord aX, nscoord aY,
214 : nsRenderingContext *aContext);
215 : void DrawString(const PRUnichar* aString, PRUint32 aLength,
216 : nscoord aX, nscoord aY,
217 : nsRenderingContext *aContext,
218 : nsRenderingContext *aTextRunConstructionContext);
219 :
220 : nsBoundingMetrics GetBoundingMetrics(const PRUnichar *aString,
221 : PRUint32 aLength,
222 : nsRenderingContext *aContext);
223 :
224 0 : void SetTextRunRTL(bool aIsRTL) { mTextRunRTL = aIsRTL; }
225 0 : bool GetTextRunRTL() { return mTextRunRTL; }
226 :
227 0 : gfxFontGroup* GetThebesFontGroup() { return mFontGroup; }
228 0 : gfxUserFontSet* GetUserFontSet() { return mFontGroup->GetUserFontSet(); }
229 :
230 0 : PRUint32 AppUnitsPerDevPixel() { return mP2A; }
231 :
232 : protected:
233 : const gfxFont::Metrics& GetMetrics() const;
234 :
235 : nsFont mFont;
236 : nsRefPtr<gfxFontGroup> mFontGroup;
237 : nsCOMPtr<nsIAtom> mLanguage;
238 : nsDeviceContext *mDeviceContext;
239 : PRUint32 mP2A;
240 : bool mTextRunRTL;
241 : };
242 :
243 : #endif /* NSFONTMETRICS__H__ */
|