From 7cf9ea83cb8ab3d31a57e1969975aa79870fc6fa Mon Sep 17 00:00:00 2001 From: TingPing Date: Sat, 26 Oct 2013 08:51:50 -0400 Subject: xtext: Get proper text width with attributes --- src/fe-gtk/xtext.c | 63 +++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/src/fe-gtk/xtext.c b/src/fe-gtk/xtext.c index 438a8246..48d4ec61 100644 --- a/src/fe-gtk/xtext.c +++ b/src/fe-gtk/xtext.c @@ -162,6 +162,37 @@ gtk_xtext_text_width_8bit (GtkXText *xtext, unsigned char *str, int len) /* ============ PANGO BACKEND ============ */ /* ======================================= */ +static void +xtext_layout_set_text (GtkXText *xtext, char *str, int len) +{ + PangoAttrList *attr_list; + PangoAttribute *attr; + + attr_list = pango_attr_list_new (); + + if (xtext->italics) + { + attr = pango_attr_style_new (PANGO_STYLE_ITALIC); + attr->start_index = PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING; + attr->end_index = PANGO_ATTR_INDEX_TO_TEXT_END; + + pango_attr_list_insert (attr_list, attr); + } + if (xtext->bold) + { + attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD); + attr->start_index = PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING; + attr->end_index = PANGO_ATTR_INDEX_TO_TEXT_END; + + pango_attr_list_insert (attr_list, attr); + } + + pango_layout_set_attributes (xtext->layout, attr_list); + pango_layout_set_text (xtext->layout, str, len); + + pango_attr_list_unref (attr_list); +} + static void backend_font_close (GtkXText *xtext) { @@ -238,13 +269,11 @@ backend_get_text_width (GtkXText *xtext, guchar *str, int len, int is_mb) { int width; - if (!is_mb) - return gtk_xtext_text_width_8bit (xtext, str, len); - if (*str == 0) return 0; - pango_layout_set_text (xtext->layout, str, len); + /* set text w/ attributes for proper width */ + xtext_layout_set_text (xtext, str, len); pango_layout_get_pixel_size (xtext->layout, &width, NULL); return width; @@ -303,30 +332,8 @@ backend_draw_text (GtkXText *xtext, int dofill, GdkGC *gc, int x, int y, GdkGCValues val; GdkColor col; PangoLayoutLine *line; - PangoAttrList *attr_list; - PangoAttribute *attr; - - attr_list = pango_attr_list_new (); - - if (xtext->italics) - { - attr = pango_attr_style_new (PANGO_STYLE_ITALIC); - attr->start_index = PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING; - attr->end_index = PANGO_ATTR_INDEX_TO_TEXT_END; - - pango_attr_list_insert (attr_list, attr); - } - if (xtext->bold) - { - attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD); - attr->start_index = PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING; - attr->end_index = PANGO_ATTR_INDEX_TO_TEXT_END; - pango_attr_list_insert (attr_list, attr); - } - - pango_layout_set_attributes (xtext->layout, attr_list); - pango_layout_set_text (xtext->layout, str, len); + xtext_layout_set_text (xtext, str, len); if (dofill) { @@ -342,8 +349,6 @@ backend_draw_text (GtkXText *xtext, int dofill, GdkGC *gc, int x, int y, line = pango_layout_get_lines (xtext->layout)->data; xtext_draw_layout_line (xtext->draw_buf, gc, x, y, line); - - pango_attr_list_unref (attr_list); } static void -- cgit 1.4.1