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

Last change on this file since 769 was 769, checked in by Sam Hocevar, 14 years ago
  • Removed \file directives from all files except caca.h and cucul.h, to remove redundencies in the Doxygen documentation.
  • Property svn:keywords set to Id
File size: 5.9 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_html.c 769 2006-04-14 07:30:53Z 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 export functions for HTML and HTML3
17 */
18
19#include "config.h"
20
21#if !defined(__KERNEL__)
22#   include <stdlib.h>
23#   include <stdio.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30/** \brief Generate HTML representation of current image.
31 *
32 *  This function generates and returns the HTML representation of
33 *  the current image.
34 */
35void _cucul_get_html(cucul_t *qq, struct cucul_buffer *ex)
36{
37    static int const palette[] =
38    {
39        0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888,
40        0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff,
41    };
42    char *cur;
43    unsigned int x, y, len;
44
45    /* The CSS palette: roughly 13000 bytes
46     * A line: 7 chars for "<br />\n"
47     * A glyph: 18 chars for "<span class='bxx'>"
48     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
49     *          7 chars for "</span>" */
50    ex->size = 13000 + qq->height * (7 + qq->width * (18 + 9 + 7));
51    ex->data = malloc(ex->size);
52
53    cur = ex->data;
54
55    /* HTML header */
56    cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
57
58    /* CSS */
59    cur += sprintf(cur, "<style>\n");
60    cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
61    for(x = 0; x < 0x100; x++)
62    {
63        cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
64                       x, palette[x & 0xf ], palette[x >> 4]);
65    }
66    cur += sprintf(cur, "</style>\n</head>\n<body>\n");
67
68    cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
69                        "font-family: monospace, fixed; font-weight: bold;");
70
71    for(y = 0; y < qq->height; y++)
72    {
73        uint32_t *lineattr = qq->attr + y * qq->width;
74        uint32_t *linechar = qq->chars + y * qq->width;
75
76        for(x = 0; x < qq->width; x += len)
77        {
78            cur += sprintf(cur, "<span class='b%02x'>",
79                           _cucul_argb32_to_ansi8(lineattr[x]));
80
81            for(len = 0;
82                x + len < qq->width && lineattr[x + len] == lineattr[x];
83                len++)
84            {
85                if(linechar[x + len] <= 0x00000020)
86                    cur += sprintf(cur, "&nbsp;");
87                else if(linechar[x + len] < 0x00000080)
88                    cur += sprintf(cur, "%c", linechar[x + len]);
89                else
90                    cur += sprintf(cur, "&#%i;", linechar[x + len]);
91            }
92            cur += sprintf(cur, "</span>");
93        }
94        /* New line */
95        cur += sprintf(cur, "<br />\n");
96    }
97
98    cur += sprintf(cur, "</div></body></html>\n");
99
100    /* Crop to really used size */
101    ex->size = strlen(ex->data) + 1;
102    ex->data = realloc(ex->data, ex->size);
103}
104
105
106/** \brief Generate HTML3 representation of current image.
107 *
108 *  This function generates and returns the HTML3 representation of
109 *  the current image. It is way bigger than cucul_get_html(), but
110 *  permits viewing in old browsers (or limited ones such as links)
111 *  Won't work under gecko (mozilla rendering engine) unless you set
112 *  a correct header.
113 */
114void _cucul_get_html3(cucul_t *qq, struct cucul_buffer *ex)
115{
116    static int const palette[] =
117    {
118        0x000000, 0x000088, 0x008800, 0x008888,
119        0x880000, 0x880088, 0x888800, 0x888888,
120        0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
121        0xff4444, 0xff44ff, 0xffff44, 0xffffff,
122    };
123
124    char *cur;
125    unsigned int x, y, len;
126
127    /* The CSS palette: roughly 13000 bytes
128     * A line: 10 chars for "<tr></tr>\n"
129     * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
130     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
131     *          12 chars for "</font></td>" */
132    ex->size = 13000 + qq->height * (10 + qq->width * (40 + 9 + 12));
133    ex->data = malloc(ex->size);
134
135    cur = ex->data;
136
137    /* Table */
138    cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
139                        qq->height);
140
141    for(y = 0; y < qq->height; y++)
142    {
143        uint32_t *lineattr = qq->attr + y * qq->width;
144        uint32_t *linechar = qq->chars + y * qq->width;
145
146        cur += sprintf(cur, "<tr>");
147
148        for(x = 0; x < qq->width; x += len)
149        {
150            unsigned int i;
151
152            /* Use colspan option to factorize cells with same attributes
153             * (see below) */
154            len = 1;
155            while(x + len < qq->width && lineattr[x + len] == lineattr[x])
156                len++;
157
158            cur += sprintf(cur, "<td bgcolor=#%06x",
159                           palette[_cucul_argb32_to_ansi4bg(lineattr[x])]);
160
161            if(len > 1)
162                cur += sprintf(cur, " colspan=%d", len);
163
164            cur += sprintf(cur, "><font color=#%06x>",
165                           palette[_cucul_argb32_to_ansi4fg(lineattr[x])]);
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 = (uintptr_t)(cur - ex->data);
187    ex->data = realloc(ex->data, ex->size);
188}
189
190
Note: See TracBrowser for help on using the repository browser.