toilet/tools/caca2tlf.c

153 lines
3.7 KiB
C
Raw Normal View History

/*
* 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);
}