source: libcaca/trunk/caca/figfont.c @ 3159

Last change on this file since 3159 was 3130, checked in by nico, 12 years ago
  • Check that a caca_figfont was loaded before using it in caca_put_figchar()
File size: 14.8 KB
RevLine 
[2110]1/*
[2821]2 *  libcaca       Colour ASCII-Art library
[2110]3 *  Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains FIGlet and TOIlet font handling functions.
17 */
18
[2111]19/*
20 *  FIXME: this file needs huge cleanup to be usable
21 */
22
[2110]23#include "config.h"
24
25#if !defined(__KERNEL__)
26#   include <stdio.h>
27#   include <stdlib.h>
28#   include <string.h>
29#endif
30
[2821]31#include "caca.h"
32#include "caca_internals.h"
[2110]33
[2821]34struct caca_figfont
[2110]35{
[2305]36    int term_width;
[2111]37    int x, y, w, h, lines;
38
39    enum { H_DEFAULT, H_KERN, H_SMUSH, H_NONE, H_OVERLAP } hmode;
[2305]40    int hsmushrule;
[2303]41    uint32_t hardblank;
[2305]42    int height, baseline, max_length;
[2110]43    int old_layout;
[2305]44    int print_direction, full_layout, codetag_count;
45    int glyphs;
[2821]46    caca_canvas_t *fontcv, *charcv;
[2111]47    int *left, *right; /* Unused yet */
[2305]48    uint32_t *lookup;
[2110]49};
50
[2305]51static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule);
[2821]52static caca_figfont_t * open_figfont(char const *);
53static int free_figfont(caca_figfont_t *);
[2111]54
[2821]55int caca_canvas_set_figfont(caca_canvas_t *cv, char const *path)
[2110]56{
[2821]57    caca_figfont_t *ff = NULL;
[2110]58
59    if(path)
60    {
[2111]61        ff = open_figfont(path);
[2110]62        if(!ff)
63            return -1;
64    }
65
66    if(cv->ff)
[2111]67    {
[2821]68        caca_free_canvas(cv->ff->charcv);
[2111]69        free(cv->ff->left);
70        free(cv->ff->right);
71        free_figfont(cv->ff);
72    }
[2110]73
74    cv->ff = ff;
75
[2111]76    if(!path)
77        return 0;
78
79    /* from TOIlet’s main.c */
80    ff->term_width = 80;
81    ff->hmode = H_DEFAULT;
82
83    /* from TOIlet’s render.c */
84    ff->x = ff->y = 0;
85    ff->w = ff->h = 0;
86    ff->lines = 0;
[2821]87    caca_set_canvas_size(cv, 0, 0); /* XXX */
[2111]88
89    /* from TOIlet’s figlet.c */
90    if(ff->full_layout & 0x3f)
91        ff->hsmushrule = ff->full_layout & 0x3f;
92    else if(ff->old_layout > 0)
93        ff->hsmushrule = ff->old_layout;
94
95    switch(ff->hmode)
96    {
97    case H_DEFAULT:
98        if(ff->old_layout == -1)
99            ff->hmode = H_NONE;
100        else if(ff->old_layout == 0 && (ff->full_layout & 0xc0) == 0x40)
101            ff->hmode = H_KERN;
102        else if((ff->old_layout & 0x3f) && (ff->full_layout & 0x3f)
103                 && (ff->full_layout & 0x80))
104        {
105            ff->hmode = H_SMUSH;
106            ff->hsmushrule = ff->full_layout & 0x3f;
107        }
108        else if(ff->old_layout == 0 && (ff->full_layout & 0xbf) == 0x80)
109        {
110            ff->hmode = H_SMUSH;
111            ff->hsmushrule = 0x3f;
112        }
113        else
114            ff->hmode = H_OVERLAP;
115        break;
116    default:
117        break;
118    }
119
[2821]120    ff->charcv = caca_create_canvas(ff->max_length - 2, ff->height);
[2111]121
122    ff->left = malloc(ff->height * sizeof(int));
123    ff->right = malloc(ff->height * sizeof(int));
124
125    cv->ff = ff;
126
[2110]127    return 0;
128}
129
[2821]130int caca_put_figchar(caca_canvas_t *cv, uint32_t ch)
[2111]131{
[2821]132    caca_figfont_t *ff = cv->ff;
[2305]133    int c, w, h, x, y, overlap, extra, xleft, xright;
[2111]134
[3130]135    if (!ff)
136        return -1;
137
[2111]138    switch(ch)
139    {
140        case (uint32_t)'\r':
141            return 0;
142        case (uint32_t)'\n':
143            ff->x = 0;
144            ff->y += ff->height;
145            return 0;
146        /* FIXME: handle '\t' */
147    }
148
149    /* Look whether our glyph is available */
150    for(c = 0; c < ff->glyphs; c++)
151        if(ff->lookup[c * 2] == ch)
152            break;
153
154    if(c == ff->glyphs)
155        return 0;
156
157    w = ff->lookup[c * 2 + 1];
158    h = ff->height;
159
[2821]160    caca_set_canvas_handle(ff->fontcv, 0, c * ff->height);
161    caca_blit(ff->charcv, 0, 0, ff->fontcv, NULL);
[2111]162
163    /* Check whether we reached the end of the screen */
164    if(ff->x && ff->x + w > ff->term_width)
165    {
166        ff->x = 0;
167        ff->y += h;
168    }
169
170    /* Compute how much the next character will overlap */
171    switch(ff->hmode)
172    {
173    case H_SMUSH:
174    case H_KERN:
175    case H_OVERLAP:
176        extra = (ff->hmode == H_OVERLAP);
177        overlap = w;
178        for(y = 0; y < h; y++)
179        {
180            /* Compute how much spaces we can eat from the new glyph */
181            for(xright = 0; xright < overlap; xright++)
[2821]182                if(caca_get_char(ff->charcv, xright, y) != ' ')
[2111]183                    break;
184
185            /* Compute how much spaces we can eat from the previous glyph */
186            for(xleft = 0; xright + xleft < overlap && xleft < ff->x; xleft++)
[2821]187                if(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y) != ' ')
[2111]188                    break;
189
190            /* Handle overlapping */
191            if(ff->hmode == H_OVERLAP && xleft < ff->x)
192                xleft++;
193
194            /* Handle smushing */
195            if(ff->hmode == H_SMUSH)
196            {
197                if(xleft < ff->x &&
[2821]198                    hsmush(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y),
199                          caca_get_char(ff->charcv, xright, y),
[2111]200                          ff->hsmushrule))
201                    xleft++;
202            }
203
204            if(xleft + xright < overlap)
205                overlap = xleft + xright;
206        }
207        break;
208    case H_NONE:
209        overlap = 0;
210        break;
211    default:
212        return -1;
213    }
214
215    /* Check whether the current canvas is large enough */
216    if(ff->x + w - overlap > ff->w)
217        ff->w = ff->x + w - overlap < ff->term_width
218              ? ff->x + w - overlap : ff->term_width;
219
220    if(ff->y + h > ff->h)
221        ff->h = ff->y + h;
222
223#if 0 /* deactivated for libcaca insertion */
224    if(attr)
[2821]225        caca_set_attr(cv, attr);
[2111]226#endif
[2821]227    caca_set_canvas_size(cv, ff->w, ff->h);
[2111]228
[2821]229    /* Render our char (FIXME: create a rect-aware caca_blit_canvas?) */
[2111]230    for(y = 0; y < h; y++)
231        for(x = 0; x < w; x++)
232    {
233        uint32_t ch1, ch2;
[2821]234        //uint32_t tmpat = caca_get_attr(ff->fontcv, x, y + c * ff->height);
235        ch2 = caca_get_char(ff->charcv, x, y);
[2111]236        if(ch2 == ' ')
237            continue;
[2821]238        ch1 = caca_get_char(cv, ff->x + x - overlap, ff->y + y);
239        /* FIXME: this could be changed to caca_put_attr() when the
240         * function is fixed in libcaca */
241        //caca_set_attr(cv, tmpat);
[2111]242        if(ch1 == ' ' || ff->hmode != H_SMUSH)
[2821]243            caca_put_char(cv, ff->x + x - overlap, ff->y + y, ch2);
[2111]244        else
[2821]245            caca_put_char(cv, ff->x + x - overlap, ff->y + y,
[2111]246                           hsmush(ch1, ch2, ff->hsmushrule));
[2821]247        //caca_put_attr(cv, ff->x + x, ff->y + y, tmpat);
[2111]248    }
249
250    /* Advance cursor */
251    ff->x += w - overlap;
252
253    return 0;
254}
255
[2821]256int caca_flush_figlet(caca_canvas_t *cv)
[2111]257{
[2821]258    caca_figfont_t *ff = cv->ff;
[2305]259    int x, y;
[2111]260
261    //ff->torender = cv;
[2821]262    //caca_set_canvas_size(ff->torender, ff->w, ff->h);
263    caca_set_canvas_size(cv, ff->w, ff->h);
[2111]264
265    /* FIXME: do this somewhere else, or record hardblank positions */
266    for(y = 0; y < ff->h; y++)
267        for(x = 0; x < ff->w; x++)
[2821]268            if(caca_get_char(cv, x, y) == 0xa0)
[2111]269            {
[2821]270                uint32_t attr = caca_get_attr(cv, x, y);
271                caca_put_char(cv, x, y, ' ');
272                caca_put_attr(cv, x, y, attr);
[2111]273            }
274
275    ff->x = ff->y = 0;
276    ff->w = ff->h = 0;
277
[2821]278    //cv = caca_create_canvas(1, 1); /* XXX */
[2111]279
280    /* from render.c */
[2821]281    ff->lines += caca_get_canvas_height(cv);
[2111]282
283    return 0;
284}
285
[2110]286#define STD_GLYPHS (127 - 32)
287#define EXT_GLYPHS (STD_GLYPHS + 7)
288
[2821]289static caca_figfont_t * open_figfont(char const *path)
[2110]290{
[2960]291#if !defined __KERNEL__ && defined HAVE_SNPRINTF
[2110]292    char altpath[2048];
[2960]293#endif
[2110]294    char buf[2048];
295    char hardblank[10];
[2821]296    caca_figfont_t *ff;
[2110]297    char *data = NULL;
[2821]298    caca_file_t *f;
[2305]299    int i, j, size, comment_lines;
[2110]300
[2821]301    ff = malloc(sizeof(caca_figfont_t));
[2110]302    if(!ff)
303    {
304        seterrno(ENOMEM);
305        return NULL;
306    }
307
308    /* Open font: if not found, try .tlf, then .flf */
[2821]309    f = caca_file_open(path, "r");
[2144]310#if !defined __KERNEL__ && defined HAVE_SNPRINTF
[2225]311
312#if (! defined(snprintf)) && ( defined(_WIN32) || defined(WIN32) ) && (! defined(__CYGWIN__))
313#define snprintf _snprintf
314#endif
315
[2110]316    if(!f)
317    {
[2225]318        snprintf(altpath, 2047, "%s.tlf", path);
[2110]319        altpath[2047] = '\0';
[2821]320        f = caca_file_open(altpath, "r");
[2110]321    }
322    if(!f)
323    {
[2225]324        snprintf(altpath, 2047, "%s.flf", path);
[2110]325        altpath[2047] = '\0';
[2821]326        f = caca_file_open(altpath, "r");
[2110]327    }
[2144]328#endif
[2110]329    if(!f)
330    {
331        free(ff);
332        seterrno(ENOENT);
333        return NULL;
334    }
335
336    /* Read header */
337    ff->print_direction = 0;
338    ff->full_layout = 0;
339    ff->codetag_count = 0;
[2821]340    caca_file_gets(f, buf, 2048);
[2110]341    if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank,
342              &ff->height, &ff->baseline, &ff->max_length,
343              &ff->old_layout, &comment_lines, &ff->print_direction,
344              &ff->full_layout, &ff->codetag_count) < 6)
345    {
346        debug("figfont error: `%s' has invalid header: %s", path, buf);
[2821]347        caca_file_close(f);
[2110]348        free(ff);
349        seterrno(EINVAL);
350        return NULL;
351    }
352
353    if(ff->old_layout < -1 || ff->old_layout > 63 || ff->full_layout > 32767
354        || ((ff->full_layout & 0x80) && (ff->full_layout & 0x3f) == 0
355            && ff->old_layout))
356    {
357        debug("figfont error: `%s' has invalid layout %i/%u",
358                path, ff->old_layout, ff->full_layout);
[2821]359        caca_file_close(f);
[2110]360        free(ff);
361        seterrno(EINVAL);
362        return NULL;
363    }
364
[2821]365    ff->hardblank = caca_utf8_to_utf32(hardblank, NULL);
[2110]366
367    /* Skip comment lines */
368    for(i = 0; i < comment_lines; i++)
[2821]369        caca_file_gets(f, buf, 2048);
[2110]370
371    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
372     * then read additional characters. */
373    ff->glyphs = 0;
374    ff->lookup = NULL;
375
[2821]376    for(i = 0, size = 0; !caca_file_eof(f); ff->glyphs++)
[2110]377    {
378        if((ff->glyphs % 2048) == 0)
379            ff->lookup = realloc(ff->lookup,
380                                   (ff->glyphs + 2048) * 2 * sizeof(int));
381
382        if(ff->glyphs < STD_GLYPHS)
383        {
384            ff->lookup[ff->glyphs * 2] = 32 + ff->glyphs;
385        }
386        else if(ff->glyphs < EXT_GLYPHS)
387        {
388            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
389            ff->lookup[ff->glyphs * 2] = tab[ff->glyphs - STD_GLYPHS];
390        }
391        else
392        {
[2821]393            if(caca_file_gets(f, buf, 2048) == NULL)
[2110]394                break;
395
396            /* Ignore blank lines, as in jacky.flf */
397            if(buf[0] == '\n' || buf[0] == '\r')
398                continue;
399
400            /* Ignore negative indices for now, as in ivrit.flf */
401            if(buf[0] == '-')
402            {
403                for(j = 0; j < ff->height; j++)
[2821]404                    caca_file_gets(f, buf, 2048);
[2110]405                continue;
406            }
407
408            if(!buf[0] || buf[0] < '0' || buf[0] > '9')
409            {
410                debug("figfont error: glyph #%u in `%s'", ff->glyphs, path);
411                free(data);
412                free(ff->lookup);
413                free(ff);
414                seterrno(EINVAL);
415                return NULL;
416            }
417
418            if(buf[1] == 'x')
419                sscanf(buf, "%x", &ff->lookup[ff->glyphs * 2]);
420            else
421                sscanf(buf, "%u", &ff->lookup[ff->glyphs * 2]);
422        }
423
424        ff->lookup[ff->glyphs * 2 + 1] = 0;
425
426        for(j = 0; j < ff->height; j++)
427        {
428            if(i + 2048 >= size)
429                data = realloc(data, size += 2048);
430
[2821]431            caca_file_gets(f, data + i, 2048);
[2110]432            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
433        }
434    }
435
[2821]436    caca_file_close(f);
[2110]437
438    if(ff->glyphs < EXT_GLYPHS)
439    {
440        debug("figfont error: only %u glyphs in `%s', expected at least %u",
441                        ff->glyphs, path, EXT_GLYPHS);
442        free(data);
443        free(ff->lookup);
444        free(ff);
445        seterrno(EINVAL);
446        return NULL;
447    }
448
449    /* Import buffer into canvas */
[2821]450    ff->fontcv = caca_create_canvas(0, 0);
451    caca_import_memory(ff->fontcv, data, i, "utf8");
[2110]452    free(data);
453
454    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
455     * smushing, nor any kind of error checking. */
456    for(j = 0; j < ff->height * ff->glyphs; j++)
457    {
[2303]458        uint32_t ch, oldch = 0;
[2110]459
460        for(i = ff->max_length; i--;)
461        {
[2821]462            ch = caca_get_char(ff->fontcv, i, j);
[2110]463
464            /* Replace hardblanks with U+00A0 NO-BREAK SPACE */
465            if(ch == ff->hardblank)
[2821]466                caca_put_char(ff->fontcv, i, j, ch = 0xa0);
[2110]467
468            if(oldch && ch != oldch)
469            {
470                if(!ff->lookup[j / ff->height * 2 + 1])
471                    ff->lookup[j / ff->height * 2 + 1] = i + 1;
472            }
473            else if(oldch && ch == oldch)
[2821]474                caca_put_char(ff->fontcv, i, j, ' ');
[2110]475            else if(ch != ' ')
476            {
477                oldch = ch;
[2821]478                caca_put_char(ff->fontcv, i, j, ' ');
[2110]479            }
480        }
481    }
482
483    return ff;
484}
485
[2821]486int free_figfont(caca_figfont_t *ff)
[2110]487{
[2821]488    caca_free_canvas(ff->fontcv);
[2110]489    free(ff->lookup);
490    free(ff);
491
492    return 0;
493}
494
[2305]495static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule)
[2111]496{
497    /* Rule 1 */
498    if((rule & 0x01) && ch1 == ch2 && ch1 != 0xa0)
499        return ch2;
500
501    if(ch1 < 0x80 && ch2 < 0x80)
502    {
503        char const charlist[] = "|/\\[]{}()<>";
504        char *tmp1, *tmp2;
505
506        /* Rule 2 */
507        if(rule & 0x02)
508        {
509            if(ch1 == '_' && strchr(charlist, ch2))
510                return ch2;
511
512            if(ch2 == '_' && strchr(charlist, ch1))
513                return ch1;
514        }
515
516        /* Rule 3 */
517        if((rule & 0x04) &&
518           (tmp1 = strchr(charlist, ch1)) && (tmp2 = strchr(charlist, ch2)))
519        {
520            int cl1 = (tmp1 + 1 - charlist) / 2;
521            int cl2 = (tmp2 + 1 - charlist) / 2;
522
523            if(cl1 < cl2)
524                return ch2;
525            if(cl1 > cl2)
526                return ch1;
527        }
528
529        /* Rule 4 */
530        if(rule & 0x08)
531        {
532            uint16_t s = ch1 + ch2;
533            uint16_t p = ch1 * ch2;
534
535            if(p == 15375 /* '{' * '}' */
536                || p == 8463 /* '[' * ']' */
537                || (p == 1640 && s == 81)) /* '(' *|+ ')' */
538                return '|';
539        }
540
541        /* Rule 5 */
542        if(rule & 0x10)
543        {
544            switch((ch1 << 8) | ch2)
545            {
546                case 0x2f5c: return '|'; /* /\ */
547                case 0x5c2f: return 'Y'; /* \/ */
548                case 0x3e3c: return 'X'; /* >< */
549            }
550        }
551
552        /* Rule 6 */
553        if((rule & 0x20) && ch1 == ch2 && ch1 == 0xa0)
554            return 0xa0;
555    }
556
557    return 0;
558}
559
[2826]560/*
561 * XXX: The following functions are aliases.
562 */
563
564int cucul_canvas_set_figfont(cucul_canvas_t *, char const *)
565         CACA_ALIAS(caca_canvas_set_figfont);
566int cucul_put_figchar(cucul_canvas_t *, uint32_t) CACA_ALIAS(caca_put_figchar);
567int cucul_flush_figlet(cucul_canvas_t *) CACA_ALIAS(caca_flush_figlet);
568
Note: See TracBrowser for help on using the repository browser.