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

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