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

Last change on this file since 1151 was 1151, checked in by Sam Hocevar, 16 years ago
  • Fixed a few unimportant memory leaks upon error.
  • Property svn:keywords set to Id
File size: 6.4 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 1151 2006-09-30 18:06:47Z 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 char 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    struct figfont *font;
89    cucul_buffer_t *b;
90    FILE *f;
91    unsigned int i, j, size, comment_lines;
92
93    /* Open font: try .tlf, then .flf */
94    snprintf(path, 2047, "%s/%s.tlf", toilet_dir, toilet_font);
95    path[2047] = '\0';
96    f = fopen(path, "r");
97    if(!f)
98    {
99        snprintf(path, 2047, "%s/%s.flf", toilet_dir, toilet_font);
100        path[2047] = '\0';
101        f = fopen(path, "r");
102        if(!f)
103        {
104            fprintf(stderr, "font `%s' not found\n", path);
105            return NULL;
106        }
107    }
108
109    font = malloc(sizeof(struct figfont));
110
111    /* Read header */
112    font->print_direction = 0;
113    font->full_layout = 0;
114    font->codetag_count = 0;
115    if(fscanf(f, "%*[ft]lf2a%c %u %u %u %i %u %u %u %u\n", &font->hardblank,
116              &font->height, &font->baseline, &font->max_length,
117              &font->old_layout, &comment_lines, &font->print_direction,
118              &font->full_layout, &font->codetag_count) < 6)
119    {
120        fprintf(stderr, "font `%s' has invalid header\n", path);
121        free(font);
122        fclose(f);
123        return NULL;
124    }
125
126    /* Skip comment lines */
127    for(i = 0; i < comment_lines; i++)
128    {
129        fscanf(f, "%*[^\n]");
130        fscanf(f, "%*c");
131    }
132
133    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
134     * then read additional characters. */
135    font->glyphs = 0;
136    font->lookup = NULL;
137
138    for(i = 0, size = 0; !feof(f); font->glyphs++)
139    {
140        if((font->glyphs % 2048) == 0)
141            font->lookup = realloc(font->lookup,
142                                   (font->glyphs + 2048) * 2 * sizeof(int));
143
144        if(font->glyphs < STD_GLYPHS)
145        {
146            font->lookup[font->glyphs * 2] = 32 + font->glyphs;
147        }
148        else if(font->glyphs < EXT_GLYPHS)
149        {
150            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
151            font->lookup[font->glyphs * 2] = tab[font->glyphs - STD_GLYPHS];
152        }
153        else
154        {
155            char number[10];
156            int ret = fscanf(f, "%s %*[^\n]", number);
157
158            if(ret == EOF)
159                break;
160
161            if(!ret)
162            {
163                free(data);
164                free(font->lookup);
165                free(font);
166                fprintf(stderr, "read error at glyph %u in `%s'\n",
167                                font->glyphs, path);
168                return NULL;
169            }
170
171            if(number[1] == 'x')
172                sscanf(number, "%x", &font->lookup[font->glyphs * 2]);
173            else
174                sscanf(number, "%u", &font->lookup[font->glyphs * 2]);
175
176            fscanf(f, "%*c");
177        }
178
179        font->lookup[font->glyphs * 2 + 1] = 0;
180
181        for(j = 0; j < font->height; j++)
182        {
183            if(i + 2048 >= size)
184                data = realloc(data, size += 2048);
185
186            fgets(data + i, 2048, f);
187            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
188        }
189    }
190
191    fclose(f);
192
193    if(font->glyphs < EXT_GLYPHS)
194    {
195        free(data);
196        free(font->lookup);
197        free(font);
198        fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n",
199                        font->glyphs, path, EXT_GLYPHS);
200        return NULL;
201    }
202
203    /* Import buffer into canvas */
204    b = cucul_load_memory(data, i);
205    font->image = cucul_import_canvas(b, "utf8");
206    cucul_free_buffer(b);
207    free(data);
208
209    if(!font->image)
210    {
211        free(font->lookup);
212        free(font);
213        fprintf(stderr, "libcucul could not load data in `%s'\n", path);
214        return NULL;
215    }
216
217    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
218     * smushing, nor any kind of error checking. */
219    for(j = 0; j < font->height * font->glyphs; j++)
220    {
221        unsigned long int ch, oldch = 0;
222
223        for(i = font->max_length; i--;)
224        {
225            ch = cucul_getchar(font->image, i, j);
226
227            if(ch == font->hardblank)
228                cucul_putchar(font->image, i, j, ch = ' ');
229
230            if(oldch && ch != oldch)
231            {
232                if(!font->lookup[j / font->height * 2 + 1])
233                    font->lookup[j / font->height * 2 + 1] = i + 1;
234            }
235            else if(oldch && ch == oldch)
236                cucul_putchar(font->image, i, j, ' ');
237            else if(ch != ' ')
238            {
239                oldch = ch;
240                cucul_putchar(font->image, i, j, ' ');
241            }
242        }
243    }
244
245    return font;
246}
247
248static void free_font(struct figfont *font)
249{
250    cucul_free_canvas(font->image);
251    free(font->lookup);
252    free(font);
253}
254
Note: See TracBrowser for help on using the repository browser.