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