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

Annotation of /psiconv/trunk/program/psiconv/gen_html4.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 351 - (hide annotations)
Wed Oct 22 19:53:40 2014 UTC (6 years ago) by frodo
File MIME type: text/plain
File size: 12267 byte(s)
(Frodo) Update copyright year in all source files

1 frodo 196 /*
2     gen_html.c - Part of psiconv, a PSION 5 file formats converter
3 frodo 351 Copyright (c) 1999-2014 Frodo Looijaard <frodo@frodo.looijaard.name>
4 frodo 192
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 frodo 14
20 frodo 192 #include "config.h"
21 frodo 147
22 frodo 192 #include <psiconv/configuration.h>
23     #include <psiconv/data.h>
24     #include "general.h"
25 frodo 147
26 frodo 14 #include <string.h>
27     #include <stdlib.h>
28    
29 frodo 142 #ifdef DMALLOC
30     #include "dmalloc.h"
31     #endif
32    
33 frodo 192 #define TEMPSTR_LEN 100
34 frodo 14
35 frodo 192 static void text(const psiconv_config config,psiconv_list list,
36     psiconv_string_t data,const encoding enc);
37     static void header(const psiconv_config config, psiconv_list list,
38     const encoding enc);
39     static void footer(const psiconv_config config, psiconv_list list,
40     const encoding enc);
41     static void characters(const psiconv_config config, psiconv_list list,
42     const psiconv_string_t textstr,
43     const psiconv_character_layout layout,const encoding enc);
44     static void paragraph(const psiconv_config config, psiconv_list list,
45     psiconv_paragraph para, const encoding enc);
46     static void paragraphs(const psiconv_config config, psiconv_list list,
47     psiconv_text_and_layout paragraphs, const encoding enc);
48     static void gen_word(const psiconv_config config, psiconv_list list,
49     const psiconv_word_f file, const encoding enc);
50     static void gen_texted(const psiconv_config config, psiconv_list list,
51     const psiconv_texted_f file, const encoding enc);
52 frodo 193 static int gen_html4(const psiconv_config config, psiconv_list list,
53     const psiconv_file file, const char *dest,
54     const encoding enc);
55 frodo 14
56 frodo 147
57 frodo 192 void text(const psiconv_config config,psiconv_list list,
58     psiconv_string_t data,const encoding enc)
59     {
60     int i;
61     for (i = 0; i < psiconv_unicode_strlen(data); i++) {
62     if ((data[i] == 0x06) || (data[i] == 0x07) || (data[i] == 0x08))
63     output_simple_chars(config,list,"<BR>",enc);
64     else if ((data[i] == 0x0b) || (data[i] == 0x0c))
65     output_simple_chars(config,list,"-",enc);
66 frodo 193 else if ((data[i] == 0x0f) || (data[i] == 0x09) || (data[i] == 0x0a))
67 frodo 192 output_simple_chars(config,list," ",enc);
68     else if (data[i] >= 0x20)
69     output_char(config,list,data[i],enc);
70     }
71     }
72 frodo 147
73 frodo 192 void header(const psiconv_config config, psiconv_list list, const encoding enc)
74     {
75     output_simple_chars(config,list,"<!DOCTYPE html PUBLIC "
76     "\"-//W3C//DTD HTML 4.01 Transitional//EN\" "
77     "\"http://www.w3.org/TR/html4/loose.dtd\">\n",
78     enc);
79     output_simple_chars(config,list,"<HTML>\n",enc);
80     output_simple_chars(config,list,"<HEAD>\n",enc);
81     output_simple_chars(config,list,"<META HTTP-EQUIV=\"Content-Type\" "
82     "CONTENT=\"text/html; charset=",enc);
83     output_simple_chars(config,list,enc==ENCODING_UTF8?"UTF-8":
84     enc==ENCODING_UCS2?"UTF-16BE":
85     enc==ENCODING_ASCII?"US-ASCII":
86     "",enc);
87     output_simple_chars(config,list,"\">\n",enc);
88     output_simple_chars(config,list,"<TITLE>EPOC32 file "
89     "converted by psiconv</TITLE>\n",enc);
90     output_simple_chars(config,list,"</HEAD>\n",enc);
91     output_simple_chars(config,list,"<BODY>\n",enc);
92     }
93 frodo 147
94 frodo 192 void footer(const psiconv_config config, psiconv_list list, const encoding enc)
95     {
96     output_simple_chars(config,list,"</BODY>\n",enc);
97     output_simple_chars(config,list,"</HTML>\n",enc);
98     }
99 frodo 14
100 frodo 195 int character_layout_equal(const psiconv_character_layout l1,
101     const psiconv_character_layout l2)
102     {
103     int font_size1,font_size2;
104 frodo 14
105 frodo 195 font_size1 = l1->font_size < 8 ?1:
106     l1->font_size < 10 ?2:
107     l1->font_size < 13 ?3:
108     l1->font_size < 17 ?4:
109     l1->font_size < 24 ?5:
110     l1->font_size < 36 ?6:7;
111     font_size2 = l2->font_size < 8 ?1:
112     l2->font_size < 10 ?2:
113     l2->font_size < 13 ?3:
114     l2->font_size < 17 ?4:
115     l2->font_size < 24 ?5:
116     l2->font_size < 36 ?6:7;
117    
118     return (l1 && l2 &&
119     (l1->color->red == l2->color->red) &&
120     (l1->color->green == l2->color->green) &&
121     (l1->color->blue == l2->color->blue) &&
122     (font_size1 == font_size2) &&
123     (l1->italic == l2->italic) &&
124     (l1->bold == l2->bold) &&
125     (l1->super_sub == l2->super_sub) &&
126     (l1->underline == l2->underline) &&
127     (l1->strikethrough == l2->strikethrough) &&
128     (l1->font->screenfont == l2->font->screenfont));
129     }
130    
131 frodo 192 void characters(const psiconv_config config, psiconv_list list,
132     const psiconv_string_t textstr,
133     const psiconv_character_layout layout,const encoding enc)
134     {
135     char tempstr[TEMPSTR_LEN];
136 frodo 14
137 frodo 192 output_simple_chars(config,list,"<FONT face=\"",enc);
138     output_simple_chars(config,list,
139     layout->font->screenfont == psiconv_font_serif?"serif":
140     layout->font->screenfont == psiconv_font_sansserif?"sans-serif":
141     layout->font->screenfont == psiconv_font_nonprop?"monospace":
142     layout->font->screenfont == psiconv_font_misc?"fantasy":"",
143     enc);
144     output_simple_chars(config,list,"\"",enc);
145    
146     if ((layout->font_size < 10) || (layout->font_size >= 13)) {
147     output_simple_chars(config,list," size=",enc);
148     output_simple_chars(config,list,
149     layout->font_size < 8 ?"1":
150     layout->font_size < 10 ?"2":
151     layout->font_size < 13 ?"3":
152     layout->font_size < 17 ?"4":
153     layout->font_size < 24 ?"5":
154     layout->font_size < 36 ?"6":"7",enc);
155     }
156     if ((layout->color->red != 0) || (layout->color->green != 0) ||
157     (layout->color->blue != 0)) {
158     snprintf(tempstr,TEMPSTR_LEN,"%02x%02x%02x",
159     layout->color->red,layout->color->green,layout->color->blue);
160     output_simple_chars(config,list," color=#",enc);
161     output_simple_chars(config,list,tempstr,enc);
162     }
163     output_simple_chars(config,list,">",enc);
164 frodo 14
165    
166 frodo 192 if (layout->italic)
167     output_simple_chars(config,list,"<I>",enc);
168     if (layout->bold)
169     output_simple_chars(config,list,"<B>",enc);
170     if (layout->super_sub != psiconv_normalscript)
171     output_simple_chars(config,list,
172     layout->super_sub == psiconv_superscript?"<SUP>":
173     layout->super_sub == psiconv_subscript?"<SUB>":
174     "",enc);
175     if (layout->strikethrough)
176     output_simple_chars(config,list,"<S>",enc);
177     if (layout->underline)
178     output_simple_chars(config,list,"<U>",enc);
179 frodo 14
180 frodo 192 text(config,list,textstr,enc);
181    
182     if (layout->underline)
183     output_simple_chars(config,list,"</U>",enc);
184     if (layout->strikethrough)
185     output_simple_chars(config,list,"</S>",enc);
186     if (layout->super_sub != psiconv_normalscript)
187     output_simple_chars(config,list,
188     layout->super_sub == psiconv_superscript?"</SUP>":
189     layout->super_sub == psiconv_subscript?"</SUB>":
190     "",enc);
191     if (layout->bold)
192     output_simple_chars(config,list,"</B>",enc);
193     if (layout->italic)
194     output_simple_chars(config,list,"</I>",enc);
195     output_simple_chars(config,list,"</FONT>",enc);
196 frodo 14 }
197    
198 frodo 192 void paragraph(const psiconv_config config, psiconv_list list,
199     psiconv_paragraph para, const encoding enc)
200 frodo 14 {
201 frodo 195 int i,charnr,start,len;
202 frodo 192 psiconv_string_t text;
203 frodo 195 psiconv_in_line_layout layout,next_layout;
204 frodo 14
205    
206 frodo 192 output_simple_chars(config,list,
207     para->base_paragraph->bullet->on?"<UL><LI":"<P",enc);
208 frodo 14
209 frodo 192 if (para->base_paragraph->justify_hor == psiconv_justify_centre)
210     output_simple_chars(config,list," align=center",enc);
211     else if (para->base_paragraph->justify_hor == psiconv_justify_right)
212     output_simple_chars(config,list," align=right",enc);
213     else if (para->base_paragraph->justify_hor == psiconv_justify_full)
214     output_simple_chars(config,list," align=justify",enc);
215 frodo 14
216 frodo 192 output_simple_chars(config,list,">",enc);
217 frodo 14
218 frodo 192 if (psiconv_list_length(para->in_lines) == 0) {
219 frodo 195 if (psiconv_unicode_strlen(para->text))
220     characters(config,list,para->text,para->base_character,enc);
221 frodo 192 } else {
222     charnr = 0;
223 frodo 195 start = -1;
224 frodo 192 for (i = 0; i < psiconv_list_length(para->in_lines); i++) {
225 frodo 195 if (start < 0)
226     start = charnr;
227 frodo 192 if (!(layout = psiconv_list_get(para->in_lines,i))) {
228     fputs("Internal data structures corruption\n",stderr);
229     exit(1);
230     }
231 frodo 195 if (i+1 < psiconv_list_length(para->in_lines)) {
232     if (!(next_layout = psiconv_list_get(para->in_lines,i+1))) {
233     fputs("Internal data structures corruption\n",stderr);
234     exit(1);
235     }
236     } else {
237     next_layout = NULL;
238 frodo 192 }
239 frodo 195 if (next_layout &&
240     character_layout_equal(layout->layout,next_layout->layout)) {
241     charnr += layout->length;
242     continue;
243     }
244     len = charnr - start + layout->length;
245     if (len) {
246     if (!(text = malloc(sizeof (*text) * (len + 1)))) {
247     fputs("Out of memory error\n",stderr);
248     exit(1);
249     }
250 frodo 339 memcpy(text,para->text+start,len * sizeof(*text));
251 frodo 195 text[len] = 0;
252     characters(config,list,text,layout->layout,enc);
253     free(text);
254     }
255 frodo 192 charnr += layout->length;
256 frodo 195 start = -1;
257 frodo 14 }
258 frodo 192 }
259     output_simple_chars(config, list,
260     para->base_paragraph->bullet->on?"</UL>\n":"\n",enc);
261 frodo 14 }
262    
263 frodo 192 void paragraphs(const psiconv_config config, psiconv_list list,
264     psiconv_text_and_layout paragraphs, const encoding enc)
265 frodo 14 {
266 frodo 192 int i;
267     psiconv_paragraph para;
268     for (i = 0; i < psiconv_list_length(paragraphs); i++) {
269     if (!(para = psiconv_list_get(paragraphs,i))) {
270     fputs("Internal datastructure corruption\n",stderr);
271     exit(1);
272 frodo 14 }
273 frodo 192 paragraph(config,list,para,enc);
274     }
275 frodo 14 }
276    
277 frodo 192 void gen_word(const psiconv_config config, psiconv_list list,
278     const psiconv_word_f file, const encoding enc)
279 frodo 14 {
280 frodo 192 if (!file)
281     return;
282 frodo 14
283 frodo 192 header(config,list,enc);
284     paragraphs(config,list,file->paragraphs,enc);
285     footer(config,list,enc);
286 frodo 14 }
287    
288    
289 frodo 192 void gen_texted(const psiconv_config config, psiconv_list list,
290     const psiconv_texted_f file, const encoding enc)
291 frodo 14 {
292 frodo 192 header(config,list,enc);
293     paragraphs(config,list,file->texted_sec->paragraphs,enc);
294     footer(config,list,enc);
295 frodo 14 }
296    
297 frodo 192 int gen_html4(const psiconv_config config, psiconv_list list,
298     const psiconv_file file, const char *dest,
299     const encoding enc)
300 frodo 14 {
301 frodo 192 encoding enc1 = enc;
302 frodo 14
303 frodo 192 if (enc == ENCODING_PSION) {
304     fputs("Unsupported encoding\n",stderr);
305     return -1;
306     } else if (enc == ENCODING_ASCII)
307     enc1 = ENCODING_ASCII_HTML;
308 frodo 14
309 frodo 192 if (file->type == psiconv_word_file) {
310     gen_word(config,list,(psiconv_word_f) file->file,enc1);
311     return 0;
312     } else if (file->type == psiconv_texted_file) {
313     gen_texted(config,list,(psiconv_texted_f) file->file,enc1);
314     return 0;
315     } else
316     return -1;
317 frodo 14 }
318 frodo 192
319 frodo 14
320 frodo 267 static struct fileformat_s fileformats[] =
321 frodo 34 {
322 frodo 192 {
323     "HTML4",
324     "HTML 4.01 Transitional, without CSS",
325     FORMAT_WORD | FORMAT_TEXTED,
326     gen_html4
327     },
328     {
329     NULL,
330     NULL,
331     0,
332     NULL
333     }
334 frodo 34 };
335    
336 frodo 192
337 frodo 34 void init_html4(void)
338     {
339 frodo 192 int i;
340 frodo 267 for (i = 0; fileformats[i].name; i++)
341     psiconv_list_add(fileformat_list,fileformats+i);
342 frodo 34 }

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