~ubuntu-branches/ubuntu/raring/libcaca/raring

« back to all changes in this revision

Viewing changes to caca/codec/import.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hocevar
  • Date: 2010-02-08 01:40:59 UTC
  • mfrom: (1.1.8 upstream) (4.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100208014059-9q4av8pze8p7uw3i
Tags: 0.99.beta17-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  libcaca       Colour ASCII-Art library
3
 
 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
 
3
 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
4
4
 *                All Rights Reserved
5
5
 *
6
 
 *  $Id$
7
 
 *
8
6
 *  This library is free software. It comes without any warranty, to
9
7
 *  the extent permitted by applicable law. You can redistribute it
10
8
 *  and/or modify it under the terms of the Do What The Fuck You Want
21
19
#if !defined __KERNEL__
22
20
#   include <stdlib.h>
23
21
#   include <string.h>
 
22
#   include <stdio.h>
24
23
#endif
25
24
 
26
25
#include "caca.h"
27
26
#include "caca_internals.h"
 
27
#include "codec.h"
28
28
 
29
29
static inline uint32_t sscanu32(void const *s)
30
30
{
40
40
    return hton16(x);
41
41
}
42
42
 
43
 
struct import
44
 
{
45
 
    uint32_t clearattr;
46
 
 
47
 
    /* ANSI Graphic Rendition Combination Mode */
48
 
    uint8_t fg, bg;   /* ANSI-context fg/bg */
49
 
    uint8_t dfg, dbg; /* Default fg/bg */
50
 
    uint8_t bold, blink, italics, negative, concealed, underline;
51
 
    uint8_t faint, strike, proportional; /* unsupported */
52
 
};
53
 
 
54
43
static ssize_t import_caca(caca_canvas_t *, void const *, size_t);
55
 
static ssize_t import_text(caca_canvas_t *, void const *, size_t);
56
 
static ssize_t import_ansi(caca_canvas_t *, void const *, size_t, int);
57
 
 
58
 
static void ansi_parse_grcm(caca_canvas_t *, struct import *,
59
 
                            unsigned int, unsigned int const *);
60
44
 
61
45
/** \brief Import a memory buffer into a canvas
62
46
 *
85
69
 *  \return The number of bytes read, or 0 if there was not enough data,
86
70
 *  or -1 if an error occurred.
87
71
 */
88
 
ssize_t caca_import_memory(caca_canvas_t *cv, void const *data,
89
 
                            size_t len, char const *format)
 
72
ssize_t caca_import_canvas_from_memory(caca_canvas_t *cv, void const *data,
 
73
                                       size_t len, char const *format)
90
74
{
91
75
    if(!strcasecmp("caca", format))
92
76
        return import_caca(cv, data, len);
93
77
    if(!strcasecmp("utf8", format))
94
 
        return import_ansi(cv, data, len, 1);
 
78
        return _import_ansi(cv, data, len, 1);
95
79
    if(!strcasecmp("text", format))
96
 
        return import_text(cv, data, len);
 
80
        return _import_text(cv, data, len);
97
81
    if(!strcasecmp("ansi", format))
98
 
        return import_ansi(cv, data, len, 0);
 
82
        return _import_ansi(cv, data, len, 0);
99
83
 
100
84
    /* Autodetection */
101
85
    if(!strcasecmp("", format))
110
94
 
111
95
        /* If we find ESC[ argv, we guess it's an ANSI file */
112
96
        for(i = 0; i + 1 < len; i++)
113
 
            if((str[i] == 0x1b) && (str[i + 1] == '['))
114
 
                return import_ansi(cv, data, len, 0);
 
97
            if((str[i] == '\033') && (str[i + 1] == '['))
 
98
                return _import_ansi(cv, data, len, 0);
115
99
 
116
100
        /* Otherwise, import it as text */
117
 
        return import_text(cv, data, len);
 
101
        return _import_text(cv, data, len);
118
102
    }
119
103
 
120
104
    seterrno(EINVAL);
150
134
 *  \return The number of bytes read, or 0 if there was not enough data,
151
135
 *  or -1 if an error occurred.
152
136
 */
153
 
ssize_t caca_import_file(caca_canvas_t *cv, char const *filename,
154
 
                          char const *format)
 
137
ssize_t caca_import_canvas_from_file(caca_canvas_t *cv, char const *filename,
 
138
                                     char const *format)
155
139
{
156
140
#if defined __KERNEL__
157
141
    seterrno(ENOSYS);
159
143
#else
160
144
    caca_file_t *f;
161
145
    char *data = NULL;
162
 
    ssize_t size = 0;
163
 
    int ret;
 
146
    ssize_t ret, size = 0;
164
147
 
165
148
    f = caca_file_open(filename, "rb");
166
149
    if(!f)
176
159
            return -1;
177
160
        }
178
161
 
179
 
        ret = caca_file_read(f, data + size, 1024);
 
162
        ret = (ssize_t)caca_file_read(f, data + size, 1024);
180
163
        if(ret >= 0)
181
164
            size += ret;
182
165
    }
