source: toilet/trunk/tools/caca2tlf.c @ 1367

Last change on this file since 1367 was 1296, checked in by Sam Hocevar, 17 years ago
  • Add --half and --quarter options to caca2tlf.
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/*
2 *  caca2tlf      Create a TOIlet font from a libcaca font
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: caca2tlf.c 1296 2006-11-06 17:24:02Z sam $
7 *
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.
12 */
13
14/*
15 * This is the main program entry point.
16 */
17
18#include "config.h"
19
20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#endif
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <cucul.h>
27
28enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
29
30static void list_fonts(void);
31static void add_char(unsigned int);
32
33cucul_font_t *f;
34cucul_canvas_t *out, *onechar;
35unsigned long int const *blocks;
36uint8_t * image;
37unsigned int w, h, gw, gh, iw, ih;
38
39int main(int argc, char *argv[])
40{
41    char *fontname, *extraflag;
42    unsigned int b, i;
43
44    if(argc < 2)
45    {
46        fprintf(stderr, "Usage: %s [--half|--quarter] <font>\n", argv[0]);
47        list_fonts();
48        return -1;
49    }
50
51    if(!strcmp(argv[1], "--half") && argc >= 3)
52    {
53        extraflag = "--half ";
54        mode = HALFBLOCKS;
55        fontname = argv[2];
56    }
57    else if(!strcmp(argv[1], "--quarter") && argc >= 3)
58    {
59        extraflag = "--quarter ";
60        mode = QUARTERBLOCKS;
61        fontname = argv[2];
62    }
63    else
64    {
65        extraflag = "";
66        mode = GRAY;
67        fontname = argv[1];
68    }
69
70    f = cucul_load_font(fontname, 0);
71
72    if(!f)
73    {
74        fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
75        list_fonts();
76        return -2;
77    }
78
79    w = cucul_get_font_width(f);
80    h = cucul_get_font_height(f);
81    iw = w + 1;
82    ih = h + 1;
83    switch(mode)
84    {
85    case GRAY:
86        gw = w;
87        gh = h;
88        break;
89    case HALFBLOCKS:
90        gw = w;
91        gh = (h + 1) / 2;
92        break;
93    case QUARTERBLOCKS:
94        gw = (w + 1) / 2;
95        gh = (h + 1) / 2;
96        break;
97    }
98
99    blocks = cucul_get_font_blocks(f);
100    onechar = cucul_create_canvas(1, 1); /* FIXME: support double width */
101    cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
102    image = malloc(4 * iw * ih);
103
104    out = cucul_create_canvas(gw + 2, gh);
105    printf("tlf2a$ %u %u %u 0 4 0 0 0\n", gh, gh - 1, gw + 2);
106
107    printf("=============================================="
108                                       "==================================\n");
109    printf("  This font was automatically generated using:\n");
110    printf("   %% caca2tlf %s\"%s\"\n", extraflag, fontname);
111    printf("=============================================="
112                                       "==================================\n");
113
114    for(i = 32; i < 127; i++)
115        add_char(i);
116
117    add_char(196);
118    add_char(214);
119    add_char(220);
120    add_char(228);
121    add_char(246);
122    add_char(252);
123    add_char(223);
124
125    for(b = 0, i = 0; blocks[i + 1]; i += 2)
126    {
127        int j, n = (int)(blocks[i + 1] - blocks[i]);
128
129        for(j = 0; j < n; j++)
130        {
131            char buf[7];
132            unsigned int len;
133            unsigned long int ch = blocks[i] + j;
134
135            if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
136               || ch == 228 || ch == 246 || ch == 252 || ch == 223)
137                continue;
138
139            len = cucul_utf32_to_utf8(buf, ch);
140            buf[len] = '\0';
141            printf("0x%.04lX %s\n", ch, buf);
142            add_char(ch);
143        }
144    }
145
146    cucul_free_canvas(out);
147    cucul_free_canvas(onechar);
148    free(image);
149    cucul_free_font(f);
150
151    return 0;
152}
153
154static void list_fonts(void)
155{
156    char const * const * fonts;
157    unsigned int i;
158
159    fprintf(stderr, "Available fonts:\n");
160
161    fonts = cucul_get_font_list();
162    for(i = 0; fonts[i]; i++)
163        fprintf(stderr, "  \"%s\"\n", fonts[i]);
164}
165
166static void add_char(unsigned int ch)
167{
168    cucul_buffer_t *buf;
169    unsigned int x, y;
170
171    cucul_putchar(onechar, 0, 0, ch);
172    cucul_render_canvas(onechar, f, image, iw, ih, 4 * iw);
173
174    switch(mode)
175    {
176    case GRAY:
177        for(y = 0; y < h; y++)
178           for(x = 0; x < w; x++)
179        {
180            uint8_t c = image[4 * (x + y * iw) + 2];
181
182            if(c >= 0xa0)
183                cucul_putstr(out, x, y, "█");
184            else if(c >= 0x80)
185                cucul_putstr(out, x, y, "▓");
186            else if(c >= 0x40)
187                cucul_putstr(out, x, y, "▒");
188            else if(c >= 0x20)
189                cucul_putstr(out, x, y, "░");
190            else
191                cucul_putchar(out, x, y, ' ');
192        }
193        break;
194    case HALFBLOCKS:
195        for(y = 0; y < gh; y++)
196           for(x = 0; x < gw; x++)
197        {
198            static char const *str[] = { " ", "▀", "▄", "█" };
199
200            uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
201            uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
202
203            cucul_putstr(out, x, y,
204                         str[(p1 < 0x80 ? 0 : 1) + (p2 < 0x80 ? 0 : 2)]);
205        }
206        break;
207    case QUARTERBLOCKS:
208        for(y = 0; y < gh; y++)
209           for(x = 0; x < gw; x++)
210        {
211            static char const *str[] =
212            {
213                " ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
214                "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█"
215            };
216
217            uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
218            uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
219            uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
220            uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
221
222            cucul_putstr(out, x, y,
223                         str[(p1 < 0x80 ? 0 : 1) + (p2 < 0x80 ? 0 : 2) +
224                             (p3 < 0x80 ? 0 : 4) + (p4 < 0x80 ? 0 : 8)]);
225        }
226        break;
227    }
228
229    cucul_draw_line(out, gw, 0, gw, gh - 1, "@");
230    cucul_putchar(out, gw + 1, gh - 1, '@');
231
232    buf = cucul_export_canvas(out, "utf8");
233    fwrite(cucul_get_buffer_data(buf), cucul_get_buffer_size(buf), 1, stdout);
234    cucul_free_buffer(buf);
235}
236
Note: See TracBrowser for help on using the repository browser.