Git init
[external/pango1.0.git] / examples / cairosimple.c
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 150
10 #define N_WORDS 10
11 #define FONT "Sans Bold 27"
12
13   PangoLayout *layout;
14   PangoFontDescription *desc;
15   int i;
16
17   /* Center coordinates on the middle of the region we are drawing
18    */
19   cairo_translate (cr, RADIUS, RADIUS);
20
21   /* Create a PangoLayout, set the font and text */
22   layout = pango_cairo_create_layout (cr);
23
24   pango_layout_set_text (layout, "Text", -1);
25   desc = pango_font_description_from_string (FONT);
26   pango_layout_set_font_description (layout, desc);
27   pango_font_description_free (desc);
28
29   /* Draw the layout N_WORDS times in a circle */
30   for (i = 0; i < N_WORDS; i++)
31     {
32       int width, height;
33       double angle = (360. * i) / N_WORDS;
34       double red;
35
36       cairo_save (cr);
37
38       /* Gradient from red at angle == 60 to blue at angle == 240 */
39       red   = (1 + cos ((angle - 60) * G_PI / 180.)) / 2;
40       cairo_set_source_rgb (cr, red, 0, 1.0 - red);
41
42       cairo_rotate (cr, angle * G_PI / 180.);
43
44       /* Inform Pango to re-layout the text with the new transformation */
45       pango_cairo_update_layout (cr, layout);
46
47       pango_layout_get_size (layout, &width, &height);
48       cairo_move_to (cr, - ((double)width / PANGO_SCALE) / 2, - RADIUS);
49       pango_cairo_show_layout (cr, layout);
50
51       cairo_restore (cr);
52     }
53
54   /* free the layout object */
55   g_object_unref (layout);
56 }
57
58 int main (int argc, char **argv)
59 {
60   cairo_t *cr;
61   char *filename;
62   cairo_status_t status;
63   cairo_surface_t *surface;
64
65   if (argc != 2)
66     {
67       g_printerr ("Usage: cairosimple OUTPUT_FILENAME\n");
68       return 1;
69     }
70
71   filename = argv[1];
72
73   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
74                                         2 * RADIUS, 2 * RADIUS);
75   cr = cairo_create (surface);
76
77
78   cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
79   cairo_paint (cr);
80   draw_text (cr);
81   cairo_destroy (cr);
82
83   status = cairo_surface_write_to_png (surface, filename);
84   cairo_surface_destroy (surface);
85
86   if (status != CAIRO_STATUS_SUCCESS)
87     {
88       g_printerr ("Could not save png to '%s'\n", filename);
89       return 1;
90     }
91
92   return 0;
93 }