summaryrefslogtreecommitdiff
path: root/text.c
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-08-30 21:57:11 +0200
committerAki <please@ignore.pl>2021-08-31 21:37:27 +0200
commit74a89676841f37d83c1ec02c9813fec95d773c55 (patch)
tree5f1f30845e56f7c22cee07c7d0fbeb9432846be4 /text.c
downloadtext-74a89676841f37d83c1ec02c9813fec95d773c55.zip
text-74a89676841f37d83c1ec02c9813fec95d773c55.tar.gz
text-74a89676841f37d83c1ec02c9813fec95d773c55.tar.bz2
Put together a simple text renderer to a PNG
Diffstat (limited to 'text.c')
-rw-r--r--text.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/text.c b/text.c
new file mode 100644
index 0000000..1e488a5
--- /dev/null
+++ b/text.c
@@ -0,0 +1,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);
+}