--- psiconv/trunk/program/psiconv/gen_rtf.c 1999/10/11 19:17:17 14 +++ psiconv/trunk/program/psiconv/gen_rtf.c 2004/02/04 12:19:09 196 @@ -1,7 +1,7 @@ /* * gen_rtf.c - Part of psiconv, a PSION 5 file formats converter * Copyright (c) 1999 Andrew Johnson - * Portions Copyright (c) 1999 Frodo Looijaard + * Portions Copyright (c) 1999-2004 Frodo Looijaard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,11 +19,18 @@ */ #include "config.h" +#include "compat.h" #include #include -#include "data.h" -#include "list.h" +#include "psiconv/data.h" +#include "psiconv/list.h" #include "gen.h" +#include "psiconv.h" + +#ifdef DMALLOC +#include "dmalloc.h" +#endif + /* @@ -67,13 +74,54 @@ /* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff", }; +static const char *utf_table[0x100] = { + /* 0x00 */ "", "", "", "", "", "", "\n", "\n", + /* 0x08 */ "\n", "\t", "", "", "", "", "", "", + /* 0x10 */ " ", "", "", "", "", "", "", "", + /* 0x18 */ "", "", "", "", "", "", "", "", + /* 0x20 */ " ", "!", "\"", "#", "$", "%", "&", "'", + /* 0x28 */ "(", ")", "*", "+", ",", "-", ".", "/", + /* 0x30 */ "0", "1", "2", "3", "4", "5", "6", "7", + /* 0x38 */ "8", "9", ":", ";", "<", "=", ">", "?", + /* 0x40 */ "@", "A", "B", "C", "D", "E", "F", "G", + /* 0x48 */ "H", "I", "J", "K", "L", "M", "N", "O", + /* 0x50 */ "P", "Q", "R", "S", "T", "U", "V", "W", + /* 0x58 */ "X", "Y", "Z", "[", "\\", "]", "^", "_", + /* 0x60 */ "`", "a", "b", "c", "d", "e", "f", "g", + /* 0x68 */ "h", "i", "j", "k", "l", "m", "n", "o", + /* 0x70 */ "p", "q", "r", "s", "t", "u", "v", "w", + /* 0x78 */ "x", "y", "z", "{", "|", "}", "~", "", + /* 0x80 */ "\x80", "\x81", "\x82", "\x83", "\x84", "\x85", "\x86", "\x87", + /* 0x88 */ "\x88", "\x89", "\x8a", "\x8b", "\x8c", "\x8d", "\x8e", "\x8f", + /* 0x90 */ "\x90", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96", "\x97", + /* 0x98 */ "\x98", "\x99", "\x9a", "\x9b", "\x9c", "\x9d", "\x9e", "\x9f", + /* 0xa0 */ "\xa0", "\xa1", "\xa2", "\xa3", "\xa4", "\xa5", "\xa6", "\xa7", + /* 0xa8 */ "\xa8", "\xa9", "\xaa", "\xab", "\xac", "\xad", "\xae", "\xaf", + /* 0xb0 */ "\xb0", "\xb1", "\xb2", "\xb3", "\xb4", "\xb5", "\xb6", "\xb7", + /* 0xb8 */ "\xb8", "\xb9", "\xba", "\xbb", "\xbc", "\xbd", "\xbe", "\xbf", + /* 0xc0 */ "\xc0", "\xc1", "\xc2", "\xc3", "\xc4", "\xc5", "\xc6", "\xc7", + /* 0xc8 */ "\xc8", "\xc9", "\xca", "\xcb", "\xcc", "\xcd", "\xce", "\xcf", + /* 0xd0 */ "\xd0", "\xd1", "\xd2", "\xd3", "\xd4", "\xd5", "\xd6", "\xd7", + /* 0xd8 */ "\xd8", "\xd9", "\xda", "\xdb", "\xdc", "\xdd", "\xde", "\xdf", + /* 0xe0 */ "\xe0", "\xe1", "\xe2", "\xe3", "\xe4", "\xe5", "\xe6", "\xe7", + /* 0xe8 */ "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef", + /* 0xf0 */ "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7", + /* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff", +}; +/* a flag to indicate the use of UTF8 */ +static psiconv_encoding encoding= PSICONV_ENCODING_CP1252; static void fput_text(FILE * of, const char *text, int length) { int j; - + char ** table; + if (encoding == PSICONV_ENCODING_UTF8){ + table = (char**) utf_table; + }else{ + table = (char**) char_table; + } for (j = 0; j < length; j++) { - fputs(char_table[(unsigned char) (text[j])], of); + fputs(table[(unsigned char) (text[j])], of); } } @@ -293,11 +341,11 @@ for (i = 0; i < psiconv_list_length(fonts); i++) { font = psiconv_list_get(fonts,i); fprintf(of,"{\\f%d",i); - if ((*font)->screenfont == 1) + if ((*font)->screenfont == psiconv_font_sansserif) fprintf(of,"\\fswiss"); - else if ((*font)->screenfont == 2) + else if ((*font)->screenfont == psiconv_font_nonprop) fprintf(of,"\\fmodern"); - else if ((*font)->screenfont == 3) + else if ((*font)->screenfont == psiconv_font_serif) fprintf(of,"\\froman"); else fprintf(of,"\\fnil"); @@ -325,11 +373,11 @@ static psiconv_character_layout gen_base_char(psiconv_list colors, psiconv_list fonts) { - struct psiconv_color white = { 0,0,0 }; - struct psiconv_color black = { 0xff,0xff,0xff }; - struct psiconv_font font = { strdup(""),-1 }; /* Pseudo - not added! */ + struct psiconv_color_s white = { 0,0,0 }; + struct psiconv_color_s black = { 0xff,0xff,0xff }; + struct psiconv_font_s font = { NULL,-1 }; /* Pseudo - not added! */ - struct psiconv_character_layout base_char_struct = + struct psiconv_character_layout_s base_char_struct = { &black, /* color */ &white, /* back_color */ @@ -338,17 +386,18 @@ psiconv_bool_false, /* bold */ psiconv_normalscript, /* super_sub */ psiconv_bool_false, /* underline */ - psiconv_bool_false, /* strike_out */ + psiconv_bool_false, /* strikethrough */ &font, /* font */ }; + font.name = strdup(""); add_color(colors,&white); add_color(colors,&black); return psiconv_clone_character_layout(&base_char_struct); } -void diff_char(FILE *of,psiconv_list colors, psiconv_list fonts, +static void diff_char(FILE *of,psiconv_list colors, psiconv_list fonts, const psiconv_character_layout old, const psiconv_character_layout new) { @@ -371,8 +420,8 @@ fprintf(of,"\\i%s",new->italic?"":"0"); if (old->underline != new->underline) fprintf(of,"\\ul%s",new->underline?"":"0"); - if (old->strike_out != new->strike_out) - fprintf(of,"\\strike%s",new->strike_out?"":"0"); + if (old->strikethrough != new->strikethrough) + fprintf(of,"\\strike%s",new->strikethrough?"":"0"); if ((old->color->red != new->color->red) || (old->color->green != new->color->green) || (old->color->blue != new->color->blue)) @@ -410,16 +459,16 @@ fprintf(of,"\\ql"); */ } - if (para->interline != 0.0) - fprintf(of,"\\sl%d",(para->interline_exact?-1:1) * - length_to_twips(para->interline)); - if (para->top_space != 0.0) - fprintf(of,"\\sb%d",length_to_twips(para->top_space)); - if (para->bottom_space != 0.0) - fprintf(of,"\\sa%d",length_to_twips(para->bottom_space)); - if (para->on_one_page) + if (para->linespacing != 0.0) + fprintf(of,"\\sl%d",(para->linespacing_exact?-1:1) * + length_to_twips(para->linespacing)); + if (para->space_above != 0.0) + fprintf(of,"\\sb%d",length_to_twips(para->space_above)); + if (para->space_below != 0.0) + fprintf(of,"\\sa%d",length_to_twips(para->space_below)); + if (para->keep_together) fprintf(of,"\\keep"); - if (para->together_with) + if (para->keep_with_next) fprintf(of,"\\keepn"); if (!para->on_next_page) fprintf(of,"pagebb"); @@ -435,6 +484,13 @@ psiconv_in_line_layout inl; int loc,i,j; + char ** table; + if (encoding == PSICONV_ENCODING_UTF8){ + table = (char**) utf_table; + }else{ + table = (char**) char_table; + } + fprintf(of,"\\par\\pard"); gen_paragraph_codes(of,colors,fonts,para->base_paragraph); @@ -452,7 +508,7 @@ diff_char(of,colors,fonts,cur_char,inl->layout); cur_char = inl->layout; for (j = loc; j < inl->length + loc; j ++) { - fputs(char_table[(unsigned char) (para->text[j])],of); + fputs(table[(unsigned char) (para->text[j])],of); } loc = j; } @@ -461,7 +517,7 @@ diff_char(of,colors,fonts,cur_char,para->base_character); cur_char = para->base_character; for (j = loc; j < strlen(para->text); j ++) { - fputs(char_table[(unsigned char) (para->text[j])],of); + fputs(table[(unsigned char) (para->text[j])],of); } } @@ -519,13 +575,34 @@ psiconv_list_free(colors); } -void psiconv_gen_rtf(FILE * of, psiconv_file file) +static int psiconv_gen_rtf(const char *filename, const psiconv_file file, + const char *dest, const psiconv_encoding encoding_type) { - if (file->type == psiconv_word_file) + FILE *of = fopen(filename,"w"); + if (! of) + return -1; + + encoding = encoding_type; + if (file->type == psiconv_word_file) { psiconv_gen_rtf_word(of,(psiconv_word_f) file->file); - else if (file->type == psiconv_texted_file) + } else if (file->type == psiconv_texted_file) { psiconv_gen_rtf_texted(of,(psiconv_texted_f) file->file); - else - return; + } else { + fclose(of); + return -1; + } + return fclose(of); +} + +static struct psiconv_fileformat_s ff = +{ + "RTF", + "Rich Text Format (not functional yet!)", + psiconv_gen_rtf +}; + +void init_rtf(void) +{ + /* psiconv_list_add(fileformat_list,&ff); */ }