[gnome-disk-utility/gnome-3-16] Fix font rendering in benchmark dialog



commit 6cf5a8616cd0f339f803c2a6de29b26d980bceef
Author: David King <dking redhat com>
Date:   Tue Jun 2 11:03:31 2015 +0100

    Fix font rendering in benchmark dialog
    
    Use Pango to render text around the benchmark graph, rather than the toy
    Cairo text API.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=656864

 src/disks/gdubenchmarkdialog.c |   65 ++++++++++++++++++++++++++-------------
 1 files changed, 43 insertions(+), 22 deletions(-)
---
diff --git a/src/disks/gdubenchmarkdialog.c b/src/disks/gdubenchmarkdialog.c
index e47d7b6..01f68d6 100644
--- a/src/disks/gdubenchmarkdialog.c
+++ b/src/disks/gdubenchmarkdialog.c
@@ -248,6 +248,11 @@ on_drawing_area_draw (GtkWidget      *widget,
   gdouble access_time_max = 0.0;
   gdouble prev_x;
   gdouble prev_y;
+  GtkStyleContext *context;
+  PangoFontDescription *font_desc;
+  gint size;
+  GdkRGBA fg;
+  PangoLayout *layout;
 
   G_LOCK (bm_lock);
 
@@ -342,9 +347,6 @@ on_drawing_area_draw (GtkWidget      *widget,
   width = allocation.width;
   height = allocation.height;
 
-  cairo_select_font_face (cr, "sans",
-                          CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
-  cairo_set_font_size (cr, 8.0);
   cairo_set_line_width (cr, 1.0);
 
 #if 0
@@ -409,23 +411,38 @@ on_drawing_area_draw (GtkWidget      *widget,
       gh -= needed;
     }
 
+  context = gtk_widget_get_style_context (widget);
+  gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &fg);
+  gtk_style_context_get (context,
+                         GTK_STATE_FLAG_NORMAL,
+                         GTK_STYLE_PROPERTY_FONT,
+                         &font_desc,
+                         NULL);
+  size = pango_font_description_get_size (font_desc);
+  if (pango_font_description_get_size_is_absolute (font_desc))
+    size *= PANGO_SCALE;
+  pango_font_description_set_size (font_desc, PANGO_SCALE_X_SMALL * size);
+  layout = pango_cairo_create_layout (cr);
+  pango_layout_set_font_description (layout, font_desc);
+  pango_font_description_free (font_desc);
+
   /* draw x markers ("%d%%") + vertical grid */
   for (n = 0; n <= 10; n++)
     {
-      cairo_text_extents_t te;
+      PangoRectangle extents;
 
       x = gx + ceil (n * gw / 10.0);
       y = gy + gh + x_marker_height/2.0;
 
       s = g_strdup_printf ("%d%%", n * 10);
 
-      cairo_text_extents (cr, s, &te);
-
+      pango_layout_set_text (layout, s, -1);
+      pango_layout_get_extents (layout, NULL, &extents);
       cairo_move_to (cr,
-                     x - te.x_bearing - te.width/2,
-                     y - te.y_bearing - te.height/2);
-      cairo_set_source_rgb (cr, 0, 0, 0);
-      cairo_show_text (cr, s);
+                     x - extents.width/PANGO_SCALE/2,
+                     y - extents.height/PANGO_SCALE/2);
+      gdk_cairo_set_source_rgba (cr, &fg);
+      pango_cairo_show_layout (cr, layout);
 
       g_free (s);
     }
@@ -433,37 +450,41 @@ on_drawing_area_draw (GtkWidget      *widget,
   /* draw left y markers ("%d MB/s") */
   for (n = 0; n <= num_y_markers; n++)
     {
-      cairo_text_extents_t te;
+      PangoRectangle extents;
 
       x = gx/2.0;
       y = gy + gh - gh * n / num_y_markers;
 
       s = y_left_markers[n];
-      cairo_text_extents (cr, s, &te);
+      pango_layout_set_text (layout, s, -1);
+      pango_layout_get_extents (layout, NULL, &extents);
       cairo_move_to (cr,
-                     x - te.x_bearing - te.width/2,
-                     y - te.y_bearing - te.height/2);
-      cairo_set_source_rgb (cr, 0, 0, 0);
-      cairo_show_text (cr, s);
+                     x - extents.width/PANGO_SCALE/2,
+                     y - extents.height/PANGO_SCALE/2);
+      gdk_cairo_set_source_rgba (cr, &fg);
+      pango_cairo_show_layout (cr, layout);
     }
 
   /* draw right y markers ("%d ms") */
   for (n = 0; n <= num_y_markers; n++)
     {
-      cairo_text_extents_t te;
+      PangoRectangle extents;
 
       x = gx + gw + (width - (gx + gw))/2.0;
       y = gy + gh - gh * n / num_y_markers;
 
       s = y_right_markers[n];
-      cairo_text_extents (cr, s, &te);
+      pango_layout_set_text (layout, s, -1);
+      pango_layout_get_extents (layout, NULL, &extents);
       cairo_move_to (cr,
-                     x - te.x_bearing - te.width/2,
-                     y - te.y_bearing - te.height/2);
-      cairo_set_source_rgb (cr, 0, 0, 0);
-      cairo_show_text (cr, s);
+                     x - extents.width/PANGO_SCALE/2,
+                     y - extents.height/PANGO_SCALE/2);
+      gdk_cairo_set_source_rgba (cr, &fg);
+      pango_cairo_show_layout (cr, layout);
     }
 
+  g_object_unref (layout);
+
   /* fill graph area */
   cairo_set_source_rgb (cr, 1, 1, 1);
   cairo_rectangle (cr, gx + 0.5, gy + 0.5, gw, gh);


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