From 85317fe4bd138cd1c4112d2ecd2f917681121d25 Mon Sep 17 00:00:00 2001 From: "michaelp%netscape.com" Date: Fri, 2 Oct 1998 01:07:56 +0000 Subject: [PATCH] moved GetWidth() APIs from fontmetrics to the renderingcontext. --- gfx/src/mac/nsFontMetricsMac.cpp | 79 ----------------------- gfx/src/mac/nsFontMetricsMac.h | 7 -- gfx/src/mac/nsRenderingContextMac.cpp | 77 +++++++++++++++++++++- gfx/src/mac/nsRenderingContextMac.h | 6 ++ gfx/src/motif/nsFontMetricsUnix.cpp | 79 ----------------------- gfx/src/motif/nsFontMetricsUnix.h | 9 --- gfx/src/motif/nsRenderingContextUnix.cpp | 77 ++++++++++++++++++++++ gfx/src/motif/nsRenderingContextUnix.h | 7 ++ gfx/src/nsDeviceContext.cpp | 22 +++++++ gfx/src/nsDeviceContext.h | 1 + gfx/src/nsIDeviceContext.h | 1 + gfx/src/nsIFontMetrics.h | 35 +--------- gfx/src/nsIRenderingContext.h | 32 +++++++++ gfx/src/windows/nsFontMetricsWin.cpp | 4 ++ gfx/src/windows/nsFontMetricsWin.h | 2 + gfx/src/windows/nsRenderingContextWin.cpp | 57 ++++++++++++++++ gfx/src/windows/nsRenderingContextWin.h | 8 +++ 17 files changed, 292 insertions(+), 211 deletions(-) diff --git a/gfx/src/mac/nsFontMetricsMac.cpp b/gfx/src/mac/nsFontMetricsMac.cpp index bbe46c431049..b48e9cdbf5f2 100644 --- a/gfx/src/mac/nsFontMetricsMac.cpp +++ b/gfx/src/mac/nsFontMetricsMac.cpp @@ -310,85 +310,6 @@ nsFontMetricsMac :: GetUnderline(nscoord& aOffset, nscoord& aSize) return NS_OK; } -NS_IMETHODIMP nsFontMetricsMac :: GetWidth(char ch, nscoord &aWidth) -{ - char buf[1]; - buf[0] = ch; - return GetWidth(buf, 1, aWidth); -} - -NS_IMETHODIMP nsFontMetricsMac :: GetWidth(PRUnichar ch, nscoord &aWidth) -{ - PRUnichar buf[1]; - buf[0] = ch; - return GetWidth(buf, 1, aWidth); -} - -NS_IMETHODIMP nsFontMetricsMac :: GetWidth(const nsString& aString, nscoord &aWidth) -{ - return GetWidth(aString.GetUnicode(), aString.Length(), aWidth); -} - -NS_IMETHODIMP nsFontMetricsMac :: GetWidth(const char *aString, nscoord &aWidth) -{ - return GetWidth(aString, strlen(aString), aWidth); -} - -NS_IMETHODIMP -nsFontMetricsMac :: GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth) -{ - if (nsnull == mContext) - { - aWidth = 0; - return NS_ERROR_NULL_POINTER; - } - - if (mFont != nsnull) - nsFontMetricsMac::SetFont(*mFont, mContext); - - float dev2app; - mContext->GetDevUnitsToAppUnits(dev2app); - - short textWidth = ::TextWidth(aString, 0, aLength); - aWidth = NSToCoordRound(float(textWidth) * dev2app); - - if (mFont != nsnull) { - switch (mFont->style) - { - case NS_FONT_STYLE_ITALIC: - case NS_FONT_STYLE_OBLIQUE: - nscoord aAdvance; - GetMaxAdvance(aAdvance); - aWidth += aAdvance; - break; - } - } - return NS_OK; -} - -NS_IMETHODIMP nsFontMetricsMac :: GetWidth(const PRUnichar *aString, PRUint32 aLength, nscoord &aWidth) -{ - nsString nsStr; - nsStr.SetString(aString, aLength); - char* cStr = nsStr.ToNewCString(); - GetWidth(cStr, aLength, aWidth); - delete[] cStr; - return NS_OK; -} - -NS_IMETHODIMP nsFontMetricsMac :: GetWidth(nsIDeviceContext *aContext, const nsString& aString, nscoord &aWidth) -{ - nsIDeviceContext* saveContext = mContext; - mContext = aContext; - - char* cStr = aString.ToNewCString(); - nsresult res = GetWidth(cStr, aString.Length(), aWidth); - delete[] cStr; - - mContext = saveContext; - return res; -} - NS_IMETHODIMP nsFontMetricsMac :: GetHeight(nscoord &aHeight) { aHeight = mHeight; diff --git a/gfx/src/mac/nsFontMetricsMac.h b/gfx/src/mac/nsFontMetricsMac.h index f9a2e2b729b9..22e7f58f2a4e 100644 --- a/gfx/src/mac/nsFontMetricsMac.h +++ b/gfx/src/mac/nsFontMetricsMac.h @@ -48,13 +48,6 @@ public: NS_IMETHOD GetSubscriptOffset(nscoord& aResult); NS_IMETHOD GetStrikeout(nscoord& aOffset, nscoord& aSize); NS_IMETHOD GetUnderline(nscoord& aOffset, nscoord& aSize); - NS_IMETHOD GetWidth(char aC, nscoord &aWidth); - NS_IMETHOD GetWidth(PRUnichar aC, nscoord &aWidth); - NS_IMETHOD GetWidth(const nsString& aString, nscoord &aWidth); - NS_IMETHOD GetWidth(const char *aString, nscoord &aWidth); - NS_IMETHOD GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth); - NS_IMETHOD GetWidth(const PRUnichar *aString, PRUint32 aLength, nscoord &aWidth); - NS_IMETHOD GetWidth(nsIDeviceContext *aContext, const nsString& aString, nscoord &aWidth); NS_IMETHOD GetHeight(nscoord &aHeight); NS_IMETHOD GetLeading(nscoord &aLeading); NS_IMETHOD GetMaxAscent(nscoord &aAscent); diff --git a/gfx/src/mac/nsRenderingContextMac.cpp b/gfx/src/mac/nsRenderingContextMac.cpp index 251e333eb0dc..deb2f4a0d5a4 100644 --- a/gfx/src/mac/nsRenderingContextMac.cpp +++ b/gfx/src/mac/nsRenderingContextMac.cpp @@ -547,15 +547,15 @@ void nsRenderingContextMac :: SetFont(nsIFontMetrics *aFontMetrics) if (mFontMetrics) { - //nsFont font; + nsFont *font; //XXX this is incredibly hokey. nothing should really //be done with the fontmetrics passed in (either here or in //SetFont() above) until you want to actually *use* the font //for somethin. MMP - //mFontMetrics->GetFont(&font); - //nsFontMetricsMac::SetFont(font, mContext); + mFontMetrics->GetFont(font); + nsFontMetricsMac::SetFont(*font, mContext); } } @@ -886,6 +886,77 @@ Rect therect; //------------------------------------------------------------------------ +NS_IMETHODIMP nsRenderingContextMac :: GetWidth(char ch, nscoord &aWidth) +{ + char buf[1]; + buf[0] = ch; + return GetWidth(buf, 1, aWidth); +} + +//------------------------------------------------------------------------ + +NS_IMETHODIMP nsRenderingContextMac :: GetWidth(PRUnichar ch, nscoord &aWidth) +{ + PRUnichar buf[1]; + buf[0] = ch; + return GetWidth(buf, 1, aWidth); +} + +//------------------------------------------------------------------------ + +NS_IMETHODIMP nsRenderingContextMac :: GetWidth(const nsString& aString, nscoord &aWidth) +{ + return GetWidth(aString.GetUnicode(), aString.Length(), aWidth); +} + +//------------------------------------------------------------------------ + +NS_IMETHODIMP nsRenderingContextMac :: GetWidth(const char *aString, nscoord &aWidth) +{ + return GetWidth(aString, strlen(aString), aWidth); +} + +//------------------------------------------------------------------------ + +NS_IMETHODIMP +nsRenderingContextMac :: GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth) +{ + nsFont *font; + + short textWidth = ::TextWidth(aString, 0, aLength); + aWidth = NSToCoordRound(float(textWidth) * mP2T); + + mFontMetrics->GetFont(font); + + if (font != nsnull) { + switch (font->style) + { + case NS_FONT_STYLE_ITALIC: + case NS_FONT_STYLE_OBLIQUE: + nscoord aAdvance; + mFontMetrics->GetMaxAdvance(aAdvance); + aWidth += aAdvance; + break; + } + } + + return NS_OK; +} + +//------------------------------------------------------------------------ + +NS_IMETHODIMP nsRenderingContextMac :: GetWidth(const PRUnichar *aString, PRUint32 aLength, nscoord &aWidth) +{ + nsString nsStr; + nsStr.SetString(aString, aLength); + char* cStr = nsStr.ToNewCString(); + GetWidth(cStr, aLength, aWidth); + delete[] cStr; + return NS_OK; +} + +//------------------------------------------------------------------------ + void nsRenderingContextMac :: DrawString(const char *aString, PRUint32 aLength, nscoord aX, nscoord aY, nscoord aWidth) diff --git a/gfx/src/mac/nsRenderingContextMac.h b/gfx/src/mac/nsRenderingContextMac.h index 3b0068f156af..4ad5becd3c31 100644 --- a/gfx/src/mac/nsRenderingContextMac.h +++ b/gfx/src/mac/nsRenderingContextMac.h @@ -99,6 +99,12 @@ public: virtual void DrawArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,float aStartAngle, float aEndAngle); virtual void FillArc(const nsRect& aRect,float aStartAngle, float aEndAngle); virtual void FillArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,float aStartAngle, float aEndAngle); + NS_IMETHOD GetWidth(char aC, nscoord &aWidth); + NS_IMETHOD GetWidth(PRUnichar aC, nscoord &aWidth); + NS_IMETHOD GetWidth(const nsString& aString, nscoord &aWidth); + NS_IMETHOD GetWidth(const char *aString, nscoord &aWidth); + NS_IMETHOD GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth); + NS_IMETHOD GetWidth(const PRUnichar *aString, PRUint32 aLength, nscoord &aWidth); virtual void DrawString(const char *aString, PRUint32 aLength,nscoord aX, nscoord aY,nscoord aWidth); virtual void DrawString(const PRUnichar *aString, PRUint32 aLength, nscoord aX, nscoord aY,nscoord aWidth); virtual void DrawString(const nsString& aString, nscoord aX, nscoord aY,nscoord aWidth); diff --git a/gfx/src/motif/nsFontMetricsUnix.cpp b/gfx/src/motif/nsFontMetricsUnix.cpp index e3bfd798c181..b51980446d3a 100644 --- a/gfx/src/motif/nsFontMetricsUnix.cpp +++ b/gfx/src/motif/nsFontMetricsUnix.cpp @@ -33,8 +33,6 @@ nsFontMetricsUnix :: nsFontMetricsUnix() mFont = nsnull; mFontHandle = nsnull; mFontInfo = nsnull; - mXstring = nsnull; - mXstringSize = 0; } nsFontMetricsUnix :: ~nsFontMetricsUnix() @@ -44,9 +42,6 @@ nsFontMetricsUnix :: ~nsFontMetricsUnix() mFont = nsnull; } - if (nsnull != mXstring) - PR_Free(mXstring); - if (nsnull != mFontHandle) { nsNativeWidget widget; mContext->GetNativeWidget(widget); @@ -309,80 +304,6 @@ nsFontMetricsUnix :: GetUnderline(nscoord& aOffset, nscoord& aSize) return NS_OK; } -NS_IMETHODIMP nsFontMetricsUnix :: GetWidth(char ch, nscoord &aWidth) -{ - char buf[1]; - buf[0] = ch; - return GetWidth(buf, 1, aWidth); -} - -NS_IMETHODIMP nsFontMetricsUnix :: GetWidth(PRUnichar ch, nscoord &aWidth) -{ - PRUnichar buf[1]; - buf[0] = ch; - return GetWidth(buf, 1, aWidth); -} - -NS_IMETHODIMP nsFontMetricsUnix :: GetWidth(const nsString& aString, nscoord &aWidth) -{ - return GetWidth(aString.GetUnicode(), aString.Length(), aWidth); -} - -NS_IMETHODIMP nsFontMetricsUnix :: GetWidth(const char *aString, nscoord &aWidth) -{ - return GetWidth(aString, strlen(aString), aWidth); -} - -NS_IMETHODIMP nsFontMetricsUnix :: GetWidth(const char *aString, - PRUint32 aLength, nscoord &aWidth) -{ - PRInt32 rc = 0 ; - - rc = (PRInt32) ::XTextWidth(mFontInfo, aString, aLength); - - float dev2app; - mContext->GetDevUnitsToAppUnits(dev2app); - aWidth = nscoord(rc * dev2app); - return NS_OK; -} - -NS_IMETHODIMP nsFontMetricsUnix :: GetWidth(const PRUnichar *aString, - PRUint32 aLength, - nscoord &aWidth) -{ - XChar2b * thischar ; - PRUint16 aunichar; - nscoord width ; - PRUint32 i ; - PRUint32 desiredSize = sizeof(XChar2b) * aLength; - - // Make the temporary buffer larger if needed. - if (nsnull == mXstring) { - mXstring = (XChar2b *) PR_Malloc(desiredSize); - mXstringSize = desiredSize; - } - else { - if (mXstringSize < desiredSize) { - mXstring = (XChar2b *) PR_Realloc(mXstring, desiredSize); - mXstringSize = desiredSize; - } - } - - for (i=0; ibyte2 = (aunichar & 0xff); - thischar->byte1 = (aunichar & 0xff00) >> 8; - } - - width = ::XTextWidth16(mFontInfo, mXstring, aLength); - - float dev2app; - mContext->GetDevUnitsToAppUnits(dev2app); - aWidth = nscoord(width * dev2app); - return NS_OK; -} - NS_IMETHODIMP nsFontMetricsUnix :: GetHeight(nscoord &aHeight) { aHeight = mHeight; diff --git a/gfx/src/motif/nsFontMetricsUnix.h b/gfx/src/motif/nsFontMetricsUnix.h index a1b761bc6f1d..d94b1f716fe5 100644 --- a/gfx/src/motif/nsFontMetricsUnix.h +++ b/gfx/src/motif/nsFontMetricsUnix.h @@ -50,12 +50,6 @@ public: NS_IMETHOD GetSubscriptOffset(nscoord& aResult); NS_IMETHOD GetStrikeout(nscoord& aOffset, nscoord& aSize); NS_IMETHOD GetUnderline(nscoord& aOffset, nscoord& aSize); - NS_IMETHOD GetWidth(char aC, nscoord &aWidth); - NS_IMETHOD GetWidth(PRUnichar aC, nscoord &aWidth); - NS_IMETHOD GetWidth(const nsString& aString, nscoord &aWidth); - NS_IMETHOD GetWidth(const char *aString, nscoord &aWidth); - NS_IMETHOD GetWidth(const char *aString, PRUint32 aLength, nscoord &aWidth); - NS_IMETHOD GetWidth(const PRUnichar *aString, PRUint32 aLength, nscoord &aWidth); NS_IMETHOD GetHeight(nscoord &aHeight); NS_IMETHOD GetLeading(nscoord &aLeading); @@ -85,9 +79,6 @@ protected: nscoord mMaxAscent; nscoord mMaxDescent; nscoord mMaxAdvance; - - XChar2b * mXstring; - PRUint32 mXstringSize; }; #endif diff --git a/gfx/src/motif/nsRenderingContextUnix.cpp b/gfx/src/motif/nsRenderingContextUnix.cpp index 4b21180a0d5e..b54f82e7bda7 100644 --- a/gfx/src/motif/nsRenderingContextUnix.cpp +++ b/gfx/src/motif/nsRenderingContextUnix.cpp @@ -1038,6 +1038,83 @@ void nsRenderingContextUnix :: FillArc(nscoord aX, nscoord aY, nscoord aWidth, n NSToIntRound(aEndAngle * 64.0f)); } +NS_IMETHODIMP nsRenderingContextUnix :: GetWidth(char ch, nscoord &aWidth) +{ + char buf[1]; + buf[0] = ch; + return GetWidth(buf, 1, aWidth); +} + +NS_IMETHODIMP nsRenderingContextUnix :: GetWidth(PRUnichar ch, nscoord &aWidth) +{ + PRUnichar buf[1]; + buf[0] = ch; + return GetWidth(buf, 1, aWidth); +} + +NS_IMETHODIMP nsRenderingContextUnix :: GetWidth(const nsString& aString, nscoord &aWidth) +{ + return GetWidth(aString.GetUnicode(), aString.Length(), aWidth); +} + +NS_IMETHODIMP nsRenderingContextUnix :: GetWidth(const char *aString, nscoord &aWidth) +{ + return GetWidth(aString, strlen(aString), aWidth); +} + +NS_IMETHODIMP nsRenderingContextUnix :: GetWidth(const char *aString, + PRUint32 aLength, nscoord &aWidth) +{ + PRInt32 rc; + XFontStruct *font; + + font = ::XQueryFont(mRenderingSurface->display, (Font)mCurrFontHandle); + rc = (PRInt32) ::XTextWidth(font, aString, aLength); + aWidth = nscoord(rc * mP2T); + + return NS_OK; +} + +NS_IMETHODIMP nsRenderingContextUnix :: GetWidth(const PRUnichar *aString, + PRUint32 aLength, + nscoord &aWidth) +{ + XChar2b * thischar ; + PRUint16 aunichar; + nscoord width ; + PRUint32 i ; + PRUint32 desiredSize = sizeof(XChar2b) * aLength; + XFontStruct *font; + + // Make the temporary buffer larger if needed. + if (nsnull == mDrawStringBuf) { + mDrawStringBuf = (XChar2b *) PR_Malloc(aLength); + mDrawStringSize = aLength; + } + else { + if (mDrawStringSize < PRInt32(aLength)) { + mDrawStringBuf = (XChar2b *) PR_Realloc(mDrawStringBuf, aLength); + mDrawStringSize = aLength; + } + } + + // Translate the unicode data into XChar2b's + XChar2b* xc = mDrawStringBuf; + XChar2b* end = xc + aLength; + while (xc < end) { + PRUnichar ch = *aString++; + xc->byte2 = (ch & 0xff); + xc->byte1 = (ch & 0xff00) >> 8; + xc++; + } + + font = ::XQueryFont(mRenderingSurface->display, (Font)mCurrFontHandle); + width = ::XTextWidth16(font, mDrawStringBuf, aLength); + aWidth = nscoord(width * mP2T); + + return NS_OK; +} + void nsRenderingContextUnix :: DrawString(const char *aString, PRUint32 aLength, nscoord aX, nscoord aY, nscoord aWidth) diff --git a/gfx/src/motif/nsRenderingContextUnix.h b/gfx/src/motif/nsRenderingContextUnix.h index aadd34e92712..5042f4fd84de 100644 --- a/gfx/src/motif/nsRenderingContextUnix.h +++ b/gfx/src/motif/nsRenderingContextUnix.h @@ -127,6 +127,13 @@ public: virtual void FillArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight, float aStartAngle, float aEndAngle); + NS_IMETHOD GetWidth(char aC, nscoord &aWidth); + NS_IMETHOD GetWidth(PRUnichar aC, nscoord &aWidth); + NS_IMETHOD GetWidth(const nsString& aString, nscoord &aWidth); + NS_IMETHOD GetWidth(const char *aString, nscoord &aWidth); + NS_IMETHOD GetWidth(const char *aString, PRUint32 aLength, nscoord &aWidth); + NS_IMETHOD GetWidth(const PRUnichar *aString, PRUint32 aLength, nscoord &aWidth); + virtual void DrawString(const char *aString, PRUint32 aLength, nscoord aX, nscoord aY, nscoord aWidth); diff --git a/gfx/src/nsDeviceContext.cpp b/gfx/src/nsDeviceContext.cpp index eb30cb12e3f2..2b5cc9f6174a 100644 --- a/gfx/src/nsDeviceContext.cpp +++ b/gfx/src/nsDeviceContext.cpp @@ -183,6 +183,28 @@ NS_IMETHODIMP DeviceContextImpl :: CreateRenderingContext(nsIView *aView, nsIRen return rv; } +NS_IMETHODIMP DeviceContextImpl :: CreateRenderingContext(nsIWidget *aWidget, nsIRenderingContext *&aContext) +{ + nsIRenderingContext *pContext; + nsresult rv; + + static NS_DEFINE_IID(kRCCID, NS_RENDERING_CONTEXT_CID); + static NS_DEFINE_IID(kRCIID, NS_IRENDERING_CONTEXT_IID); + + aContext = nsnull; + rv = nsRepository::CreateInstance(kRCCID, nsnull, kRCIID, (void **)&pContext); + + if (NS_OK == rv) { + rv = InitRenderingContext(pContext, aWidget); + if (NS_OK != rv) { + NS_RELEASE(pContext); + } + } + + aContext = pContext; + return rv; +} + NS_IMETHODIMP DeviceContextImpl :: InitRenderingContext(nsIRenderingContext *aContext, nsIWidget *aWin) { return (aContext->Init(this, aWin)); diff --git a/gfx/src/nsDeviceContext.h b/gfx/src/nsDeviceContext.h index 0add4d7ae575..af3889c3e579 100644 --- a/gfx/src/nsDeviceContext.h +++ b/gfx/src/nsDeviceContext.h @@ -36,6 +36,7 @@ public: NS_IMETHOD Init(nsNativeWidget aWidget); NS_IMETHOD CreateRenderingContext(nsIView *aView, nsIRenderingContext *&aContext); + NS_IMETHOD CreateRenderingContext(nsIWidget *aWidget, nsIRenderingContext *&aContext); NS_IMETHOD InitRenderingContext(nsIRenderingContext *aContext, nsIWidget *aWindow); NS_IMETHOD GetDevUnitsToTwips(float &aDevUnitsToTwips) const; diff --git a/gfx/src/nsIDeviceContext.h b/gfx/src/nsIDeviceContext.h index d7d974f99355..eb5f05defaaa 100644 --- a/gfx/src/nsIDeviceContext.h +++ b/gfx/src/nsIDeviceContext.h @@ -68,6 +68,7 @@ public: NS_IMETHOD Init(nsNativeWidget aWidget) = 0; NS_IMETHOD CreateRenderingContext(nsIView *aView, nsIRenderingContext *&aContext) = 0; + NS_IMETHOD CreateRenderingContext(nsIWidget *aWidget, nsIRenderingContext *&aContext) = 0; NS_IMETHOD InitRenderingContext(nsIRenderingContext *aContext, nsIWidget *aWindow) = 0; //these are queries to figure out how large an output unit diff --git a/gfx/src/nsIFontMetrics.h b/gfx/src/nsIFontMetrics.h index bd36ba9d8380..2331789e65ac 100644 --- a/gfx/src/nsIFontMetrics.h +++ b/gfx/src/nsIFontMetrics.h @@ -56,8 +56,7 @@ public: /** * Destroy this font metrics. This breaks the association between - * the font metrics and the device context (which means that calls - * to GetWidth following this call will fail). + * the font metrics and the device context. */ NS_IMETHOD Destroy() = 0; @@ -94,38 +93,6 @@ public: */ NS_IMETHOD GetUnderline(nscoord& aOffset, nscoord& aSize) = 0; - /** - * Returns the width (in app units) of an 8-bit character - */ - NS_IMETHOD GetWidth(char aC, nscoord &aWidth) = 0; - - /** - * Returns the width (in app units) of a unicode character - */ - NS_IMETHOD GetWidth(PRUnichar aC, nscoord &aWidth) = 0; - - /** - * Returns the width (in app units) of an nsString - */ - NS_IMETHOD GetWidth(const nsString& aString, nscoord &aWidth) = 0; - - /** - * Returns the width (in app units) of an 8-bit character string - */ - NS_IMETHOD GetWidth(const char* aString, nscoord& aWidth) = 0; - - /** - * Returns the width (in app units) of an 8-bit character string - */ - NS_IMETHOD GetWidth(const char* aString, PRUint32 aLength, - nscoord& aWidth) = 0; - - /** - * Returns the width (in app units) of a Unicode character string - */ - NS_IMETHOD GetWidth(const PRUnichar *aString, PRUint32 aLength, - nscoord &aWidth) = 0; - /** * Returns the height (in app units) of the font. This is ascent plus descent * plus any internal leading diff --git a/gfx/src/nsIRenderingContext.h b/gfx/src/nsIRenderingContext.h index fae6891a12fb..3e0f4ed503ec 100644 --- a/gfx/src/nsIRenderingContext.h +++ b/gfx/src/nsIRenderingContext.h @@ -377,6 +377,38 @@ public: virtual void FillArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight, float aStartAngle, float aEndAngle) = 0; + /** + * Returns the width (in app units) of an 8-bit character + */ + NS_IMETHOD GetWidth(char aC, nscoord &aWidth) = 0; + + /** + * Returns the width (in app units) of a unicode character + */ + NS_IMETHOD GetWidth(PRUnichar aC, nscoord &aWidth) = 0; + + /** + * Returns the width (in app units) of an nsString + */ + NS_IMETHOD GetWidth(const nsString& aString, nscoord &aWidth) = 0; + + /** + * Returns the width (in app units) of an 8-bit character string + */ + NS_IMETHOD GetWidth(const char* aString, nscoord& aWidth) = 0; + + /** + * Returns the width (in app units) of an 8-bit character string + */ + NS_IMETHOD GetWidth(const char* aString, PRUint32 aLength, + nscoord& aWidth) = 0; + + /** + * Returns the width (in app units) of a Unicode character string + */ + NS_IMETHOD GetWidth(const PRUnichar *aString, PRUint32 aLength, + nscoord &aWidth) = 0; + /** * Draw a string in the RenderingContext * @param aString The string to draw diff --git a/gfx/src/windows/nsFontMetricsWin.cpp b/gfx/src/windows/nsFontMetricsWin.cpp index 59e7f770433a..dc8af00464a4 100644 --- a/gfx/src/windows/nsFontMetricsWin.cpp +++ b/gfx/src/windows/nsFontMetricsWin.cpp @@ -294,6 +294,8 @@ nsFontMetricsWin :: GetUnderline(nscoord& aOffset, nscoord& aSize) return NS_OK; } +#if 0 + NS_IMETHODIMP nsFontMetricsWin :: GetWidth(char ch, nscoord& aWidth) { @@ -374,6 +376,8 @@ nsFontMetricsWin :: GetWidth(const PRUnichar *aString, return NS_OK; } +#endif + NS_IMETHODIMP nsFontMetricsWin :: GetHeight(nscoord &aHeight) { diff --git a/gfx/src/windows/nsFontMetricsWin.h b/gfx/src/windows/nsFontMetricsWin.h index 6462d5dcc93d..c11d23319063 100644 --- a/gfx/src/windows/nsFontMetricsWin.h +++ b/gfx/src/windows/nsFontMetricsWin.h @@ -50,6 +50,7 @@ public: NS_IMETHOD GetSubscriptOffset(nscoord& aResult); NS_IMETHOD GetStrikeout(nscoord& aOffset, nscoord& aSize); NS_IMETHOD GetUnderline(nscoord& aOffset, nscoord& aSize); +#if 0 NS_IMETHOD GetWidth(char aC, nscoord& aWidth); NS_IMETHOD GetWidth(PRUnichar aC, nscoord& aWidth); NS_IMETHOD GetWidth(const nsString& aString, nscoord& aWidth); @@ -57,6 +58,7 @@ public: NS_IMETHOD GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth); NS_IMETHOD GetWidth(const PRUnichar* aString, PRUint32 aLength, nscoord& aWidth); +#endif NS_IMETHOD GetHeight(nscoord &aHeight); NS_IMETHOD GetLeading(nscoord &aLeading); diff --git a/gfx/src/windows/nsRenderingContextWin.cpp b/gfx/src/windows/nsRenderingContextWin.cpp index 9e851f120c3b..0a17ffd8c654 100644 --- a/gfx/src/windows/nsRenderingContextWin.cpp +++ b/gfx/src/windows/nsRenderingContextWin.cpp @@ -1260,6 +1260,63 @@ void nsRenderingContextWin :: FillArc(nscoord aX, nscoord aY, nscoord aWidth, ns ::Pie(mDC, aX, aY, aX + aWidth, aY + aHeight, sx, sy, ex, ey); } + +NS_IMETHODIMP +nsRenderingContextWin :: GetWidth(char ch, nscoord& aWidth) +{ + char buf[1]; + buf[0] = ch; + return GetWidth(buf, 1, aWidth); +} + +NS_IMETHODIMP +nsRenderingContextWin :: GetWidth(PRUnichar ch, nscoord &aWidth) +{ + PRUnichar buf[1]; + buf[0] = ch; + return GetWidth(buf, 1, aWidth); +} + +NS_IMETHODIMP +nsRenderingContextWin :: GetWidth(const char* aString, nscoord& aWidth) +{ + return GetWidth(aString, strlen(aString), aWidth); +} + +NS_IMETHODIMP +nsRenderingContextWin :: GetWidth(const char* aString, + PRUint32 aLength, + nscoord& aWidth) +{ + SIZE size; + + SetupFontAndColor(); + ::GetTextExtentPoint32(mDC, aString, aLength, &size); + aWidth = NSToCoordRound(float(size.cx) * mP2T); + + return NS_OK; +} + +NS_IMETHODIMP +nsRenderingContextWin :: GetWidth(const nsString& aString, nscoord& aWidth) +{ + return GetWidth(aString.GetUnicode(), aString.Length(), aWidth); +} + +NS_IMETHODIMP +nsRenderingContextWin :: GetWidth(const PRUnichar *aString, + PRUint32 aLength, + nscoord &aWidth) +{ + SIZE size; + + SetupFontAndColor(); + ::GetTextExtentPoint32W(mDC, aString, aLength, &size); + aWidth = NSToCoordRound(float(size.cx) * mP2T); + + return NS_OK; +} + void nsRenderingContextWin :: DrawString(const char *aString, PRUint32 aLength, nscoord aX, nscoord aY, nscoord aWidth) diff --git a/gfx/src/windows/nsRenderingContextWin.h b/gfx/src/windows/nsRenderingContextWin.h index 94fdd86c7316..ddbc6c3836f3 100644 --- a/gfx/src/windows/nsRenderingContextWin.h +++ b/gfx/src/windows/nsRenderingContextWin.h @@ -119,6 +119,14 @@ public: virtual void FillArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight, float aStartAngle, float aEndAngle); + NS_IMETHOD GetWidth(char aC, nscoord& aWidth); + NS_IMETHOD GetWidth(PRUnichar aC, nscoord& aWidth); + NS_IMETHOD GetWidth(const nsString& aString, nscoord& aWidth); + NS_IMETHOD GetWidth(const char* aString, nscoord& aWidth); + NS_IMETHOD GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth); + NS_IMETHOD GetWidth(const PRUnichar* aString, PRUint32 aLength, + nscoord& aWidth); + virtual void DrawString(const char *aString, PRUint32 aLength, nscoord aX, nscoord aY, nscoord aWidth);