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 "psiconv/data.h" |
25 |
#include "psiconv/list.h" |
26 |
#include "gen.h" |
27 |
#include "psiconv.h" |
28 |
|
29 |
#ifdef DMALLOC |
30 |
#include "dmalloc.h" |
31 |
#endif |
32 |
|
33 |
/* This determines for each character how it is displayed */ |
34 |
static const char *char_table[0x100] = |
35 |
{ |
36 |
/* 0x00 */ "" ,"" ,"" ,"" ,"" ,"" ,"<P> ","<BR>" , |
37 |
/* 0x08 */ "<P>" ," " ,"" ,"" ,"" ,"" ,"" ,"" , |
38 |
/* 0x10 */ " " ,"" ,"" ,"" ,"" ,"" ,"" ,"" , |
39 |
/* 0x18 */ "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , |
40 |
/* 0x20 */ " " ,"!" ,""","#" ,"$" ,"%" ,"&","'" , |
41 |
/* 0x28 */ "(" ,")" ,"*" ,"+" ,"," ,"-" ,"." ,"/" , |
42 |
/* 0x30 */ "0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" , |
43 |
/* 0x38 */ "8" ,"9" ,":" ,";" ,"<" ,"=" ,">" ,"?" , |
44 |
/* 0x40 */ "@" ,"A" ,"B" ,"C" ,"D" ,"E" ,"F" ,"G" , |
45 |
/* 0x48 */ "H" ,"I" ,"J" ,"K" ,"L" ,"M" ,"N" ,"O" , |
46 |
/* 0x50 */ "P" ,"Q" ,"R" ,"S" ,"T" ,"U" ,"V" ,"W" , |
47 |
/* 0x58 */ "X" ,"Y" ,"Z" ,"[" ,"\\" ,"]" ,"^" ,"_" , |
48 |
/* 0x60 */ "`" ,"a" ,"b" ,"c" ,"d" ,"e" ,"f" ,"g" , |
49 |
/* 0x68 */ "h" ,"i" ,"j" ,"k" ,"l" ,"m" ,"n" ,"o" , |
50 |
/* 0x70 */ "p" ,"q" ,"r" ,"s" ,"t" ,"u" ,"v" ,"w" , |
51 |
/* 0x78 */ "x" ,"y" ,"z" ,"{" ,"|" ,"}" ,"~" ,"" , |
52 |
/* 0x80 */ "" ,"","&sbquot;","ƒ","„","…", |
53 |
"†","‡", |
54 |
/* 0x88 */ "^","‰","Š","⟨","Œ","" ,"" ,"" , |
55 |
/* 0x90 */ "","‘","’","“","”", |
56 |
"·","&ndash","&mdash", |
57 |
/* 0x98 */ "˜","™","š","⟩","œ","","","Ÿ", |
58 |
/* 0xa0 */ "","¡","¢","£", |
59 |
"¤","¥","¦","§", |
60 |
/* 0xa8 */ ""","©","a","«","¬","-","®","¯on;", |
61 |
/* 0xb0 */ "°","±","²","³", |
62 |
"&rsquot;","µn;","¶","·", |
63 |
/* 0xb8 */ ",","¹","°","»", |
64 |
"¼","½","¾","¿", |
65 |
/* 0xc0 */ "À","Á","Â","Ã", |
66 |
"Ä","Å","Æ","Ç", |
67 |
/* 0xc8 */ "È","É","Ê","Ë", |
68 |
"Ì","Í","Î","Ï", |
69 |
/* 0xd0 */ "Ð","Ñ","Ò","Ó", |
70 |
"Ô","Õ","Ö","×", |
71 |
/* 0xd8 */ "Ø","Ù","Ú","Û", |
72 |
"Ü","Ý","Þ","ß", |
73 |
/* 0xe0 */ "à","á","â","ã", |
74 |
"ä","å","æ","ç", |
75 |
/* 0xe8 */ "è","é","ê","ë", |
76 |
"ì","í","î","ï", |
77 |
/* 0xf0 */ "ð","ñ","ò","ó", |
78 |
"ô","õ","ö","÷", |
79 |
/* 0xf8 */ "ø","ù","ú","û", |
80 |
"ü","ý","þ","ÿ" |
81 |
}; |
82 |
|
83 |
static psiconv_character_layout gen_base_char(const psiconv_font font, |
84 |
const psiconv_color color, |
85 |
const psiconv_color back_color); |
86 |
static void diff_char(FILE *of, const psiconv_character_layout old, |
87 |
const psiconv_character_layout new, int *flags); |
88 |
static void gen_para(FILE *of, const psiconv_paragraph para, |
89 |
const psiconv_character_layout base_char); |
90 |
|
91 |
static void psiconv_gen_html_word(FILE *of,psiconv_word_f wf); |
92 |
static void psiconv_gen_html_texted(FILE *of,psiconv_texted_f tf); |
93 |
|
94 |
/* This is not necessarily the same as returned by basic_character_layout_status |
95 |
This one is specific for the base point of HTML */ |
96 |
psiconv_character_layout gen_base_char(const psiconv_font font, |
97 |
const psiconv_color color, |
98 |
const psiconv_color back_color) |
99 |
{ |
100 |
struct psiconv_character_layout_s base_char_struct = |
101 |
{ |
102 |
NULL, /* color */ |
103 |
NULL, /* back_color */ |
104 |
13.0, /* font_size */ |
105 |
psiconv_bool_false, /* italic */ |
106 |
psiconv_bool_false, /* bold */ |
107 |
psiconv_normalscript, /* super_sub */ |
108 |
psiconv_bool_false, /* underline */ |
109 |
psiconv_bool_false, /* strikethrough */ |
110 |
NULL, /* font */ |
111 |
}; |
112 |
base_char_struct.color = color; |
113 |
base_char_struct.back_color = back_color; |
114 |
base_char_struct.font = font; |
115 |
return psiconv_clone_character_layout(&base_char_struct); |
116 |
} |
117 |
|
118 |
/* flags & 1: 0 if no <FONT> was yet generated. |
119 |
flags & 2: 1 if at end-of-paragraph |
120 |
*/ |
121 |
void diff_char(FILE *of, const psiconv_character_layout old, |
122 |
const psiconv_character_layout new, |
123 |
int *flags) |
124 |
{ |
125 |
int font_set = 0; |
126 |
|
127 |
if ((old->font_size != new->font_size) || |
128 |
(old->color->red != new->color->red) || |
129 |
(old->color->green != new->color->green) || |
130 |
(old->color->blue != new->color->blue) || |
131 |
(strcmp(old->font->name,new->font->name)) || |
132 |
(old->font->screenfont != new->font->screenfont) || |
133 |
((*flags & 0x03) == 3)) { |
134 |
if (old->italic) |
135 |
fputs("</I>",of); |
136 |
if (old->bold) |
137 |
fputs("</B>",of); |
138 |
if (old->underline) |
139 |
fputs("</U>",of); |
140 |
if (old->strikethrough) |
141 |
fputs("</STRIKE>",of); |
142 |
if (old->super_sub == psiconv_superscript) |
143 |
fputs("</SUP>",of); |
144 |
if (old->super_sub == psiconv_subscript) |
145 |
fputs("</SUB>",of); |
146 |
if ((*flags & 1) == 1) |
147 |
fputs("</FONT>",of); |
148 |
if ((*flags & 2) == 0) { |
149 |
*flags |= 1; |
150 |
fputs("<FONT SIZE=",of); |
151 |
if (new->font_size <= 8.0) |
152 |
fputs("1",of); |
153 |
else if (new->font_size <= 10.0) |
154 |
fputs("2",of); |
155 |
else if (new->font_size <= 12.0) |
156 |
fputs("3",of); |
157 |
else if (new->font_size <= 14.0) |
158 |
fputs("4",of); |
159 |
else if (new->font_size <= 18.0) |
160 |
fputs("5",of); |
161 |
else if (new->font_size <= 24.0) |
162 |
fputs("6",of); |
163 |
else |
164 |
fputs("7",of); |
165 |
fprintf(of," COLOR=#%02x%02x%02x",new->color->red,new->color->green, |
166 |
new->color->blue); |
167 |
if (new->font->screenfont == psiconv_font_sansserif) |
168 |
fprintf(of," FACE=\"%s, Sans-Serif\">",new->font->name); |
169 |
else if (new->font->screenfont == psiconv_font_nonprop) |
170 |
fprintf(of," FACE=\"%s, Monospace\">",new->font-> name); |
171 |
else if (new->font->screenfont == psiconv_font_serif) |
172 |
fprintf(of," FACE=\"%s, Serif\">",new->font-> name); |
173 |
else |
174 |
fprintf(of," FACE=\"%s, Serif\">",new->font-> name); |
175 |
} |
176 |
if (new->italic) |
177 |
fputs("<I>",of); |
178 |
if (new->bold) |
179 |
fputs("<B>",of); |
180 |
if (new->underline) |
181 |
fputs("<U>",of); |
182 |
if (new->strikethrough) |
183 |
fputs("<STRIKE>",of); |
184 |
if (new->super_sub == psiconv_superscript) |
185 |
fputs("<SUP>",of); |
186 |
if (new->super_sub == psiconv_subscript) |
187 |
fputs("<SUB>",of); |
188 |
} else { |
189 |
if (font_set || (old->italic != new->italic)) { |
190 |
if (old->italic) |
191 |
fputs("</I>",of); |
192 |
else |
193 |
fputs("<I>",of); |
194 |
} |
195 |
if (old->bold != new->bold) { |
196 |
if (old->bold) |
197 |
fputs("</B>",of); |
198 |
else |
199 |
fputs("<B>",of); |
200 |
} |
201 |
if (old->underline != new->underline) { |
202 |
if (old->underline) |
203 |
fputs("</U>",of); |
204 |
else |
205 |
fputs("<U>",of); |
206 |
} |
207 |
if (old->strikethrough != new->strikethrough) { |
208 |
if (old->strikethrough) |
209 |
fputs("</STRIKE>",of); |
210 |
else |
211 |
fputs("<STRIKE>",of); |
212 |
} |
213 |
if (old->super_sub != new->super_sub) { |
214 |
if (old->super_sub == psiconv_superscript) |
215 |
fputs("</SUP>",of); |
216 |
else if (old->super_sub == psiconv_subscript) |
217 |
fputs("</SUB>",of); |
218 |
if (new->super_sub == psiconv_superscript) |
219 |
fputs("<SUP>",of); |
220 |
else if (new->super_sub == psiconv_subscript) |
221 |
fputs("<SUB>",of); |
222 |
} |
223 |
} |
224 |
} |
225 |
|
226 |
void gen_para(FILE *of, const psiconv_paragraph para, |
227 |
const psiconv_character_layout base_char) |
228 |
{ |
229 |
int i,j,loc; |
230 |
psiconv_character_layout cur_char; |
231 |
psiconv_in_line_layout inl; |
232 |
int flags = 0; |
233 |
|
234 |
|
235 |
fputs("<P",of); |
236 |
if (para->base_paragraph->justify_hor == psiconv_justify_left) |
237 |
fputs(" ALIGN=left",of); |
238 |
else if (para->base_paragraph->justify_hor == psiconv_justify_right) |
239 |
fputs(" ALIGN=right",of); |
240 |
else if (para->base_paragraph->justify_hor == psiconv_justify_centre) |
241 |
fputs(" ALIGN=center",of); |
242 |
else if (para->base_paragraph->justify_hor == psiconv_justify_full) |
243 |
fputs(" ALIGN=left",of); |
244 |
fputs(">",of); |
245 |
if (para->base_paragraph->bullet->on) |
246 |
fputs("<UL><LI>",of); |
247 |
|
248 |
cur_char = base_char; |
249 |
|
250 |
if (psiconv_list_length(para->in_lines) == 0) { |
251 |
diff_char(of,cur_char,para->base_character,&flags); |
252 |
cur_char = para->base_character; |
253 |
} |
254 |
loc = 0; |
255 |
|
256 |
for (i = 0; i < psiconv_list_length(para->in_lines); i++) { |
257 |
inl = psiconv_list_get(para->in_lines,i); |
258 |
diff_char(of,cur_char,inl->layout,&flags); |
259 |
cur_char = inl->layout; |
260 |
for (j = loc; j < inl->length + loc; j ++) { |
261 |
fputs(char_table[(unsigned char) (para->text[j])],of); |
262 |
} |
263 |
loc = j; |
264 |
} |
265 |
|
266 |
if (loc < strlen(para->text)) { |
267 |
diff_char(of,cur_char,para->base_character,&flags); |
268 |
cur_char = para->base_character; |
269 |
for (j = loc; j < strlen(para->text); j ++) { |
270 |
fputs(char_table[(unsigned char) (para->text[j])],of); |
271 |
} |
272 |
} |
273 |
|
274 |
if (strlen(para->text) == 0) |
275 |
fputs("<BR>",of); |
276 |
|
277 |
flags |= 2; |
278 |
diff_char(of,cur_char,base_char,&flags); |
279 |
|
280 |
if (para->base_paragraph->bullet->on) |
281 |
fputs("</UL>",of); |
282 |
|
283 |
fputs("</P>\n",of); |
284 |
} |
285 |
|
286 |
int psiconv_gen_html(const char * filename,const psiconv_file file, |
287 |
const char *dest) |
288 |
{ |
289 |
FILE *of = fopen(filename,"w"); |
290 |
if (! of) |
291 |
return -1; |
292 |
|
293 |
if (file->type == psiconv_word_file) { |
294 |
psiconv_gen_html_word(of,(psiconv_word_f) file->file); |
295 |
} else if (file->type == psiconv_texted_file) { |
296 |
psiconv_gen_html_texted(of,(psiconv_texted_f) file->file); |
297 |
} else { |
298 |
fclose(of); |
299 |
return -1; |
300 |
} |
301 |
return fclose(of); |
302 |
} |
303 |
|
304 |
void psiconv_gen_html_texted(FILE *of,psiconv_texted_f tf) |
305 |
{ |
306 |
psiconv_character_layout base_char; |
307 |
psiconv_paragraph para; |
308 |
int i; |
309 |
|
310 |
/* We have nothing better */ |
311 |
base_char = psiconv_basic_character_layout(); |
312 |
|
313 |
fputs("<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">", of); |
314 |
fputs("\n<HTML>\n<HEAD>\n <META NAME=\"GENERATOR\"", of); |
315 |
fputs(" CONTENT=\"psiconv-" VERSION "\">\n", of); |
316 |
fputs("<BODY>\n",of); |
317 |
for (i = 0; i < psiconv_list_length(tf->texted_sec->paragraphs); i++) { |
318 |
para = psiconv_list_get(tf->texted_sec->paragraphs,i); |
319 |
gen_para(of,para,base_char); |
320 |
} |
321 |
fputs("</BODY>\n</HTML>\n",of); |
322 |
psiconv_free_character_layout(base_char); |
323 |
} |
324 |
|
325 |
|
326 |
void psiconv_gen_html_word(FILE *of,psiconv_word_f wf) |
327 |
{ |
328 |
int i; |
329 |
psiconv_paragraph para; |
330 |
psiconv_color white,black; |
331 |
psiconv_character_layout base_char; |
332 |
|
333 |
white = malloc(sizeof(*white)); |
334 |
black = malloc(sizeof(*black)); |
335 |
white->red = 0x00; |
336 |
white->green = 0x00; |
337 |
white->blue = 0x00; |
338 |
black->red = 0xff; |
339 |
black->green = 0xff; |
340 |
black->blue = 0xff; |
341 |
|
342 |
/* To keep from generating a font desc for each line */ |
343 |
base_char = gen_base_char(wf->styles_sec->normal->character->font, |
344 |
black,white); |
345 |
|
346 |
psiconv_free_color(black); |
347 |
psiconv_free_color(white); |
348 |
|
349 |
fputs("<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">", of); |
350 |
fputs("\n<HTML>\n<HEAD>\n <META NAME=\"GENERATOR\"", of); |
351 |
fputs(" CONTENT=\"psiconv-" VERSION "\">\n", of); |
352 |
fputs("<BODY>\n",of); |
353 |
|
354 |
for (i = 0; i < psiconv_list_length(wf->paragraphs); i++) { |
355 |
para = psiconv_list_get(wf->paragraphs,i); |
356 |
gen_para(of,para,base_char); |
357 |
} |
358 |
fputs("</BODY>\n</HTML>\n",of); |
359 |
psiconv_free_character_layout(base_char); |
360 |
} |
361 |
|
362 |
static struct psiconv_fileformat_s ff = |
363 |
{ |
364 |
"HTML3", |
365 |
"HTML 3.2, not verified so probably not completely compliant", |
366 |
&psiconv_gen_html |
367 |
}; |
368 |
|
369 |
void init_html(void) |
370 |
{ |
371 |
psiconv_list_add(fileformat_list,&ff); |
372 |
} |