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

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