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

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