source: libcaca/trunk/cucul/import.c @ 958

Last change on this file since 958 was 958, checked in by Sam Hocevar, 14 years ago
  • Changed cucul_putchar's prototype so that it accepts a 32-bit unsigned int which shall be used as an UTF-32 character. We do not have any casting problems due to the signedness of chars because all characters were ASCII (ie. <= 0x7f) beforehands.
  • Property svn:keywords set to Id
File size: 14.6 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: import.c 958 2006-05-18 06:23:47Z sam $
7 *
8 *  This library 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 various import functions.
16 */
17
18#include "config.h"
19#include "common.h"
20
21#if !defined(__KERNEL__)
22#   if defined(HAVE_ERRNO_H)
23#       include <errno.h>
24#   endif
25#   include <stdio.h>
26#   include <stdlib.h>
27#   include <string.h>
28#endif
29
30#include "cucul.h"
31#include "cucul_internals.h"
32
33/* ANSI Graphic Rendition Combination Mode */
34struct ansi_grcm
35{
36    uint8_t fg, bg;
37    uint8_t bold, negative, concealed;
38};
39
40static cucul_canvas_t *import_caca(void const *, unsigned int);
41static cucul_canvas_t *import_text(void const *, unsigned int);
42static cucul_canvas_t *import_ansi(void const *, unsigned int);
43
44static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
45                            unsigned int, unsigned int const *);
46
47/** \brief Import a buffer into a canvas
48 *
49 *  This function imports a libcucul buffer as returned by cucul_load_memory()
50 *  or cucul_load_file() into an internal libcucul canvas.
51 *
52 *  Valid values for \c format are:
53 *
54 *  \li \c "": attempt to autodetect the file format.
55 *
56 *  \li \c "ansi": import ANSI files.
57 *
58 *  \li \c "caca": import native libcaca files.
59 *
60 *  If an error occurs, NULL is returned and \b errno is set accordingly:
61 *  - \c ENOMEM Not enough memory to allocate canvas.
62 *  - \c EINVAL Invalid format requested.
63 *
64 *  \param buffer A \e libcucul buffer containing the data to be loaded
65 *         into a canvas.
66 *  \param format A string describing the input format.
67 *  \return A libcucul canvas, or NULL in case of error.
68 */
69cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)
70{
71    char const *buf = (char const*)buffer->data;
72
73    if(!strcasecmp("caca", format))
74        return import_caca(buffer->data, buffer->size);
75    if(!strcasecmp("text", format))
76        return import_text(buffer->data, buffer->size);
77    if(!strcasecmp("ansi", format))
78        return import_ansi(buffer->data, buffer->size);
79
80    /* Autodetection */
81    if(!strcasecmp("", format))
82    {
83        unsigned int i;
84
85        /* If 4 first letters are CACA */
86        if(buffer->size >= 4 &&
87           buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A')
88            return import_caca(buffer->data, buffer->size);
89
90        /* If we find ESC[ argv, we guess it's an ANSI file */
91        for(i = 0; i < buffer->size - 1; i++)
92            if((buf[i] == 0x1b) && (buf[i + 1] == '['))
93                return import_ansi(buffer->data, buffer->size);
94
95        /* Otherwise, import it as text */
96        return import_text(buffer->data, buffer->size);
97    }
98
99#if defined(HAVE_ERRNO_H)
100    errno = EINVAL;
101#endif
102    return NULL;
103}
104
105/** \brief Get available import formats
106 *
107 *  Return a list of available import formats. The list is a NULL-terminated
108 *  array of strings, interleaving a string containing the internal value for
109 *  the import format, to be used with cucul_import_canvas(), and a string
110 *  containing the natural language description for that import format.
111 *
112 *  This function never fails.
113 *
114 *  \return An array of strings.
115 */
116char const * const * cucul_get_import_list(void)
117{
118    static char const * const list[] =
119        {
120            "", "autodetect",
121            "text", "plain text",
122            "caca", "native libcaca format",
123            "ansi", "ANSI coloured text",
124            NULL, NULL
125        };
126
127    return list;
128}
129
130/*
131 * XXX: the following functions are local.
132 */
133
134static cucul_canvas_t *import_caca(void const *data, unsigned int size)
135{
136    cucul_canvas_t *cv;
137    uint8_t const *buf = (uint8_t const *)data;
138    unsigned int width, height, n;
139
140    if(size < 16)
141        goto invalid_caca;
142
143    if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
144        goto invalid_caca;
145
146    if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
147        goto invalid_caca;
148
149    width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
150        | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
151    height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
152        | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
153
154    if(!width || !height)
155        goto invalid_caca;
156
157    if(size != 16 + width * height * 8)
158        goto invalid_caca;
159
160    cv = cucul_create_canvas(width, height);
161
162    if(!cv)
163    {
164#if defined(HAVE_ERRNO_H)
165        errno = ENOMEM;
166#endif
167        return NULL;
168    }
169
170    for(n = height * width; n--; )
171        {
172            cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
173                | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
174                | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
175                | (uint32_t)buf[16 + 3 + 8 * n];
176            cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
177                | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
178                | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
179                | (uint32_t)buf[16 + 7 + 8 * n];
180        }
181
182    return cv;
183
184invalid_caca:
185#if defined(HAVE_ERRNO_H)
186    errno = EINVAL;
187#endif
188    return NULL;
189}
190
191static cucul_canvas_t *import_text(void const *data, unsigned int size)
192{
193    cucul_canvas_t *cv;
194    char const *text = (char const *)data;
195    unsigned int width = 1, height = 1, x = 0, y = 0, i;
196
197    cv = cucul_create_canvas(width, height);
198    if(!cv)
199    {
200#if defined(HAVE_ERRNO_H)
201        errno = ENOMEM;
202#endif
203        return NULL;
204    }
205
206    cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
207
208    for(i = 0; i < size; i++)
209    {
210        unsigned char ch = *text++;
211
212        if(ch == '\r')
213            continue;
214
215        if(ch == '\n')
216        {
217            x = 0;
218            y++;
219            continue;
220        }
221
222        if(x >= width || y >= height)
223        {
224            if(x >= width)
225                width = x + 1;
226
227            if(y >= height)
228                height = y + 1;
229
230            cucul_set_canvas_size(cv, width, height);
231        }
232
233        cucul_putchar(cv, x, y, ch);
234        x++;
235    }
236
237    return cv;
238}
239
240static cucul_canvas_t *import_ansi(void const *data, unsigned int size)
241{
242    struct ansi_grcm grcm;
243    unsigned char const *buffer = (unsigned char const*)data;
244    cucul_canvas_t *cv;
245    unsigned int i, j, skip, dummy = 0;
246    unsigned int width = 1, height = 1;
247    int x = 0, y = 0, save_x = 0, save_y = 0;
248
249    cv = cucul_create_canvas(width, height);
250    if(!cv)
251    {
252#if defined(HAVE_ERRNO_H)
253        errno = ENOMEM;
254#endif
255        return NULL;
256    }
257
258    ansi_parse_grcm(cv, &grcm, 1, &dummy);
259
260    for(i = 0; i < size; i += skip)
261    {
262        skip = 1;
263
264        /* Wrap long lines */
265        if((unsigned int)x >= 80)
266        {
267            x = 0;
268            y++;
269        }
270
271        if(buffer[i] == '\x1a' && size - i >= 8
272           && !memcmp(buffer + i + 1, "SAUCE00", 7))
273            break; /* End before SAUCE data */
274
275        if(buffer[i] == '\r')
276            continue; /* DOS sucks */
277
278        if(buffer[i] == '\n')
279        {
280            x = 0;
281            y++;
282            continue;
283        }
284
285        /* Interpret escape commands, as per Standard ECMA-48 "Control
286         * Functions for Coded Character Sets", 5.4. Control sequences. */
287        if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
288        {
289            unsigned int argc = 0, argv[101];
290            unsigned int param, inter, final;
291
292        /* Compute offsets to parameter bytes, intermediate bytes and
293         * to the final byte. Only the final byte is mandatory, there
294         * can be zero of the others.
295         * 0  param=2             inter                 final           final+1
296         * +-----+------------------+---------------------+-----------------+
297         * | CSI | parameter bytes  | intermediate bytes  |   final byte    |
298         * |     |   0x30 - 0x3f    |    0x20 - 0x2f      |   0x40 - 0x7e   |
299         * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
300         * +-----+------------------+---------------------+-----------------+
301         */
302            param = 2;
303
304            for(inter = param; i + inter < size; inter++)
305                if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
306                    break;
307
308            for(final = inter; i + final < size; final++)
309                if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
310                    break;
311
312            if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
313                break; /* Invalid Final Byte */
314
315            skip += final;
316
317            /* Sanity checks */
318            if(param < inter && buffer[i + param] >= 0x3c)
319            {
320                fprintf(stderr, "private sequence \"^[[%.*s\"\n",
321                        final - param + 1, buffer + i + param);
322                continue; /* Private sequence, skip it entirely */
323            }
324
325            if(final - param > 100)
326                continue; /* Suspiciously long sequence, skip it */
327
328            /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
329             * format */
330            if(param < inter)
331            {
332                argv[0] = 0;
333                for(j = param; j < inter; j++)
334                {
335                    if(buffer[i + j] == ';')
336                        argv[++argc] = 0;
337                    else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
338                        argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
339                }
340                argc++;
341            }
342
343            /* Interpret final byte. The code representations are given in
344             * ECMA-48 5.4: Control sequences, and the code definitions are
345             * given in ECMA-48 8.3: Definition of control functions. */
346            switch(buffer[i + final])
347            {
348            case 'f': /* CUP - Cursor Position */
349            case 'H': /* HVP - Character And Line Position */
350                x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
351                y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
352                break;
353            case 'A': /* CUU - Cursor Up */
354                y -= argc ? argv[0] : 1;
355                if(y < 0)
356                    y = 0;
357                break;
358            case 'B': /* CUD - Cursor Down */
359                y += argc ? argv[0] : 1;
360                break;
361            case 'C': /* CUF - Cursor Right */
362                x += argc ? argv[0] : 1;
363                break;
364            case 'D': /* CUB - Cursor Left */
365                x -= argc ? argv[0] : 1;
366                if(x < 0)
367                    x = 0;
368                break;
369            case 's': /* Private (save cursor position) */
370                save_x = x;
371                save_y = y;
372                break;
373            case 'u': /* Private (reload cursor position) */
374                x = save_x;
375                y = save_y;
376                break;
377            case 'J': /* ED - Erase In Page */
378                if(argv[0] == 2)
379                    x = y = 0;
380                break;
381            case 'K': /* EL - Erase In Line */
382                if(width < 80)
383                    cucul_set_canvas_size(cv, width = 80, height);
384                for(j = x; j < 80; j++)
385                    cucul_putchar(cv, j, y, ' ');
386                x = 80;
387                break;
388            case 'm': /* SGR - Select Graphic Rendition */
389                ansi_parse_grcm(cv, &grcm, argc, argv);
390                break;
391            default:
392                fprintf(stderr, "unknown command %c\n", buffer[i + final]);
393                break;
394            }
395
396            continue;
397        }
398
399        /* We're going to paste a character. First make sure the canvas
400         * is big enough. */
401        if((unsigned int)x >= width)
402            cucul_set_canvas_size(cv, width = x + 1, height);
403
404        if((unsigned int)y >= height)
405            cucul_set_canvas_size(cv, width, height = y + 1);
406
407        /* Now paste our character */
408        cucul_putchar(cv, x, y, _cucul_cp437_to_utf32(buffer[i]));
409        x++;
410    }
411
412    return cv;
413}
414
415/* XXX : ANSI loader helper */
416
417static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
418                            unsigned int argc, unsigned int const *argv)
419{
420    static uint8_t const ansi2cucul[] =
421    {
422        CUCUL_COLOR_BLACK, CUCUL_COLOR_RED,
423        CUCUL_COLOR_GREEN, CUCUL_COLOR_BROWN,
424        CUCUL_COLOR_BLUE, CUCUL_COLOR_MAGENTA,
425        CUCUL_COLOR_CYAN, CUCUL_COLOR_LIGHTGRAY
426    };
427
428    unsigned int j;
429    uint8_t myfg, mybg;
430
431    for(j = 0; j < argc; j++)
432    {
433        /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
434        if(argv[j] >= 30 && argv[j] <= 37)
435            g->fg = ansi2cucul[argv[j] - 30];
436        else if(argv[j] >= 40 && argv[j] <= 47)
437            g->bg = ansi2cucul[argv[j] - 40];
438        else if(argv[j] >= 90 && argv[j] <= 97)
439            g->fg = ansi2cucul[argv[j] - 90] + 8;
440        else if(argv[j] >= 100 && argv[j] <= 107)
441            g->bg = ansi2cucul[argv[j] - 100] + 8;
442        else switch(argv[j])
443        {
444        case 0: /* default rendition */
445            g->fg = CUCUL_COLOR_DEFAULT;
446            g->bg = CUCUL_COLOR_DEFAULT;
447            g->bold = g->negative = g->concealed = 0;
448            break;
449        case 1: /* bold or increased intensity */
450            g->bold = 1;
451            break;
452        case 4: /* singly underlined */
453            break;
454        case 5: /* slowly blinking (less then 150 per minute) */
455            break;
456        case 7: /* negative image */
457            g->negative = 1;
458            break;
459        case 8: /* concealed characters */
460            g->concealed = 1;
461            break;
462        case 22: /* normal colour or normal intensity (neither bold nor faint) */
463            g->bold = 0;
464            break;
465        case 28: /* revealed characters */
466            g->concealed = 0;
467            break;
468        case 39: /* default display colour (implementation-defined) */
469            g->fg = CUCUL_COLOR_DEFAULT;
470            break;
471        case 49: /* default background colour (implementation-defined) */
472            g->bg = CUCUL_COLOR_DEFAULT;
473            break;
474        default:
475            fprintf(stderr, "unknown sgr %i\n", argv[j]);
476            break;
477        }
478    }
479
480    if(g->concealed)
481    {
482        myfg = mybg = CUCUL_COLOR_TRANSPARENT;
483    }
484    else
485    {
486        myfg = g->negative ? g->bg : g->fg;
487        mybg = g->negative ? g->fg : g->bg;
488
489        if(g->bold)
490        {
491            if(myfg < 8)
492                myfg += 8;
493            else if(myfg == CUCUL_COLOR_DEFAULT)
494                myfg = CUCUL_COLOR_WHITE;
495        }
496    }
497
498    cucul_set_color(cv, myfg, mybg);
499}
500
Note: See TracBrowser for help on using the repository browser.