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

Last change on this file since 3495 was 3495, checked in by Sam Hocevar, 11 years ago

Add caca_export_area_to_memory() to export only a selected part of the
current canvas. This is useful to export dirty rectangles. Accordingly,
add caca_import_area_from_memory() and caca_import_area_from_file().

File size: 14.8 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
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
19/*
20 *  FIXME: this file needs huge cleanup to be usable
21 */
22
23#include "config.h"
24
25#if !defined(__KERNEL__)
26#   include <stdio.h>
27#   include <stdlib.h>
28#   include <string.h>
29#endif
30
31#include "caca.h"
32#include "caca_internals.h"
33
34struct caca_figfont
35{
36    int term_width;
37    int x, y, w, h, lines;
38
39    enum { H_DEFAULT, H_KERN, H_SMUSH, H_NONE, H_OVERLAP } hmode;
40    int hsmushrule;
41    uint32_t hardblank;
42    int height, baseline, max_length;
43    int old_layout;
44    int print_direction, full_layout, codetag_count;
45    int glyphs;
46    caca_canvas_t *fontcv, *charcv;
47    int *left, *right; /* Unused yet */
48    uint32_t *lookup;
49};
50
51static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule);
52static caca_figfont_t * open_figfont(char const *);
53static int free_figfont(caca_figfont_t *);
54
55int caca_canvas_set_figfont(caca_canvas_t *cv, char const *path)
56{
57    caca_figfont_t *ff = NULL;
58
59    if(path)
60    {
61        ff = open_figfont(path);
62        if(!ff)
63            return -1;
64    }
65
66    if(cv->ff)
67    {
68        caca_free_canvas(cv->ff->charcv);
69        free(cv->ff->left);
70        free(cv->ff->right);
71        free_figfont(cv->ff);
72    }
73
74    cv->ff = ff;
75
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;
87    caca_set_canvas_size(cv, 0, 0); /* XXX */
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
120    ff->charcv = caca_create_canvas(ff->max_length - 2, ff->height);
121
122    ff->left = malloc(ff->height * sizeof(int));
123    ff->right = malloc(ff->height * sizeof(int));
124
125    cv->ff = ff;
126
127    return 0;
128}
129
130int caca_put_figchar(caca_canvas_t *cv, uint32_t ch)
131{
132    caca_figfont_t *ff = cv->ff;
133    int c, w, h, x, y, overlap, extra, xleft, xright;
134
135    if (!ff)
136        return -1;
137
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
160    caca_set_canvas_handle(ff->fontcv, 0, c * ff->height);
161    caca_blit(ff->charcv, 0, 0, ff->fontcv, NULL);
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++)
182                if(caca_get_char(ff->charcv, xright, y) != ' ')
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++)
187                if(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y) != ' ')
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 &&
198                    hsmush(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y),
199                          caca_get_char(ff->charcv, xright, y),
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)
225        caca_set_attr(cv, attr);
226#endif
227    caca_set_canvas_size(cv, ff->w, ff->h);
228
229    /* Render our char (FIXME: create a rect-aware caca_blit_canvas?) */
230    for(y = 0; y < h; y++)
231        for(x = 0; x < w; x++)
232    {
233        uint32_t ch1, ch2;
234        //uint32_t tmpat = caca_get_attr(ff->fontcv, x, y + c * ff->height);
235        ch2 = caca_get_char(ff->charcv, x, y);
236        if(ch2 == ' ')
237            continue;
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);
242        if(ch1 == ' ' || ff->hmode != H_SMUSH)
243            caca_put_char(cv, ff->x + x - overlap, ff->y + y, ch2);
244        else
245            caca_put_char(cv, ff->x + x - overlap, ff->y + y,
246                           hsmush(ch1, ch2, ff->hsmushrule));
247        //caca_put_attr(cv, ff->x + x, ff->y + y, tmpat);
248    }
249
250    /* Advance cursor */
251    ff->x += w - overlap;
252
253    return 0;
254}
255
256int caca_flush_figlet(caca_canvas_t *cv)
257{
258    caca_figfont_t *ff = cv->ff;
259    int x, y;
260
261    //ff->torender = cv;
262    //caca_set_canvas_size(ff->torender, ff->w, ff->h);
263    caca_set_canvas_size(cv, ff->w, ff->h);
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++)
268            if(caca_get_char(cv, x, y) == 0xa0)
269            {
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);
273            }
274
275    ff->x = ff->y = 0;
276    ff->w = ff->h = 0;
277
278    //cv = caca_create_canvas(1, 1); /* XXX */
279
280    /* from render.c */
281    ff->lines += caca_get_canvas_height(cv);
282
283    return 0;
284}
285
286#define STD_GLYPHS (127 - 32)
287#define EXT_GLYPHS (STD_GLYPHS + 7)
288
289static caca_figfont_t * open_figfont(char const *path)
290{
291#if !defined __KERNEL__ && defined HAVE_SNPRINTF
292    char altpath[2048];
293#endif
294    char buf[2048];
295    char hardblank[10];
296    caca_figfont_t *ff;
297    char *data = NULL;
298    caca_file_t *f;
299    int i, j, size, comment_lines;
300
301    ff = malloc(sizeof(caca_figfont_t));
302    if(!ff)
303    {
304        seterrno(ENOMEM);
305        return NULL;
306    }
307
308    /* Open font: if not found, try .tlf, then .flf */
309    f = caca_file_open(path, "r");
310#if !defined __KERNEL__ && defined HAVE_SNPRINTF
311
312#if (! defined(snprintf)) && ( defined(_WIN32) || defined(WIN32) ) && (! defined(__CYGWIN__))
313#define snprintf _snprintf
314#endif
315
316    if(!f)
317    {
318        snprintf(altpath, 2047, "%s.tlf", path);
319        altpath[2047] = '\0';
320        f = caca_file_open(altpath, "r");
321    }
322    if(!f)
323    {
324        snprintf(altpath, 2047, "%s.flf", path);
325        altpath[2047] = '\0';
326        f = caca_file_open(altpath, "r");
327    }
328#endif
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;
340    caca_file_gets(f, buf, 2048);
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);
347        caca_file_close(f);
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);
359        caca_file_close(f);
360        free(ff);
361        seterrno(EINVAL);
362        return NULL;
363    }
364
365    ff->hardblank = caca_utf8_to_utf32(hardblank, NULL);
366
367    /* Skip comment lines */
368    for(i = 0; i < comment_lines; i++)
369        caca_file_gets(f, buf, 2048);
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
376    for(i = 0, size = 0; !caca_file_eof(f); ff->glyphs++)
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        {
393            unsigned int tmp;
394
395            if(caca_file_gets(f, buf, 2048) == NULL)
396                break;
397
398            /* Ignore blank lines, as in jacky.flf */
399            if(buf[0] == '\n' || buf[0] == '\r')
400                continue;
401
402            /* Ignore negative indices for now, as in ivrit.flf */
403            if(buf[0] == '-')
404            {
405                for(j = 0; j < ff->height; j++)
406                    caca_file_gets(f, buf, 2048);
407                continue;
408            }
409
410            if(!buf[0] || buf[0] < '0' || buf[0] > '9')
411            {
412                debug("figfont error: glyph #%u in `%s'", ff->glyphs, path);
413                free(data);
414                free(ff->lookup);
415                free(ff);
416                seterrno(EINVAL);
417                return NULL;
418            }
419
420            sscanf(buf, buf[1] == 'x' ? "%x" : "%u", &tmp);
421            ff->lookup[ff->glyphs * 2] = tmp;
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
431            caca_file_gets(f, data + i, 2048);
432            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
433        }
434    }
435
436    caca_file_close(f);
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 */
450    ff->fontcv = caca_create_canvas(0, 0);
451    caca_import_canvas_from_memory(ff->fontcv, data, i, "utf8");
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    {
458        uint32_t ch, oldch = 0;
459
460        for(i = ff->max_length; i--;)
461        {
462            ch = caca_get_char(ff->fontcv, i, j);
463
464            /* Replace hardblanks with U+00A0 NO-BREAK SPACE */
465            if(ch == ff->hardblank)
466                caca_put_char(ff->fontcv, i, j, ch = 0xa0);
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)
474                caca_put_char(ff->fontcv, i, j, ' ');
475            else if(ch != ' ')
476            {
477                oldch = ch;
478                caca_put_char(ff->fontcv, i, j, ' ');
479            }
480        }
481    }
482
483    return ff;
484}
485
486int free_figfont(caca_figfont_t *ff)
487{
488    caca_free_canvas(ff->fontcv);
489    free(ff->lookup);
490    free(ff);
491
492    return 0;
493}
494
495static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule)
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
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.