mirror of
git://projects.qi-hardware.com/antorcha.git
synced 2025-04-21 12:27:27 +03:00
rename libtxt to libant
This commit is contained in:
35
tools/libant/Makefile
Normal file
35
tools/libant/Makefile
Normal file
@@ -0,0 +1,35 @@
|
||||
#
|
||||
# tools/libant/Makefile - Build the Antorcha library
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
|
||||
LIB = libant.a
|
||||
|
||||
# _GNU_SOURCE for vasprintf
|
||||
CFLAGS = -g -Wall -D_GNU_SOURCE
|
||||
|
||||
OBJS = edit.o font.o
|
||||
|
||||
.PHONY: all ben clean spotless
|
||||
|
||||
all: $(LIB)
|
||||
|
||||
ben:
|
||||
$(MAKE) CC=mipsel-openwrt-linux-gcc
|
||||
|
||||
$(LIB): $(OBJS)
|
||||
$(AR) cr $@ $^
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
||||
spotless: clean
|
||||
rm -f $(LIB)
|
||||
343
tools/libant/edit.c
Normal file
343
tools/libant/edit.c
Normal file
@@ -0,0 +1,343 @@
|
||||
/*
|
||||
* tools/libant/edit.c - Editing and rendering
|
||||
*
|
||||
* 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 <sys/types.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "libant.h"
|
||||
|
||||
|
||||
#define DEFAULT_FONT "5x7"
|
||||
|
||||
|
||||
/* ----- Render a list of editing instructions ----------------------------- */
|
||||
|
||||
|
||||
struct font_dir {
|
||||
const char *name;
|
||||
struct font_dir *next;
|
||||
} *font_path = NULL, **last_font_dir = &font_path;
|
||||
|
||||
|
||||
void add_font_dir(const char *name)
|
||||
{
|
||||
struct font_dir *dir;
|
||||
|
||||
dir = alloc_type(struct font_dir);
|
||||
dir->name = strdup(name);
|
||||
if (!dir->name)
|
||||
abort();
|
||||
dir->next = NULL;
|
||||
*last_font_dir = dir;
|
||||
last_font_dir = &dir->next;
|
||||
}
|
||||
|
||||
|
||||
static struct image *find_font_image(const char *name, const char **error)
|
||||
{
|
||||
struct image *img;
|
||||
const char *err;
|
||||
const struct font_dir *dir;
|
||||
FILE *file;
|
||||
char *path;
|
||||
|
||||
img = load_image(name, error);
|
||||
if (img)
|
||||
return img;
|
||||
if (error)
|
||||
err = *error;
|
||||
for (dir = font_path; dir; dir = dir->next) {
|
||||
asprintf(&path, "%s/%s.xbm", dir->name, name);
|
||||
file = fopen(path, "r");
|
||||
if (!file)
|
||||
continue;
|
||||
img = load_image_file(file, error);
|
||||
fclose(file);
|
||||
return img;
|
||||
}
|
||||
if (error)
|
||||
*error = err;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static struct font *load_font(const char *name, const char **error)
|
||||
{
|
||||
struct image *img;
|
||||
|
||||
img = find_font_image(name, error);
|
||||
if (!img)
|
||||
return NULL;
|
||||
return make_font(img, error);
|
||||
}
|
||||
|
||||
|
||||
static int do_edit(uint8_t *canvas, int width, int height,
|
||||
const struct edit *e, const char **error)
|
||||
{
|
||||
struct image *img;
|
||||
struct font *font = NULL;
|
||||
int x = 0, y = 0, max = 0;
|
||||
int spc = 1;
|
||||
const char *s;
|
||||
int xo, yo;
|
||||
|
||||
while (e) {
|
||||
switch (e->type) {
|
||||
case edit_string:
|
||||
if (!font) {
|
||||
font = load_font(DEFAULT_FONT, error);
|
||||
if (!font)
|
||||
goto fail;
|
||||
}
|
||||
for (s = e->u.s; *s; s++) {
|
||||
xo = draw_char(canvas, width, height,
|
||||
font, *s, x, y);
|
||||
yo = char_height(font, *s);
|
||||
if (yo > max)
|
||||
max = yo;
|
||||
x += xo+spc;
|
||||
}
|
||||
break;
|
||||
case edit_font:
|
||||
free_font(font);
|
||||
font = load_font(e->u.s, error);
|
||||
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;
|
||||
case edit_xoff:
|
||||
x += e->u.n;
|
||||
break;
|
||||
case edit_xpos:
|
||||
x = e->u.n;
|
||||
break;
|
||||
case edit_yoff:
|
||||
y += e->u.n;
|
||||
break;
|
||||
case edit_ypos:
|
||||
y = e->u.n;
|
||||
break;
|
||||
case edit_nl:
|
||||
x = 0;
|
||||
y += max+1;
|
||||
max = 0;
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
e = e->next;
|
||||
}
|
||||
return 1;
|
||||
|
||||
fail:
|
||||
free_font(font);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void *apply_edits(int width, int height, const struct edit *e,
|
||||
const char **error)
|
||||
{
|
||||
uint8_t *canvas;
|
||||
|
||||
canvas = calloc(1, (width*height+7) >> 3);
|
||||
if (!canvas)
|
||||
abort();
|
||||
if (do_edit(canvas, width, height, e, error))
|
||||
return canvas;
|
||||
free(canvas);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* ----- Compile editing instructions -------------------------------------- */
|
||||
|
||||
|
||||
static char *alloc_string_n(const char *s, size_t len)
|
||||
{
|
||||
char *t;
|
||||
|
||||
t = alloc_size(len+1);
|
||||
memcpy(t, s, len);
|
||||
t[len] = 0;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
static void add_string(struct edit ***last, const char *start, size_t len)
|
||||
{
|
||||
struct edit *e;
|
||||
|
||||
e = alloc_type(struct edit);
|
||||
e->type = edit_string;
|
||||
e->u.s = alloc_string_n(start, len);
|
||||
e->next = NULL;
|
||||
**last = e;
|
||||
*last = &e->next;
|
||||
}
|
||||
|
||||
|
||||
static const char *parse_coord(struct edit *e, const char *s,
|
||||
enum edit_type off, enum edit_type pos)
|
||||
{
|
||||
char *end;
|
||||
|
||||
if (!s[2])
|
||||
return alloc_sprintf("incomplete %c tag", s[1]);
|
||||
e->u.n = strtoul(s+3, &end, 0);
|
||||
if (*end != '>')
|
||||
return alloc_sprintf("invalid number in %c tag", s[1]);
|
||||
switch (s[2]) {
|
||||
case '-':
|
||||
e->u.n = -e->u.n;
|
||||
/* fall through */
|
||||
case '+':
|
||||
e->type = off;
|
||||
break;
|
||||
case '=':
|
||||
e->type = pos;
|
||||
break;
|
||||
default:
|
||||
return
|
||||
alloc_sprintf("unrecognized positioning %c%c", s[1], s[2]);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *parse_edit(struct edit **edits, const char *s)
|
||||
{
|
||||
struct edit *e;
|
||||
const char *start, *err;
|
||||
int have_text = 0;
|
||||
char *end;
|
||||
|
||||
start = s;
|
||||
for (start = s; *s; s++) {
|
||||
if (*s != '<' && *s != '\n')
|
||||
continue;
|
||||
if (s != start) {
|
||||
add_string(&edits, start, s-start);
|
||||
have_text = 1;
|
||||
}
|
||||
start = s+1;
|
||||
|
||||
if (*s == '\n' && !have_text)
|
||||
continue;
|
||||
|
||||
e = alloc_type(struct edit);
|
||||
e->type = edit_nl; /* pick something without data */
|
||||
e->next = NULL;
|
||||
*edits = e;
|
||||
edits = &e->next;
|
||||
|
||||
if (*s == '\n') {
|
||||
have_text = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
end = strchr(s, '>');
|
||||
if (!end)
|
||||
return alloc_sprintf("< without >");
|
||||
|
||||
switch (s[1]) {
|
||||
case 'F':
|
||||
if (strncmp(s, "<FONT ", 6))
|
||||
goto fail_tag;
|
||||
e->type = edit_font;
|
||||
e->u.s = alloc_string_n(s+6, end-s-6);
|
||||
break;
|
||||
case 'I':
|
||||
if (strncmp(s, "<IMG ", 5))
|
||||
goto fail_tag;
|
||||
e->type = edit_img;
|
||||
e->u.s = alloc_string_n(s+5, end-s-5);
|
||||
break;
|
||||
case 'S':
|
||||
if (strncmp(s, "<SPC ", 5))
|
||||
goto fail_tag;
|
||||
e->type = edit_spc;
|
||||
e->u.n = strtoul(s+5, &end, 0);
|
||||
if (*end != '>')
|
||||
return
|
||||
alloc_sprintf("invalid number in SPC tag");
|
||||
break;
|
||||
case 'X':
|
||||
err = parse_coord(e, s, edit_xoff, edit_xpos);
|
||||
if (err)
|
||||
return err;
|
||||
break;
|
||||
case 'Y':
|
||||
err = parse_coord(e, s, edit_yoff, edit_ypos);
|
||||
if (err)
|
||||
return err;
|
||||
break;
|
||||
default:
|
||||
goto fail_tag;
|
||||
}
|
||||
s = end;
|
||||
start = s+1;
|
||||
}
|
||||
if (s != start)
|
||||
add_string(&edits, start, s-start);
|
||||
return NULL;
|
||||
|
||||
fail_tag:
|
||||
return alloc_sprintf("unrecognized tag in %.*s", end-s+1, s);
|
||||
}
|
||||
|
||||
|
||||
struct edit *text2edit(const char *s, const char **error)
|
||||
{
|
||||
struct edit *edits = NULL;
|
||||
const char *err;
|
||||
|
||||
err = parse_edit(&edits, s);
|
||||
if (!err)
|
||||
return edits;
|
||||
if (error)
|
||||
*error = err;
|
||||
free_edit(edits);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* ----- Free edit list ---------------------------------------------------- */
|
||||
|
||||
|
||||
void free_edit(struct edit *e)
|
||||
{
|
||||
struct edit *next;
|
||||
|
||||
while (e) {
|
||||
if (e->type == edit_font || e->type == edit_img)
|
||||
free(e->u.s);
|
||||
next = e->next;
|
||||
free(e);
|
||||
e = next;
|
||||
}
|
||||
}
|
||||
301
tools/libant/font.c
Normal file
301
tools/libant/font.c
Normal file
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* tools/libant/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>
|
||||
|
||||
#include "util.h"
|
||||
#include "libant.h"
|
||||
|
||||
|
||||
static const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?$%+-*/=@.";
|
||||
|
||||
|
||||
#define CHARS (sizeof(charset)-1)
|
||||
|
||||
|
||||
struct sym {
|
||||
int x, y;
|
||||
int w, h;
|
||||
};
|
||||
|
||||
struct image {
|
||||
int w, h;
|
||||
int span; /* bytes per row */
|
||||
uint8_t *data;
|
||||
};
|
||||
|
||||
struct font {
|
||||
struct image *img;
|
||||
struct sym sym[CHARS];
|
||||
int max_width;
|
||||
};
|
||||
|
||||
|
||||
/* ----- XBM image --------------------------------------------------------- */
|
||||
|
||||
|
||||
static const char *read_xbm_file(FILE *file, struct image *img)
|
||||
{
|
||||
int n;
|
||||
unsigned v;
|
||||
char c;
|
||||
int bytes = 0;
|
||||
|
||||
img->data = NULL;
|
||||
|
||||
n = fscanf(file, "#define %*[^_]_width %d\n", &img->w);
|
||||
if (n < 0)
|
||||
return alloc_sprintf("reading width: %s", strerror(errno));
|
||||
if (n != 1)
|
||||
return alloc_sprintf("width not found (%d)", n);
|
||||
|
||||
n = fscanf(file, "#define %*[^_]_height %d\n", &img->h);
|
||||
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++;
|
||||
img->data = realloc(img->data, bytes);
|
||||
if (!img->data)
|
||||
abort();
|
||||
img->data[bytes-1] = v;
|
||||
|
||||
if (c == ',')
|
||||
continue;
|
||||
if (c == '}')
|
||||
break;
|
||||
return alloc_sprintf("invalid data syntax");
|
||||
}
|
||||
img->span = (img->w+7) >> 3;
|
||||
if (bytes != img->h*img->span)
|
||||
return alloc_sprintf("%d bytes for %dx%d bitmap",
|
||||
bytes, img->h, img->w);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
struct image *load_image_file(FILE *file, const char **error)
|
||||
{
|
||||
struct image *img;
|
||||
const char *err;
|
||||
|
||||
img = alloc_type(struct image);
|
||||
err = read_xbm_file(file, img);
|
||||
if (err) {
|
||||
if (error)
|
||||
*error = err;
|
||||
free_image(img);
|
||||
return NULL;
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
|
||||
struct image *load_image(const char *name, const char **error)
|
||||
{
|
||||
FILE *file;
|
||||
struct image *img;
|
||||
|
||||
file = fopen(name, "r");
|
||||
if (!file) {
|
||||
if (error)
|
||||
*error = alloc_sprintf("%s: %s", name, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
img = load_image_file(file, error);
|
||||
fclose(file);
|
||||
return img;
|
||||
}
|
||||
|
||||
|
||||
void free_image(struct image *img)
|
||||
{
|
||||
if (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)
|
||||
{
|
||||
int width = x-xlast-1;
|
||||
|
||||
if (width > font->max_width)
|
||||
font->max_width = width;
|
||||
font->sym[*n].x = xlast+1;
|
||||
font->sym[*n].y = y0;
|
||||
font->sym[*n].w = width;
|
||||
font->sym[*n].h = y1-y0+1;
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
|
||||
static void analyze_block(struct font *font, int *n, int y0, int y1)
|
||||
{
|
||||
const struct image *img = font->img;
|
||||
int x, y, last = -1;
|
||||
|
||||
for (x = 0; x != img->w; x++) {
|
||||
for (y = y0; y <= y1; y++)
|
||||
if (img->data[y*img->span+(x >> 3)] & (1 << (x & 7)))
|
||||
break;
|
||||
if (y <= y1)
|
||||
continue;
|
||||
if (x != last+1)
|
||||
set_block(font, n, last, x, y0, y1);
|
||||
last = x;
|
||||
}
|
||||
if (x != last+1)
|
||||
set_block(font, n, last, x, y0, y1);
|
||||
}
|
||||
|
||||
|
||||
static const char *analyze_font(struct font *font)
|
||||
{
|
||||
const struct image *img = font->img;
|
||||
int x, y, last = -1;
|
||||
int n = 0;
|
||||
|
||||
for (y = 0; y != img->h; y++) {
|
||||
for (x = 0; x != img->span; x++)
|
||||
if (img->data[y*img->span+x])
|
||||
break;
|
||||
if (x != img->span)
|
||||
continue;
|
||||
if (y != last+1)
|
||||
analyze_block(font, &n, last+1, y-1);
|
||||
last = y;
|
||||
}
|
||||
if (y != last+1)
|
||||
analyze_block(font, &n, last+1, y-1);
|
||||
if (n != CHARS)
|
||||
return alloc_sprintf("found %d instead of %d characters",
|
||||
n, CHARS);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
struct font *make_font(struct image *img, const char **error)
|
||||
{
|
||||
struct font *font;
|
||||
const char *err;
|
||||
|
||||
font = calloc(1, sizeof(struct font));
|
||||
if (!font)
|
||||
abort();
|
||||
font->img = img;
|
||||
err = analyze_font(font);
|
||||
if (err) {
|
||||
if (error)
|
||||
*error = err;
|
||||
free_font(font);
|
||||
return NULL;
|
||||
}
|
||||
return font;
|
||||
}
|
||||
|
||||
|
||||
void free_font(struct font *font)
|
||||
{
|
||||
if (font) {
|
||||
free_image(font->img);
|
||||
free(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 < 0 || x+ix >= width)
|
||||
continue;
|
||||
for (iy = 0; iy != h; iy++) {
|
||||
if (y+iy < 0 || 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 char *cp;
|
||||
const struct sym *sym;
|
||||
|
||||
if (c == ' ')
|
||||
return font->max_width;
|
||||
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);
|
||||
|
||||
return sym->w;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
80
tools/libant/libant.h
Normal file
80
tools/libant/libant.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* tools/libant/libant.h - Text processing functions
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef LIBANT_H
|
||||
#define LIBANT_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
struct image;
|
||||
|
||||
struct font;
|
||||
|
||||
struct edit {
|
||||
enum edit_type {
|
||||
edit_string,
|
||||
edit_font,
|
||||
edit_img,
|
||||
edit_spc,
|
||||
edit_xoff,
|
||||
edit_xpos,
|
||||
edit_yoff,
|
||||
edit_ypos,
|
||||
edit_nl,
|
||||
} type;
|
||||
union {
|
||||
char *s;
|
||||
int n;
|
||||
} u;
|
||||
struct edit *next;
|
||||
};
|
||||
|
||||
/*
|
||||
* Markup:
|
||||
*
|
||||
* <FONT fontname>
|
||||
* <IMG imagefile>
|
||||
* <SPC offset>
|
||||
* <X+offset> <X-offset> <X=pos>
|
||||
* <Y+offset> ...
|
||||
*
|
||||
* Empty lines are ignored. The newline of a line containing only markup is
|
||||
* also ignored.
|
||||
*
|
||||
* Newline leaves one blank row between text lines.
|
||||
*/
|
||||
|
||||
struct image *load_image_file(FILE *file, const char **error);
|
||||
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);
|
||||
|
||||
int draw_char(void *canvas, int width, int height,
|
||||
const struct font *font, char c, int x, int y);
|
||||
int char_height(const struct font *font, char c);
|
||||
|
||||
struct edit *text2edit(const char *s, const char **error);
|
||||
char *edit2text(const struct edit *e);
|
||||
void free_edit(struct edit *e);
|
||||
|
||||
void add_font_dir(const char *name);
|
||||
|
||||
void *apply_edits(int width, int height, const struct edit *e,
|
||||
const char **error);
|
||||
|
||||
#endif /* !LIBANT_H */
|
||||
53
tools/libant/util.h
Normal file
53
tools/libant/util.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* tools/libant/util.h - Utility functions
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static inline void *alloc_size(size_t size)
|
||||
{
|
||||
void *tmp = malloc(size);
|
||||
|
||||
if (!tmp)
|
||||
abort();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
#define alloc_type(t) ((t *) alloc_size(sizeof(t)))
|
||||
|
||||
|
||||
static const char *alloc_sprintf(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *tmp, *res;
|
||||
int n;
|
||||
|
||||
va_start(ap, fmt);
|
||||
n = vasprintf(&tmp, fmt, ap);
|
||||
va_end(ap);
|
||||
if (n < 0)
|
||||
abort();
|
||||
res = malloc(n+1);
|
||||
if (!res)
|
||||
abort();
|
||||
memcpy(res, tmp, n+1);
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif /* !UTIL_H */
|
||||
Reference in New Issue
Block a user