source: libcaca/trunk/cucul/export_html.c @ 757

Last change on this file since 757 was 757, checked in by Sam Hocevar, 15 years ago
  • Renamed cucul_export into cucul_buffer. We'll use it for other things.
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file export_html.c
13 *  \version \$Id: export_html.c 757 2006-04-13 12:50:41Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \author Jean-Yves Lamoureux <jylam@lnxscene.org>
16 *  \brief Export function
17 *
18 *  This file contains export functions for HTML and HTML3
19 */
20
21#include "config.h"
22
23#if !defined(__KERNEL__)
24#   include <stdlib.h>
25#   include <stdio.h>
26#   include <string.h>
27#endif
28
29#include "cucul.h"
30#include "cucul_internals.h"
31
32/** \brief Generate HTML representation of current image.
33 *
34 *  This function generates and returns the HTML representation of
35 *  the current image.
36 */
37void _cucul_get_html(cucul_t *qq, struct cucul_buffer *ex)
38{
39    static int const palette[] =
40    {
41        0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888,
42        0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff,
43    };
44    char *cur;
45    unsigned int x, y, len;
46
47    /* The CSS palette: roughly 13000 bytes
48     * A line: 7 chars for "<br />\n"
49     * A glyph: 18 chars for "<span class='bxx'>"
50     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
51     *          7 chars for "</span>" */
52    ex->size = 13000 + qq->height * (7 + qq->width * (18 + 9 + 7));
53    ex->data = malloc(ex->size);
54
55    cur = ex->data;
56
57    /* HTML header */
58    cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
59
60    /* CSS */
61    cur += sprintf(cur, "<style>\n");
62    cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
63    for(x = 0; x < 0x100; x++)
64    {
65        cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
66                       x, palette[x & 0xf ], palette[x >> 4]);
67    }
68    cur += sprintf(cur, "</style>\n</head>\n<body>\n");
69
70    cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
71                        "font-family: monospace, fixed; font-weight: bold;");
72
73    for(y = 0; y < qq->height; y++)
74    {
75        uint32_t *lineattr = qq->attr + y * qq->width;
76        uint32_t *linechar = qq->chars + y * qq->width;
77
78        for(x = 0; x < qq->width; x += len)
79        {
80            cur += sprintf(cur, "<span class='b%02x'>",
81                           _cucul_argb32_to_ansi8(lineattr[x]));
82
83            for(len = 0;
84                x + len < qq->width && lineattr[x + len] == lineattr[x];
85                len++)
86            {
87                if(linechar[x + len] <= 0x00000020)
88                    cur += sprintf(cur, "&nbsp;");
89                else if(linechar[x + len] < 0x00000080)
90                    cur += sprintf(cur, "%c", linechar[x + len]);
91                else
92                    cur += sprintf(cur, "&#%i;", linechar[x + len]);
93            }
94            cur += sprintf(cur, "</span>");
95        }
96        /* New line */
97        cur += sprintf(cur, "<br />\n");
98    }
99
100    cur += sprintf(cur, "</div></body></html>\n");
101
102    /* Crop to really used size */
103    ex->size = strlen(ex->data) + 1;
104    ex->data = realloc(ex->data, ex->size);
105}
106
107
108/** \brief Generate HTML3 representation of current image.
109 *
110 *  This function generates and returns the HTML3 representation of
111 *  the current image. It is way bigger than cucul_get_html(), but
112 *  permits viewing in old browsers (or limited ones such as links)
113 *  Won't work under gecko (mozilla rendering engine) unless you set
114 *  a correct header.
115 */
116void _cucul_get_html3(cucul_t *qq, struct cucul_buffer *ex)
117{
118    static int const palette[] =
119    {
120        0x000000, 0x000088, 0x008800, 0x008888,
121        0x880000, 0x880088, 0x888800, 0x888888,
122        0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
123        0xff4444, 0xff44ff, 0xffff44, 0xffffff,
124    };
125
126    char *cur;
127    unsigned int x, y, len;
128
129    /* The CSS palette: roughly 13000 bytes
130     * A line: 10 chars for "<tr></tr>\n"
131     * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
132     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
133     *          12 chars for "</font></td>" */
134    ex->size = 13000 + qq->height * (10 + qq->width * (40 + 9 + 12));
135    ex->data = malloc(ex->size);
136
137    cur = ex->data;
138
139    /* Table */
140    cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
141                        qq->height);
142
143    for(y = 0; y < qq->height; y++)
144    {
145        uint32_t *lineattr = qq->attr + y * qq->width;
146        uint32_t *linechar = qq->chars + y * qq->width;
147
148        cur += sprintf(cur, "<tr>");
149
150        for(x = 0; x < qq->width; x += len)
151        {
152            unsigned int i;
153
154            /* Use colspan option to factorize cells with same attributes
155             * (see below) */
156            len = 1;
157            while(x + len < qq->width && lineattr[x + len] == lineattr[x])
158                len++;
159
160            cur += sprintf(cur, "<td bgcolor=#%06x",
161                           palette[_cucul_argb32_to_ansi4bg(lineattr[x])]);
162
163            if(len > 1)
164                cur += sprintf(cur, " colspan=%d", len);
165
166            cur += sprintf(cur, "><font color=#%06x>",
167                           palette[_cucul_argb32_to_ansi4fg(lineattr[x])]);
168
169            for(i = 0; i < len; i++)
170            {
171                if(linechar[x + i] <= 0x00000020)
172                    cur += sprintf(cur, "&nbsp;");
173                else if(linechar[x + i] < 0x00000080)
174                    cur += sprintf(cur, "%c", linechar[x + i]);
175                else
176                    cur += sprintf(cur, "&#%i;", linechar[x + i]);
177            }
178
179            cur += sprintf(cur, "</font></td>");
180        }
181        cur += sprintf(cur, "</tr>\n");
182    }
183
184    /* Footer */
185    cur += sprintf(cur, "</table>\n");
186
187    /* Crop to really used size */
188    ex->size = (uintptr_t)(cur - ex->data);
189    ex->data = realloc(ex->data, ex->size);
190}
191
192
Note: See TracBrowser for help on using the repository browser.