[gnome-chess] Use pango to render clock text



commit c54acfcb178d5df245cf69a7c7a47d74db0fba19
Author: Michael Catanzaro <mcatanzaro redhat com>
Date:   Tue Aug 9 15:03:12 2022 -0500

    Use pango to render clock text
    
    The cairo toy text API is not really suitable for rendering more than a
    single character of ASCII text. I'm not sure when this broke, but it's
    currently failing to render both the ratio character that we use, and
    also the left-to-right mark that's supposed to be invisible. Better
    switch to something smarter, and pango is the answer.

 meson.build           |  1 +
 src/chess-window.vala | 27 ++++++++++++++++++++-------
 src/meson.build       |  1 +
 3 files changed, 22 insertions(+), 7 deletions(-)
---
diff --git a/meson.build b/meson.build
index 31c8635..2e08116 100644
--- a/meson.build
+++ b/meson.build
@@ -35,6 +35,7 @@ gmodule = dependency('gmodule-2.0', version: '>=' + min_glib_version)
 gtk = dependency('gtk4')
 libadwaita = dependency('libadwaita-1', version: '>= 1.0.0.alpha.3')
 librsvg = dependency('librsvg-2.0', version: '>= 2.46.0')
+pangocairo = dependency('pangocairo')
 
 posix = meson.get_compiler('vala').find_library('posix')
 
diff --git a/src/chess-window.vala b/src/chess-window.vala
index 208d721..d957837 100644
--- a/src/chess-window.vala
+++ b/src/chess-window.vala
@@ -247,14 +247,26 @@ public class ChessWindow : Gtk.ApplicationWindow
         c.set_source_rgba (bg[0], bg[1], bg[2], alpha);
         c.paint ();
 
+        var pango_context = Pango.cairo_create_context (c);
+        Pango.cairo_context_set_font_options (pango_context, get_font_options ());
+        var layout = new Pango.Layout (pango_context);
+        layout.set_font_description (Pango.FontDescription.from_string ("Sans Bold 14"));
+        layout.set_text (text, -1);
+
+        // https://docs.microsoft.com/en-us/typography/opentype/spec/features_pt#tnum
+        var attributes = new Pango.AttrList ();
+        attributes.insert (new Pango.AttrFontFeatures ("tnum=1"));
+        layout.set_attributes (attributes);
+
+        int layout_width;
+        int layout_height;
+        layout.get_size (out layout_width, out layout_height);
+        layout_width /= Pango.SCALE;
+        layout_height /= Pango.SCALE;
+        c.move_to ((widget.get_allocated_width () - layout_width) / 2,
+                   (widget.get_allocated_height () - layout_height) / 2);
         c.set_source_rgba (fg[0], fg[1], fg[2], alpha);
-        c.select_font_face ("fixed", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
-        c.set_font_size (0.6 * widget.get_allocated_height ());
-        Cairo.TextExtents extents;
-        c.text_extents (text, out extents);
-        c.move_to ((widget.get_allocated_width () - extents.width) / 2 - extents.x_bearing,
-                   (widget.get_allocated_height () - extents.height) / 2 - extents.y_bearing);
-        c.show_text (text);
+        Pango.cairo_show_layout (c, layout);
     }
 
     private string make_clock_text (ChessClock? clock, Color color)
@@ -266,6 +278,7 @@ public class ChessWindow : Gtk.ApplicationWindow
         else
             time = clock.black_remaining_seconds;
 
+        // E2 80 8E is the left-to-right mark.
         if (time >= 60)
             return "%d∶\xE2\x80\x8E%02d".printf (time / 60, time % 60);
         else
diff --git a/src/meson.build b/src/meson.build
index d59af83..dc751ff 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -24,6 +24,7 @@ chess_deps = [
   gtk,
   libadwaita,
   librsvg,
+  pangocairo,
   posix,
   libchess_dep,
   libengine_dep,


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