source: libcaca/trunk/caca/codec/export.c @ 3209

Last change on this file since 3209 was 3209, checked in by bsittler, 12 years ago

use a single table cell for adjacent attribute runs provided the background color will be the same; this allows us to avoid special stylesheets for our example programs

  • Property svn:keywords set to Id
File size: 35.0 KB
RevLine 
[804]1/*
[2821]2 *  libcaca       Colour ASCII-Art library
[804]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id: export.c 3209 2008-11-02 17:38:37Z bsittler $
8 *
[1462]9 *  This library is free software. It comes without any warranty, to
[1452]10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
[804]13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16/*
17 *  This file contains various export functions
18 */
19
20#include "config.h"
21
22#if !defined(__KERNEL__)
23#   include <stdlib.h>
24#   include <stdio.h>
25#   include <string.h>
26#endif
27
[2821]28#include "caca.h"
29#include "caca_internals.h"
[804]30
[1283]31static inline int sprintu32(char *s, uint32_t x)
32{
33    s[0] = (uint8_t)(x >> 24);
34    s[1] = (uint8_t)(x >> 16) & 0xff;
35    s[2] = (uint8_t)(x >>  8) & 0xff;
36    s[3] = (uint8_t)(x      ) & 0xff;
37    return 4;
38}
39
40static inline int sprintu16(char *s, uint16_t x)
41{
42    s[0] = (uint8_t)(x >>  8) & 0xff;
43    s[1] = (uint8_t)(x      ) & 0xff;
44    return 2;
45}
46
[2821]47static void *export_caca(caca_canvas_t const *, size_t *);
48static void *export_ansi(caca_canvas_t const *, size_t *);
49static void *export_utf8(caca_canvas_t const *, size_t *, int);
50static void *export_html(caca_canvas_t const *, size_t *);
51static void *export_html3(caca_canvas_t const *, size_t *);
52static void *export_bbfr(caca_canvas_t const *, size_t *);
53static void *export_irc(caca_canvas_t const *, size_t *);
54static void *export_ps(caca_canvas_t const *, size_t *);
55static void *export_svg(caca_canvas_t const *, size_t *);
56static void *export_tga(caca_canvas_t const *, size_t *);
[804]57
58/** \brief Export a canvas into a foreign format.
59 *
[2821]60 *  This function exports a libcaca canvas into various foreign formats such
[1302]61 *  as ANSI art, HTML, IRC colours, etc. The returned pointer should be passed
62 *  to free() to release the allocated storage when it is no longer needed.
[804]63 *
[825]64 *  Valid values for \c format are:
[1104]65 *  - \c "caca": export native libcaca files.
66 *  - \c "ansi": export ANSI art (CP437 charset with ANSI colour codes).
67 *  - \c "html": export an HTML page with CSS information.
68 *  - \c "html3": export an HTML table that should be compatible with
69 *    most navigators, including textmode ones.
70 *  - \c "irc": export UTF-8 text with mIRC colour codes.
71 *  - \c "ps": export a PostScript document.
72 *  - \c "svg": export an SVG vector image.
73 *  - \c "tga": export a TGA image.
[804]74 *
[920]75 *  If an error occurs, NULL is returned and \b errno is set accordingly:
[1237]76 *  - \c EINVAL Unsupported format requested.
[920]77 *  - \c ENOMEM Not enough memory to allocate output buffer.
78 *
[2821]79 *  \param cv A libcaca canvas
[804]80 *  \param format A string describing the requested output format.
[2304]81 *  \param bytes A pointer to a size_t where the number of allocated bytes
82 *         will be written.
[1302]83 *  \return A pointer to the exported memory area, or NULL in case of error.
[804]84 */
[2821]85void *caca_export_memory(caca_canvas_t const *cv, char const *format,
[2304]86                          size_t *bytes)
[804]87{
[1302]88    if(!strcasecmp("caca", format))
89        return export_caca(cv, bytes);
[804]90
[1302]91    if(!strcasecmp("ansi", format))
92        return export_ansi(cv, bytes);
[920]93
[1302]94    if(!strcasecmp("utf8", format))
95        return export_utf8(cv, bytes, 0);
[804]96
[1302]97    if(!strcasecmp("utf8cr", format))
98        return export_utf8(cv, bytes, 1);
[804]99
[1302]100    if(!strcasecmp("html", format))
101        return export_html(cv, bytes);
102
103    if(!strcasecmp("html3", format))
104        return export_html3(cv, bytes);
105
[2102]106    if(!strcasecmp("bbfr", format))
107        return export_bbfr(cv, bytes);
108
[1302]109    if(!strcasecmp("irc", format))
110        return export_irc(cv, bytes);
111
112    if(!strcasecmp("ps", format))
113        return export_ps(cv, bytes);
114
115    if(!strcasecmp("svg", format))
116        return export_svg(cv, bytes);
117
118    if(!strcasecmp("tga", format))
119        return export_tga(cv, bytes);
120
[1362]121    seterrno(EINVAL);
[1302]122    return NULL;
[804]123}
124
125/** \brief Get available export formats
126 *
127 *  Return a list of available export formats. The list is a NULL-terminated
128 *  array of strings, interleaving a string containing the internal value for
[2821]129 *  the export format, to be used with caca_export_memory(), and a string
[804]130 *  containing the natural language description for that export format.
131 *
[920]132 *  This function never fails.
133 *
[804]134 *  \return An array of strings.
135 */
[2821]136char const * const * caca_get_export_list(void)
[804]137{
138    static char const * const list[] =
139    {
[818]140        "caca", "native libcaca format",
[804]141        "ansi", "ANSI",
[925]142        "utf8", "UTF-8 with ANSI escape codes",
[1283]143        "utf8cr", "UTF-8 with ANSI escape codes and MS-DOS \\r",
[804]144        "html", "HTML",
145        "html3", "backwards-compatible HTML",
[2102]146        "bbfr", "BBCode (French)",
[804]147        "irc", "IRC with mIRC colours",
148        "ps", "PostScript document",
149        "svg", "SVG vector image",
150        "tga", "TGA image",
151        NULL, NULL
152    };
153
154    return list;
155}
156
157/*
158 * XXX: the following functions are local.
159 */
160
[818]161/* Generate a native libcaca canvas file. */
[2821]162static void *export_caca(caca_canvas_t const *cv, size_t *bytes)
[818]163{
[1302]164    char *data, *cur;
[2305]165    int f, n;
[818]166
[1354]167    /* 52 bytes for the header:
[1283]168     *  - 4 bytes for "\xCA\xCA" + "CV"
169     *  - 16 bytes for the canvas header
[1354]170     *  - 32 bytes for the frame info
[1283]171     * 8 bytes for each character cell */
[2147]172    *bytes = 20 + (32 + 8 * cv->width * cv->height) * cv->framecount;
[1302]173    cur = data = malloc(*bytes);
[818]174
[1283]175    /* magic */
176    cur += sprintf(cur, "%s", "\xCA\xCA" "CV");
[818]177
[1283]178    /* canvas_header */
[2147]179    cur += sprintu32(cur, 16 + 32 * cv->framecount);
180    cur += sprintu32(cur, cv->width * cv->height * 8 * cv->framecount);
[1283]181    cur += sprintu16(cur, 0x0001);
[2147]182    cur += sprintu32(cur, cv->framecount);
[1283]183    cur += sprintu16(cur, 0x0000);
[818]184
[1283]185    /* frame_info */
[2147]186    for(f = 0; f < cv->framecount; f++)
187    {
188        cur += sprintu32(cur, cv->width);
189        cur += sprintu32(cur, cv->height);
190        cur += sprintu32(cur, 0);
191        cur += sprintu32(cur, cv->curattr);
192        cur += sprintu32(cur, cv->frames[f].x);
193        cur += sprintu32(cur, cv->frames[f].y);
194        cur += sprintu32(cur, cv->frames[f].handlex);
195        cur += sprintu32(cur, cv->frames[f].handley);
196    }
[1283]197
198    /* canvas_data */
[2147]199    for(f = 0; f < cv->framecount; f++)
[818]200    {
[2147]201        uint32_t *attrs = cv->frames[f].attrs;
202        uint32_t *chars = cv->frames[f].chars;
203
204        for(n = cv->height * cv->width; n--; )
205        {
206            cur += sprintu32(cur, *chars++);
207            cur += sprintu32(cur, *attrs++);
208        }
[818]209    }
[1237]210
[1302]211    return data;
[818]212}
213
[925]214/* Generate UTF-8 representation of current canvas. */
[2821]215static void *export_utf8(caca_canvas_t const *cv, size_t *bytes,
[2043]216                         int cr)
[925]217{
218    static uint8_t const palette[] =
219    {
220        0,  4,  2,  6, 1,  5,  3,  7,
221        8, 12, 10, 14, 9, 13, 11, 15
222    };
223
[1302]224    char *data, *cur;
[2305]225    int x, y;
[925]226
227    /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus
228     * 4 max bytes for a UTF-8 character).
229     * Add height*9 to that (zeroes color at the end and jump to next line) */
[1302]230    *bytes = (cv->height * 9) + (cv->width * cv->height * 23);
231    cur = data = malloc(*bytes);
[925]232
233    for(y = 0; y < cv->height; y++)
234    {
[1254]235        uint32_t *lineattr = cv->attrs + y * cv->width;
[925]236        uint32_t *linechar = cv->chars + y * cv->width;
237
[1175]238        uint8_t prevfg = 0x10;
239        uint8_t prevbg = 0x10;
[925]240
241        for(x = 0; x < cv->width; x++)
242        {
[1175]243            uint32_t attr = lineattr[x];
[925]244            uint32_t ch = linechar[x];
[1332]245            uint8_t ansifg, ansibg, fg, bg;
[925]246
[2821]247            if(ch == CACA_MAGIC_FULLWIDTH)
[1221]248                continue;
249
[2821]250            ansifg = caca_attr_to_ansi_fg(attr);
251            ansibg = caca_attr_to_ansi_bg(attr);
[1175]252
[1332]253            fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
254            bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
255
[1175]256            /* TODO: the [0 could be omitted in some cases */
[925]257            if(fg != prevfg || bg != prevbg)
258            {
[1175]259                cur += sprintf(cur, "\033[0");
[925]260
261                if(fg < 8)
[1175]262                    cur += sprintf(cur, ";3%d", fg);
263                else if(fg < 16)
264                    cur += sprintf(cur, ";1;3%d;9%d", fg - 8, fg - 8);
265
266                if(bg < 8)
267                    cur += sprintf(cur, ";4%d", bg);
268                else if(bg < 16)
269                    cur += sprintf(cur, ";5;4%d;10%d", bg - 8, bg - 8);
270
271                cur += sprintf(cur, "m");
[925]272            }
273
[2821]274            cur += caca_utf32_to_utf8(cur, ch);
[925]275
276            prevfg = fg;
277            prevbg = bg;
278        }
279
[1175]280        if(prevfg != 0x10 || prevbg != 0x10)
281            cur += sprintf(cur, "\033[0m");
282
[1283]283        cur += sprintf(cur, cr ? "\r\n" : "\n");
[925]284    }
285
286    /* Crop to really used size */
[1362]287    debug("utf8 export: alloc %lu bytes, realloc %lu",
288          (unsigned long int)*bytes, (unsigned long int)(cur - data));
[1302]289    *bytes = (uintptr_t)(cur - data);
290    data = realloc(data, *bytes);
[1237]291
[1302]292    return data;
[925]293}
294
[804]295/* Generate ANSI representation of current canvas. */
[2821]296static void *export_ansi(caca_canvas_t const *cv, size_t *bytes)
[804]297{
[805]298    static uint8_t const palette[] =
[804]299    {
300        0,  4,  2,  6, 1,  5,  3,  7,
301        8, 12, 10, 14, 9, 13, 11, 15
302    };
303
[1302]304    char *data, *cur;
[2305]305    int x, y;
[804]306
[926]307    uint8_t prevfg = -1;
308    uint8_t prevbg = -1;
309
310    /* 16 bytes assumed for max length per pixel ('\e[5;1;3x;4ym' plus
311     * 1 byte for a CP437 character).
[804]312     * Add height*9 to that (zeroes color at the end and jump to next line) */
[1302]313    *bytes = (cv->height * 9) + (cv->width * cv->height * 16);
314    cur = data = malloc(*bytes);
[804]315
[811]316    for(y = 0; y < cv->height; y++)
[804]317    {
[1254]318        uint32_t *lineattr = cv->attrs + y * cv->width;
[811]319        uint32_t *linechar = cv->chars + y * cv->width;
[804]320
[811]321        for(x = 0; x < cv->width; x++)
[804]322        {
[2821]323            uint8_t ansifg = caca_attr_to_ansi_fg(lineattr[x]);
324            uint8_t ansibg = caca_attr_to_ansi_bg(lineattr[x]);
325            uint8_t fg = ansifg < 0x10 ? palette[ansifg] : CACA_LIGHTGRAY;
326            uint8_t bg = ansibg < 0x10 ? palette[ansibg] : CACA_BLACK;
[810]327            uint32_t ch = linechar[x];
[804]328
[2821]329            if(ch == CACA_MAGIC_FULLWIDTH)
[1221]330                ch = '?';
331
[804]332            if(fg != prevfg || bg != prevbg)
333            {
334                cur += sprintf(cur, "\033[0;");
335
336                if(fg < 8)
337                    if(bg < 8)
338                        cur += sprintf(cur, "3%d;4%dm", fg, bg);
339                    else
[926]340                        cur += sprintf(cur, "5;3%d;4%dm", fg, bg - 8);
[804]341                else
342                    if(bg < 8)
[926]343                        cur += sprintf(cur, "1;3%d;4%dm", fg - 8, bg);
[804]344                    else
[926]345                        cur += sprintf(cur, "5;1;3%d;4%dm", fg - 8, bg - 8);
[804]346            }
347
[2821]348            *cur++ = caca_utf32_to_cp437(ch);
[804]349
350            prevfg = fg;
351            prevbg = bg;
352        }
353
[952]354        if(cv->width == 80)
[926]355        {
[952]356            cur += sprintf(cur, "\033[s\n\033[u");
357        }
358        else
359        {
[926]360            cur += sprintf(cur, "\033[0m\r\n");
361            prevfg = -1;
362            prevbg = -1;
363        }
[804]364    }
365
366    /* Crop to really used size */
[1362]367    debug("ansi export: alloc %lu bytes, realloc %lu",
368          (unsigned long int)*bytes, (unsigned long int)(cur - data));
[1302]369    *bytes = (uintptr_t)(cur - data);
370    data = realloc(data, *bytes);
[1237]371
[1302]372    return data;
[804]373}
374
375/* Generate HTML representation of current canvas. */
[2821]376static void *export_html(caca_canvas_t const *cv, size_t *bytes)
[804]377{
[1302]378    char *data, *cur;
[2305]379    int x, y, len;
[804]380
[808]381    /* The HTML header: less than 1000 bytes
[804]382     * A line: 7 chars for "<br />\n"
[808]383     * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
[1264]384     *          83 chars for ";font-weight..."
[804]385     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
386     *          7 chars for "</span>" */
[1302]387    *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
388    cur = data = malloc(*bytes);
[804]389
390    /* HTML header */
[808]391    cur += sprintf(cur, "<html><head>\n");
[2074]392    cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n",
[2821]393                        caca_get_version());
[808]394    cur += sprintf(cur, "</head><body>\n");
[804]395
396    cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
397                        "font-family: monospace, fixed; font-weight: bold;");
398
[811]399    for(y = 0; y < cv->height; y++)
[804]400    {
[1254]401        uint32_t *lineattr = cv->attrs + y * cv->width;
[811]402        uint32_t *linechar = cv->chars + y * cv->width;
[804]403
[811]404        for(x = 0; x < cv->width; x += len)
[804]405        {
[1417]406            cur += sprintf(cur, "<span style=\"");
[2821]407            if(caca_attr_to_ansi_fg(lineattr[x]) < 0x10)
[1417]408                cur += sprintf(cur, ";color:#%.03x",
[2821]409                               caca_attr_to_rgb12_fg(lineattr[x]));
410            if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
[1417]411                cur += sprintf(cur, ";background-color:#%.03x",
[2821]412                               caca_attr_to_rgb12_bg(lineattr[x]));
413            if(lineattr[x] & CACA_BOLD)
[1264]414                cur += sprintf(cur, ";font-weight:bold");
[2821]415            if(lineattr[x] & CACA_ITALICS)
[1264]416                cur += sprintf(cur, ";font-style:italic");
[2821]417            if(lineattr[x] & CACA_UNDERLINE)
[1264]418                cur += sprintf(cur, ";text-decoration:underline");
[2821]419            if(lineattr[x] & CACA_BLINK)
[1264]420                cur += sprintf(cur, ";text-decoration:blink");
421            cur += sprintf(cur, "\">");
[804]422
423            for(len = 0;
[811]424                x + len < cv->width && lineattr[x + len] == lineattr[x];
[804]425                len++)
426            {
[2821]427                if(linechar[x + len] == CACA_MAGIC_FULLWIDTH)
[1221]428                    ;
429                else if(linechar[x + len] <= 0x00000020)
[804]430                    cur += sprintf(cur, "&nbsp;");
431                else if(linechar[x + len] < 0x00000080)
[2300]432                    cur += sprintf(cur, "%c", (uint8_t)linechar[x + len]);
[804]433                else
[981]434                    cur += sprintf(cur, "&#%i;",
435                                   (unsigned int)linechar[x + len]);
[804]436            }
437            cur += sprintf(cur, "</span>");
438        }
439        /* New line */
440        cur += sprintf(cur, "<br />\n");
441    }
442
443    cur += sprintf(cur, "</div></body></html>\n");
444
445    /* Crop to really used size */
[1362]446    debug("html export: alloc %lu bytes, realloc %lu",
447          (unsigned long int)*bytes, (unsigned long int)(cur - data));
[1302]448    *bytes = (uintptr_t)(cur - data);
449    data = realloc(data, *bytes);
[1237]450
[1302]451    return data;
[804]452}
453
454/* Export an HTML3 document. This function is way bigger than export_html(),
455 * but permits viewing in old browsers (or limited ones such as links). It
456 * will not work under gecko (mozilla rendering engine) unless you set a
457 * correct header. */
[2821]458static void *export_html3(caca_canvas_t const *cv, size_t *bytes)
[804]459{
[1302]460    char *data, *cur;
[2305]461    int x, y, len;
[3179]462    int maxcols;
[804]463
464    /* Table */
[3179]465    maxcols = 0;
466    for(y = 0; y < cv->height; y++)
467    {
468        uint32_t *linechar = cv->chars + y * cv->width;
469        int cols = 0;
470
471        for(x = 0; x < cv->width; x++)
472        {
473            if(linechar[x] == 0x00000009)
474                while((cols + 1) % 8)
475                    cols ++;
476            cols ++;
477        }
478        if (cols > maxcols)
479            maxcols = cols;
480    }
481   
[3184]482    /* The HTML table markup: less than 1000 bytes
483     * A line: 10 chars for "<tr></tr>\n"
[3209]484     * A glyph: up to 48 chars for "<td bgcolor=\"#xxxxxx\"><tt><font color=\"#xxxxxx\">"
485     *          up to 36 chars for "<b><i><u><blink></blink></u></i></b>"
[3184]486     *          up to 9 chars for "&#xxxxxx;" (far less for pure ASCII)
[3209]487     *          17 chars for "</font></tt></td>" */
488    *bytes = 1000 + cv->height * (10 + maxcols * (48 + 36 + 9 + 17));
[3184]489    cur = data = malloc(*bytes);
[804]490
[3184]491    cur += sprintf(cur, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
492
[811]493    for(y = 0; y < cv->height; y++)
[804]494    {
[1254]495        uint32_t *lineattr = cv->attrs + y * cv->width;
[811]496        uint32_t *linechar = cv->chars + y * cv->width;
[3179]497        int taboff = 0;
[804]498
499        cur += sprintf(cur, "<tr>");
500
[811]501        for(x = 0; x < cv->width; x += len)
[804]502        {
[3209]503            int i, needfont = 0;
[3179]504            int thistab = 0;
[804]505
[808]506            /* Use colspan option to factor cells with same attributes
[804]507             * (see below) */
508            len = 1;
[3209]509            while((x + len < cv->width)
510                  &&
511                  (caca_attr_to_ansi_bg(lineattr[x + len])
512                   ==
513                   caca_attr_to_ansi_bg(lineattr[x]))
514                  &&
515                  ((caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
516                   ?
517                   (_caca_attr_to_rgb24bg(lineattr[x + len])
518                    &&
519                    _caca_attr_to_rgb24bg(lineattr[x]))
520                   :
521                   1))
[804]522                len++;
523
[3179]524            for(i = 0; i < len; i++)
525                if(linechar[x + i] == 0x00000009)
526                    while((x + i + taboff + thistab + 1) % 8)
527                        thistab ++;
528
[1417]529            cur += sprintf(cur, "<td");
[804]530
[3182]531            if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
[3184]532                cur += sprintf(cur, " bgcolor=\"#%.06lx\"", (unsigned long int)
[2821]533                               _caca_attr_to_rgb24bg(lineattr[x]));
[1417]534
[3179]535            if((len + thistab) > 1)
[3184]536                cur += sprintf(cur, " colspan=\"%d\"", len + thistab);
[804]537
[1417]538            cur += sprintf(cur, ">");
[804]539
[3184]540            cur += sprintf(cur, "<tt>");
[1264]541
[804]542            for(i = 0; i < len; i++)
543            {
[3209]544                if((! i) || (lineattr[x + i] != lineattr[x + i - 1]))
545                {
546                    needfont = caca_attr_to_ansi_fg(lineattr[x + i]) < 0x10;
547
548                    if(needfont)
549                        cur += sprintf(cur, "<font color=\"#%.06lx\">", (unsigned long int)
550                                       _caca_attr_to_rgb24fg(lineattr[x + i]));
551
552                    if(lineattr[x + i] & CACA_BOLD)
553                        cur += sprintf(cur, "<b>");
554                    if(lineattr[x + i] & CACA_ITALICS)
555                        cur += sprintf(cur, "<i>");
556                    if(lineattr[x + i] & CACA_UNDERLINE)
557                        cur += sprintf(cur, "<u>");
558                    if(lineattr[x + i] & CACA_BLINK)
559                        cur += sprintf(cur, "<blink>");
560                }
561
[2821]562                if(linechar[x + i] == CACA_MAGIC_FULLWIDTH)
[1221]563                    ;
564                else if(linechar[x + i] <= 0x00000020)
[3179]565                {
[3209]566                    /* Control characters and space converted to
567                     * U+00A0 NO-BREAK SPACE, a.k.a. "&nbsp;" in HTML,
568                     * but we use the equivalent numeric character
569                     * reference &#160; so this will work in plain
570                     * XHTML with no DTD too. We also expand tabs
571                     * here, since they are not honored in HTML. */
[3179]572                    if(linechar[x + i] == 0x00000009)
573                    {
574                        while((x + i + taboff + 1) % 8)
575                        {
[3209]576                            cur += sprintf(cur, "&#160;");
[3179]577                            taboff ++;
578                        }
579                    }
[3209]580                    cur += sprintf(cur, "&#160;");
[3179]581                }
[3182]582                else if(linechar[x + i] == '&')
583                    cur += sprintf(cur, "&amp;");
584                else if(linechar[x + i] == '<')
585                    cur += sprintf(cur, "&lt;");
586                else if(linechar[x + i] == '>')
587                    cur += sprintf(cur, "&gt;");
588                else if(linechar[x + i] == '\"')
589                    cur += sprintf(cur, "&quot;");
590                else if(linechar[x + i] == '\'')
591                    cur += sprintf(cur, "&#39;");
[804]592                else if(linechar[x + i] < 0x00000080)
[2300]593                    cur += sprintf(cur, "%c", (uint8_t)linechar[x + i]);
[804]594                else
[981]595                    cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
[3209]596
597                if (((i + 1) == len) || (lineattr[x + i + 1] != lineattr[x + i]))
598                {
599                    if(lineattr[x + i] & CACA_BLINK)
600                        cur += sprintf(cur, "</blink>");
601                    if(lineattr[x + i] & CACA_UNDERLINE)
602                        cur += sprintf(cur, "</u>");
603                    if(lineattr[x + i] & CACA_ITALICS)
604                        cur += sprintf(cur, "</i>");
605                    if(lineattr[x + i] & CACA_BOLD)
606                        cur += sprintf(cur, "</b>");
607
608                    if(needfont)
609                        cur += sprintf(cur, "</font>");
610                }
[804]611            }
612
[3184]613            cur += sprintf(cur, "</tt>");
[1417]614            cur += sprintf(cur, "</td>");
[804]615        }
616        cur += sprintf(cur, "</tr>\n");
617    }
618
619    /* Footer */
620    cur += sprintf(cur, "</table>\n");
621
622    /* Crop to really used size */
[1362]623    debug("html3 export: alloc %lu bytes, realloc %lu",
624          (unsigned long int)*bytes, (unsigned long int)(cur - data));
[1302]625    *bytes = (uintptr_t)(cur - data);
626    data = realloc(data, *bytes);
[1237]627
[1302]628    return data;
[804]629}
630
[2821]631static void *export_bbfr(caca_canvas_t const *cv, size_t *bytes)
[2102]632{
633    char *data, *cur;
[2305]634    int x, y, len;
[2102]635
636    /* The font markup: less than 100 bytes
637     * A line: 1 char for "\n"
638     * A glyph: 22 chars for "[f=#xxxxxx][c=#xxxxxx]"
639     *          up to 21 chars for "[g][i][s][/s][/i][/g]"
640     *          up to 6 chars for the UTF-8 glyph
641     *          8 chars for "[/c][/f]" */
642    *bytes = 100 + cv->height * (1 + cv->width * (22 + 21 + 6 + 8));
643    cur = data = malloc(*bytes);
644
645    /* Table */
646    cur += sprintf(cur, "[font=Courier New]");
647
648    for(y = 0; y < cv->height; y++)
649    {
650        uint32_t *lineattr = cv->attrs + y * cv->width;
651        uint32_t *linechar = cv->chars + y * cv->width;
652
653        for(x = 0; x < cv->width; x += len)
654        {
[2305]655            int i, needback, needfront;
[2102]656
657            /* Use colspan option to factor cells with same attributes
658             * (see below) */
659            len = 1;
660            if(linechar[x] == ' ')
661                while(x + len < cv->width && lineattr[x + len] == lineattr[x]
662                        && linechar[x] == ' ')
663                    len++;
664            else
665                while(x + len < cv->width && lineattr[x + len] == lineattr[x]
666                        && linechar[x] != ' ')
667                    len++;
668
[2821]669            needback = caca_attr_to_ansi_bg(lineattr[x]) < 0x10;
670            needfront = caca_attr_to_ansi_fg(lineattr[x]) < 0x10;
[2102]671
672            if(needback)
673                cur += sprintf(cur, "[f=#%.06lx]", (unsigned long int)
[2821]674                               _caca_attr_to_rgb24bg(lineattr[x]));
[2102]675
676            if(linechar[x] == ' ')
677                cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
[2821]678                               _caca_attr_to_rgb24bg(lineattr[x]));
[2102]679            else if(needfront)
680                cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
[2821]681                               _caca_attr_to_rgb24fg(lineattr[x]));
[2102]682
[2821]683            if(lineattr[x] & CACA_BOLD)
[2102]684                cur += sprintf(cur, "[g]");
[2821]685            if(lineattr[x] & CACA_ITALICS)
[2102]686                cur += sprintf(cur, "[i]");
[2821]687            if(lineattr[x] & CACA_UNDERLINE)
[2102]688                cur += sprintf(cur, "[s]");
[2821]689            if(lineattr[x] & CACA_BLINK)
[2102]690                ; /* FIXME */
691
692            for(i = 0; i < len; i++)
693            {
[2821]694                if(linechar[x + i] == CACA_MAGIC_FULLWIDTH)
[2102]695                    ;
696                else if(linechar[x + i] == ' ')
697                    *cur++ = '_';
698                else
[2821]699                    cur += caca_utf32_to_utf8(cur, linechar[x + i]);
[2102]700            }
701
[2821]702            if(lineattr[x] & CACA_BLINK)
[2102]703                ; /* FIXME */
[2821]704            if(lineattr[x] & CACA_UNDERLINE)
[2102]705                cur += sprintf(cur, "[/s]");
[2821]706            if(lineattr[x] & CACA_ITALICS)
[2102]707                cur += sprintf(cur, "[/i]");
[2821]708            if(lineattr[x] & CACA_BOLD)
[2102]709                cur += sprintf(cur, "[/g]");
710
711            if(linechar[x] == ' ' || needfront)
712                cur += sprintf(cur, "[/c]");
713            if(needback)
714                cur += sprintf(cur, "[/f]");
715        }
716        cur += sprintf(cur, "\n");
717    }
718
719    /* Footer */
720    cur += sprintf(cur, "[/font]\n");
721
722    /* Crop to really used size */
723    debug("bbfr export: alloc %lu bytes, realloc %lu",
724          (unsigned long int)*bytes, (unsigned long int)(cur - data));
725    *bytes = (uintptr_t)(cur - data);
726    data = realloc(data, *bytes);
727
728    return data;
729}
730
[804]731/* Export a text file with IRC colours */
[2821]732static void *export_irc(caca_canvas_t const *cv, size_t *bytes)
[804]733{
[805]734    static uint8_t const palette[] =
[804]735    {
736        1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
737        14, 12, 9, 11, 4, 13, 8, 0, /* Light */
738    };
739
[1302]740    char *data, *cur;
[2305]741    int x, y;
[804]742
[1239]743    /* 14 bytes assumed for max length per pixel. Worst case scenario:
[804]744     * ^Cxx,yy   6 bytes
745     * ^B^B      2 bytes
[1078]746     * ch        6 bytes
[1239]747     * 3 bytes for max length per line. Worst case scenario:
748     * <spc>     1 byte (for empty lines)
[804]749     * \r\n      2 bytes
750     * In real life, the average bytes per pixel value will be around 5.
751     */
752
[1302]753    *bytes = 2 + cv->height * (3 + cv->width * 14);
754    cur = data = malloc(*bytes);
[804]755
[811]756    for(y = 0; y < cv->height; y++)
[804]757    {
[1254]758        uint32_t *lineattr = cv->attrs + y * cv->width;
[811]759        uint32_t *linechar = cv->chars + y * cv->width;
[804]760
[1089]761        uint8_t prevfg = 0x10;
762        uint8_t prevbg = 0x10;
[804]763
[811]764        for(x = 0; x < cv->width; x++)
[804]765        {
[1089]766            uint32_t attr = lineattr[x];
[810]767            uint32_t ch = linechar[x];
[1332]768            uint8_t ansifg, ansibg, fg, bg;
[804]769
[2821]770            if(ch == CACA_MAGIC_FULLWIDTH)
[1221]771                continue;
772
[2821]773            ansifg = caca_attr_to_ansi_fg(attr);
774            ansibg = caca_attr_to_ansi_bg(attr);
[1089]775
[1332]776            fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
777            bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
[804]778
[1191]779            /* TODO: optimise series of same fg / same bg
780             *       don't change fg value if ch == ' '
781             *       make sure the \x03,%d trick works everywhere */
[840]782            if(bg != prevbg || fg != prevfg)
783            {
[1191]784                int need_escape = 0;
785
[1089]786                if(bg == 0x10)
787                {
788                    if(fg == 0x10)
789                        cur += sprintf(cur, "\x0f");
790                    else
[1191]791                    {
792                        if(prevbg == 0x10)
793                            cur += sprintf(cur, "\x03%d", fg);
794                        else
795                            cur += sprintf(cur, "\x0f\x03%d", fg);
796
797                        if(ch == (uint32_t)',')
798                            need_escape = 1;
799                    }
[1089]800                }
801                else
802                {
803                    if(fg == 0x10)
804                        cur += sprintf(cur, "\x0f\x03,%d", bg);
805                    else
806                        cur += sprintf(cur, "\x03%d,%d", fg, bg);
807                }
808
[840]809                if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
[1191]810                    need_escape = 1;
811
812                if(need_escape)
[840]813                    cur += sprintf(cur, "\x02\x02");
814            }
[1191]815
[2821]816            cur += caca_utf32_to_utf8(cur, ch);
[804]817            prevfg = fg;
818            prevbg = bg;
819        }
[1239]820
821        /* TODO: do the same the day we optimise whole lines above */
822        if(!cv->width)
823            *cur++ = ' ';
824
[804]825        *cur++ = '\r';
826        *cur++ = '\n';
827    }
828
829    /* Crop to really used size */
[1362]830    debug("IRC export: alloc %lu bytes, realloc %lu",
831          (unsigned long int)*bytes, (unsigned long int)(cur - data));
[1302]832    *bytes = (uintptr_t)(cur - data);
833    data = realloc(data, *bytes);
[1237]834
[1302]835    return data;
[804]836}
837
838/* Export a PostScript document. */
[2821]839static void *export_ps(caca_canvas_t const *cv, size_t *bytes)
[804]840{
841    static char const *ps_header =
842        "%!\n"
843        "%% libcaca PDF export\n"
844        "%%LanguageLevel: 2\n"
845        "%%Pages: 1\n"
846        "%%DocumentData: Clean7Bit\n"
847        "/csquare {\n"
848        "  newpath\n"
849        "  0 0 moveto\n"
850        "  0 1 rlineto\n"
851        "  1 0 rlineto\n"
852        "  0 -1 rlineto\n"
853        "  closepath\n"
854        "  setrgbcolor\n"
855        "  fill\n"
856        "} def\n"
857        "/S {\n"
858        "  Show\n"
859        "} bind def\n"
860        "/Courier-Bold findfont\n"
861        "8 scalefont\n"
862        "setfont\n"
863        "gsave\n"
864        "6 10 scale\n";
865
[1302]866    char *data, *cur;
[2305]867    int x, y;
[804]868
869    /* 200 is arbitrary but should be ok */
[1302]870    *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
871    cur = data = malloc(*bytes);
[804]872
873    /* Header */
874    cur += sprintf(cur, "%s", ps_header);
[951]875    cur += sprintf(cur, "0 %d translate\n", cv->height);
[804]876
877    /* Background, drawn using csquare macro defined in header */
[811]878    for(y = cv->height; y--; )
[804]879    {
[1254]880        uint32_t *lineattr = cv->attrs + y * cv->width;
[804]881
[811]882        for(x = 0; x < cv->width; x++)
[804]883        {
884            uint8_t argb[8];
[2821]885            caca_attr_to_argb64(*lineattr++, argb);
[804]886            cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
887                           (float)argb[1] * (1.0 / 0xf),
888                           (float)argb[2] * (1.0 / 0xf),
889                           (float)argb[3] * (1.0 / 0xf));
890        }
891
892        /* Return to beginning of the line, and jump to the next one */
[811]893        cur += sprintf(cur, "-%d 1 translate\n", cv->width);
[804]894    }
895
896    cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
[951]897    cur += sprintf(cur, "0 %d translate\n", cv->height*10);
[804]898
[811]899    for(y = cv->height; y--; )
[804]900    {
[1254]901        uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
[811]902        uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
[804]903
[811]904        for(x = 0; x < cv->width; x++)
[804]905        {
906            uint8_t argb[8];
[810]907            uint32_t ch = *linechar++;
[804]908
[2821]909            caca_attr_to_argb64(*lineattr++, argb);
[804]910
911            cur += sprintf(cur, "newpath\n");
912            cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
913            cur += sprintf(cur, "%f %f %f setrgbcolor\n",
914                           (float)argb[5] * (1.0 / 0xf),
915                           (float)argb[6] * (1.0 / 0xf),
916                           (float)argb[7] * (1.0 / 0xf));
917
[810]918            if(ch < 0x00000020)
[804]919                cur += sprintf(cur, "(?) show\n");
[810]920            else if(ch >= 0x00000080)
[804]921                cur += sprintf(cur, "(?) show\n");
[810]922            else switch((uint8_t)(ch & 0x7f))
[804]923            {
924                case '\\':
925                case '(':
926                case ')':
[2300]927                    cur += sprintf(cur, "(\\%c) show\n", (uint8_t)ch);
[804]928                    break;
929                default:
[2300]930                    cur += sprintf(cur, "(%c) show\n", (uint8_t)ch);
[804]931                    break;
932            }
933        }
934    }
935
936    cur += sprintf(cur, "showpage\n");
937
938    /* Crop to really used size */
[1362]939    debug("PS export: alloc %lu bytes, realloc %lu",
940          (unsigned long int)*bytes, (unsigned long int)(cur - data));
[1302]941    *bytes = (uintptr_t)(cur - data);
942    data = realloc(data, *bytes);
[1237]943
[1302]944    return data;
[804]945}
946
947/* Export an SVG vector image */
[2821]948static void *export_svg(caca_canvas_t const *cv, size_t *bytes)
[804]949{
950    static char const svg_header[] =
951        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
952        "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
953        " xmlns=\"http://www.w3.org/2000/svg\""
954        " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
955        " xml:space=\"preserve\" version=\"1.1\"  baseProfile=\"full\">\n";
956
[1302]957    char *data, *cur;
[2305]958    int x, y;
[804]959
960    /* 200 is arbitrary but should be ok */
[1302]961    *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200;
962    cur = data = malloc(*bytes);
[804]963
964    /* Header */
[811]965    cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
966                                    cv->width * 6, cv->height * 10);
[804]967
[1221]968    cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
969                        " style=\"font-family: monospace\">\n");
[804]970
971    /* Background */
[811]972    for(y = 0; y < cv->height; y++)
[804]973    {
[1254]974        uint32_t *lineattr = cv->attrs + y * cv->width;
[804]975
[811]976        for(x = 0; x < cv->width; x++)
[804]977        {
978            cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
979                                " width=\"6\" height=\"10\"/>\n",
[2821]980                                caca_attr_to_rgb12_bg(*lineattr++),
[804]981                                x * 6, y * 10);
982        }
983    }
984
985    /* Text */
[811]986    for(y = 0; y < cv->height; y++)
[804]987    {
[1254]988        uint32_t *lineattr = cv->attrs + y * cv->width;
[811]989        uint32_t *linechar = cv->chars + y * cv->width;
[804]990
[811]991        for(x = 0; x < cv->width; x++)
[804]992        {
[810]993            uint32_t ch = *linechar++;
[804]994
[2821]995            if(ch == ' ' || ch == CACA_MAGIC_FULLWIDTH)
[1221]996            {
997                lineattr++;
998                continue;
999            }
1000
[804]1001            cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
1002                                "x=\"%d\" y=\"%d\">",
[2821]1003                                caca_attr_to_rgb12_fg(*lineattr++),
[1221]1004                                x * 6, (y * 10) + 8);
1005
[810]1006            if(ch < 0x00000020)
[928]1007                *cur++ = '?';
[810]1008            else if(ch > 0x0000007f)
[2821]1009                cur += caca_utf32_to_utf8(cur, ch);
[810]1010            else switch((uint8_t)ch)
[804]1011            {
1012                case '>': cur += sprintf(cur, "&gt;"); break;
1013                case '<': cur += sprintf(cur, "&lt;"); break;
1014                case '&': cur += sprintf(cur, "&amp;"); break;
[928]1015                default: *cur++ = ch; break;
[804]1016            }
1017            cur += sprintf(cur, "</text>\n");
1018        }
1019    }
1020
1021    cur += sprintf(cur, " </g>\n");
1022    cur += sprintf(cur, "</svg>\n");
1023
1024    /* Crop to really used size */
[1362]1025    debug("SVG export: alloc %lu bytes, realloc %lu",
1026          (unsigned long int)*bytes, (unsigned long int)(cur - data));
[1302]1027    *bytes = (uintptr_t)(cur - data);
1028    data = realloc(data, *bytes);
[1237]1029
[1302]1030    return data;
[804]1031}
1032
1033/* Export a TGA image */
[2821]1034static void *export_tga(caca_canvas_t const *cv, size_t *bytes)
[804]1035{
[1237]1036    char const * const *fontlist;
[1302]1037    char *data, *cur;
[2821]1038    caca_font_t *f;
[2305]1039    int i, w, h;
[804]1040
[2821]1041    fontlist = caca_get_font_list();
[805]1042    if(!fontlist[0])
[1302]1043    {
[1362]1044        seterrno(EINVAL);
[1302]1045        return NULL;
1046    }
[804]1047
[2821]1048    f = caca_load_font(fontlist[0], 0);
[804]1049
[2821]1050    w = caca_get_canvas_width(cv) * caca_get_font_width(f);
1051    h = caca_get_canvas_height(cv) * caca_get_font_height(f);
[804]1052
[1302]1053    *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
1054    cur = data = malloc(*bytes);
[804]1055
1056    /* ID Length */
1057    cur += sprintf(cur, "%c", 0);
1058    /* Color Map Type: no colormap */
1059    cur += sprintf(cur, "%c", 0);
1060    /* Image Type: uncompressed truecolor */
1061    cur += sprintf(cur, "%c", 2);
1062    /* Color Map Specification: no color map */
1063    memset(cur, 0, 5); cur += 5;
1064
1065    /* Image Specification */
1066    cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
1067    cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
1068    cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
1069    cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
1070    cur += sprintf(cur, "%c", 32); /* Pixel Depth */
1071    cur += sprintf(cur, "%c", 40); /* Image Descriptor */
1072
1073    /* Image ID: no ID */
1074    /* Color Map Data: no colormap */
1075
1076    /* Image Data */
[2821]1077    caca_render_canvas(cv, f, cur, w, h, 4 * w);
[804]1078
1079    /* Swap bytes. What a waste of time. */
1080    for(i = 0; i < w * h * 4; i += 4)
1081    {
[1438]1082        char c;
1083        c = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = c;
1084        c = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = c;
[804]1085    }
1086
[2821]1087    caca_free_font(f);
[1237]1088
[1302]1089    return data;
[804]1090}
1091
[2826]1092/*
1093 * XXX: The following functions are aliases.
1094 */
1095
1096void *cucul_export_memory(cucul_canvas_t const *, char const *,
1097                          size_t *) CACA_ALIAS(caca_export_memory);
1098char const * const * cucul_get_export_list(void)
1099         CACA_ALIAS(caca_get_export_list);
1100
Note: See TracBrowser for help on using the repository browser.