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

Last change on this file since 962 was 962, checked in by Sam Hocevar, 14 years ago
  • Export the UTF-8 / UTF-32 / CP437 character conversions to applications.
  • Property svn:keywords set to Id
File size: 23.6 KB
RevLine 
[804]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 962 2006-05-19 04:53:48Z 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"
[859]20#include "common.h"
[804]21
22#if !defined(__KERNEL__)
[920]23#   if defined(HAVE_ERRNO_H)
24#       include <errno.h>
25#   endif
[804]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
[818]34static void export_caca(cucul_canvas_t *, cucul_buffer_t *);
[810]35static void export_ansi(cucul_canvas_t *, cucul_buffer_t *);
[925]36static void export_utf8(cucul_canvas_t *, cucul_buffer_t *);
[810]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 *);
[804]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 *
[825]51 *  Valid values for \c format are:
[804]52 *
[825]53 *  \li \c "caca": export native libcaca files.
[818]54 *
[825]55 *  \li \c "ansi": export ANSI art (CP437 charset with ANSI colour codes).
[804]56 *
[825]57 *  \li \c "html": export an HTML page with CSS information.
[804]58 *
[825]59 *  \li \c "html3": export an HTML table that should be compatible with
[804]60 *      most navigators, including textmode ones.
61 *
[825]62 *  \li \c "irc": export UTF-8 text with mIRC colour codes.
[804]63 *
[825]64 *  \li \c "ps": export a PostScript document.
[804]65 *
[825]66 *  \li \c "svg": export an SVG vector image.
[804]67 *
[825]68 *  \li \c "tga": export a TGA image.
[804]69 *
[920]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 *
[811]74 *  \param cv A libcucul canvas
[804]75 *  \param format A string describing the requested output format.
[920]76 *  \return A libcucul buffer, or NULL in case of error.
[804]77 */
[826]78cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
[804]79{
80    cucul_buffer_t *ex;
81
82    ex = malloc(sizeof(cucul_buffer_t));
[920]83    if(!ex)
84    {
85#if defined(HAVE_ERRNO_H)
86        errno = ENOMEM;
87#endif
88        return NULL;
89    }
90
[804]91    ex->size = 0;
92    ex->data = NULL;
[896]93    ex->user_data = 0;
[804]94
[818]95    if(!strcasecmp("caca", format))
96        export_caca(cv, ex);
97    else if(!strcasecmp("ansi", format))
[811]98        export_ansi(cv, ex);
[925]99    else if(!strcasecmp("utf8", format))
100        export_utf8(cv, ex);
[804]101    else if(!strcasecmp("html", format))
[811]102        export_html(cv, ex);
[804]103    else if(!strcasecmp("html3", format))
[811]104        export_html3(cv, ex);
[804]105    else if(!strcasecmp("irc", format))
[811]106        export_irc(cv, ex);
[804]107    else if(!strcasecmp("ps", format))
[811]108        export_ps(cv, ex);
[804]109    else if(!strcasecmp("svg", format))
[811]110        export_svg(cv, ex);
[804]111    else if(!strcasecmp("tga", format))
[811]112        export_tga(cv, ex);
[804]113
114    if(ex->size == 0)
115    {
116        free(ex);
[920]117#if defined(HAVE_ERRNO_H)
118        errno = EINVAL;
119#endif
[804]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
[826]130 *  the export format, to be used with cucul_export_canvas(), and a string
[804]131 *  containing the natural language description for that export format.
132 *
[920]133 *  This function never fails.
134 *
[804]135 *  \return An array of strings.
136 */
137char const * const * cucul_get_export_list(void)
138{
139    static char const * const list[] =
140    {
[818]141        "caca", "native libcaca format",
[804]142        "ansi", "ANSI",
[925]143        "utf8", "UTF-8 with ANSI escape codes",
[804]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
[818]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
[925]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
[962]252            cur += cucul_utf32_to_utf8(cur, ch);
[925]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
[804]266/* Generate ANSI representation of current canvas. */
[811]267static void export_ansi(cucul_canvas_t *cv, cucul_buffer_t *ex)
[804]268{
[805]269    static uint8_t const palette[] =
[804]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
[926]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).
[804]283     * Add height*9 to that (zeroes color at the end and jump to next line) */
[926]284    ex->size = (cv->height * 9) + (cv->width * cv->height * 16);
[804]285    ex->data = malloc(ex->size);
286
287    cur = ex->data;
288
[811]289    for(y = 0; y < cv->height; y++)
[804]290    {
[811]291        uint32_t *lineattr = cv->attr + y * cv->width;
292        uint32_t *linechar = cv->chars + y * cv->width;
[804]293
[811]294        for(x = 0; x < cv->width; x++)
[804]295        {
296            uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
297            uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
[810]298            uint32_t ch = linechar[x];
[804]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
[926]308                        cur += sprintf(cur, "5;3%d;4%dm", fg, bg - 8);
[804]309                else
310                    if(bg < 8)
[926]311                        cur += sprintf(cur, "1;3%d;4%dm", fg - 8, bg);
[804]312                    else
[926]313                        cur += sprintf(cur, "5;1;3%d;4%dm", fg - 8, bg - 8);
[804]314            }
315
[962]316            *cur++ = cucul_utf32_to_cp437(ch);
[804]317
318            prevfg = fg;
319            prevbg = bg;
320        }
321
[952]322        if(cv->width == 80)
[926]323        {
[952]324            cur += sprintf(cur, "\033[s\n\033[u");
325        }
326        else
327        {
[926]328            cur += sprintf(cur, "\033[0m\r\n");
329            prevfg = -1;
330            prevbg = -1;
331        }
[804]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. */
[811]340static void export_html(cucul_canvas_t *cv, cucul_buffer_t *ex)
[804]341{
342    char *cur;
343    unsigned int x, y, len;
344
[808]345    /* The HTML header: less than 1000 bytes
[804]346     * A line: 7 chars for "<br />\n"
[808]347     * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
[804]348     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
349     *          7 chars for "</span>" */
[811]350    ex->size = 1000 + cv->height * (7 + cv->width * (47 + 9 + 7));
[804]351    ex->data = malloc(ex->size);
352
353    cur = ex->data;
354
355    /* HTML header */
[808]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");
[804]359
360    cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
361                        "font-family: monospace, fixed; font-weight: bold;");
362
[811]363    for(y = 0; y < cv->height; y++)
[804]364    {
[811]365        uint32_t *lineattr = cv->attr + y * cv->width;
366        uint32_t *linechar = cv->chars + y * cv->width;
[804]367
[811]368        for(x = 0; x < cv->width; x += len)
[804]369        {
[808]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]));
[804]374
375            for(len = 0;
[811]376                x + len < cv->width && lineattr[x + len] == lineattr[x];
[804]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. */
[811]403static void export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
[804]404{
405    char *cur;
406    unsigned int x, y, len;
407
[808]408    /* The HTML table markup: less than 1000 bytes
[804]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>" */
[811]413    ex->size = 1000 + cv->height * (10 + cv->width * (40 + 9 + 12));
[804]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",
[811]420                        cv->height);
[804]421
[811]422    for(y = 0; y < cv->height; y++)
[804]423    {
[811]424        uint32_t *lineattr = cv->attr + y * cv->width;
425        uint32_t *linechar = cv->chars + y * cv->width;
[804]426
427        cur += sprintf(cur, "<tr>");
428
[811]429        for(x = 0; x < cv->width; x += len)
[804]430        {
431            unsigned int i;
432
[808]433            /* Use colspan option to factor cells with same attributes
[804]434             * (see below) */
435            len = 1;
[811]436            while(x + len < cv->width && lineattr[x + len] == lineattr[x])
[804]437                len++;
438
[808]439            cur += sprintf(cur, "<td bgcolor=#%.06x",
440                           _cucul_argb32_to_rgb24bg(lineattr[x]));
[804]441
442            if(len > 1)
443                cur += sprintf(cur, " colspan=%d", len);
444
[808]445            cur += sprintf(cur, "><font color=#%.06x>",
446                           _cucul_argb32_to_rgb24fg(lineattr[x]));
[804]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 */
[811]472static void export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
[804]473{
[805]474    static uint8_t const palette[] =
[804]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
[811]486     * ch        1 byte
[804]487     * \r\n      2 bytes
488     * In real life, the average bytes per pixel value will be around 5.
489     */
490
[811]491    ex->size = 2 + (cv->width * cv->height * 11);
[804]492    ex->data = malloc(ex->size);
493
494    cur = ex->data;
495
[811]496    for(y = 0; y < cv->height; y++)
[804]497    {
[811]498        uint32_t *lineattr = cv->attr + y * cv->width;
499        uint32_t *linechar = cv->chars + y * cv->width;
[804]500
501        uint8_t prevfg = -1;
502        uint8_t prevbg = -1;
503
[811]504        for(x = 0; x < cv->width; x++)
[804]505        {
506            uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
507            uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
[810]508            uint32_t ch = linechar[x];
[804]509
[840]510#if 0
[804]511            if(bg == prevbg)
512            {
513                if(fg == prevfg)
514                    ; /* Same fg/bg, do nothing */
[810]515                else if(ch == (uint32_t)' ')
[804]516                    fg = prevfg; /* Hackety hack */
517                else
518                {
519                    cur += sprintf(cur, "\x03%d", fg);
[810]520                    if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
[804]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
[810]531                if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
[804]532                    cur += sprintf(cur, "\x02\x02");
533            }
[840]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
[810]542            *cur++ = ch & 0x7f;
[804]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. */
[811]556static void export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
[804]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 */
[811]587    ex->size = strlen(ps_header) + (cv->width * cv->height * 200);
[804]588    ex->data = malloc(ex->size);
589
590    cur = ex->data;
591
592    /* Header */
593    cur += sprintf(cur, "%s", ps_header);
[951]594    cur += sprintf(cur, "0 %d translate\n", cv->height);
[804]595
596    /* Background, drawn using csquare macro defined in header */
[811]597    for(y = cv->height; y--; )
[804]598    {
[811]599        uint32_t *lineattr = cv->attr + y * cv->width;
[804]600
[811]601        for(x = 0; x < cv->width; x++)
[804]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 */
[811]612        cur += sprintf(cur, "-%d 1 translate\n", cv->width);
[804]613    }
614
615    cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
[951]616    cur += sprintf(cur, "0 %d translate\n", cv->height*10);
[804]617
[811]618    for(y = cv->height; y--; )
[804]619    {
[811]620        uint32_t *lineattr = cv->attr + (cv->height - y - 1) * cv->width;
621        uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
[804]622
[811]623        for(x = 0; x < cv->width; x++)
[804]624        {
625            uint8_t argb[8];
[810]626            uint32_t ch = *linechar++;
[804]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
[810]637            if(ch < 0x00000020)
[804]638                cur += sprintf(cur, "(?) show\n");
[810]639            else if(ch >= 0x00000080)
[804]640                cur += sprintf(cur, "(?) show\n");
[810]641            else switch((uint8_t)(ch & 0x7f))
[804]642            {
643                case '\\':
644                case '(':
645                case ')':
[810]646                    cur += sprintf(cur, "(\\%c) show\n", ch);
[804]647                    break;
648                default:
[810]649                    cur += sprintf(cur, "(%c) show\n", ch);
[804]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 */
[811]663static void export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
[804]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 */
[811]676    ex->size = strlen(svg_header) + (cv->width * cv->height * 200);
[804]677    ex->data = malloc(ex->size);
678
679    cur = ex->data;
680
681    /* Header */
[811]682    cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
683                                    cv->width * 6, cv->height * 10);
[804]684
685    cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"12\">\n");
686
687    /* Background */
[811]688    for(y = 0; y < cv->height; y++)
[804]689    {
[811]690        uint32_t *lineattr = cv->attr + y * cv->width;
[804]691
[811]692        for(x = 0; x < cv->width; x++)
[804]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 */
[811]702    for(y = 0; y < cv->height; y++)
[804]703    {
[811]704        uint32_t *lineattr = cv->attr + y * cv->width;
705        uint32_t *linechar = cv->chars + y * cv->width;
[804]706
[811]707        for(x = 0; x < cv->width; x++)
[804]708        {
[810]709            uint32_t ch = *linechar++;
[804]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);
[810]715            if(ch < 0x00000020)
[928]716                *cur++ = '?';
[810]717            else if(ch > 0x0000007f)
[962]718                cur += cucul_utf32_to_utf8(cur, ch);
[810]719            else switch((uint8_t)ch)
[804]720            {
721                case '>': cur += sprintf(cur, "&gt;"); break;
722                case '<': cur += sprintf(cur, "&lt;"); break;
723                case '&': cur += sprintf(cur, "&amp;"); break;
[928]724                default: *cur++ = ch; break;
[804]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 */
[811]739static void export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
[804]740{
[805]741    char const * const * fontlist;
[804]742    char * cur;
743    cucul_font_t *f;
744    unsigned int i, w, h;
745
[805]746    fontlist = cucul_get_font_list();
747    if(!fontlist[0])
[804]748        return;
749
[805]750    f = cucul_load_font(fontlist[0], 0);
[804]751
[813]752    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
753    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
[804]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 */
[811]781    cucul_render_canvas(cv, f, cur, w, h, 4 * w);
[804]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.