From 0d912a947be975faec5390ef29471a35b8bfe4a0 Mon Sep 17 00:00:00 2001 From: "karlt+@karlt.net" Date: Sun, 11 Nov 2007 20:39:17 -0800 Subject: [PATCH] Bug 324857 - MathML all screwed up in Cairo builds: implement nsIRenderingContext::GetBoundingMetrics() p=steve.swanson@mackichan.com and karlt, r=pavlov a=blocking1.9+ --- gfx/src/thebes/nsIThebesFontMetrics.h | 5 +- gfx/src/thebes/nsThebesFontMetrics.cpp | 61 ++++++++++++++++----- gfx/src/thebes/nsThebesFontMetrics.h | 12 ++-- gfx/src/thebes/nsThebesRenderingContext.cpp | 4 +- 4 files changed, 55 insertions(+), 27 deletions(-) diff --git a/gfx/src/thebes/nsIThebesFontMetrics.h b/gfx/src/thebes/nsIThebesFontMetrics.h index 3e2d78830bc..14e506fdb05 100644 --- a/gfx/src/thebes/nsIThebesFontMetrics.h +++ b/gfx/src/thebes/nsIThebesFontMetrics.h @@ -99,12 +99,13 @@ public: // caller will have to update them to twips before passing it // back. virtual nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength, + nsThebesRenderingContext *aContext, nsBoundingMetrics &aBoundingMetrics) = 0; // aCachedOffset will be updated with a new offset. virtual nsresult GetBoundingMetrics(const PRUnichar *aString, PRUint32 aLength, - nsBoundingMetrics &aBoundingMetrics, - PRInt32 *aFontID) = 0; + nsThebesRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics) = 0; #endif /* MOZ_MATHML */ // Set the direction of the text rendering diff --git a/gfx/src/thebes/nsThebesFontMetrics.cpp b/gfx/src/thebes/nsThebesFontMetrics.cpp index 7d91e8cbdc6..5444dfdea85 100644 --- a/gfx/src/thebes/nsThebesFontMetrics.cpp +++ b/gfx/src/thebes/nsThebesFontMetrics.cpp @@ -393,7 +393,6 @@ nsThebesFontMetrics::DrawString(const char *aString, PRUint32 aLength, return NS_OK; } -// aCachedOffset will be updated with a new offset. nsresult nsThebesFontMetrics::DrawString(const PRUnichar* aString, PRUint32 aLength, nscoord aX, nscoord aY, @@ -419,25 +418,57 @@ nsThebesFontMetrics::DrawString(const PRUnichar* aString, PRUint32 aLength, } #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. -nsresult -nsThebesFontMetrics::GetBoundingMetrics(const char *aString, PRUint32 aLength, - nsBoundingMetrics &aBoundingMetrics) + +static void +GetTextRunBoundingMetrics(gfxTextRun *aTextRun, PRUint32 aStart, PRUint32 aLength, + nsThebesRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics) { - return NS_ERROR_NOT_IMPLEMENTED; + StubPropertyProvider provider; + gfxTextRun::Metrics theMetrics = + aTextRun->MeasureText(aStart, aLength, PR_TRUE, aContext->Thebes(), &provider); + + aBoundingMetrics.leftBearing = NSToCoordFloor(theMetrics.mBoundingBox.X()); + aBoundingMetrics.rightBearing = NSToCoordCeil(theMetrics.mBoundingBox.XMost()); + aBoundingMetrics.width = NSToCoordRound(theMetrics.mAdvanceWidth); + aBoundingMetrics.ascent = NSToCoordCeil(- theMetrics.mBoundingBox.Y()); + aBoundingMetrics.descent = NSToCoordCeil(theMetrics.mBoundingBox.YMost()); } -// aCachedOffset will be updated with a new offset. nsresult -nsThebesFontMetrics::GetBoundingMetrics(const PRUnichar *aString, - PRUint32 aLength, - nsBoundingMetrics &aBoundingMetrics, - PRInt32 *aFontID) +nsThebesFontMetrics::GetBoundingMetrics(const char *aString, PRUint32 aLength, + nsThebesRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics) { - return NS_ERROR_NOT_IMPLEMENTED; + if (aLength == 0) { + aBoundingMetrics.Clear(); + return NS_OK; + } + + AutoTextRun textRun(this, aContext, aString, aLength); + if (!textRun.get()) + return NS_ERROR_FAILURE; + + GetTextRunBoundingMetrics(textRun.get(), 0, aLength, aContext, aBoundingMetrics); + return NS_OK; +} + +nsresult +nsThebesFontMetrics::GetBoundingMetrics(const PRUnichar *aString, PRUint32 aLength, + nsThebesRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics) +{ + if (aLength == 0) { + aBoundingMetrics.Clear(); + return NS_OK; + } + + AutoTextRun textRun(this, aContext, aString, aLength); + if (!textRun.get()) + return NS_ERROR_FAILURE; + + GetTextRunBoundingMetrics(textRun.get(), 0, aLength, aContext, aBoundingMetrics); + return NS_OK; } #endif /* MOZ_MATHML */ diff --git a/gfx/src/thebes/nsThebesFontMetrics.h b/gfx/src/thebes/nsThebesFontMetrics.h index 5f17ead5ad4..3ec91d34da5 100644 --- a/gfx/src/thebes/nsThebesFontMetrics.h +++ b/gfx/src/thebes/nsThebesFontMetrics.h @@ -85,7 +85,6 @@ public: virtual nsresult GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth, nsThebesRenderingContext *aContext); - // aCachedOffset will be updated with a new offset. virtual nsresult GetWidth(const PRUnichar* aString, PRUint32 aLength, nscoord& aWidth, PRInt32 *aFontID, nsThebesRenderingContext *aContext); @@ -119,7 +118,6 @@ public: nscoord aX, nscoord aY, const nscoord* aSpacing, nsThebesRenderingContext *aContext); - // aCachedOffset will be updated with a new offset. virtual nsresult DrawString(const PRUnichar* aString, PRUint32 aLength, nscoord aX, nscoord aY, PRInt32 aFontID, @@ -128,16 +126,14 @@ public: #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. + // updating the aBoundingMetrics in app units. virtual nsresult GetBoundingMetrics(const char *aString, PRUint32 aLength, + nsThebesRenderingContext *aContext, nsBoundingMetrics &aBoundingMetrics); - // aCachedOffset will be updated with a new offset. virtual nsresult GetBoundingMetrics(const PRUnichar *aString, PRUint32 aLength, - nsBoundingMetrics &aBoundingMetrics, - PRInt32 *aFontID); + nsThebesRenderingContext *aContext, + nsBoundingMetrics &aBoundingMetrics); #endif /* MOZ_MATHML */ // Set the direction of the text rendering diff --git a/gfx/src/thebes/nsThebesRenderingContext.cpp b/gfx/src/thebes/nsThebesRenderingContext.cpp index e54f7c2b7f2..19130522125 100644 --- a/gfx/src/thebes/nsThebesRenderingContext.cpp +++ b/gfx/src/thebes/nsThebesRenderingContext.cpp @@ -973,7 +973,7 @@ nsThebesRenderingContext::GetBoundingMetricsInternal(const char* aString, PRUint32 aLength, nsBoundingMetrics& aBoundingMetrics) { - return NS_ERROR_NOT_IMPLEMENTED; + return mFontMetrics->GetBoundingMetrics(aString, aLength, this, aBoundingMetrics); } NS_IMETHODIMP @@ -982,7 +982,7 @@ nsThebesRenderingContext::GetBoundingMetricsInternal(const PRUnichar* aString, nsBoundingMetrics& aBoundingMetrics, PRInt32* aFontID) { - return NS_ERROR_NOT_IMPLEMENTED; + return mFontMetrics->GetBoundingMetrics(aString, aLength, this, aBoundingMetrics); } #endif // MOZ_MATHML