#include #include #include #include #include #include void draw_text(cairo_t *, const char *); static const int MARGIN = 20; static const int WIDTH = 800; static const int HEIGHT = 1131; static const char * FONT = "Serif 34"; int main(int argc, const char ** argv) { if (1 >= argc) { dprintf(2, "Usage: %s OUTPUT [TEXT]\n", argv[0]); return 1; } const char * filename = argv[1]; const char * text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; if (3 == argc) text = argv[2]; cairo_surface_t * surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT); cairo_t * ctx = cairo_create(surface); cairo_scale(ctx, 1, 1); cairo_set_source_rgb(ctx, 1., 1., 1.); cairo_paint(ctx); cairo_set_source_rgb(ctx, 0., 0., 0.); draw_text(ctx, text); cairo_destroy(ctx); cairo_status_t status = cairo_surface_write_to_png(surface, filename); cairo_surface_destroy(surface); if (CAIRO_STATUS_SUCCESS != status) { dprintf(2, "Could not save output to %s\n", filename); return 1; } } void draw_text(cairo_t * ctx, const char * text) { cairo_translate(ctx, MARGIN, MARGIN); PangoLayout * layout = pango_cairo_create_layout(ctx); PangoFontDescription * desc = pango_font_description_from_string(FONT); pango_layout_set_font_description(layout, desc); pango_font_description_free(desc); pango_layout_set_text(layout, text, -1); pango_layout_set_wrap(layout, PANGO_WRAP_WORD); pango_layout_set_width(layout, (WIDTH - 2 * MARGIN) * PANGO_SCALE); pango_cairo_update_layout(ctx, layout); pango_cairo_show_layout(ctx, layout); g_object_unref(layout); }