183
166
    caca_file_close(f);
184
167
 
185
 
    ret = caca_import_memory(cv, data, size, format);
 
168
    ret = caca_import_canvas_from_memory(cv, data, size, format);
186
169
    free(data);
187
170
 
188
171
    return ret;
189
172
#endif
190
173
}
191
174
 
 
175
/** \brief Import a memory buffer into a canvas area
 
176
 *
 
177
 *  Import a memory buffer into the given libcaca canvas's current
 
178
 *  frame, at the specified position. For more information, see
 
179
 *  caca_import_canvas_from_memory().
 
180
 *
 
181
 *  If an error occurs, -1 is returned and \b errno is set accordingly:
 
182
 *  - \c EINVAL Unsupported format requested or invalid coordinates.
 
183
 *  - \c ENOMEM Not enough memory to allocate canvas.
 
184
 *
 
185
 *  \param cv A libcaca canvas in which to import the file.
 
186
 *  \param x The leftmost coordinate of the area to import to.
 
187
 *  \param y The topmost coordinate of the area to import to.
 
188
 *  \param data A memory area containing the data to be loaded into the canvas.
 
189
 *  \param len The size in bytes of the memory area.
 
190
 *  \param format A string describing the input format.
 
191
 *  \return The number of bytes read, or 0 if there was not enough data,
 
192
 *  or -1 if an error occurred.
 
193
 */
 
194
ssize_t caca_import_area_from_memory(caca_canvas_t *cv, int x, int y,
 
195
                                     void const *data, size_t len,
 
196
                                     char const *format)
 
197
{
 
198
    caca_canvas_t *tmp;
 
199
    ssize_t ret;
 
200
 
 
201
    tmp = caca_create_canvas(0, 0);
 
202
    ret = caca_import_canvas_from_memory(tmp, data, len, format);
 
203
 
 
204
    if(ret > 0)
 
205
        caca_blit(cv, x, y, tmp, NULL);
 
206
 
 
207
    caca_free_canvas(tmp);
 
208
 
 
209
    return ret;
 
210
}
 
211
 
 
212
/** \brief Import a file into a canvas area
 
213
 *
 
214
 *  Import a file into the given libcaca canvas's current frame, at the
 
215
 *  specified position. For more information, see
 
216
 *  caca_import_canvas_from_file().
 
217
 *
 
218
 *  If an error occurs, -1 is returned and \b errno is set accordingly:
 
219
 *  - \c ENOSYS File access is not implemented on this system.
 
220
 *  - \c ENOMEM Not enough memory to allocate canvas.
 
221
 *  - \c EINVAL Unsupported format requested or invalid coordinates.
 
222
 *  caca_import_file() may also fail and set \b errno for any of the
 
223
 *  errors specified for the routine fopen().
 
224
 *
 
225
 *  \param cv A libcaca canvas in which to import the file.
 
226
 *  \param x The leftmost coordinate of the area to import to.
 
227
 *  \param y The topmost coordinate of the area to import to.
 
228
 *  \param filename The name of the file to load.
 
229
 *  \param format A string describing the input format.
 
230
 *  \return The number of bytes read, or 0 if there was not enough data,
 
231
 *  or -1 if an error occurred.
 
232
 */
 
