mirror of
git://projects.qi-hardware.com/antorcha.git
synced 2024-11-01 05:15:20 +02:00
tools/libtxt/: add image drawing (future <IMG name> directive)
This commit is contained in:
parent
e2eced62a2
commit
2a83110369
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user