[gnome-sudoku/mcatanzaro/print-rtl] Use pango to format sudoku titles when printing




commit ad4b4323af714d7aa56891f1ff5edef062cdeab7
Author: Michael Catanzaro <mcatanzaro redhat com>
Date:   Thu Jul 22 15:35:31 2021 -0500

    Use pango to format sudoku titles when printing
    
    Currently they are printed using cairo's toy text API, which is good
    enough to print individual numbers in our Sudoku grid, but not nearly
    good enough to handle internationalized complex text. We can fix this by
    switching to Pango instead.
    
    Fixes #52

 src/sudoku-printer.vala | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)
---
diff --git a/src/sudoku-printer.vala b/src/sudoku-printer.vala
index 7b5fcbf..1e0b192 100644
--- a/src/sudoku-printer.vala
+++ b/src/sudoku-printer.vala
@@ -72,6 +72,14 @@ public class SudokuPrinter : GLib.Object {
         operation.set_n_pages (pages);
     }
 
+    private void configure_font_options (Pango.Context context)
+    {
+        var screen = Gdk.Screen.get_default ();
+        unowned Cairo.FontOptions font_options = screen.get_font_options ();
+
+        Pango.cairo_context_set_font_options (context, font_options);
+    }
+
     private void draw_page_cb (PrintOperation operation, PrintContext context, int page_nr)
     {
         Cairo.Context cr = context.get_cairo_context ();
@@ -93,18 +101,29 @@ public class SudokuPrinter : GLib.Object {
 
         uint index = 0;
 
+        var pango_context = Pango.cairo_create_context (cr);
+        configure_font_options (pango_context);
+
         foreach (SudokuBoard sudoku in sudokus_on_page)
         {
             double left = margin_x + (index % n_across) * (best_square_size + margin_x);
             double top = margin_y + label_extents.height + (index / n_across) * (best_square_size + margin_y 
+ label_extents.height);
 
             var label = sudoku.difficulty_category.to_string ();
-            set_label_font (cr);
+            var layout = new Pango.Layout (pango_context);
+            layout.set_font_description (Pango.FontDescription.from_string ("Sans Bold 12"));
+            layout.set_text (label, -1);
+
+            int layout_width;
+            int layout_height;
+            layout.get_size (out layout_width, out layout_height);
+            layout_width /= Pango.SCALE;
+            layout_height /= Pango.SCALE;
+// FIXME off by a little here somewhow
+warning ("width=%d height=%d", layout_width, layout_height);
+            cr.move_to (left + (best_square_size - layout_width) / 2, top - layout_height / 2);
             cr.set_source_rgb (0, 0, 0);
-            Cairo.TextExtents extents;
-            cr.text_extents (label, out extents);
-            cr.move_to (left + (best_square_size - extents.width) / 2, top - extents.height / 2);
-            cr.show_text (label);
+            Pango.cairo_show_layout (cr, layout);
 
             draw_sudoku (cr, sudoku, best_square_size, left, top);
 
@@ -118,6 +137,12 @@ public class SudokuPrinter : GLib.Object {
         cr.select_font_face ("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
     }
 
+    private void set_pango_layout_font (Pango.Layout layout)
+    {
+        var description = ;
+        layout.set_font_description (description);
+    }
+
     private double fit_squares_in_rectangle (double width, double height, double label_height, int margin, 
out uint across, out uint down)
     {
         var n = sudokus_per_page;


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