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