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

Last change on this file since 1113 was 1113, checked in by Sam Hocevar, 15 years ago
  • Crop the final canvas to the effective width.
  • Fixed a bug that prevented loading characters after 127.
  • Allow to load fonts that hex-encode their Unicode values.
  • Property svn:keywords set to Id
File size: 5.3 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 1113 2006-09-26 21:58:26Z 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 "figlet.h"
29
30struct figfont
31{
32    /* From the font format */
33    unsigned char hardblank;
34    unsigned int height, baseline, max_length;
35    int old_layout;
36    unsigned int print_direction, full_layout, codetag_count;
37
38    unsigned int glyphs;
39    cucul_canvas_t *image;
40    unsigned int *lookup;
41};
42
43static struct figfont *open_font(char const *);
44static void free_font(struct figfont *);
45
46cucul_canvas_t *render_figlet(uint32_t const *string, unsigned int length,
47                              char const *fontname)
48{
49    cucul_canvas_t *cv;
50    struct figfont *font;
51    unsigned int x, i, c;
52
53    font = open_font(fontname);
54
55    cv = cucul_create_canvas(length * font->max_length, font->height);
56
57    for(x = 0, i = 0; i < length; i++)
58    {
59        for(c = 0; c < font->glyphs; c++)
60            if(font->lookup[c * 2] == string[i])
61                break;
62
63        if(c == font->glyphs)
64            continue;
65
66        cucul_blit(cv, x, - (int)(c * font->height), font->image, NULL);
67
68        x += font->lookup[c * 2 + 1];
69    }
70
71    cucul_set_canvas_boundaries(cv, 0, 0, x, font->height);
72
73    free_font(font);
74
75    return cv;
76}
77
78static struct figfont *open_font(char const *fontname)
79{
80    char *data = NULL;
81    char path[2048];
82    struct figfont *font;
83    cucul_buffer_t *b;
84    FILE *f;
85    unsigned int i, j, size, comment_lines;
86
87    /* Open font */
88    snprintf(path, 2047, "/usr/share/figlet/%s.flf", fontname);
89    path[2047] = '\0';
90    f = fopen(path, "r");
91    if(!f)
92    {
93        fprintf(stderr, "font `%s' not found\n", path);
94        return NULL;
95    }
96
97    font = malloc(sizeof(struct figfont));
98
99    /* Read header */
100    font->print_direction = 0;
101    font->full_layout = 0;
102    font->codetag_count = 0;
103    if(fscanf(f, "flf2a%c %u %u %u %i %u %u %u %u\n", &font->hardblank,
104              &font->height, &font->baseline, &font->max_length,
105              &font->old_layout, &comment_lines, &font->print_direction,
106              &font->full_layout, &font->codetag_count) < 6)
107    {
108        fprintf(stderr, "font `%s' has invalid header\n", path);
109        free(font);
110        fclose(f);
111        return NULL;
112    }
113
114    /* Skip comment lines */
115    for(i = 0; i < comment_lines; i++)
116    {
117        fscanf(f, "%*[^\n]");
118        fscanf(f, "%*c");
119    }
120
121    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
122     * then read additional characters. */
123    font->glyphs = 0;
124    font->lookup = NULL;
125
126    for(i = 0, size = 0; !feof(f); font->glyphs++)
127    {
128        if((font->glyphs % 2048) == 0)
129            font->lookup = realloc(font->lookup,
130                                   (font->glyphs + 2048) * 2 * sizeof(int));
131
132        if(font->glyphs < 127 - 32)
133        {
134            font->lookup[font->glyphs * 2] = 32 + font->glyphs;
135        }
136        else if(font->glyphs < (127 - 32) + 7)
137        {
138            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
139            font->lookup[font->glyphs * 2] = tab[font->glyphs + (127 - 32)];
140        }
141        else
142        {
143            char number[10];
144
145            fscanf(f, "%s %*[^\n]", number);
146
147            if(number[1] == 'x')
148                sscanf(number, "%x", &font->lookup[font->glyphs * 2]);
149            else
150                sscanf(number, "%u", &font->lookup[font->glyphs * 2]);
151
152            fscanf(f, "%*c");
153        }
154
155        font->lookup[font->glyphs * 2 + 1] = 0;
156
157        for(j = 0; j < font->height; j++)
158        {
159            if(i + 2048 >= size)
160                data = realloc(data, size += 2048);
161
162            fgets(data + i, 2048, f);
163            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
164        }
165    }
166
167    fclose(f);
168
169    /* Import buffer into canvas */
170    b = cucul_load_memory(data, i);
171    font->image = cucul_import_canvas(b, "ansi");
172    cucul_free_buffer(b);
173    free(data);
174
175    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
176     * smushing, nor any kind of error checking. */
177    for(j = 0; j < font->height * font->glyphs; j++)
178    {
179        unsigned long int ch, oldch = 0;
180
181        for(i = font->max_length; i--;)
182        {
183            ch = cucul_getchar(font->image, i, j);
184
185            if(ch == font->hardblank)
186                cucul_putchar(font->image, i, j, ch = ' ');
187
188            if(oldch && ch != oldch)
189            {
190                if(!font->lookup[j / font->height * 2 + 1])
191                    font->lookup[j / font->height * 2 + 1] = i + 1;
192            }
193            else if(oldch && ch == oldch)
194                cucul_putchar(font->image, i, j, ' ');
195            else if(ch != ' ')
196            {
197                oldch = ch;
198                cucul_putchar(font->image, i, j, ' ');
199            }
200        }
201    }
202
203    return font;
204}
205
206static void free_font(struct figfont *font)
207{
208    free(font->lookup);
209    free(font);
210}
211
Note: See TracBrowser for help on using the repository browser.