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

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