1 Title: Pango Interaction
2
3Pango is the text layout system used by GDK and GTK. The functions
4and types in this section are used to obtain clip regions for
5`PangoLayout`s, and to get `PangoContext`s that can be used with
6GDK.
7
8## Using Pango in GDK
9
10Creating a `PangoLayout` object is the first step in rendering text,
11and requires getting a handle to a `PangoContext`. For GTK programs,
12you’ll usually want to use [method@Gtk.Widget.get_pango_context], or
13[method@Gtk.Widget.create_pango_layout]. Once you have a `PangoLayout`,
14you can set the text and attributes of it with Pango functions like
15[method@Pango.Layout.set_text] and get its size with
16[method@Pango.Layout.get_size].
17
18*Note*: Pango uses a fixed point system internally, so converting
19between Pango units and pixels using `PANGO_SCALE` or the `PANGO_PIXELS()`
20macro.
21
22Rendering a Pango layout is done most simply with [func@PangoCairo.show_layout];
23you can also draw pieces of the layout with [func@PangoCairo.show_layout_line].
24
25### Draw transformed text with Pango and cairo
26
27```c
28#define RADIUS 100
29#define N_WORDS 10
30#define FONT "Sans Bold 18"
31
32PangoContext *context;
33PangoLayout *layout;
34PangoFontDescription *desc;
35
36double radius;
37int width, height;
38int i;
39
40// Set up a transformation matrix so that the user space coordinates for
41// where we are drawing are [-RADIUS, RADIUS], [-RADIUS, RADIUS]
42// We first center, then change the scale
43
44width = gdk_surface_get_width (surface);
45height = gdk_surface_get_height (surface);
46radius = MIN (width, height) / 2.;
47
48cairo_translate (cr,
49                 radius + (width - 2 * radius) / 2,
50                 radius + (height - 2 * radius) / 2);
51                 cairo_scale (cr, radius / RADIUS, radius / RADIUS);
52
53// Create a PangoLayout, set the font and text
54context = gdk_pango_context_get_for_display (display);
55layout = pango_layout_new (context);
56pango_layout_set_text (layout, "Text", -1);
57desc = pango_font_description_from_string (FONT);
58pango_layout_set_font_description (layout, desc);
59pango_font_description_free (desc);
60
61// Draw the layout N_WORDS times in a circle
62for (i = 0; i < N_WORDS; i++)
63  {
64    double red, green, blue;
65    double angle = 2 * G_PI * i / n_words;
66
67    cairo_save (cr);
68
69    // Gradient from red at angle == 60 to blue at angle == 300
70    red = (1 + cos (angle - 60)) / 2;
71    green = 0;
72    blue = 1 - red;
73
74    cairo_set_source_rgb (cr, red, green, blue);
75    cairo_rotate (cr, angle);
76
77    // Inform Pango to re-layout the text with the new transformation matrix
78    pango_cairo_update_layout (cr, layout);
79
80    pango_layout_get_size (layout, &width, &height);
81
82    cairo_move_to (cr, - width / 2 / PANGO_SCALE, - DEFAULT_TEXT_RADIUS);
83    pango_cairo_show_layout (cr, layout);
84
85    cairo_restore (cr);
86  }
87
88g_object_unref (layout);
89g_object_unref (context);
90```
91
92The example code above will yield the following result:
93
94![](rotated-text.png)
95