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

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