Debian Codegraph @

examples/cairosimple.c

raw
1 /* Simple example to use pangocairo to render rotated text */
2
3 #include <math.h>
4 #include <pango/pangocairo.h>
5
6 static void
7 draw_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, "Test\nسَلام", -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("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 ((angle - 60) * G_PI / 180.)) / 2;
52 cairo_set_source_rgb (cr, red, 0, 1.0 - red);
53
54 cairo_rotate (cr, 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, &height);
60 cairo_move_to (cr,( - (((double)width) / PANGO_SCALE) / 2.0) , (- 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 (layout);
68 }
69
70 int 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 ("Usage: cairosimple OUTPUT_FILENAME\n");
80 return 1;
81 }
82
83 filename = argv[1];
84
85 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
86 2 * RADIUS, 2 * RADIUS);
87 cr = cairo_create (surface);
88
89 cairo_scale(cr, 1 * TWEAKABLE_SCALE, 1 * TWEAKABLE_SCALE);
90
91 cairo_set_source_rgb (cr, 1.0, 1.0, 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 ("Could not save png to '%s'\n", filename);
102 return 1;
103 }
104
105 return 0;
106 }