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

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