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

Last change on this file since 1242 was 1242, checked in by Sam Hocevar, 13 years ago
  • Factor code from renderers to main render.c functions.
  • Property svn:keywords set to Id
File size: 7.0 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 1242 2006-10-26 13:14:37Z 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            if(!buf[0])
202            {
203                free(data);
204                free(cx->lookup);
205                fprintf(stderr, "read error at glyph %u in `%s'\n",
206                                cx->glyphs, path);
207                return -1;
208            }
209
210            if(buf[1] == 'x')
211                sscanf(buf, "%x", &cx->lookup[cx->glyphs * 2]);
212            else
213                sscanf(buf, "%u", &cx->lookup[cx->glyphs * 2]);
214        }
215
216        cx->lookup[cx->glyphs * 2 + 1] = 0;
217
218        for(j = 0; j < cx->height; j++)
219        {
220            if(i + 2048 >= size)
221                data = realloc(data, size += 2048);
222
223            toigets(data + i, 2048, f);
224            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
225        }
226    }
227
228    toiclose(f);
229
230    if(cx->glyphs < EXT_GLYPHS)
231    {
232        free(data);
233        free(cx->lookup);
234        fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n",
235                        cx->glyphs, path, EXT_GLYPHS);
236        return -1;
237    }
238
239    /* Import buffer into canvas */
240    b = cucul_load_memory(data, i);
241    cx->image = cucul_import_canvas(b, "utf8");
242    cucul_free_buffer(b);
243    free(data);
244
245    if(!cx->image)
246    {
247        free(cx->lookup);
248        fprintf(stderr, "libcucul could not load data in `%s'\n", path);
249        return -1;
250    }
251
252    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
253     * smushing, nor any kind of error checking. */
254    for(j = 0; j < cx->height * cx->glyphs; j++)
255    {
256        unsigned long int ch, oldch = 0;
257
258        for(i = cx->max_length; i--;)
259        {
260            ch = cucul_getchar(cx->image, i, j);
261
262            /* TODO: Replace hardblanks with U+00A0 NO-BREAK SPACE */
263            if(ch == cx->hardblank)
264                cucul_putchar(cx->image, i, j, ch = ' ');
265                //cucul_putchar(cx->image, i, j, ch = 0xa0);
266
267            if(oldch && ch != oldch)
268            {
269                if(!cx->lookup[j / cx->height * 2 + 1])
270                    cx->lookup[j / cx->height * 2 + 1] = i + 1;
271            }
272            else if(oldch && ch == oldch)
273                cucul_putchar(cx->image, i, j, ' ');
274            else if(ch != ' ')
275            {
276                oldch = ch;
277                cucul_putchar(cx->image, i, j, ' ');
278            }
279        }
280    }
281
282    return 0;
283}
284
Note: See TracBrowser for help on using the repository browser.