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 1299 2006-11-06 17:39:42Z 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 | |
---|
35 | static int feed_figlet(context_t *, uint32_t); |
---|
36 | static int flush_figlet(context_t *); |
---|
37 | static int end_figlet(context_t *); |
---|
38 | |
---|
39 | static int open_font(context_t *cx); |
---|
40 | |
---|
41 | int 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 | |
---|
53 | static 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 | |
---|
109 | static 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 | |
---|
121 | static int end_figlet(context_t *cx) |
---|
122 | { |
---|
123 | cucul_free_canvas(cx->image); |
---|
124 | free(cx->lookup); |
---|
125 | |
---|
126 | return 0; |
---|
127 | } |
---|
128 | |
---|
129 | static 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 | /* Ignore blank lines, as in jacky.flf */ |
---|
202 | if(buf[0] == '\n' || buf[0] == '\r') |
---|
203 | continue; |
---|
204 | |
---|
205 | /* Ignore negative indices for now, as in ivrit.flf */ |
---|
206 | if(buf[0] == '-') |
---|
207 | { |
---|
208 | for(j = 0; j < cx->height; j++) |
---|
209 | toigets(buf, 2048, f); |
---|
210 | continue; |
---|
211 | } |
---|
212 | |
---|
213 | if(!buf[0] || buf[0] < '0' || buf[0] > '9') |
---|
214 | { |
---|
215 | free(data); |
---|
216 | free(cx->lookup); |
---|
217 | fprintf(stderr, "read error at glyph #%u in `%s'\n", |
---|
218 | cx->glyphs, path); |
---|
219 | return -1; |
---|
220 | } |
---|
221 | |
---|
222 | if(buf[1] == 'x') |
---|
223 | sscanf(buf, "%x", &cx->lookup[cx->glyphs * 2]); |
---|
224 | else |
---|
225 | sscanf(buf, "%u", &cx->lookup[cx->glyphs * 2]); |
---|
226 | } |
---|
227 | |
---|
228 | cx->lookup[cx->glyphs * 2 + 1] = 0; |
---|
229 | |
---|
230 | for(j = 0; j < cx->height; j++) |
---|
231 | { |
---|
232 | if(i + 2048 >= size) |
---|
233 | data = realloc(data, size += 2048); |
---|
234 | |
---|
235 | toigets(data + i, 2048, f); |
---|
236 | i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data; |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | toiclose(f); |
---|
241 | |
---|
242 | if(cx->glyphs < EXT_GLYPHS) |
---|
243 | { |
---|
244 | free(data); |
---|
245 | free(cx->lookup); |
---|
246 | fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n", |
---|
247 | cx->glyphs, path, EXT_GLYPHS); |
---|
248 | return -1; |
---|
249 | } |
---|
250 | |
---|
251 | /* Import buffer into canvas */ |
---|
252 | b = cucul_load_memory(data, i); |
---|
253 | cx->image = cucul_import_canvas(b, "utf8"); |
---|
254 | cucul_free_buffer(b); |
---|
255 | free(data); |
---|
256 | |
---|
257 | if(!cx->image) |
---|
258 | { |
---|
259 | free(cx->lookup); |
---|
260 | fprintf(stderr, "libcucul could not load data in `%s'\n", path); |
---|
261 | return -1; |
---|
262 | } |
---|
263 | |
---|
264 | /* Remove EOL characters. For now we ignore hardblanks, don’t do any |
---|
265 | * smushing, nor any kind of error checking. */ |
---|
266 | for(j = 0; j < cx->height * cx->glyphs; j++) |
---|
267 | { |
---|
268 | unsigned long int ch, oldch = 0; |
---|
269 | |
---|
270 | for(i = cx->max_length; i--;) |
---|
271 | { |
---|
272 | ch = cucul_getchar(cx->image, i, j); |
---|
273 | |
---|
274 | /* TODO: Replace hardblanks with U+00A0 NO-BREAK SPACE */ |
---|
275 | if(ch == cx->hardblank) |
---|
276 | cucul_putchar(cx->image, i, j, ch = ' '); |
---|
277 | //cucul_putchar(cx->image, i, j, ch = 0xa0); |
---|
278 | |
---|
279 | if(oldch && ch != oldch) |
---|
280 | { |
---|
281 | if(!cx->lookup[j / cx->height * 2 + 1]) |
---|
282 | cx->lookup[j / cx->height * 2 + 1] = i + 1; |
---|
283 | } |
---|
284 | else if(oldch && ch == oldch) |
---|
285 | cucul_putchar(cx->image, i, j, ' '); |
---|
286 | else if(ch != ' ') |
---|
287 | { |
---|
288 | oldch = ch; |
---|
289 | cucul_putchar(cx->image, i, j, ' '); |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | return 0; |
---|
295 | } |
---|
296 | |
---|