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

Last change on this file since 1376 was 1376, checked in by Sam Hocevar, 13 years ago
  • Now that libcaca 0.99.beta10 is out, we can switch to the new API.
  • Property svn:keywords set to Id
File size: 7.2 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 1376 2006-11-12 20:37:58Z 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_get_char(cx->image, x, y + c * cx->height);
100        cucul_put_char(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    TOIFILE *f;
136    unsigned int i, j, size, comment_lines;
137
138    /* Open font: try .tlf, then .flf */
139    snprintf(path, 2047, "%s/%s.tlf", cx->dir, cx->font);
140    path[2047] = '\0';
141    f = toiopen(path, "r");
142    if(!f)
143    {
144        snprintf(path, 2047, "%s/%s.flf", cx->dir, cx->font);
145        path[2047] = '\0';
146        f = toiopen(path, "r");
147        if(!f)
148        {
149            fprintf(stderr, "font `%s' not found\n", path);
150            return -1;
151        }
152    }
153
154    /* Read header */
155    cx->print_direction = 0;
156    cx->full_layout = 0;
157    cx->codetag_count = 0;
158    toigets(buf, 2048, f);
159    if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank,
160              &cx->height, &cx->baseline, &cx->max_length,
161              &cx->old_layout, &comment_lines, &cx->print_direction,
162              &cx->full_layout, &cx->codetag_count) < 6)
163    {
164        fprintf(stderr, "font `%s' has invalid header: %s\n", path, buf);
165        toiclose(f);
166        return -1;
167    }
168
169    cx->hardblank = cucul_utf8_to_utf32(hardblank, NULL);
170
171    /* Skip comment lines */
172    for(i = 0; i < comment_lines; i++)
173        toigets(buf, 2048, f);
174
175    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
176     * then read additional characters. */
177    cx->glyphs = 0;
178    cx->lookup = NULL;
179
180    for(i = 0, size = 0; !toieof(f); cx->glyphs++)
181    {
182        if((cx->glyphs % 2048) == 0)
183            cx->lookup = realloc(cx->lookup,
184                                   (cx->glyphs + 2048) * 2 * sizeof(int));
185
186        if(cx->glyphs < STD_GLYPHS)
187        {
188            cx->lookup[cx->glyphs * 2] = 32 + cx->glyphs;
189        }
190        else if(cx->glyphs < EXT_GLYPHS)
191        {
192            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
193            cx->lookup[cx->glyphs * 2] = tab[cx->glyphs - STD_GLYPHS];
194        }
195        else
196        {
197            if(toigets(buf, 2048, f) == NULL)
198                break;
199
200            /* Ignore blank lines, as in jacky.flf */
201            if(buf[0] == '\n' || buf[0] == '\r')
202                continue;
203
204            /* Ignore negative indices for now, as in ivrit.flf */
205            if(buf[0] == '-')
206            {
207                for(j = 0; j < cx->height; j++)
208                    toigets(buf, 2048, f);
209                continue;
210            }
211
212            if(!buf[0] || buf[0] < '0' || buf[0] > '9')
213            {
214                free(data);
215                free(cx->lookup);
216                fprintf(stderr, "read error at glyph #%u in `%s'\n",
217                                cx->glyphs, path);
218                return -1;
219            }
220
221            if(buf[1] == 'x')
222                sscanf(buf, "%x", &cx->lookup[cx->glyphs * 2]);
223            else
224                sscanf(buf, "%u", &cx->lookup[cx->glyphs * 2]);
225        }
226
227        cx->lookup[cx->glyphs * 2 + 1] = 0;
228
229        for(j = 0; j < cx->height; j++)
230        {
231            if(i + 2048 >= size)
232                data = realloc(data, size += 2048);
233
234            toigets(data + i, 2048, f);
235            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
236        }
237    }
238
239    toiclose(f);
240
241    if(cx->glyphs < EXT_GLYPHS)
242    {
243        free(data);
244        free(cx->lookup);
245        fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n",
246                        cx->glyphs, path, EXT_GLYPHS);
247        return -1;
248    }
249
250    /* Import buffer into canvas */
251    cx->image = cucul_create_canvas(0, 0);
252    cucul_import_memory(cx->image, data, i, "utf8");
253    free(data);
254
255    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
256     * smushing, nor any kind of error checking. */
257    for(j = 0; j < cx->height * cx->glyphs; j++)
258    {
259        unsigned long int ch, oldch = 0;
260
261        for(i = cx->max_length; i--;)
262        {
263            ch = cucul_get_char(cx->image, i, j);
264
265            /* TODO: Replace hardblanks with U+00A0 NO-BREAK SPACE */
266            if(ch == cx->hardblank)
267                cucul_put_char(cx->image, i, j, ch = ' ');
268                //cucul_put_char(cx->image, i, j, ch = 0xa0);
269
270            if(oldch && ch != oldch)
271            {
272                if(!cx->lookup[j / cx->height * 2 + 1])
273                    cx->lookup[j / cx->height * 2 + 1] = i + 1;
274            }
275            else if(oldch && ch == oldch)
276                cucul_put_char(cx->image, i, j, ' ');
277            else if(ch != ' ')
278            {
279                oldch = ch;
280                cucul_put_char(cx->image, i, j, ' ');
281            }
282        }
283    }
284
285    return 0;
286}
287
Note: See TracBrowser for help on using the repository browser.