/[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 190 - (show annotations)
Mon Jan 26 21:56:49 2004 UTC (16 years, 9 months ago) by frodo
File MIME type: text/plain
File size: 12689 byte(s)
(Frodo) XHTML work

1 /* gen_html.c - Part of psiconv, a PSION 5 file formats converter
2 Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "config.h"
20
21 #include <psiconv/configuration.h>
22 #include <psiconv/data.h>
23 #include "general.h"
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 #ifdef DMALLOC
29 #include "dmalloc.h"
30 #endif
31
32 #define TEMPSTR_LEN 100
33
34 typedef enum output_type_e { output_html, output_xhtml } output_type;
35
36 void character_layout_diffs(const psiconv_config config, psiconv_list list,
37 const psiconv_character_layout new,
38 const psiconv_character_layout base,
39 const encoding enc)
40 {
41 if (new->italic != base->italic) {
42 output_simple_chars(config,list,"font-style:",enc);
43 output_simple_chars(config,list,new->italic?"italic":"normal",enc);
44 output_simple_chars(config,list,";",enc);
45 }
46 if ((new->underline != base->underline) ||
47 (new->strikethrough != base->strikethrough)) {
48 output_simple_chars(config,list,"text-decoration:",enc);
49 output_simple_chars(config,list,new->underline?"underline":
50 new->strikethrough?"line-through":
51 "none",enc);
52 output_simple_chars(config,list,";",enc);
53 }
54 if (new->bold != base->bold) {
55 output_simple_chars(config,list,"font-weight:",enc);
56 output_simple_chars(config,list,new->bold?"bold":"normal",enc);
57 output_simple_chars(config,list,";",enc);
58 }
59 if (new->super_sub != base->super_sub) {
60 output_simple_chars(config,list,"font-style:",enc);
61 output_simple_chars(config,list,
62 new->super_sub==psiconv_superscript?"super":
63 new->super_sub==psiconv_subscript?"sub":
64 "normal",enc);
65 output_simple_chars(config,list,";",enc);
66 }
67 }
68
69 void paragraph_layout_diffs(const psiconv_config config, psiconv_list list,
70 const psiconv_paragraph_layout new,
71 const psiconv_paragraph_layout base)
72 {
73 }
74
75 void header(const psiconv_config config, psiconv_list list,
76 output_type type, const encoding enc)
77 {
78 if (type == output_xhtml)
79 output_simple_chars(config,list,
80 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",enc);
81 output_simple_chars(config,list,"<!DOCTYPE html PUBLIC ",enc);
82 output_simple_chars(config,list,type == output_xhtml?
83 "\"-//W3C//DTD XHTML 1.0 Strict//EN\" ":
84 "\"-//W3C//DTD HTML 4.01//EN\" ",enc);
85 output_simple_chars(config,list,type == output_xhtml?
86 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">":
87 "\"http://www.w3.org/TR/html4/strict.dtd\">",enc);
88 output_simple_chars(config,list,"\n<html",enc);
89 if (type == output_xhtml)
90 output_simple_chars(config,list,
91 " xmlns=\"http://www.w3.org/1999/xhtml\"",enc);
92 output_simple_chars(config,list,">\n",enc);
93 output_simple_chars(config,list,"<head>\n",enc);
94 output_simple_chars(config,list,"<meta http-equiv=\"Content-Type\" "
95 "content=\"text/html; charset=",enc);
96 output_simple_chars(config,list,enc==ENCODING_UTF8?"UTF-8":
97 enc==ENCODING_UCS2?"UTF-16BE":
98 enc==ENCODING_ASCII?"US-ASCII":
99 "",enc);
100 output_simple_chars(config,list,"\">\n",enc);
101 output_simple_chars(config,list,"<meta http-equiv=\"Content-Style-Type\" "
102 "content=\"text/css\">\n",enc);
103 output_simple_chars(config,list,"<title>EPOC32 file "
104 "converted by psiconv</title>\n",enc);
105 output_simple_chars(config,list,"</head>\n",enc);
106 output_simple_chars(config,list,"<body>\n",enc);
107 }
108
109 void footer(const psiconv_config config, psiconv_list list,
110 output_type type, const encoding enc)
111 {
112 output_simple_chars(config,list,"</body>\n",enc);
113 output_simple_chars(config,list,"</html>\n",enc);
114 }
115
116 void color(const psiconv_config config, psiconv_list list,
117 psiconv_color color, output_type type, const encoding enc)
118 {
119 char tempstr[TEMPSTR_LEN];
120 if ((color->red == 0xff) &&
121 (color->blue == 0xff) &&
122 (color->green == 0xff))
123 output_simple_chars(config,list,"transparant",enc);
124 else {
125 snprintf(tempstr,TEMPSTR_LEN,"rgb(%d,%d,%d)",
126 color->red,
127 color->green,
128 color->blue);
129 output_simple_chars(config,list,tempstr,enc);
130 }
131 }
132
133 void charlayout(const psiconv_config config, psiconv_list list,
134 psiconv_string_t text,psiconv_character_layout layout,
135 output_type type, const encoding enc)
136 {
137 char tempstr[TEMPSTR_LEN];
138 if (layout->italic)
139 output_simple_chars(config,list,"<i>",enc);
140 if (layout->bold)
141 output_simple_chars(config,list,"<b>",enc);
142 if (layout->super_sub != psiconv_normalscript)
143 output_simple_chars(config,list,
144 layout->super_sub == psiconv_superscript?"<sup>":
145 layout->super_sub == psiconv_subscript?"<sub>":
146 "",enc);
147
148 output_simple_chars(config,list,"<span style=\"",enc);
149
150 if (layout->underline || layout->strikethrough) {
151 output_simple_chars(config,list,"text-decoration:",enc);
152 output_simple_chars(config,list,layout->underline?"underline":
153 layout->strikethrough?"line-through":
154 "",enc);
155 output_simple_chars(config,list,";",enc);
156 }
157
158 output_simple_chars(config,list,"color:",enc);
159 color(config,list,layout->color,type,enc);
160 output_simple_chars(config,list,";",enc);
161
162 output_simple_chars(config,list,"background-color:",enc);
163 color(config,list,layout->back_color,type,enc);
164 output_simple_chars(config,list,";",enc);
165
166 output_simple_chars(config,list,"font-size:",enc);
167 snprintf(tempstr,TEMPSTR_LEN,"%f",layout->font_size);
168 output_simple_chars(config,list,tempstr,enc);
169 output_simple_chars(config,list,"pt;",enc);
170
171 output_simple_chars(config,list,"\">",enc);
172
173 output_string(config,list,text,enc);
174
175 output_simple_chars(config,list,"</span>",enc);
176 if (layout->super_sub != psiconv_normalscript)
177 output_simple_chars(config,list,
178 layout->super_sub == psiconv_superscript?"</sup>":
179 layout->super_sub == psiconv_subscript?"</sub>":
180 "",enc);
181 if (layout->bold)
182 output_simple_chars(config,list,"</b>",enc);
183 if (layout->italic)
184 output_simple_chars(config,list,"</i>",enc);
185 }
186
187 void paragraph(const psiconv_config config, psiconv_list list,
188 psiconv_paragraph para,output_type type,
189 const encoding enc)
190 {
191 char tempstr[TEMPSTR_LEN];
192 int i,charnr;
193 psiconv_string_t text;
194 psiconv_in_line_layout layout;
195
196 output_simple_chars(config,list,"<p style=\"",enc);
197
198 output_simple_chars(config,list,"background-color:",enc);
199 color(config,list,para->base_paragraph->back_color,type,enc);
200 output_simple_chars(config,list,";",enc);
201
202 output_simple_chars(config,list,"padding-left:",enc);
203 snprintf(tempstr,TEMPSTR_LEN,"%f",para->base_paragraph->indent_left);
204 output_simple_chars(config,list,tempstr,enc);
205 output_simple_chars(config,list,"cm;",enc);
206
207 output_simple_chars(config,list,"padding-right:",enc);
208 snprintf(tempstr,TEMPSTR_LEN,"%f",para->base_paragraph->indent_right);
209 output_simple_chars(config,list,tempstr,enc);
210 output_simple_chars(config,list,"cm;",enc);
211
212 output_simple_chars(config,list,"text-indent:",enc);
213 snprintf(tempstr,TEMPSTR_LEN,"%f",para->base_paragraph->indent_left - para->base_paragraph->indent_first);
214 output_simple_chars(config,list,tempstr,enc);
215 output_simple_chars(config,list,"cm;",enc);
216
217 output_simple_chars(config,list,"text-align:",enc);
218 output_simple_chars(config,list,para->base_paragraph->justify_hor==psiconv_justify_left?"left":
219 para->base_paragraph->justify_hor==psiconv_justify_centre?"center":
220 para->base_paragraph->justify_hor==psiconv_justify_right?"right":
221 para->base_paragraph->justify_hor==psiconv_justify_full?"justify":
222 "",enc);
223 output_simple_chars(config,list,";",enc);
224
225
226 output_simple_chars(config,list,"line-height:",enc);
227 snprintf(tempstr,TEMPSTR_LEN,"%f",para->base_paragraph->linespacing);
228 output_simple_chars(config,list,tempstr,enc);
229 output_simple_chars(config,list,"pt;",enc);
230
231 output_simple_chars(config,list,"padding-top:",enc);
232 snprintf(tempstr,TEMPSTR_LEN,"%f",para->base_paragraph->space_above);
233 output_simple_chars(config,list,tempstr,enc);
234 output_simple_chars(config,list,"pt;",enc);
235
236 output_simple_chars(config,list,"padding-bottom:",enc);
237 snprintf(tempstr,TEMPSTR_LEN,"%f",para->base_paragraph->space_below);
238 output_simple_chars(config,list,tempstr,enc);
239 output_simple_chars(config,list,"pt;",enc);
240
241 output_simple_chars(config,list,"\">",enc);
242
243 if (psiconv_list_length(para->in_lines) == 0) {
244 charlayout(config,list,para->text,para->base_character,type,enc);
245 } else {
246 charnr = 0;
247 for (i = 0; i < psiconv_list_length(para->in_lines); i++) {
248 if (!(layout = psiconv_list_get(para->in_lines,i))) {
249 fputs("Internal data structures corruption\n",stderr);
250 exit(1);
251 }
252 if (!(text = malloc(sizeof (*text) * (layout->length + 1)))) {
253 fputs("Out of memory error\n",stderr);
254 exit(1);
255 }
256 memcpy(text,para->text+charnr,layout->length * sizeof(*text));
257 text[layout->length] = 0;
258 charlayout(config,list,text,layout->layout,type,enc);
259 free(text);
260 charnr += layout->length;
261 }
262 }
263 output_simple_chars(config,list,"</p>\n",enc);
264 }
265
266 void paragraphs(const psiconv_config config, psiconv_list list,
267 psiconv_text_and_layout paragraphs,output_type type,
268 const encoding enc)
269 {
270 int i;
271 psiconv_paragraph para;
272 for (i = 0; i < psiconv_list_length(paragraphs); i++) {
273 if (!(para = psiconv_list_get(paragraphs,i))) {
274 fputs("Internal datastructure corruption\n",stderr);
275 exit(1);
276 }
277 paragraph(config,list,para,type,enc);
278 }
279 }
280
281 void gen_html_word(const psiconv_config config, psiconv_list list,
282 const psiconv_word_f file, output_type type,
283 const encoding enc)
284 {
285 if (!file)
286 return;
287
288 header(config,list,type,enc);
289 paragraphs(config,list,file->paragraphs,type,enc);
290 footer(config,list,type,enc);
291 }
292
293
294 void gen_html_texted(const psiconv_config config, psiconv_list list,
295 const psiconv_texted_f file, output_type type,
296 const encoding enc)
297 {
298 header(config,list,type,enc);
299 paragraphs(config,list,file->texted_sec->paragraphs,type,enc);
300 footer(config,list,type,enc);
301 }
302
303 int gen_html(const psiconv_config config, psiconv_list list,
304 const psiconv_file file, const char *dest,
305 const encoding enc)
306 {
307 output_type type;
308
309 if (!strcmp("HTML4",dest))
310 type = output_html;
311 else if (!strcmp("XHTML",dest))
312 type = output_xhtml;
313 else
314 return -1;
315
316 if (enc == ENCODING_PSION) {
317 fputs("Unsupported encoding\n",stderr);
318 return -1;
319 }
320
321 if (file->type == psiconv_word_file) {
322 gen_html_word(config,list,(psiconv_word_f) file->file,type,enc);
323 return 0;
324 } else if (file->type == psiconv_texted_file) {
325 gen_html_texted(config,list,(psiconv_texted_f) file->file,type, enc);
326 return 0;
327 } else
328 return -1;
329 }
330
331
332 static struct fileformat_s ffs[] =
333 {
334 {
335 "XHTML",
336 "XHTML 1.0 Strict, using CSS for formatting",
337 gen_html
338 },
339 {
340 "HTML4",
341 "HTML 4.01 Strict, using CSS for formatting",
342 gen_html
343 },
344 {
345 NULL,
346 NULL,
347 NULL
348 }
349 };
350
351
352 void init_html(void)
353 {
354 int i;
355 for (i = 0; ffs[i].name; i++)
356 psiconv_list_add(fileformat_list,ffs+i);
357 }

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