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

Last change on this file since 1143 was 1143, checked in by Sam Hocevar, 14 years ago
  • Better error checking in the figlet renderer.
  • Allow to open fonts that have Unicode data (requires libcucul 0.99.beta6 which is not yet released).
  • Property svn:keywords set to Id
File size: 5.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 1143 2006-09-30 11:06:20Z 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
31struct figfont
32{
33    /* From the font format */
34    unsigned char hardblank;
35    unsigned int height, baseline, max_length;
36    int old_layout;
37    unsigned int print_direction, full_layout, codetag_count;
38
39    unsigned int glyphs;
40    cucul_canvas_t *image;
41    unsigned int *lookup;
42};
43
44static struct figfont *open_font(void);
45static void free_font(struct figfont *);
46
47cucul_canvas_t *render_figlet(uint32_t const *string, unsigned int length)
48{
49    cucul_canvas_t *cv;
50    struct figfont *font;
51    unsigned int x, i, c;
52
53    font = open_font();
54
55    if(!font)
56        return NULL;
57
58    cv = cucul_create_canvas(length * font->max_length, font->height);
59
60    for(x = 0, i = 0; i < length; i++)
61    {
62        for(c = 0; c < font->glyphs; c++)
63            if(font->lookup[c * 2] == string[i])
64                break;
65
66        if(c == font->glyphs)
67            continue;
68
69        cucul_blit(cv, x, - (int)(c * font->height), font->image, NULL);
70
71        x += font->lookup[c * 2 + 1];
72    }
73
74    cucul_set_canvas_boundaries(cv, 0, 0, x, font->height);
75
76    free_font(font);
77
78    return cv;
79}
80
81static struct figfont *open_font(void)
82{
83    char *data = NULL;
84    char path[2048];
85    struct figfont *font;
86    cucul_buffer_t *b;
87    FILE *f;
88    unsigned int i, j, size, comment_lines;
89
90    /* Open font: try .tlf, then .flf */
91    snprintf(path, 2047, "%s/%s.tlf", toilet_dir, toilet_font);
92    path[2047] = '\0';
93    f = fopen(path, "r");
94    if(!f)
95    {
96        snprintf(path, 2047, "%s/%s.flf", toilet_dir, toilet_font);
97        path[2047] = '\0';
98        f = fopen(path, "r");
99        if(!f)
100        {
101            fprintf(stderr, "font `%s' not found\n", path);
102            return NULL;
103        }
104    }
105
106    font = malloc(sizeof(struct figfont));
107
108    /* Read header */
109    font->print_direction = 0;
110    font->full_layout = 0;
111    font->codetag_count = 0;
112    if(fscanf(f, "%*[ft]lf2a%c %u %u %u %i %u %u %u %u\n", &font->hardblank,
113              &font->height, &font->baseline, &font->max_length,
114              &font->old_layout, &comment_lines, &font->print_direction,
115              &font->full_layout, &font->codetag_count) < 6)
116    {
117        fprintf(stderr, "font `%s' has invalid header\n", path);
118        free(font);
119        fclose(f);
120        return NULL;
121    }
122
123    /* Skip comment lines */
124    for(i = 0; i < comment_lines; i++)
125    {
126        fscanf(f, "%*[^\n]");
127        fscanf(f, "%*c");
128    }
129
130    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
131     * then read additional characters. */
132    font->glyphs = 0;
133    font->lookup = NULL;
134
135    for(i = 0, size = 0; !feof(f); font->glyphs++)
136    {
137        if((font->glyphs % 2048) == 0)
138            font->lookup = realloc(font->lookup,
139                                   (font->glyphs + 2048) * 2 * sizeof(int));
140
141        if(font->glyphs < 127 - 32)
142        {
143            font->lookup[font->glyphs * 2] = 32 + font->glyphs;
144        }
145        else if(font->glyphs < (127 - 32) + 7)
146        {
147            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
148            font->lookup[font->glyphs * 2] = tab[font->glyphs + (127 - 32)];
149        }
150        else
151        {
152            char number[10];
153
154            fscanf(f, "%s %*[^\n]", number);
155
156            if(number[1] == 'x')
157                sscanf(number, "%x", &font->lookup[font->glyphs * 2]);
158            else
159                sscanf(number, "%u", &font->lookup[font->glyphs * 2]);
160
161            fscanf(f, "%*c");
162        }
163
164        font->lookup[font->glyphs * 2 + 1] = 0;
165
166        for(j = 0; j < font->height; j++)
167        {
168            if(i + 2048 >= size)
169                data = realloc(data, size += 2048);
170
171            fgets(data + i, 2048, f);
172            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
173        }
174    }
175
176    fclose(f);
177
178    /* Import buffer into canvas */
179    b = cucul_load_memory(data, i);
180    font->image = cucul_import_canvas(b, "utf8");
181    cucul_free_buffer(b);
182    free(data);
183
184    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
185     * smushing, nor any kind of error checking. */
186    for(j = 0; j < font->height * font->glyphs; j++)
187    {
188        unsigned long int ch, oldch = 0;
189
190        for(i = font->max_length; i--;)
191        {
192            ch = cucul_getchar(font->image, i, j);
193
194            if(ch == font->hardblank)
195                cucul_putchar(font->image, i, j, ch = ' ');
196
197            if(oldch && ch != oldch)
198            {
199                if(!font->lookup[j / font->height * 2 + 1])
200                    font->lookup[j / font->height * 2 + 1] = i + 1;
201            }
202            else if(oldch && ch == oldch)
203                cucul_putchar(font->image, i, j, ' ');
204            else if(ch != ' ')
205            {
206                oldch = ch;
207                cucul_putchar(font->image, i, j, ' ');
208            }
209        }
210    }
211
212    return font;
213}
214
215static void free_font(struct figfont *font)
216{
217    free(font->lookup);
218    free(font);
219}
220
Note: See TracBrowser for help on using the repository browser.