1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#include <math.h>
#include <stdio.h>
#include <cairo.h>
#include <glib-object.h>
#include <pango/pango.h>
#include <pango/pangocairo.h>
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);
}
|