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

Last change on this file since 1254 was 1254, checked in by Sam Hocevar, 13 years ago
  • Use the LSB of alpha and blue components to encode additional flags such as bold or underline. No higher level support for these yet, but at least everything is consistent.
  • Created cucul_set_attr_ansi() and cucul_set_attr_argb() which are bound to replace cucul_set_color() and cucul_set_truecolor().
  • Property svn:keywords set to Id
File size: 25.5 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 1254 2006-10-28 23:53:46Z 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 *attrs = cv->attrs;
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 = *attrs++;
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->attrs + 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 & 0xeffe) == (CUCUL_COLOR_DEFAULT << 4)) ?
234                     0x10 : palette[_cucul_attr_to_ansi4fg(attr)];
235            bg = (((attr >> 16) & 0xeffe) == (CUCUL_COLOR_TRANSPARENT << 4)) ?
236                     0x10 : palette[_cucul_attr_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->attrs + 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_attr_to_ansi4fg(lineattr[x])];
306            uint8_t bg = palette[_cucul_attr_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->attrs + 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_attr_to_rgb12fg(lineattr[x]),
387                                _cucul_attr_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->attrs + 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_attr_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_attr_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    /* 14 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     * 3 bytes for max length per line. Worst case scenario:
512     * <spc>     1 byte (for empty lines)
513     * \r\n      2 bytes
514     * In real life, the average bytes per pixel value will be around 5.
515     */
516
517    ex->size = 2 + cv->height * (3 + cv->width * 14);
518    ex->data = malloc(ex->size);
519
520    cur = ex->data;
521
522    for(y = 0; y < cv->height; y++)
523    {
524        uint32_t *lineattr = cv->attrs + y * cv->width;
525        uint32_t *linechar = cv->chars + y * cv->width;
526
527        uint8_t prevfg = 0x10;
528        uint8_t prevbg = 0x10;
529
530        for(x = 0; x < cv->width; x++)
531        {
532            uint32_t attr = lineattr[x];
533            uint8_t fg = palette[_cucul_attr_to_ansi4fg(attr)];
534            uint8_t bg = palette[_cucul_attr_to_ansi4bg(attr)];
535            uint32_t ch = linechar[x];
536
537            if(ch == CUCUL_MAGIC_FULLWIDTH)
538                continue;
539
540            if((attr & 0xeffe) == (CUCUL_COLOR_DEFAULT << 4))
541                fg = 0x10;
542
543            if(((attr >> 16) & 0xeffe) == (CUCUL_COLOR_TRANSPARENT << 4))
544                bg = 0x10;
545
546            /* TODO: optimise series of same fg / same bg
547             *       don't change fg value if ch == ' '
548             *       make sure the \x03,%d trick works everywhere */
549            if(bg != prevbg || fg != prevfg)
550            {
551                int need_escape = 0;
552
553                if(bg == 0x10)
554                {
555                    if(fg == 0x10)
556                        cur += sprintf(cur, "\x0f");
557                    else
558                    {
559                        if(prevbg == 0x10)
560                            cur += sprintf(cur, "\x03%d", fg);
561                        else
562                            cur += sprintf(cur, "\x0f\x03%d", fg);
563
564                        if(ch == (uint32_t)',')
565                            need_escape = 1;
566                    }
567                }
568                else
569                {
570                    if(fg == 0x10)
571                        cur += sprintf(cur, "\x0f\x03,%d", bg);
572                    else
573                        cur += sprintf(cur, "\x03%d,%d", fg, bg);
574                }
575
576                if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
577                    need_escape = 1;
578
579                if(need_escape)
580                    cur += sprintf(cur, "\x02\x02");
581            }
582
583            cur += cucul_utf32_to_utf8(cur, ch);
584            prevfg = fg;
585            prevbg = bg;
586        }
587
588        /* TODO: do the same the day we optimise whole lines above */
589        if(!cv->width)
590            *cur++ = ' ';
591
592        *cur++ = '\r';
593        *cur++ = '\n';
594    }
595
596    /* Crop to really used size */
597    ex->size = (uintptr_t)(cur - ex->data);
598    ex->data = realloc(ex->data, ex->size);
599
600    return 0;
601}
602
603/* Export a PostScript document. */
604static int export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
605{
606    static char const *ps_header =
607        "%!\n"
608        "%% libcaca PDF export\n"
609        "%%LanguageLevel: 2\n"
610        "%%Pages: 1\n"
611        "%%DocumentData: Clean7Bit\n"
612        "/csquare {\n"
613        "  newpath\n"
614        "  0 0 moveto\n"
615        "  0 1 rlineto\n"
616        "  1 0 rlineto\n"
617        "  0 -1 rlineto\n"
618        "  closepath\n"
619        "  setrgbcolor\n"
620        "  fill\n"
621        "} def\n"
622        "/S {\n"
623        "  Show\n"
624        "} bind def\n"
625        "/Courier-Bold findfont\n"
626        "8 scalefont\n"
627        "setfont\n"
628        "gsave\n"
629        "6 10 scale\n";
630
631    char *cur;
632    unsigned int x, y;
633
634    /* 200 is arbitrary but should be ok */
635    ex->size = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
636    ex->data = malloc(ex->size);
637
638    cur = ex->data;
639
640    /* Header */
641    cur += sprintf(cur, "%s", ps_header);
642    cur += sprintf(cur, "0 %d translate\n", cv->height);
643
644    /* Background, drawn using csquare macro defined in header */
645    for(y = cv->height; y--; )
646    {
647        uint32_t *lineattr = cv->attrs + y * cv->width;
648
649        for(x = 0; x < cv->width; x++)
650        {
651            uint8_t argb[8];
652            _cucul_attr_to_argb4(*lineattr++, argb);
653            cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
654                           (float)argb[1] * (1.0 / 0xf),
655                           (float)argb[2] * (1.0 / 0xf),
656                           (float)argb[3] * (1.0 / 0xf));
657        }
658
659        /* Return to beginning of the line, and jump to the next one */
660        cur += sprintf(cur, "-%d 1 translate\n", cv->width);
661    }
662
663    cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
664    cur += sprintf(cur, "0 %d translate\n", cv->height*10);
665
666    for(y = cv->height; y--; )
667    {
668        uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
669        uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
670
671        for(x = 0; x < cv->width; x++)
672        {
673            uint8_t argb[8];
674            uint32_t ch = *linechar++;
675
676            _cucul_attr_to_argb4(*lineattr++, argb);
677
678            cur += sprintf(cur, "newpath\n");
679            cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
680            cur += sprintf(cur, "%f %f %f setrgbcolor\n",
681                           (float)argb[5] * (1.0 / 0xf),
682                           (float)argb[6] * (1.0 / 0xf),
683                           (float)argb[7] * (1.0 / 0xf));
684
685            if(ch < 0x00000020)
686                cur += sprintf(cur, "(?) show\n");
687            else if(ch >= 0x00000080)
688                cur += sprintf(cur, "(?) show\n");
689            else switch((uint8_t)(ch & 0x7f))
690            {
691                case '\\':
692                case '(':
693                case ')':
694                    cur += sprintf(cur, "(\\%c) show\n", (unsigned char)ch);
695                    break;
696                default:
697                    cur += sprintf(cur, "(%c) show\n", (unsigned char)ch);
698                    break;
699            }
700        }
701    }
702
703    cur += sprintf(cur, "showpage\n");
704
705    /* Crop to really used size */
706    ex->size = (uintptr_t)(cur - ex->data);
707    ex->data = realloc(ex->data, ex->size);
708
709    return 0;
710}
711
712/* Export an SVG vector image */
713static int export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
714{
715    static char const svg_header[] =
716        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
717        "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
718        " xmlns=\"http://www.w3.org/2000/svg\""
719        " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
720        " xml:space=\"preserve\" version=\"1.1\"  baseProfile=\"full\">\n";
721
722    char *cur;
723    unsigned int x, y;
724
725    /* 200 is arbitrary but should be ok */
726    ex->size = strlen(svg_header) + 128 + cv->width * cv->height * 200;
727    ex->data = malloc(ex->size);
728
729    cur = ex->data;
730
731    /* Header */
732    cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
733                                    cv->width * 6, cv->height * 10);
734
735    cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
736                        " style=\"font-family: monospace\">\n");
737
738    /* Background */
739    for(y = 0; y < cv->height; y++)
740    {
741        uint32_t *lineattr = cv->attrs + y * cv->width;
742
743        for(x = 0; x < cv->width; x++)
744        {
745            cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
746                                " width=\"6\" height=\"10\"/>\n",
747                                _cucul_attr_to_rgb12bg(*lineattr++),
748                                x * 6, y * 10);
749        }
750    }
751
752    /* Text */
753    for(y = 0; y < cv->height; y++)
754    {
755        uint32_t *lineattr = cv->attrs + y * cv->width;
756        uint32_t *linechar = cv->chars + y * cv->width;
757
758        for(x = 0; x < cv->width; x++)
759        {
760            uint32_t ch = *linechar++;
761
762            if(ch == ' ' || ch == CUCUL_MAGIC_FULLWIDTH)
763            {
764                lineattr++;
765                continue;
766            }
767
768            cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
769                                "x=\"%d\" y=\"%d\">",
770                                _cucul_attr_to_rgb12fg(*lineattr++),
771                                x * 6, (y * 10) + 8);
772
773            if(ch < 0x00000020)
774                *cur++ = '?';
775            else if(ch > 0x0000007f)
776                cur += cucul_utf32_to_utf8(cur, ch);
777            else switch((uint8_t)ch)
778            {
779                case '>': cur += sprintf(cur, "&gt;"); break;
780                case '<': cur += sprintf(cur, "&lt;"); break;
781                case '&': cur += sprintf(cur, "&amp;"); break;
782                default: *cur++ = ch; break;
783            }
784            cur += sprintf(cur, "</text>\n");
785        }
786    }
787
788    cur += sprintf(cur, " </g>\n");
789    cur += sprintf(cur, "</svg>\n");
790
791    /* Crop to really used size */
792    ex->size = (uintptr_t)(cur - ex->data);
793    ex->data = realloc(ex->data, ex->size);
794
795    return 0;
796}
797
798/* Export a TGA image */
799static int export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
800{
801    char const * const *fontlist;
802    char * cur;
803    cucul_font_t *f;
804    unsigned int i, w, h;
805
806    fontlist = cucul_get_font_list();
807    if(!fontlist[0])
808        return -1;
809
810    f = cucul_load_font(fontlist[0], 0);
811
812    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
813    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
814
815    ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
816    ex->data = malloc(ex->size);
817
818    cur = ex->data;
819
820    /* ID Length */
821    cur += sprintf(cur, "%c", 0);
822    /* Color Map Type: no colormap */
823    cur += sprintf(cur, "%c", 0);
824    /* Image Type: uncompressed truecolor */
825    cur += sprintf(cur, "%c", 2);
826    /* Color Map Specification: no color map */
827    memset(cur, 0, 5); cur += 5;
828
829    /* Image Specification */
830    cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
831    cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
832    cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
833    cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
834    cur += sprintf(cur, "%c", 32); /* Pixel Depth */
835    cur += sprintf(cur, "%c", 40); /* Image Descriptor */
836
837    /* Image ID: no ID */
838    /* Color Map Data: no colormap */
839
840    /* Image Data */
841    cucul_render_canvas(cv, f, cur, w, h, 4 * w);
842
843    /* Swap bytes. What a waste of time. */
844    for(i = 0; i < w * h * 4; i += 4)
845    {
846        char w;
847        w = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = w;
848        w = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = w;
849    }
850
851    cucul_free_font(f);
852
853    return 0;
854}
855
Note: See TracBrowser for help on using the repository browser.