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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 20 - (hide annotations)
Wed Oct 27 15:05:03 1999 UTC (24 years, 5 months ago) by frodo
File MIME type: text/plain
File size: 12870 byte(s)
(Frodo) Some IRIX compatibility issues fixed, as well as some compiler
        warnings

1 frodo 14 /*
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 frodo 20 NULL, /* color */
98     NULL, /* back_color */
99 frodo 14 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 frodo 20 NULL, /* font */
106 frodo 14 };
107 frodo 20 base_char_struct.color = color;
108     base_char_struct.back_color = back_color;
109     base_char_struct.font = font;
110 frodo 14 return psiconv_clone_character_layout(&base_char_struct);
111     }
112    
113     /* flags & 1: 0 if no <FONT> was yet generated.
114     flags & 2: 1 if at end-of-paragraph
115     */
116     void diff_char(FILE *of, const psiconv_character_layout old,
117     const psiconv_character_layout new,
118     int *flags)
119     {
120     int font_set = 0;
121    
122     if ((old->font_size != new->font_size) ||
123     (old->color->red != new->color->red) ||
124     (old->color->green != new->color->green) ||
125     (old->color->blue != new->color->blue) ||
126     (strcmp(old->font->name,new->font->name)) ||
127     (old->font->screenfont != new->font->screenfont) ||
128     ((*flags & 0x03) == 3)) {
129     if (old->italic)
130     fputs("</I>",of);
131     if (old->bold)
132     fputs("</B>",of);
133     if (old->underline)
134     fputs("</U>",of);
135     if (old->strike_out)
136     fputs("</STRIKE>",of);
137     if (old->super_sub == psiconv_superscript)
138     fputs("</SUP>",of);
139     if (old->super_sub == psiconv_subscript)
140     fputs("</SUB>",of);
141     if ((*flags & 1) == 1)
142     fputs("</FONT>",of);
143     if ((*flags & 2) == 0) {
144     *flags |= 1;
145     fputs("<FONT SIZE=",of);
146     if (new->font_size <= 8.0)
147     fputs("1",of);
148     else if (new->font_size <= 10.0)
149     fputs("2",of);
150     else if (new->font_size <= 12.0)
151     fputs("3",of);
152     else if (new->font_size <= 14.0)
153     fputs("4",of);
154     else if (new->font_size <= 18.0)
155     fputs("5",of);
156     else if (new->font_size <= 24.0)
157     fputs("6",of);
158     else
159     fputs("7",of);
160     fprintf(of," COLOR=#%02x%02x%02x",new->color->red,new->color->green,
161     new->color->blue);
162     if (new->font->screenfont == 1)
163     fprintf(of," FACE=\"%s, Sans-Serif\">",new->font->name);
164     else if (new->font->screenfont == 2)
165     fprintf(of," FACE=\"%s, Monospace\">",new->font-> name);
166     else if (new->font->screenfont == 3)
167     fprintf(of," FACE=\"%s, Serif\">",new->font-> name);
168     else
169     fprintf(of," FACE=\"%s, Serif\">",new->font-> name);
170     }
171     if (new->italic)
172     fputs("<I>",of);
173     if (new->bold)
174     fputs("<B>",of);
175     if (new->underline)
176     fputs("<U>",of);
177     if (new->strike_out)
178     fputs("<STRIKE>",of);
179     if (new->super_sub == psiconv_superscript)
180     fputs("<SUP>",of);
181     if (new->super_sub == psiconv_subscript)
182     fputs("<SUB>",of);
183     } else {
184     if (font_set || (old->italic != new->italic)) {
185     if (old->italic)
186     fputs("</I>",of);
187     else
188     fputs("<I>",of);
189     }
190     if (old->bold != new->bold) {
191     if (old->bold)
192     fputs("</B>",of);
193     else
194     fputs("<B>",of);
195     }
196     if (old->underline != new->underline) {
197     if (old->underline)
198     fputs("</U>",of);
199     else
200     fputs("<U>",of);
201     }
202     if (old->strike_out != new->strike_out) {
203     if (old->strike_out)
204     fputs("</STRIKE>",of);
205     else
206     fputs("<STRIKE>",of);
207     }
208     if (old->super_sub != new->super_sub) {
209     if (old->super_sub == psiconv_superscript)
210     fputs("</SUP>",of);
211     else if (old->super_sub == psiconv_subscript)
212     fputs("</SUB>",of);
213     if (new->super_sub == psiconv_superscript)
214     fputs("<SUP>",of);
215     else if (new->super_sub == psiconv_subscript)
216     fputs("<SUB>",of);
217     }
218     }
219     }
220    
221     void gen_para(FILE *of, const psiconv_paragraph para,
222     const psiconv_character_layout base_char)
223     {
224     int i,j,loc;
225     psiconv_character_layout cur_char;
226     psiconv_in_line_layout inl;
227     int flags = 0;
228    
229    
230     fputs("<P",of);
231     if (para->base_paragraph->justify_hor == psiconv_justify_left)
232     fputs(" ALIGN=left",of);
233     else if (para->base_paragraph->justify_hor == psiconv_justify_right)
234     fputs(" ALIGN=right",of);
235     else if (para->base_paragraph->justify_hor == psiconv_justify_centre)
236     fputs(" ALIGN=center",of);
237     else if (para->base_paragraph->justify_hor == psiconv_justify_full)
238     fputs(" ALIGN=left",of);
239     fputs(">",of);
240     if (para->base_paragraph->bullet->on)
241     fputs("<UL><LI>",of);
242    
243     cur_char = base_char;
244    
245     if (psiconv_list_length(para->in_lines) == 0) {
246     diff_char(of,cur_char,para->base_character,&flags);
247     cur_char = para->base_character;
248     }
249     loc = 0;
250    
251     for (i = 0; i < psiconv_list_length(para->in_lines); i++) {
252     inl = psiconv_list_get(para->in_lines,i);
253     diff_char(of,cur_char,inl->layout,&flags);
254     cur_char = inl->layout;
255     for (j = loc; j < inl->length + loc; j ++) {
256     fputs(char_table[(unsigned char) (para->text[j])],of);
257     }
258     loc = j;
259     }
260    
261     if (loc < strlen(para->text)) {
262     diff_char(of,cur_char,para->base_character,&flags);
263     cur_char = para->base_character;
264     for (j = loc; j < strlen(para->text); j ++) {
265     fputs(char_table[(unsigned char) (para->text[j])],of);
266     }
267     }
268    
269     if (strlen(para->text) == 0)
270     fputs("<BR>",of);
271    
272     flags |= 2;
273     diff_char(of,cur_char,base_char,&flags);
274    
275     if (para->base_paragraph->bullet->on)
276     fputs("</UL>",of);
277    
278     fputs("</P>\n",of);
279     }
280    
281     void psiconv_gen_html(FILE *of,psiconv_file file)
282     {
283     if (file->type == psiconv_word_file)
284     psiconv_gen_html_word(of,(psiconv_word_f) file->file);
285     else if (file->type == psiconv_texted_file)
286     psiconv_gen_html_texted(of,(psiconv_texted_f) file->file);
287     else
288     return;
289     }
290    
291     void psiconv_gen_html_texted(FILE *of,psiconv_texted_f tf)
292     {
293     psiconv_character_layout base_char;
294     psiconv_paragraph para;
295     int i;
296    
297     /* We have nothing better */
298     base_char = psiconv_basic_character_layout();
299    
300     fputs("<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">", of);
301     fputs("\n<HTML>\n<HEAD>\n <META NAME=\"GENERATOR\"", of);
302     fputs(" CONTENT=\"psiconv-" VERSION "\">\n", of);
303     fputs("<BODY>\n",of);
304     for (i = 0; i < psiconv_list_length(tf->texted_sec->paragraphs); i++) {
305     para = psiconv_list_get(tf->texted_sec->paragraphs,i);
306     gen_para(of,para,base_char);
307     }
308     fputs("</BODY>\n</HTML>\n",of);
309     psiconv_free_character_layout(base_char);
310     }
311    
312    
313     void psiconv_gen_html_word(FILE *of,psiconv_word_f wf)
314     {
315     int i;
316     psiconv_paragraph para;
317     psiconv_color white,black;
318     psiconv_character_layout base_char;
319    
320     white = malloc(sizeof(*white));
321     black = malloc(sizeof(*black));
322     white->red = 0x00;
323     white->green = 0x00;
324     white->blue = 0x00;
325     black->red = 0xff;
326     black->green = 0xff;
327     black->blue = 0xff;
328    
329     /* To keep from generating a font desc for each line */
330     base_char = gen_base_char(wf->styles_sec->normal->character->font,
331     black,white);
332    
333     psiconv_free_color(black);
334     psiconv_free_color(white);
335    
336     fputs("<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">", of);
337     fputs("\n<HTML>\n<HEAD>\n <META NAME=\"GENERATOR\"", of);
338     fputs(" CONTENT=\"psiconv-" VERSION "\">\n", of);
339     fputs("<BODY>\n",of);
340    
341     for (i = 0; i < psiconv_list_length(wf->paragraphs); i++) {
342     para = psiconv_list_get(wf->paragraphs,i);
343     gen_para(of,para,base_char);
344     }
345     fputs("</BODY>\n</HTML>\n",of);
346     psiconv_free_character_layout(base_char);
347     }
348    
349    

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