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