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