diff --git a/gfx/src/gtk/nsFontMetricsGTK.cpp b/gfx/src/gtk/nsFontMetricsGTK.cpp index 87c580e215a..dd57204e892 100644 --- a/gfx/src/gtk/nsFontMetricsGTK.cpp +++ b/gfx/src/gtk/nsFontMetricsGTK.cpp @@ -60,6 +60,7 @@ nsFontMetricsGTK::nsFontMetricsGTK() mStrikeoutOffset = 0; mUnderlineSize = 0; mUnderlineOffset = 0; + mSpaceWidth = 0; } nsFontMetricsGTK::~nsFontMetricsGTK() @@ -418,6 +419,9 @@ void nsFontMetricsGTK::RealizeFont() // 56% of ascent, best guess for non-true type mXHeight = NSToCoordRound((float) fontInfo->ascent* f * 0.56f); + gint rawWidth = gdk_text_width(mFontHandle, " ", 1); + mSpaceWidth = NSToCoordRound(rawWidth * f); + unsigned long pr = 0; if (::XGetFontProperty(fontInfo, XA_X_HEIGHT, &pr)) @@ -2179,4 +2183,11 @@ nsFontMetricsGTK::DrawString(nsDrawingSurfaceGTK* aSurface, nsFontGTK* aFont, aY + aFont->mBaselineAdjust, (char*) buf, len); } +nsresult +nsFontMetricsGTK::GetSpaceWidth(nscoord &aSpaceWidth) +{ + aSpaceWidth = mSpaceWidth; + return NS_OK; +} + #endif /* FONT_SWITCHING */ diff --git a/gfx/src/gtk/nsFontMetricsGTK.h b/gfx/src/gtk/nsFontMetricsGTK.h index 8ea970d5f7e..3b4de913585 100644 --- a/gfx/src/gtk/nsFontMetricsGTK.h +++ b/gfx/src/gtk/nsFontMetricsGTK.h @@ -99,6 +99,8 @@ public: NS_IMETHOD GetMaxAdvance(nscoord &aAdvance); NS_IMETHOD GetFont(const nsFont *&aFont); NS_IMETHOD GetFontHandle(nsFontHandle &aHandle); + + virtual nsresult GetSpaceWidth(nscoord &aSpaceWidth); #ifdef FONT_SWITCHING @@ -149,6 +151,7 @@ protected: nscoord mStrikeoutOffset; nscoord mUnderlineSize; nscoord mUnderlineOffset; + nscoord mSpaceWidth; #ifdef FONT_SWITCHING diff --git a/gfx/src/gtk/nsRenderingContextGTK.cpp b/gfx/src/gtk/nsRenderingContextGTK.cpp index 3af1b38ee10..c8addd491d6 100644 --- a/gfx/src/gtk/nsRenderingContextGTK.cpp +++ b/gfx/src/gtk/nsRenderingContextGTK.cpp @@ -913,6 +913,12 @@ NS_IMETHODIMP nsRenderingContextGTK::FillArc(nscoord aX, nscoord aY, NS_IMETHODIMP nsRenderingContextGTK::GetWidth(char aC, nscoord &aWidth) { + // Check for the very common case of trying to get the width of a single + // space. + if ((aC == ' ') && (nsnull != mFontMetrics)) { + nsFontMetricsGTK* fontMetricsGTK = (nsFontMetricsGTK*)mFontMetrics; + return fontMetricsGTK->GetSpaceWidth(aWidth); + } return GetWidth(&aC, 1, aWidth); }