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

Last change on this file since 1291 was 1291, checked in by Sam Hocevar, 14 years ago
  • Added the caca2tlf tool, which converts a libcaca font into a TOIlet font using a naive pixel-to-glyphs algorithm. I will improve it later.
  • Property svn:keywords set to Id
File size: 3.7 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 1291 2006-11-06 01:09:44Z 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 <stdlib.h>
25#include <cucul.h>
26
27static void list_fonts(void);
28static void add_char(unsigned int);
29
30cucul_font_t *f;
31cucul_canvas_t *out, *onechar;
32unsigned long int const *blocks;
33uint8_t * image;
34unsigned int width, height;
35
36int main(int argc, char *argv[])
37{
38    unsigned int b, i;
39
40    if(argc < 2)
41    {
42        fprintf(stderr, "Wrong argument count. Usage: caca2tlf <font>\n");
43        list_fonts();
44        return -1;
45    }
46
47    f = cucul_load_font(argv[1], 0);
48
49    if(!f)
50    {
51        fprintf(stderr, "Font \"%s\" not found.\n", argv[1]);
52        list_fonts();
53        return -2;
54    }
55
56    width = cucul_get_font_width(f);
57    height = cucul_get_font_height(f);
58    blocks = cucul_get_font_blocks(f);
59    onechar = cucul_create_canvas(1, 1); /* FIXME: support double width */
60    cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
61    out = cucul_create_canvas(width + 2, height);
62    image = malloc(4 * width * height);
63
64    printf("tlf2a$ %u %u %u 0 3 0 0 0\n", height, height - 1, width + 2);
65
66    printf("================================================================================\n");
67    printf("  This font was automatically generated by caca2tlf\n");
68    printf("================================================================================\n");
69
70    for(i = 32; i < 127; i++)
71        add_char(i);
72
73    add_char(196);
74    add_char(214);
75    add_char(220);
76    add_char(228);
77    add_char(246);
78    add_char(252);
79    add_char(223);
80
81    for(b = 0, i = 0; blocks[i + 1]; i += 2)
82    {
83        int j, n = (int)(blocks[i + 1] - blocks[i]);
84
85        for(j = 0; j < n; j++)
86        {
87            char buf[7];
88            unsigned int len;
89            unsigned long int ch = blocks[i] + j;
90
91            if(ch < 0x80)
92                continue;
93
94            len = cucul_utf32_to_utf8(buf, ch);
95            buf[len] = '\0';
96            printf("0x%.04lX %s\n", ch, buf);
97            add_char(ch);
98        }
99    }
100
101    cucul_free_canvas(out);
102    cucul_free_canvas(onechar);
103    free(image);
104    cucul_free_font(f);
105
106    return 0;
107}
108
109static void list_fonts(void)
110{
111    char const * const * fonts;
112    unsigned int i;
113       
114    fprintf(stderr, "Available fonts:\n");
115
116    fonts = cucul_get_font_list();
117    for(i = 0; fonts[i]; i++)
118        fprintf(stderr, "  \"%s\"\n", fonts[i]);
119}
120
121static void add_char(unsigned int ch)
122{
123    cucul_buffer_t *buf;
124    unsigned int x, y;
125
126    cucul_putchar(onechar, 0, 0, ch);
127    cucul_render_canvas(onechar, f, image, width, height, 4 * width);
128
129    for(y = 0; y < height; y++)
130       for(x = 0; x < width; x++)
131    {
132        uint8_t c = image[4 * (x + y * width) + 2];
133
134        if(c >= 0xa0)
135            cucul_putstr(out, x, y, "█");
136        else if(c >= 0x80)
137            cucul_putstr(out, x, y, "▓");
138        else if(c >= 0x40)
139            cucul_putstr(out, x, y, "▒");
140        else if(c >= 0x20)
141            cucul_putstr(out, x, y, "░");
142        else
143            cucul_putchar(out, x, y, ' ');
144    }
145    cucul_draw_line(out, width, 0, width, height - 1, "@");
146    cucul_putchar(out, width + 1, height - 1, '@');
147
148    buf = cucul_export_canvas(out, "utf8");
149    fwrite(cucul_get_buffer_data(buf), cucul_get_buffer_size(buf), 1, stdout);
150    cucul_free_buffer(buf);
151}
152
Note: See TracBrowser for help on using the repository browser.