2
* caca2tlf Create a TOIlet font from a libcaca font
3
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
6
* $Id: caca2tlf.c 88 2006-11-16 00:05:22Z sam $
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the Do What The Fuck You Want To
10
* Public License, Version 2, as published by Sam Hocevar. See
11
* http://sam.zoy.org/wtfpl/COPYING for more details.
15
* This is the main program entry point.
20
#if defined(HAVE_INTTYPES_H)
21
# include <inttypes.h>
28
enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
30
static void list_fonts(void);
31
static void add_char(unsigned long int);
34
cucul_canvas_t *out, *onechar;
35
unsigned long int const *blocks;
37
unsigned int w, h, gw, fgw, gh, iw, ih;
39
int main(int argc, char *argv[])
41
char *fontname, *extraflag;
46
fprintf(stderr, "Usage: %s [--half|--quarter] <font>\n", argv[0]);
51
if(!strcmp(argv[1], "--half") && argc >= 3)
53
extraflag = "--half ";
57
else if(!strcmp(argv[1], "--quarter") && argc >= 3)
59
extraflag = "--quarter ";
70
f = cucul_load_font(fontname, 0);
74
fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
79
w = cucul_get_font_width(f);
80
h = cucul_get_font_height(f);
97
fgw = (w * 2 + 1) / 2;
102
blocks = cucul_get_font_blocks(f);
103
onechar = cucul_create_canvas(0, 0);
104
cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
105
image = malloc(4 * iw * ih);
107
out = cucul_create_canvas(0, 0);
108
printf("tlf2a$ %u %u %u -1 4 0 0 0\n", gh, gh - 1, fgw + 2);
110
printf("=============================================="
111
"==================================\n");
112
printf(" This font was automatically generated using:\n");
113
printf(" %% caca2tlf %s\"%s\"\n", extraflag, fontname);
114
printf("=============================================="
115
"==================================\n");
117
for(i = 32; i < 127; i++)
128
for(b = 0, i = 0; blocks[i + 1]; i += 2)
130
int j, n = (int)(blocks[i + 1] - blocks[i]);
132
for(j = 0; j < n; j++)
136
unsigned long int ch = blocks[i] + j;
138
if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
139
|| ch == 228 || ch == 246 || ch == 252 || ch == 223)
142
len = cucul_utf32_to_utf8(buf, ch);
144
printf("0x%.04lX %s\n", ch, buf);
149
cucul_free_canvas(out);
150
cucul_free_canvas(onechar);
157
static void list_fonts(void)
159
char const * const * fonts;
162
fprintf(stderr, "Available fonts:\n");
164
fonts = cucul_get_font_list();
165
for(i = 0; fonts[i]; i++)
166
fprintf(stderr, " \"%s\"\n", fonts[i]);
169
static void add_char(unsigned long int ch)
172
unsigned long int len;
173
unsigned int x, y, myw, mygw;
174
int full = cucul_utf32_is_fullwidth(ch);
176
cucul_set_canvas_size(onechar, full ? 2 : 1, 1);
177
cucul_put_char(onechar, 0, 0, ch);
178
cucul_render_canvas(onechar, f, image, iw, ih, 4 * iw);
180
myw = full ? 2 * w : w;
181
mygw = full ? fgw : gw;
183
cucul_set_canvas_size(out, (full ? fgw : gw) + 2, gh);
188
for(y = 0; y < h; y++)
189
for(x = 0; x < myw; x++)
191
uint8_t c = image[4 * (x + y * iw) + 2];
194
cucul_put_str(out, x, y, "█");
196
cucul_put_str(out, x, y, "▓");
198
cucul_put_str(out, x, y, "▒");
200
cucul_put_str(out, x, y, "░");
202
cucul_put_char(out, x, y, ' ');
206
for(y = 0; y < gh; y++)
207
for(x = 0; x < mygw; x++)
209
static char const *str[] = { " ", "▀", "▄", "█" };
211
uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
212
uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
214
cucul_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80)]);
218
for(y = 0; y < gh; y++)
219
for(x = 0; x < mygw; x++)
221
static char const *str[] =
223
" ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
224
"▗", "▚", "▐", "▜", "▄", "▙", "▟", "█"
227
uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
228
uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
229
uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
230
uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
232
cucul_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80) +
233
4 * (p3 > 0x80) + 8 * (p4 > 0x80)]);
238
if(ch == ' ' || ch == 0xa0)
240
cucul_draw_line(out, mygw - 1, 0, mygw - 1, gh - 1, '$');
241
cucul_draw_line(out, mygw / 2, 0, mygw / 2, gh - 1, '$');
244
cucul_draw_line(out, mygw, 0, mygw, gh - 1, '@');
245
cucul_put_char(out, mygw + 1, gh - 1, '@');
247
buf = cucul_export_memory(out, "utf8", &len);
248
fwrite(buf, len, 1, stdout);