tools/libtxt/edit.c |
17 | 17 | #include "libtxt.h" |
18 | 18 | |
19 | 19 | |
20 | | static int *do_edit(uint8_t *canvas, int width, int height, |
| 20 | static int do_edit(uint8_t *canvas, int width, int height, |
21 | 21 | const struct edit *e, const char **error) |
22 | 22 | { |
23 | 23 | struct image *img; |
... | ... | |
48 | 48 | if (!font) |
49 | 49 | goto fail; |
50 | 50 | 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; |
51 | 59 | case edit_spc: |
52 | 60 | spc = e->u.n; |
53 | 61 | break; |
... | ... | |
73 | 81 | } |
74 | 82 | e = e->next; |
75 | 83 | } |
| 84 | return 1; |
76 | 85 | |
77 | 86 | fail: |
78 | | free_image(img); |
79 | 87 | free_font(font); |
80 | 88 | return 0; |
81 | 89 | } |
tools/libtxt/font.c |
246 | 246 | /* ----- Drawing on a canvas ----------------------------------------------- */ |
247 | 247 | |
248 | 248 | |
249 | | int draw_char(void *canvas, int width, int height, |
250 | | const struct font *font, char c, int x, int y) |
| 249 | static 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) |
251 | 251 | { |
252 | | const struct image *img = font->img; |
253 | | uint8_t *p = canvas; |
254 | | const char *cp; |
255 | | const struct sym *sym; |
256 | 252 | int ix, iy, xt; |
257 | 253 | |
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++) { |
264 | 255 | if (x+ix >= width) |
265 | 256 | 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) |
268 | 259 | 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)] & |
271 | 262 | (1 << (xt & 7))) { |
272 | 263 | xt = x+ix; |
273 | | p[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7); |
| 264 | canvas[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7); |
274 | 265 | } |
275 | 266 | } |
276 | 267 | } |
| 268 | } |
| 269 | |
| 270 | |
| 271 | int 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 | |
| 279 | int 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 | |
277 | 293 | return sym->w; |
278 | 294 | } |
279 | 295 | |
tools/libtxt/libtxt.h |
21 | 21 | enum edit_type { |
22 | 22 | edit_string, |
23 | 23 | edit_font, |
| 24 | edit_img, |
24 | 25 | edit_spc, |
25 | 26 | edit_xoff, |
26 | 27 | edit_xpos, |
... | ... | |
53 | 54 | struct image *load_image(const char *name, const char **error); |
54 | 55 | void free_image(struct image *img); |
55 | 56 | |
| 57 | int draw_image(void *canvas, int width, int height, |
| 58 | const struct image *img, int x, int y); |
| 59 | |
56 | 60 | struct font *make_font(struct image *img, const char **error); |
57 | 61 | void free_font(struct font *font); |
58 | 62 | |