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

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