233
ssize_t caca_import_area_from_file(caca_canvas_t *cv, int x, int y,
 
234
                                   char const *filename, char const *format)
 
235
{
 
236
    caca_canvas_t *tmp;
 
237
    ssize_t ret;
 
238
 
 
239
    tmp = caca_create_canvas(0, 0);
 
240
    ret = caca_import_canvas_from_file(tmp, filename, format);
 
241
 
 
242
    if(ret > 0)
 
243
        caca_blit(cv, x, y, tmp, NULL);
 
244
 
 
245
    caca_free_canvas(tmp);
 
246
 
 
247
    return ret;
 
248
}
 
249
 
192
250
/** \brief Get available import formats
193
251
 *
194
252
 *  Return a list of available import formats. The list is a NULL-terminated
329
387
 
330
388
    caca_set_frame(cv, 0);
331
389
 
332
 
    return 4 + control_size + data_size;
 
390
    return (ssize_t)(4 + control_size + data_size);
333
391
 
334
392
invalid_caca:
335
393
    seterrno(EINVAL);
336
394
    return -1;
337
395
}
338
396
 
339
 
static 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 size;
379
 
}
380
 
 
381
 
static 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] == '\x1b' && i + 2 >= size)
459
 
            break;
460
 
 
461
 
        /* XXX: What the fuck is this shit? */
462
 
        else if(buffer[i] == '\x1b' && 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] == '\x1b' && 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] == '\x1b' && 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
 
        /* Get the character we’re going to paste */
681
 
        else if(utf8)
682
 
        {
683
 
            size_t bytes;
684
 
 
685
 
            if(i + 6 < size)
686
 
                ch = caca_utf8_to_utf32((char const *)(buffer + i), &bytes);
687
 
            else
688
 
            {
689
 
                /* Add a trailing zero to what we're going to read */
690
 
                char tmp[7];
691
 
                memcpy(tmp, buffer + i, size - i);
692
 
                tmp[size - i] = '\0';
693
 
                ch = caca_utf8_to_utf32(tmp, &bytes);
694
 
            }
695
 
 
696
 
            if(!bytes)
697
 
            {
698
 
                /* If the Unicode is invalid, assume it was latin1. */
699
 
                ch = buffer[i];
700
 
                bytes = 1;
701
 
            }
702
 
            wch = caca_utf32_is_fullwidth(ch) ? 2 : 1;
703
 
            skip += bytes - 1;
704
 
        }
705
 
        else
706
 
        {
707
 
            ch = caca_cp437_to_utf32(buffer[i]);
708
 
            wch = 1;
709
 
        }
710
 
 
711
 
        /* Wrap long lines or grow horizontally */
712
 
        while((unsigned int)x + wch > width)
713
 
        {
714
 
            if(growx)
715
 
            {
716
 
                savedattr = caca_get_attr(cv, -1, -1);
717
 
                caca_set_attr(cv, im.clearattr);
718
 
                caca_set_canvas_size(cv, width = x + wch, height);
719
 
                caca_set_attr(cv, savedattr);
720
 
            }
721
 
            else
722
 
            {
723
 
                x -= width;
724
 
                y++;
725
 
            }
726
 
        }
727
 
 
728
 
        /* Scroll or grow vertically */
729
 
        if((unsigned int)y >= height)
