source: libcaca/trunk/cucul/export.c @ 2102

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