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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 56 Revision 270
1/* 1/*
2 * gen_rtf.c - Part of psiconv, a PSION 5 file formats converter 2 * gen_rtf.c - Part of psiconv, a PSION 5 file formats converter
3 * Copyright (c) 1999 Andrew Johnson <anjohnson@iee.org> 3 * Copyright (c) 1999 Andrew Johnson <anjohnson@iee.org>
4 * Portions Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> 4 * Portions Copyright (c) 1999-2005 Frodo Looijaard <frodo@frodo.looijaard.name>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 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 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 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
20 20
21#include "config.h" 21#include "config.h"
22#include "compat.h" 22#include "compat.h"
23#include <stdio.h> 23#include <stdio.h>
24#include <string.h> 24#include <string.h>
25#include "data.h" 25#include "psiconv/data.h"
26#include "list.h" 26#include "psiconv/list.h"
27#include "gen.h" 27#include "gen.h"
28#include "psiconv.h" 28#include "psiconv.h"
29
30#ifdef DMALLOC
31#include "dmalloc.h"
32#endif
33
29 34
30 35
31/* 36/*
32 * Various string tables for RTF settings 37 * Various string tables for RTF settings
33 */ 38 */
67 /* 0xe8 */ "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef", 72 /* 0xe8 */ "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef",
68 /* 0xf0 */ "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7", 73 /* 0xf0 */ "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7",
69 /* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff", 74 /* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff",
70}; 75};
71 76
77static const char *utf_table[0x100] = {
78 /* 0x00 */ "", "", "", "", "", "", "\n", "\n",
79 /* 0x08 */ "\n", "\t", "", "", "", "", "", "",
80 /* 0x10 */ " ", "", "", "", "", "", "", "",
81 /* 0x18 */ "", "", "", "", "", "", "", "",
82 /* 0x20 */ " ", "!", "\"", "#", "$", "%", "&", "'",
83 /* 0x28 */ "(", ")", "*", "+", ",", "-", ".", "/",
84 /* 0x30 */ "0", "1", "2", "3", "4", "5", "6", "7",
85 /* 0x38 */ "8", "9", ":", ";", "<", "=", ">", "?",
86 /* 0x40 */ "@", "A", "B", "C", "D", "E", "F", "G",
87 /* 0x48 */ "H", "I", "J", "K", "L", "M", "N", "O",
88 /* 0x50 */ "P", "Q", "R", "S", "T", "U", "V", "W",
89 /* 0x58 */ "X", "Y", "Z", "[", "\\", "]", "^", "_",
90 /* 0x60 */ "`", "a", "b", "c", "d", "e", "f", "g",
91 /* 0x68 */ "h", "i", "j", "k", "l", "m", "n", "o",
92 /* 0x70 */ "p", "q", "r", "s", "t", "u", "v", "w",
93 /* 0x78 */ "x", "y", "z", "{", "|", "}", "~", "",
94 /* 0x80 */ "\x80", "\x81", "\x82", "\x83", "\x84", "\x85", "\x86", "\x87",
95 /* 0x88 */ "\x88", "\x89", "\x8a", "\x8b", "\x8c", "\x8d", "\x8e", "\x8f",
96 /* 0x90 */ "\x90", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96", "\x97",
97 /* 0x98 */ "\x98", "\x99", "\x9a", "\x9b", "\x9c", "\x9d", "\x9e", "\x9f",
98 /* 0xa0 */ "\xa0", "\xa1", "\xa2", "\xa3", "\xa4", "\xa5", "\xa6", "\xa7",
99 /* 0xa8 */ "\xa8", "\xa9", "\xaa", "\xab", "\xac", "\xad", "\xae", "\xaf",
100 /* 0xb0 */ "\xb0", "\xb1", "\xb2", "\xb3", "\xb4", "\xb5", "\xb6", "\xb7",
101 /* 0xb8 */ "\xb8", "\xb9", "\xba", "\xbb", "\xbc", "\xbd", "\xbe", "\xbf",
102 /* 0xc0 */ "\xc0", "\xc1", "\xc2", "\xc3", "\xc4", "\xc5", "\xc6", "\xc7",
103 /* 0xc8 */ "\xc8", "\xc9", "\xca", "\xcb", "\xcc", "\xcd", "\xce", "\xcf",
104 /* 0xd0 */ "\xd0", "\xd1", "\xd2", "\xd3", "\xd4", "\xd5", "\xd6", "\xd7",
105 /* 0xd8 */ "\xd8", "\xd9", "\xda", "\xdb", "\xdc", "\xdd", "\xde", "\xdf",
106 /* 0xe0 */ "\xe0", "\xe1", "\xe2", "\xe3", "\xe4", "\xe5", "\xe6", "\xe7",
107 /* 0xe8 */ "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef",
108 /* 0xf0 */ "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7",
109 /* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff",
110};
72 111
112/* a flag to indicate the use of UTF8 */
113static psiconv_encoding encoding= PSICONV_ENCODING_CP1252;
73 114
74static void fput_text(FILE * of, const char *text, int length) { 115static void fput_text(FILE * of, const char *text, int length) {
75 int j; 116 int j;
117 char ** table;
118 if (encoding == PSICONV_ENCODING_UTF8){
119 table = (char**) utf_table;
120 }else{
121 table = (char**) char_table;
76 122 }
77 for (j = 0; j < length; j++) { 123 for (j = 0; j < length; j++) {
78 fputs(char_table[(unsigned char) (text[j])], of); 124 fputs(table[(unsigned char) (text[j])], of);
79 } 125 }
80} 126}
81 127
82static int length_to_twips(psiconv_length_t len) 128static int length_to_twips(psiconv_length_t len)
83{ 129{
293 339
294 fprintf(of,"{\\fonttbl"); 340 fprintf(of,"{\\fonttbl");
295 for (i = 0; i < psiconv_list_length(fonts); i++) { 341 for (i = 0; i < psiconv_list_length(fonts); i++) {
296 font = psiconv_list_get(fonts,i); 342 font = psiconv_list_get(fonts,i);
297 fprintf(of,"{\\f%d",i); 343 fprintf(of,"{\\f%d",i);
298 if ((*font)->screenfont == 1) 344 if ((*font)->screenfont == psiconv_font_sansserif)
299 fprintf(of,"\\fswiss"); 345 fprintf(of,"\\fswiss");
300 else if ((*font)->screenfont == 2) 346 else if ((*font)->screenfont == psiconv_font_nonprop)
301 fprintf(of,"\\fmodern"); 347 fprintf(of,"\\fmodern");
302 else if ((*font)->screenfont == 3) 348 else if ((*font)->screenfont == psiconv_font_serif)
303 fprintf(of,"\\froman"); 349 fprintf(of,"\\froman");
304 else 350 else
305 fprintf(of,"\\fnil"); 351 fprintf(of,"\\fnil");
306 fprintf(of,"\\cpg1252\\f%s;}",(*font)->name); 352 fprintf(of,"\\cpg1252\\f%s;}",(*font)->name);
307 } 353 }
338 13.0, /* font_size */ 384 13.0, /* font_size */
339 psiconv_bool_false, /* italic */ 385 psiconv_bool_false, /* italic */
340 psiconv_bool_false, /* bold */ 386 psiconv_bool_false, /* bold */
341 psiconv_normalscript, /* super_sub */ 387 psiconv_normalscript, /* super_sub */
342 psiconv_bool_false, /* underline */ 388 psiconv_bool_false, /* underline */
343 psiconv_bool_false, /* strike_out */ 389 psiconv_bool_false, /* strikethrough */
344 &font, /* font */ 390 &font, /* font */
345 }; 391 };
346 392
347 font.name = strdup(""); 393 font.name = strdup("");
348 add_color(colors,&white); 394 add_color(colors,&white);
372 fprintf(of,"\\b%s",new->bold?"":"0"); 418 fprintf(of,"\\b%s",new->bold?"":"0");
373 if (old->italic != new->italic) 419 if (old->italic != new->italic)
374 fprintf(of,"\\i%s",new->italic?"":"0"); 420 fprintf(of,"\\i%s",new->italic?"":"0");
375 if (old->underline != new->underline) 421 if (old->underline != new->underline)
376 fprintf(of,"\\ul%s",new->underline?"":"0"); 422 fprintf(of,"\\ul%s",new->underline?"":"0");
377 if (old->strike_out != new->strike_out) 423 if (old->strikethrough != new->strikethrough)
378 fprintf(of,"\\strike%s",new->strike_out?"":"0"); 424 fprintf(of,"\\strike%s",new->strikethrough?"":"0");
379 if ((old->color->red != new->color->red) || 425 if ((old->color->red != new->color->red) ||
380 (old->color->green != new->color->green) || 426 (old->color->green != new->color->green) ||
381 (old->color->blue != new->color->blue)) 427 (old->color->blue != new->color->blue))
382 fprintf(of,"\\cf%d",lookup_color(colors,new->color)); 428 fprintf(of,"\\cf%d",lookup_color(colors,new->color));
383 if ((old->back_color->red != new->back_color->red) || 429 if ((old->back_color->red != new->back_color->red) ||
411/* 457/*
412 else if (para->justify_hor == psiconv_justify_left) 458 else if (para->justify_hor == psiconv_justify_left)
413 fprintf(of,"\\ql"); 459 fprintf(of,"\\ql");
414*/ 460*/
415 } 461 }
416 if (para->interline != 0.0) 462 if (para->linespacing != 0.0)
417 fprintf(of,"\\sl%d",(para->interline_exact?-1:1) * 463 fprintf(of,"\\sl%d",(para->linespacing_exact?-1:1) *
418 length_to_twips(para->interline)); 464 length_to_twips(para->linespacing));
419 if (para->top_space != 0.0) 465 if (para->space_above != 0.0)
420 fprintf(of,"\\sb%d",length_to_twips(para->top_space)); 466 fprintf(of,"\\sb%d",length_to_twips(para->space_above));
421 if (para->bottom_space != 0.0) 467 if (para->space_below != 0.0)
422 fprintf(of,"\\sa%d",length_to_twips(para->bottom_space)); 468 fprintf(of,"\\sa%d",length_to_twips(para->space_below));
423 if (para->on_one_page) 469 if (para->keep_together)
424 fprintf(of,"\\keep"); 470 fprintf(of,"\\keep");
425 if (para->together_with) 471 if (para->keep_with_next)
426 fprintf(of,"\\keepn"); 472 fprintf(of,"\\keepn");
427 if (!para->on_next_page) 473 if (!para->on_next_page)
428 fprintf(of,"pagebb"); 474 fprintf(of,"pagebb");
429 if (!para->no_widow_protection) 475 if (!para->no_widow_protection)
430 fprintf(of,"\\nowidctlpar"); 476 fprintf(of,"\\nowidctlpar");
436{ 482{
437 psiconv_character_layout cur_char,base_char; 483 psiconv_character_layout cur_char,base_char;
438 psiconv_in_line_layout inl; 484 psiconv_in_line_layout inl;
439 int loc,i,j; 485 int loc,i,j;
440 486
487 char ** table;
488 if (encoding == PSICONV_ENCODING_UTF8){
489 table = (char**) utf_table;
490 }else{
491 table = (char**) char_table;
492 }
493
441 fprintf(of,"\\par\\pard"); 494 fprintf(of,"\\par\\pard");
442 gen_paragraph_codes(of,colors,fonts,para->base_paragraph); 495 gen_paragraph_codes(of,colors,fonts,para->base_paragraph);
443 496
444 base_char = gen_base_char(colors,fonts); 497 base_char = gen_base_char(colors,fonts);
445 cur_char = base_char; 498 cur_char = base_char;
453 for (i = 0; i < psiconv_list_length(para->in_lines); i++) { 506 for (i = 0; i < psiconv_list_length(para->in_lines); i++) {
454 inl = psiconv_list_get(para->in_lines,i); 507 inl = psiconv_list_get(para->in_lines,i);
455 diff_char(of,colors,fonts,cur_char,inl->layout); 508 diff_char(of,colors,fonts,cur_char,inl->layout);
456 cur_char = inl->layout; 509 cur_char = inl->layout;
457 for (j = loc; j < inl->length + loc; j ++) { 510 for (j = loc; j < inl->length + loc; j ++) {
458 fputs(char_table[(unsigned char) (para->text[j])],of); 511 fputs(table[(unsigned char) (para->text[j])],of);
459 } 512 }
460 loc = j; 513 loc = j;
461 } 514 }
462 515
463 if (loc < strlen(para->text)) { 516 if (loc < strlen(para->text)) {
464 diff_char(of,colors,fonts,cur_char,para->base_character); 517 diff_char(of,colors,fonts,cur_char,para->base_character);
465 cur_char = para->base_character; 518 cur_char = para->base_character;
466 for (j = loc; j < strlen(para->text); j ++) { 519 for (j = loc; j < strlen(para->text); j ++) {
467 fputs(char_table[(unsigned char) (para->text[j])],of); 520 fputs(table[(unsigned char) (para->text[j])],of);
468 } 521 }
469 } 522 }
470 523
471 psiconv_free_character_layout(base_char); 524 psiconv_free_character_layout(base_char);
472} 525}
521 psiconv_list_free(fonts); 574 psiconv_list_free(fonts);
522 psiconv_list_free(colors); 575 psiconv_list_free(colors);
523} 576}
524 577
525static int psiconv_gen_rtf(const char *filename, const psiconv_file file, 578static int psiconv_gen_rtf(const char *filename, const psiconv_file file,
526 const char *dest) 579 const char *dest, const psiconv_encoding encoding_type)
527{ 580{
528 FILE *of = fopen(filename,"w"); 581 FILE *of = fopen(filename,"w");
529 if (! of) 582 if (! of)
530 return -1; 583 return -1;
531 584
585 encoding = encoding_type;
532 if (file->type == psiconv_word_file) { 586 if (file->type == psiconv_word_file) {
533 psiconv_gen_rtf_word(of,(psiconv_word_f) file->file); 587 psiconv_gen_rtf_word(of,(psiconv_word_f) file->file);
534 } else if (file->type == psiconv_texted_file) { 588 } else if (file->type == psiconv_texted_file) {
535 psiconv_gen_rtf_texted(of,(psiconv_texted_f) file->file); 589 psiconv_gen_rtf_texted(of,(psiconv_texted_f) file->file);
536 } else { 590 } else {

Legend:
Removed from v.56  
changed lines
  Added in v.270

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