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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 142 - (show annotations)
Tue Jan 29 18:38:38 2002 UTC (22 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 17747 byte(s)
(Frodo) DMALLOC support

1 /*
2 * gen_rtf.c - Part of psiconv, a PSION 5 file formats converter
3 * Copyright (c) 1999 Andrew Johnson <anjohnson@iee.org>
4 * Portions Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "config.h"
22 #include "compat.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include "psiconv/data.h"
26 #include "psiconv/list.h"
27 #include "gen.h"
28 #include "psiconv.h"
29
30 #ifdef DMALLOC
31 #include "dmalloc.h"
32 #endif
33
34
35 /*
36 * Various string tables for RTF settings
37 */
38
39 /* Character conversion table */
40 /* TO BE ADJUSTED FOR RTF! */
41 static const char *char_table[0x100] = {
42 /* 0x00 */ "", "", "", "", "", "", "\n", "\n",
43 /* 0x08 */ "\n", "\t", "", "", "", "", "", "",
44 /* 0x10 */ " ", "", "", "", "", "", "", "",
45 /* 0x18 */ "", "", "", "", "", "", "", "",
46 /* 0x20 */ " ", "!", "\"", "#", "$", "%", "&", "'",
47 /* 0x28 */ "(", ")", "*", "+", ",", "-", ".", "/",
48 /* 0x30 */ "0", "1", "2", "3", "4", "5", "6", "7",
49 /* 0x38 */ "8", "9", ":", ";", "<", "=", ">", "?",
50 /* 0x40 */ "@", "A", "B", "C", "D", "E", "F", "G",
51 /* 0x48 */ "H", "I", "J", "K", "L", "M", "N", "O",
52 /* 0x50 */ "P", "Q", "R", "S", "T", "U", "V", "W",
53 /* 0x58 */ "X", "Y", "Z", "[", "\\", "]", "^", "_",
54 /* 0x60 */ "`", "a", "b", "c", "d", "e", "f", "g",
55 /* 0x68 */ "h", "i", "j", "k", "l", "m", "n", "o",
56 /* 0x70 */ "p", "q", "r", "s", "t", "u", "v", "w",
57 /* 0x78 */ "x", "y", "z", "{", "|", "}", "~", "",
58 /* 0x80 */ "", "", ",", "f", ",,", "...", "+", "#",
59 /* 0x88 */ "^", "\176/oo","S", "<", "OE", "", "", "",
60 /* 0x90 */ "", "`", "'", "``", "''", "*", "-", "--",
61 /* 0x98 */ "~", "(TM)", "s", ">", "oe", "", "", "Y",
62 /* 0xa0 */ "\xa0", "\xa1", "\xa2", "\xa3", "\xa4", "\xa5", "\xa6", "\xa7",
63 /* 0xa8 */ "\xa8", "\xa9", "\xaa", "\xab", "\xac", "\xad", "\xae", "\xaf",
64 /* 0xb0 */ "\xb0", "\xb1", "\xb2", "\xb3", "\xb4", "\xb5", "\xb6", "\xb7",
65 /* 0xb8 */ "\xb8", "\xb9", "\xba", "\xbb", "\xbc", "\xbd", "\xbe", "\xbf",
66 /* 0xc0 */ "\xc0", "\xc1", "\xc2", "\xc3", "\xc4", "\xc5", "\xc6", "\xc7",
67 /* 0xc8 */ "\xc8", "\xc9", "\xca", "\xcb", "\xcc", "\xcd", "\xce", "\xcf",
68 /* 0xd0 */ "\xd0", "\xd1", "\xd2", "\xd3", "\xd4", "\xd5", "\xd6", "\xd7",
69 /* 0xd8 */ "\xd8", "\xd9", "\xda", "\xdb", "\xdc", "\xdd", "\xde", "\xdf",
70 /* 0xe0 */ "\xe0", "\xe1", "\xe2", "\xe3", "\xe4", "\xe5", "\xe6", "\xe7",
71 /* 0xe8 */ "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef",
72 /* 0xf0 */ "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7",
73 /* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff",
74 };
75
76
77
78 static void fput_text(FILE * of, const char *text, int length) {
79 int j;
80
81 for (j = 0; j < length; j++) {
82 fputs(char_table[(unsigned char) (text[j])], of);
83 }
84 }
85
86 static int length_to_twips(psiconv_length_t len)
87 {
88 return 1440.0 / 2.54 * len;
89 }
90
91 static int lookup_color(psiconv_list colors,psiconv_color color)
92 {
93 int i;
94 psiconv_color comp;
95 for (i = 0; i < psiconv_list_length(colors); i ++) {
96 comp = * (psiconv_color *) psiconv_list_get(colors,i);
97 if ((comp->red == color->red) && (comp->green == color->green) &&
98 (comp->blue == color->blue))
99 return i;
100 }
101 return -1;
102 }
103
104 static void add_color(psiconv_list colors, psiconv_color color)
105 {
106 if (color)
107 if (lookup_color(colors,color) < 0)
108 psiconv_list_add(colors,&color);
109 }
110
111 static int lookup_font(psiconv_list fonts,psiconv_font font)
112 {
113 int i;
114 psiconv_font comp;
115 for (i = 0; i < psiconv_list_length(fonts); i ++) {
116 comp = *(psiconv_font *) psiconv_list_get(fonts,i);
117 if ((comp->screenfont == font->screenfont) &&
118 ! strcmp(comp->name,font->name))
119 return i;
120 }
121 return -1;
122 }
123
124 static void add_font(psiconv_list fonts, psiconv_font font)
125 {
126 if (font)
127 if (lookup_font(fonts,font) < 0)
128 psiconv_list_add(fonts,&font);
129 }
130
131 static void scan_border(psiconv_list colors,psiconv_list fonts,
132 psiconv_border sec)
133 {
134 if (sec) {
135 add_color(colors,sec->color);
136 }
137 }
138
139 static void scan_bullet(psiconv_list colors,psiconv_list fonts,
140 psiconv_bullet sec)
141 {
142 if (sec) {
143 add_color(colors,sec->color);
144 add_font(fonts,sec->font);
145 }
146 }
147
148 static void scan_paragraph_layout(psiconv_list colors,psiconv_list fonts,
149 psiconv_paragraph_layout sec)
150 {
151 if (sec) {
152 add_color(colors,sec->back_color);
153 scan_bullet(colors,fonts,sec->bullet);
154 scan_border(colors,fonts,sec->left_border);
155 scan_border(colors,fonts,sec->right_border);
156 scan_border(colors,fonts,sec->top_border);
157 scan_border(colors,fonts,sec->bottom_border);
158 }
159 }
160
161 static void scan_character_layout(psiconv_list colors,psiconv_list fonts,
162 psiconv_character_layout sec)
163 {
164 if (sec) {
165 add_color(colors,sec->color);
166 add_color(colors,sec->back_color);
167 add_font(fonts,sec->font);
168 }
169 }
170
171 static void scan_in_line_layout(psiconv_list colors,psiconv_list fonts,
172 psiconv_in_line_layout sec)
173 {
174 if (sec) {
175 scan_character_layout(colors,fonts,sec->layout);
176 }
177 }
178
179 static void scan_in_line_layouts(psiconv_list colors,psiconv_list fonts,
180 psiconv_in_line_layouts sec)
181 {
182 int i;
183 psiconv_in_line_layout layout;
184 if (sec) {
185 for (i = 0; i < psiconv_list_length(sec); i++) {
186 layout = psiconv_list_get(sec,i);
187 scan_in_line_layout(colors,fonts,layout);
188 }
189 }
190 }
191
192 static void scan_paragraph(psiconv_list colors,psiconv_list fonts,
193 psiconv_paragraph sec)
194 {
195 if (sec) {
196 scan_paragraph_layout(colors,fonts,sec->base_paragraph);
197 scan_character_layout(colors,fonts,sec->base_character);
198 scan_in_line_layouts(colors,fonts,sec->in_lines);
199 }
200 }
201
202 static void scan_text_and_layout(psiconv_list colors,psiconv_list fonts,
203 psiconv_text_and_layout sec)
204 {
205 int i;
206 psiconv_paragraph para;
207 if (sec) {
208 for (i = 0; i < psiconv_list_length(sec); i++) {
209 para = psiconv_list_get(sec,i);
210 scan_paragraph(colors,fonts,para);
211 }
212 }
213 }
214
215 static void scan_texted_section(psiconv_list colors,psiconv_list fonts,
216 psiconv_texted_section sec)
217 {
218 if (sec) {
219 scan_text_and_layout(colors,fonts,sec->paragraphs);
220 }
221 }
222
223 static void scan_page_header(psiconv_list colors,psiconv_list fonts,
224 psiconv_page_header sec)
225 {
226 if (sec) {
227 scan_paragraph_layout(colors,fonts,sec->base_paragraph_layout);
228 scan_character_layout(colors,fonts,sec->base_character_layout);
229 scan_texted_section(colors,fonts,sec->text);
230 }
231 }
232
233 static void scan_page_layout_section(psiconv_list colors,psiconv_list fonts,
234 psiconv_page_layout_section sec)
235 {
236 if (sec) {
237 scan_page_header(colors,fonts,sec->header);
238 scan_page_header(colors,fonts,sec->footer);
239 }
240 }
241
242 static void scan_word_style(psiconv_list colors,psiconv_list fonts,
243 psiconv_word_style sec)
244 {
245 if (sec) {
246 scan_character_layout(colors,fonts,sec->character);
247 scan_paragraph_layout(colors,fonts,sec->paragraph);
248 }
249 }
250
251 static void scan_word_style_list(psiconv_list colors,psiconv_list fonts,
252 psiconv_word_style_list sec)
253 {
254 int i;
255 psiconv_word_style style;
256 if (sec) {
257 for (i = 0; i < psiconv_list_length(sec); i++) {
258 style = psiconv_list_get(sec,i);
259 scan_word_style(colors,fonts,style);
260 }
261 }
262 }
263
264 static void scan_word_styles_section(psiconv_list colors,psiconv_list fonts,
265 psiconv_word_styles_section sec)
266 {
267 if (sec) {
268 scan_word_style(colors,fonts,sec->normal);
269 scan_word_style_list(colors,fonts,sec->styles);
270 }
271 }
272
273
274 static void scan_word_f(psiconv_list colors,psiconv_list fonts,
275 psiconv_word_f sec)
276 {
277 if (sec) {
278 scan_page_layout_section(colors,fonts,sec->page_sec);
279 scan_text_and_layout(colors,fonts,sec->paragraphs);
280 scan_word_styles_section(colors,fonts,sec->styles_sec);
281 }
282 }
283
284 static void scan_texted_f(psiconv_list colors,psiconv_list fonts,
285 psiconv_texted_f sec)
286 {
287 if (sec) {
288 scan_page_layout_section(colors,fonts,sec->page_sec);
289 scan_texted_section(colors,fonts,sec->texted_sec);
290 }
291 }
292
293 static void gen_font_table(FILE *of,psiconv_list fonts)
294 {
295 int i;
296 psiconv_font *font;
297
298 fprintf(of,"{\\fonttbl");
299 for (i = 0; i < psiconv_list_length(fonts); i++) {
300 font = psiconv_list_get(fonts,i);
301 fprintf(of,"{\\f%d",i);
302 if ((*font)->screenfont == psiconv_font_sansserif)
303 fprintf(of,"\\fswiss");
304 else if ((*font)->screenfont == psiconv_font_nonprop)
305 fprintf(of,"\\fmodern");
306 else if ((*font)->screenfont == psiconv_font_serif)
307 fprintf(of,"\\froman");
308 else
309 fprintf(of,"\\fnil");
310 fprintf(of,"\\cpg1252\\f%s;}",(*font)->name);
311 }
312 fprintf(of,"}\n");
313 }
314
315 static void gen_color_table(FILE *of, psiconv_list colors)
316 {
317 int i;
318 psiconv_color *color;
319
320 fprintf(of,"{\\colortbl");
321 for (i = 0; i < psiconv_list_length(colors); i++) {
322 color = psiconv_list_get(colors,i);
323 fprintf(of,"\\red%d\\green%d\\blue%d;",(*color)->red,
324 (*color)->green, (*color)->blue);
325 }
326 fprintf(of,"}\n");
327 }
328
329 /* This is not necessarily the same as returned by basic_character_layout_status
330 This one is specific for the base point of RTF */
331 static psiconv_character_layout
332 gen_base_char(psiconv_list colors, psiconv_list fonts)
333 {
334 struct psiconv_color_s white = { 0,0,0 };
335 struct psiconv_color_s black = { 0xff,0xff,0xff };
336 struct psiconv_font_s font = { NULL,-1 }; /* Pseudo - not added! */
337
338 struct psiconv_character_layout_s base_char_struct =
339 {
340 &black, /* color */
341 &white, /* back_color */
342 13.0, /* font_size */
343 psiconv_bool_false, /* italic */
344 psiconv_bool_false, /* bold */
345 psiconv_normalscript, /* super_sub */
346 psiconv_bool_false, /* underline */
347 psiconv_bool_false, /* strikethrough */
348 &font, /* font */
349 };
350
351 font.name = strdup("");
352 add_color(colors,&white);
353 add_color(colors,&black);
354
355 return psiconv_clone_character_layout(&base_char_struct);
356 }
357
358 static void diff_char(FILE *of,psiconv_list colors, psiconv_list fonts,
359 const psiconv_character_layout old,
360 const psiconv_character_layout new)
361 {
362 if ((old->font->screenfont != new->font->screenfont) ||
363 strcmp(old->font->name,new->font->name))
364 fprintf(of,"\\f%d",lookup_font(fonts,new->font));
365 if (old->font_size != new->font_size)
366 fprintf(of,"\\fs%d",(int) (new->font_size * 2));
367 if (old->super_sub != new->super_sub) {
368 if (new->super_sub == psiconv_normalscript)
369 fprintf(of,"\\nosupersub");
370 else if (new->super_sub == psiconv_superscript)
371 fprintf(of,"\\super");
372 else if (new->super_sub == psiconv_subscript)
373 fprintf(of,"\\sub");
374 }
375 if (old->bold != new->bold)
376 fprintf(of,"\\b%s",new->bold?"":"0");
377 if (old->italic != new->italic)
378 fprintf(of,"\\i%s",new->italic?"":"0");
379 if (old->underline != new->underline)
380 fprintf(of,"\\ul%s",new->underline?"":"0");
381 if (old->strikethrough != new->strikethrough)
382 fprintf(of,"\\strike%s",new->strikethrough?"":"0");
383 if ((old->color->red != new->color->red) ||
384 (old->color->green != new->color->green) ||
385 (old->color->blue != new->color->blue))
386 fprintf(of,"\\cf%d",lookup_color(colors,new->color));
387 if ((old->back_color->red != new->back_color->red) ||
388 (old->back_color->green != new->back_color->green) ||
389 (old->back_color->blue != new->back_color->blue))
390 fprintf(of,"\\cb%d",lookup_color(colors,new->back_color));
391 }
392
393
394 /* Note: this generates also some character formatting; Psion Word and
395 RTF do not agree on what is paragraph-level formatting and what is
396 character-level formatting. The character-level formatting is put
397 at the end. This should work out. */
398 static void gen_paragraph_codes(FILE *of, psiconv_list colors,
399 psiconv_list fonts,
400 psiconv_paragraph_layout para)
401 {
402 if (para->indent_first != 0.0)
403 fprintf(of,"\\fl%d",length_to_twips( para->indent_first));
404 if (para->indent_left != 0.0)
405 fprintf(of,"\\ll%d",length_to_twips(para->indent_left));
406 if (para->indent_right != 0.0)
407 fprintf(of,"\\rl%d",length_to_twips(para->indent_right));
408 if (para->justify_hor != psiconv_justify_left) {
409 if (para->justify_hor == psiconv_justify_right)
410 fprintf(of,"\\qr");
411 else if (para->justify_hor == psiconv_justify_centre)
412 fprintf(of,"\\qc");
413 else if (para->justify_hor == psiconv_justify_full)
414 fprintf(of,"\\qj");
415 /*
416 else if (para->justify_hor == psiconv_justify_left)
417 fprintf(of,"\\ql");
418 */
419 }
420 if (para->linespacing != 0.0)
421 fprintf(of,"\\sl%d",(para->linespacing_exact?-1:1) *
422 length_to_twips(para->linespacing));
423 if (para->space_above != 0.0)
424 fprintf(of,"\\sb%d",length_to_twips(para->space_above));
425 if (para->space_below != 0.0)
426 fprintf(of,"\\sa%d",length_to_twips(para->space_below));
427 if (para->keep_together)
428 fprintf(of,"\\keep");
429 if (para->keep_with_next)
430 fprintf(of,"\\keepn");
431 if (!para->on_next_page)
432 fprintf(of,"pagebb");
433 if (!para->no_widow_protection)
434 fprintf(of,"\\nowidctlpar");
435 }
436
437
438 static void gen_para(FILE *of, psiconv_list colors, psiconv_list fonts,
439 psiconv_paragraph para)
440 {
441 psiconv_character_layout cur_char,base_char;
442 psiconv_in_line_layout inl;
443 int loc,i,j;
444
445 fprintf(of,"\\par\\pard");
446 gen_paragraph_codes(of,colors,fonts,para->base_paragraph);
447
448 base_char = gen_base_char(colors,fonts);
449 cur_char = base_char;
450
451 if (psiconv_list_length(para->in_lines) == 0) {
452 diff_char(of,colors,fonts,cur_char,para->base_character);
453 cur_char = para->base_character;
454 }
455 loc = 0;
456
457 for (i = 0; i < psiconv_list_length(para->in_lines); i++) {
458 inl = psiconv_list_get(para->in_lines,i);
459 diff_char(of,colors,fonts,cur_char,inl->layout);
460 cur_char = inl->layout;
461 for (j = loc; j < inl->length + loc; j ++) {
462 fputs(char_table[(unsigned char) (para->text[j])],of);
463 }
464 loc = j;
465 }
466
467 if (loc < strlen(para->text)) {
468 diff_char(of,colors,fonts,cur_char,para->base_character);
469 cur_char = para->base_character;
470 for (j = loc; j < strlen(para->text); j ++) {
471 fputs(char_table[(unsigned char) (para->text[j])],of);
472 }
473 }
474
475 psiconv_free_character_layout(base_char);
476 }
477
478 static void gen_text(FILE *of, psiconv_list colors, psiconv_list fonts,
479 psiconv_text_and_layout paragraphs)
480 {
481 int i;
482 psiconv_paragraph para;
483 fprintf(of,"\\sect");
484 for (i = 0; i < psiconv_list_length(paragraphs); i++) {
485 para = psiconv_list_get(paragraphs,i);
486 gen_para(of,colors,fonts,para);
487 }
488 }
489
490 static void psiconv_gen_rtf_word(FILE * of, psiconv_word_f wf)
491 {
492 psiconv_list fonts;
493 psiconv_list colors;
494
495 fonts = psiconv_list_new(sizeof(psiconv_font));
496 colors = psiconv_list_new(sizeof(psiconv_color));
497 scan_word_f(colors,fonts,wf);
498
499 fputs("{\\rtf1\\ansi\n",of);
500 gen_font_table(of,fonts);
501 gen_color_table(of,colors);
502 gen_text(of,colors,fonts,wf->paragraphs);
503 fputs("}\n",of);
504
505 psiconv_list_free(fonts);
506 psiconv_list_free(colors);
507
508 }
509
510 static void psiconv_gen_rtf_texted(FILE * of, psiconv_texted_f tf)
511 {
512 psiconv_list fonts;
513 psiconv_list colors;
514
515 fonts = psiconv_list_new(sizeof(psiconv_font));
516 colors = psiconv_list_new(sizeof(psiconv_color));
517 scan_texted_f(colors,fonts,tf);
518
519 fputs("{\\rtf1\\ansi\n",of);
520 gen_font_table(of,fonts);
521 gen_color_table(of,colors);
522 gen_text(of,colors,fonts,tf->texted_sec->paragraphs);
523 fputs("}\n",of);
524
525 psiconv_list_free(fonts);
526 psiconv_list_free(colors);
527 }
528
529 static int psiconv_gen_rtf(const char *filename, const psiconv_file file,
530 const char *dest)
531 {
532 FILE *of = fopen(filename,"w");
533 if (! of)
534 return -1;
535
536 if (file->type == psiconv_word_file) {
537 psiconv_gen_rtf_word(of,(psiconv_word_f) file->file);
538 } else if (file->type == psiconv_texted_file) {
539 psiconv_gen_rtf_texted(of,(psiconv_texted_f) file->file);
540 } else {
541 fclose(of);
542 return -1;
543 }
544 return fclose(of);
545 }
546
547 static struct psiconv_fileformat_s ff =
548 {
549 "RTF",
550 "Rich Text Format (not functional yet!)",
551 psiconv_gen_rtf
552 };
553
554 void init_rtf(void)
555 {
556 /* psiconv_list_add(fileformat_list,&ff); */
557 }
558

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