Date:2012-06-30 17:01:24 (11 years 8 months ago)
Author:Werner Almesberger
Commit:2a83110369da33132434cd3d1eb71883f505567e
Message:tools/libtxt/: add image drawing (future <IMG name> directive)

Files: tools/libtxt/edit.c (3 diffs)
tools/libtxt/font.c (1 diff)
tools/libtxt/libtxt.h (2 diffs)

Change Details

tools/libtxt/edit.c
1717#include "libtxt.h"
1818
1919
20static int *do_edit(uint8_t *canvas, int width, int height,
20static int do_edit(uint8_t *canvas, int width, int height,
2121    const struct edit *e, const char **error)
2222{
2323    struct image *img;
...... 
4848            if (!font)
4949                goto fail;
5050            break;
51        case edit_img:
52            img = load_image(e->u.s, error);
53            if (!img)
54                return 0;
55            xo = draw_image(canvas, width, height, img, x, y);
56            free_image(img);
57            x += xo;
58            break;
5159        case edit_spc:
5260            spc = e->u.n;
5361            break;
...... 
7381        }
7482        e = e->next;
7583    }
84    return 1;
7685
7786fail:
78    free_image(img);
7987    free_font(font);
8088    return 0;
8189}
tools/libtxt/font.c
246246/* ----- Drawing on a canvas ----------------------------------------------- */
247247
248248
249int draw_char(void *canvas, int width, int height,
250    const struct font *font, char c, int x, int y)
249static void do_draw(uint8_t *canvas, int width, int heigth,
250    const struct image *img, int ox, int oy, int w, int h, int x, int y)
251251{
252    const struct image *img = font->img;
253    uint8_t *p = canvas;
254    const char *cp;
255    const struct sym *sym;
256252    int ix, iy, xt;
257253
258    cp = strchr(charset, c);
259    if (!cp)
260        return 0;
261    sym = font->sym+(cp-charset);
262
263    for (ix = 0; ix != sym->w; ix++) {
254    for (ix = 0; ix != w; ix++) {
264255        if (x+ix >= width)
265256            continue;
266        for (iy = 0; iy != sym->h; iy++) {
267            if (y+iy >= height)
257        for (iy = 0; iy != h; iy++) {
258            if (y+iy >= heigth)
268259                continue;
269            xt = sym->x+ix;
270            if (img->data[(sym->y+iy)*img->span+(xt >> 3)] &
260            xt = ox+ix;
261            if (img->data[(oy+iy)*img->span+(xt >> 3)] &
271262                (1 << (xt & 7))) {
272263                xt = x+ix;
273                p[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7);
264                canvas[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7);
274265            }
275266        }
276267    }
268}
269
270
271int draw_image(void *canvas, int width, int height,
272    const struct image *img, int x, int y)
273{
274    do_draw(canvas, width, height, img, 0, 0, img->w, img->h, x, y);
275    return img->w;
276}
277
278
279int draw_char(void *canvas, int width, int height,
280    const struct font *font, char c, int x, int y)
281{
282    const char *cp;
283    const struct sym *sym;
284
285    cp = strchr(charset, c);
286    if (!cp)
287        return 0;
288    sym = font->sym+(cp-charset);
289
290    do_draw(canvas, width, height,
291        font->img, sym->x, sym->y, sym->w, sym->h, x, y);
292
277293    return sym->w;
278294}
279295
tools/libtxt/libtxt.h
2121    enum edit_type {
2222        edit_string,
2323        edit_font,
24        edit_img,
2425        edit_spc,
2526        edit_xoff,
2627        edit_xpos,
...... 
5354struct image *load_image(const char *name, const char **error);
5455void free_image(struct image *img);
5556
57int draw_image(void *canvas, int width, int height,
58    const struct image *img, int x, int y);
59
5660struct font *make_font(struct image *img, const char **error);
5761void free_font(struct font *font);
5862

Archive Download the corresponding diff file

Branches:
master
tornado-v1



interactive