1/* Simple example to use pangocairo to render rotated text */
2
3#include <math.h>
4#include <pango/pangocairo.h>
5
6static void
7draw_text (cairo_t *cr)
8{
9#define RADIUS 200
10#define N_WORDS 8
11#define FONT_WITH_MANUAL_SIZE "Times new roman,Sans"
12#define FONT_SIZE 36
13#define DEVICE_DPI 72
14
15/* The following number applies a cairo CTM. Tests for
16 * https://bugzilla.gnome.org/show_bug.cgi?id=700592
17 */
18#define TWEAKABLE_SCALE ((double) 0.1)
19
20 PangoLayout *layout;
21 PangoFontDescription *desc;
22 int i;
23
24 /* Center coordinates on the middle of the region we are drawing
25 */
26 cairo_translate (cr, RADIUS / TWEAKABLE_SCALE, RADIUS / TWEAKABLE_SCALE);
27
28 /* Create a PangoLayout, set the font and text */
29 layout = pango_cairo_create_layout (cr);
30
31 pango_layout_set_text (layout, text: "Test\nسَلام", length: -1);
32
33 desc = pango_font_description_from_string (FONT_WITH_MANUAL_SIZE);
34 pango_font_description_set_absolute_size(desc, FONT_SIZE * DEVICE_DPI * PANGO_SCALE / (72.0 * TWEAKABLE_SCALE));
35 //pango_font_description_set_size(desc, 27 * PANGO_SCALE / TWEAKABLE_SCALE);
36
37 printf(format: "PANGO_SCALE = %d\n", PANGO_SCALE);
38 pango_layout_set_font_description (layout, desc);
39 pango_font_description_free (desc);
40
41 /* Draw the layout N_WORDS times in a circle */
42 for (i = 0; i < N_WORDS; i++)
43 {
44 int width, height;
45 double angle = (360. * i) / N_WORDS;
46 double red;
47
48 cairo_save (cr);
49
50 /* Gradient from red at angle == 60 to blue at angle == 240 */
51 red = (1 + cos (x: (angle - 60) * G_PI / 180.)) / 2;
52 cairo_set_source_rgb (cr, red, green: 0, blue: 1.0 - red);
53
54 cairo_rotate (cr, angle: angle * G_PI / 180.);
55
56 /* Inform Pango to re-layout the text with the new transformation */
57 pango_cairo_update_layout (cr, layout);
58
59 pango_layout_get_size (layout, width: &width, height: &height);
60 cairo_move_to (cr,x: ( - (((double)width) / PANGO_SCALE) / 2.0) , y: (- RADIUS) / TWEAKABLE_SCALE);
61 pango_cairo_show_layout (cr, layout);
62
63 cairo_restore (cr);
64 }
65
66 /* free the layout object */
67 g_object_unref (object: layout);
68}
69
70int main (int argc, char **argv)
71{
72 cairo_t *cr;
73 char *filename;
74 cairo_status_t status;
75 cairo_surface_t *surface;
76
77 if (argc != 2)
78 {
79 g_printerr (format: "Usage: cairosimple OUTPUT_FILENAME\n");
80 return 1;
81 }
82
83 filename = argv[1];
84
85 surface = cairo_image_surface_create (format: CAIRO_FORMAT_ARGB32,
86 width: 2 * RADIUS, height: 2 * RADIUS);
87 cr = cairo_create (target: surface);
88
89 cairo_scale(cr, sx: 1 * TWEAKABLE_SCALE, sy: 1 * TWEAKABLE_SCALE);
90
91 cairo_set_source_rgb (cr, red: 1.0, green: 1.0, blue: 1.0);
92 cairo_paint (cr);
93 draw_text (cr);
94 cairo_destroy (cr);
95
96 status = cairo_surface_write_to_png (surface, filename);
97 cairo_surface_destroy (surface);
98
99 if (status != CAIRO_STATUS_SUCCESS)
100 {
101 g_printerr (format: "Could not save png to '%s'\n", filename);
102 return 1;
103 }
104
105 return 0;
106}
107

source code of gtk/subprojects/pango/examples/cairosimple.c