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

Last change on this file since 1264 was 1264, checked in by Sam Hocevar, 14 years ago
  • Property svn:keywords set to Id
File size: 26.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 1264 2006-10-30 16:37:36Z 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 >> 4) & 0x3fff) == CUCUL_DEFAULT) ?
234                     0x10 : palette[_cucul_attr_to_ansi4fg(attr)];
235            bg = (((attr >> 18) & 0x3fff) == CUCUL_TRANSPARENT) ?
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     *          83 chars for ";font-weight..."
363     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
364     *          7 chars for "</span>" */
365    ex->size = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
366    ex->data = malloc(ex->size);
367
368    cur = ex->data;
369
370    /* HTML header */
371    cur += sprintf(cur, "<html><head>\n");
372    cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n", VERSION);
373    cur += sprintf(cur, "</head><body>\n");
374
375    cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
376                        "font-family: monospace, fixed; font-weight: bold;");
377
378    for(y = 0; y < cv->height; y++)
379    {
380        uint32_t *lineattr = cv->attrs + y * cv->width;
381        uint32_t *linechar = cv->chars + y * cv->width;
382
383        for(x = 0; x < cv->width; x += len)
384        {
385            cur += sprintf(cur, "<span style=\"color:#%.03x;"
386                                "background-color:#%.03x",
387                                _cucul_attr_to_rgb12fg(lineattr[x]),
388                                _cucul_attr_to_rgb12bg(lineattr[x]));
389            if(lineattr[x] & CUCUL_BOLD)
390                cur += sprintf(cur, ";font-weight:bold");
391            if(lineattr[x] & CUCUL_ITALICS)
392                cur += sprintf(cur, ";font-style:italic");
393            if(lineattr[x] & CUCUL_UNDERLINE)
394                cur += sprintf(cur, ";text-decoration:underline");
395            if(lineattr[x] & CUCUL_BLINK)
396                cur += sprintf(cur, ";text-decoration:blink");
397            cur += sprintf(cur, "\">");
398
399            for(len = 0;
400                x + len < cv->width && lineattr[x + len] == lineattr[x];
401                len++)
402            {
403                if(linechar[x + len] == CUCUL_MAGIC_FULLWIDTH)
404                    ;
405                else if(linechar[x + len] <= 0x00000020)
406                    cur += sprintf(cur, "&nbsp;");
407                else if(linechar[x + len] < 0x00000080)
408                    cur += sprintf(cur, "%c",
409                                   (unsigned char)linechar[x + len]);
410                else
411                    cur += sprintf(cur, "&#%i;",
412                                   (unsigned int)linechar[x + len]);
413            }
414            cur += sprintf(cur, "</span>");
415        }
416        /* New line */
417        cur += sprintf(cur, "<br />\n");
418    }
419
420    cur += sprintf(cur, "</div></body></html>\n");
421
422    /* Crop to really used size */
423    ex->size = strlen(ex->data) + 1;
424    ex->data = realloc(ex->data, ex->size);
425
426    return 0;
427}
428
429/* Export an HTML3 document. This function is way bigger than export_html(),
430 * but permits viewing in old browsers (or limited ones such as links). It
431 * will not work under gecko (mozilla rendering engine) unless you set a
432 * correct header. */
433static int export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
434{
435    char *cur;
436    unsigned int x, y, len;
437
438    /* The HTML table markup: less than 1000 bytes
439     * A line: 10 chars for "<tr></tr>\n"
440     * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
441     *          up to 36 chars for "<b><i><u><blink></blink></u></i></b>"
442     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
443     *          12 chars for "</font></td>" */
444    ex->size = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
445    ex->data = malloc(ex->size);
446
447    cur = ex->data;
448
449    /* Table */
450    cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
451                        cv->height);
452
453    for(y = 0; y < cv->height; y++)
454    {
455        uint32_t *lineattr = cv->attrs + y * cv->width;
456        uint32_t *linechar = cv->chars + y * cv->width;
457
458        cur += sprintf(cur, "<tr>");
459
460        for(x = 0; x < cv->width; x += len)
461        {
462            unsigned int i;
463
464            /* Use colspan option to factor cells with same attributes
465             * (see below) */
466            len = 1;
467            while(x + len < cv->width && lineattr[x + len] == lineattr[x])
468                len++;
469
470            cur += sprintf(cur, "<td bgcolor=#%.06lx", (unsigned long int)
471                           _cucul_attr_to_rgb24bg(lineattr[x]));
472
473            if(len > 1)
474                cur += sprintf(cur, " colspan=%d", len);
475
476            cur += sprintf(cur, "><font color=#%.06lx>", (unsigned long int)
477                           _cucul_attr_to_rgb24fg(lineattr[x]));
478
479            if(lineattr[x] & CUCUL_BOLD)
480                cur += sprintf(cur, "<b>");
481            if(lineattr[x] & CUCUL_ITALICS)
482                cur += sprintf(cur, "<i>");
483            if(lineattr[x] & CUCUL_UNDERLINE)
484                cur += sprintf(cur, "<u>");
485            if(lineattr[x] & CUCUL_BLINK)
486                cur += sprintf(cur, "<blink>");
487
488            for(i = 0; i < len; i++)
489            {
490                if(linechar[x + i] == CUCUL_MAGIC_FULLWIDTH)
491                    ;
492                else if(linechar[x + i] <= 0x00000020)
493                    cur += sprintf(cur, "&nbsp;");
494                else if(linechar[x + i] < 0x00000080)
495                    cur += sprintf(cur, "%c", (unsigned char)linechar[x + i]);
496                else
497                    cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
498            }
499
500            if(lineattr[x] & CUCUL_BLINK)
501                cur += sprintf(cur, "</blink>");
502            if(lineattr[x] & CUCUL_UNDERLINE)
503                cur += sprintf(cur, "</u>");
504            if(lineattr[x] & CUCUL_ITALICS)
505                cur += sprintf(cur, "</i>");
506            if(lineattr[x] & CUCUL_BOLD)
507                cur += sprintf(cur, "</b>");
508
509            cur += sprintf(cur, "</font></td>");
510        }
511        cur += sprintf(cur, "</tr>\n");
512    }
513
514    /* Footer */
515    cur += sprintf(cur, "</table>\n");
516
517    /* Crop to really used size */
518    ex->size = (uintptr_t)(cur - ex->data);
519    ex->data = realloc(ex->data, ex->size);
520
521    return 0;
522}
523
524/* Export a text file with IRC colours */
525static int export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
526{
527    static uint8_t const palette[] =
528    {
529        1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
530        14, 12, 9, 11, 4, 13, 8, 0, /* Light */
531    };
532
533    char *cur;
534    unsigned int x, y;
535
536    /* 14 bytes assumed for max length per pixel. Worst case scenario:
537     * ^Cxx,yy   6 bytes
538     * ^B^B      2 bytes
539     * ch        6 bytes
540     * 3 bytes for max length per line. Worst case scenario:
541     * <spc>     1 byte (for empty lines)
542     * \r\n      2 bytes
543     * In real life, the average bytes per pixel value will be around 5.
544     */
545
546    ex->size = 2 + cv->height * (3 + cv->width * 14);
547    ex->data = malloc(ex->size);
548
549    cur = ex->data;
550
551    for(y = 0; y < cv->height; y++)
552    {
553        uint32_t *lineattr = cv->attrs + y * cv->width;
554        uint32_t *linechar = cv->chars + y * cv->width;
555
556        uint8_t prevfg = 0x10;
557        uint8_t prevbg = 0x10;
558
559        for(x = 0; x < cv->width; x++)
560        {
561            uint32_t attr = lineattr[x];
562            uint8_t fg = palette[_cucul_attr_to_ansi4fg(attr)];
563            uint8_t bg = palette[_cucul_attr_to_ansi4bg(attr)];
564            uint32_t ch = linechar[x];
565
566            if(ch == CUCUL_MAGIC_FULLWIDTH)
567                continue;
568
569            if(((attr >> 4) & 0x3fff) == CUCUL_DEFAULT)
570                fg = 0x10;
571
572            if(((attr >> 18) & 0x3fff) == CUCUL_TRANSPARENT)
573                bg = 0x10;
574
575            /* TODO: optimise series of same fg / same bg
576             *       don't change fg value if ch == ' '
577             *       make sure the \x03,%d trick works everywhere */
578            if(bg != prevbg || fg != prevfg)
579            {
580                int need_escape = 0;
581
582                if(bg == 0x10)
583                {
584                    if(fg == 0x10)
585                        cur += sprintf(cur, "\x0f");
586                    else
587                    {
588                        if(prevbg == 0x10)
589                            cur += sprintf(cur, "\x03%d", fg);
590                        else
591                            cur += sprintf(cur, "\x0f\x03%d", fg);
592
593                        if(ch == (uint32_t)',')
594                            need_escape = 1;
595                    }
596                }
597                else
598                {
599                    if(fg == 0x10)
600                        cur += sprintf(cur, "\x0f\x03,%d", bg);
601                    else
602                        cur += sprintf(cur, "\x03%d,%d", fg, bg);
603                }
604
605                if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
606                    need_escape = 1;
607
608                if(need_escape)
609                    cur += sprintf(cur, "\x02\x02");
610            }
611
612            cur += cucul_utf32_to_utf8(cur, ch);
613            prevfg = fg;
614            prevbg = bg;
615        }
616
617        /* TODO: do the same the day we optimise whole lines above */
618        if(!cv->width)
619            *cur++ = ' ';
620
621        *cur++ = '\r';
622        *cur++ = '\n';
623    }
624
625    /* Crop to really used size */
626    ex->size = (uintptr_t)(cur - ex->data);
627    ex->data = realloc(ex->data, ex->size);
628
629    return 0;
630}
631
632/* Export a PostScript document. */
633static int export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
634{
635    static char const *ps_header =
636        "%!\n"
637        "%% libcaca PDF export\n"
638        "%%LanguageLevel: 2\n"
639        "%%Pages: 1\n"
640        "%%DocumentData: Clean7Bit\n"
641        "/csquare {\n"
642        "  newpath\n"
643        "  0 0 moveto\n"
644        "  0 1 rlineto\n"
645        "  1 0 rlineto\n"
646        "  0 -1 rlineto\n"
647        "  closepath\n"
648        "  setrgbcolor\n"
649        "  fill\n"
650        "} def\n"
651        "/S {\n"
652        "  Show\n"
653        "} bind def\n"
654        "/Courier-Bold findfont\n"
655        "8 scalefont\n"
656        "setfont\n"
657        "gsave\n"
658        "6 10 scale\n";
659
660    char *cur;
661    unsigned int x, y;
662
663    /* 200 is arbitrary but should be ok */
664    ex->size = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
665    ex->data = malloc(ex->size);
666
667    cur = ex->data;
668
669    /* Header */
670    cur += sprintf(cur, "%s", ps_header);
671    cur += sprintf(cur, "0 %d translate\n", cv->height);
672
673    /* Background, drawn using csquare macro defined in header */
674    for(y = cv->height; y--; )
675    {
676        uint32_t *lineattr = cv->attrs + y * cv->width;
677
678        for(x = 0; x < cv->width; x++)
679        {
680            uint8_t argb[8];
681            _cucul_attr_to_argb4(*lineattr++, argb);
682            cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
683                           (float)argb[1] * (1.0 / 0xf),
684                           (float)argb[2] * (1.0 / 0xf),
685                           (float)argb[3] * (1.0 / 0xf));
686        }
687
688        /* Return to beginning of the line, and jump to the next one */
689        cur += sprintf(cur, "-%d 1 translate\n", cv->width);
690    }
691
692    cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
693    cur += sprintf(cur, "0 %d translate\n", cv->height*10);
694
695    for(y = cv->height; y--; )
696    {
697        uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
698        uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
699
700        for(x = 0; x < cv->width; x++)
701        {
702            uint8_t argb[8];
703            uint32_t ch = *linechar++;
704
705            _cucul_attr_to_argb4(*lineattr++, argb);
706
707            cur += sprintf(cur, "newpath\n");
708            cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
709            cur += sprintf(cur, "%f %f %f setrgbcolor\n",
710                           (float)argb[5] * (1.0 / 0xf),
711                           (float)argb[6] * (1.0 / 0xf),
712                           (float)argb[7] * (1.0 / 0xf));
713
714            if(ch < 0x00000020)
715                cur += sprintf(cur, "(?) show\n");
716            else if(ch >= 0x00000080)
717                cur += sprintf(cur, "(?) show\n");
718            else switch((uint8_t)(ch & 0x7f))
719            {
720                case '\\':
721                case '(':
722                case ')':
723                    cur += sprintf(cur, "(\\%c) show\n", (unsigned char)ch);
724                    break;
725                default:
726                    cur += sprintf(cur, "(%c) show\n", (unsigned char)ch);
727                    break;
728            }
729        }
730    }
731
732    cur += sprintf(cur, "showpage\n");
733
734    /* Crop to really used size */
735    ex->size = (uintptr_t)(cur - ex->data);
736    ex->data = realloc(ex->data, ex->size);
737
738    return 0;
739}
740
741/* Export an SVG vector image */
742static int export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
743{
744    static char const svg_header[] =
745        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
746        "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
747        " xmlns=\"http://www.w3.org/2000/svg\""
748        " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
749        " xml:space=\"preserve\" version=\"1.1\"  baseProfile=\"full\">\n";
750
751    char *cur;
752    unsigned int x, y;
753
754    /* 200 is arbitrary but should be ok */
755    ex->size = strlen(svg_header) + 128 + cv->width * cv->height * 200;
756    ex->data = malloc(ex->size);
757
758    cur = ex->data;
759
760    /* Header */
761    cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
762                                    cv->width * 6, cv->height * 10);
763
764    cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
765                        " style=\"font-family: monospace\">\n");
766
767    /* Background */
768    for(y = 0; y < cv->height; y++)
769    {
770        uint32_t *lineattr = cv->attrs + y * cv->width;
771
772        for(x = 0; x < cv->width; x++)
773        {
774            cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
775                                " width=\"6\" height=\"10\"/>\n",
776                                _cucul_attr_to_rgb12bg(*lineattr++),
777                                x * 6, y * 10);
778        }
779    }
780
781    /* Text */
782    for(y = 0; y < cv->height; y++)
783    {
784        uint32_t *lineattr = cv->attrs + y * cv->width;
785        uint32_t *linechar = cv->chars + y * cv->width;
786
787        for(x = 0; x < cv->width; x++)
788        {
789            uint32_t ch = *linechar++;
790
791            if(ch == ' ' || ch == CUCUL_MAGIC_FULLWIDTH)
792            {
793                lineattr++;
794                continue;
795            }
796
797            cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
798                                "x=\"%d\" y=\"%d\">",
799                                _cucul_attr_to_rgb12fg(*lineattr++),
800                                x * 6, (y * 10) + 8);
801
802            if(ch < 0x00000020)
803                *cur++ = '?';
804            else if(ch > 0x0000007f)
805                cur += cucul_utf32_to_utf8(cur, ch);
806            else switch((uint8_t)ch)
807            {
808                case '>': cur += sprintf(cur, "&gt;"); break;
809                case '<': cur += sprintf(cur, "&lt;"); break;
810                case '&': cur += sprintf(cur, "&amp;"); break;
811                default: *cur++ = ch; break;
812            }
813            cur += sprintf(cur, "</text>\n");
814        }
815    }
816
817    cur += sprintf(cur, " </g>\n");
818    cur += sprintf(cur, "</svg>\n");
819
820    /* Crop to really used size */
821    ex->size = (uintptr_t)(cur - ex->data);
822    ex->data = realloc(ex->data, ex->size);
823
824    return 0;
825}
826
827/* Export a TGA image */
828static int export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
829{
830    char const * const *fontlist;
831    char * cur;
832    cucul_font_t *f;
833    unsigned int i, w, h;
834
835    fontlist = cucul_get_font_list();
836    if(!fontlist[0])
837        return -1;
838
839    f = cucul_load_font(fontlist[0], 0);
840
841    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
842    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
843
844    ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
845    ex->data = malloc(ex->size);
846
847    cur = ex->data;
848
849    /* ID Length */
850    cur += sprintf(cur, "%c", 0);
851    /* Color Map Type: no colormap */
852    cur += sprintf(cur, "%c", 0);
853    /* Image Type: uncompressed truecolor */
854    cur += sprintf(cur, "%c", 2);
855    /* Color Map Specification: no color map */
856    memset(cur, 0, 5); cur += 5;
857
858    /* Image Specification */
859    cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
860    cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
861    cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
862    cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
863    cur += sprintf(cur, "%c", 32); /* Pixel Depth */
864    cur += sprintf(cur, "%c", 40); /* Image Descriptor */
865
866    /* Image ID: no ID */
867    /* Color Map Data: no colormap */
868
869    /* Image Data */
870    cucul_render_canvas(cv, f, cur, w, h, 4 * w);
871
872    /* Swap bytes. What a waste of time. */
873    for(i = 0; i < w * h * 4; i += 4)
874    {
875        char w;
876        w = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = w;
877        w = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = w;
878    }
879
880    cucul_free_font(f);
881
882    return 0;
883}
884
Note: See TracBrowser for help on using the repository browser.