diff options
author | Aki <please@ignore.pl> | 2021-08-30 21:57:11 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-08-31 21:37:27 +0200 |
commit | 74a89676841f37d83c1ec02c9813fec95d773c55 (patch) | |
tree | 5f1f30845e56f7c22cee07c7d0fbeb9432846be4 | |
download | text-74a89676841f37d83c1ec02c9813fec95d773c55.zip text-74a89676841f37d83c1ec02c9813fec95d773c55.tar.gz text-74a89676841f37d83c1ec02c9813fec95d773c55.tar.bz2 |
Put together a simple text renderer to a PNG
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | text.c | 59 |
3 files changed, 71 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..11c8a1e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +text diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b27a35c --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +CFLAGS += -Wall -Wextra -Wpedantic +CFLAGS += `pkg-config --cflags cairo pango pangocairo` +LDLIBS += -lm `pkg-config --libs cairo pango pangocairo` + +all: text + +clean: + rm -f text *.o + +.PHONY: all clean @@ -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); +} |