diff --git a/gfx/src/Makefile.in b/gfx/src/Makefile.in index 5bac15113ab..7e8f8ecda1d 100644 --- a/gfx/src/Makefile.in +++ b/gfx/src/Makefile.in @@ -73,7 +73,7 @@ EXPORTS = \ nsMargin.h \ nsTransform2D.h \ nsRenderingContext.h \ - nsIFontMetrics.h \ + nsFontMetrics.h \ nsIDeviceContext.h \ nsGfxCIID.h \ nsIRegion.h \ @@ -100,7 +100,7 @@ CPPSRCS = \ nsThebesRegion.cpp \ nsThebesGfxFactory.cpp \ nsRenderingContext.cpp \ - nsThebesFontMetrics.cpp \ + nsFontMetrics.cpp \ nsThebesFontEnumerator.cpp \ $(NULL) diff --git a/gfx/src/nsBoundingMetrics.h b/gfx/src/nsBoundingMetrics.h index a90827623f9..fb733d4a027 100644 --- a/gfx/src/nsBoundingMetrics.h +++ b/gfx/src/nsBoundingMetrics.h @@ -43,7 +43,7 @@ /* Struct used for accurate measurements of a string, in order to * allow precise positioning when processing MathML. This is in its * own header file because some very-widely-included headers need it - * but not the rest of nsIFontMetrics, or vice versa. + * but not the rest of nsFontMetrics, or vice versa. */ #ifdef MOZ_MATHML diff --git a/gfx/src/nsThebesFontMetrics.cpp b/gfx/src/nsFontMetrics.cpp similarity index 63% rename from gfx/src/nsThebesFontMetrics.cpp rename to gfx/src/nsFontMetrics.cpp index de033ccdb85..a8939196b90 100644 --- a/gfx/src/nsThebesFontMetrics.cpp +++ b/gfx/src/nsFontMetrics.cpp @@ -36,29 +36,51 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsThebesFontMetrics.h" +#include "nsFontMetrics.h" #include "nsBoundingMetrics.h" +#include "nsRenderingContext.h" #include "nsThebesDeviceContext.h" -#include "nsFont.h" -#include "nsString.h" -#include +class AutoTextRun { +public: + AutoTextRun(nsFontMetrics* aMetrics, nsRenderingContext* aRC, + const char* aString, PRInt32 aLength) { + mTextRun = gfxTextRunCache::MakeTextRun( + reinterpret_cast(aString), aLength, + aMetrics->GetThebesFontGroup(), aRC->ThebesContext(), + aMetrics->AppUnitsPerDevPixel(), + ComputeFlags(aMetrics)); + } + AutoTextRun(nsFontMetrics* aMetrics, nsRenderingContext* aRC, + const PRUnichar* aString, PRInt32 aLength) { + mTextRun = gfxTextRunCache::MakeTextRun( + aString, aLength, aMetrics->GetThebesFontGroup(), + aRC->ThebesContext(), + aMetrics->AppUnitsPerDevPixel(), + ComputeFlags(aMetrics)); + } + gfxTextRun* operator->() { return mTextRun.get(); } + gfxTextRun* get() { return mTextRun.get(); } -#include "gfxTextRunCache.h" -#include "gfxPlatform.h" -#include "gfxUserFontSet.h" +private: + gfxTextRunCache::AutoTextRun mTextRun; -NS_IMPL_ISUPPORTS1(nsThebesFontMetrics, nsIFontMetrics) + static PRUint32 ComputeFlags(nsFontMetrics* aMetrics) { + PRUint32 flags = 0; + if (aMetrics->GetRightToLeftTextRunMode()) { + flags |= gfxTextRunFactory::TEXT_IS_RTL; + } + return flags; + } +}; -#include - -nsThebesFontMetrics::nsThebesFontMetrics() +nsFontMetrics::nsFontMetrics() { mFontStyle = nsnull; mFontGroup = nsnull; } -nsThebesFontMetrics::~nsThebesFontMetrics() +nsFontMetrics::~nsFontMetrics() { if (mDeviceContext) mDeviceContext->FontMetricsDeleted(this); @@ -66,10 +88,10 @@ nsThebesFontMetrics::~nsThebesFontMetrics() //delete mFontGroup; } -NS_IMETHODIMP -nsThebesFontMetrics::Init(const nsFont& aFont, nsIAtom* aLanguage, - nsIDeviceContext *aContext, - gfxUserFontSet *aUserFontSet) +nsresult +nsFontMetrics::Init(const nsFont& aFont, nsIAtom* aLanguage, + nsIDeviceContext *aContext, + gfxUserFontSet *aUserFontSet) { mFont = aFont; mLanguage = aLanguage; @@ -89,16 +111,16 @@ nsThebesFontMetrics::Init(const nsFont& aFont, nsIAtom* aLanguage, aFont.languageOverride); mFontGroup = - gfxPlatform::GetPlatform()->CreateFontGroup(aFont.name, mFontStyle, + gfxPlatform::GetPlatform()->CreateFontGroup(aFont.name, mFontStyle, aUserFontSet); - if (mFontGroup->FontListLength() < 1) + if (mFontGroup->FontListLength() < 1) return NS_ERROR_UNEXPECTED; return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::Destroy() +nsresult +nsFontMetrics::Destroy() { mDeviceContext = nsnull; return NS_OK; @@ -108,42 +130,42 @@ nsThebesFontMetrics::Destroy() #define ROUND_TO_TWIPS(x) (nscoord)floor(((x) * mP2A) + 0.5) #define CEIL_TO_TWIPS(x) (nscoord)NS_ceil((x) * mP2A) -const gfxFont::Metrics& nsThebesFontMetrics::GetMetrics() const +const gfxFont::Metrics& nsFontMetrics::GetMetrics() const { return mFontGroup->GetFontAt(0)->GetMetrics(); } -NS_IMETHODIMP -nsThebesFontMetrics::GetXHeight(nscoord& aResult) +nsresult +nsFontMetrics::GetXHeight(nscoord& aResult) { aResult = ROUND_TO_TWIPS(GetMetrics().xHeight); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetSuperscriptOffset(nscoord& aResult) +nsresult +nsFontMetrics::GetSuperscriptOffset(nscoord& aResult) { aResult = ROUND_TO_TWIPS(GetMetrics().superscriptOffset); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetSubscriptOffset(nscoord& aResult) +nsresult +nsFontMetrics::GetSubscriptOffset(nscoord& aResult) { aResult = ROUND_TO_TWIPS(GetMetrics().subscriptOffset); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetStrikeout(nscoord& aOffset, nscoord& aSize) +nsresult +nsFontMetrics::GetStrikeout(nscoord& aOffset, nscoord& aSize) { aOffset = ROUND_TO_TWIPS(GetMetrics().strikeoutOffset); aSize = ROUND_TO_TWIPS(GetMetrics().strikeoutSize); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetUnderline(nscoord& aOffset, nscoord& aSize) +nsresult +nsFontMetrics::GetUnderline(nscoord& aOffset, nscoord& aSize) { aOffset = ROUND_TO_TWIPS(mFontGroup->GetUnderlineOffset()); aSize = ROUND_TO_TWIPS(GetMetrics().underlineSize); @@ -170,109 +192,109 @@ static gfxFloat ComputeMaxAscent(const gfxFont::Metrics& aMetrics) return NS_floor(aMetrics.maxAscent + 0.5); } -NS_IMETHODIMP -nsThebesFontMetrics::GetHeight(nscoord &aHeight) +nsresult +nsFontMetrics::GetHeight(nscoord &aHeight) { aHeight = CEIL_TO_TWIPS(ComputeMaxAscent(GetMetrics())) + CEIL_TO_TWIPS(ComputeMaxDescent(GetMetrics(), mFontGroup)); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetInternalLeading(nscoord &aLeading) +nsresult +nsFontMetrics::GetInternalLeading(nscoord &aLeading) { aLeading = ROUND_TO_TWIPS(GetMetrics().internalLeading); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetExternalLeading(nscoord &aLeading) +nsresult +nsFontMetrics::GetExternalLeading(nscoord &aLeading) { aLeading = ROUND_TO_TWIPS(GetMetrics().externalLeading); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetEmHeight(nscoord &aHeight) +nsresult +nsFontMetrics::GetEmHeight(nscoord &aHeight) { aHeight = ROUND_TO_TWIPS(GetMetrics().emHeight); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetEmAscent(nscoord &aAscent) +nsresult +nsFontMetrics::GetEmAscent(nscoord &aAscent) { aAscent = ROUND_TO_TWIPS(GetMetrics().emAscent); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetEmDescent(nscoord &aDescent) +nsresult +nsFontMetrics::GetEmDescent(nscoord &aDescent) { aDescent = ROUND_TO_TWIPS(GetMetrics().emDescent); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetMaxHeight(nscoord &aHeight) +nsresult +nsFontMetrics::GetMaxHeight(nscoord &aHeight) { aHeight = CEIL_TO_TWIPS(ComputeMaxAscent(GetMetrics())) + CEIL_TO_TWIPS(ComputeMaxDescent(GetMetrics(), mFontGroup)); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetMaxAscent(nscoord &aAscent) +nsresult +nsFontMetrics::GetMaxAscent(nscoord &aAscent) { aAscent = CEIL_TO_TWIPS(ComputeMaxAscent(GetMetrics())); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetMaxDescent(nscoord &aDescent) +nsresult +nsFontMetrics::GetMaxDescent(nscoord &aDescent) { aDescent = CEIL_TO_TWIPS(ComputeMaxDescent(GetMetrics(), mFontGroup)); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetMaxAdvance(nscoord &aAdvance) +nsresult +nsFontMetrics::GetMaxAdvance(nscoord &aAdvance) { aAdvance = CEIL_TO_TWIPS(GetMetrics().maxAdvance); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetLanguage(nsIAtom** aLanguage) +nsresult +nsFontMetrics::GetLanguage(nsIAtom** aLanguage) { *aLanguage = mLanguage; NS_IF_ADDREF(*aLanguage); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetFontHandle(nsFontHandle &aHandle) +nsresult +nsFontMetrics::GetFontHandle(nsFontHandle &aHandle) { return NS_ERROR_NOT_IMPLEMENTED; } -NS_IMETHODIMP -nsThebesFontMetrics::GetAveCharWidth(nscoord& aAveCharWidth) +nsresult +nsFontMetrics::GetAveCharWidth(nscoord& aAveCharWidth) { // Use CEIL instead of ROUND for consistency with GetMaxAdvance aAveCharWidth = CEIL_TO_TWIPS(GetMetrics().aveCharWidth); return NS_OK; } -NS_IMETHODIMP -nsThebesFontMetrics::GetSpaceWidth(nscoord& aSpaceCharWidth) +nsresult +nsFontMetrics::GetSpaceWidth(nscoord& aSpaceCharWidth) { aSpaceCharWidth = CEIL_TO_TWIPS(GetMetrics().spaceWidth); return NS_OK; } PRInt32 -nsThebesFontMetrics::GetMaxStringLength() +nsFontMetrics::GetMaxStringLength() { const gfxFont::Metrics& m = GetMetrics(); const double x = 32767.0 / m.maxAdvance; @@ -296,9 +318,9 @@ public: } }; -nsresult -nsThebesFontMetrics::GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth, - nsRenderingContext *aContext) +nsresult +nsFontMetrics::GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth, + nsRenderingContext *aContext) { if (aLength == 0) { aWidth = 0; @@ -320,9 +342,9 @@ nsThebesFontMetrics::GetWidth(const char* aString, PRUint32 aLength, nscoord& aW } nsresult -nsThebesFontMetrics::GetWidth(const PRUnichar* aString, PRUint32 aLength, - nscoord& aWidth, PRInt32 *aFontID, - nsRenderingContext *aContext) +nsFontMetrics::GetWidth(const PRUnichar* aString, PRUint32 aLength, + nscoord& aWidth, PRInt32 *aFontID, + nsRenderingContext *aContext) { if (aLength == 0) { aWidth = 0; @@ -345,47 +367,47 @@ nsThebesFontMetrics::GetWidth(const PRUnichar* aString, PRUint32 aLength, // Get the text dimensions for this string nsresult -nsThebesFontMetrics::GetTextDimensions(const PRUnichar* aString, - PRUint32 aLength, - nsTextDimensions& aDimensions, - PRInt32* aFontID) +nsFontMetrics::GetTextDimensions(const PRUnichar* aString, + PRUint32 aLength, + nsTextDimensions& aDimensions, + PRInt32* aFontID) { return NS_OK; } nsresult -nsThebesFontMetrics::GetTextDimensions(const char* aString, - PRInt32 aLength, - PRInt32 aAvailWidth, - PRInt32* aBreaks, - PRInt32 aNumBreaks, - nsTextDimensions& aDimensions, - PRInt32& aNumCharsFit, - nsTextDimensions& aLastWordDimensions, - PRInt32* aFontID) +nsFontMetrics::GetTextDimensions(const char* aString, + PRInt32 aLength, + PRInt32 aAvailWidth, + PRInt32* aBreaks, + PRInt32 aNumBreaks, + nsTextDimensions& aDimensions, + PRInt32& aNumCharsFit, + nsTextDimensions& aLastWordDimensions, + PRInt32* aFontID) { return NS_OK; } nsresult -nsThebesFontMetrics::GetTextDimensions(const PRUnichar* aString, - PRInt32 aLength, - PRInt32 aAvailWidth, - PRInt32* aBreaks, - PRInt32 aNumBreaks, - nsTextDimensions& aDimensions, - PRInt32& aNumCharsFit, - nsTextDimensions& aLastWordDimensions, - PRInt32* aFontID) +nsFontMetrics::GetTextDimensions(const PRUnichar* aString, + PRInt32 aLength, + PRInt32 aAvailWidth, + PRInt32* aBreaks, + PRInt32 aNumBreaks, + nsTextDimensions& aDimensions, + PRInt32& aNumCharsFit, + nsTextDimensions& aLastWordDimensions, + PRInt32* aFontID) { return NS_OK; } -// Draw a string using this font handle on the surface passed in. +// Draw a string using this font handle on the surface passed in. nsresult -nsThebesFontMetrics::DrawString(const char *aString, PRUint32 aLength, - nscoord aX, nscoord aY, - const nscoord* aSpacing, - nsRenderingContext *aContext) +nsFontMetrics::DrawString(const char *aString, PRUint32 aLength, + nscoord aX, nscoord aY, + const nscoord* aSpacing, + nsRenderingContext *aContext) { if (aLength == 0) return NS_OK; @@ -405,10 +427,10 @@ nsThebesFontMetrics::DrawString(const char *aString, PRUint32 aLength, } nsresult -nsThebesFontMetrics::DrawString(const PRUnichar* aString, PRUint32 aLength, - nscoord aX, nscoord aY, - nsRenderingContext *aContext, - nsRenderingContext *aTextRunConstructionContext) +nsFontMetrics::DrawString(const PRUnichar* aString, PRUint32 aLength, + nscoord aX, nscoord aY, + nsRenderingContext *aContext, + nsRenderingContext *aTextRunConstructionContext) { if (aLength == 0) return NS_OK; @@ -429,16 +451,18 @@ nsThebesFontMetrics::DrawString(const PRUnichar* aString, PRUint32 aLength, #ifdef MOZ_MATHML static void -GetTextRunBoundingMetrics(gfxTextRun *aTextRun, PRUint32 aStart, PRUint32 aLength, +GetTextRunBoundingMetrics(gfxTextRun *aTextRun, + PRUint32 aStart, PRUint32 aLength, nsRenderingContext *aContext, nsBoundingMetrics &aBoundingMetrics) { StubPropertyProvider provider; gfxTextRun::Metrics theMetrics = - aTextRun->MeasureText(aStart, aLength, gfxFont::TIGHT_HINTED_OUTLINE_EXTENTS, + aTextRun->MeasureText(aStart, aLength, + gfxFont::TIGHT_HINTED_OUTLINE_EXTENTS, aContext->ThebesContext(), &provider); - // note that TIGHT_HINTED_OUTLINE_EXTENTS can be expensive (on Windows) - // but this is only used for MathML positioning so it's not critical + // note that TIGHT_HINTED_OUTLINE_EXTENTS can be expensive (on Windows) + // but this is only used for MathML positioning so it's not critical aBoundingMetrics.leftBearing = NSToCoordFloor(theMetrics.mBoundingBox.X()); aBoundingMetrics.rightBearing = NSToCoordCeil(theMetrics.mBoundingBox.XMost()); @@ -448,9 +472,9 @@ GetTextRunBoundingMetrics(gfxTextRun *aTextRun, PRUint32 aStart, PRUint32 aLengt } nsresult -nsThebesFontMetrics::GetBoundingMetrics(const char *aString, PRUint32 aLength, - nsRenderingContext *aContext, - nsBoundingMetrics &aBoundingMetrics) +nsFontMetrics::GetBoundingMetrics(const char *aString, PRUint32 aLength, + nsRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics) { if (aLength == 0) { aBoundingMetrics = nsBoundingMetrics(); @@ -461,14 +485,15 @@ nsThebesFontMetrics::GetBoundingMetrics(const char *aString, PRUint32 aLength, if (!textRun.get()) return NS_ERROR_FAILURE; - GetTextRunBoundingMetrics(textRun.get(), 0, aLength, aContext, aBoundingMetrics); + GetTextRunBoundingMetrics(textRun.get(), 0, aLength, aContext, + aBoundingMetrics); return NS_OK; } nsresult -nsThebesFontMetrics::GetBoundingMetrics(const PRUnichar *aString, PRUint32 aLength, - nsRenderingContext *aContext, - nsBoundingMetrics &aBoundingMetrics) +nsFontMetrics::GetBoundingMetrics(const PRUnichar *aString, PRUint32 aLength, + nsRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics) { if (aLength == 0) { aBoundingMetrics = nsBoundingMetrics(); @@ -479,7 +504,8 @@ nsThebesFontMetrics::GetBoundingMetrics(const PRUnichar *aString, PRUint32 aLeng if (!textRun.get()) return NS_ERROR_FAILURE; - GetTextRunBoundingMetrics(textRun.get(), 0, aLength, aContext, aBoundingMetrics); + GetTextRunBoundingMetrics(textRun.get(), 0, aLength, aContext, + aBoundingMetrics); return NS_OK; } @@ -487,7 +513,7 @@ nsThebesFontMetrics::GetBoundingMetrics(const PRUnichar *aString, PRUint32 aLeng // Set the direction of the text rendering nsresult -nsThebesFontMetrics::SetRightToLeftText(PRBool aIsRTL) +nsFontMetrics::SetRightToLeftText(PRBool aIsRTL) { mIsRightToLeft = aIsRTL; return NS_OK; @@ -495,13 +521,13 @@ nsThebesFontMetrics::SetRightToLeftText(PRBool aIsRTL) // Set the direction of the text rendering PRBool -nsThebesFontMetrics::GetRightToLeftText() +nsFontMetrics::GetRightToLeftText() { return mIsRightToLeft; } -/* virtual */ gfxUserFontSet* -nsThebesFontMetrics::GetUserFontSet() +gfxUserFontSet* +nsFontMetrics::GetUserFontSet() { return mFontGroup->GetUserFontSet(); } diff --git a/gfx/src/nsFontMetrics.h b/gfx/src/nsFontMetrics.h new file mode 100644 index 00000000000..31710178545 --- /dev/null +++ b/gfx/src/nsFontMetrics.h @@ -0,0 +1,298 @@ +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * mozilla.org. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Stuart Parmenter + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef NSFONTMETRICS__H__ +#define NSFONTMETRICS__H__ + +#include "nsCOMPtr.h" +#include "nsCoord.h" +#include "nsFont.h" +#include "gfxFont.h" +#include "gfxTextRunCache.h" + +class gfxFontGroup; +class gfxUserFontSet; +class nsIAtom; +class nsIDeviceContext; +class nsRenderingContext; +class nsString; +class nsThebesDeviceContext; +struct nsBoundingMetrics; +struct nsTextDimensions; + +/** + * A native font handle + */ +typedef void* nsFontHandle; + +/** + * Font metrics + * + * This class may be somewhat misnamed. A better name might be + * nsFontList. The style system uses the nsFont struct for various + * font properties, one of which is font-family, which can contain a + * *list* of font names. The nsFont struct is "realized" by asking the + * device context to cough up an nsFontMetrics object, which contains + * a list of real font handles, one for each font mentioned in + * font-family (and for each fallback when we fall off the end of that + * list). + * + * The style system needs to have access to certain metrics, such as + * the em height (for the CSS "em" unit), and we use the first Western + * font's metrics for that purpose. The platform-specific + * implementations are expected to select non-Western fonts that "fit" + * reasonably well with the Western font that is loaded at Init time. + */ +class nsFontMetrics +{ +public: + nsFontMetrics(); + ~nsFontMetrics(); + + NS_INLINE_DECL_REFCOUNTING(nsFontMetrics) + + /** + * Initialize the font metrics. Call this after creating the font metrics. + * Font metrics you get from the font cache do NOT need to be initialized + * + * @see nsIDeviceContext#GetMetricsFor() + */ + nsresult Init(const nsFont& aFont, nsIAtom* aLanguage, + nsIDeviceContext *aContext, + gfxUserFontSet *aUserFontSet = nsnull); + /** + * Destroy this font metrics. This breaks the association between + * the font metrics and the device context. + */ + nsresult Destroy(); + /** + * Return the font's xheight property, scaled into app-units. + */ + nsresult GetXHeight(nscoord& aResult); + /** + * Return the font's superscript offset (the distance from the + * baseline to where a superscript's baseline should be placed). + * The value returned will be positive. + */ + nsresult GetSuperscriptOffset(nscoord& aResult); + /** + * Return the font's subscript offset (the distance from the + * baseline to where a subscript's baseline should be placed). The + * value returned will be a positive value. + */ + nsresult GetSubscriptOffset(nscoord& aResult); + /** + * Return the font's strikeout offset (the distance from the + * baseline to where a strikeout should be placed) and size. + * Positive values are above the baseline, negative below. + */ + nsresult GetStrikeout(nscoord& aOffset, nscoord& aSize); + /** + * Return the font's underline offset (the distance from the + * baseline to where a underline should be placed) and size. + * Positive values are above the baseline, negative below. + */ + nsresult GetUnderline(nscoord& aOffset, nscoord& aSize); + /** + * Returns the height (in app units) of the font. This is ascent plus descent + * plus any internal leading + * + * This method will be removed once the callers have been moved over to the + * new GetEmHeight (and possibly GetMaxHeight). + */ + nsresult GetHeight(nscoord &aHeight); + /** + * Returns the amount of internal leading (in app units) for the font. This + * is computed as the "height - (ascent + descent)" + */ + nsresult GetInternalLeading(nscoord &aLeading); + /** + * Returns the amount of external leading (in app units) as + * suggested by font vendor. This value is suggested by font vendor + * to add to normal line-height beside font height. + */ + nsresult GetExternalLeading(nscoord &aLeading); + /** + * Returns the height (in app units) of the Western font's em square. This is + * em ascent plus em descent. + */ + nsresult GetEmHeight(nscoord &aHeight); + /** + * Returns, in app units, the ascent part of the Western font's em square. + */ + nsresult GetEmAscent(nscoord &aAscent); + /** + * Returns, in app units, the descent part of the Western font's em square. + */ + nsresult GetEmDescent(nscoord &aDescent); + /** + * Returns the height (in app units) of the Western font's bounding box. + * This is max ascent plus max descent. + */ + nsresult GetMaxHeight(nscoord &aHeight); + /** + * Returns, in app units, the maximum distance characters in this font extend + * above the base line. + */ + nsresult GetMaxAscent(nscoord &aAscent); + /** + * Returns, in app units, the maximum distance characters in this font extend + * below the base line. + */ + nsresult GetMaxDescent(nscoord &aDescent); + /** + * Returns, in app units, the maximum character advance for the font + */ + nsresult GetMaxAdvance(nscoord &aAdvance); + /** + * Returns the font associated with these metrics. The return value + * is only defined after Init() has been called. + */ + const nsFont &Font() { return mFont; } + /** + * Returns the language associated with these metrics + */ + nsresult GetLanguage(nsIAtom** aLanguage); + /** + * Returns the font handle associated with these metrics + */ + nsresult GetFontHandle(nsFontHandle &aHandle); + /** + * Returns the average character width + */ + nsresult GetAveCharWidth(nscoord& aAveCharWidth); + /** + * Returns the often needed width of the space character + */ + nsresult GetSpaceWidth(nscoord& aSpaceCharWidth); + + PRInt32 GetMaxStringLength(); + + // Get the width for this string. aWidth will be updated with the + // width in points, not twips. Callers must convert it if they + // want it in another format. + nsresult GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth, + nsRenderingContext *aContext); + nsresult GetWidth(const PRUnichar* aString, PRUint32 aLength, + nscoord& aWidth, PRInt32 *aFontID, + nsRenderingContext *aContext); + + // Get the text dimensions for this string + nsresult GetTextDimensions(const PRUnichar* aString, + PRUint32 aLength, + nsTextDimensions& aDimensions, + PRInt32* aFontID); + nsresult GetTextDimensions(const char* aString, + PRInt32 aLength, + PRInt32 aAvailWidth, + PRInt32* aBreaks, + PRInt32 aNumBreaks, + nsTextDimensions& aDimensions, + PRInt32& aNumCharsFit, + nsTextDimensions& aLastWordDimensions, + PRInt32* aFontID); + nsresult GetTextDimensions(const PRUnichar* aString, + PRInt32 aLength, + PRInt32 aAvailWidth, + PRInt32* aBreaks, + PRInt32 aNumBreaks, + nsTextDimensions& aDimensions, + PRInt32& aNumCharsFit, + nsTextDimensions& aLastWordDimensions, + PRInt32* aFontID); + + // Draw a string using this font handle on the surface passed in. + nsresult DrawString(const char *aString, PRUint32 aLength, + nscoord aX, nscoord aY, + const nscoord* aSpacing, + nsRenderingContext *aContext); + nsresult DrawString(const PRUnichar* aString, PRUint32 aLength, + nscoord aX, nscoord aY, + PRInt32 aFontID, + const nscoord* aSpacing, + nsRenderingContext *aContext) + { + NS_ASSERTION(!aSpacing, "Spacing not supported here"); + return DrawString(aString, aLength, aX, aY, aContext, aContext); + } + nsresult DrawString(const PRUnichar* aString, PRUint32 aLength, + nscoord aX, nscoord aY, + nsRenderingContext *aContext, + nsRenderingContext *aTextRunConstructionContext); + +#ifdef MOZ_MATHML + // These two functions get the bounding metrics for this handle, + // updating the aBoundingMetrics in app units. + nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength, + nsRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics); + nsresult GetBoundingMetrics(const PRUnichar *aString, + PRUint32 aLength, + nsRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics); +#endif /* MOZ_MATHML */ + + // Set the direction of the text rendering + nsresult SetRightToLeftText(PRBool aIsRTL); + PRBool GetRightToLeftText(); + void SetTextRunRTL(PRBool aIsRTL) { mTextRunRTL = aIsRTL; } + + gfxFontGroup* GetThebesFontGroup() { return mFontGroup; } + + gfxUserFontSet* GetUserFontSet(); + + PRBool GetRightToLeftTextRunMode() { + return mTextRunRTL; + } + + PRInt32 AppUnitsPerDevPixel() { return mP2A; } + +protected: + const gfxFont::Metrics& GetMetrics() const; + + nsFont mFont; // The font for this metrics object. + nsRefPtr mFontGroup; + gfxFontStyle *mFontStyle; + nsThebesDeviceContext *mDeviceContext; + nsCOMPtr mLanguage; + PRInt32 mP2A; + PRPackedBool mIsRightToLeft; + PRPackedBool mTextRunRTL; +}; + +#endif /* NSFONTMETRICS__H__ */ diff --git a/gfx/src/nsGfxCIID.h b/gfx/src/nsGfxCIID.h index 630cba01332..303d3ead2b9 100644 --- a/gfx/src/nsGfxCIID.h +++ b/gfx/src/nsGfxCIID.h @@ -50,10 +50,6 @@ { 0x6049b262, 0xc1e6, 0x11d1, \ { 0xa8, 0x27, 0x00, 0x40, 0x95, 0x9a, 0x28, 0xc9 } } -#define NS_FONT_METRICS_CID \ -{ 0x6049b263, 0xc1e6, 0x11d1, \ -{ 0xa8, 0x27, 0x00, 0x40, 0x95, 0x9a, 0x28, 0xc9 } } - #define NS_FONT_ENUMERATOR_CID \ { 0xa6cf9115, 0x15b3, 0x11d2, \ { 0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32 } } diff --git a/gfx/src/nsIDeviceContext.h b/gfx/src/nsIDeviceContext.h index 2851ddd4ef9..44d0a79913b 100644 --- a/gfx/src/nsIDeviceContext.h +++ b/gfx/src/nsIDeviceContext.h @@ -46,7 +46,7 @@ #include "nsStringFwd.h" class nsIView; -class nsIFontMetrics; +class nsFontMetrics; class nsIWidget; class nsIDeviceContextSpec; class nsIAtom; @@ -309,7 +309,7 @@ public: NS_IMETHOD GetSystemFont(nsSystemFontID aID, nsFont *aFont) const = 0; /** - * Get the nsIFontMetrics that describe the properties of + * Get the nsFontMetrics that describe the properties of * an nsFont. * @param aFont font description to obtain metrics for * @param aLanguage the language of the document @@ -319,10 +319,10 @@ public: */ NS_IMETHOD GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, gfxUserFontSet* aUserFontSet, - nsIFontMetrics*& aMetrics) = 0; + nsFontMetrics*& aMetrics) = 0; /** - * Get the nsIFontMetrics that describe the properties of + * Get the nsFontMetrics that describe the properties of * an nsFont. * @param aFont font description to obtain metrics for * @param aMetrics out parameter for font metrics @@ -330,7 +330,7 @@ public: * @return error status */ NS_IMETHOD GetMetricsFor(const nsFont& aFont, gfxUserFontSet* aUserFontSet, - nsIFontMetrics*& aMetrics) = 0; + nsFontMetrics*& aMetrics) = 0; /** * Check to see if a particular named font exists. @@ -347,7 +347,7 @@ public: * Notification when a font metrics instance created for this device is * about to be deleted */ - NS_IMETHOD FontMetricsDeleted(const nsIFontMetrics* aFontMetrics) = 0; + NS_IMETHOD FontMetricsDeleted(const nsFontMetrics* aFontMetrics) = 0; /** * Attempt to free up resoruces by flushing out any fonts no longer diff --git a/gfx/src/nsIFontMetrics.h b/gfx/src/nsIFontMetrics.h deleted file mode 100644 index a7ac12f3ce5..00000000000 --- a/gfx/src/nsIFontMetrics.h +++ /dev/null @@ -1,306 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is mozilla.org code. - * - * The Initial Developer of the Original Code is - * Netscape Communications Corporation. - * Portions created by the Initial Developer are Copyright (C) 1998 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Alternatively, the contents of this file may be used under the terms of - * either of the GNU General Public License Version 2 or later (the "GPL"), - * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef nsIFontMetrics_h___ -#define nsIFontMetrics_h___ - -#include "nsISupports.h" -#include "nsCoord.h" -#include "nsFont.h" - -class nsString; -class nsIDeviceContext; -class nsIAtom; -class gfxUserFontSet; -struct nsTextDimensions; -struct nsBoundingMetrics; -class gfxFontGroup; -class nsRenderingContext; - -// IID for the nsIFontMetrics interface -#define NS_IFONT_METRICS_IID \ -{ 0x360C5575, 0xF7AC, 0x4079, \ -{ 0xB8, 0xA6, 0x56, 0x91, 0x4B, 0xEA, 0x2A, 0xEA } } - -/** - * A native font handle - */ -typedef void* nsFontHandle; - -/** - * Font metrics interface - * - * This interface may be somewhat misnamed. A better name might be - * nsIFontList. The style system uses the nsFont struct for various font - * properties, one of which is font-family, which can contain a *list* of - * font names. The nsFont struct is "realized" by asking the device context - * to cough up an nsIFontMetrics object, which contains a list of real font - * handles, one for each font mentioned in font-family (and for each fallback - * when we fall off the end of that list). - * - * The style system needs to have access to certain metrics, such as the - * em height (for the CSS "em" unit), and we use the first Western font's - * metrics for that purpose. The platform-specific implementations are - * expected to select non-Western fonts that "fit" reasonably well with the - * Western font that is loaded at Init time. - */ -class nsIFontMetrics : public nsISupports -{ -public: - NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFONT_METRICS_IID) - - /** - * Initialize the font metrics. Call this after creating the font metrics. - * Font metrics you get from the font cache do NOT need to be initialized - * - * @see nsIDeviceContext#GetMetricsFor() - */ - NS_IMETHOD Init(const nsFont& aFont, nsIAtom* aLanguage, - nsIDeviceContext *aContext, gfxUserFontSet *aUserFontSet = nsnull) = 0; - - /** - * Destroy this font metrics. This breaks the association between - * the font metrics and the device context. - */ - NS_IMETHOD Destroy() = 0; - - /** - * Return the font's xheight property, scaled into app-units. - */ - NS_IMETHOD GetXHeight(nscoord& aResult) = 0; - - /** - * Return the font's superscript offset (the distance from the - * baseline to where a superscript's baseline should be placed). - * The value returned will be positive. - */ - NS_IMETHOD GetSuperscriptOffset(nscoord& aResult) = 0; - - /** - * Return the font's subscript offset (the distance from the - * baseline to where a subscript's baseline should be placed). The - * value returned will be a positive value. - */ - NS_IMETHOD GetSubscriptOffset(nscoord& aResult) = 0; - - /** - * Return the font's strikeout offset (the distance from the - * baseline to where a strikeout should be placed) and size. - * Positive values are above the baseline, negative below. - */ - NS_IMETHOD GetStrikeout(nscoord& aOffset, nscoord& aSize) = 0; - - /** - * Return the font's underline offset (the distance from the - * baseline to where a underline should be placed) and size. - * Positive values are above the baseline, negative below. - */ - NS_IMETHOD GetUnderline(nscoord& aOffset, nscoord& aSize) = 0; - - /** - * Returns the height (in app units) of the font. This is ascent plus descent - * plus any internal leading - * - * This method will be removed once the callers have been moved over to the - * new GetEmHeight (and possibly GetMaxHeight). - */ - NS_IMETHOD GetHeight(nscoord &aHeight) = 0; - - /** - * Returns the amount of internal leading (in app units) for the font. This - * is computed as the "height - (ascent + descent)" - */ - NS_IMETHOD GetInternalLeading(nscoord &aLeading) = 0; - - /** - * Returns the amount of external leading (in app units) as - * suggested by font vendor. This value is suggested by font vendor - * to add to normal line-height beside font height. - */ - NS_IMETHOD GetExternalLeading(nscoord &aLeading) = 0; - - /** - * Returns the height (in app units) of the Western font's em square. This is - * em ascent plus em descent. - */ - NS_IMETHOD GetEmHeight(nscoord &aHeight) = 0; - - /** - * Returns, in app units, the ascent part of the Western font's em square. - */ - NS_IMETHOD GetEmAscent(nscoord &aAscent) = 0; - - /** - * Returns, in app units, the descent part of the Western font's em square. - */ - NS_IMETHOD GetEmDescent(nscoord &aDescent) = 0; - - /** - * Returns the height (in app units) of the Western font's bounding box. - * This is max ascent plus max descent. - */ - NS_IMETHOD GetMaxHeight(nscoord &aHeight) = 0; - - /** - * Returns, in app units, the maximum distance characters in this font extend - * above the base line. - */ - NS_IMETHOD GetMaxAscent(nscoord &aAscent) = 0; - - /** - * Returns, in app units, the maximum distance characters in this font extend - * below the base line. - */ - NS_IMETHOD GetMaxDescent(nscoord &aDescent) = 0; - - /** - * Returns, in app units, the maximum character advance for the font - */ - NS_IMETHOD GetMaxAdvance(nscoord &aAdvance) = 0; - - /** - * Returns the font associated with these metrics. The return value - * is only defined after Init() has been called. - */ - const nsFont &Font() { return mFont; } - - /** - * Returns the language associated with these metrics - */ - NS_IMETHOD GetLanguage(nsIAtom** aLanguage) = 0; - - /** - * Returns the font handle associated with these metrics - */ - NS_IMETHOD GetFontHandle(nsFontHandle &aHandle) = 0; - - /** - * Returns the average character width - */ - NS_IMETHOD GetAveCharWidth(nscoord& aAveCharWidth) = 0; - - /** - * Returns the often needed width of the space character - */ - NS_IMETHOD GetSpaceWidth(nscoord& aSpaceCharWidth) = 0; - - // methods formerly in nsIThebesFontMetrics - - // Get the width for this string. aWidth will be updated with the - // width in points, not twips. Callers must convert it if they - // want it in another format. - virtual nsresult GetWidth(const char* aString, PRUint32 aLength, - nscoord& aWidth, nsRenderingContext *aContext) = 0; - virtual nsresult GetWidth(const PRUnichar* aString, PRUint32 aLength, - nscoord& aWidth, PRInt32 *aFontID, - nsRenderingContext *aContext) = 0; - - // Get the text dimensions for this string - virtual nsresult GetTextDimensions(const PRUnichar* aString, - PRUint32 aLength, - nsTextDimensions& aDimensions, - PRInt32* aFontID) = 0; - virtual nsresult GetTextDimensions(const char* aString, - PRInt32 aLength, - PRInt32 aAvailWidth, - PRInt32* aBreaks, - PRInt32 aNumBreaks, - nsTextDimensions& aDimensions, - PRInt32& aNumCharsFit, - nsTextDimensions& aLastWordDimensions, - PRInt32* aFontID) = 0; - virtual nsresult GetTextDimensions(const PRUnichar* aString, - PRInt32 aLength, - PRInt32 aAvailWidth, - PRInt32* aBreaks, - PRInt32 aNumBreaks, - nsTextDimensions& aDimensions, - PRInt32& aNumCharsFit, - nsTextDimensions& aLastWordDimensions, - PRInt32* aFontID) = 0; - - // Draw a string using this font handle on the surface passed in. - virtual nsresult DrawString(const char *aString, PRUint32 aLength, - nscoord aX, nscoord aY, - const nscoord* aSpacing, - nsRenderingContext *aContext) = 0; - virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength, - nscoord aX, nscoord aY, - PRInt32 aFontID, - const nscoord* aSpacing, - nsRenderingContext *aContext) = 0; - virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength, - nscoord aX, nscoord aY, - nsRenderingContext *aContext, - nsRenderingContext *aTextRunConstructionContext) = 0; - -#ifdef MOZ_MATHML - // These two functions get the bounding metrics for this handle, - // updating the aBoundingMetrics in Points. This means that the - // caller will have to update them to twips before passing it - // back. - virtual nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength, - nsRenderingContext *aContext, - nsBoundingMetrics &aBoundingMetrics) = 0; - // aCachedOffset will be updated with a new offset. - virtual nsresult GetBoundingMetrics(const PRUnichar *aString, - PRUint32 aLength, - nsRenderingContext *aContext, - nsBoundingMetrics &aBoundingMetrics) = 0; -#endif /* MOZ_MATHML */ - - // Set the direction of the text rendering - virtual nsresult SetRightToLeftText(PRBool aIsRTL) = 0; - virtual PRBool GetRightToLeftText() = 0; - virtual void SetTextRunRTL(PRBool aIsRTL) = 0; - - virtual PRInt32 GetMaxStringLength() = 0; - - virtual gfxFontGroup* GetThebesFontGroup() = 0; - - // Needs to be virtual and at this level so that its caller in gkgfx can - // avoid linking against thebes. - virtual gfxUserFontSet* GetUserFontSet() = 0; - -protected: - - nsFont mFont; // The font for this metrics object. -}; - -NS_DEFINE_STATIC_IID_ACCESSOR(nsIFontMetrics, NS_IFONT_METRICS_IID) - -#endif /* nsIFontMetrics_h___ */ diff --git a/gfx/src/nsRenderingContext.cpp b/gfx/src/nsRenderingContext.cpp index 1f55ee403bb..8c146af9bc6 100644 --- a/gfx/src/nsRenderingContext.cpp +++ b/gfx/src/nsRenderingContext.cpp @@ -477,7 +477,7 @@ nsRenderingContext::SetFont(const nsFont& aFont, } void -nsRenderingContext::SetFont(nsIFontMetrics *aFontMetrics) +nsRenderingContext::SetFont(nsFontMetrics *aFontMetrics) { mFontMetrics = aFontMetrics; } diff --git a/gfx/src/nsRenderingContext.h b/gfx/src/nsRenderingContext.h index b8455afb8ff..6611f8978a4 100644 --- a/gfx/src/nsRenderingContext.h +++ b/gfx/src/nsRenderingContext.h @@ -42,7 +42,7 @@ #include "nsAutoPtr.h" #include "nsCOMPtr.h" #include "nsIDeviceContext.h" -#include "nsIFontMetrics.h" +#include "nsFontMetrics.h" #include "nsColor.h" #include "nsCoord.h" #include "gfxContext.h" @@ -120,8 +120,8 @@ public: void SetFont(const nsFont& aFont, nsIAtom* aLanguage, gfxUserFontSet *aUserFontSet); void SetFont(const nsFont& aFont, gfxUserFontSet *aUserFontSet); - void SetFont(nsIFontMetrics *aFontMetrics); - nsIFontMetrics *FontMetrics() { return mFontMetrics; } // may be null + void SetFont(nsFontMetrics *aFontMetrics); + nsFontMetrics *FontMetrics() { return mFontMetrics; } // may be null void SetRightToLeftText(PRBool aIsRTL); void SetTextRunRTL(PRBool aIsRTL); @@ -151,7 +151,7 @@ protected: nsRefPtr mThebes; nsCOMPtr mDeviceContext; - nsCOMPtr mFontMetrics; + nsRefPtr mFontMetrics; double mP2A; // cached app units per device pixel value }; diff --git a/gfx/src/nsThebesDeviceContext.cpp b/gfx/src/nsThebesDeviceContext.cpp index c913f209323..bd1924cc70c 100644 --- a/gfx/src/nsThebesDeviceContext.cpp +++ b/gfx/src/nsThebesDeviceContext.cpp @@ -105,15 +105,14 @@ public: nsresult Init(nsIDeviceContext* aContext); nsresult GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, gfxUserFontSet* aUserFontSet, - nsIFontMetrics*& aMetrics); + nsFontMetrics*& aMetrics); - nsresult FontMetricsDeleted(const nsIFontMetrics* aFontMetrics); + nsresult FontMetricsDeleted(const nsFontMetrics* aFontMetrics); nsresult Compact(); nsresult Flush(); - nsresult CreateFontMetricsInstance(nsIFontMetrics** fm); protected: - nsTArray mFontMetrics; + nsTArray mFontMetrics; nsIDeviceContext *mContext; // we do not addref this since // ownership is implied. MMP. }; @@ -142,12 +141,12 @@ nsFontCache::Init(nsIDeviceContext* aContext) nsresult nsFontCache::GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, - gfxUserFontSet* aUserFontSet, nsIFontMetrics*& aMetrics) + gfxUserFontSet* aUserFontSet, nsFontMetrics*& aMetrics) { // First check our cache // start from the end, which is where we put the most-recent-used element - nsIFontMetrics* fm; + nsFontMetrics* fm; PRInt32 n = mFontMetrics.Length() - 1; for (PRInt32 i = n; i >= 0; --i) { fm = mFontMetrics[i]; @@ -169,10 +168,9 @@ nsFontCache::GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, // It's not in the cache. Get font metrics and then cache them. - aMetrics = nsnull; - nsresult rv = CreateFontMetricsInstance(&fm); - if (NS_FAILED(rv)) return rv; - rv = fm->Init(aFont, aLanguage, mContext, aUserFontSet); + fm = new nsFontMetrics(); + NS_ADDREF(fm); + nsresult rv = fm->Init(aFont, aLanguage, mContext, aUserFontSet); if (NS_SUCCEEDED(rv)) { // the mFontMetrics list has the "head" at the end, because append // is cheaper than insert @@ -189,13 +187,12 @@ nsFontCache::GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, // objects are available. Compact the cache and try again. Compact(); - rv = CreateFontMetricsInstance(&fm); - if (NS_FAILED(rv)) return rv; + fm = new nsFontMetrics(); + NS_ADDREF(fm); rv = fm->Init(aFont, aLanguage, mContext, aUserFontSet); if (NS_SUCCEEDED(rv)) { mFontMetrics.AppendElement(fm); aMetrics = fm; - NS_ADDREF(aMetrics); return NS_OK; } fm->Destroy(); @@ -215,14 +212,7 @@ nsFontCache::GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, return rv; } -nsresult -nsFontCache::CreateFontMetricsInstance(nsIFontMetrics** fm) -{ - static NS_DEFINE_CID(kFontMetricsCID, NS_FONT_METRICS_CID); - return CallCreateInstance(kFontMetricsCID, fm); -} - -nsresult nsFontCache::FontMetricsDeleted(const nsIFontMetrics* aFontMetrics) +nsresult nsFontCache::FontMetricsDeleted(const nsFontMetrics* aFontMetrics) { mFontMetrics.RemoveElement(aFontMetrics); return NS_OK; @@ -233,8 +223,8 @@ nsresult nsFontCache::Compact() // Need to loop backward because the running element can be removed on // the way for (PRInt32 i = mFontMetrics.Length()-1; i >= 0; --i) { - nsIFontMetrics* fm = mFontMetrics[i]; - nsIFontMetrics* oldfm = fm; + nsFontMetrics* fm = mFontMetrics[i]; + nsFontMetrics* oldfm = fm; // Destroy() isn't here because we want our device context to be // notified NS_RELEASE(fm); // this will reset fm to nsnull @@ -251,7 +241,7 @@ nsresult nsFontCache::Compact() nsresult nsFontCache::Flush() { for (PRInt32 i = mFontMetrics.Length()-1; i >= 0; --i) { - nsIFontMetrics* fm = mFontMetrics[i]; + nsFontMetrics* fm = mFontMetrics[i]; // Destroy() will unhook our device context from the fm so that we // won't waste time in triggering the notification of // FontMetricsDeleted() in the subsequent release @@ -324,7 +314,7 @@ NS_IMETHODIMP nsThebesDeviceContext::CreateFontCache() return mFontCache->Init(this); } -NS_IMETHODIMP nsThebesDeviceContext::FontMetricsDeleted(const nsIFontMetrics* aFontMetrics) +NS_IMETHODIMP nsThebesDeviceContext::FontMetricsDeleted(const nsFontMetrics* aFontMetrics) { if (mFontCache) { mFontCache->FontMetricsDeleted(aFontMetrics); @@ -348,7 +338,7 @@ nsThebesDeviceContext::GetLocaleLanguage(void) } NS_IMETHODIMP nsThebesDeviceContext::GetMetricsFor(const nsFont& aFont, - nsIAtom* aLanguage, gfxUserFontSet* aUserFontSet, nsIFontMetrics*& aMetrics) + nsIAtom* aLanguage, gfxUserFontSet* aUserFontSet, nsFontMetrics*& aMetrics) { if (nsnull == mFontCache) { nsresult rv = CreateFontCache(); @@ -371,7 +361,7 @@ NS_IMETHODIMP nsThebesDeviceContext::GetMetricsFor(const nsFont& aFont, NS_IMETHODIMP nsThebesDeviceContext::GetMetricsFor(const nsFont& aFont, gfxUserFontSet* aUserFontSet, - nsIFontMetrics*& aMetrics) + nsFontMetrics*& aMetrics) { if (nsnull == mFontCache) { nsresult rv = CreateFontCache(); diff --git a/gfx/src/nsThebesDeviceContext.h b/gfx/src/nsThebesDeviceContext.h index 5cb66230c77..a2ed1193df0 100644 --- a/gfx/src/nsThebesDeviceContext.h +++ b/gfx/src/nsThebesDeviceContext.h @@ -86,10 +86,10 @@ public: NS_IMETHOD GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, gfxUserFontSet* aUserFontSet, - nsIFontMetrics*& aMetrics); + nsFontMetrics*& aMetrics); NS_IMETHOD GetMetricsFor(const nsFont& aFont, gfxUserFontSet* aUserFontSet, - nsIFontMetrics*& aMetrics); + nsFontMetrics*& aMetrics); NS_IMETHOD FirstExistingFont(const nsFont& aFont, nsString& aFaceName); @@ -97,7 +97,7 @@ public: PRBool& aAliased); NS_IMETHOD CreateFontCache(); - NS_IMETHOD FontMetricsDeleted(const nsIFontMetrics* aFontMetrics); + NS_IMETHOD FontMetricsDeleted(const nsFontMetrics* aFontMetrics); NS_IMETHOD FlushFontCache(void); NS_IMETHOD PrepareNativeWidget(nsIWidget *aWidget, void **aOut); diff --git a/gfx/src/nsThebesFontMetrics.h b/gfx/src/nsThebesFontMetrics.h deleted file mode 100644 index 56743674820..00000000000 --- a/gfx/src/nsThebesFontMetrics.h +++ /dev/null @@ -1,209 +0,0 @@ -/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is mozilla.org code. - * - * The Initial Developer of the Original Code is - * mozilla.org. - * Portions created by the Initial Developer are Copyright (C) 2005 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Stuart Parmenter - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef NSTHEBESFONTMETRICS__H__ -#define NSTHEBESFONTMETRICS__H__ - -#include "nsIFontMetrics.h" -#include "nsCOMPtr.h" -#include "nsRenderingContext.h" -#include "gfxFont.h" -#include "gfxTextRunCache.h" - -class nsIAtom; -class nsIDeviceContext; -class nsThebesDeviceContext; - -class nsThebesFontMetrics : public nsIFontMetrics -{ -public: - nsThebesFontMetrics(); - virtual ~nsThebesFontMetrics(); - - NS_DECL_ISUPPORTS - - NS_IMETHOD Init(const nsFont& aFont, nsIAtom* aLanguage, - nsIDeviceContext *aContext, - gfxUserFontSet *aUserFontSet = nsnull); - NS_IMETHOD Destroy(); - NS_IMETHOD GetXHeight(nscoord& aResult); - NS_IMETHOD GetSuperscriptOffset(nscoord& aResult); - NS_IMETHOD GetSubscriptOffset(nscoord& aResult); - NS_IMETHOD GetStrikeout(nscoord& aOffset, nscoord& aSize); - NS_IMETHOD GetUnderline(nscoord& aOffset, nscoord& aSize); - NS_IMETHOD GetHeight(nscoord &aHeight); - NS_IMETHOD GetInternalLeading(nscoord &aLeading); - NS_IMETHOD GetExternalLeading(nscoord &aLeading); - NS_IMETHOD GetEmHeight(nscoord &aHeight); - NS_IMETHOD GetEmAscent(nscoord &aAscent); - NS_IMETHOD GetEmDescent(nscoord &aDescent); - NS_IMETHOD GetMaxHeight(nscoord &aHeight); - NS_IMETHOD GetMaxAscent(nscoord &aAscent); - NS_IMETHOD GetMaxDescent(nscoord &aDescent); - NS_IMETHOD GetMaxAdvance(nscoord &aAdvance); - NS_IMETHOD GetLanguage(nsIAtom** aLanguage); - NS_IMETHOD GetFontHandle(nsFontHandle &aHandle); - NS_IMETHOD GetAveCharWidth(nscoord& aAveCharWidth); - NS_IMETHOD GetSpaceWidth(nscoord& aSpaceCharWidth); - virtual PRInt32 GetMaxStringLength(); - - - virtual nsresult GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth, - nsRenderingContext *aContext); - virtual nsresult GetWidth(const PRUnichar* aString, PRUint32 aLength, - nscoord& aWidth, PRInt32 *aFontID, - nsRenderingContext *aContext); - - // Get the text dimensions for this string - virtual nsresult GetTextDimensions(const PRUnichar* aString, - PRUint32 aLength, - nsTextDimensions& aDimensions, - PRInt32* aFontID); - virtual nsresult GetTextDimensions(const char* aString, - PRInt32 aLength, - PRInt32 aAvailWidth, - PRInt32* aBreaks, - PRInt32 aNumBreaks, - nsTextDimensions& aDimensions, - PRInt32& aNumCharsFit, - nsTextDimensions& aLastWordDimensions, - PRInt32* aFontID); - virtual nsresult GetTextDimensions(const PRUnichar* aString, - PRInt32 aLength, - PRInt32 aAvailWidth, - PRInt32* aBreaks, - PRInt32 aNumBreaks, - nsTextDimensions& aDimensions, - PRInt32& aNumCharsFit, - nsTextDimensions& aLastWordDimensions, - PRInt32* aFontID); - - // Draw a string using this font handle on the surface passed in. - virtual nsresult DrawString(const char *aString, PRUint32 aLength, - nscoord aX, nscoord aY, - const nscoord* aSpacing, - nsRenderingContext *aContext); - virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength, - nscoord aX, nscoord aY, - PRInt32 aFontID, - const nscoord* aSpacing, - nsRenderingContext *aContext) - { - NS_ASSERTION(!aSpacing, "Spacing not supported here"); - return DrawString(aString, aLength, aX, aY, aContext, aContext); - } - virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength, - nscoord aX, nscoord aY, - nsRenderingContext *aContext, - nsRenderingContext *aTextRunConstructionContext); - -#ifdef MOZ_MATHML - // These two functions get the bounding metrics for this handle, - // updating the aBoundingMetrics in app units. - virtual nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength, - nsRenderingContext *aContext, - nsBoundingMetrics &aBoundingMetrics); - virtual nsresult GetBoundingMetrics(const PRUnichar *aString, - PRUint32 aLength, - nsRenderingContext *aContext, - nsBoundingMetrics &aBoundingMetrics); -#endif /* MOZ_MATHML */ - - // Set the direction of the text rendering - virtual nsresult SetRightToLeftText(PRBool aIsRTL); - virtual PRBool GetRightToLeftText(); - virtual void SetTextRunRTL(PRBool aIsRTL) { mTextRunRTL = aIsRTL; } - - virtual gfxFontGroup* GetThebesFontGroup() { return mFontGroup; } - - virtual gfxUserFontSet* GetUserFontSet(); - - PRBool GetRightToLeftTextRunMode() { - return mTextRunRTL; - } - -protected: - - const gfxFont::Metrics& GetMetrics() const; - - class AutoTextRun { - public: - AutoTextRun(nsThebesFontMetrics* aMetrics, nsRenderingContext* aRC, - const char* aString, PRInt32 aLength) { - mTextRun = gfxTextRunCache::MakeTextRun( - reinterpret_cast(aString), aLength, - aMetrics->mFontGroup, aRC->ThebesContext(), - aMetrics->mP2A, - ComputeFlags(aMetrics)); - } - AutoTextRun(nsThebesFontMetrics* aMetrics, nsRenderingContext* aRC, - const PRUnichar* aString, PRInt32 aLength) { - mTextRun = gfxTextRunCache::MakeTextRun( - aString, aLength, aMetrics->mFontGroup, - aRC->ThebesContext(), - aMetrics->mP2A, - ComputeFlags(aMetrics)); - } - gfxTextRun* operator->() { return mTextRun.get(); } - gfxTextRun* get() { return mTextRun.get(); } - - private: - gfxTextRunCache::AutoTextRun mTextRun; - - static PRUint32 ComputeFlags(nsThebesFontMetrics* aMetrics) { - PRUint32 flags = 0; - if (aMetrics->GetRightToLeftTextRunMode()) { - flags |= gfxTextRunFactory::TEXT_IS_RTL; - } - return flags; - } - }; - friend class AutoTextRun; - - nsRefPtr mFontGroup; - gfxFontStyle *mFontStyle; - -private: - nsThebesDeviceContext *mDeviceContext; - nsCOMPtr mLanguage; - PRInt32 mP2A; - PRPackedBool mIsRightToLeft; - PRPackedBool mTextRunRTL; -}; - -#endif /* NSTHEBESFONTMETRICS__H__ */ diff --git a/gfx/src/nsThebesGfxFactory.cpp b/gfx/src/nsThebesGfxFactory.cpp index 9ce25ee3ea8..0f514e6e8ac 100644 --- a/gfx/src/nsThebesGfxFactory.cpp +++ b/gfx/src/nsThebesGfxFactory.cpp @@ -44,11 +44,9 @@ #include "nsThebesDeviceContext.h" #include "nsThebesRegion.h" -#include "nsThebesFontMetrics.h" #include "nsThebesFontEnumerator.h" #include "gfxPlatform.h" -NS_GENERIC_FACTORY_CONSTRUCTOR(nsThebesFontMetrics) NS_GENERIC_FACTORY_CONSTRUCTOR(nsThebesDeviceContext) NS_GENERIC_FACTORY_CONSTRUCTOR(nsThebesRegion) NS_GENERIC_FACTORY_CONSTRUCTOR(nsThebesFontEnumerator) @@ -94,14 +92,12 @@ nsScriptableRegionConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult return rv; } -NS_DEFINE_NAMED_CID(NS_FONT_METRICS_CID); NS_DEFINE_NAMED_CID(NS_FONT_ENUMERATOR_CID); NS_DEFINE_NAMED_CID(NS_DEVICE_CONTEXT_CID); NS_DEFINE_NAMED_CID(NS_REGION_CID); NS_DEFINE_NAMED_CID(NS_SCRIPTABLE_REGION_CID); static const mozilla::Module::CIDEntry kThebesCIDs[] = { - { &kNS_FONT_METRICS_CID, false, NULL, nsThebesFontMetricsConstructor }, { &kNS_FONT_ENUMERATOR_CID, false, NULL, nsThebesFontEnumeratorConstructor }, { &kNS_DEVICE_CONTEXT_CID, false, NULL, nsThebesDeviceContextConstructor }, { &kNS_REGION_CID, false, NULL, nsThebesRegionConstructor }, @@ -110,7 +106,6 @@ static const mozilla::Module::CIDEntry kThebesCIDs[] = { }; static const mozilla::Module::ContractIDEntry kThebesContracts[] = { - { "@mozilla.org/gfx/fontmetrics;1", &kNS_FONT_METRICS_CID }, { "@mozilla.org/gfx/fontenumerator;1", &kNS_FONT_ENUMERATOR_CID }, { "@mozilla.org/gfx/devicecontext;1", &kNS_DEVICE_CONTEXT_CID }, { "@mozilla.org/gfx/region/nsThebes;1", &kNS_REGION_CID },