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

Last change on this file since 644 was 644, checked in by Sam Hocevar, 14 years ago
  • Renamed cucul_buffer to cucul_export. Vanilla rum rules.
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/*
2 *  libcucul      Unicode canvas library
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.c
13 *  \version \$Id: export_html.c 644 2006-03-18 00:06:24Z 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_export *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->buffer = malloc(ex->size);
54
55    cur = ex->buffer;
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        uint8_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'>", lineattr[x]);
81
82            for(len = 0;
83                x + len < qq->width && lineattr[x + len] == lineattr[x];
84                len++)
85            {
86                if(linechar[x + len] <= 0x00000020)
87                    cur += sprintf(cur, "&nbsp;");
88                else if(linechar[x + len] < 0x00000080)
89                    cur += sprintf(cur, "%c", linechar[x + len]);
90                else
91                    cur += sprintf(cur, "&#%i;", linechar[x + len]);
92            }
93            cur += sprintf(cur, "</span>");
94        }
95        /* New line */
96        cur += sprintf(cur, "<br />\n");
97    }
98
99    cur += sprintf(cur, "</div></body></html>\n");
100
101    /* Crop to really used size */
102    ex->size = strlen(ex->buffer) + 1;
103    ex->buffer = realloc(ex->buffer, ex->size);
104}
105
106
107/** \brief Generate HTML3 representation of current image.
108 *
109 *  This function generates and returns the HTML3 representation of
110 *  the current image. It is way bigger than cucul_get_html(), but
111 *  permits viewing in old browsers (or limited ones such as links)
112 *  Won't work under gecko (mozilla rendering engine) unless you set
113 *  a correct header.
114 */
115void _cucul_get_html3(cucul_t *qq, struct cucul_export *ex)
116{
117    static int const palette[] =
118    {
119        0x000000, 0x000088, 0x008800, 0x008888,
120        0x880000, 0x880088, 0x888800, 0x888888,
121        0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
122        0xff4444, 0xff44ff, 0xffff44, 0xffffff,
123    };
124
125    char *cur;
126    unsigned int x, y, len;
127
128    /* The CSS palette: roughly 13000 bytes
129     * A line: 10 chars for "<tr></tr>\n"
130     * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
131     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
132     *          12 chars for "</font></td>" */
133    ex->size = 13000 + qq->height * (10 + qq->width * (40 + 9 + 12));
134    ex->buffer = malloc(ex->size);
135
136    cur = ex->buffer;
137
138    /* Table */
139    cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
140                        qq->height);
141
142    for(y = 0; y < qq->height; y++)
143    {
144        uint8_t *lineattr = qq->attr + y * qq->width;
145        uint32_t *linechar = qq->chars + y * qq->width;
146
147        cur += sprintf(cur, "<tr>");
148
149        for(x = 0; x < qq->width; x += len)
150        {
151            unsigned int i;
152
153            /* Use colspan option to factorize cells with same attributes
154             * (see below) */
155            len = 1;
156            while(x + len < qq->width && lineattr[x + len] == lineattr[x])
157                len++;
158
159            cur += sprintf(cur, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]);
160
161            if(len > 1)
162                cur += sprintf(cur, " colspan=%d", len);
163
164            cur += sprintf(cur, "><font color=#%06x>",
165                                palette[lineattr[x] & 0x0f]);
166
167            for(i = 0; i < len; i++)
168            {
169                if(linechar[x + i] <= 0x00000020)
170                    cur += sprintf(cur, "&nbsp;");
171                else if(linechar[x + i] < 0x00000080)
172                    cur += sprintf(cur, "%c", linechar[x + i]);
173                else
174                    cur += sprintf(cur, "&#%i;", linechar[x + i]);
175            }
176
177            cur += sprintf(cur, "</font></td>");
178        }
179        cur += sprintf(cur, "</tr>\n");
180    }
181
182    /* Footer */
183    cur += sprintf(cur, "</table>\n");
184
185    /* Crop to really used size */
186    ex->size = strlen(ex->buffer) + 1;
187    ex->buffer = realloc(ex->buffer, ex->size);
188}
189
190
Note: See TracBrowser for help on using the repository browser.