source: toilet/trunk/src/figlet.c @ 1170

Last change on this file since 1170 was 1170, checked in by Sam Hocevar, 16 years ago
  • Allow the hardblank character to be UTF-8.
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: figlet.c 1170 2006-10-01 07:55:45Z 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 file contains functions for handling FIGlet fonts.
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 <string.h>
26#include <cucul.h>
27
28#include "toilet.h"
29#include "figlet.h"
30
31#define STD_GLYPHS (127 - 32)
32#define EXT_GLYPHS (STD_GLYPHS + 7)
33
34struct figfont
35{
36    /* From the font format */
37    unsigned long int hardblank;
38    unsigned int height, baseline, max_length;
39    int old_layout;
40    unsigned int print_direction, full_layout, codetag_count;
41
42    unsigned int glyphs;
43    cucul_canvas_t *image;
44    unsigned int *lookup;
45};
46
47static struct figfont *open_font(void);
48static void free_font(struct figfont *);
49
50cucul_canvas_t *render_figlet(uint32_t const *string, unsigned int length)
51{
52    cucul_canvas_t *cv;
53    struct figfont *font;
54    unsigned int x, i, c;
55
56    font = open_font();
57
58    if(!font)
59        return NULL;
60
61    cv = cucul_create_canvas(length * font->max_length, font->height);
62
63    for(x = 0, i = 0; i < length; i++)
64    {
65        for(c = 0; c < font->glyphs; c++)
66            if(font->lookup[c * 2] == string[i])
67                break;
68
69        if(c == font->glyphs)
70            continue;
71
72        cucul_blit(cv, x, - (int)(c * font->height), font->image, NULL);
73
74        x += font->lookup[c * 2 + 1];
75    }
76
77    cucul_set_canvas_boundaries(cv, 0, 0, x, font->height);
78
79    free_font(font);
80
81    return cv;
82}
83
84static struct figfont *open_font(void)
85{
86    char *data = NULL;
87    char path[2048];
88    char hardblank[10];
89    struct figfont *font;
90    cucul_buffer_t *b;
91    FILE *f;
92    unsigned int i, j, size, comment_lines;
93
94    /* Open font: try .tlf, then .flf */
95    snprintf(path, 2047, "%s/%s.tlf", toilet_dir, toilet_font);
96    path[2047] = '\0';
97    f = fopen(path, "r");
98    if(!f)
99    {
100        snprintf(path, 2047, "%s/%s.flf", toilet_dir, toilet_font);
101        path[2047] = '\0';
102        f = fopen(path, "r");
103        if(!f)
104        {
105            fprintf(stderr, "font `%s' not found\n", path);
106            return NULL;
107        }
108    }
109
110    font = malloc(sizeof(struct figfont));
111
112    /* Read header */
113    font->print_direction = 0;
114    font->full_layout = 0;
115    font->codetag_count = 0;
116    if(fscanf(f, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank,
117              &font->height, &font->baseline, &font->max_length,
118              &font->old_layout, &comment_lines, &font->print_direction,
119              &font->full_layout, &font->codetag_count) < 6)
120    {
121        fprintf(stderr, "font `%s' has invalid header\n", path);
122        free(font);
123        fclose(f);
124        return NULL;
125    }
126
127    font->hardblank = cucul_utf8_to_utf32(hardblank, NULL);
128
129    /* Skip comment lines */
130    for(i = 0; i < comment_lines; i++)
131    {
132        fscanf(f, "%*[^\n]");
133        fscanf(f, "%*c");
134    }
135
136    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
137     * then read additional characters. */
138    font->glyphs = 0;
139    font->lookup = NULL;
140
141    for(i = 0, size = 0; !feof(f); font->glyphs++)
142    {
143        if((font->glyphs % 2048) == 0)
144            font->lookup = realloc(font->lookup,
145                                   (font->glyphs + 2048) * 2 * sizeof(int));
146
147        if(font->glyphs < STD_GLYPHS)
148        {
149            font->lookup[font->glyphs * 2] = 32 + font->glyphs;
150        }
151        else if(font->glyphs < EXT_GLYPHS)
152        {
153            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
154            font->lookup[font->glyphs * 2] = tab[font->glyphs - STD_GLYPHS];
155        }
156        else
157        {
158            char number[10];
159            int ret = fscanf(f, "%s %*[^\n]", number);
160
161            if(ret == EOF)
162                break;
163
164            if(!ret)
165            {
166                free(data);
167                free(font->lookup);
168                free(font);
169                fprintf(stderr, "read error at glyph %u in `%s'\n",
170                                font->glyphs, path);
171                return NULL;
172            }
173
174            if(number[1] == 'x')
175                sscanf(number, "%x", &font->lookup[font->glyphs * 2]);
176            else
177                sscanf(number, "%u", &font->lookup[font->glyphs * 2]);
178
179            fscanf(f, "%*c");
180        }
181
182        font->lookup[font->glyphs * 2 + 1] = 0;
183
184        for(j = 0; j < font->height; j++)
185        {
186            if(i + 2048 >= size)
187                data = realloc(data, size += 2048);
188
189            fgets(data + i, 2048, f);
190            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
191        }
192    }
193
194    fclose(f);
195
196    if(font->glyphs < EXT_GLYPHS)
197    {
198        free(data);
199        free(font->lookup);
200        free(font);
201        fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n",
202                        font->glyphs, path, EXT_GLYPHS);
203        return NULL;
204    }
205
206    /* Import buffer into canvas */
207    b = cucul_load_memory(data, i);
208    font->image = cucul_import_canvas(b, "utf8");
209    cucul_free_buffer(b);
210    free(data);
211
212    if(!font->image)
213    {
214        free(font->lookup);
215        free(font);
216        fprintf(stderr, "libcucul could not load data in `%s'\n", path);
217        return NULL;
218    }
219
220    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
221     * smushing, nor any kind of error checking. */
222    for(j = 0; j < font->height * font->glyphs; j++)
223    {
224        unsigned long int ch, oldch = 0;
225
226        for(i = font->max_length; i--;)
227        {
228            ch = cucul_getchar(font->image, i, j);
229
230            if(ch == font->hardblank)
231                cucul_putchar(font->image, i, j, ch = ' ');
232
233            if(oldch && ch != oldch)
234            {
235                if(!font->lookup[j / font->height * 2 + 1])
236                    font->lookup[j / font->height * 2 + 1] = i + 1;
237            }
238            else if(oldch && ch == oldch)
239                cucul_putchar(font->image, i, j, ' ');
240            else if(ch != ' ')
241            {
242                oldch = ch;
243                cucul_putchar(font->image, i, j, ' ');
244            }
245        }
246    }
247
248    return font;
249}
250
251static void free_font(struct figfont *font)
252{
253    cucul_free_canvas(font->image);
254    free(font->lookup);
255    free(font);
256}
257
Note: See TracBrowser for help on using the repository browser.