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

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