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