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

Last change on this file since 1298 was 1298, checked in by Sam Hocevar, 14 years ago
  • Make figlet font importer more tolerant with empty lines.
  • Property svn:keywords set to Id
File size: 7.1 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 1298 2006-11-06 17:33:31Z 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 "render.h"
30#include "io.h"
31
32#define STD_GLYPHS (127 - 32)
33#define EXT_GLYPHS (STD_GLYPHS + 7)
34
35static int feed_figlet(context_t *, uint32_t);
36static int flush_figlet(context_t *);
37static int end_figlet(context_t *);
38
39static int open_font(context_t *cx);
40
41int init_figlet(context_t *cx)
42{
43    if(open_font(cx))
44        return -1;
45
46    cx->feed = feed_figlet;
47    cx->flush = flush_figlet;
48    cx->end = end_figlet;
49
50    return 0;
51}
52
53static int feed_figlet(context_t *cx, uint32_t ch)
54{
55    unsigned int c, w, h, x, y;
56
57    switch(ch)
58    {
59        case (uint32_t)'\r':
60            return 0;
61        case (uint32_t)'\n':
62            cx->x = 0;
63            cx->y += cx->height;
64            return 0;
65        /* FIXME: handle '\t' */
66    }
67
68    /* Look whether our glyph is available */
69    for(c = 0; c < cx->glyphs; c++)
70        if(cx->lookup[c * 2] == ch)
71            break;
72
73    if(c == cx->glyphs)
74        return 0;
75
76    w = cx->lookup[c * 2 + 1];
77    h = cx->height;
78
79    /* Check whether we reached the end of the screen */
80    if(cx->x && cx->x + w > cx->term_width)
81    {
82        cx->x = 0;
83        cx->y += h;
84    }
85
86    /* Check whether the current canvas is large enough */
87    if(cx->x + w > cx->w)
88        cx->w = cx->x + w < cx->term_width ? cx->x + w : cx->term_width;
89
90    if(cx->y + h > cx->h)
91        cx->h = cx->y + h;
92
93    cucul_set_canvas_size(cx->cv, cx->w, cx->h);
94
95    /* Render our char (FIXME: create a rect-aware cucul_blit_canvas?) */
96    for(y = 0; y < h; y++)
97        for(x = 0; x < w; x++)
98    {
99        uint32_t tmp = cucul_getchar(cx->image, x, y + c * cx->height);
100        cucul_putchar(cx->cv, cx->x + x, cx->y + y, tmp);
101    }
102
103    /* Advance cursor */
104    cx->x += w;
105
106    return 0;
107}
108
109static int flush_figlet(context_t *cx)
110{
111    cx->torender = cx->cv;
112    cucul_set_canvas_size(cx->torender, cx->w, cx->h);
113
114    cx->x = cx->y = 0;
115    cx->w = cx->h = 0;
116    cx->cv = cucul_create_canvas(1, 1);
117
118    return 0;
119}
120
121static int end_figlet(context_t *cx)
122{
123    cucul_free_canvas(cx->image);
124    free(cx->lookup);
125
126    return 0;
127}
128
129static int open_font(context_t *cx)
130{
131    char *data = NULL;
132    char path[2048];
133    char buf[2048];
134    char hardblank[10];
135    cucul_buffer_t *b;
136    TOIFILE *f;
137    unsigned int i, j, size, comment_lines;
138
139    /* Open font: try .tlf, then .flf */
140    snprintf(path, 2047, "%s/%s.tlf", cx->dir, cx->font);
141    path[2047] = '\0';
142    f = toiopen(path, "r");
143    if(!f)
144    {
145        snprintf(path, 2047, "%s/%s.flf", cx->dir, cx->font);
146        path[2047] = '\0';
147        f = toiopen(path, "r");
148        if(!f)
149        {
150            fprintf(stderr, "font `%s' not found\n", path);
151            return -1;
152        }
153    }
154
155    /* Read header */
156    cx->print_direction = 0;
157    cx->full_layout = 0;
158    cx->codetag_count = 0;
159    toigets(buf, 2048, f);
160    if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank,
161              &cx->height, &cx->baseline, &cx->max_length,
162              &cx->old_layout, &comment_lines, &cx->print_direction,
163              &cx->full_layout, &cx->codetag_count) < 6)
164    {
165        fprintf(stderr, "font `%s' has invalid header: %s\n", path, buf);
166        toiclose(f);
167        return -1;
168    }
169
170    cx->hardblank = cucul_utf8_to_utf32(hardblank, NULL);
171
172    /* Skip comment lines */
173    for(i = 0; i < comment_lines; i++)
174        toigets(buf, 2048, f);
175
176    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
177     * then read additional characters. */
178    cx->glyphs = 0;
179    cx->lookup = NULL;
180
181    for(i = 0, size = 0; !toieof(f); cx->glyphs++)
182    {
183        if((cx->glyphs % 2048) == 0)
184            cx->lookup = realloc(cx->lookup,
185                                   (cx->glyphs + 2048) * 2 * sizeof(int));
186
187        if(cx->glyphs < STD_GLYPHS)
188        {
189            cx->lookup[cx->glyphs * 2] = 32 + cx->glyphs;
190        }
191        else if(cx->glyphs < EXT_GLYPHS)
192        {
193            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
194            cx->lookup[cx->glyphs * 2] = tab[cx->glyphs - STD_GLYPHS];
195        }
196        else
197        {
198            if(toigets(buf, 2048, f) == NULL)
199                break;
200
201            /* Ignore blank lines, as in jacky.flf */
202            if(buf[0] == '\n' || buf[0] == '\r')
203                continue;
204
205            if(!buf[0] || buf[0] < '0' || buf[0] > '9')
206            {
207                free(data);
208                free(cx->lookup);
209                fprintf(stderr, "read error at glyph #%u in `%s'\n",
210                                cx->glyphs, path);
211                return -1;
212            }
213
214            if(buf[1] == 'x')
215                sscanf(buf, "%x", &cx->lookup[cx->glyphs * 2]);
216            else
217                sscanf(buf, "%u", &cx->lookup[cx->glyphs * 2]);
218        }
219
220        cx->lookup[cx->glyphs * 2 + 1] = 0;
221
222        for(j = 0; j < cx->height; j++)
223        {
224            if(i + 2048 >= size)
225                data = realloc(data, size += 2048);
226
227            toigets(data + i, 2048, f);
228            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
229        }
230    }
231
232    toiclose(f);
233
234    if(cx->glyphs < EXT_GLYPHS)
235    {
236        free(data);
237        free(cx->lookup);
238        fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n",
239                        cx->glyphs, path, EXT_GLYPHS);
240        return -1;
241    }
242
243    /* Import buffer into canvas */
244    b = cucul_load_memory(data, i);
245    cx->image = cucul_import_canvas(b, "utf8");
246    cucul_free_buffer(b);
247    free(data);
248
249    if(!cx->image)
250    {
251        free(cx->lookup);
252        fprintf(stderr, "libcucul could not load data in `%s'\n", path);
253        return -1;
254    }
255
256    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
257     * smushing, nor any kind of error checking. */
258    for(j = 0; j < cx->height * cx->glyphs; j++)
259    {
260        unsigned long int ch, oldch = 0;
261
262        for(i = cx->max_length; i--;)
263        {
264            ch = cucul_getchar(cx->image, i, j);
265
266            /* TODO: Replace hardblanks with U+00A0 NO-BREAK SPACE */
267            if(ch == cx->hardblank)
268                cucul_putchar(cx->image, i, j, ch = ' ');
269                //cucul_putchar(cx->image, i, j, ch = 0xa0);
270
271            if(oldch && ch != oldch)
272            {
273                if(!cx->lookup[j / cx->height * 2 + 1])
274                    cx->lookup[j / cx->height * 2 + 1] = i + 1;
275            }
276            else if(oldch && ch == oldch)
277                cucul_putchar(cx->image, i, j, ' ');
278            else if(ch != ' ')
279            {
280                oldch = ch;
281                cucul_putchar(cx->image, i, j, ' ');
282            }
283        }
284    }
285
286    return 0;
287}
288
Note: See TracBrowser for help on using the repository browser.