From 429ad2de2400a7f913fc2ce00882707b12927f97 Mon Sep 17 00:00:00 2001 From: "blizzard%redhat.com" Date: Tue, 10 Aug 2004 20:57:26 +0000 Subject: [PATCH] Fix problems with pages that include spacing and unicode characters that generated more than one byte length utf-8 characters. --- gfx/src/gtk/nsFontMetricsPango.cpp | 33 +++++++++++++++++++++++++----- gfx/src/gtk/nsFontMetricsPango.h | 4 +++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/gfx/src/gtk/nsFontMetricsPango.cpp b/gfx/src/gtk/nsFontMetricsPango.cpp index 72ffa67d4a6..bd1265f567a 100644 --- a/gfx/src/gtk/nsFontMetricsPango.cpp +++ b/gfx/src/gtk/nsFontMetricsPango.cpp @@ -600,7 +600,7 @@ nsFontMetricsPango::DrawString(const char *aString, PRUint32 aLength, GdkGC *gc = aContext->GetGC(); if (aSpacing && *aSpacing) { - DrawStringSlowly(aString, aLength, aSurface->GetDrawable(), + DrawStringSlowly(aString, NULL, aLength, aSurface->GetDrawable(), gc, x, y, line, aSpacing); } else { @@ -647,7 +647,7 @@ nsFontMetricsPango::DrawString(const PRUnichar* aString, PRUint32 aLength, GdkGC *gc = aContext->GetGC(); if (aSpacing && *aSpacing) { - DrawStringSlowly(text, aLength, aSurface->GetDrawable(), + DrawStringSlowly(text, aString, aLength, aSurface->GetDrawable(), gc, x, y, line, aSpacing); } else { @@ -917,6 +917,7 @@ nsFontMetricsPango::EnumFontCallback(const nsString &aFamily, void nsFontMetricsPango::DrawStringSlowly(const gchar *aText, + const PRUnichar *aOrigString, PRUint32 aLength, GdkDrawable *aDrawable, GdkGC *aGC, gint aX, gint aY, @@ -931,8 +932,29 @@ nsFontMetricsPango::DrawStringSlowly(const gchar *aText, * We walk the list of glyphs returned in each layout run, * matching up the glyphs with the characters in the source text. * We use the aSpacing argument to figure out where to place those - * glyphs. + * glyphs. It's important to note that since the string we're + * working with is in UTF-8 while the spacing argument assumes + * that offset will be part of the UTF-16 string. Logical + * attributes in pango are in byte offsets in the UTF-8 string, so + * we need to store the offsets based on the UTF-8 string. */ + nscoord *spacing = new nscoord[strlen(aText)]; + + if (aOrigString) { + gint outpos = 0; + gunichar c; + gchar outbuf[6]; + const PRUnichar *curpos = aOrigString; + + for (guint i = 0; i < aLength; i++, curpos++) { + spacing[outpos] = aSpacing[i]; + c = *curpos; + outpos += g_unichar_to_utf8(c, outbuf); + } + } + else { + memcpy(spacing, aSpacing, (sizeof(nscoord *) * aLength)); + } gint curRun = 0; @@ -950,8 +972,8 @@ nsFontMetricsPango::DrawStringSlowly(const gchar *aText, "some exciting rendering!"); } - for (gint i; i < layoutRun->glyphs->num_glyphs; i++) { - gint thisOffset = (gint)(aSpacing[layoutRun->glyphs->log_clusters[i]] * app2dev * PANGO_SCALE); + for (gint i=0; i < layoutRun->glyphs->num_glyphs; i++) { + gint thisOffset = (gint)(spacing[layoutRun->glyphs->log_clusters[i]] * app2dev * PANGO_SCALE); layoutRun->glyphs->glyphs[i].geometry.width = thisOffset; tmpOffset += thisOffset; } @@ -964,6 +986,7 @@ nsFontMetricsPango::DrawStringSlowly(const gchar *aText, offset += tmpOffset; } + delete[] spacing; } /* static */ diff --git a/gfx/src/gtk/nsFontMetricsPango.h b/gfx/src/gtk/nsFontMetricsPango.h index b463639411c..d56b0a1ba6b 100644 --- a/gfx/src/gtk/nsFontMetricsPango.h +++ b/gfx/src/gtk/nsFontMetricsPango.h @@ -250,7 +250,9 @@ private: static PRBool EnumFontCallback(const nsString &aFamily, PRBool aIsGeneric, void *aData); - void DrawStringSlowly(const gchar *aText, PRUint32 aLength, + void DrawStringSlowly(const gchar *aText, + const PRUnichar *aOrigString, + PRUint32 aLength, GdkDrawable *aDrawable, GdkGC *aGC, gint aX, gint aY, PangoLayoutLine *aLine,