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

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