chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / html.c
1 /*
2  * html.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.9  2008/02/27 09:42:22  james
14  * *** empty log message ***
15  *
16  * Revision 1.8  2008/02/27 01:52:38  james
17  * *** empty log message ***
18  *
19  * Revision 1.7  2008/02/27 01:52:08  james
20  * *** empty log message ***
21  *
22  * Revision 1.6  2008/02/20 23:42:05  staffcvs
23  * *** empty log message ***
24  *
25  * Revision 1.5  2008/02/20 23:31:48  staffcvs
26  * *** empty log message ***
27  *
28  * Revision 1.4  2008/02/20 22:54:22  staffcvs
29  * *** empty log message ***
30  *
31  * Revision 1.3  2008/02/20 20:16:07  james
32  * *** empty log message ***
33  *
34  * Revision 1.2  2008/02/04 02:05:06  james
35  * *** empty log message ***
36  *
37  * Revision 1.1  2008/02/03 23:31:25  james
38  * *** empty log message ***
39  *
40  */
41
42 #include "project.h"
43
44
45 #define V(i) (((i)==0)?0x80:(((i)==1)?0xc0:0xff))
46 #define COLOR(r,g,b,i) ((((r)?(V(i)):0) << 0)| (((g)?(V(i)):0) << 8)| (((b)?(V(i)):0) << 16))
47
48 static int colormap[] = {
49   [CRT_COLOR_BLACK] = COLOR (0, 0, 0, 0),
50   [CRT_COLOR_RED] = COLOR (0, 0, 1, 0),
51   [CRT_COLOR_GREEN] = COLOR (0, 1, 0, 0),
52   [CRT_COLOR_YELLOW] = COLOR (0, 1, 1, 0),
53   [CRT_COLOR_BLUE] = COLOR (1, 0, 0, 0),
54   [CRT_COLOR_MAGENTA] = COLOR (1, 0, 1, 0),
55   [CRT_COLOR_CYAN] = COLOR (1, 1, 0, 0),
56   [CRT_COLOR_WHITE] = COLOR (1, 1, 1, 1),
57   [CRT_COLOR_BLACK | CRT_COLOR_INTENSITY] = COLOR (1, 1, 1, 0),
58   [CRT_COLOR_RED | CRT_COLOR_INTENSITY] = COLOR (0, 0, 1, 2),
59   [CRT_COLOR_GREEN | CRT_COLOR_INTENSITY] = COLOR (0, 1, 0, 2),
60   [CRT_COLOR_YELLOW | CRT_COLOR_INTENSITY] = COLOR (0, 1, 1, 2),
61   [CRT_COLOR_BLUE | CRT_COLOR_INTENSITY] = COLOR (1, 0, 0, 2),
62   [CRT_COLOR_MAGENTA | CRT_COLOR_INTENSITY] = COLOR (1, 0, 1, 2),
63   [CRT_COLOR_CYAN | CRT_COLOR_INTENSITY] = COLOR (1, 1, 0, 2),
64   [CRT_COLOR_WHITE | CRT_COLOR_INTENSITY] = COLOR (1, 1, 1, 2),
65 };
66
67
68 static void
69 html_entity (FILE * f, int c)
70 {
71   switch (c)
72     {
73     case 32:
74       fprintf (f, "&nbsp;");
75       break;
76     case 38:
77       fprintf (f, "&amp;");
78       break;
79     case 60:
80       fprintf (f, "&lt;");
81       break;
82     case 62:
83       fprintf (f, "&gt;");
84       break;
85     default:
86
87       if ((c >= 32) && (c < 127))
88         {
89           fputc (c, f);
90         }
91       else if (c > 127)
92         {
93           fprintf (f, "&#x%04x;", c);
94         }
95       else
96         {
97           fputc (' ', f);
98         }
99     }
100 }
101
102 static void
103 html_render (FILE * f, CRT_CA c)
104 {
105   int fg, bg;
106
107   if (c.attr & CRT_ATTR_REVERSE)
108     {
109       fg = CRT_COLOR_BG (c.color);
110       bg = CRT_COLOR_FG (c.color);
111     }
112   else
113     {
114       fg = CRT_COLOR_FG (c.color);
115       bg = CRT_COLOR_BG (c.color);
116       if (c.attr & CRT_ATTR_BOLD)
117         fg |= CRT_COLOR_INTENSITY;
118     }
119 #ifdef CSS
120   fprintf (f, "<span style='color: #%06x; background-color: #%06x'>",
121            colormap[fg], colormap[bg]);
122 #else
123   fprintf (f, "<td bgcolor='#%06x'><font color='#%06x'>", colormap[bg],
124            colormap[fg]);
125   fprintf (f, "<tt>");
126 #endif
127
128   if (c.attr & CRT_ATTR_UNDERLINE)
129     fprintf (f, "<ul>");
130   if (c.attr & CRT_ATTR_BOLD)
131     fprintf (f, "<b>");
132
133   html_entity (f, c.chr);
134
135   if (c.attr & CRT_ATTR_BOLD)
136     fprintf (f, "</b>");
137   if (c.attr & CRT_ATTR_UNDERLINE)
138     fprintf (f, "</ul>");
139   if (c.attr & CRT_ATTR_REVERSE)
140     {
141       fprintf (f, "</font>");
142     }
143 #ifdef CSS
144   fprintf (f, "</span>");
145 #else
146   fprintf (f, "</tt>");
147   fprintf (f, "</td>");
148 #endif
149 }
150
151 static void
152 html_draw (FILE * f, CRT * c)
153 {
154   CRT_Pos p;
155   int o;
156
157 #ifdef CSS
158   fprintf (f, "<pre>");
159 #else
160   fprintf (f, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
161 #endif
162   for (p.y = 0; p.y < c->size.y; ++p.y)
163     {
164       o = CRT_ADDR (p.y, 0);
165 #ifndef CSS
166       fprintf (f, "<tr>");
167 #endif
168       for (p.x = 0; p.x < c->size.x; ++p.x, ++o)
169         {
170           html_render (f, c->screen[o]);
171         }
172 #ifdef CSS
173       fprintf (f, "\n");
174 #else
175       fprintf (f, "</tr>\n");
176 #endif
177     }
178 #ifdef CSS
179   fprintf (f, "</pre>\n");
180 #else
181   fprintf (f, "</table>");
182 #endif
183 }
184
185
186 static void
187 html_one_shot (ANSI * a, CRT * c)
188 {
189   html_draw (a->file, c);
190 }
191
192
193 static void
194 ansi_free (ANSI * a)
195 {
196   free (a);
197 }
198
199 ANSI *
200 ansi_new_html (FILE * f)
201 {
202   ANSI *ret;
203
204   ret = malloc (sizeof (ANSI));
205   memset (ret, 0, sizeof (ANSI));
206
207   ret->file = f;
208   ret->close = ansi_free;
209   ret->one_shot = html_one_shot;
210
211   return ret;
212 }