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

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

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

Revision 5 Revision 9
66 /* 0xf0 */ "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7", 66 /* 0xf0 */ "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7",
67 /* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff", 67 /* 0xf8 */ "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff",
68}; 68};
69 69
70 70
71
71static void fput_text(FILE * of, const char *text, int length) { 72static void fput_text(FILE * of, const char *text, int length) {
72 int j; 73 int j;
73 74
74 for (j = 0; j < length; j++) { 75 for (j = 0; j < length; j++) {
75 fputs(char_table[(unsigned char) (text[j])], of); 76 fputs(char_table[(unsigned char) (text[j])], of);
76 } 77 }
77} 78}
78 79
80static int length_to_twips(psiconv_length_t len)
81{
82 return 1440.0 / 2.54 * len;
83}
84
85static int lookup_color(psiconv_list colors,psiconv_color color)
86{
87 int i;
88 psiconv_color comp;
89 for (i = 0; i < psiconv_list_length(colors); i ++) {
90 comp = * (psiconv_color *) psiconv_list_get(colors,i);
91 if ((comp->red == color->red) && (comp->green == color->green) &&
92 (comp->blue == color->blue))
93 return i;
94 }
95 return -1;
96}
97
98static void add_color(psiconv_list colors, psiconv_color color)
99{
100 if (color)
101 if (lookup_color(colors,color) < 0)
102 psiconv_list_add(colors,&color);
103}
104
105static int lookup_font(psiconv_list fonts,psiconv_font font)
106{
107 int i;
108 psiconv_font comp;
109 for (i = 0; i < psiconv_list_length(fonts); i ++) {
110 comp = *(psiconv_font *) psiconv_list_get(fonts,i);
111 if ((comp->screenfont == font->screenfont) &&
112 ! strcmp(comp->name,font->name))
113 return i;
114 }
115 return -1;
116}
117
118static void add_font(psiconv_list fonts, psiconv_font font)
119{
120 if (font)
121 if (lookup_font(fonts,font) < 0)
122 psiconv_list_add(fonts,&font);
123}
124
125static void scan_border(psiconv_list colors,psiconv_list fonts,
126 psiconv_border sec)
127{
128 if (sec) {
129 add_color(colors,sec->color);
130 }
131}
132
133static void scan_bullet(psiconv_list colors,psiconv_list fonts,
134 psiconv_bullet sec)
135{
136 if (sec) {
137 add_color(colors,sec->color);
138 add_font(fonts,sec->font);
139 }
140}
141
142static void scan_paragraph_layout(psiconv_list colors,psiconv_list fonts,
143 psiconv_paragraph_layout sec)
144{
145 if (sec) {
146 add_color(colors,sec->back_color);
147 scan_bullet(colors,fonts,sec->bullet);
148 scan_border(colors,fonts,sec->left_border);
149 scan_border(colors,fonts,sec->right_border);
150 scan_border(colors,fonts,sec->top_border);
151 scan_border(colors,fonts,sec->bottom_border);
152 }
153}
154
155static void scan_character_layout(psiconv_list colors,psiconv_list fonts,
156 psiconv_character_layout sec)
157{
158 if (sec) {
159 add_color(colors,sec->color);
160 add_color(colors,sec->back_color);
161 add_font(fonts,sec->font);
162 }
163}
164
165static void scan_in_line_layout(psiconv_list colors,psiconv_list fonts,
166 psiconv_in_line_layout sec)
167{
168 if (sec) {
169 scan_character_layout(colors,fonts,sec->layout);
170 }
171}
172
173static void scan_in_line_layouts(psiconv_list colors,psiconv_list fonts,
174 psiconv_in_line_layouts sec)
175{
176 int i;
177 psiconv_in_line_layout layout;
178 if (sec) {
179 for (i = 0; i < psiconv_list_length(sec); i++) {
180 layout = psiconv_list_get(sec,i);
181 scan_in_line_layout(colors,fonts,layout);
182 }
183 }
184}
185
186static void scan_paragraph(psiconv_list colors,psiconv_list fonts,
187 psiconv_paragraph sec)
188{
189 if (sec) {
190 scan_paragraph_layout(colors,fonts,sec->base_paragraph);
191 scan_character_layout(colors,fonts,sec->base_character);
192 scan_in_line_layouts(colors,fonts,sec->in_lines);
193 }
194}
195
196static void scan_text_and_layout(psiconv_list colors,psiconv_list fonts,
197 psiconv_text_and_layout sec)
198{
199 int i;
200 psiconv_paragraph para;
201 if (sec) {
202 for (i = 0; i < psiconv_list_length(sec); i++) {
203 para = psiconv_list_get(sec,i);
204 scan_paragraph(colors,fonts,para);
205 }
206 }
207}
208
209static void scan_texted_section(psiconv_list colors,psiconv_list fonts,
210 psiconv_texted_section sec)
211{
212 if (sec) {
213 scan_text_and_layout(colors,fonts,sec->paragraphs);
214 }
215}
216
217static void scan_page_header(psiconv_list colors,psiconv_list fonts,
218 psiconv_page_header sec)
219{
220 if (sec) {
221 scan_paragraph_layout(colors,fonts,sec->base_paragraph_layout);
222 scan_character_layout(colors,fonts,sec->base_character_layout);
223 scan_texted_section(colors,fonts,sec->text);
224 }
225}
226
227static void scan_page_layout_section(psiconv_list colors,psiconv_list fonts,
228 psiconv_page_layout_section sec)
229{
230 if (sec) {
231 scan_page_header(colors,fonts,sec->header);
232 scan_page_header(colors,fonts,sec->footer);
233 }
234}
235
236static void scan_word_style(psiconv_list colors,psiconv_list fonts,
237 psiconv_word_style sec)
238{
239 if (sec) {
240 scan_character_layout(colors,fonts,sec->character);
241 scan_paragraph_layout(colors,fonts,sec->paragraph);
242 }
243}
244
245static void scan_word_style_list(psiconv_list colors,psiconv_list fonts,
246 psiconv_word_style_list sec)
247{
248 int i;
249 psiconv_word_style style;
250 if (sec) {
251 for (i = 0; i < psiconv_list_length(sec); i++) {
252 style = psiconv_list_get(sec,i);
253 scan_word_style(colors,fonts,style);
254 }
255 }
256}
257
258static void scan_word_styles_section(psiconv_list colors,psiconv_list fonts,
259 psiconv_word_styles_section sec)
260{
261 if (sec) {
262 scan_word_style(colors,fonts,sec->normal);
263 scan_word_style_list(colors,fonts,sec->styles);
264 }
265}
266
267
268static void scan_word_f(psiconv_list colors,psiconv_list fonts,
269 psiconv_word_f sec)
270{
271 if (sec) {
272 scan_page_layout_section(colors,fonts,sec->page_sec);
273 scan_text_and_layout(colors,fonts,sec->paragraphs);
274 scan_word_styles_section(colors,fonts,sec->styles_sec);
275 }
276}
277
278static void scan_texted_f(psiconv_list colors,psiconv_list fonts,
279 psiconv_texted_f sec)
280{
281 if (sec) {
282 scan_page_layout_section(colors,fonts,sec->page_sec);
283 scan_texted_section(colors,fonts,sec->texted_sec);
284 }
285}
286
287static void gen_font_table(FILE *of,psiconv_list fonts)
288{
289 int i;
290 psiconv_font *font;
291
292 fprintf(of,"{\\fonttbl");
293 for (i = 0; i < psiconv_list_length(fonts); i++) {
294 font = psiconv_list_get(fonts,i);
295 fprintf(of,"{\\f%d",i);
296 if ((*font)->screenfont == 1)
297 fprintf(of,"\\fswiss");
298 else if ((*font)->screenfont == 2)
299 fprintf(of,"\\fmodern");
300 else if ((*font)->screenfont == 3)
301 fprintf(of,"\\froman");
302 else
303 fprintf(of,"\\fnil");
304 fprintf(of,"\\cpg1252\\f%s;}",(*font)->name);
305 }
306 fprintf(of,"}\n");
307}
308
309static void gen_color_table(FILE *of, psiconv_list colors)
310{
311 int i;
312 psiconv_color *color;
313
314 fprintf(of,"{\\colortbl");
315 for (i = 0; i < psiconv_list_length(colors); i++) {
316 color = psiconv_list_get(colors,i);
317 fprintf(of,"\\red%d\\green%d\\blue%d;",(*color)->red,
318 (*color)->green, (*color)->blue);
319 }
320 fprintf(of,"}\n");
321}
322
323/* This is not necessarily the same as returned by basic_character_layout_status
324 This one is specific for the base point of RTF */
325static psiconv_character_layout
326 gen_base_char(psiconv_list colors, psiconv_list fonts)
327{
328 struct psiconv_color white = { 0,0,0 };
329 struct psiconv_color black = { 0xff,0xff,0xff };
330 struct psiconv_font font = { strdup(""),-1 }; /* Pseudo - not added! */
331
332 struct psiconv_character_layout base_char_struct =
333 {
334 &black, /* color */
335 &white, /* back_color */
336 13.0, /* font_size */
337 psiconv_bool_false, /* italic */
338 psiconv_bool_false, /* bold */
339 psiconv_normalscript, /* super_sub */
340 psiconv_bool_false, /* underline */
341 psiconv_bool_false, /* strike_out */
342 &font, /* font */
343 };
344
345 add_color(colors,&white);
346 add_color(colors,&black);
347
348 return psiconv_clone_character_layout(&base_char_struct);
349}
350
351void diff_char(FILE *of,psiconv_list colors, psiconv_list fonts,
352 const psiconv_character_layout old,
353 const psiconv_character_layout new)
354{
355 if ((old->font->screenfont != new->font->screenfont) ||
356 strcmp(old->font->name,new->font->name))
357 fprintf(of,"\\f%d",lookup_font(fonts,new->font));
358 if (old->font_size != new->font_size)
359 fprintf(of,"\\fs%d",(int) (new->font_size * 2));
360 if (old->super_sub != new->super_sub) {
361 if (new->super_sub == psiconv_normalscript)
362 fprintf(of,"\\nosupersub");
363 else if (new->super_sub == psiconv_superscript)
364 fprintf(of,"\\super");
365 else if (new->super_sub == psiconv_subscript)
366 fprintf(of,"\\sub");
367 }
368 if (old->bold != new->bold)
369 fprintf(of,"\\b%s",new->bold?"":"0");
370 if (old->italic != new->italic)
371 fprintf(of,"\\i%s",new->italic?"":"0");
372 if (old->underline != new->underline)
373 fprintf(of,"\\ul%s",new->underline?"":"0");
374 if (old->strike_out != new->strike_out)
375 fprintf(of,"\\strike%s",new->strike_out?"":"0");
376 if ((old->color->red != new->color->red) ||
377 (old->color->green != new->color->green) ||
378 (old->color->blue != new->color->blue))
379 fprintf(of,"\\cf%d",lookup_color(colors,new->color));
380 if ((old->back_color->red != new->back_color->red) ||
381 (old->back_color->green != new->back_color->green) ||
382 (old->back_color->blue != new->back_color->blue))
383 fprintf(of,"\\cb%d",lookup_color(colors,new->back_color));
384}
385
386
387/* Note: this generates also some character formatting; Psion Word and
388 RTF do not agree on what is paragraph-level formatting and what is
389 character-level formatting. The character-level formatting is put
390 at the end. This should work out. */
391static void gen_paragraph_codes(FILE *of, psiconv_list colors,
392 psiconv_list fonts,
393 psiconv_paragraph_layout para)
394{
395 if (para->indent_first != 0.0)
396 fprintf(of,"\\fl%d",length_to_twips( para->indent_first));
397 if (para->indent_left != 0.0)
398 fprintf(of,"\\ll%d",length_to_twips(para->indent_left));
399 if (para->indent_right != 0.0)
400 fprintf(of,"\\rl%d",length_to_twips(para->indent_right));
401 if (para->justify_hor != psiconv_justify_left) {
402 if (para->justify_hor == psiconv_justify_right)
403 fprintf(of,"\\qr");
404 else if (para->justify_hor == psiconv_justify_centre)
405 fprintf(of,"\\qc");
406 else if (para->justify_hor == psiconv_justify_full)
407 fprintf(of,"\\qj");
408/*
409 else if (para->justify_hor == psiconv_justify_left)
410 fprintf(of,"\\ql");
411*/
412 }
413 if (para->interline != 0.0)
414 fprintf(of,"\\sl%d",(para->interline_exact?-1:1) *
415 length_to_twips(para->interline));
416 if (para->top_space != 0.0)
417 fprintf(of,"\\sb%d",length_to_twips(para->top_space));
418 if (para->bottom_space != 0.0)
419 fprintf(of,"\\sa%d",length_to_twips(para->bottom_space));
420 if (para->on_one_page)
421 fprintf(of,"\\keep");
422 if (para->together_with)
423 fprintf(of,"\\keepn");
424 if (!para->on_next_page)
425 fprintf(of,"pagebb");
426 if (!para->no_widow_protection)
427 fprintf(of,"\\nowidctlpar");
428}
429
430
431static void gen_para(FILE *of, psiconv_list colors, psiconv_list fonts,
432 psiconv_paragraph para)
433{
434 psiconv_character_layout cur_char,base_char;
435 psiconv_in_line_layout inl;
436 int loc,i,j;
437
438 fprintf(of,"\\par\\pard");
439 gen_paragraph_codes(of,colors,fonts,para->base_paragraph);
440
441 base_char = gen_base_char(colors,fonts);
442 cur_char = base_char;
443
444 if (psiconv_list_length(para->in_lines) == 0) {
445 diff_char(of,colors,fonts,cur_char,para->base_character);
446 cur_char = para->base_character;
447 }
448 loc = 0;
449
450 for (i = 0; i < psiconv_list_length(para->in_lines); i++) {
451 inl = psiconv_list_get(para->in_lines,i);
452 diff_char(of,colors,fonts,cur_char,inl->layout);
453 cur_char = inl->layout;
454 for (j = loc; j < inl->length + loc; j ++) {
455 fputs(char_table[(unsigned char) (para->text[j])],of);
456 }
457 loc = j;
458 }
459
460 if (loc < strlen(para->text)) {
461 diff_char(of,colors,fonts,cur_char,para->base_character);
462 cur_char = para->base_character;
463 for (j = loc; j < strlen(para->text); j ++) {
464 fputs(char_table[(unsigned char) (para->text[j])],of);
465 }
466 }
467
468 psiconv_free_character_layout(base_char);
469}
470
471static void gen_text(FILE *of, psiconv_list colors, psiconv_list fonts,
472 psiconv_text_and_layout paragraphs)
473{
474 int i;
475 psiconv_paragraph para;
476 fprintf(of,"\\sect");
477 for (i = 0; i < psiconv_list_length(paragraphs); i++) {
478 para = psiconv_list_get(paragraphs,i);
479 gen_para(of,colors,fonts,para);
480 }
481}
482
79static void psiconv_gen_rtf_word(FILE * of, psiconv_word_f tf) 483static void psiconv_gen_rtf_word(FILE * of, psiconv_word_f wf)
80{ 484{
81 return; 485 psiconv_list fonts;
486 psiconv_list colors;
487
488 fonts = psiconv_list_new(sizeof(psiconv_font));
489 colors = psiconv_list_new(sizeof(psiconv_color));
490 scan_word_f(colors,fonts,wf);
491
492 fputs("{\\rtf1\\ansi\n",of);
493 gen_font_table(of,fonts);
494 gen_color_table(of,colors);
495 gen_text(of,colors,fonts,wf->paragraphs);
496 fputs("}\n",of);
497
498 psiconv_list_free(fonts);
499 psiconv_list_free(colors);
500
82} 501}
83 502
84static void psiconv_gen_rtf_texted(FILE * of, psiconv_texted_f tf) 503static void psiconv_gen_rtf_texted(FILE * of, psiconv_texted_f tf)
85{ 504{
86 return; 505 psiconv_list fonts;
506 psiconv_list colors;
507
508 fonts = psiconv_list_new(sizeof(psiconv_font));
509 colors = psiconv_list_new(sizeof(psiconv_color));
510 scan_texted_f(colors,fonts,tf);
511
512 fputs("{\\rtf1\\ansi\n",of);
513 gen_font_table(of,fonts);
514 gen_color_table(of,colors);
515 gen_text(of,colors,fonts,tf->texted_sec->paragraphs);
516 fputs("}\n",of);
517
518 psiconv_list_free(fonts);
519 psiconv_list_free(colors);
87} 520}
88 521
89void psiconv_gen_rtf(FILE * of, psiconv_file file) 522void psiconv_gen_rtf(FILE * of, psiconv_file file)
90{ 523{
91 if (file->type == psiconv_word_file) 524 if (file->type == psiconv_word_file)

Legend:
Removed from v.5  
changed lines
  Added in v.9

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