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

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