michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef NSFONTMETRICS__H__ michael@0: #define NSFONTMETRICS__H__ michael@0: michael@0: #include // for uint32_t michael@0: #include // for int32_t michael@0: #include "gfxFont.h" // for gfxFont, gfxFontGroup michael@0: #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2 michael@0: #include "nsAutoPtr.h" // for nsRefPtr michael@0: #include "nsCOMPtr.h" // for nsCOMPtr michael@0: #include "nsCoord.h" // for nscoord michael@0: #include "nsError.h" // for nsresult michael@0: #include "nsFont.h" // for nsFont michael@0: #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING michael@0: #include "nscore.h" // for char16_t michael@0: michael@0: class gfxUserFontSet; michael@0: class gfxTextPerfMetrics; michael@0: class nsDeviceContext; michael@0: class nsIAtom; michael@0: class nsRenderingContext; michael@0: struct nsBoundingMetrics; michael@0: michael@0: /** michael@0: * Font metrics michael@0: * michael@0: * This class may be somewhat misnamed. A better name might be michael@0: * nsFontList. The style system uses the nsFont struct for various michael@0: * font properties, one of which is font-family, which can contain a michael@0: * *list* of font names. The nsFont struct is "realized" by asking the michael@0: * device context to cough up an nsFontMetrics object, which contains michael@0: * a list of real font handles, one for each font mentioned in michael@0: * font-family (and for each fallback when we fall off the end of that michael@0: * list). michael@0: * michael@0: * The style system needs to have access to certain metrics, such as michael@0: * the em height (for the CSS "em" unit), and we use the first Western michael@0: * font's metrics for that purpose. The platform-specific michael@0: * implementations are expected to select non-Western fonts that "fit" michael@0: * reasonably well with the Western font that is loaded at Init time. michael@0: */ michael@0: class nsFontMetrics MOZ_FINAL michael@0: { michael@0: public: michael@0: nsFontMetrics(); michael@0: michael@0: NS_INLINE_DECL_REFCOUNTING(nsFontMetrics) michael@0: michael@0: /** michael@0: * Initialize the font metrics. Call this after creating the font metrics. michael@0: * Font metrics you get from the font cache do NOT need to be initialized michael@0: * michael@0: * @see nsDeviceContext#GetMetricsFor() michael@0: */ michael@0: nsresult Init(const nsFont& aFont, nsIAtom* aLanguage, michael@0: nsDeviceContext *aContext, michael@0: gfxUserFontSet *aUserFontSet, michael@0: gfxTextPerfMetrics *aTextPerf); michael@0: michael@0: /** michael@0: * Destroy this font metrics. This breaks the association between michael@0: * the font metrics and the device context. michael@0: */ michael@0: void Destroy(); michael@0: michael@0: /** michael@0: * Return the font's x-height. michael@0: */ michael@0: nscoord XHeight(); michael@0: michael@0: /** michael@0: * Return the font's superscript offset (the distance from the michael@0: * baseline to where a superscript's baseline should be placed). michael@0: * The value returned will be positive. michael@0: */ michael@0: nscoord SuperscriptOffset(); michael@0: michael@0: /** michael@0: * Return the font's subscript offset (the distance from the michael@0: * baseline to where a subscript's baseline should be placed). michael@0: * The value returned will be positive. michael@0: */ michael@0: nscoord SubscriptOffset(); michael@0: michael@0: /** michael@0: * Return the font's strikeout offset (the distance from the michael@0: * baseline to where a strikeout should be placed) and size. michael@0: * Positive values are above the baseline, negative below. michael@0: */ michael@0: void GetStrikeout(nscoord& aOffset, nscoord& aSize); michael@0: michael@0: /** michael@0: * Return the font's underline offset (the distance from the michael@0: * baseline to where a underline should be placed) and size. michael@0: * Positive values are above the baseline, negative below. michael@0: */ michael@0: void GetUnderline(nscoord& aOffset, nscoord& aSize); michael@0: michael@0: /** michael@0: * Returns the amount of internal leading for the font. michael@0: * This is normally the difference between the max ascent michael@0: * and the em ascent. michael@0: */ michael@0: nscoord InternalLeading(); michael@0: michael@0: /** michael@0: * Returns the amount of external leading for the font. michael@0: * em ascent(?) plus external leading is the font designer's michael@0: * recommended line-height for this font. michael@0: */ michael@0: nscoord ExternalLeading(); michael@0: michael@0: /** michael@0: * Returns the height of the em square. michael@0: * This is em ascent plus em descent. michael@0: */ michael@0: nscoord EmHeight(); michael@0: michael@0: /** michael@0: * Returns the ascent part of the em square. michael@0: */ michael@0: nscoord EmAscent(); michael@0: michael@0: /** michael@0: * Returns the descent part of the em square. michael@0: */ michael@0: nscoord EmDescent(); michael@0: michael@0: /** michael@0: * Returns the height of the bounding box. michael@0: * This is max ascent plus max descent. michael@0: */ michael@0: nscoord MaxHeight(); michael@0: michael@0: /** michael@0: * Returns the maximum distance characters in this font extend michael@0: * above the base line. michael@0: */ michael@0: nscoord MaxAscent(); michael@0: michael@0: /** michael@0: * Returns the maximum distance characters in this font extend michael@0: * below the base line. michael@0: */ michael@0: nscoord MaxDescent(); michael@0: michael@0: /** michael@0: * Returns the maximum character advance for the font. michael@0: */ michael@0: nscoord MaxAdvance(); michael@0: michael@0: /** michael@0: * Returns the average character width michael@0: */ michael@0: nscoord AveCharWidth(); michael@0: michael@0: /** michael@0: * Returns the often needed width of the space character michael@0: */ michael@0: nscoord SpaceWidth(); michael@0: michael@0: /** michael@0: * Returns the font associated with these metrics. The return value michael@0: * is only defined after Init() has been called. michael@0: */ michael@0: const nsFont &Font() { return mFont; } michael@0: michael@0: /** michael@0: * Returns the language associated with these metrics michael@0: */ michael@0: nsIAtom* Language() { return mLanguage; } michael@0: michael@0: int32_t GetMaxStringLength(); michael@0: michael@0: // Get the width for this string. aWidth will be updated with the michael@0: // width in points, not twips. Callers must convert it if they michael@0: // want it in another format. michael@0: nscoord GetWidth(const char* aString, uint32_t aLength, michael@0: nsRenderingContext *aContext); michael@0: nscoord GetWidth(const char16_t* aString, uint32_t aLength, michael@0: nsRenderingContext *aContext); michael@0: michael@0: // Draw a string using this font handle on the surface passed in. michael@0: void DrawString(const char *aString, uint32_t aLength, michael@0: nscoord aX, nscoord aY, michael@0: nsRenderingContext *aContext); michael@0: void DrawString(const char16_t* aString, uint32_t aLength, michael@0: nscoord aX, nscoord aY, michael@0: nsRenderingContext *aContext, michael@0: nsRenderingContext *aTextRunConstructionContext); michael@0: michael@0: nsBoundingMetrics GetBoundingMetrics(const char16_t *aString, michael@0: uint32_t aLength, michael@0: nsRenderingContext *aContext); michael@0: michael@0: // Returns the LOOSE_INK_EXTENTS bounds of the text for determing the michael@0: // overflow area of the string. michael@0: nsBoundingMetrics GetInkBoundsForVisualOverflow(const char16_t *aString, michael@0: uint32_t aLength, michael@0: nsRenderingContext *aContext); michael@0: michael@0: void SetTextRunRTL(bool aIsRTL) { mTextRunRTL = aIsRTL; } michael@0: bool GetTextRunRTL() { return mTextRunRTL; } michael@0: michael@0: gfxFontGroup* GetThebesFontGroup() { return mFontGroup; } michael@0: gfxUserFontSet* GetUserFontSet() { return mFontGroup->GetUserFontSet(); } michael@0: michael@0: int32_t AppUnitsPerDevPixel() { return mP2A; } michael@0: michael@0: private: michael@0: // Private destructor, to discourage deletion outside of Release(): michael@0: ~nsFontMetrics(); michael@0: michael@0: const gfxFont::Metrics& GetMetrics() const; michael@0: michael@0: nsFont mFont; michael@0: nsRefPtr mFontGroup; michael@0: nsCOMPtr mLanguage; michael@0: nsDeviceContext *mDeviceContext; michael@0: int32_t mP2A; michael@0: bool mTextRunRTL; michael@0: }; michael@0: michael@0: #endif /* NSFONTMETRICS__H__ */