mirror of
git://projects.qi-hardware.com/antorcha.git
synced 2024-11-01 07:24:58 +02:00
tools/libtxt/: restructure to make more modular and avoid global variables
This commit is contained in:
parent
1b692d0961
commit
d05c6a9558
@ -33,15 +33,19 @@ struct sym {
|
|||||||
int w, h;
|
int w, h;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct font {
|
struct image {
|
||||||
int w, h;
|
int w, h;
|
||||||
int span; /* bytes per row */
|
int span; /* bytes per row */
|
||||||
struct sym sym[CHARS];
|
|
||||||
uint8_t *data;
|
uint8_t *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct font {
|
||||||
|
const struct image *img;
|
||||||
|
struct sym sym[CHARS];
|
||||||
|
};
|
||||||
|
|
||||||
static struct font font;
|
|
||||||
|
/* ----- Helper functions -------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
static const char *alloc_sprintf(const char *fmt, ...)
|
static const char *alloc_sprintf(const char *fmt, ...)
|
||||||
@ -63,23 +67,25 @@ static const char *alloc_sprintf(const char *fmt, ...)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *read_font_file(FILE *file)
|
/* ----- XBM image --------------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
|
static const char *read_xbm_file(FILE *file, struct image *img)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
unsigned v;
|
unsigned v;
|
||||||
char c;
|
char c;
|
||||||
int bytes = 0;
|
int bytes = 0;
|
||||||
|
|
||||||
free(font.data);
|
img->data = NULL;
|
||||||
font.data = NULL;
|
|
||||||
|
|
||||||
n = fscanf(file, "#define %*[^_]_width %d\n", &font.w);
|
n = fscanf(file, "#define %*[^_]_width %d\n", &img->w);
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
return alloc_sprintf("reading width: %s", strerror(errno));
|
return alloc_sprintf("reading width: %s", strerror(errno));
|
||||||
if (n != 1)
|
if (n != 1)
|
||||||
return alloc_sprintf("width not found (%d)", n);
|
return alloc_sprintf("width not found (%d)", n);
|
||||||
|
|
||||||
n = fscanf(file, "#define %*[^_]_height %d\n", &font.h);
|
n = fscanf(file, "#define %*[^_]_height %d\n", &img->h);
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
return alloc_sprintf("reading height: %s", strerror(errno));
|
return alloc_sprintf("reading height: %s", strerror(errno));
|
||||||
if (n != 1)
|
if (n != 1)
|
||||||
@ -98,10 +104,10 @@ static const char *read_font_file(FILE *file)
|
|||||||
return alloc_sprintf("data not found (%d)", n);
|
return alloc_sprintf("data not found (%d)", n);
|
||||||
|
|
||||||
bytes++;
|
bytes++;
|
||||||
font.data = realloc(font.data, bytes);
|
img->data = realloc(img->data, bytes);
|
||||||
if (!font.data)
|
if (!img->data)
|
||||||
abort();
|
abort();
|
||||||
font.data[bytes-1] = v;
|
img->data[bytes-1] = v;
|
||||||
|
|
||||||
if (c == ',')
|
if (c == ',')
|
||||||
continue;
|
continue;
|
||||||
@ -109,60 +115,100 @@ static const char *read_font_file(FILE *file)
|
|||||||
break;
|
break;
|
||||||
return alloc_sprintf("invalid data syntax");
|
return alloc_sprintf("invalid data syntax");
|
||||||
}
|
}
|
||||||
font.span = (font.w+7) >> 3;
|
img->span = (img->w+7) >> 3;
|
||||||
if (bytes != font.h*font.span)
|
if (bytes != img->h*img->span)
|
||||||
return alloc_sprintf("%d bytes for %dx%d bitmap",
|
return alloc_sprintf("%d bytes for %dx%d bitmap",
|
||||||
bytes, font.h, font.w);
|
bytes, img->h, img->w);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void set_block(int *n, int xlast, int x, int y0, int y1)
|
struct image *load_image(const char *name, const char **error)
|
||||||
{
|
{
|
||||||
font.sym[*n].x = xlast+1;
|
FILE *file;
|
||||||
font.sym[*n].y = y0;
|
struct image *img;
|
||||||
font.sym[*n].w = x-xlast-1;
|
const char *err;
|
||||||
font.sym[*n].h = y1-y0+1;
|
|
||||||
|
file = fopen(name, "r");
|
||||||
|
if (!file) {
|
||||||
|
if (error)
|
||||||
|
*error = alloc_sprintf("%s: %s", name, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
img = malloc(sizeof(struct image));
|
||||||
|
if (!img)
|
||||||
|
abort();
|
||||||
|
err = read_xbm_file(file, img);
|
||||||
|
if (err) {
|
||||||
|
if (error)
|
||||||
|
*error = err;
|
||||||
|
free(img);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void free_image(struct image *img)
|
||||||
|
{
|
||||||
|
free(img->data);
|
||||||
|
free(img);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ----- Font generation --------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
|
static void set_block(struct font *font, int *n,
|
||||||
|
int xlast, int x, int y0, int y1)
|
||||||
|
{
|
||||||
|
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;
|
||||||
(*n)++;
|
(*n)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void analyze_block(int *n, int y0, int y1)
|
static void analyze_block(struct font *font, int *n, int y0, int y1)
|
||||||
{
|
{
|
||||||
|
const struct image *img = font->img;
|
||||||
int x, y, last = -1;
|
int x, y, last = -1;
|
||||||
|
|
||||||
for (x = 0; x != font.w; x++) {
|
for (x = 0; x != img->w; x++) {
|
||||||
for (y = y0; y <= y1; y++)
|
for (y = y0; y <= y1; y++)
|
||||||
if (font.data[y*font.span+(x >> 3)] & (1 << (x & 7)))
|
if (img->data[y*img->span+(x >> 3)] & (1 << (x & 7)))
|
||||||
break;
|
break;
|
||||||
if (y <= y1)
|
if (y <= y1)
|
||||||
continue;
|
continue;
|
||||||
if (x != last+1)
|
if (x != last+1)
|
||||||
set_block(n, last, x, y0, y1);
|
set_block(font, n, last, x, y0, y1);
|
||||||
last = x;
|
last = x;
|
||||||
}
|
}
|
||||||
if (x != last+1)
|
if (x != last+1)
|
||||||
set_block(n, last, x, y0, y1);
|
set_block(font, n, last, x, y0, y1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *analyze_font(void)
|
static const char *analyze_font(struct font *font)
|
||||||
{
|
{
|
||||||
|
const struct image *img = font->img;
|
||||||
int x, y, last = -1;
|
int x, y, last = -1;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
for (y = 0; y != font.h; y++) {
|
for (y = 0; y != img->h; y++) {
|
||||||
for (x = 0; x != font.span; x++)
|
for (x = 0; x != img->span; x++)
|
||||||
if (font.data[y*font.span+x])
|
if (img->data[y*img->span+x])
|
||||||
break;
|
break;
|
||||||
if (x != font.span)
|
if (x != img->span)
|
||||||
continue;
|
continue;
|
||||||
if (y != last+1)
|
if (y != last+1)
|
||||||
analyze_block(&n, last+1, y-1);
|
analyze_block(font, &n, last+1, y-1);
|
||||||
last = y;
|
last = y;
|
||||||
}
|
}
|
||||||
if (y != last+1)
|
if (y != last+1)
|
||||||
analyze_block(&n, last+1, y-1);
|
analyze_block(font, &n, last+1, y-1);
|
||||||
if (n != CHARS)
|
if (n != CHARS)
|
||||||
return alloc_sprintf("found %d instead of %d characters",
|
return alloc_sprintf("found %d instead of %d characters",
|
||||||
n, CHARS);
|
n, CHARS);
|
||||||
@ -170,25 +216,39 @@ static const char *analyze_font(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const char *load_font(const char *name)
|
struct font *make_font(const struct image *img, const char **error)
|
||||||
{
|
{
|
||||||
FILE *file;
|
struct font *font;
|
||||||
const char *error;
|
const char *err;
|
||||||
|
|
||||||
file = fopen(name, "r");
|
font = calloc(1, sizeof(struct font));
|
||||||
if (!file)
|
if (!font)
|
||||||
return alloc_sprintf("%s: %s", name, strerror(errno));
|
abort();
|
||||||
error = read_font_file(file);
|
font->img = img;
|
||||||
fclose(file);
|
err = analyze_font(font);
|
||||||
|
if (err) {
|
||||||
if (error)
|
if (error)
|
||||||
return error;
|
*error = err;
|
||||||
|
free(font);
|
||||||
return analyze_font();
|
return NULL;
|
||||||
|
}
|
||||||
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int draw_char(void *canvas, int width, int height, char c, int x, int y)
|
void free_font(struct font *font)
|
||||||
{
|
{
|
||||||
|
free(font);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ----- Drawing on a canvas ----------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
uint8_t *p = canvas;
|
||||||
const char *cp;
|
const char *cp;
|
||||||
const struct sym *sym;
|
const struct sym *sym;
|
||||||
@ -197,7 +257,7 @@ int draw_char(void *canvas, int width, int height, char c, int x, int y)
|
|||||||
cp = strchr(charset, c);
|
cp = strchr(charset, c);
|
||||||
if (!cp)
|
if (!cp)
|
||||||
return 0;
|
return 0;
|
||||||
sym = font.sym+(cp-charset);
|
sym = font->sym+(cp-charset);
|
||||||
|
|
||||||
for (ix = 0; ix != sym->w; ix++) {
|
for (ix = 0; ix != sym->w; ix++) {
|
||||||
if (x+ix >= width)
|
if (x+ix >= width)
|
||||||
@ -206,7 +266,7 @@ int draw_char(void *canvas, int width, int height, char c, int x, int y)
|
|||||||
if (y+iy >= height)
|
if (y+iy >= height)
|
||||||
continue;
|
continue;
|
||||||
xt = sym->x+ix;
|
xt = sym->x+ix;
|
||||||
if (font.data[(sym->y+iy)*font.span+(xt >> 3)] &
|
if (img->data[(sym->y+iy)*img->span+(xt >> 3)] &
|
||||||
(1 << (xt & 7))) {
|
(1 << (xt & 7))) {
|
||||||
xt = x+ix;
|
xt = x+ix;
|
||||||
p[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7);
|
p[((y+iy)*width+xt) >> 3] |= 1 << (xt & 7);
|
||||||
@ -217,25 +277,36 @@ int draw_char(void *canvas, int width, int height, char c, int x, int y)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ----- Testing ----------------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
#define W 64
|
#define W 64
|
||||||
#define H 20
|
#define H 20
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
struct font *font;
|
||||||
|
const struct image *img;
|
||||||
const char *error;
|
const char *error;
|
||||||
uint8_t canvas[W*H/8] = { 0 };
|
uint8_t canvas[W*H/8] = { 0 };
|
||||||
char *s;
|
char *s;
|
||||||
int x, xo, y;
|
int x, xo, y;
|
||||||
|
|
||||||
error = load_font(argv[1]);
|
img = load_image(argv[1], &error);
|
||||||
if (error) {
|
if (!img) {
|
||||||
fprintf(stderr, "%s\n", error);
|
fprintf(stderr, "%s\n", error);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
font = make_font(img, &error);
|
||||||
|
if (!font) {
|
||||||
|
fprintf(stderr, "%s\n", error);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
x = 0;
|
x = 0;
|
||||||
for (s = argv[2]; *s; s++) {
|
for (s = argv[2]; *s; s++) {
|
||||||
xo = draw_char(canvas, W, H, *s, x, 0);
|
xo = draw_char(canvas, W, H, font, *s, x, 0);
|
||||||
if (xo)
|
if (xo)
|
||||||
x += xo+1;
|
x += xo+1;
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#ifndef LIBTXT_H
|
#ifndef LIBTXT_H
|
||||||
#define LIBTXT_H
|
#define LIBTXT_H
|
||||||
|
|
||||||
|
|
||||||
struct edit {
|
struct edit {
|
||||||
enum edit_type {
|
enum edit_type {
|
||||||
edit_string,
|
edit_string,
|
||||||
@ -40,11 +41,17 @@ struct edit {
|
|||||||
* Empty lines are ignored. The newline of a line containing only markup is
|
* Empty lines are ignored. The newline of a line containing only markup is
|
||||||
* also ignored.
|
* also ignored.
|
||||||
*
|
*
|
||||||
* Newline leaves one
|
* Newline leaves one blank row between text lines.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const char *load_font(const char *name);
|
struct image *load_image(const char *name, const char **error);
|
||||||
int draw_char(void *canvas, int width, int height, char c, int x, int y);
|
void free_image(struct image *img);
|
||||||
|
|
||||||
|
struct font *make_font(const struct image *img, const char **error);
|
||||||
|
void free_font(struct font *font);
|
||||||
|
|
||||||
|
int draw_char(void *canvas, int width, int height,
|
||||||
|
const struct font *font, char c, int x, int y);
|
||||||
|
|
||||||
struct edit *text2edit(const char *s);
|
struct edit *text2edit(const char *s);
|
||||||
char *edit2text(const struct edit *e);
|
char *edit2text(const struct edit *e);
|
||||||
|
Loading…
Reference in New Issue
Block a user