2012-06-30 03:04:11 +03:00
|
|
|
/*
|
|
|
|
* tools/libtxt/font.c - Font operations
|
|
|
|
*
|
|
|
|
* Written 2012 by Werner Almesberger
|
|
|
|
* Copyright 2012 Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2012-07-02 02:32:56 +03:00
|
|
|
#include "util.h"
|
2012-06-30 03:04:11 +03:00
|
|
|
#include "libtxt.h"
|
|
|
|
|
|
|
|
|
|
|
|
static const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?$%+-*/=@";
|
|
|
|
|
|
|
|
|
|
|
|
#define CHARS (sizeof(charset)-1)
|
|
|
|
|
|
|
|
|
|
|
|
struct sym {
|
|
|
|
int x, y;
|
|
|
|
int w, h;
|
|
|
|
};
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
struct image {
|
2012-06-30 03:04:11 +03:00
|
|
|
int w, h;
|
|
|
|
int span; /* bytes per row */
|
|
|
|
uint8_t *data;
|
|
|
|
};
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
struct font {
|
2012-06-30 17:43:13 +03:00
|
|
|
struct image *img;
|
2012-06-30 04:21:30 +03:00
|
|
|
struct sym sym[CHARS];
|
|
|
|
};
|
|
|
|
|
2012-06-30 03:04:11 +03:00
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
/* ----- XBM image --------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static const char *read_xbm_file(FILE *file, struct image *img)
|
2012-06-30 03:04:11 +03:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
unsigned v;
|
|
|
|
char c;
|
|
|
|
int bytes = 0;
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
img->data = NULL;
|
2012-06-30 03:04:11 +03:00
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
n = fscanf(file, "#define %*[^_]_width %d\n", &img->w);
|
2012-06-30 03:04:11 +03:00
|
|
|
if (n < 0)
|
|
|
|
return alloc_sprintf("reading width: %s", strerror(errno));
|
|
|
|
if (n != 1)
|
|
|
|
return alloc_sprintf("width not found (%d)", n);
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
n = fscanf(file, "#define %*[^_]_height %d\n", &img->h);
|
2012-06-30 03:04:11 +03:00
|
|
|
if (n < 0)
|
|
|
|
return alloc_sprintf("reading height: %s", strerror(errno));
|
|
|
|
if (n != 1)
|
|
|
|
return alloc_sprintf("height not found");
|
|
|
|
|
|
|
|
n = fscanf(file, "%*[^{]{\n");
|
|
|
|
if (n < 0)
|
|
|
|
return alloc_sprintf("finding data: %s", strerror(errno));
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n = fscanf(file, " 0x%x%c", &v, &c);
|
|
|
|
if (n < 0)
|
|
|
|
return alloc_sprintf("reading data: %s",
|
|
|
|
strerror(errno));
|
|
|
|
if (n != 2)
|
|
|
|
return alloc_sprintf("data not found (%d)", n);
|
|
|
|
|
|
|
|
bytes++;
|
2012-06-30 04:21:30 +03:00
|
|
|
img->data = realloc(img->data, bytes);
|
|
|
|
if (!img->data)
|
2012-06-30 03:04:11 +03:00
|
|
|
abort();
|
2012-06-30 04:21:30 +03:00
|
|
|
img->data[bytes-1] = v;
|
2012-06-30 03:04:11 +03:00
|
|
|
|
|
|
|
if (c == ',')
|
|
|
|
continue;
|
|
|
|
if (c == '}')
|
|
|
|
break;
|
|
|
|
return alloc_sprintf("invalid data syntax");
|
|
|
|
}
|
2012-06-30 04:21:30 +03:00
|
|
|
img->span = (img->w+7) >> 3;
|
|
|
|
if (bytes != img->h*img->span)
|
2012-06-30 03:04:11 +03:00
|
|
|
return alloc_sprintf("%d bytes for %dx%d bitmap",
|
2012-06-30 04:21:30 +03:00
|
|
|
bytes, img->h, img->w);
|
2012-06-30 03:04:11 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
struct image *load_image(const char *name, const char **error)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
struct image *img;
|
|
|
|
const char *err;
|
|
|
|
|
|
|
|
file = fopen(name, "r");
|
|
|
|
if (!file) {
|
|
|
|
if (error)
|
|
|
|
*error = alloc_sprintf("%s: %s", name, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-02 02:43:04 +03:00
|
|
|
img = alloc_type(struct image);
|
2012-06-30 04:21:30 +03:00
|
|
|
err = read_xbm_file(file, img);
|
|
|
|
if (err) {
|
|
|
|
if (error)
|
|
|
|
*error = err;
|
2012-06-30 17:43:13 +03:00
|
|
|
free_image(img);
|
2012-06-30 04:21:30 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void free_image(struct image *img)
|
|
|
|
{
|
2012-06-30 18:59:50 +03:00
|
|
|
if (img) {
|
|
|
|
free(img->data);
|
|
|
|
free(img);
|
|
|
|
}
|
2012-06-30 04:21:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Font generation --------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static void set_block(struct font *font, int *n,
|
|
|
|
int xlast, int x, int y0, int y1)
|
2012-06-30 03:04:11 +03:00
|
|
|
{
|
2012-06-30 04:21:30 +03:00
|
|
|
font->sym[*n].x = xlast+1;
|
|
|
|
font->sym[*n].y = y0;
|
|
|
|
font->sym[*n].w = x-xlast-1;
|
|
|
|
font->sym[*n].h = y1-y0+1;
|
2012-06-30 03:04:11 +03:00
|
|
|
(*n)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
static void analyze_block(struct font *font, int *n, int y0, int y1)
|
2012-06-30 03:04:11 +03:00
|
|
|
{
|
2012-06-30 04:21:30 +03:00
|
|
|
const struct image *img = font->img;
|
2012-06-30 03:04:11 +03:00
|
|
|
int x, y, last = -1;
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
for (x = 0; x != img->w; x++) {
|
2012-06-30 03:04:11 +03:00
|
|
|
for (y = y0; y <= y1; y++)
|
2012-06-30 04:21:30 +03:00
|
|
|
if (img->data[y*img->span+(x >> 3)] & (1 << (x & 7)))
|
2012-06-30 03:04:11 +03:00
|
|
|
break;
|
|
|
|
if (y <= y1)
|
|
|
|
continue;
|
|
|
|
if (x != last+1)
|
2012-06-30 04:21:30 +03:00
|
|
|
set_block(font, n, last, x, y0, y1);
|
2012-06-30 03:04:11 +03:00
|
|
|
last = x;
|
|
|
|
}
|
|
|
|
if (x != last+1)
|
2012-06-30 04:21:30 +03:00
|
|
|
set_block(font, n, last, x, y0, y1);
|
2012-06-30 03:04:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
static const char *analyze_font(struct font *font)
|
2012-06-30 03:04:11 +03:00
|
|
|
{
|
2012-06-30 04:21:30 +03:00
|
|
|
const struct image *img = font->img;
|
2012-06-30 03:04:11 +03:00
|
|
|
int x, y, last = -1;
|
|
|
|
int n = 0;
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
for (y = 0; y != img->h; y++) {
|
|
|
|
for (x = 0; x != img->span; x++)
|
|
|
|
if (img->data[y*img->span+x])
|
2012-06-30 03:04:11 +03:00
|
|
|
break;
|
2012-06-30 04:21:30 +03:00
|
|
|
if (x != img->span)
|
2012-06-30 03:04:11 +03:00
|
|
|
continue;
|
|
|
|
if (y != last+1)
|
2012-06-30 04:21:30 +03:00
|
|
|
analyze_block(font, &n, last+1, y-1);
|
2012-06-30 03:04:11 +03:00
|
|
|
last = y;
|
|
|
|
}
|
|
|
|
if (y != last+1)
|
2012-06-30 04:21:30 +03:00
|
|
|
analyze_block(font, &n, last+1, y-1);
|
2012-06-30 03:04:11 +03:00
|
|
|
if (n != CHARS)
|
|
|
|
return alloc_sprintf("found %d instead of %d characters",
|
|
|
|
n, CHARS);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-30 17:43:13 +03:00
|
|
|
struct font *make_font(struct image *img, const char **error)
|
2012-06-30 03:04:11 +03:00
|
|
|
{
|
2012-06-30 04:21:30 +03:00
|
|
|
struct font *font;
|
|
|
|
const char *err;
|
2012-06-30 03:04:11 +03:00
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
font = calloc(1, sizeof(struct font));
|
|
|
|
if (!font)
|
|
|
|
abort();
|
|
|
|
font->img = img;
|
|
|
|
err = analyze_font(font);
|
|
|
|
if (err) {
|
|
|
|
if (error)
|
|
|
|
*error = err;
|
2012-06-30 17:43:13 +03:00
|
|
|
free_font(font);
|
2012-06-30 04:21:30 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return font;
|
2012-06-30 03:04:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-30 04:21:30 +03:00
|
|
|
void free_font(struct font *font)
|
2012-06-30 03:04:11 +03:00
|
|
|
{
|
2012-06-30 18:59:50 +03:00
|
|
|
if (font) {
|
|
|
|
free_image(font->img);
|
|
|
|
free(font);
|
|
|
|
}
|
2012-06-30 04:21:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Drawing on a canvas ----------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-06-30 18:01:24 +03:00
|
|
|
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)
|
2012-06-30 04:21:30 +03:00
|
|
|
{
|
2012-06-30 03:04:11 +03:00
|
|
|
int ix, iy, xt;
|
|
|
|
|
2012-06-30 18:01:24 +03:00
|
|
|
for (ix = 0; ix != w; ix++) {
|
2012-06-30 19:57:03 +03:00
|
|
|
if (x+ix < 0 || x+ix >= width)
|
2012-06-30 03:04:11 +03:00
|
|
|
continue;
|
2012-06-30 18:01:24 +03:00
|
|
|
for (iy = 0; iy != h; iy++) {
|
2012-06-30 19:57:03 +03:00
|
|
|
if (y+iy < 0 || y+iy >= heigth)
|
2012-06-30 03:04:11 +03:00
|
|
|
continue;
|
2012-06-30 18:01:24 +03:00
|
|
|
xt = ox+ix;
|
|
|
|
if (img->data[(oy+iy)*img->span+(xt >> 3)] &
|
2012-06-30 03:04:11 +03:00
|
|
|
(1 << (xt & 7))) {
|
|
|
|
xt = x+ix;
|
2012-06-30 18:01:24 +03:00
|
|
|
canvas[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7);
|
2012-06-30 03:04:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-30 18:01:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 char *cp;
|
|
|
|
const struct sym *sym;
|
|
|
|
|
|
|
|
cp = strchr(charset, c);
|
|
|
|
if (!cp)
|
|
|
|
return 0;
|
|
|
|
sym = font->sym+(cp-charset);
|
|
|
|
|
|
|
|
do_draw(canvas, width, height,
|
|
|
|
font->img, sym->x, sym->y, sym->w, sym->h, x, y);
|
|
|
|
|
2012-06-30 03:04:11 +03:00
|
|
|
return sym->w;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-30 04:52:25 +03:00
|
|
|
int char_height(const struct font *font, char c)
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
const struct sym *sym;
|
|
|
|
|
|
|
|
cp = strchr(charset, c);
|
|
|
|
if (!cp)
|
|
|
|
return 0;
|
|
|
|
sym = font->sym+(cp-charset);
|
|
|
|
return sym->h;
|
|
|
|
}
|