~ubuntu-branches/ubuntu/karmic/pango1.0/karmic-security

« back to all changes in this revision

Viewing changes to examples/cairosimple.c

Tags: upstream-1.15.4
ImportĀ upstreamĀ versionĀ 1.15.4

Show diffs side-by-side

added added

removed removed

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