source: libcaca/trunk/caca/codec/import.c @ 3451

Last change on this file since 3451 was 3451, checked in by Sam Hocevar, 14 years ago

Fix a few implicit cast related warnings on VC++.

  • Property svn:keywords set to Id
File size: 29.6 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: import.c 3451 2009-05-14 08:43:52Z sam $
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 various import functions.
17 */
18
19#include "config.h"
20
21#if !defined __KERNEL__
22#   include <stdlib.h>
23#   include <string.h>
24#   include <stdio.h>
25#endif
26
27#include "caca.h"
28#include "caca_internals.h"
29
30static inline uint32_t sscanu32(void const *s)
31{
32    uint32_t x;
33    memcpy(&x, s, 4);
34    return hton32(x);
35}
36
37static inline uint16_t sscanu16(void const *s)
38{
39    uint16_t x;
40    memcpy(&x, s, 2);
41    return hton16(x);
42}
43
44struct import
45{
46    uint32_t clearattr;
47
48    /* ANSI Graphic Rendition Combination Mode */
49    uint8_t fg, bg;   /* ANSI-context fg/bg */
50    uint8_t dfg, dbg; /* Default fg/bg */
51    uint8_t bold, blink, italics, negative, concealed, underline;
52    uint8_t faint, strike, proportional; /* unsupported */
53};
54
55static ssize_t import_caca(caca_canvas_t *, void const *, size_t);
56static ssize_t import_text(caca_canvas_t *, void const *, size_t);
57static ssize_t import_ansi(caca_canvas_t *, void const *, size_t, int);
58
59static void ansi_parse_grcm(caca_canvas_t *, struct import *,
60                            unsigned int, unsigned int const *);
61
62/** \brief Import a memory buffer into a canvas
63 *
64 *  Import a memory buffer into the given libcaca canvas's current
65 *  frame. The current frame is resized accordingly and its contents are
66 *  replaced with the imported data.
67 *
68 *  Valid values for \c format are:
69 *  - \c "": attempt to autodetect the file format.
70 *  - \c "caca": import native libcaca files.
71 *  - \c "text": import ASCII text files.
72 *  - \c "ansi": import ANSI files.
73 *  - \c "utf8": import UTF-8 files with ANSI colour codes.
74 *
75 *  The number of bytes read is returned. If the file format is valid, but
76 *  not enough data was available, 0 is returned.
77 *
78 *  If an error occurs, -1 is returned and \b errno is set accordingly:
79 *  - \c ENOMEM Not enough memory to allocate canvas.
80 *  - \c EINVAL Invalid format requested.
81 *
82 *  \param cv A libcaca canvas in which to import the file.
83 *  \param data A memory area containing the data to be loaded into the canvas.
84 *  \param len The size in bytes of the memory area.
85 *  \param format A string describing the input format.
86 *  \return The number of bytes read, or 0 if there was not enough data,
87 *  or -1 if an error occurred.
88 */
89ssize_t caca_import_memory(caca_canvas_t *cv, void const *data,
90                            size_t len, char const *format)
91{
92    if(!strcasecmp("caca", format))
93        return import_caca(cv, data, len);
94    if(!strcasecmp("utf8", format))
95        return import_ansi(cv, data, len, 1);
96    if(!strcasecmp("text", format))
97        return import_text(cv, data, len);
98    if(!strcasecmp("ansi", format))
99        return import_ansi(cv, data, len, 0);
100
101    /* Autodetection */
102    if(!strcasecmp("", format))
103    {
104        unsigned char const *str = data;
105        unsigned int i;
106
107        /* If 4 first bytes are 0xcaca + 'CV' */
108        if(len >= 4 && str[0] == 0xca &&
109           str[1] == 0xca && str[2] == 'C' && str[3] == 'V')
110            return import_caca(cv, data, len);
111
112        /* If we find ESC[ argv, we guess it's an ANSI file */
113        for(i = 0; i + 1 < len; i++)
114            if((str[i] == '\033') && (str[i + 1] == '['))
115                return import_ansi(cv, data, len, 0);
116
117        /* Otherwise, import it as text */
118        return import_text(cv, data, len);
119    }
120
121    seterrno(EINVAL);
122    return -1;
123}
124
125/** \brief Import a file into a canvas
126 *
127 *  Import a file into the given libcaca canvas's current frame. The
128 *  current frame is resized accordingly and its contents are replaced
129 *  with the imported data.
130 *
131 *  Valid values for \c format are:
132 *  - \c "": attempt to autodetect the file format.
133 *  - \c "caca": import native libcaca files.
134 *  - \c "text": import ASCII text files.
135 *  - \c "ansi": import ANSI files.
136 *  - \c "utf8": import UTF-8 files with ANSI colour codes.
137 *
138 *  The number of bytes read is returned. If the file format is valid, but
139 *  not enough data was available, 0 is returned.
140 *
141 *  If an error occurs, -1 is returned and \b errno is set accordingly:
142 *  - \c ENOSYS File access is not implemented on this system.
143 *  - \c ENOMEM Not enough memory to allocate canvas.
144 *  - \c EINVAL Invalid format requested.
145 *  caca_import_file() may also fail and set \b errno for any of the
146 *  errors specified for the routine fopen().
147 *
148 *  \param cv A libcaca canvas in which to import the file.
149 *  \param filename The name of the file to load.
150 *  \param format A string describing the input format.
151 *  \return The number of bytes read, or 0 if there was not enough data,
152 *  or -1 if an error occurred.
153 */
154ssize_t caca_import_file(caca_canvas_t *cv, char const *filename,
155                          char const *format)
156{
157#if defined __KERNEL__
158    seterrno(ENOSYS);
159    return -1;
160#else
161    caca_file_t *f;
162    char *data = NULL;
163    ssize_t ret, size = 0;
164
165    f = caca_file_open(filename, "rb");
166    if(!f)
167        return -1; /* fopen already set errno */
168
169    while(!caca_file_eof(f))
170    {
171        data = realloc(data, size + 1024);
172        if(!data)
173        {
174            caca_file_close(f);
175            seterrno(ENOMEM);
176            return -1;
177        }
178
179        ret = (ssize_t)caca_file_read(f, data + size, 1024);
180        if(ret >= 0)
181            size += ret;
182    }
183    caca_file_close(f);
184
185    ret = caca_import_memory(cv, data, size, format);
186    free(data);
187
188    return ret;
189#endif
190}
191
192/** \brief Get available import formats
193 *
194 *  Return a list of available import formats. The list is a NULL-terminated
195 *  array of strings, interleaving a string containing the internal value for
196 *  the import format, to be used with caca_import_canvas(), and a string
197 *  containing the natural language description for that import format.
198 *
199 *  This function never fails.
200 *
201 *  \return An array of strings.
202 */
203char const * const * caca_get_import_list(void)
204{
205    static char const * const list[] =
206    {
207        "", "autodetect",
208        "caca", "native libcaca format",
209        "text", "plain text",
210        "ansi", "ANSI coloured text",
211        "utf8", "UTF-8 files with ANSI colour codes",
212        NULL, NULL
213    };
214
215    return list;
216}
217
218/*
219 * XXX: the following functions are local.
220 */
221
222static ssize_t import_caca(caca_canvas_t *cv, void const *data, size_t size)
223{
224    uint8_t const *buf = (uint8_t const *)data;
225    size_t control_size, data_size, expected_size;
226    unsigned int frames, f, n, offset;
227    uint16_t version, flags;
228    int32_t xmin = 0, ymin = 0, xmax = 0, ymax = 0;
229
230    if(size < 20)
231        return 0;
232
233    if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
234    {
235        debug("caca import error: expected \\xca\\xcaCV header");
236        goto invalid_caca;
237    }
238
239    control_size = sscanu32(buf + 4);
240    data_size = sscanu32(buf + 8);
241    version = sscanu16(buf + 12);
242    frames = sscanu32(buf + 14);
243    flags = sscanu16(buf + 18);
244
245    if(size < 4 + control_size + data_size)
246        return 0;
247
248    if(control_size < 16 + frames * 32)
249    {
250        debug("caca import error: control size %u < expected %u",
251              (unsigned int)control_size, 16 + frames * 32);
252        goto invalid_caca;
253    }
254
255    for(expected_size = 0, f = 0; f < frames; f++)
256    {
257        unsigned int width, height, duration;
258        uint32_t attr;
259        int x, y, handlex, handley;
260
261        width = sscanu32(buf + 4 + 16 + f * 32);
262        height = sscanu32(buf + 4 + 16 + f * 32 + 4);
263        duration = sscanu32(buf + 4 + 16 + f * 32 + 8);
264        attr = sscanu32(buf + 4 + 16 + f * 32 + 12);
265        x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
266        y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
267        handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
268        handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
269        expected_size += width * height * 8;
270        if(-handlex < xmin)
271            xmin = -handlex;
272        if(-handley < ymin)
273            ymin = -handley;
274        if((((int32_t) width) - handlex) > xmax)
275            xmax = ((int32_t) width) - handlex;
276        if((((int32_t) height) - handley) > ymax)
277            ymax = ((int32_t) height) - handley;
278    }
279
280    if(expected_size != data_size)
281    {
282        debug("caca import error: data size %u < expected %u",
283              (unsigned int)data_size, (unsigned int)expected_size);
284        goto invalid_caca;
285    }
286
287    caca_set_canvas_size(cv, 0, 0);
288    caca_set_canvas_size(cv, xmax - xmin, ymax - ymin);
289
290    for (f = caca_get_frame_count(cv); f--; )
291    {
292        caca_free_frame(cv, f);
293    }
294
295    for (offset = 0, f = 0; f < frames; f ++)
296    {
297        unsigned int width, height;
298
299        width = sscanu32(buf + 4 + 16 + f * 32);
300        height = sscanu32(buf + 4 + 16 + f * 32 + 4);
301        caca_create_frame(cv, f);
302        caca_set_frame(cv, f);
303
304        cv->curattr = sscanu32(buf + 4 + 16 + f * 32 + 12);
305        cv->frames[f].x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
306        cv->frames[f].y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
307        cv->frames[f].handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
308        cv->frames[f].handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
309
310        /* FIXME: check for return value */
311
312        for(n = width * height; n--; )
313        {
314            int x = (n % width) - cv->frames[f].handlex - xmin;
315            int y = (n / width) - cv->frames[f].handley - ymin;
316
317            caca_put_char(cv, x, y, sscanu32(buf + 4 + control_size
318                                               + offset + 8 * n));
319            caca_put_attr(cv, x, y, sscanu32(buf + 4 + control_size
320                                               + offset + 8 * n + 4));
321        }
322        offset += width * height * 8;
323
324        cv->frames[f].x -= cv->frames[f].handlex;
325        cv->frames[f].y -= cv->frames[f].handley;
326        cv->frames[f].handlex = -xmin;
327        cv->frames[f].handley = -ymin;
328    }
329
330    caca_set_frame(cv, 0);
331
332    return (ssize_t)(4 + control_size + data_size);
333
334invalid_caca:
335    seterrno(EINVAL);
336    return -1;
337}
338
339static ssize_t import_text(caca_canvas_t *cv, void const *data, size_t size)
340{
341    char const *text = (char const *)data;
342    unsigned int width = 0, height = 0, x = 0, y = 0, i;
343
344    caca_set_canvas_size(cv, width, height);
345
346    for(i = 0; i < size; i++)
347    {
348        unsigned char ch = *text++;
349
350        if(ch == '\r')
351            continue;
352
353        if(ch == '\n')
354        {
355            x = 0;
356            y++;
357            continue;
358        }
359
360        if(x >= width || y >= height)
361        {
362            if(x >= width)
363                width = x + 1;
364
365            if(y >= height)
366                height = y + 1;
367
368            caca_set_canvas_size(cv, width, height);
369        }
370
371        caca_put_char(cv, x, y, ch);
372        x++;
373    }
374
375    if(y > height)
376        caca_set_canvas_size(cv, width, height = y);
377
378    return (ssize_t)size;
379}
380
381static ssize_t import_ansi(caca_canvas_t *cv, void const *data,
382                           size_t size, int utf8)
383{
384    struct import im;
385    unsigned char const *buffer = (unsigned char const*)data;
386    unsigned int i, j, skip, growx = 0, growy = 0, dummy = 0;
387    unsigned int width, height;
388    uint32_t savedattr;
389    int x = 0, y = 0, save_x = 0, save_y = 0;
390
391    if(utf8)
392    {
393        width = cv->width;
394        height = cv->height;
395        growx = !width;
396        growy = !height;
397        x = cv->frames[cv->frame].x;
398        y = cv->frames[cv->frame].y;
399    }
400    else
401    {
402        caca_set_canvas_size(cv, width = 80, height = 0);
403        growx = 0;
404        growy = 1;
405    }
406
407    if(utf8)
408    {
409        im.dfg = CACA_DEFAULT;
410        im.dbg = CACA_TRANSPARENT;
411    }
412    else
413    {
414        im.dfg = CACA_LIGHTGRAY;
415        im.dbg = CACA_BLACK;
416    }
417
418    caca_set_color_ansi(cv, im.dfg, im.dbg);
419    im.clearattr = caca_get_attr(cv, -1, -1);
420
421    ansi_parse_grcm(cv, &im, 1, &dummy);
422
423    for(i = 0; i < size; i += skip)
424    {
425        uint32_t ch = 0;
426        int wch = 0;
427
428        skip = 1;
429
430        if(!utf8 && buffer[i] == '\x1a' && i + 7 < size
431           && !memcmp(buffer + i + 1, "SAUCE00", 7))
432            break; /* End before SAUCE data */
433
434        else if(buffer[i] == '\r')
435        {
436            x = 0;
437        }
438
439        else if(buffer[i] == '\n')
440        {
441            x = 0;
442            y++;
443        }
444
445        else if(buffer[i] == '\t')
446        {
447            x = (x + 7) & ~7;
448        }
449
450        else if(buffer[i] == '\x08')
451        {
452            if(x > 0)
453                x--;
454        }
455
456        /* If there are not enough characters to parse the escape sequence,
457         * wait until the next try. We require 3. */
458        else if(buffer[i] == '\033' && i + 2 >= size)
459            break;
460
461        /* XXX: What the fuck is this shit? */
462        else if(buffer[i] == '\033' && buffer[i + 1] == '('
463                 && buffer[i + 2] == 'B')
464        {
465            skip += 2;
466        }
467
468        /* Interpret escape commands, as per Standard ECMA-48 "Control
469         * Functions for Coded Character Sets", 5.4. Control sequences. */
470        else if(buffer[i] == '\033' && buffer[i + 1] == '[')
471        {
472            unsigned int argc = 0, argv[101];
473            unsigned int param, inter, final;
474
475        /* Compute offsets to parameter bytes, intermediate bytes and
476         * to the final byte. Only the final byte is mandatory, there
477         * can be zero of the others.
478         * 0  param=2             inter                 final           final+1
479         * +-----+------------------+---------------------+-----------------+
480         * | CSI | parameter bytes  | intermediate bytes  |   final byte    |
481         * |     |   0x30 - 0x3f    |    0x20 - 0x2f      |   0x40 - 0x7e   |
482         * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
483         * +-----+------------------+---------------------+-----------------+
484         */
485            param = 2;
486
487            for(inter = param; i + inter < size; inter++)
488                if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
489                    break;
490
491            for(final = inter; i + final < size; final++)
492                if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
493                    break;
494
495            if(i + final >= size
496                || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
497                break; /* Invalid Final Byte */
498
499            skip += final;
500
501            /* Sanity checks */
502            if(param < inter && buffer[i + param] >= 0x3c)
503            {
504                /* Private sequence, only parse what we know */
505                debug("ansi import: private sequence \"^[[%.*s\"",
506                      final - param + 1, buffer + i + param);
507                continue; /* Private sequence, skip it entirely */
508            }
509
510            if(final - param > 100)
511                continue; /* Suspiciously long sequence, skip it */
512
513            /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
514             * format */
515            if(param < inter)
516            {
517                argv[0] = 0;
518                for(j = param; j < inter; j++)
519                {
520                    if(buffer[i + j] == ';')
521                        argv[++argc] = 0;
522                    else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
523                        argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
524                }
525                argc++;
526            }
527
528            /* Interpret final byte. The code representations are given in
529             * ECMA-48 5.4: Control sequences, and the code definitions are
530             * given in ECMA-48 8.3: Definition of control functions. */
531            switch(buffer[i + final])
532            {
533            case 'H': /* CUP (0x48) - Cursor Position */
534                x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
535                y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
536                break;
537            case 'A': /* CUU (0x41) - Cursor Up */
538                y -= argc ? argv[0] : 1;
539                if(y < 0)
540                    y = 0;
541                break;
542            case 'B': /* CUD (0x42) - Cursor Down */
543                y += argc ? argv[0] : 1;
544                break;
545            case 'C': /* CUF (0x43) - Cursor Right */
546                x += argc ? argv[0] : 1;
547                break;
548            case 'D': /* CUB (0x44) - Cursor Left */
549                x -= argc ? argv[0] : 1;
550                if(x < 0)
551                    x = 0;
552                break;
553            case 'G': /* CHA (0x47) - Cursor Character Absolute */
554                x = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
555                break;
556            case 'J': /* ED (0x4a) - Erase In Page */
557                savedattr = caca_get_attr(cv, -1, -1);
558                caca_set_attr(cv, im.clearattr);
559                if(!argc || argv[0] == 0)
560                {
561                    caca_draw_line(cv, x, y, width, y, ' ');
562                    caca_fill_box(cv, 0, y + 1, width - 1, height - 1, ' ');
563                }
564                else if(argv[0] == 1)
565                {
566                    caca_fill_box(cv, 0, 0, width - 1, y - 1, ' ');
567                    caca_draw_line(cv, 0, y, x, y, ' ');
568                }
569                else if(argv[0] == 2)
570                    //x = y = 0;
571                    caca_fill_box(cv, 0, 0, width - 1, height - 1, ' ');
572                caca_set_attr(cv, savedattr);
573                break;
574            case 'K': /* EL (0x4b) - Erase In Line */
575                if(!argc || argv[0] == 0)
576                    caca_draw_line(cv, x, y, width, y, ' ');
577                else if(argv[0] == 1)
578                    caca_draw_line(cv, 0, y, x, y, ' ');
579                else if(argv[0] == 2)
580                    if((unsigned int)x < width)
581                        caca_draw_line(cv, x, y, width - 1, y, ' ');
582                //x = width;
583                break;
584            case 'P': /* DCH (0x50) - Delete Character */
585                if(!argc || argv[0] == 0)
586                    argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */
587                for(j = 0; (unsigned int)(j + argv[0]) < width; j++)
588                {
589                    caca_put_char(cv, j, y,
590                                   caca_get_char(cv, j + argv[0], y));
591                    caca_put_attr(cv, j, y,
592                                   caca_get_attr(cv, j + argv[0], y));
593                }
594#if 0
595                savedattr = caca_get_attr(cv, -1, -1);
596                caca_set_attr(cv, im.clearattr);
597                for( ; (unsigned int)j < width; j++)
598                    caca_put_char(cv, j, y, ' ');
599                caca_set_attr(cv, savedattr);
600#endif
601            case 'X': /* ECH (0x58) - Erase Character */
602                if(argc && argv[0])
603                {
604                    savedattr = caca_get_attr(cv, -1, -1);
605                    caca_set_attr(cv, im.clearattr);
606                    caca_draw_line(cv, x, y, x + argv[0] - 1, y, ' ');
607                    caca_set_attr(cv, savedattr);
608                }
609            case 'd': /* VPA (0x64) - Line Position Absolute */
610                y = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
611                break;
612            case 'f': /* HVP (0x66) - Character And Line Position */
613                x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
614                y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
615                break;
616            case 'h': /* SM (0x68) - FIXME */
617                debug("ansi import: set mode %i", argc ? (int)argv[0] : -1);
618                break;
619            case 'l': /* RM (0x6c) - FIXME */
620                debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1);
621                break;
622            case 'm': /* SGR (0x6d) - Select Graphic Rendition */
623                if(argc)
624                    ansi_parse_grcm(cv, &im, argc, argv);
625                else
626                    ansi_parse_grcm(cv, &im, 1, &dummy);
627                break;
628            case 's': /* Private (save cursor position) */
629                save_x = x;
630                save_y = y;
631                break;
632            case 'u': /* Private (reload cursor position) */
633                x = save_x;
634                y = save_y;
635                break;
636            default:
637                debug("ansi import: unknown command \"^[[%.*s\"",
638                      final - param + 1, buffer + i + param);
639                break;
640            }
641        }
642
643        /* Parse OSC stuff. */
644        else if(buffer[i] == '\033' && buffer[i + 1] == ']')
645        {
646            char *string;
647            unsigned int command = 0;
648            unsigned int mode = 2, semicolon, final;
649
650            for(semicolon = mode; i + semicolon < size; semicolon++)
651            {
652                if(buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9')
653                    break;
654                command = 10 * command + (buffer[i + semicolon] - '0');
655            }
656
657            if(i + semicolon >= size || buffer[i + semicolon] != ';')
658                break; /* Invalid Mode */
659
660            for(final = semicolon + 1; i + final < size; final++)
661                if(buffer[i + final] < 0x20)
662                    break;
663
664            if(i + final >= size || buffer[i + final] != '\a')
665                break; /* Not enough data or no bell found */
666                /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */
667                /* FIXME: differenciate between not enough data (try again)
668                 *        and invalid data (print shit) */
669
670            skip += final;
671
672            string = malloc(final - (semicolon + 1) + 1);
673            memcpy(string, buffer + (semicolon + 1), final - (semicolon + 1));
674            string[final - (semicolon + 1)] = '\0';
675            debug("ansi import: got OSC command %i string '%s'", command,
676                  string);
677            free(string);
678        }
679
680        /* Form feed means a new frame */
681        else if(buffer[i] == '\f' && buffer[i + 1] == '\n')
682        {
683            int f = caca_get_frame_count(cv);
684            caca_create_frame(cv, f);
685            caca_set_frame(cv, f);
686            x = y = 0;
687            skip++;
688        }
689
690        /* Get the character we’re going to paste */
691        else if(utf8)
692        {
693            size_t bytes;
694
695            if(i + 6 < size)
696                ch = caca_utf8_to_utf32((char const *)(buffer + i), &bytes);
697            else
698            {
699                /* Add a trailing zero to what we're going to read */
700                char tmp[7];
701                memcpy(tmp, buffer + i, size - i);
702                tmp[size - i] = '\0';
703                ch = caca_utf8_to_utf32(tmp, &bytes);
704            }
705
706            if(!bytes)
707            {
708                /* If the Unicode is invalid, assume it was latin1. */
709                ch = buffer[i];
710                bytes = 1;
711            }
712            wch = caca_utf32_is_fullwidth(ch) ? 2 : 1;
713            skip += (int)(bytes - 1);
714        }
715        else
716        {
717            ch = caca_cp437_to_utf32(buffer[i]);
718            wch = 1;
719        }
720
721        /* Wrap long lines or grow horizontally */
722        while((unsigned int)x + wch > width)
723        {
724            if(growx)
725            {
726                savedattr = caca_get_attr(cv, -1, -1);
727                caca_set_attr(cv, im.clearattr);
728                caca_set_canvas_size(cv, width = x + wch, height);
729                caca_set_attr(cv, savedattr);
730            }
731            else
732            {
733                x -= width;
734                y++;
735            }
736        }
737
738        /* Scroll or grow vertically */
739        if((unsigned int)y >= height)
740        {
741            savedattr = caca_get_attr(cv, -1, -1);
742            caca_set_attr(cv, im.clearattr);
743            if(growy)
744            {
745                caca_set_canvas_size(cv, width, height = y + 1);
746            }
747            else
748            {
749                int lines = (y - height) + 1;
750
751                for(j = 0; j + lines < height; j++)
752                {
753                    memcpy(cv->attrs + j * cv->width,
754                           cv->attrs + (j + lines) * cv->width, cv->width * 4);
755                    memcpy(cv->chars + j * cv->width,
756                           cv->chars + (j + lines) * cv->width, cv->width * 4);
757                }
758                caca_fill_box(cv, 0, height - lines,
759                                   cv->width - 1, height - 1, ' ');
760                y -= lines;
761            }
762            caca_set_attr(cv, savedattr);
763        }
764
765        /* Now paste our character, if any */
766        if(wch)
767        {
768            caca_put_char(cv, x, y, ch);
769            x += wch;
770        }
771    }
772
773    if(growy && (unsigned int)y > height)
774    {
775        savedattr = caca_get_attr(cv, -1, -1);
776        caca_set_attr(cv, im.clearattr);
777        caca_set_canvas_size(cv, width, height = y);
778        caca_set_attr(cv, savedattr);
779    }
780
781    cv->frames[cv->frame].x = x;
782    cv->frames[cv->frame].y = y;
783
784//    if(utf8)
785//        caca_set_attr(cv, savedattr);
786
787    return i;
788}
789
790/* XXX : ANSI loader helper */
791
792static void ansi_parse_grcm(caca_canvas_t *cv, struct import *im,
793                            unsigned int argc, unsigned int const *argv)
794{
795    static uint8_t const ansi2caca[] =
796    {
797        CACA_BLACK, CACA_RED, CACA_GREEN, CACA_BROWN,
798        CACA_BLUE, CACA_MAGENTA, CACA_CYAN, CACA_LIGHTGRAY
799    };
800
801    unsigned int j;
802    uint8_t efg, ebg; /* Effective (libcaca) fg/bg */
803
804    for(j = 0; j < argc; j++)
805    {
806        /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
807        if(argv[j] >= 30 && argv[j] <= 37)
808            im->fg = ansi2caca[argv[j] - 30];
809        else if(argv[j] >= 40 && argv[j] <= 47)
810            im->bg = ansi2caca[argv[j] - 40];
811        else if(argv[j] >= 90 && argv[j] <= 97)
812            im->fg = ansi2caca[argv[j] - 90] + 8;
813        else if(argv[j] >= 100 && argv[j] <= 107)
814            im->bg = ansi2caca[argv[j] - 100] + 8;
815        else switch(argv[j])
816        {
817        case 0: /* default rendition */
818            im->fg = im->dfg;
819            im->bg = im->dbg;
820            im->bold = im->blink = im->italics = im->negative
821             = im->concealed = im->underline = im->faint = im->strike
822             = im->proportional = 0;
823            break;
824        case 1: /* bold or increased intensity */
825            im->bold = 1;
826            break;
827        case 2: /* faint, decreased intensity or second colour */
828            im->faint = 1;
829            break;
830        case 3: /* italicized */
831            im->italics = 1;
832            break;
833        case 4: /* singly underlined */
834            im->underline = 1;
835            break;
836        case 5: /* slowly blinking (less then 150 per minute) */
837        case 6: /* rapidly blinking (150 per minute or more) */
838            im->blink = 1;
839            break;
840        case 7: /* negative image */
841            im->negative = 1;
842            break;
843        case 8: /* concealed characters */
844            im->concealed = 1;
845            break;
846        case 9: /* crossed-out (characters still legible but marked as to be
847                 * deleted */
848            im->strike = 1;
849            break;
850        case 21: /* doubly underlined */
851            im->underline = 1;
852            break;
853        case 22: /* normal colour or normal intensity (neither bold nor
854                  * faint) */
855            im->bold = im->faint = 0;
856            break;
857        case 23: /* not italicized, not fraktur */
858            im->italics = 0;
859            break;
860        case 24: /* not underlined (neither singly nor doubly) */
861            im->underline = 0;
862            break;
863        case 25: /* steady (not blinking) */
864            im->blink = 0;
865            break;
866        case 26: /* (reserved for proportional spacing as specified in CCITT
867                  * Recommendation T.61) */
868            im->proportional = 1;
869            break;
870        case 27: /* positive image */
871            im->negative = 0;
872            break;
873        case 28: /* revealed characters */
874            im->concealed = 0;
875            break;
876        case 29: /* not crossed out */
877            im->strike = 0;
878            break;
879        case 38: /* (reserved for future standardization, intended for setting
880                  * character foreground colour as specified in ISO 8613-6
881                  * [CCITT Recommendation T.416]) */
882            break;
883        case 39: /* default display colour (implementation-defined) */
884            im->fg = im->dfg;
885            break;
886        case 48: /* (reserved for future standardization, intended for setting
887                  * character background colour as specified in ISO 8613-6
888                  * [CCITT Recommendation T.416]) */
889            break;
890        case 49: /* default background colour (implementation-defined) */
891            im->bg = im->dbg;
892            break;
893        case 50: /* (reserved for cancelling the effect of the rendering
894                  * aspect established by parameter value 26) */
895            im->proportional = 0;
896            break;
897        default:
898            debug("ansi import: unknown sgr %i", argv[j]);
899            break;
900        }
901    }
902
903    if(im->concealed)
904    {
905        efg = ebg = CACA_TRANSPARENT;
906    }
907    else
908    {
909        efg = im->negative ? im->bg : im->fg;
910        ebg = im->negative ? im->fg : im->bg;
911
912        if(im->bold)
913        {
914            if(efg < 8)
915                efg += 8;
916            else if(efg == CACA_DEFAULT)
917                efg = CACA_WHITE;
918        }
919    }
920
921    caca_set_color_ansi(cv, efg, ebg);
922}
923
924/*
925 * XXX: The following functions are aliases.
926 */
927
928ssize_t cucul_import_memory(cucul_canvas_t *, void const *, size_t,
929                            char const *) CACA_ALIAS(caca_import_memory);
930ssize_t cucul_import_file(cucul_canvas_t *, char const *,
931                          char const *) CACA_ALIAS(caca_import_file);
932char const * const * cucul_get_import_list(void)
933         CACA_ALIAS(caca_get_import_list);
934
Note: See TracBrowser for help on using the repository browser.