[1101] | 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 1401 2006-11-15 03:29:34Z 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 | |
---|
[1102] | 14 | /* |
---|
| 15 | * This file contains functions for handling FIGlet fonts. |
---|
| 16 | */ |
---|
| 17 | |
---|
[1101] | 18 | #include "config.h" |
---|
| 19 | |
---|
| 20 | #if defined(HAVE_INTTYPES_H) |
---|
| 21 | # include <inttypes.h> |
---|
| 22 | #endif |
---|
[1106] | 23 | #include <stdio.h> |
---|
| 24 | #include <stdlib.h> |
---|
| 25 | #include <string.h> |
---|
[1101] | 26 | #include <cucul.h> |
---|
| 27 | |
---|
[1143] | 28 | #include "toilet.h" |
---|
[1241] | 29 | #include "render.h" |
---|
[1201] | 30 | #include "io.h" |
---|
[1101] | 31 | |
---|
[1150] | 32 | #define STD_GLYPHS (127 - 32) |
---|
| 33 | #define EXT_GLYPHS (STD_GLYPHS + 7) |
---|
| 34 | |
---|
[1385] | 35 | static int feed_figlet(context_t *, uint32_t, uint32_t); |
---|
[1241] | 36 | static int flush_figlet(context_t *); |
---|
[1196] | 37 | static int end_figlet(context_t *); |
---|
| 38 | |
---|
| 39 | static int open_font(context_t *cx); |
---|
[1401] | 40 | static uint32_t smush(uint32_t, uint32_t, unsigned int); |
---|
[1196] | 41 | |
---|
| 42 | int init_figlet(context_t *cx) |
---|
[1106] | 43 | { |
---|
[1196] | 44 | if(open_font(cx)) |
---|
| 45 | return -1; |
---|
[1106] | 46 | |
---|
[1401] | 47 | /* FIXME: read the font's contents */ |
---|
| 48 | if(cx->hlayout == H_DEFAULT) |
---|
| 49 | cx->hlayout = H_SMUSH; |
---|
| 50 | |
---|
| 51 | cx->charcv = cucul_create_canvas(cx->max_length - 2, cx->height); |
---|
| 52 | |
---|
[1400] | 53 | cx->left = malloc(cx->height * sizeof(int)); |
---|
| 54 | cx->right = malloc(cx->height * sizeof(int)); |
---|
| 55 | |
---|
[1196] | 56 | cx->feed = feed_figlet; |
---|
[1241] | 57 | cx->flush = flush_figlet; |
---|
[1196] | 58 | cx->end = end_figlet; |
---|
[1106] | 59 | |
---|
[1196] | 60 | return 0; |
---|
| 61 | } |
---|
| 62 | |
---|
[1385] | 63 | static int feed_figlet(context_t *cx, uint32_t ch, uint32_t attr) |
---|
[1101] | 64 | { |
---|
[1401] | 65 | unsigned int c, w, h, x, y, overlap, extra, xleft, xright; |
---|
[1101] | 66 | |
---|
[1196] | 67 | switch(ch) |
---|
| 68 | { |
---|
| 69 | case (uint32_t)'\r': |
---|
| 70 | return 0; |
---|
| 71 | case (uint32_t)'\n': |
---|
| 72 | cx->x = 0; |
---|
| 73 | cx->y += cx->height; |
---|
| 74 | return 0; |
---|
| 75 | /* FIXME: handle '\t' */ |
---|
| 76 | } |
---|
[1101] | 77 | |
---|
[1196] | 78 | /* Look whether our glyph is available */ |
---|
| 79 | for(c = 0; c < cx->glyphs; c++) |
---|
| 80 | if(cx->lookup[c * 2] == ch) |
---|
| 81 | break; |
---|
[1143] | 82 | |
---|
[1196] | 83 | if(c == cx->glyphs) |
---|
| 84 | return 0; |
---|
[1101] | 85 | |
---|
[1196] | 86 | w = cx->lookup[c * 2 + 1]; |
---|
| 87 | h = cx->height; |
---|
| 88 | |
---|
[1401] | 89 | cucul_set_canvas_handle(cx->fontcv, 0, c * cx->height); |
---|
| 90 | cucul_blit(cx->charcv, 0, 0, cx->fontcv, NULL); |
---|
| 91 | |
---|
[1196] | 92 | /* Check whether we reached the end of the screen */ |
---|
| 93 | if(cx->x && cx->x + w > cx->term_width) |
---|
[1106] | 94 | { |
---|
[1196] | 95 | cx->x = 0; |
---|
| 96 | cx->y += h; |
---|
| 97 | } |
---|
[1106] | 98 | |
---|
[1400] | 99 | /* Compute how much the next character will overlap */ |
---|
[1401] | 100 | switch(cx->hlayout) |
---|
[1400] | 101 | { |
---|
[1401] | 102 | case H_SMUSH: |
---|
| 103 | case H_KERN: |
---|
| 104 | case H_OVERLAP: |
---|
| 105 | extra = (cx->hlayout == H_OVERLAP); |
---|
| 106 | overlap = w; |
---|
| 107 | for(y = 0; y < h; y++) |
---|
| 108 | { |
---|
| 109 | /* Compute how much spaces we can eat from the new glyph */ |
---|
| 110 | for(xright = 0; xright < overlap; xright++) |
---|
| 111 | if(cucul_get_char(cx->charcv, xright, y) != ' ') |
---|
| 112 | break; |
---|
[1400] | 113 | |
---|
[1401] | 114 | /* Compute how much spaces we can eat from the previous glyph */ |
---|
| 115 | for(xleft = 0; xright + xleft < overlap && xleft < cx->x; xleft++) |
---|
| 116 | if(cucul_get_char(cx->cv, cx->x - 1 - xleft, cx->y + y) != ' ') |
---|
| 117 | break; |
---|
[1400] | 118 | |
---|
[1401] | 119 | /* Handle overlapping */ |
---|
| 120 | if(cx->hlayout == H_OVERLAP && xleft < cx->x) |
---|
| 121 | xleft++; |
---|
| 122 | |
---|
| 123 | /* Handle smushing */ |
---|
| 124 | if(cx->hlayout == H_SMUSH) |
---|
| 125 | { |
---|
| 126 | if(xleft < cx->x && |
---|
| 127 | smush(cucul_get_char(cx->charcv, xright, y), |
---|
| 128 | cucul_get_char(cx->cv, cx->x - 1 - xleft, cx->y + y), |
---|
| 129 | 0)) |
---|
| 130 | xleft++; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | if(xleft + xright < overlap) |
---|
| 134 | overlap = xleft + xright; |
---|
| 135 | } |
---|
| 136 | break; |
---|
| 137 | case H_NONE: |
---|
| 138 | overlap = 0; |
---|
| 139 | break; |
---|
| 140 | default: |
---|
| 141 | return -1; |
---|
[1400] | 142 | } |
---|
| 143 | |
---|
[1196] | 144 | /* Check whether the current canvas is large enough */ |
---|
[1400] | 145 | if(cx->x + w - overlap > cx->w) |
---|
| 146 | cx->w = cx->x + w - overlap < cx->term_width |
---|
| 147 | ? cx->x + w - overlap : cx->term_width; |
---|
[1106] | 148 | |
---|
[1196] | 149 | if(cx->y + h > cx->h) |
---|
| 150 | cx->h = cx->y + h; |
---|
[1106] | 151 | |
---|
[1385] | 152 | if(attr) |
---|
| 153 | cucul_set_attr(cx->cv, attr); |
---|
[1196] | 154 | cucul_set_canvas_size(cx->cv, cx->w, cx->h); |
---|
| 155 | |
---|
| 156 | /* Render our char (FIXME: create a rect-aware cucul_blit_canvas?) */ |
---|
| 157 | for(y = 0; y < h; y++) |
---|
| 158 | for(x = 0; x < w; x++) |
---|
| 159 | { |
---|
[1401] | 160 | uint32_t ch1, ch2; |
---|
| 161 | //uint32_t tmpat = cucul_get_attr(cx->fontcv, x, y + c * cx->height); |
---|
| 162 | ch2 = cucul_get_char(cx->charcv, x, y); |
---|
| 163 | if(ch2 == ' ') |
---|
[1400] | 164 | continue; |
---|
[1401] | 165 | ch1 = cucul_get_char(cx->cv, cx->x + x - overlap, cx->y + y); |
---|
[1385] | 166 | /* FIXME: this could be changed to cucul_put_attr() when the |
---|
| 167 | * function is fixed in libcucul */ |
---|
| 168 | //cucul_set_attr(cx->cv, tmpat); |
---|
[1401] | 169 | if(ch1 == ' ' || cx->hlayout != H_SMUSH) |
---|
| 170 | cucul_put_char(cx->cv, cx->x + x - overlap, cx->y + y, ch2); |
---|
| 171 | else |
---|
| 172 | cucul_put_char(cx->cv, cx->x + x - overlap, cx->y + y, |
---|
| 173 | smush(ch1, ch2, 0)); |
---|
[1385] | 174 | //cucul_put_attr(cx->cv, cx->x + x, cx->y + y, tmpat); |
---|
[1106] | 175 | } |
---|
| 176 | |
---|
[1196] | 177 | /* Advance cursor */ |
---|
[1400] | 178 | cx->x += w - overlap; |
---|
[1113] | 179 | |
---|
[1196] | 180 | return 0; |
---|
| 181 | } |
---|
[1106] | 182 | |
---|
[1241] | 183 | static int flush_figlet(context_t *cx) |
---|
| 184 | { |
---|
[1400] | 185 | unsigned int x, y; |
---|
| 186 | |
---|
[1241] | 187 | cx->torender = cx->cv; |
---|
| 188 | cucul_set_canvas_size(cx->torender, cx->w, cx->h); |
---|
| 189 | |
---|
[1400] | 190 | /* FIXME: do this somewhere else, or record hardblank positions */ |
---|
| 191 | for(y = 0; y < cx->h; y++) |
---|
| 192 | for(x = 0; x < cx->w; x++) |
---|
| 193 | if(cucul_get_char(cx->torender, x, y) == 0xa0) |
---|
| 194 | { |
---|
| 195 | uint32_t attr = cucul_get_attr(cx->torender, x, y); |
---|
| 196 | cucul_put_char(cx->torender, x, y, ' '); |
---|
| 197 | cucul_put_attr(cx->torender, x, y, attr); |
---|
| 198 | } |
---|
| 199 | |
---|
[1241] | 200 | cx->x = cx->y = 0; |
---|
| 201 | cx->w = cx->h = 0; |
---|
| 202 | cx->cv = cucul_create_canvas(1, 1); |
---|
| 203 | |
---|
| 204 | return 0; |
---|
| 205 | } |
---|
| 206 | |
---|
[1196] | 207 | static int end_figlet(context_t *cx) |
---|
| 208 | { |
---|
[1400] | 209 | free(cx->left); |
---|
| 210 | free(cx->right); |
---|
[1401] | 211 | cucul_free_canvas(cx->charcv); |
---|
| 212 | cucul_free_canvas(cx->fontcv); |
---|
[1196] | 213 | free(cx->lookup); |
---|
| 214 | |
---|
| 215 | return 0; |
---|
[1101] | 216 | } |
---|
| 217 | |
---|
[1196] | 218 | static int open_font(context_t *cx) |
---|
[1106] | 219 | { |
---|
| 220 | char *data = NULL; |
---|
| 221 | char path[2048]; |
---|
[1200] | 222 | char buf[2048]; |
---|
[1170] | 223 | char hardblank[10]; |
---|
[1201] | 224 | TOIFILE *f; |
---|
[1106] | 225 | unsigned int i, j, size, comment_lines; |
---|
| 226 | |
---|
[1143] | 227 | /* Open font: try .tlf, then .flf */ |
---|
[1193] | 228 | snprintf(path, 2047, "%s/%s.tlf", cx->dir, cx->font); |
---|
[1106] | 229 | path[2047] = '\0'; |
---|
[1201] | 230 | f = toiopen(path, "r"); |
---|
[1106] | 231 | if(!f) |
---|
| 232 | { |
---|
[1193] | 233 | snprintf(path, 2047, "%s/%s.flf", cx->dir, cx->font); |
---|
[1143] | 234 | path[2047] = '\0'; |
---|
[1201] | 235 | f = toiopen(path, "r"); |
---|
[1143] | 236 | if(!f) |
---|
| 237 | { |
---|
| 238 | fprintf(stderr, "font `%s' not found\n", path); |
---|
[1196] | 239 | return -1; |
---|
[1143] | 240 | } |
---|
[1106] | 241 | } |
---|
| 242 | |
---|
| 243 | /* Read header */ |
---|
[1196] | 244 | cx->print_direction = 0; |
---|
| 245 | cx->full_layout = 0; |
---|
| 246 | cx->codetag_count = 0; |
---|
[1201] | 247 | toigets(buf, 2048, f); |
---|
[1200] | 248 | if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank, |
---|
[1196] | 249 | &cx->height, &cx->baseline, &cx->max_length, |
---|
| 250 | &cx->old_layout, &comment_lines, &cx->print_direction, |
---|
| 251 | &cx->full_layout, &cx->codetag_count) < 6) |
---|
[1106] | 252 | { |
---|
[1204] | 253 | fprintf(stderr, "font `%s' has invalid header: %s\n", path, buf); |
---|
[1201] | 254 | toiclose(f); |
---|
[1196] | 255 | return -1; |
---|
[1106] | 256 | } |
---|
| 257 | |
---|
[1196] | 258 | cx->hardblank = cucul_utf8_to_utf32(hardblank, NULL); |
---|
[1170] | 259 | |
---|
[1106] | 260 | /* Skip comment lines */ |
---|
| 261 | for(i = 0; i < comment_lines; i++) |
---|
[1201] | 262 | toigets(buf, 2048, f); |
---|
[1106] | 263 | |
---|
| 264 | /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223) |
---|
| 265 | * then read additional characters. */ |
---|
[1196] | 266 | cx->glyphs = 0; |
---|
| 267 | cx->lookup = NULL; |
---|
[1106] | 268 | |
---|
[1202] | 269 | for(i = 0, size = 0; !toieof(f); cx->glyphs++) |
---|
[1106] | 270 | { |
---|
[1196] | 271 | if((cx->glyphs % 2048) == 0) |
---|
| 272 | cx->lookup = realloc(cx->lookup, |
---|
| 273 | (cx->glyphs + 2048) * 2 * sizeof(int)); |
---|
[1106] | 274 | |
---|
[1196] | 275 | if(cx->glyphs < STD_GLYPHS) |
---|
[1106] | 276 | { |
---|
[1196] | 277 | cx->lookup[cx->glyphs * 2] = 32 + cx->glyphs; |
---|
[1106] | 278 | } |
---|
[1196] | 279 | else if(cx->glyphs < EXT_GLYPHS) |
---|
[1106] | 280 | { |
---|
| 281 | static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 }; |
---|
[1196] | 282 | cx->lookup[cx->glyphs * 2] = tab[cx->glyphs - STD_GLYPHS]; |
---|
[1106] | 283 | } |
---|
| 284 | else |
---|
| 285 | { |
---|
[1201] | 286 | if(toigets(buf, 2048, f) == NULL) |
---|
[1150] | 287 | break; |
---|
[1113] | 288 | |
---|
[1298] | 289 | /* Ignore blank lines, as in jacky.flf */ |
---|
| 290 | if(buf[0] == '\n' || buf[0] == '\r') |
---|
| 291 | continue; |
---|
| 292 | |
---|
[1299] | 293 | /* Ignore negative indices for now, as in ivrit.flf */ |
---|
| 294 | if(buf[0] == '-') |
---|
| 295 | { |
---|
| 296 | for(j = 0; j < cx->height; j++) |
---|
| 297 | toigets(buf, 2048, f); |
---|
| 298 | continue; |
---|
| 299 | } |
---|
| 300 | |
---|
[1289] | 301 | if(!buf[0] || buf[0] < '0' || buf[0] > '9') |
---|
[1150] | 302 | { |
---|
| 303 | free(data); |
---|
[1196] | 304 | free(cx->lookup); |
---|
[1289] | 305 | fprintf(stderr, "read error at glyph #%u in `%s'\n", |
---|
[1196] | 306 | cx->glyphs, path); |
---|
| 307 | return -1; |
---|
[1150] | 308 | } |
---|
| 309 | |
---|
[1200] | 310 | if(buf[1] == 'x') |
---|
| 311 | sscanf(buf, "%x", &cx->lookup[cx->glyphs * 2]); |
---|
[1113] | 312 | else |
---|
[1200] | 313 | sscanf(buf, "%u", &cx->lookup[cx->glyphs * 2]); |
---|
[1106] | 314 | } |
---|
| 315 | |
---|
[1196] | 316 | cx->lookup[cx->glyphs * 2 + 1] = 0; |
---|
[1106] | 317 | |
---|
[1196] | 318 | for(j = 0; j < cx->height; j++) |
---|
[1106] | 319 | { |
---|
| 320 | if(i + 2048 >= size) |
---|
| 321 | data = realloc(data, size += 2048); |
---|
| 322 | |
---|
[1201] | 323 | toigets(data + i, 2048, f); |
---|
[1106] | 324 | i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data; |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | |
---|
[1201] | 328 | toiclose(f); |
---|
[1106] | 329 | |
---|
[1196] | 330 | if(cx->glyphs < EXT_GLYPHS) |
---|
[1150] | 331 | { |
---|
| 332 | free(data); |
---|
[1196] | 333 | free(cx->lookup); |
---|
[1150] | 334 | fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n", |
---|
[1196] | 335 | cx->glyphs, path, EXT_GLYPHS); |
---|
| 336 | return -1; |
---|
[1150] | 337 | } |
---|
| 338 | |
---|
[1106] | 339 | /* Import buffer into canvas */ |
---|
[1401] | 340 | cx->fontcv = cucul_create_canvas(0, 0); |
---|
| 341 | cucul_import_memory(cx->fontcv, data, i, "utf8"); |
---|
[1106] | 342 | free(data); |
---|
| 343 | |
---|
| 344 | /* Remove EOL characters. For now we ignore hardblanks, don’t do any |
---|
| 345 | * smushing, nor any kind of error checking. */ |
---|
[1196] | 346 | for(j = 0; j < cx->height * cx->glyphs; j++) |
---|
[1106] | 347 | { |
---|
| 348 | unsigned long int ch, oldch = 0; |
---|
| 349 | |
---|
[1196] | 350 | for(i = cx->max_length; i--;) |
---|
[1106] | 351 | { |
---|
[1401] | 352 | ch = cucul_get_char(cx->fontcv, i, j); |
---|
[1106] | 353 | |
---|
[1400] | 354 | /* Replace hardblanks with U+00A0 NO-BREAK SPACE */ |
---|
[1196] | 355 | if(ch == cx->hardblank) |
---|
[1401] | 356 | cucul_put_char(cx->fontcv, i, j, ch = 0xa0); |
---|
[1113] | 357 | |
---|
| 358 | if(oldch && ch != oldch) |
---|
[1106] | 359 | { |
---|
[1196] | 360 | if(!cx->lookup[j / cx->height * 2 + 1]) |
---|
| 361 | cx->lookup[j / cx->height * 2 + 1] = i + 1; |
---|
[1106] | 362 | } |
---|
| 363 | else if(oldch && ch == oldch) |
---|
[1401] | 364 | cucul_put_char(cx->fontcv, i, j, ' '); |
---|
[1106] | 365 | else if(ch != ' ') |
---|
| 366 | { |
---|
| 367 | oldch = ch; |
---|
[1401] | 368 | cucul_put_char(cx->fontcv, i, j, ' '); |
---|
[1106] | 369 | } |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | |
---|
[1196] | 373 | return 0; |
---|
[1106] | 374 | } |
---|
| 375 | |
---|
[1401] | 376 | static uint32_t smush(uint32_t ch1, uint32_t ch2, unsigned int mode) |
---|
| 377 | { |
---|
| 378 | |
---|
| 379 | /* Rule 1 */ |
---|
| 380 | if(ch1 == ch2 && ch1 != 0xa0) |
---|
| 381 | return ch2; |
---|
| 382 | |
---|
| 383 | if(ch1 < 0x80 && ch2 < 0x80) |
---|
| 384 | { |
---|
| 385 | char const rule2[] = "|/\\[]{}()<>"; |
---|
| 386 | char const rule3[] = "||/\\[]{}()<>"; |
---|
| 387 | char *tmp1, *tmp2; |
---|
| 388 | uint16_t s, p; |
---|
| 389 | |
---|
| 390 | /* Rule 2 */ |
---|
| 391 | if(ch1 == '_' && strchr(rule2, ch2)) |
---|
| 392 | return ch2; |
---|
| 393 | |
---|
| 394 | /* Rule 3 */ |
---|
| 395 | if((tmp1 = strchr(rule3, ch1)) && (tmp2 = strchr(rule3, ch2))) |
---|
| 396 | { |
---|
| 397 | int cl1 = (tmp1 - rule3) / 2; |
---|
| 398 | int cl2 = (tmp2 - rule3) / 2; |
---|
| 399 | |
---|
| 400 | if(cl1 < cl2) |
---|
| 401 | return ch2; |
---|
| 402 | if(cl1 > cl2) |
---|
| 403 | return ch1; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | /* Rule 4 */ |
---|
| 407 | s = ch1 + ch2; |
---|
| 408 | p = ch1 * ch2; |
---|
| 409 | |
---|
| 410 | if(p == 15375 /* '{' * '}' */ |
---|
| 411 | || p == 8463 /* '[' * ']' */ |
---|
| 412 | || (p == 1640 && s == 81)) /* '(' *|+ ')' */ |
---|
| 413 | return '|'; |
---|
| 414 | |
---|
| 415 | /* Rule 5 */ |
---|
| 416 | switch((ch1 << 8) | ch2) |
---|
| 417 | { |
---|
| 418 | case 0x2f5c: return '|'; /* /\ */ |
---|
| 419 | case 0x5c2f: return 'Y'; /* \/ */ |
---|
| 420 | case 0x3e3c: return 'X'; /* >< */ |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | /* Rule 6 */ |
---|
| 424 | if(ch1 == ch2 && ch1 == 0xa0) |
---|
| 425 | return 0xa0; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | return 0; |
---|
| 429 | } |
---|
| 430 | |
---|