tools/libtxt/edit.c |
| 1 | /* |
| 2 | * tools/libtxt/edit.c - Editing and rendering |
| 3 | * |
| 4 | * Written 2012 by Werner Almesberger |
| 5 | * Copyright 2012 Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | #include <stdlib.h> |
| 16 | |
| 17 | #include "libtxt.h" |
| 18 | |
| 19 | |
| 20 | static int *do_edit(uint8_t *canvas, int width, int height, |
| 21 | const struct edit *e, const char **error) |
| 22 | { |
| 23 | struct image *img; |
| 24 | struct font *font = NULL; |
| 25 | int x = 0, y = 0, max = 0; |
| 26 | int spc = 1; |
| 27 | const char *s; |
| 28 | int xo, yo; |
| 29 | |
| 30 | while (e) { |
| 31 | switch (e->type) { |
| 32 | case edit_string: |
| 33 | for (s = e->u.s; *s; s++) { |
| 34 | xo = draw_char(canvas, width, height, |
| 35 | font, *s, x, y); |
| 36 | yo = char_height(font, *s); |
| 37 | if (yo > max) |
| 38 | max = yo; |
| 39 | x += xo+spc; |
| 40 | } |
| 41 | break; |
| 42 | case edit_font: |
| 43 | free_font(font); |
| 44 | img = load_image(e->u.s, error); |
| 45 | if (!img) |
| 46 | return 0; |
| 47 | font = make_font(img, error); |
| 48 | if (!font) |
| 49 | goto fail; |
| 50 | break; |
| 51 | case edit_spc: |
| 52 | spc = e->u.n; |
| 53 | break; |
| 54 | case edit_xoff: |
| 55 | x += e->u.n; |
| 56 | break; |
| 57 | case edit_xpos: |
| 58 | x = e->u.n; |
| 59 | break; |
| 60 | case edit_yoff: |
| 61 | y += e->u.n; |
| 62 | break; |
| 63 | case edit_ypos: |
| 64 | y = e->u.n; |
| 65 | break; |
| 66 | case edit_nl: |
| 67 | x = 0; |
| 68 | y += max+1; |
| 69 | max = 0; |
| 70 | break; |
| 71 | default: |
| 72 | abort(); |
| 73 | } |
| 74 | e = e->next; |
| 75 | } |
| 76 | |
| 77 | fail: |
| 78 | free_image(img); |
| 79 | free_font(font); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void *apply_edits(int width, int height, const struct edit *e, |
| 85 | const char **error) |
| 86 | { |
| 87 | uint8_t *canvas; |
| 88 | |
| 89 | canvas = calloc(1, (width*height+7) >> 3); |
| 90 | if (!canvas) |
| 91 | abort(); |
| 92 | if (do_edit(canvas, width, height, e, error)) |
| 93 | return canvas; |
| 94 | free(canvas); |
| 95 | return NULL; |
| 96 | } |