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

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