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 <stdio.h> |
23 |
#include <string.h> |
24 |
#include "data.h" |
25 |
#include "list.h" |
26 |
#include "gen.h" |
27 |
|
28 |
|
29 |
/* |
30 |
* Various string tables for RTF settings |
31 |
*/ |
32 |
|
33 |
/* Character conversion table */ |
34 |
/* TO BE ADJUSTED FOR RTF! */ |
35 |
static const char *char_table[0x100] = { |
36 |
/* 0x00 */ "", "", "", "", "", "", "\n", "\n", |
37 |
/* 0x08 */ "\n", "\t", "", "", "", "", "", "", |
38 |
/* 0x10 */ " ", "", "", "", "", "", "", "", |
39 |
/* 0x18 */ "", "", "", "", "", "", "", "", |
40 |
/* 0x20 */ " ", "!", "\"", "#", "$", "%", "&", "'", |
41 |
/* 0x28 */ "(", ")", "*", "+", ",", "-", ".", "/", |
42 |
/* 0x30 */ "0", "1", "2", "3", "4", "5", "6", "7", |
43 |
/* 0x38 */ "8", "9", ":", ";", "<", "=", ">", "?", |
44 |
/* 0x40 */ "@", "A", "B", "C", "D", "E", "F", "G", |
45 |
/* 0x48 */ "H", "I", "J", "K", "L", "M", "N", "O", |
46 |
/* 0x50 */ "P", "Q", "R", "S", "T", "U", "V", "W", |
47 |
/* 0x58 */ "X", "Y", "Z", "[", "\\", "]", "^", "_", |
48 |
/* 0x60 */ "`", "a", "b", "c", "d", "e", "f", "g", |
49 |
/* 0x68 */ "h", "i", "j", "k", "l", "m", "n", "o", |
50 |
/* 0x70 */ "p", "q", "r", "s", "t", "u", "v", "w", |
51 |
/* 0x78 */ "x", "y", "z", "{", "|", "}", "~", "", |
52 |
/* 0x80 */ "", "", ",", "f", ",,", "...", "+", "#", |
53 |
/* 0x88 */ "^", "\176/oo","S", "<", "OE", "", "", "", |
54 |
/* 0x90 */ "", "`", "'", "``", "''", "*", "-", "--", |
55 |
/* 0x98 */ "~", "(TM)", "s", ">", "oe", "", "", "Y", |
56 |
/* 0xa0 */ "\xa0", "\xa1", "\xa2", "\xa3", "\xa4", "\xa5", "\xa6", "\xa7", |
57 |
/* 0xa8 */ "\xa8", "\xa9", "\xaa", "\xab", "\xac", "\xad", "\xae", "\xaf", |
58 |
/* 0xb0 */ "\xb0", "\xb1", "\xb2", "\xb3", "\xb4", "\xb5", "\xb6", "\xb7", |
59 |
/* 0xb8 */ "\xb8", "\xb9", "\xba", "\xbb", "\xbc", "\xbd", "\xbe", "\xbf", |
60 |
/* 0xc0 */ "\xc0", "\xc1", "\xc2", "\xc3", "\xc4", "\xc5", "\xc6", "\xc7", |
61 |
/* 0xc8 */ "\xc8", "\xc9", "\xca", "\xcb", "\xcc", "\xcd", "\xce", "\xcf", |
62 |
/* 0xd0 */ "\xd0", "\xd1", "\xd2", "\xd3", "\xd4", "\xd5", "\xd6", "\xd7", |
63 |
/* 0xd8 */ "\xd8", "\xd9", "\xda", "\xdb", "\xdc", "\xdd", "\xde", "\xdf", |
64 |
/* 0xe0 */ "\xe0", "\xe1", "\xe2", "\xe3", "\xe4", "\xe5", "\xe6", "\xe7", |
65 |
/* 0xe8 */ "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef", |
66 |
/* 0xf0 */ "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7", |
67 |
/* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff", |
68 |
}; |
69 |
|
70 |
|
71 |
static void fput_text(FILE * of, const char *text, int length) { |
72 |
int j; |
73 |
|
74 |
for (j = 0; j < length; j++) { |
75 |
fputs(char_table[(unsigned char) (text[j])], of); |
76 |
} |
77 |
} |
78 |
|
79 |
static void add_color(psiconv_list colors, psiconv_color color) |
80 |
{ |
81 |
int i; |
82 |
psiconv_color comp; |
83 |
if (color) { |
84 |
for (i = 0; i < psiconv_list_length(colors); i ++) { |
85 |
comp = * (psiconv_color *) psiconv_list_get(colors,i); |
86 |
if ((comp->red == color->red) && (comp->green == color->green) && |
87 |
(comp->blue == color->blue)) |
88 |
return; |
89 |
} |
90 |
psiconv_list_add(colors,&color); |
91 |
} |
92 |
} |
93 |
|
94 |
static void add_font(psiconv_list fonts, psiconv_font font) |
95 |
{ |
96 |
int i; |
97 |
psiconv_font comp; |
98 |
if (font) { |
99 |
for (i = 0; i < psiconv_list_length(fonts); i ++) { |
100 |
comp = *(psiconv_font *) psiconv_list_get(fonts,i); |
101 |
if ((comp->screenfont == font->screenfont) && |
102 |
! strcmp(comp->name,font->name)) |
103 |
return; |
104 |
} |
105 |
psiconv_list_add(fonts,&font); |
106 |
} |
107 |
} |
108 |
|
109 |
static void scan_border(psiconv_list colors,psiconv_list fonts, |
110 |
psiconv_border sec) |
111 |
{ |
112 |
if (sec) { |
113 |
add_color(colors,sec->color); |
114 |
} |
115 |
} |
116 |
|
117 |
static void scan_bullet(psiconv_list colors,psiconv_list fonts, |
118 |
psiconv_bullet sec) |
119 |
{ |
120 |
if (sec) { |
121 |
add_color(colors,sec->color); |
122 |
add_font(fonts,sec->font); |
123 |
} |
124 |
} |
125 |
|
126 |
static void scan_paragraph_layout(psiconv_list colors,psiconv_list fonts, |
127 |
psiconv_paragraph_layout sec) |
128 |
{ |
129 |
if (sec) { |
130 |
add_color(colors,sec->back_color); |
131 |
scan_bullet(colors,fonts,sec->bullet); |
132 |
scan_border(colors,fonts,sec->left_border); |
133 |
scan_border(colors,fonts,sec->right_border); |
134 |
scan_border(colors,fonts,sec->top_border); |
135 |
scan_border(colors,fonts,sec->bottom_border); |
136 |
} |
137 |
} |
138 |
|
139 |
static void scan_character_layout(psiconv_list colors,psiconv_list fonts, |
140 |
psiconv_character_layout sec) |
141 |
{ |
142 |
if (sec) { |
143 |
add_color(colors,sec->color); |
144 |
add_color(colors,sec->back_color); |
145 |
add_font(fonts,sec->font); |
146 |
} |
147 |
} |
148 |
|
149 |
static void scan_in_line_layout(psiconv_list colors,psiconv_list fonts, |
150 |
psiconv_in_line_layout sec) |
151 |
{ |
152 |
if (sec) { |
153 |
scan_character_layout(colors,fonts,sec->layout); |
154 |
} |
155 |
} |
156 |
|
157 |
static void scan_in_line_layouts(psiconv_list colors,psiconv_list fonts, |
158 |
psiconv_in_line_layouts sec) |
159 |
{ |
160 |
int i; |
161 |
psiconv_in_line_layout layout; |
162 |
if (sec) { |
163 |
for (i = 0; i < psiconv_list_length(sec); i++) { |
164 |
layout = psiconv_list_get(sec,i); |
165 |
scan_in_line_layout(colors,fonts,layout); |
166 |
} |
167 |
} |
168 |
} |
169 |
|
170 |
static void scan_paragraph(psiconv_list colors,psiconv_list fonts, |
171 |
psiconv_paragraph sec) |
172 |
{ |
173 |
if (sec) { |
174 |
scan_paragraph_layout(colors,fonts,sec->base_paragraph); |
175 |
scan_character_layout(colors,fonts,sec->base_character); |
176 |
scan_in_line_layouts(colors,fonts,sec->in_lines); |
177 |
} |
178 |
} |
179 |
|
180 |
static void scan_text_and_layout(psiconv_list colors,psiconv_list fonts, |
181 |
psiconv_text_and_layout sec) |
182 |
{ |
183 |
int i; |
184 |
psiconv_paragraph para; |
185 |
if (sec) { |
186 |
for (i = 0; i < psiconv_list_length(sec); i++) { |
187 |
para = psiconv_list_get(sec,i); |
188 |
scan_paragraph(colors,fonts,para); |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
static void scan_texted_section(psiconv_list colors,psiconv_list fonts, |
194 |
psiconv_texted_section sec) |
195 |
{ |
196 |
if (sec) { |
197 |
scan_text_and_layout(colors,fonts,sec->paragraphs); |
198 |
} |
199 |
} |
200 |
|
201 |
static void scan_page_header(psiconv_list colors,psiconv_list fonts, |
202 |
psiconv_page_header sec) |
203 |
{ |
204 |
if (sec) { |
205 |
scan_paragraph_layout(colors,fonts,sec->base_paragraph_layout); |
206 |
scan_character_layout(colors,fonts,sec->base_character_layout); |
207 |
scan_texted_section(colors,fonts,sec->text); |
208 |
} |
209 |
} |
210 |
|
211 |
static void scan_page_layout_section(psiconv_list colors,psiconv_list fonts, |
212 |
psiconv_page_layout_section sec) |
213 |
{ |
214 |
if (sec) { |
215 |
scan_page_header(colors,fonts,sec->header); |
216 |
scan_page_header(colors,fonts,sec->footer); |
217 |
} |
218 |
} |
219 |
|
220 |
static void scan_word_style(psiconv_list colors,psiconv_list fonts, |
221 |
psiconv_word_style sec) |
222 |
{ |
223 |
if (sec) { |
224 |
scan_character_layout(colors,fonts,sec->character); |
225 |
scan_paragraph_layout(colors,fonts,sec->paragraph); |
226 |
} |
227 |
} |
228 |
|
229 |
static void scan_word_style_list(psiconv_list colors,psiconv_list fonts, |
230 |
psiconv_word_style_list sec) |
231 |
{ |
232 |
int i; |
233 |
psiconv_word_style style; |
234 |
if (sec) { |
235 |
for (i = 0; i < psiconv_list_length(sec); i++) { |
236 |
style = psiconv_list_get(sec,i); |
237 |
scan_word_style(colors,fonts,style); |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
static void scan_word_styles_section(psiconv_list colors,psiconv_list fonts, |
243 |
psiconv_word_styles_section sec) |
244 |
{ |
245 |
if (sec) { |
246 |
scan_word_style(colors,fonts,sec->normal); |
247 |
scan_word_style_list(colors,fonts,sec->styles); |
248 |
} |
249 |
} |
250 |
|
251 |
|
252 |
static void scan_word_f(psiconv_list colors,psiconv_list fonts, |
253 |
psiconv_word_f sec) |
254 |
{ |
255 |
if (sec) { |
256 |
scan_page_layout_section(colors,fonts,sec->page_sec); |
257 |
scan_text_and_layout(colors,fonts,sec->paragraphs); |
258 |
scan_word_styles_section(colors,fonts,sec->styles_sec); |
259 |
} |
260 |
} |
261 |
|
262 |
static void scan_texted_f(psiconv_list colors,psiconv_list fonts, |
263 |
psiconv_texted_f sec) |
264 |
{ |
265 |
if (sec) { |
266 |
scan_page_layout_section(colors,fonts,sec->page_sec); |
267 |
scan_texted_section(colors,fonts,sec->texted_sec); |
268 |
} |
269 |
} |
270 |
|
271 |
static gen_font_table(FILE *of,psiconv_list fonts) |
272 |
{ |
273 |
int i; |
274 |
psiconv_font *font; |
275 |
|
276 |
fprintf(of,"{\\fonttbl"); |
277 |
for (i = 0; i < psiconv_list_length(fonts); i++) { |
278 |
font = psiconv_list_get(fonts,i); |
279 |
fprintf(of,"{\\f%d",i); |
280 |
if ((*font)->screenfont == 1) |
281 |
fprintf(of,"\\fswiss"); |
282 |
else if ((*font)->screenfont == 2) |
283 |
fprintf(of,"\\fmodern"); |
284 |
else if ((*font)->screenfont == 3) |
285 |
fprintf(of,"\\froman"); |
286 |
else |
287 |
fprintf(of,"\\fnil"); |
288 |
fprintf(of,"\\cpg1252\\f%s;}",(*font)->name); |
289 |
} |
290 |
fprintf(of,"}\n"); |
291 |
} |
292 |
|
293 |
static gen_color_table(FILE *of, psiconv_list colors) |
294 |
{ |
295 |
int i; |
296 |
psiconv_color *color; |
297 |
|
298 |
fprintf(of,"{\\colortbl"); |
299 |
for (i = 0; i < psiconv_list_length(colors); i++) { |
300 |
color = psiconv_list_get(colors,i); |
301 |
fprintf(of,"\\red%d\\green%d\\blue%d;",(*color)->red, |
302 |
(*color)->green, (*color)->blue); |
303 |
} |
304 |
fprintf(of,"}\n"); |
305 |
} |
306 |
|
307 |
static void psiconv_gen_rtf_word(FILE * of, psiconv_word_f wf) |
308 |
{ |
309 |
psiconv_list fonts; |
310 |
psiconv_list colors; |
311 |
|
312 |
fonts = psiconv_list_new(sizeof(psiconv_font)); |
313 |
colors = psiconv_list_new(sizeof(psiconv_color)); |
314 |
scan_word_f(colors,fonts,wf); |
315 |
|
316 |
fputs("{\\rtf1\\ansi\n",of); |
317 |
gen_font_table(of,fonts); |
318 |
gen_color_table(of,colors); |
319 |
fputs("}\n",of); |
320 |
|
321 |
psiconv_list_free(fonts); |
322 |
psiconv_list_free(colors); |
323 |
|
324 |
} |
325 |
|
326 |
static void psiconv_gen_rtf_texted(FILE * of, psiconv_texted_f tf) |
327 |
{ |
328 |
psiconv_list fonts; |
329 |
psiconv_list colors; |
330 |
|
331 |
fonts = psiconv_list_new(sizeof(psiconv_font)); |
332 |
colors = psiconv_list_new(sizeof(psiconv_color)); |
333 |
scan_texted_f(colors,fonts,tf); |
334 |
|
335 |
fputs("{\\rtf1\\ansi\n",of); |
336 |
gen_font_table(of,fonts); |
337 |
gen_color_table(of,colors); |
338 |
fputs("}\n",of); |
339 |
|
340 |
psiconv_list_free(fonts); |
341 |
psiconv_list_free(colors); |
342 |
} |
343 |
|
344 |
void psiconv_gen_rtf(FILE * of, psiconv_file file) |
345 |
{ |
346 |
if (file->type == psiconv_word_file) |
347 |
psiconv_gen_rtf_word(of,(psiconv_word_f) file->file); |
348 |
else if (file->type == psiconv_texted_file) |
349 |
psiconv_gen_rtf_texted(of,(psiconv_texted_f) file->file); |
350 |
else |
351 |
return; |
352 |
} |
353 |
|