diff --git a/tools/libtxt/edit.c b/tools/libtxt/edit.c index 8f96684..e175edb 100644 --- a/tools/libtxt/edit.c +++ b/tools/libtxt/edit.c @@ -17,7 +17,7 @@ #include "libtxt.h" -static int *do_edit(uint8_t *canvas, int width, int height, +static int do_edit(uint8_t *canvas, int width, int height, const struct edit *e, const char **error) { struct image *img; @@ -48,6 +48,14 @@ static int *do_edit(uint8_t *canvas, int width, int height, if (!font) goto fail; break; + case edit_img: + img = load_image(e->u.s, error); + if (!img) + return 0; + xo = draw_image(canvas, width, height, img, x, y); + free_image(img); + x += xo; + break; case edit_spc: spc = e->u.n; break; @@ -73,9 +81,9 @@ static int *do_edit(uint8_t *canvas, int width, int height, } e = e->next; } + return 1; fail: - free_image(img); free_font(font); return 0; } diff --git a/tools/libtxt/font.c b/tools/libtxt/font.c index 6375d6b..6ba37c5 100644 --- a/tools/libtxt/font.c +++ b/tools/libtxt/font.c @@ -246,34 +246,50 @@ void free_font(struct font *font) /* ----- Drawing on a canvas ----------------------------------------------- */ +static void do_draw(uint8_t *canvas, int width, int heigth, + const struct image *img, int ox, int oy, int w, int h, int x, int y) +{ + int ix, iy, xt; + + for (ix = 0; ix != w; ix++) { + if (x+ix >= width) + continue; + for (iy = 0; iy != h; iy++) { + if (y+iy >= heigth) + continue; + xt = ox+ix; + if (img->data[(oy+iy)*img->span+(xt >> 3)] & + (1 << (xt & 7))) { + xt = x+ix; + canvas[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7); + } + } + } +} + + +int draw_image(void *canvas, int width, int height, + const struct image *img, int x, int y) +{ + do_draw(canvas, width, height, img, 0, 0, img->w, img->h, x, y); + return img->w; +} + + int draw_char(void *canvas, int width, int height, const struct font *font, char c, int x, int y) { - const struct image *img = font->img; - uint8_t *p = canvas; const char *cp; const struct sym *sym; - int ix, iy, xt; cp = strchr(charset, c); if (!cp) return 0; sym = font->sym+(cp-charset); - for (ix = 0; ix != sym->w; ix++) { - if (x+ix >= width) - continue; - for (iy = 0; iy != sym->h; iy++) { - if (y+iy >= height) - continue; - xt = sym->x+ix; - if (img->data[(sym->y+iy)*img->span+(xt >> 3)] & - (1 << (xt & 7))) { - xt = x+ix; - p[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7); - } - } - } + do_draw(canvas, width, height, + font->img, sym->x, sym->y, sym->w, sym->h, x, y); + return sym->w; } diff --git a/tools/libtxt/libtxt.h b/tools/libtxt/libtxt.h index f901c52..49941a9 100644 --- a/tools/libtxt/libtxt.h +++ b/tools/libtxt/libtxt.h @@ -21,6 +21,7 @@ struct edit { enum edit_type { edit_string, edit_font, + edit_img, edit_spc, edit_xoff, edit_xpos, @@ -53,6 +54,9 @@ struct edit { struct image *load_image(const char *name, const char **error); void free_image(struct image *img); +int draw_image(void *canvas, int width, int height, + const struct image *img, int x, int y); + struct font *make_font(struct image *img, const char **error); void free_font(struct font *font);