xtext: Always use Pango to get correct glyph width on Unix

This should be slower but pango seems to be reasonably fast
on Unix though extremely slow on Windows.

https://bugzilla.redhat.com/show_bug.cgi?id=1536298
This commit is contained in:
Peng Wu 2018-02-01 18:52:16 -05:00 committed by Patrick Griffis
parent 4cdf6460b6
commit d3f1ab7813

View File

@ -157,7 +157,10 @@ static char * gtk_xtext_get_word (GtkXText * xtext, int x, int y, textentry ** r
#define EMPH_BOLD 2 #define EMPH_BOLD 2
#define EMPH_HIDDEN 4 #define EMPH_HIDDEN 4
static PangoAttrList *attr_lists[4]; static PangoAttrList *attr_lists[4];
#ifdef G_OS_WIN32
/* the fontwidths variable is used on Windows only. */
static int fontwidths[4][128]; static int fontwidths[4][128];
#endif
static PangoAttribute * static PangoAttribute *
xtext_pango_attr (PangoAttribute *attr) xtext_pango_attr (PangoAttribute *attr)
@ -170,7 +173,10 @@ xtext_pango_attr (PangoAttribute *attr)
static void static void
xtext_pango_init (GtkXText *xtext) xtext_pango_init (GtkXText *xtext)
{ {
int i, j; int i;
#ifdef G_OS_WIN32
int j;
#endif
char buf[2] = "\000"; char buf[2] = "\000";
if (attr_lists[0]) if (attr_lists[0])
@ -202,6 +208,7 @@ xtext_pango_init (GtkXText *xtext)
break; break;
} }
#ifdef G_OS_WIN32
/* Now initialize fontwidths[i] */ /* Now initialize fontwidths[i] */
pango_layout_set_attributes (xtext->layout, attr_lists[i]); pango_layout_set_attributes (xtext->layout, attr_lists[i]);
for (j = 0; j < 128; j++) for (j = 0; j < 128; j++)
@ -210,8 +217,14 @@ xtext_pango_init (GtkXText *xtext)
pango_layout_set_text (xtext->layout, buf, 1); pango_layout_set_text (xtext->layout, buf, 1);
pango_layout_get_pixel_size (xtext->layout, &fontwidths[i][j], NULL); pango_layout_get_pixel_size (xtext->layout, &fontwidths[i][j], NULL);
} }
#endif
} }
xtext->space_width = fontwidths[0][' '];
/* re-compute space_width without using fontwidths */
pango_layout_set_attributes (xtext->layout, attr_lists[0]);
buf[0] = ' ';
pango_layout_set_text (xtext->layout, buf, 1);
pango_layout_get_pixel_size (xtext->layout, &(xtext->space_width), NULL);
} }
static void static void
@ -289,8 +302,10 @@ static int
backend_get_text_width_emph (GtkXText *xtext, guchar *str, int len, int emphasis) backend_get_text_width_emph (GtkXText *xtext, guchar *str, int len, int emphasis)
{ {
int width; int width;
#ifdef G_OS_WIN32
int deltaw; int deltaw;
int mbl; int mbl;
#endif
if (*str == 0) if (*str == 0)
return 0; return 0;
@ -301,6 +316,8 @@ backend_get_text_width_emph (GtkXText *xtext, guchar *str, int len, int emphasis
width = 0; width = 0;
pango_layout_set_attributes (xtext->layout, attr_lists[emphasis]); pango_layout_set_attributes (xtext->layout, attr_lists[emphasis]);
#ifdef G_OS_WIN32
while (len > 0) while (len > 0)
{ {
mbl = charlen(str); mbl = charlen(str);
@ -315,6 +332,12 @@ backend_get_text_width_emph (GtkXText *xtext, guchar *str, int len, int emphasis
str += mbl; str += mbl;
len -= mbl; len -= mbl;
} }
#else
/* This code is slow on Windows,
but it will get the correct width from pango on Linux. */
pango_layout_set_text (xtext->layout, str, len);
pango_layout_get_pixel_size (xtext->layout, &width, NULL);
#endif
return width; return width;
} }