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