* Added the caca2tlf tool, which converts a libcaca font into a TOIlet font

using a naive pixel-to-glyphs algorithm. I will improve it later.
This commit is contained in:
Sam Hocevar 2006-11-06 01:09:44 +00:00 committed by sam
parent dadd049ca3
commit eb711671a2
4 changed files with 164 additions and 3 deletions

View file

@ -1,5 +1,5 @@
SUBDIRS = src fonts
SUBDIRS = src tools fonts
DIST_SUBDIRS = $(SUBDIRS)
EXTRA_DIST = bootstrap

View file

@ -51,10 +51,10 @@ if test "${ac_cv_have_tiocgwinsz}" = "yes"; then
fi
CUCUL="no"
PKG_CHECK_MODULES(cucul, cucul >= 0.99.beta6,
PKG_CHECK_MODULES(cucul, cucul >= 0.99.beta9,
[CUCUL="yes"],
[AC_MSG_RESULT(no)
AC_MSG_ERROR([you need libcucul version 0.99.beta6 or later])])
AC_MSG_ERROR([you need libcucul version 0.99.beta9 or later])])
AC_CACHE_CHECK([for build date],
[ac_build_date],
@ -74,6 +74,7 @@ CFLAGS="${CFLAGS} -Wall -Wpointer-arith -Wcast-align -Wcast-qual -Wstrict-protot
AC_OUTPUT([
Makefile
src/Makefile
tools/Makefile
fonts/Makefile
])

8
tools/Makefile.am Normal file
View file

@ -0,0 +1,8 @@
# $Id: Makefile.am 929 2006-10-20 16:19:41Z sam $
noinst_PROGRAMS = caca2tlf
caca2tlf_SOURCES = caca2tlf.c
caca2tlf_CFLAGS = `pkg-config --cflags cucul`
caca2tlf_LDFLAGS = `pkg-config --libs cucul`

152
tools/caca2tlf.c Normal file
View file

@ -0,0 +1,152 @@
/*
* caca2tlf Create a TOIlet font from a libcaca font
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
/*
* This is the main program entry point.
*/
#include "config.h"
#if defined(HAVE_INTTYPES_H)
# include <inttypes.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <cucul.h>
static void list_fonts(void);
static void add_char(unsigned int);
cucul_font_t *f;
cucul_canvas_t *out, *onechar;
unsigned long int const *blocks;
uint8_t * image;
unsigned int width, height;
int main(int argc, char *argv[])
{
unsigned int b, i;
if(argc < 2)
{
fprintf(stderr, "Wrong argument count. Usage: caca2tlf <font>\n");
list_fonts();
return -1;
}
f = cucul_load_font(argv[1], 0);
if(!f)
{
fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
list_fonts();
return -2;
}
width = cucul_get_font_width(f);
height = cucul_get_font_height(f);
blocks = cucul_get_font_blocks(f);
onechar = cucul_create_canvas(1, 1); /* FIXME: support double width */
cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
out = cucul_create_canvas(width + 2, height);
image = malloc(4 * width * height);
printf("tlf2a$ %u %u %u 0 3 0 0 0\n", height, height - 1, width + 2);
printf("================================================================================\n");
printf(" This font was automatically generated by caca2tlf\n");
printf("================================================================================\n");
for(i = 32; i < 127; i++)
add_char(i);
add_char(196);
add_char(214);
add_char(220);
add_char(228);
add_char(246);
add_char(252);
add_char(223);
for(b = 0, i = 0; blocks[i + 1]; i += 2)
{
int j, n = (int)(blocks[i + 1] - blocks[i]);
for(j = 0; j < n; j++)
{
char buf[7];
unsigned int len;
unsigned long int ch = blocks[i] + j;
if(ch < 0x80)
continue;
len = cucul_utf32_to_utf8(buf, ch);
buf[len] = '\0';
printf("0x%.04lX %s\n", ch, buf);
add_char(ch);
}
}
cucul_free_canvas(out);
cucul_free_canvas(onechar);
free(image);
cucul_free_font(f);
return 0;
}
static void list_fonts(void)
{
char const * const * fonts;
unsigned int i;
fprintf(stderr, "Available fonts:\n");
fonts = cucul_get_font_list();
for(i = 0; fonts[i]; i++)
fprintf(stderr, " \"%s\"\n", fonts[i]);
}
static void add_char(unsigned int ch)
{
cucul_buffer_t *buf;
unsigned int x, y;
cucul_putchar(onechar, 0, 0, ch);
cucul_render_canvas(onechar, f, image, width, height, 4 * width);
for(y = 0; y < height; y++)
for(x = 0; x < width; x++)
{
uint8_t c = image[4 * (x + y * width) + 2];
if(c >= 0xa0)
cucul_putstr(out, x, y, "");
else if(c >= 0x80)
cucul_putstr(out, x, y, "");
else if(c >= 0x40)
cucul_putstr(out, x, y, "");
else if(c >= 0x20)
cucul_putstr(out, x, y, "");
else
cucul_putchar(out, x, y, ' ');
}
cucul_draw_line(out, width, 0, width, height - 1, "@");
cucul_putchar(out, width + 1, height - 1, '@');
buf = cucul_export_canvas(out, "utf8");
fwrite(cucul_get_buffer_data(buf), cucul_get_buffer_size(buf), 1, stdout);
cucul_free_buffer(buf);
}