[pango/pango2: 89/168] docs: Sync the cairo example




commit 27f475c225f45becf2765b01c623c545460422d0
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jun 12 09:17:45 2022 -0400

    docs: Sync the cairo example

 docs/pango_cairo.md    |  16 +++++++--------
 docs/rotated-text.png  | Bin 23044 -> 23224 bytes
 examples/cairosimple.c |  52 ++++++++++++++++---------------------------------
 3 files changed, 25 insertions(+), 43 deletions(-)
---
diff --git a/docs/pango_cairo.md b/docs/pango_cairo.md
index e1370918..f4d699f6 100644
--- a/docs/pango_cairo.md
+++ b/docs/pango_cairo.md
@@ -13,9 +13,10 @@ this section allow using Pango to render to Cairo surfaces.
 Using Pango with Cairo is straightforward. A `PangoContext` created with
 [func@Pango.cairo_create_context] can be used on any Cairo context (`cairo_t`),
 but needs to be updated to match the current transformation matrix and target
-surface of the Cairo context using [func@Pango.cairo_update_context]. The
-convenience function [func@Pango.cairo_update_layout] handles the common case
-where the program doesn't need to manipulate the properties of the `PangoContext`.
+surface of the Cairo context using [func@Pango.cairo_update_context].
+The convenience functions [func@Pango.cairo_create_layout] and
+[func@Pango.cairo_update_layout] handle the common case where the program
+doesn't need to manipulate the properties of the `PangoContext`.
 
 When you get the metrics of a layout or of a piece of a layout using functions
 such as [method@Pango.Lines.get_extents], the reported metrics are in user-space
@@ -59,7 +60,6 @@ draw_text (cairo_t *cr)
   /* Draw the layout N_WORDS times in a circle */
   for (i = 0; i < N_WORDS; i++)
     {
-      PangoRectangle ext;
       int width, height;
       double angle = (360. * i) / N_WORDS;
       double red;
@@ -77,14 +77,14 @@ draw_text (cairo_t *cr)
 
       lines = pango_layout_get_lines (layout);
 
-      pango_lines_get_extents (lines, NULL, &ext);
-      cairo_move_to (cr, - ((double)ext.width / PANGO_SCALE) / 2, - RADIUS);
-      pango_cairo_show_lines (cr, layout);
+      pango_lines_get_size (lines, &width, &height);
+      cairo_move_to (cr, - ((double) width / PANGO_SCALE) / 2, - RADIUS);
+      pango_cairo_show_layout (cr, layout);
 
       cairo_restore (cr);
     }
 
-  /* free the layout object */
+  /* Free the layout object */
   g_object_unref (layout);
 }
 
diff --git a/docs/rotated-text.png b/docs/rotated-text.png
index b29682e8..72b17d58 100644
Binary files a/docs/rotated-text.png and b/docs/rotated-text.png differ
diff --git a/examples/cairosimple.c b/examples/cairosimple.c
index b35b43c1..c97e7118 100644
--- a/examples/cairosimple.c
+++ b/examples/cairosimple.c
@@ -1,50 +1,33 @@
-/* Simple example to use pangocairo to render rotated text */
-
 #include <math.h>
 #include <pango/pangocairo.h>
 
+#define RADIUS 150
+#define N_WORDS 10
+#define FONT "Sans Bold 27"
+
 static void
 draw_text (cairo_t *cr)
 {
-#define RADIUS 200
-#define N_WORDS 8
-#define FONT_WITH_MANUAL_SIZE "Times new roman,Sans"
-#define FONT_SIZE 36
-#define DEVICE_DPI 72
-
-/* The following number applies a cairo CTM.  Tests for
- * https://bugzilla.gnome.org/show_bug.cgi?id=700592
- */
-#define TWEAKABLE_SCALE ((double) 0.1)
-
-  PangoContext *context;
   PangoLayout *layout;
   PangoLines *lines;
   PangoFontDescription *desc;
   int i;
 
-  /* Center coordinates on the middle of the region we are drawing
-   */
-  cairo_translate (cr, RADIUS / TWEAKABLE_SCALE, RADIUS / TWEAKABLE_SCALE);
+  /* Center coordinates on the middle of the region we are drawing */
+  cairo_translate (cr, RADIUS, RADIUS);
 
   /* Create a PangoLayout, set the font and text */
-  context = pango_cairo_create_context (cr);
-  layout = pango_layout_new (context);
-  g_object_unref (context);
-
-  pango_layout_set_text (layout, "Test\nسَلام", -1);
-
-  desc = pango_font_description_from_string (FONT_WITH_MANUAL_SIZE);
-  pango_font_description_set_absolute_size (desc, FONT_SIZE * DEVICE_DPI * PANGO_SCALE / (72.0 * 
TWEAKABLE_SCALE));
-  //pango_font_description_set_size(desc, 27 * PANGO_SCALE / TWEAKABLE_SCALE);
+  layout = pango_cairo_create_layout (cr);
 
+  pango_layout_set_text (layout, "Text", -1);
+  desc = pango_font_description_from_string (FONT);
   pango_layout_set_font_description (layout, desc);
   pango_font_description_free (desc);
 
   /* Draw the layout N_WORDS times in a circle */
   for (i = 0; i < N_WORDS; i++)
     {
-      PangoRectangle ext;
+      int width, height;
       double angle = (360. * i) / N_WORDS;
       double red;
 
@@ -61,18 +44,19 @@ draw_text (cairo_t *cr)
 
       lines = pango_layout_get_lines (layout);
 
-      pango_lines_get_extents (lines, NULL, &ext);
-      cairo_move_to (cr,( - (((double)ext.width) / PANGO_SCALE) / 2.0) , (- RADIUS)  / TWEAKABLE_SCALE);
-      pango_cairo_show_lines (cr, lines);
+      pango_lines_get_size (lines, &width, &height);
+      cairo_move_to (cr, - ((double) width / PANGO_SCALE) / 2, - RADIUS);
+      pango_cairo_show_layout (cr, layout);
 
       cairo_restore (cr);
     }
 
-  /* free the layout object */
+  /* Free the layout object */
   g_object_unref (layout);
 }
 
-int main (int argc, char **argv)
+int
+main (int argc, char **argv)
 {
   cairo_t *cr;
   char *filename;
@@ -85,14 +69,12 @@ int main (int argc, char **argv)
       return 1;
     }
 
-     filename = argv[1];
+  filename = argv[1];
 
   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                         2 * RADIUS, 2 * RADIUS);
   cr = cairo_create (surface);
 
-  cairo_scale(cr, 1 * TWEAKABLE_SCALE, 1 * TWEAKABLE_SCALE);
-
   cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
   cairo_paint (cr);
   draw_text (cr);


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