~ubuntu-branches/ubuntu/raring/libcaca/raring

« back to all changes in this revision

Viewing changes to toilet-0.1/tools/caca2tlf.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hocevar (Debian packages)
  • Date: 2007-10-13 20:10:44 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071013201044-35yldy9w3xe7iy2j
Tags: 0.99.beta12.debian-3
* debian/control:
  + Build-depend on texlive instead of all the other texlive-* packages so
    that we have the proper fonts at build time (Closes: #445797).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 88 2006-11-16 00:05:22Z 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
 
 
28
 
enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
29
 
 
30
 
static void list_fonts(void);
31
 
static void add_char(unsigned long int);
32
 
 
33
 
cucul_font_t *f;
34
 
cucul_canvas_t *out, *onechar;
35
 
unsigned long int const *blocks;
36
 
uint8_t * image;
37
 
unsigned int w, h, gw, fgw, gh, iw, ih;
38
 
 
39
 
int 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 * 2 + 1;
82
 
    ih = h + 1;
83
 
    switch(mode)
84
 
    {
85
 
    case GRAY:
86
 
        gw = w;
87
 
        fgw = w * 2;
88
 
        gh = h;
89
 
        break;
90
 
    case HALFBLOCKS:
91
 
        gw = w;
92
 
        fgw = w * 2;
93
 
        gh = (h + 1) / 2;
94
 
        break;
95
 
    case QUARTERBLOCKS:
96
 
        gw = (w + 1) / 2;
97
 
        fgw = (w * 2 + 1) / 2;
98
 
        gh = (h + 1) / 2;
99
 
        break;
100
 
    }
101
 
 
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);
106
 
 
107
 
    out = cucul_create_canvas(0, 0);
108
 
    printf("tlf2a$ %u %u %u -1 4 0 0 0\n", gh, gh - 1, fgw + 2);
109
 
 
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");
116
 
 
117
 
    for(i = 32; i < 127; i++)
118
 
        add_char(i);
119
 
 
120
 
    add_char(196);
121
 
    add_char(214);
122
 
    add_char(220);
123
 
    add_char(228);
124
 
    add_char(246);
125
 
    add_char(252);
126
 
    add_char(223);
127
 
 
128
 
    for(b = 0, i = 0; blocks[i + 1]; i += 2)
129
 
    {
130
 
        int j, n = (int)(blocks[i + 1] - blocks[i]);
131
 
 
132
 
        for(j = 0; j < n; j++)
133
 
        {
134
 
            char buf[7];
135
 
            unsigned int len;
136
 
            unsigned long int ch = blocks[i] + j;
137
 
 
138
 
            if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
139
 
               || ch == 228 || ch == 246 || ch == 252 || ch == 223)
140
 
                continue;
141
 
 
142
 
            len = cucul_utf32_to_utf8(buf, ch);
143
 
            buf[len] = '\0';
144
 
            printf("0x%.04lX %s\n", ch, buf);
145
 
            add_char(ch);
146
 
        }
147
 
    }
148
 
 
149
 
    cucul_free_canvas(out);
150
 
    cucul_free_canvas(onechar);
151
 
    free(image);
152
 
    cucul_free_font(f);
153
 
 
154
 
    return 0;
155
 
}
156
 
 
157
 
static void list_fonts(void)
158
 
{
159
 
    char const * const * fonts;
160
 
    unsigned int i;
161
 
 
162
 
    fprintf(stderr, "Available fonts:\n");
163
 
 
164
 
    fonts = cucul_get_font_list();
165
 
    for(i = 0; fonts[i]; i++)
166
 
        fprintf(stderr, "  \"%s\"\n", fonts[i]);
167
 
}
168
 
 
169
 
static void add_char(unsigned long int ch)
170
 
{
171
 
    void *buf;
172
 
    unsigned long int len;
173
 
    unsigned int x, y, myw, mygw;
174
 
    int full = cucul_utf32_is_fullwidth(ch);
175
 
 
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);
179
 
 
180
 
    myw = full ? 2 * w : w;
181
 
    mygw = full ? fgw : gw;
182
 
 
183
 
    cucul_set_canvas_size(out, (full ? fgw : gw) + 2, gh);
184
 
 
185
 
    switch(mode)
186
 
    {
187
 
    case GRAY:
188
 
        for(y = 0; y < h; y++)
189
 
            for(x = 0; x < myw; x++)
190
 
        {
191
 
            uint8_t c = image[4 * (x + y * iw) + 2];
192
 
 
193
 
            if(c >= 0xa0)
194
 
                cucul_put_str(out, x, y, "█");
195
 
            else if(c >= 0x80)
196
 
                cucul_put_str(out, x, y, "▓");
197
 
            else if(c >= 0x40)
198
 
                cucul_put_str(out, x, y, "▒");
199
 
            else if(c >= 0x20)
200
 
                cucul_put_str(out, x, y, "░");
201
 
            else
202
 
                cucul_put_char(out, x, y, ' ');
203
 
        }
204
 
        break;
205
 
    case HALFBLOCKS:
206
 
        for(y = 0; y < gh; y++)
207
 
            for(x = 0; x < mygw; x++)
208
 
        {
209
 
            static char const *str[] = { " ", "▀", "▄", "█" };
210
 
 
211
 
            uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
212
 
            uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
213
 
 
214
 
            cucul_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80)]);
215
 
        }
216
 
        break;
217
 
    case QUARTERBLOCKS:
218
 
        for(y = 0; y < gh; y++)
219
 
            for(x = 0; x < mygw; x++)
220
 
        {
221
 
            static char const *str[] =
222
 
            {
223
 
                " ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
224
 
                "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█"
225
 
            };
226
 
 
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];
231
 
 
232
 
            cucul_put_str(out, x, y, str[(p1 > 0x80) + 2 * (p2 > 0x80) +
233
 
                                         4 * (p3 > 0x80) + 8 * (p4 > 0x80)]);
234
 
        }
235
 
        break;
236
 
    }
237
 
 
238
 
    if(ch == ' ' || ch == 0xa0)
239
 
    {
240
 
        cucul_draw_line(out, mygw - 1, 0, mygw - 1, gh - 1, '$');
241
 
        cucul_draw_line(out, mygw / 2, 0, mygw / 2, gh - 1, '$');
242
 
    }
243
 
 
244
 
    cucul_draw_line(out, mygw, 0, mygw, gh - 1, '@');
245
 
    cucul_put_char(out, mygw + 1, gh - 1, '@');
246
 
 
247
 
    buf = cucul_export_memory(out, "utf8", &len);
248
 
    fwrite(buf, len, 1, stdout);
249
 
    free(buf);
250
 
}
251