1 /*
2  *  caca2tlf      Create a TOIlet font from a libcaca font
3  *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
4  *                All Rights Reserved
5  *
6  *  $Id$
7  *
8  *  This program is free software. It comes without any warranty, to
9  *  the extent permitted by applicable law. You can redistribute it
10  *  and/or modify it under the terms of the Do What The Fuck You Want
11  *  To Public License, Version 2, as published by Sam Hocevar. See
12  *  http://sam.zoy.org/wtfpl/COPYING for more details.
13  */
14 
15 /*
16  * This is the main program entry point.
17  */
18 
19 #include "config.h"
20 
21 #if defined(HAVE_INTTYPES_H)
22 #   include <inttypes.h>
23 #endif
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <caca.h>
28 
29 enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
30 enum charset { SPACES, ASCII, UTF8 } charset;
31 
32 static void list_fonts(void);
33 static void add_char(unsigned long int);
34 
35 caca_font_t *f;
36 caca_canvas_t *out, *onechar;
37 uint32_t const *blocks;
38 uint8_t * image;
39 unsigned int w, h, gw, fgw, gh, iw, ih;
40 
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43     char *flag1, *flag2;
44     unsigned int b, i;
45 
46     if(argc < 2)
47     {
48         fprintf(stderr,
49                 "Usage: %s [--half|--quarter] [--spaces|--ascii|--utf8] <font>\n",
50                 argv[0]);
51         list_fonts();
52         return -1;
53     }
54 
55     if((!strcmp(argv[1], "--half") || !strcmp(argv[1], "-h")) && argc > 2)
56     {
57         flag1 = "--half ";
58         mode = HALFBLOCKS;
59         argv++; argc--;
60     }
61     else if((!strcmp(argv[1], "--quarter") || !strcmp(argv[1], "-q")) && argc > 2)
62     {
63         flag1 = "--quarter ";
64         mode = QUARTERBLOCKS;
65         argv++; argc--;
66     }
67     else
68     {
69         flag1 = "";
70         mode = GRAY;
71     }
72 
73     if((!strcmp(argv[1], "--spaces") || !strcmp(argv[1], "-s")) && argc > 2)
74     {
75         flag2 = "--spaces ";
76         charset = SPACES;
77         argv++; argc--;
78     }
79     else if((!strcmp(argv[1], "--ascii") || !strcmp(argv[1], "-a")) && argc > 2)
80     {
81         flag2 = "--ascii ";
82         charset = ASCII;
83         argv++; argc--;
84     }
85     else if((!strcmp(argv[1], "--utf8") || !strcmp(argv[1], "-u")) && argc > 2)
86     {
87         flag2 = "--utf8 ";
88         charset = UTF8;
89         argv++; argc--;
90     }
91     else
92     {
93         flag2 = "";
94         charset = ASCII;
95     }
96 
97     f = caca_load_font(argv[1], 0);
98     if(!f)
99     {
100         fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
101         list_fonts();
102         return -2;
103     }
104 
105     w = caca_get_font_width(f);
106     h = caca_get_font_height(f);
107     iw = w * 2 + 1;
108     ih = h + 1;
109     switch(mode)
110     {
111     case GRAY:
112         gw = w;
113         fgw = w * 2;
114         gh = h;
115         break;
116     case HALFBLOCKS:
117         gw = w;
118         fgw = w * 2;
119         gh = (h + 1) / 2;
120         break;
121     case QUARTERBLOCKS:
122         gw = (w + 1) / 2;
123         fgw = (w * 2 + 1) / 2;
124         gh = (h + 1) / 2;
125         break;
126     }
127 
128     blocks = caca_get_font_blocks(f);
129     onechar = caca_create_canvas(0, 0);
130     caca_set_color_ansi(onechar, CACA_WHITE, CACA_BLACK);
131     image = malloc(4 * iw * ih);
132 
133     out = caca_create_canvas(0, 0);
134     printf("tlf2a$ %u %u %u -1 4 0 0 0\n", gh, gh - 1, fgw + 2);
135 
136     printf("=============================================="
137                                        "==================================\n");
138     printf("  This font was automatically generated using:\n");
139     printf("   %% caca2tlf %s%s\"%s\"\n", flag1, flag2, argv[1]);
140     printf("=============================================="
141                                        "==================================\n");
142 
143     for(i = 32; i < 127; i++)
144         add_char(i);
145 
146     add_char(196);
147     add_char(214);
148     add_char(220);
149     add_char(228);
150     add_char(246);
151     add_char(252);
152     add_char(223);
153 
154     for(b = 0, i = 0; blocks[i + 1]; i += 2)
155     {
156         int j, n = (int)(blocks[i + 1] - blocks[i]);
157 
158         for(j = 0; j < n; j++)
159         {
160             char buf[7];
161             unsigned int len;
162             unsigned long int ch = blocks[i] + j;
163 
164             if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
165                || ch == 228 || ch == 246 || ch == 252 || ch == 223)
166                 continue;
167 
168             len = caca_utf32_to_utf8(buf, ch);
169             buf[len] = '\0';
170             printf("0x%.04lX %s\n", ch, buf);
171             add_char(ch);
172         }
173     }
174 
175     caca_free_canvas(out);
176     caca_free_canvas(onechar);
177     free(image);
178     caca_free_font(f);
179 
180     return 0;
181 }
182 
list_fonts(void)183 static void list_fonts(void)
184 {
185     char const * const * fonts;
186     unsigned int i;
187 
188     fprintf(stderr, "Available fonts:\n");
189 
190     fonts = caca_get_font_list();
191     for(i = 0; fonts[i]; i++)
192         fprintf(stderr, "  \"%s\"\n", fonts[i]);
193 }
194 
add_char(unsigned long int ch)195 static void add_char(unsigned long int ch)
196 {
197     static char const * chars[][16] =
198     {
199         { "_", "_", "_", "_", " " },
200         { " ", "_", "_", "_" },
201         { " ", "_", "_", "_", "_", "_", "_", "_",
202           "_", "_", "_", "_", "_", "_", "_", "_" },
203         { "#", "$", ":", ".", " " },
204         { " ", "\"", "m", "#" },
205         { " ", "`", "'", "\"", ",", "[", "/", "P",
206           ".", "\\", "]", "T", "m", "b", "d", "W" },
207         { "█", "▓", "▒", "░", " " },
208         { " ", "▀", "▄", "█" },
209         { " ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
210           "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█" }
211     };
212 
213     static uint8_t fgs[][4] =
214     {
215         { CACA_DEFAULT, CACA_DARKGRAY, CACA_LIGHTGRAY, CACA_WHITE },
216         { CACA_DEFAULT, CACA_DEFAULT, CACA_DEFAULT, CACA_DEFAULT },
217     };
218 
219     static uint8_t bgs[][4] =
220     {
221         { CACA_DEFAULT, CACA_DARKGRAY, CACA_LIGHTGRAY, CACA_WHITE },
222         { CACA_DEFAULT, CACA_DEFAULT, CACA_DEFAULT, CACA_DEFAULT },
223     };
224 
225     char const **str;
226     void *buf;
227     size_t len;
228     unsigned int x, y, myw, mygw;
229     int coff = 0, aoff = 0;
230     int full = caca_utf32_is_fullwidth(ch);
231 
232     caca_set_canvas_size(onechar, full ? 2 : 1, 1);
233     caca_put_char(onechar, 0, 0, ch);
234     caca_render_canvas(onechar, f, image, iw, ih, 4 * iw);
235 
236     myw = full ? 2 * w : w;
237     mygw = full ? fgw : gw;
238 
239     caca_set_canvas_size(out, (full ? fgw : gw) + 2, gh);
240     caca_clear_canvas(out);
241 
242     switch(charset)
243     {
244     case SPACES:
245         coff = 0; aoff = 0;
246         break;
247     case ASCII:
248         coff = 3; aoff = 1;
249         break;
250     case UTF8:
251         coff = 6; aoff = 1;
252         break;
253     }
254 
255     switch(mode)
256     {
257     case GRAY:
258         str = chars[coff];
259         for(y = 0; y < h; y++)
260             for(x = 0; x < myw; x++)
261         {
262             uint8_t c = image[4 * (x + y * iw) + 2];
263 
264             if(c >= 0xc0)
265             {
266                 caca_set_color_ansi(out, fgs[aoff][3], bgs[aoff][3]);
267                 caca_put_str(out, x, y, str[0]);
268             }
269             else if(c >= 0x90)
270             {
271                 caca_set_color_ansi(out, fgs[aoff][2], bgs[aoff][2]);
272                 caca_put_str(out, x, y, str[0]);
273             }
274             else if(c >= 0x80)
275             {
276                 caca_set_color_ansi(out, fgs[aoff][2], bgs[aoff][2]);
277                 caca_put_str(out, x, y, str[1]);
278             }
279             else if(c >= 0x40)
280             {
281                 caca_set_color_ansi(out, fgs[aoff][1], bgs[aoff][1]);
282                 caca_put_str(out, x, y, str[2]);
283             }
284             else if(c >= 0x20)
285             {
286                 caca_set_color_ansi(out, fgs[aoff][1], bgs[aoff][1]);
287                 caca_put_str(out, x, y, str[3]);
288             }
289             else
290             {
291                 caca_set_color_ansi(out, fgs[aoff][0], bgs[aoff][0]);
292                 caca_put_str(out, x, y, str[4]);
293             }
294         }
295         break;
296     case HALFBLOCKS:
297         str = chars[coff + 1];
298         for(y = 0; y < gh; y++)
299             for(x = 0; x < mygw; x++)
300         {
301             uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
302             uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
303 
304             caca_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80)]);
305         }
306         break;
307     case QUARTERBLOCKS:
308         str = chars[coff + 2];
309         for(y = 0; y < gh; y++)
310             for(x = 0; x < mygw; x++)
311         {
312             uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
313             uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
314             uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
315             uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
316 
317             caca_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80) +
318                                          4 * (p3 > 0x80) + 8 * (p4 > 0x80)]);
319         }
320         break;
321     }
322 
323     caca_set_color_ansi(out, CACA_DEFAULT, CACA_DEFAULT);
324 
325     if(ch == ' ' || ch == 0xa0)
326     {
327         caca_draw_line(out, mygw - 1, 0, mygw - 1, gh - 1, '$');
328         caca_draw_line(out, mygw / 2, 0, mygw / 2, gh - 1, '$');
329     }
330 
331     caca_draw_line(out, mygw, 0, mygw, gh - 1, '@');
332     caca_put_char(out, mygw + 1, gh - 1, '@');
333 
334     buf = caca_export_canvas_to_memory(out, "utf8", &len);
335     fwrite(buf, len, 1, stdout);
336     free(buf);
337 }
338 
339