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