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