#include #include #include #define WIDTH 400 #define HEIGHT 300 void draw_text (cairo_t *cr) { PangoLayout *layout; PangoFontDescription *desc; int width, height; cairo_translate (cr, 100, 100); layout = pango_cairo_create_layout (cr); desc = pango_font_description_from_string ("FreeSerif 100"); pango_layout_set_font_description (layout, desc); pango_layout_set_text (layout, "\342\216\242", -1); /* U+23A2 */ pango_layout_get_pixel_size (layout, &width, &height); g_print ("%d %d\n", width, height); cairo_rectangle (cr, 0, 0, width, height); cairo_set_source_rgb (cr, 1., .5, .5); cairo_set_line_width (cr, 1); cairo_stroke (cr); cairo_move_to (cr, 0, 0); cairo_set_source_rgb (cr, 0., 0., 0.0); pango_cairo_show_layout (cr, layout); } int main (int argc, char **argv) { const char *filename; cairo_surface_t *surface; cairo_t *cr; if (argc != 2) { g_printerr ("Usage: cairosimple OUTPUT_BASENAME\n"); exit (1); } filename = g_strconcat (argv[1], ".png", NULL); surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT); cr = cairo_create (surface); cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); cairo_paint (cr); draw_text(cr); cairo_surface_write_to_png (surface, filename); filename = g_strconcat (argv[1], ".pdf", NULL); /* output also a PDF file */ surface = cairo_pdf_surface_create (filename, WIDTH, HEIGHT); cr = cairo_create (surface); draw_text(cr); cairo_show_page (cr); cairo_surface_finish (surface); return 0; }