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

Last change on this file since 1104 was 1104, checked in by Sam Hocevar, 14 years ago
  • Fix enumeration layout in the manpage documentation.
  • Property svn:keywords set to Id
File size: 24.7 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 1104 2006-09-25 16:00:10Z 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 = -1;
219        uint8_t prevbg = -1;
220
221        for(x = 0; x < cv->width; x++)
222        {
223            uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
224            uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
225            uint32_t ch = linechar[x];
226
227            if(fg != prevfg || bg != prevbg)
228            {
229                cur += sprintf(cur, "\033[0;");
230
231                if(fg < 8)
232                    if(bg < 8)
233                        cur += sprintf(cur, "3%d;4%dm", fg, bg);
234                    else
235                        cur += sprintf(cur, "5;3%d;4%d;10%dm",
236                                            fg, bg - 8, bg - 8);
237                else
238                    if(bg < 8)
239                        cur += sprintf(cur, "1;3%d;4%d;9%dm",
240                                            fg - 8, bg, fg - 8);
241                    else
242                        cur += sprintf(cur, "5;1;3%d;4%d;9%d;10%dm",
243                                            fg - 8, bg - 8, fg - 8, bg - 8);
244            }
245
246            cur += cucul_utf32_to_utf8(cur, ch);
247
248            prevfg = fg;
249            prevbg = bg;
250        }
251
252        cur += sprintf(cur, "\033[0m\r\n");
253    }
254
255    /* Crop to really used size */
256    ex->size = (uintptr_t)(cur - ex->data);
257    ex->data = realloc(ex->data, ex->size);
258}
259
260/* Generate ANSI representation of current canvas. */
261static void export_ansi(cucul_canvas_t *cv, cucul_buffer_t *ex)
262{
263    static uint8_t const palette[] =
264    {
265        0,  4,  2,  6, 1,  5,  3,  7,
266        8, 12, 10, 14, 9, 13, 11, 15
267    };
268
269    char *cur;
270    unsigned int x, y;
271
272    uint8_t prevfg = -1;
273    uint8_t prevbg = -1;
274
275    /* 16 bytes assumed for max length per pixel ('\e[5;1;3x;4ym' plus
276     * 1 byte for a CP437 character).
277     * Add height*9 to that (zeroes color at the end and jump to next line) */
278    ex->size = (cv->height * 9) + (cv->width * cv->height * 16);
279    ex->data = malloc(ex->size);
280
281    cur = ex->data;
282
283    for(y = 0; y < cv->height; y++)
284    {
285        uint32_t *lineattr = cv->attr + y * cv->width;
286        uint32_t *linechar = cv->chars + y * cv->width;
287
288        for(x = 0; x < cv->width; x++)
289        {
290            uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
291            uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
292            uint32_t ch = linechar[x];
293
294            if(fg != prevfg || bg != prevbg)
295            {
296                cur += sprintf(cur, "\033[0;");
297
298                if(fg < 8)
299                    if(bg < 8)
300                        cur += sprintf(cur, "3%d;4%dm", fg, bg);
301                    else
302                        cur += sprintf(cur, "5;3%d;4%dm", fg, bg - 8);
303                else
304                    if(bg < 8)
305                        cur += sprintf(cur, "1;3%d;4%dm", fg - 8, bg);
306                    else
307                        cur += sprintf(cur, "5;1;3%d;4%dm", fg - 8, bg - 8);
308            }
309
310            *cur++ = cucul_utf32_to_cp437(ch);
311
312            prevfg = fg;
313            prevbg = bg;
314        }
315
316        if(cv->width == 80)
317        {
318            cur += sprintf(cur, "\033[s\n\033[u");
319        }
320        else
321        {
322            cur += sprintf(cur, "\033[0m\r\n");
323            prevfg = -1;
324            prevbg = -1;
325        }
326    }
327
328    /* Crop to really used size */
329    ex->size = (uintptr_t)(cur - ex->data);
330    ex->data = realloc(ex->data, ex->size);
331}
332
333/* Generate HTML representation of current canvas. */
334static void export_html(cucul_canvas_t *cv, cucul_buffer_t *ex)
335{
336    char *cur;
337    unsigned int x, y, len;
338
339    /* The HTML header: less than 1000 bytes
340     * A line: 7 chars for "<br />\n"
341     * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
342     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
343     *          7 chars for "</span>" */
344    ex->size = 1000 + cv->height * (7 + cv->width * (47 + 9 + 7));
345    ex->data = malloc(ex->size);
346
347    cur = ex->data;
348
349    /* HTML header */
350    cur += sprintf(cur, "<html><head>\n");
351    cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n", VERSION);
352    cur += sprintf(cur, "</head><body>\n");
353
354    cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
355                        "font-family: monospace, fixed; font-weight: bold;");
356
357    for(y = 0; y < cv->height; y++)
358    {
359        uint32_t *lineattr = cv->attr + y * cv->width;
360        uint32_t *linechar = cv->chars + y * cv->width;
361
362        for(x = 0; x < cv->width; x += len)
363        {
364            cur += sprintf(cur, "<span style=\"color:#%.03x;"
365                                "background-color:#%.03x\">",
366                                _cucul_argb32_to_rgb12fg(lineattr[x]),
367                                _cucul_argb32_to_rgb12bg(lineattr[x]));
368
369            for(len = 0;
370                x + len < cv->width && lineattr[x + len] == lineattr[x];
371                len++)
372            {
373                if(linechar[x + len] <= 0x00000020)
374                    cur += sprintf(cur, "&nbsp;");
375                else if(linechar[x + len] < 0x00000080)
376                    cur += sprintf(cur, "%c",
377                                   (unsigned char)linechar[x + len]);
378                else
379                    cur += sprintf(cur, "&#%i;",
380                                   (unsigned int)linechar[x + len]);
381            }
382            cur += sprintf(cur, "</span>");
383        }
384        /* New line */
385        cur += sprintf(cur, "<br />\n");
386    }
387
388    cur += sprintf(cur, "</div></body></html>\n");
389
390    /* Crop to really used size */
391    ex->size = strlen(ex->data) + 1;
392    ex->data = realloc(ex->data, ex->size);
393}
394
395/* Export an HTML3 document. This function is way bigger than export_html(),
396 * but permits viewing in old browsers (or limited ones such as links). It
397 * will not work under gecko (mozilla rendering engine) unless you set a
398 * correct header. */
399static void export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
400{
401    char *cur;
402    unsigned int x, y, len;
403
404    /* The HTML table markup: less than 1000 bytes
405     * A line: 10 chars for "<tr></tr>\n"
406     * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
407     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
408     *          12 chars for "</font></td>" */
409    ex->size = 1000 + cv->height * (10 + cv->width * (40 + 9 + 12));
410    ex->data = malloc(ex->size);
411
412    cur = ex->data;
413
414    /* Table */
415    cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
416                        cv->height);
417
418    for(y = 0; y < cv->height; y++)
419    {
420        uint32_t *lineattr = cv->attr + y * cv->width;
421        uint32_t *linechar = cv->chars + y * cv->width;
422
423        cur += sprintf(cur, "<tr>");
424
425        for(x = 0; x < cv->width; x += len)
426        {
427            unsigned int i;
428
429            /* Use colspan option to factor cells with same attributes
430             * (see below) */
431            len = 1;
432            while(x + len < cv->width && lineattr[x + len] == lineattr[x])
433                len++;
434
435            cur += sprintf(cur, "<td bgcolor=#%.06lx", (unsigned long int)
436                           _cucul_argb32_to_rgb24bg(lineattr[x]));
437
438            if(len > 1)
439                cur += sprintf(cur, " colspan=%d", len);
440
441            cur += sprintf(cur, "><font color=#%.06lx>", (unsigned long int)
442                           _cucul_argb32_to_rgb24fg(lineattr[x]));
443
444            for(i = 0; i < len; i++)
445            {
446                if(linechar[x + i] <= 0x00000020)
447                    cur += sprintf(cur, "&nbsp;");
448                else if(linechar[x + i] < 0x00000080)
449                    cur += sprintf(cur, "%c", (unsigned char)linechar[x + i]);
450                else
451                    cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
452            }
453
454            cur += sprintf(cur, "</font></td>");
455        }
456        cur += sprintf(cur, "</tr>\n");
457    }
458
459    /* Footer */
460    cur += sprintf(cur, "</table>\n");
461
462    /* Crop to really used size */
463    ex->size = (uintptr_t)(cur - ex->data);
464    ex->data = realloc(ex->data, ex->size);
465}
466
467/* Export a text file with IRC colours */
468static void export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
469{
470    static uint8_t const palette[] =
471    {
472        1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
473        14, 12, 9, 11, 4, 13, 8, 0, /* Light */
474    };
475
476    char *cur;
477    unsigned int x, y;
478
479    /* 16 bytes assumed for max length per pixel. Worst case scenario:
480     * ^Cxx,yy   6 bytes
481     * ^B^B      2 bytes
482     * ch        6 bytes
483     * \r\n      2 bytes
484     * In real life, the average bytes per pixel value will be around 5.
485     */
486
487    ex->size = 2 + (cv->width * cv->height * 16);
488    ex->data = malloc(ex->size);
489
490    cur = ex->data;
491
492    for(y = 0; y < cv->height; y++)
493    {
494        uint32_t *lineattr = cv->attr + y * cv->width;
495        uint32_t *linechar = cv->chars + y * cv->width;
496
497        uint8_t prevfg = 0x10;
498        uint8_t prevbg = 0x10;
499
500        for(x = 0; x < cv->width; x++)
501        {
502            uint32_t attr = lineattr[x];
503            uint8_t fg = palette[_cucul_argb32_to_ansi4fg(attr)];
504            uint8_t bg = palette[_cucul_argb32_to_ansi4bg(attr)];
505            uint32_t ch = linechar[x];
506
507            if((attr & 0xffff) == CUCUL_COLOR_DEFAULT)
508                fg = 0x10;
509
510            if((attr >> 16) == CUCUL_COLOR_TRANSPARENT)
511                bg = 0x10;
512#if 0
513            if(bg == prevbg)
514            {
515                if(fg == prevfg)
516                    ; /* Same fg/bg, do nothing */
517                else if(ch == (uint32_t)' ')
518                    fg = prevfg; /* Hackety hack */
519                else
520                {
521                    cur += sprintf(cur, "\x03%d", fg);
522                    if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
523                        cur += sprintf(cur, "\x02\x02");
524                }
525            }
526            else
527            {
528                if(fg == prevfg)
529                    cur += sprintf(cur, "\x03,%d", bg);
530                else
531                    cur += sprintf(cur, "\x03%d,%d", fg, bg);
532
533                if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
534                    cur += sprintf(cur, "\x02\x02");
535            }
536#else
537            if(bg != prevbg || fg != prevfg)
538            {
539                if(bg == 0x10)
540                {
541                    if(fg == 0x10)
542                        cur += sprintf(cur, "\x0f");
543                    else if(prevbg == 0x10)
544                        cur += sprintf(cur, "\x03%d", fg);
545                    else
546                        cur += sprintf(cur, "\x0f\x03%d", fg);
547                }
548                else
549                {
550                    if(fg == 0x10)
551                        cur += sprintf(cur, "\x0f\x03,%d", bg);
552                    else
553                        cur += sprintf(cur, "\x03%d,%d", fg, bg);
554                }
555
556                if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
557                    cur += sprintf(cur, "\x02\x02");
558            }
559#endif
560            cur += cucul_utf32_to_utf8(cur, ch);
561            prevfg = fg;
562            prevbg = bg;
563        }
564        *cur++ = '\r';
565        *cur++ = '\n';
566    }
567
568    /* Crop to really used size */
569    ex->size = (uintptr_t)(cur - ex->data);
570    ex->data = realloc(ex->data, ex->size);
571}
572
573/* Export a PostScript document. */
574static void export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
575{
576    static char const *ps_header =
577        "%!\n"
578        "%% libcaca PDF export\n"
579        "%%LanguageLevel: 2\n"
580        "%%Pages: 1\n"
581        "%%DocumentData: Clean7Bit\n"
582        "/csquare {\n"
583        "  newpath\n"
584        "  0 0 moveto\n"
585        "  0 1 rlineto\n"
586        "  1 0 rlineto\n"
587        "  0 -1 rlineto\n"
588        "  closepath\n"
589        "  setrgbcolor\n"
590        "  fill\n"
591        "} def\n"
592        "/S {\n"
593        "  Show\n"
594        "} bind def\n"
595        "/Courier-Bold findfont\n"
596        "8 scalefont\n"
597        "setfont\n"
598        "gsave\n"
599        "6 10 scale\n";
600
601    char *cur;
602    unsigned int x, y;
603
604    /* 200 is arbitrary but should be ok */
605    ex->size = strlen(ps_header) + (cv->width * cv->height * 200);
606    ex->data = malloc(ex->size);
607
608    cur = ex->data;
609
610    /* Header */
611    cur += sprintf(cur, "%s", ps_header);
612    cur += sprintf(cur, "0 %d translate\n", cv->height);
613
614    /* Background, drawn using csquare macro defined in header */
615    for(y = cv->height; y--; )
616    {
617        uint32_t *lineattr = cv->attr + y * cv->width;
618
619        for(x = 0; x < cv->width; x++)
620        {
621            uint8_t argb[8];
622            _cucul_argb32_to_argb4(*lineattr++, argb);
623            cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
624                           (float)argb[1] * (1.0 / 0xf),
625                           (float)argb[2] * (1.0 / 0xf),
626                           (float)argb[3] * (1.0 / 0xf));
627        }
628
629        /* Return to beginning of the line, and jump to the next one */
630        cur += sprintf(cur, "-%d 1 translate\n", cv->width);
631    }
632
633    cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
634    cur += sprintf(cur, "0 %d translate\n", cv->height*10);
635
636    for(y = cv->height; y--; )
637    {
638        uint32_t *lineattr = cv->attr + (cv->height - y - 1) * cv->width;
639        uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
640
641        for(x = 0; x < cv->width; x++)
642        {
643            uint8_t argb[8];
644            uint32_t ch = *linechar++;
645
646            _cucul_argb32_to_argb4(*lineattr++, argb);
647
648            cur += sprintf(cur, "newpath\n");
649            cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
650            cur += sprintf(cur, "%f %f %f setrgbcolor\n",
651                           (float)argb[5] * (1.0 / 0xf),
652                           (float)argb[6] * (1.0 / 0xf),
653                           (float)argb[7] * (1.0 / 0xf));
654
655            if(ch < 0x00000020)
656                cur += sprintf(cur, "(?) show\n");
657            else if(ch >= 0x00000080)
658                cur += sprintf(cur, "(?) show\n");
659            else switch((uint8_t)(ch & 0x7f))
660            {
661                case '\\':
662                case '(':
663                case ')':
664                    cur += sprintf(cur, "(\\%c) show\n", (unsigned char)ch);
665                    break;
666                default:
667                    cur += sprintf(cur, "(%c) show\n", (unsigned char)ch);
668                    break;
669            }
670        }
671    }
672
673    cur += sprintf(cur, "showpage\n");
674
675    /* Crop to really used size */
676    ex->size = (uintptr_t)(cur - ex->data);
677    ex->data = realloc(ex->data, ex->size);
678}
679
680/* Export an SVG vector image */
681static void export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
682{
683    static char const svg_header[] =
684        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
685        "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
686        " xmlns=\"http://www.w3.org/2000/svg\""
687        " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
688        " xml:space=\"preserve\" version=\"1.1\"  baseProfile=\"full\">\n";
689
690    char *cur;
691    unsigned int x, y;
692
693    /* 200 is arbitrary but should be ok */
694    ex->size = strlen(svg_header) + (cv->width * cv->height * 200);
695    ex->data = malloc(ex->size);
696
697    cur = ex->data;
698
699    /* Header */
700    cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
701                                    cv->width * 6, cv->height * 10);
702
703    cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"12\">\n");
704
705    /* Background */
706    for(y = 0; y < cv->height; y++)
707    {
708        uint32_t *lineattr = cv->attr + y * cv->width;
709
710        for(x = 0; x < cv->width; x++)
711        {
712            cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
713                                " width=\"6\" height=\"10\"/>\n",
714                                _cucul_argb32_to_rgb12bg(*lineattr++),
715                                x * 6, y * 10);
716        }
717    }
718
719    /* Text */
720    for(y = 0; y < cv->height; y++)
721    {
722        uint32_t *lineattr = cv->attr + y * cv->width;
723        uint32_t *linechar = cv->chars + y * cv->width;
724
725        for(x = 0; x < cv->width; x++)
726        {
727            uint32_t ch = *linechar++;
728
729            cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
730                                "x=\"%d\" y=\"%d\">",
731                                _cucul_argb32_to_rgb12fg(*lineattr++),
732                                x * 6, (y * 10) + 10);
733            if(ch < 0x00000020)
734                *cur++ = '?';
735            else if(ch > 0x0000007f)
736                cur += cucul_utf32_to_utf8(cur, ch);
737            else switch((uint8_t)ch)
738            {
739                case '>': cur += sprintf(cur, "&gt;"); break;
740                case '<': cur += sprintf(cur, "&lt;"); break;
741                case '&': cur += sprintf(cur, "&amp;"); break;
742                default: *cur++ = ch; break;
743            }
744            cur += sprintf(cur, "</text>\n");
745        }
746    }
747
748    cur += sprintf(cur, " </g>\n");
749    cur += sprintf(cur, "</svg>\n");
750
751    /* Crop to really used size */
752    ex->size = (uintptr_t)(cur - ex->data);
753    ex->data = realloc(ex->data, ex->size);
754}
755
756/* Export a TGA image */
757static void export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
758{
759    char const * const * fontlist;
760    char * cur;
761    cucul_font_t *f;
762    unsigned int i, w, h;
763
764    fontlist = cucul_get_font_list();
765    if(!fontlist[0])
766        return;
767
768    f = cucul_load_font(fontlist[0], 0);
769
770    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
771    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
772
773    ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
774    ex->data = malloc(ex->size);
775
776    cur = ex->data;
777
778    /* ID Length */
779    cur += sprintf(cur, "%c", 0);
780    /* Color Map Type: no colormap */
781    cur += sprintf(cur, "%c", 0);
782    /* Image Type: uncompressed truecolor */
783    cur += sprintf(cur, "%c", 2);
784    /* Color Map Specification: no color map */
785    memset(cur, 0, 5); cur += 5;
786
787    /* Image Specification */
788    cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
789    cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
790    cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
791    cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
792    cur += sprintf(cur, "%c", 32); /* Pixel Depth */
793    cur += sprintf(cur, "%c", 40); /* Image Descriptor */
794
795    /* Image ID: no ID */
796    /* Color Map Data: no colormap */
797
798    /* Image Data */
799    cucul_render_canvas(cv, f, cur, w, h, 4 * w);
800
801    /* Swap bytes. What a waste of time. */
802    for(i = 0; i < w * h * 4; i += 4)
803    {
804        char w;
805        w = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = w;
806        w = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = w;
807    }
808
809    cucul_free_font(f);
810}
811
Note: See TracBrowser for help on using the repository browser.