2006-11-06 02:09:44 +01:00
|
|
|
/*
|
|
|
|
* caca2tlf Create a TOIlet font from a libcaca font
|
|
|
|
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
2006-12-12 02:50:48 +01:00
|
|
|
* This program is free software. It comes without any warranty, to
|
2006-12-11 16:10:28 +01:00
|
|
|
* the extent permitted by applicable law. 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
|
2006-11-06 02:09:44 +01:00
|
|
|
* 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>
|
2006-11-06 18:24:02 +01:00
|
|
|
#include <string.h>
|
2006-11-06 02:09:44 +01:00
|
|
|
#include <stdlib.h>
|
2008-10-18 23:36:17 +02:00
|
|
|
#include <caca.h>
|
2006-11-06 02:09:44 +01:00
|
|
|
|
2006-11-06 18:24:02 +01:00
|
|
|
enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
|
|
|
|
|
2006-11-06 02:09:44 +01:00
|
|
|
static void list_fonts(void);
|
2006-11-12 19:09:54 +01:00
|
|
|
static void add_char(unsigned long int);
|
2006-11-06 02:09:44 +01:00
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_font_t *f;
|
|
|
|
caca_canvas_t *out, *onechar;
|
2008-06-15 16:46:59 +02:00
|
|
|
uint32_t const *blocks;
|
2006-11-06 02:09:44 +01:00
|
|
|
uint8_t * image;
|
2006-11-12 19:09:54 +01:00
|
|
|
unsigned int w, h, gw, fgw, gh, iw, ih;
|
2006-11-06 02:09:44 +01:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2006-11-06 18:24:02 +01:00
|
|
|
char *fontname, *extraflag;
|
2006-11-06 02:09:44 +01:00
|
|
|
unsigned int b, i;
|
|
|
|
|
|
|
|
if(argc < 2)
|
|
|
|
{
|
2006-11-06 18:24:02 +01:00
|
|
|
fprintf(stderr, "Usage: %s [--half|--quarter] <font>\n", argv[0]);
|
2006-11-06 02:09:44 +01:00
|
|
|
list_fonts();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-06 18:24:02 +01:00
|
|
|
if(!strcmp(argv[1], "--half") && argc >= 3)
|
|
|
|
{
|
|
|
|
extraflag = "--half ";
|
|
|
|
mode = HALFBLOCKS;
|
|
|
|
fontname = argv[2];
|
|
|
|
}
|
|
|
|
else if(!strcmp(argv[1], "--quarter") && argc >= 3)
|
|
|
|
{
|
|
|
|
extraflag = "--quarter ";
|
|
|
|
mode = QUARTERBLOCKS;
|
|
|
|
fontname = argv[2];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extraflag = "";
|
|
|
|
mode = GRAY;
|
|
|
|
fontname = argv[1];
|
|
|
|
}
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
f = caca_load_font(fontname, 0);
|
2006-11-06 02:09:44 +01:00
|
|
|
|
|
|
|
if(!f)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
|
|
|
|
list_fonts();
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
w = caca_get_font_width(f);
|
|
|
|
h = caca_get_font_height(f);
|
2006-11-12 19:09:54 +01:00
|
|
|
iw = w * 2 + 1;
|
2006-11-06 18:24:02 +01:00
|
|
|
ih = h + 1;
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case GRAY:
|
|
|
|
gw = w;
|
2006-11-12 19:09:54 +01:00
|
|
|
fgw = w * 2;
|
2006-11-06 18:24:02 +01:00
|
|
|
gh = h;
|
|
|
|
break;
|
|
|
|
case HALFBLOCKS:
|
|
|
|
gw = w;
|
2006-11-12 19:09:54 +01:00
|
|
|
fgw = w * 2;
|
2006-11-06 18:24:02 +01:00
|
|
|
gh = (h + 1) / 2;
|
|
|
|
break;
|
|
|
|
case QUARTERBLOCKS:
|
|
|
|
gw = (w + 1) / 2;
|
2006-11-12 19:09:54 +01:00
|
|
|
fgw = (w * 2 + 1) / 2;
|
2006-11-06 18:24:02 +01:00
|
|
|
gh = (h + 1) / 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
blocks = caca_get_font_blocks(f);
|
|
|
|
onechar = caca_create_canvas(0, 0);
|
|
|
|
caca_set_color_ansi(onechar, CACA_WHITE, CACA_BLACK);
|
2006-11-06 18:24:02 +01:00
|
|
|
image = malloc(4 * iw * ih);
|
2006-11-06 02:09:44 +01:00
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
out = caca_create_canvas(0, 0);
|
2006-11-16 01:05:22 +01:00
|
|
|
printf("tlf2a$ %u %u %u -1 4 0 0 0\n", gh, gh - 1, fgw + 2);
|
2006-11-06 02:09:44 +01:00
|
|
|
|
2006-11-06 18:24:02 +01:00
|
|
|
printf("=============================================="
|
|
|
|
"==================================\n");
|
|
|
|
printf(" This font was automatically generated using:\n");
|
|
|
|
printf(" %% caca2tlf %s\"%s\"\n", extraflag, fontname);
|
|
|
|
printf("=============================================="
|
|
|
|
"==================================\n");
|
2006-11-06 02:09:44 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2006-11-06 18:24:02 +01:00
|
|
|
if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
|
|
|
|
|| ch == 228 || ch == 246 || ch == 252 || ch == 223)
|
2006-11-06 02:09:44 +01:00
|
|
|
continue;
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
len = caca_utf32_to_utf8(buf, ch);
|
2006-11-06 02:09:44 +01:00
|
|
|
buf[len] = '\0';
|
|
|
|
printf("0x%.04lX %s\n", ch, buf);
|
|
|
|
add_char(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_free_canvas(out);
|
|
|
|
caca_free_canvas(onechar);
|
2006-11-06 02:09:44 +01:00
|
|
|
free(image);
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_free_font(f);
|
2006-11-06 02:09:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void list_fonts(void)
|
|
|
|
{
|
|
|
|
char const * const * fonts;
|
|
|
|
unsigned int i;
|
2006-11-06 18:24:02 +01:00
|
|
|
|
2006-11-06 02:09:44 +01:00
|
|
|
fprintf(stderr, "Available fonts:\n");
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
fonts = caca_get_font_list();
|
2006-11-06 02:09:44 +01:00
|
|
|
for(i = 0; fonts[i]; i++)
|
|
|
|
fprintf(stderr, " \"%s\"\n", fonts[i]);
|
|
|
|
}
|
|
|
|
|
2006-11-12 19:09:54 +01:00
|
|
|
static void add_char(unsigned long int ch)
|
2006-11-06 02:09:44 +01:00
|
|
|
{
|
2006-11-12 21:37:58 +01:00
|
|
|
void *buf;
|
|
|
|
unsigned long int len;
|
2006-11-12 19:09:54 +01:00
|
|
|
unsigned int x, y, myw, mygw;
|
2008-10-18 23:36:17 +02:00
|
|
|
int full = caca_utf32_is_fullwidth(ch);
|
2006-11-06 02:09:44 +01:00
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_set_canvas_size(onechar, full ? 2 : 1, 1);
|
|
|
|
caca_put_char(onechar, 0, 0, ch);
|
|
|
|
caca_render_canvas(onechar, f, image, iw, ih, 4 * iw);
|
2006-11-06 02:09:44 +01:00
|
|
|
|
2006-11-12 19:09:54 +01:00
|
|
|
myw = full ? 2 * w : w;
|
|
|
|
mygw = full ? fgw : gw;
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_set_canvas_size(out, (full ? fgw : gw) + 2, gh);
|
2006-11-12 19:09:54 +01:00
|
|
|
|
2006-11-06 18:24:02 +01:00
|
|
|
switch(mode)
|
2006-11-06 02:09:44 +01:00
|
|
|
{
|
2006-11-06 18:24:02 +01:00
|
|
|
case GRAY:
|
|
|
|
for(y = 0; y < h; y++)
|
2006-11-12 19:09:54 +01:00
|
|
|
for(x = 0; x < myw; x++)
|
2006-11-06 18:24:02 +01:00
|
|
|
{
|
|
|
|
uint8_t c = image[4 * (x + y * iw) + 2];
|
|
|
|
|
|
|
|
if(c >= 0xa0)
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_put_str(out, x, y, "█");
|
2006-11-06 18:24:02 +01:00
|
|
|
else if(c >= 0x80)
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_put_str(out, x, y, "▓");
|
2006-11-06 18:24:02 +01:00
|
|
|
else if(c >= 0x40)
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_put_str(out, x, y, "▒");
|
2006-11-06 18:24:02 +01:00
|
|
|
else if(c >= 0x20)
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_put_str(out, x, y, "░");
|
2006-11-06 18:24:02 +01:00
|
|
|
else
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_put_char(out, x, y, ' ');
|
2006-11-06 18:24:02 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HALFBLOCKS:
|
|
|
|
for(y = 0; y < gh; y++)
|
2006-11-12 19:09:54 +01:00
|
|
|
for(x = 0; x < mygw; x++)
|
2006-11-06 18:24:02 +01:00
|
|
|
{
|
|
|
|
static char const *str[] = { " ", "▀", "▄", "█" };
|
|
|
|
|
|
|
|
uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
|
|
|
|
uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80)]);
|
2006-11-06 18:24:02 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QUARTERBLOCKS:
|
|
|
|
for(y = 0; y < gh; y++)
|
2006-11-12 19:09:54 +01:00
|
|
|
for(x = 0; x < mygw; x++)
|
2006-11-06 18:24:02 +01:00
|
|
|
{
|
|
|
|
static char const *str[] =
|
|
|
|
{
|
|
|
|
" ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
|
|
|
|
"▗", "▚", "▐", "▜", "▄", "▙", "▟", "█"
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
|
|
|
|
uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
|
|
|
|
uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
|
|
|
|
uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80) +
|
2006-11-12 21:37:58 +01:00
|
|
|
4 * (p3 > 0x80) + 8 * (p4 > 0x80)]);
|
2006-11-06 18:24:02 +01:00
|
|
|
}
|
|
|
|
break;
|
2006-11-06 02:09:44 +01:00
|
|
|
}
|
2006-11-06 18:24:02 +01:00
|
|
|
|
2006-11-16 01:05:22 +01:00
|
|
|
if(ch == ' ' || ch == 0xa0)
|
|
|
|
{
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_draw_line(out, mygw - 1, 0, mygw - 1, gh - 1, '$');
|
|
|
|
caca_draw_line(out, mygw / 2, 0, mygw / 2, gh - 1, '$');
|
2006-11-16 01:05:22 +01:00
|
|
|
}
|
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
caca_draw_line(out, mygw, 0, mygw, gh - 1, '@');
|
|
|
|
caca_put_char(out, mygw + 1, gh - 1, '@');
|
2006-11-06 02:09:44 +01:00
|
|
|
|
2008-10-18 23:36:17 +02:00
|
|
|
buf = caca_export_memory(out, "utf8", &len);
|
2006-11-12 21:37:58 +01:00
|
|
|
fwrite(buf, len, 1, stdout);
|
|
|
|
free(buf);
|
2006-11-06 02:09:44 +01:00
|
|
|
}
|
|
|
|
|