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

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