[balsa/wip/gtk4: 50/351] Use a GtkLabel to measure character width



commit 64e9a5d149ca0b509cbca395fb1cdf96cb309ca0
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Tue Nov 7 11:22:54 2017 -0500

    Use a GtkLabel to measure character width
    
    Some APIs using GdkScreen have been removed. In particular, we cannot
    use gdk_screen_get_resolution to convert from point size to pixels. Use
    the natural width of the string "0" instead.

 src/balsa-mime-widget-text.c |   22 ++++++++++------------
 1 files changed, 10 insertions(+), 12 deletions(-)
---
diff --git a/src/balsa-mime-widget-text.c b/src/balsa-mime-widget-text.c
index fb0fe82..7bbe7d5 100644
--- a/src/balsa-mime-widget-text.c
+++ b/src/balsa-mime-widget-text.c
@@ -1274,17 +1274,14 @@ static GList *
 fill_text_buf_cited(GtkWidget *widget, const gchar *text_body,
                     gboolean is_flowed, gboolean is_plain)
 {
+    GtkWidget *label;
     LibBalsaUrlInsertInfo url_info;
     GList * cite_bars_list;
     guint cite_level;
-    GdkScreen *screen;
     guint cite_start;
     gint margin;
-    gdouble char_width;
     GtkTextTag *invisible;
     GList *url_list = NULL;
-    PangoContext *context = gtk_widget_get_pango_context(widget);
-    PangoFontDescription *desc = pango_context_get_font_description(context);
     GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
     GRegex *rex = NULL;
     gboolean have_regex;
@@ -1297,14 +1294,15 @@ fill_text_buf_cited(GtkWidget *widget, const gchar *text_body,
     } else
         have_regex = FALSE;
 
-    /* width of monospace characters is 3/5 of the size */
-    char_width = 0.6 * pango_font_description_get_size(desc);
-    if (!pango_font_description_get_size_is_absolute(desc))
-        char_width = char_width / PANGO_SCALE;
-
-    /* convert char_width from points to pixels */
-    screen = gtk_widget_get_screen(widget);
-    margin = (char_width / 72.0) * gdk_screen_get_resolution(screen);
+    /* Get the width in pixels of a character in the current font.
+     * These vary widely in a proportionally spaced font, but digits all
+     * have the same width and it matches the previous calculation based
+     * on the PangoFontDescription. */
+    label = gtk_label_new("0");
+    gtk_widget_measure(label, GTK_ORIENTATION_HORIZONTAL,
+                       300, NULL, &margin, NULL, NULL);
+    g_object_ref_sink(label);
+    g_object_unref(label);
 
     rgba = &balsa_app.url_color;
     gtk_text_buffer_create_tag(buffer, "url",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]