/[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 8 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
79static void add_color(psiconv_list colors, psiconv_color color) 98static void add_color(psiconv_list colors, psiconv_color color)
80{ 99{
81 int i;
82 psiconv_color comp;
83 if (color) { 100 if (color)
84 for (i = 0; i < psiconv_list_length(colors); i ++) { 101 if (lookup_color(colors,color) < 0)
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); 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;
91 } 114 }
115 return -1;
92} 116}
93 117
94static void add_font(psiconv_list fonts, psiconv_font font) 118static void add_font(psiconv_list fonts, psiconv_font font)
95{ 119{
96 int i;
97 psiconv_font comp;
98 if (font) { 120 if (font)
99 for (i = 0; i < psiconv_list_length(fonts); i ++) { 121 if (lookup_font(fonts,font) < 0)
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); 122 psiconv_list_add(fonts,&font);
106 }
107} 123}
108 124
109static void scan_border(psiconv_list colors,psiconv_list fonts, 125static void scan_border(psiconv_list colors,psiconv_list fonts,
110 psiconv_border sec) 126 psiconv_border sec)
111{ 127{
266 scan_page_layout_section(colors,fonts,sec->page_sec); 282 scan_page_layout_section(colors,fonts,sec->page_sec);
267 scan_texted_section(colors,fonts,sec->texted_sec); 283 scan_texted_section(colors,fonts,sec->texted_sec);
268 } 284 }
269} 285}
270 286
271static gen_font_table(FILE *of,psiconv_list fonts) 287static void gen_font_table(FILE *of,psiconv_list fonts)
272{ 288{
273 int i; 289 int i;
274 psiconv_font *font; 290 psiconv_font *font;
275 291
276 fprintf(of,"{\\fonttbl"); 292 fprintf(of,"{\\fonttbl");
288 fprintf(of,"\\cpg1252\\f%s;}",(*font)->name); 304 fprintf(of,"\\cpg1252\\f%s;}",(*font)->name);
289 } 305 }
290 fprintf(of,"}\n"); 306 fprintf(of,"}\n");
291} 307}
292 308
293static gen_color_table(FILE *of, psiconv_list colors) 309static void gen_color_table(FILE *of, psiconv_list colors)
294{ 310{
295 int i; 311 int i;
296 psiconv_color *color; 312 psiconv_color *color;
297 313
298 fprintf(of,"{\\colortbl"); 314 fprintf(of,"{\\colortbl");
302 (*color)->green, (*color)->blue); 318 (*color)->green, (*color)->blue);
303 } 319 }
304 fprintf(of,"}\n"); 320 fprintf(of,"}\n");
305} 321}
306 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
307static void psiconv_gen_rtf_word(FILE * of, psiconv_word_f wf) 483static void psiconv_gen_rtf_word(FILE * of, psiconv_word_f wf)
308{ 484{
309 psiconv_list fonts; 485 psiconv_list fonts;
310 psiconv_list colors; 486 psiconv_list colors;
311 487
314 scan_word_f(colors,fonts,wf); 490 scan_word_f(colors,fonts,wf);
315 491
316 fputs("{\\rtf1\\ansi\n",of); 492 fputs("{\\rtf1\\ansi\n",of);
317 gen_font_table(of,fonts); 493 gen_font_table(of,fonts);
318 gen_color_table(of,colors); 494 gen_color_table(of,colors);
495 gen_text(of,colors,fonts,wf->paragraphs);
319 fputs("}\n",of); 496 fputs("}\n",of);
320 497
321 psiconv_list_free(fonts); 498 psiconv_list_free(fonts);
322 psiconv_list_free(colors); 499 psiconv_list_free(colors);
323 500
333 scan_texted_f(colors,fonts,tf); 510 scan_texted_f(colors,fonts,tf);
334 511
335 fputs("{\\rtf1\\ansi\n",of); 512 fputs("{\\rtf1\\ansi\n",of);
336 gen_font_table(of,fonts); 513 gen_font_table(of,fonts);
337 gen_color_table(of,colors); 514 gen_color_table(of,colors);
515 gen_text(of,colors,fonts,tf->texted_sec->paragraphs);
338 fputs("}\n",of); 516 fputs("}\n",of);
339 517
340 psiconv_list_free(fonts); 518 psiconv_list_free(fonts);
341 psiconv_list_free(colors); 519 psiconv_list_free(colors);
342} 520}

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

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