/[public]/psiconv/trunk/program/psiconv/gen_html.c
ViewVC logotype

Contents of /psiconv/trunk/program/psiconv/gen_html.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (show annotations)
Mon Oct 11 19:17:17 1999 UTC (24 years, 5 months ago) by frodo
File MIME type: text/plain
File size: 12760 byte(s)
(Frodo) Moved generation stuff out of the library into psiconv

1 /*
2 gen_html.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "config.h"
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "data.h"
25 #include "list.h"
26 #include "gen.h"
27
28 /* This determines for each character how it is displayed */
29 static const char *char_table[0x100] =
30 {
31 /* 0x00 */ "" ,"" ,"" ,"" ,"" ,"" ,"<P> ","<BR>" ,
32 /* 0x08 */ "<P>" ," " ,"" ,"" ,"" ,"" ,"" ,"" ,
33 /* 0x10 */ " " ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
34 /* 0x18 */ "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
35 /* 0x20 */ " " ,"!" ,"&quot;","#" ,"$" ,"%" ,"&amp;","'" ,
36 /* 0x28 */ "(" ,")" ,"*" ,"+" ,"," ,"-" ,"." ,"/" ,
37 /* 0x30 */ "0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" ,
38 /* 0x38 */ "8" ,"9" ,":" ,";" ,"&lt;" ,"=" ,"&gt;" ,"?" ,
39 /* 0x40 */ "@" ,"A" ,"B" ,"C" ,"D" ,"E" ,"F" ,"G" ,
40 /* 0x48 */ "H" ,"I" ,"J" ,"K" ,"L" ,"M" ,"N" ,"O" ,
41 /* 0x50 */ "P" ,"Q" ,"R" ,"S" ,"T" ,"U" ,"V" ,"W" ,
42 /* 0x58 */ "X" ,"Y" ,"Z" ,"[" ,"\\" ,"]" ,"^" ,"_" ,
43 /* 0x60 */ "`" ,"a" ,"b" ,"c" ,"d" ,"e" ,"f" ,"g" ,
44 /* 0x68 */ "h" ,"i" ,"j" ,"k" ,"l" ,"m" ,"n" ,"o" ,
45 /* 0x70 */ "p" ,"q" ,"r" ,"s" ,"t" ,"u" ,"v" ,"w" ,
46 /* 0x78 */ "x" ,"y" ,"z" ,"{" ,"|" ,"}" ,"~" ,"" ,
47 /* 0x80 */ "" ,"","&sbquot;","&fnof;","&bdquo;","&hellip;",
48 "&dagger;","&Dagger;",
49 /* 0x88 */ "^","&permil;","&Scaron;","&lang;","&OElig;","" ,"" ,"" ,
50 /* 0x90 */ "","&lsquo;","&rsquo;","&ldquo;","&rdquo;",
51 "&middot;","&ndash","&mdash",
52 /* 0x98 */ "&tilde;","&trade;","&scaron;","&rang;","&oelig;","","","&Yuml;",
53 /* 0xa0 */ "","&iexcl;","&cent;","&pound;",
54 "&curren;","&yen;","&brvbar;","&sect",
55 /* 0xa8 */ "&quot;","&copy;","a","&laquo;","&not;","-","&reg;","&macron;",
56 /* 0xb0 */ "&deg;","&plusmn;","&sup2;","&sup3;",
57 "&rsquot;","&micron;","&para;","&middot;",
58 /* 0xb8 */ "&comma;","&sup1","&deg;","&raquo;",
59 "&frac14;","&frac12;","&frac34;","&iquest;",
60 /* 0xc0 */ "&Agrave;","&Aacute;","&Acirc;","&Atilde;",
61 "&Auml;","&Aring;","&AElig;","&Ccedil;",
62 /* 0xc8 */ "&Egrave;","&Eacute;","&Ecirc;","&Euml;",
63 "&Igrave;","&Iacute;","&Icirc;","&Iuml;",
64 /* 0xd0 */ "&ETH;","&Ntilde;","&Ograve;","&Oacute;",
65 "&Ocirc;","&Otilde;","&Ouml;","&times;",
66 /* 0xd8 */ "&Oslash;","&Ugrave;","&Uacute;","&Ucirc;",
67 "&Uuml;","&Yacute;","&THORN;","&szlig;",
68 /* 0xe0 */ "&agrave;","&aacute;","&acirc;","&atilde;",
69 "&auml;","&aring;","&aelig;","&ccedil;",
70 /* 0xe8 */ "&egrave;","&eacute;","&ecirc;","&euml;",
71 "&igrave;","&iacute;","&icirc;","&iuml;",
72 /* 0xf0 */ "&eth;","&ntilde;","&ograve;","&oacute;",
73 "&ocirc;","&otilde;","&ouml;","&divide;",
74 /* 0xf8 */ "&oslash;","&ugrave;","&uacute;","&ucirc;",
75 "&uuml;","&yacute;","&thorn;","&yuml;"
76 };
77
78 static psiconv_character_layout gen_base_char(const psiconv_font font,
79 const psiconv_color color,
80 const psiconv_color back_color);
81 static void diff_char(FILE *of, const psiconv_character_layout old,
82 const psiconv_character_layout new, int *flags);
83 static void gen_para(FILE *of, const psiconv_paragraph para,
84 const psiconv_character_layout base_char);
85
86 static void psiconv_gen_html_word(FILE *of,psiconv_word_f wf);
87 static void psiconv_gen_html_texted(FILE *of,psiconv_texted_f tf);
88
89 /* This is not necessarily the same as returned by basic_character_layout_status
90 This one is specific for the base point of HTML */
91 psiconv_character_layout gen_base_char(const psiconv_font font,
92 const psiconv_color color,
93 const psiconv_color back_color)
94 {
95 struct psiconv_character_layout base_char_struct =
96 {
97 color, /* color */
98 back_color, /* back_color */
99 13.0, /* font_size */
100 psiconv_bool_false, /* italic */
101 psiconv_bool_false, /* bold */
102 psiconv_normalscript, /* super_sub */
103 psiconv_bool_false, /* underline */
104 psiconv_bool_false, /* strike_out */
105 font, /* font */
106 };
107 return psiconv_clone_character_layout(&base_char_struct);
108 }
109
110 /* flags & 1: 0 if no <FONT> was yet generated.
111 flags & 2: 1 if at end-of-paragraph
112 */
113 void diff_char(FILE *of, const psiconv_character_layout old,
114 const psiconv_character_layout new,
115 int *flags)
116 {
117 int font_set = 0;
118
119 if ((old->font_size != new->font_size) ||
120 (old->color->red != new->color->red) ||
121 (old->color->green != new->color->green) ||
122 (old->color->blue != new->color->blue) ||
123 (strcmp(old->font->name,new->font->name)) ||
124 (old->font->screenfont != new->font->screenfont) ||
125 ((*flags & 0x03) == 3)) {
126 if (old->italic)
127 fputs("</I>",of);
128 if (old->bold)
129 fputs("</B>",of);
130 if (old->underline)
131 fputs("</U>",of);
132 if (old->strike_out)
133 fputs("</STRIKE>",of);
134 if (old->super_sub == psiconv_superscript)
135 fputs("</SUP>",of);
136 if (old->super_sub == psiconv_subscript)
137 fputs("</SUB>",of);
138 if ((*flags & 1) == 1)
139 fputs("</FONT>",of);
140 if ((*flags & 2) == 0) {
141 *flags |= 1;
142 fputs("<FONT SIZE=",of);
143 if (new->font_size <= 8.0)
144 fputs("1",of);
145 else if (new->font_size <= 10.0)
146 fputs("2",of);
147 else if (new->font_size <= 12.0)
148 fputs("3",of);
149 else if (new->font_size <= 14.0)
150 fputs("4",of);
151 else if (new->font_size <= 18.0)
152 fputs("5",of);
153 else if (new->font_size <= 24.0)
154 fputs("6",of);
155 else
156 fputs("7",of);
157 fprintf(of," COLOR=#%02x%02x%02x",new->color->red,new->color->green,
158 new->color->blue);
159 if (new->font->screenfont == 1)
160 fprintf(of," FACE=\"%s, Sans-Serif\">",new->font->name);
161 else if (new->font->screenfont == 2)
162 fprintf(of," FACE=\"%s, Monospace\">",new->font-> name);
163 else if (new->font->screenfont == 3)
164 fprintf(of," FACE=\"%s, Serif\">",new->font-> name);
165 else
166 fprintf(of," FACE=\"%s, Serif\">",new->font-> name);
167 }
168 if (new->italic)
169 fputs("<I>",of);
170 if (new->bold)
171 fputs("<B>",of);
172 if (new->underline)
173 fputs("<U>",of);
174 if (new->strike_out)
175 fputs("<STRIKE>",of);
176 if (new->super_sub == psiconv_superscript)
177 fputs("<SUP>",of);
178 if (new->super_sub == psiconv_subscript)
179 fputs("<SUB>",of);
180 } else {
181 if (font_set || (old->italic != new->italic)) {
182 if (old->italic)
183 fputs("</I>",of);
184 else
185 fputs("<I>",of);
186 }
187 if (old->bold != new->bold) {
188 if (old->bold)
189 fputs("</B>",of);
190 else
191 fputs("<B>",of);
192 }
193 if (old->underline != new->underline) {
194 if (old->underline)
195 fputs("</U>",of);
196 else
197 fputs("<U>",of);
198 }
199 if (old->strike_out != new->strike_out) {
200 if (old->strike_out)
201 fputs("</STRIKE>",of);
202 else
203 fputs("<STRIKE>",of);
204 }
205 if (old->super_sub != new->super_sub) {
206 if (old->super_sub == psiconv_superscript)
207 fputs("</SUP>",of);
208 else if (old->super_sub == psiconv_subscript)
209 fputs("</SUB>",of);
210 if (new->super_sub == psiconv_superscript)
211 fputs("<SUP>",of);
212 else if (new->super_sub == psiconv_subscript)
213 fputs("<SUB>",of);
214 }
215 }
216 }
217
218 void gen_para(FILE *of, const psiconv_paragraph para,
219 const psiconv_character_layout base_char)
220 {
221 int i,j,loc;
222 psiconv_character_layout cur_char;
223 psiconv_in_line_layout inl;
224 int flags = 0;
225
226
227 fputs("<P",of);
228 if (para->base_paragraph->justify_hor == psiconv_justify_left)
229 fputs(" ALIGN=left",of);
230 else if (para->base_paragraph->justify_hor == psiconv_justify_right)
231 fputs(" ALIGN=right",of);
232 else if (para->base_paragraph->justify_hor == psiconv_justify_centre)
233 fputs(" ALIGN=center",of);
234 else if (para->base_paragraph->justify_hor == psiconv_justify_full)
235 fputs(" ALIGN=left",of);
236 fputs(">",of);
237 if (para->base_paragraph->bullet->on)
238 fputs("<UL><LI>",of);
239
240 cur_char = base_char;
241
242 if (psiconv_list_length(para->in_lines) == 0) {
243 diff_char(of,cur_char,para->base_character,&flags);
244 cur_char = para->base_character;
245 }
246 loc = 0;
247
248 for (i = 0; i < psiconv_list_length(para->in_lines); i++) {
249 inl = psiconv_list_get(para->in_lines,i);
250 diff_char(of,cur_char,inl->layout,&flags);
251 cur_char = inl->layout;
252 for (j = loc; j < inl->length + loc; j ++) {
253 fputs(char_table[(unsigned char) (para->text[j])],of);
254 }
255 loc = j;
256 }
257
258 if (loc < strlen(para->text)) {
259 diff_char(of,cur_char,para->base_character,&flags);
260 cur_char = para->base_character;
261 for (j = loc; j < strlen(para->text); j ++) {
262 fputs(char_table[(unsigned char) (para->text[j])],of);
263 }
264 }
265
266 if (strlen(para->text) == 0)
267 fputs("<BR>",of);
268
269 flags |= 2;
270 diff_char(of,cur_char,base_char,&flags);
271
272 if (para->base_paragraph->bullet->on)
273 fputs("</UL>",of);
274
275 fputs("</P>\n",of);
276 }
277
278 void psiconv_gen_html(FILE *of,psiconv_file file)
279 {
280 if (file->type == psiconv_word_file)
281 psiconv_gen_html_word(of,(psiconv_word_f) file->file);
282 else if (file->type == psiconv_texted_file)
283 psiconv_gen_html_texted(of,(psiconv_texted_f) file->file);
284 else
285 return;
286 }
287
288 void psiconv_gen_html_texted(FILE *of,psiconv_texted_f tf)
289 {
290 psiconv_character_layout base_char;
291 psiconv_paragraph para;
292 int i;
293
294 /* We have nothing better */
295 base_char = psiconv_basic_character_layout();
296
297 fputs("<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">", of);
298 fputs("\n<HTML>\n<HEAD>\n <META NAME=\"GENERATOR\"", of);
299 fputs(" CONTENT=\"psiconv-" VERSION "\">\n", of);
300 fputs("<BODY>\n",of);
301 for (i = 0; i < psiconv_list_length(tf->texted_sec->paragraphs); i++) {
302 para = psiconv_list_get(tf->texted_sec->paragraphs,i);
303 gen_para(of,para,base_char);
304 }
305 fputs("</BODY>\n</HTML>\n",of);
306 psiconv_free_character_layout(base_char);
307 }
308
309
310 void psiconv_gen_html_word(FILE *of,psiconv_word_f wf)
311 {
312 int i;
313 psiconv_paragraph para;
314 psiconv_color white,black;
315 psiconv_character_layout base_char;
316
317 white = malloc(sizeof(*white));
318 black = malloc(sizeof(*black));
319 white->red = 0x00;
320 white->green = 0x00;
321 white->blue = 0x00;
322 black->red = 0xff;
323 black->green = 0xff;
324 black->blue = 0xff;
325
326 /* To keep from generating a font desc for each line */
327 base_char = gen_base_char(wf->styles_sec->normal->character->font,
328 black,white);
329
330 psiconv_free_color(black);
331 psiconv_free_color(white);
332
333 fputs("<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">", of);
334 fputs("\n<HTML>\n<HEAD>\n <META NAME=\"GENERATOR\"", of);
335 fputs(" CONTENT=\"psiconv-" VERSION "\">\n", of);
336 fputs("<BODY>\n",of);
337
338 for (i = 0; i < psiconv_list_length(wf->paragraphs); i++) {
339 para = psiconv_list_get(wf->paragraphs,i);
340 gen_para(of,para,base_char);
341 }
342 fputs("</BODY>\n</HTML>\n",of);
343 psiconv_free_character_layout(base_char);
344 }
345
346

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26