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