730
 
        {
731
 
            savedattr = caca_get_attr(cv, -1, -1);
732
 
            caca_set_attr(cv, im.clearattr);
733
 
            if(growy)
734
 
            {
735
 
                caca_set_canvas_size(cv, width, height = y + 1);
736
 
            }
737
 
            else
738
 
            {
739
 
                int lines = (y - height) + 1;
740
 
 
741
 
                for(j = 0; j + lines < height; j++)
742
 
                {
743
 
                    memcpy(cv->attrs + j * cv->width,
744
 
                           cv->attrs + (j + lines) * cv->width, cv->width * 4);
745
 
                    memcpy(cv->chars + j * cv->width,
746
 
                           cv->chars + (j + lines) * cv->width, cv->width * 4);
747
 
                }
748
 
                caca_fill_box(cv, 0, height - lines,
749
 
                                   cv->width - 1, height - 1, ' ');
750
 
                y -= lines;
751
 
            }
752
 
            caca_set_attr(cv, savedattr);
753
 
        }
754
 
 
755
 
        /* Now paste our character, if any */
756
 
        if(wch)
757
 
        {
758
 
            caca_put_char(cv, x, y, ch);
759
 
            x += wch;
760
 
        }
761
 
    }
762
 
 
763
 
    if(growy && (unsigned int)y > height)
764
 
    {
765
 
        savedattr = caca_get_attr(cv, -1, -1);
766
 
        caca_set_attr(cv, im.clearattr);
767
 
        caca_set_canvas_size(cv, width, height = y);
768
 
        caca_set_attr(cv, savedattr);
769
 
    }
770
 
 
771
 
    cv->frames[cv->frame].x = x;
772
 
    cv->frames[cv->frame].y = y;
773
 
 
774
 
//    if(utf8)
775
 
//        caca_set_attr(cv, savedattr);
776
 
 
777
 
    return i;
778
 
}
779
 
 
780
 
/* XXX : ANSI loader helper */
781
 
 
782
 
static void ansi_parse_grcm(caca_canvas_t *cv, struct import *im,
783
 
                            unsigned int argc, unsigned int const *argv)
784
 
{
785
 
    static uint8_t const ansi2caca[] =
786
 
    {
787
 
        CACA_BLACK, CACA_RED, CACA_GREEN, CACA_BROWN,
788
 
        CACA_BLUE, CACA_MAGENTA, CACA_CYAN, CACA_LIGHTGRAY
789
 
    };
790
 
 
791
 
    unsigned int j;
792
 
    uint8_t efg, ebg; /* Effective (libcaca) fg/bg */
793
 
 
794
 
    for(j = 0; j < argc; j++)
795
 
    {
796
 
        /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
797
 
        if(argv[j] >= 30 && argv[j] <= 37)
798
 
            im->fg = ansi2caca[argv[j] - 30];
799
 
        else if(argv[j] >= 40 && argv[j] <= 47)
800
 
            im->bg = ansi2caca[argv[j] - 40];
801
 
        else if(argv[j] >= 90 && argv[j] <= 97)
802
 
            im->fg = ansi2caca[argv[j] - 90] + 8;
803
 
        else if(argv[j] >= 100 && argv[j] <= 107)
804
 
            im->bg = ansi2caca[argv[j] - 100] + 8;
805
 
        else switch(argv[j])
806
 
        {
807
 
        case 0: /* default rendition */
808
 
            im->fg = im->dfg;
809
 
            im->bg = im->dbg;
810
 
            im->bold = im->blink = im->italics = im->negative
811
 
             = im->concealed = im->underline = im->faint = im->strike
812
 
             = im->proportional = 0;
813
 
            break;
814
 
        case 1: /* bold or increased intensity */
815
 
            im->bold = 1;
816
 
            break;
817
 
        case 2: /* faint, decreased intensity or second colour */
818
 
            im->faint = 1;
819
 
            break;
820
 
        case 3: /* italicized */
821
 
            im->italics = 1;
822
 
            break;
823
 
        case 4: /* singly underlined */
824
 
            im->underline = 1;
825
 
            break;
826
 
        case 5: /* slowly blinking (less then 150 per minute) */
827
 
        case 6: /* rapidly blinking (150 per minute or more) */
828
 
            im->blink = 1;
829
 
            break;
830
 
        case 7: /* negative image */
831
 
            im->negative = 1;
832
 
            break;
833
 
        case 8: /* concealed characters */
834
 
            im->concealed = 1;
835
 
            break;
836
 
        case 9: /* crossed-out (characters still legible but marked as to be
837
 
                 * deleted */
838
 
            im->strike = 1;
839
 
            break;
840
 
        case 21: /* doubly underlined */
841
 
            im->underline = 1;
842
 
            break;
843
 
        case 22: /* normal colour or normal intensity (neither bold nor
844
 
                  * faint) */
845
 
            im->bold = im->faint = 0;
846
 
            break;
847
 
        case 23: /* not italicized, not fraktur */
848
 
            im->italics = 0;
849
 
            break;
850
 
        case 24: /* not underlined (neither singly nor doubly) */
851
 
            im->underline = 0;
852
 
            break;
853
 
        case 25: /* steady (not blinking) */
854
 
            im->blink = 0;
855
 
            break;
856
 
        case 26: /* (reserved for proportional spacing as specified in CCITT
857
 
                  * Recommendation T.61) */
858
 
            im->proportional = 1;
859
 
            break;
860
 
        case 27: /* positive image */
861
 
            im->negative = 0;
862
 
            break;
863
 
        case 28: /* revealed characters */
864
 
            im->concealed = 0;
865
 
            break;
866
 
        case 29: /* not crossed out */
867
 
            im->strike = 0;
868
 
            break;
869
 
        case 38: /* (reserved for future standardization, intended for setting
870
 
                  * character foreground colour as specified in ISO 8613-6
871
 
                  * [CCITT Recommendation T.416]) */
872
 
            break;
873
 
        case 39: /* default display colour (implementation-defined) */
874
 
            im->fg = im->dfg;
875
 
            break;
876
 
        case 48: /* (reserved for future standardization, intended for setting
877
 
                  * character background colour as specified in ISO 8613-6
878
 
                  * [CCITT Recommendation T.416]) */
879
 
            break;
880
 
        case 49: /* default background colour (implementation-defined) */
881
 
            im->bg = im->dbg;
882
 
            break;
883
 
        case 50: /* (reserved for cancelling the effect of the rendering
884
 
                  * aspect established by parameter value 26) */
885
 
            im->proportional = 0;
886
 
            break;
887
 
        default:
888
 
            debug("ansi import: unknown sgr %i", argv[j]);
889
 
            break;
890
 
        }
891
 
    }
892
 
 
893
 
    if(im->concealed)
894
 
    {
895
 
        efg = ebg = CACA_TRANSPARENT;
896
 
    }
897
 
    else
898
 
    {
899
 
        efg = im->negative ? im->bg : im->fg;
900
 
        ebg = im->negative ? im->fg : im->bg;
901
 
 
902
 
        if(im->bold)
903
 
        {
904
 
            if(efg < 8)
905
 
                efg += 8;
906
 
            else if(efg == CACA_DEFAULT)
907
 
                efg = CACA_WHITE;
908
 
        }
909
 
    }
910
 
 
911
 
    caca_set_color_ansi(cv, efg, ebg);
912
 
}
913
 
 
914
397
/*
915
398
 * XXX: The following functions are aliases.
916
399
 */
917
400
 
918
401
ssize_t cucul_import_memory(cucul_canvas_t *, void const *, size_t,
919
 
                            char const *) CACA_ALIAS(caca_import_memory);
 
402
                     char const *) CACA_ALIAS(caca_import_canvas_from_memory);
920
403
ssize_t cucul_import_file(cucul_canvas_t *, char const *,
921
 
                          char const *) CACA_ALIAS(caca_import_file);
 
404
                     char const *) CACA_ALIAS(caca_import_canvas_from_file);
 
405
ssize_t caca_import_memory(caca_canvas_t *, void const *, size_t, char const *)
 
406
                                  CACA_ALIAS(caca_import_canvas_from_memory);
 
407
ssize_t caca_import_file(caca_canvas_t *, char const *, char const *)
 
408
                                  CACA_ALIAS(caca_import_canvas_from_file);
922
409
char const * const * cucul_get_import_list(void)
923
410
         CACA_ALIAS(caca_get_import_list);
924
411