Debian Codegraph @

reports/rtflib.li

raw
1 /*
2 * @progname rtflib.li
3 * @version 1.1
4 * @author Doug McCallum
5 * @category
6 * @output RTF
7 * @description
8 *
9 * RTF functions for implementing RTF output
10 * this allows generating Word or other
11 * documents directly.
12 *
13 */
14 global(rtf_termstring)
15 global(rtf_tcols)
16 global(rtf_row_width)
17 global(rtf_row_left)
18 global(rtf_set_cols)
19 global(rtf_col_sizes)
20 global(rtf_pointsize)
21 global(rtf_pstate)
22 global(rtf_curr_indent)
23 global(rtf_tstate)
24 global(rtf_cstate)
25 global(rtf_ccol)
26 global(twips) /* 20 points per inch */
27 global(rtf_bspace)
28 global(rtf_aspace)
29 global(rtf_ftn_last_tag) /* last footnote tag */
30 global(rtf_ftn_state)
31
32 /*
33 * Initialize the RTF state machine and variables
34 */
35
36 /*
37 * rtf_init(font)
38 * initialize the RTF state.
39 * set font as the default font to use (not working yet)
40 */
41 proc rtf_init(font)
42 {
43 set(twips, 1440)
44 list(rtf_pointsize)
45 setel(rtf_pointsize, 1, 24) /* style 1 at 12pt */
46 setel(rtf_pointsize, 2, 18) /* style 2 at 9pt */
47 setel(rtf_pointsize, 3, 20) /* style 3 at 10pt */
48 set(rtf_curr_indent, 0) /* no paragraph indent */
49
50 /* some table state */
51 set(rtf_ccol, 0)
52 set(rtf_pstate, 0)
53 set(rtf_cstate, 0)
54 list(rtf_col_sizes)
55 set(rtf_set_cols, 0)
56
57 /* all RTF files need this */
58 "{\\rtf1\\ansi\\deff2{\\fonttbl{\\f10\\fnil "
59 font
60 ";}}\n"
61 "{\\stylesheet{\\fs20\\basedon222\\snext0\\f10 Normal;}\n"
62 "{\\s1\\fs24\\basedon0\\snext3\\f10\\b\\sb240\\sa60 Heading;}\n"
63 "{\\s2\\basedon1\\fs20\\f10\\up Footnote;}\n"
64 "{\\s3\\basedon222\\snext3\\f10\\fs20\\sb120\\sa10 Text;}\n"
65 /* you can add new styles here */
66 "}\n"
67 set(rtf_termstring, "\n\\par}\n")
68 monthformat(4)
69 dayformat(0)
70 dateformat(5) /* nn-MON-yyyy */
71 }
72
73 /*
74 * rtf_open(file)
75 * just do some setup. If "file" defined, open it.
76 */
77 proc rtf_open(file)
78 {
79 if (file) {
80 newfile(file, 0)
81 }
82 call rtf_init("Palatino")
83 }
84
85 /*
86 * rtf_close()
87 * closes open tables and paragraphs then
88 * adds the closing bracket for the document
89 */
90 proc rtf_close()
91 {
92 call rtf_tend()
93 call rtf_pend()
94 rtf_termstring
95 }
96
97 /*
98 * rtf_set_page_size(height, width, left, right, top, bot)
99 * set the page size if non-standard size is desired.
100 * height and width are paper size
101 * left, right, top and bot are the margin sizes
102 * sizes in twips (20pts/inch :: 1440 == 1inch)
103 */
104 proc rtf_set_page_size(height, width, left, right, top, bot)
105 {
106 "\\paperw" d(width)
107 "\\paperh" d(height)
108 "\\margl" d(left)
109 "\\margr" d(right)
110 "\\margt" d(top)
111 "\\margb" d(bottom) nl()
112 }
113
114 /*
115 * rtf_newpage()
116 * insert a forced pagebreak at this point
117 */
118 proc rtf_newpage()
119 {
120 "\\page "
121 }
122
123 /*
124 * paragraph functions
125 * there are a number of options here
126 */
127
128 /*
129 * rtf_pstart(type)
130 * start a new paragraph with the style selected
131 */
132 proc rtf_pstart(type)
133 {
134 /* if in a paragraph, end it */
135 if (eq(rtf_pstate, 1)) {
136 call rtf_pend()
137 }
138 set(rtf_pstate, 1)
139 "\\pard {\\s" d(type)
140 if (ps, getel(rtf_pointsize, type)) {
141 "\\fs" d(ps) " "
142 }
143
144 call rtf_para_space(rtf_bspace, rtf_aspace)
145
146 if (gt(rtf_curr_indent, 0)) {
147 /* "next" paragraph */
148 call rtf_para_indent(0, rtf_curr_indent)
149 }
150 }
151
152 /*
153 * rtf_pend()
154 * end the current paragraph
155 * this is for completeness but a new pstart will do it
156 * so it is optional
157 */
158 proc rtf_pend()
159 {
160 if (eq(1, rtf_pstate)) {
161 "\\par}\n"
162 set(rtf_pstate, 0)
163 }
164 }
165
166 /*
167 * rtf_para_indent(first, all)
168 * tagged/indented paragraphs
169 * should be called right after a rtf_pstart()
170 * to select the indent of the first and remaining lines
171 * Note that the first line is also indented the same as
172 * all but can have more or less indent applied
173 * typical is to have first be the neg of the all
174 * a tab will make a hanging indent in that case
175 */
176 proc rtf_para_indent(first, all)
177 {
178 if (or(gt(first, 0), gt(all, 0))) {
179 "\\li" d(all) "\\fi"
180 d(first)
181 "\\tx" d(all) " "
182 }
183 set(rtf_curr_indent, all)
184 }
185
186 /*
187 * rtf_para_space(before, after)
188 * amount of white space before and after a paragraph
189 */
190 proc rtf_para_space(before, after)
191 {
192 if (rtf_pstate) {
193 if (ne(before, 0)) {
194 "\\sb" d(before)
195 } else {
196 "\\sb" d(mul(getel(rtf_pointsize, 3), 5))
197 }
198 if (ne(after, 0)) {
199 "\\sa" d(after)
200 }
201 }
202 set(rtf_bspace, before)
203 set(rtf_aspace, after)
204 }
205
206 /*
207 * rtf_para_keepnext()
208 * causes current paragraph to be kept on same page as
209 * the next paragraph
210 */
211 proc rtf_para_keepnext()
212 {
213 "\\keepn "
214 }
215
216 /*
217 * rtf_para_centered()
218 * make the current paragraph text be centered
219 */
220 proc rtf_para_centered()
221 {
222 "\\qc "
223 }
224
225 /*
226 * rtf_para_leftjust()
227 * make the current paragraph text left justified
228 */
229 proc rtf_para_leftjust()
230 {
231 "\\ql "
232 }
233
234 /*
235 * rtf_para_rightjust()
236 * make the current paragraph text right justified
237 */
238 proc rtf_para_rightjust()
239 {
240 "\\qr "
241 }
242
243 /*
244 * rtf_para_keepintact()
245 * don't try to break this paragraph across pages
246 */
247 proc rtf_para_keepintact()
248 {
249 "\\keep "
250 }
251
252 /*
253 * rtf_set_info(title, subject, author, operator, created)
254 * set the file's info section to have the values specified
255 */
256 proc rtf_set_info(title, subject, author, operator, created)
257 {
258 "{\\info\n"
259 if (title) {
260 "{\\title " title "}\n"
261 }
262 if (subject) {
263 "{\\subject " subject " }\n"
264 }
265 if (author) {
266 "{\\author " author "}\n"
267 }
268 if (operator) {
269 "{\\operator " operator "}\n"
270 }
271 if (created) {
272
273 set(yr, save(substring(created, 1, 4)))
274 set(mo, save(substring(created, 6, 7)))
275 set(dy, save(substring(created, 9, 10)))
276 "{\\creatim\\yr" yr
277 "\\mo" mo
278 "\\dy" dy "}\n"
279 }
280 "{\\doccomm Document generated from LifeLines "
281 version()
282 " database "
283 database() "by register-rtf 1.1.}\n"
284 "}\n"
285 }
286
287 /*
288 * table functions
289 * there are a number related to rows and cells
290 */
291
292 /*
293 * rtf_set_row_width(cols, wid)
294 * set the table row width and number of columns to expect
295 */
296 proc rtf_set_row_width(cols, wid)
297 {
298 set(rtf_tcols, cols)
299 set(rtf_row_width, sub(wid, mul(sub(rtf_tcols, 1), 108)))
300 set(rtf_row_left, rtf_row_width)
301 }
302
303 /*
304 * rtf_set_col_width(wid)
305 * set the current column width
306 * called once for each column defined
307 */
308 proc rtf_set_col_width(wid)
309 {
310 if (lt(rtf_set_cols, rtf_tcols)) {
311 setel(rtf_col_sizes, one(rtf_set_cols), wid)
312 incr(rtf_set_cols)
313 set(rtf_row_left, sub(rtf_row_left, wid))
314 set(i, rtf_set_cols)
315 while (lt(i, rtf_tcols)) {
316 setel(rtf_col_sizes, one(i),
317 div(rtf_row_left, sub(rtf_tcols, rtf_set_cols)))
318 incr(i)
319 }
320 }
321 }
322
323 /*
324 * rtf_tstart(cells)
325 * start table with cells per row
326 */
327 proc rtf_tstart(cells)
328 {
329 if (eq(rtf_tstate, 1)) {
330 call rtf_tend()
331 }
332 call rtf_pend()
333 "\\trowd "
334 set(rtf_tstate, 1)
335 "\\trgaph" d(108)
336 "\\trleft" d(neg(108))
337 set(i, 0)
338 set(cumwid, 0)
339 while (lt(i, cells)) {
340 if (gt(i, 0)) {
341 set(gap, 108)
342 } else {
343 set(gap, 0)
344 }
345 set(gap, add(gap, getel(rtf_col_sizes, one(i))))
346 set(cumwid, add(cumwid, gap))
347 "\\cellx" d(cumwid) "\n"
348 set(i, add(i, 1))
349 }
350 set(rtf_tcols, cells)
351 "\\pard\\plain\\s3\\intbl "
352 }
353
354 /*
355 * rtf_tend()
356 * end table
357 */
358 proc rtf_tend()
359 {
360 if (rtf_tstate) {
361 while (lt(rtf_ccol, rtf_tcols)) {
362 rtf_cend()
363 }
364 "\\intbl\\row\\pard\\s3 "
365 set(rtf_tstate, 0)
366 }
367 }
368
369 /*
370 * rtf_cstart()
371 * start a cell in a table
372 */
373 proc rtf_cstart()
374 {
375 if (rtf_cstate) {
376 call rtf_cend()
377 }
378 call rtf_pend()
379 "\\fs" d(getel(rtf_pointsize, 3))
380 set(rtf_cstate, 1)
381 }
382
383 /*
384 * rtf_cend()
385 * end a cell
386 */
387 proc rtf_cend()
388 {
389 if (or(rtf_cstate, rtf_tstate)) {
390 set(rtf_cstate, 0)
391 "\n\\cell "
392 set(rtf_ccol, add(rtf_ccol, 1))
393 }
394 }
395
396 /*
397 * rtf_cpar()
398 * insert a paragraph break inside a cell
399 */
400 proc rtf_cpar()
401 {
402 "\\par "
403 }
404
405 /*
406 * rtf_endrow()
407 * end a table row and get ready for next one
408 */
409 proc rtf_endrow()
410 {
411 if (rtf_tstate) {
412 while (lt(rtf_ccol, rtf_tcols)) {
413 call rtf_cend()
414 }
415 "\\pard\\s3\\inttbl\\row "
416 set(rtf_tstate, 0)
417 call rtf_tstart(rtf_tcols)
418 set(rtf_ccol, 0)
419 }
420 }
421
422 /* heading handling */
423
424 /*
425 * rtf_hstart()
426 * start a heading
427 */
428 proc rtf_hstart()
429 {
430 "\\sb" d(mul(getel(rtf_pointsize, 1), 12))
431 "\\sa" d(mul(getel(rtf_pointsize, 1), 6)) " "
432 "{\\tc\\s1\\b "
433 }
434
435 /*
436 * rtf_hend()
437 * end a heading
438 */
439 proc rtf_hend()
440 {
441 "\\b0}\n"
442 }
443
444 /*
445 * rtf_index(key, subkey, type)
446 * create an index entry
447 * if subkey is defined, a two level index is
448 * created. e.g.
449 * McCallum
450 * Charles 1
451 * the type is plain = 0, bold = 1 and italic = 2
452 */
453 func rtf_index(key, subkey, type)
454 {
455 if (eq(type, 0)) {
456 set(var, "}}\n")
457 } elsif (eq(type, 1)) {
458 set(var, "\\bxe}}\n")
459 } elsif (eq(type, 2)) {
460 set(var, "\\ixe}}\n")
461 }
462 set(ind, concat("{\\xe{\\v ", key))
463 if (subkey) {
464 set(inds, concat("\\:", subkey))
465 } else {
466 set(inds, "")
467 }
468 set(indy, concat(ind, inds, var))
469 return (indy)
470 }
471
472 /*
473 * rtf_header(type, page)
474 * create a header entry (as in header/footer)
475 * type is all pages = 0, left = 1 and right = 2
476 * page is where to place the page number
477 * no page number = 0, left side = 1, center = 2 and right = 3
478 */
479 proc rtf_header(type, page)
480 {
481 if (eq(page, 0)) {
482 set(pstr, "")
483 set(pastr, "")
484 } elsif (eq(page, 1)) {
485 set(pastr, "\\ql")
486 set(pstr, "\\chpgn")
487 } elsif (eq(page, 2)) {
488 set(pastr, "\\qc")
489 set(pstr, "\\chpgn")
490 } elsif (eq(page, 3)) {
491 set(pastr, "\\qr")
492 set(pstr, "\\chpgn")
493 }
494 if (eq(type, 0)) {
495 set(hstr, "\\header")
496 } elsif (eq(type, 1)) {
497 set(hstr, "\\headerl")
498 } elsif (eq(type, 2)) {
499 set(hstr, "\\headerr")
500 }
501 "{" hstr "\\pard\\plain\\s3" pastr "{\\plain " pstr "}\\par}\n"
502 }
503 /*
504 * rtf_footer(type, page)
505 * creates a footer.
506 * see rtf_header for details
507 */
508 proc rtf_footer(type, page)
509 {
510 if (eq(page, 0)) {
511 set(pstr, "")
512 set(pastr, "")
513 } elsif (eq(page, 1)) {
514 set(pastr, "\\ql")
515 set(pstr, "\\chpgn")
516 } elsif (eq(page, 2)) {
517 set(pastr, "\\qc")
518 set(pstr, "\\chpgn")
519 } elsif (eq(page, 3)) {
520 set(pastr, "\\qr")
521 set(pstr, "\\chpgn")
522 }
523 if (eq(type, 0)) {
524 set(hstr, "\\footer")
525 } elsif (eq(type, 1)) {
526 set(hstr, "\\footerl")
527 } elsif (eq(type, 2)) {
528 set(hstr, "\\footerr")
529 }
530 "{" hstr "\\pard\\plain\\s3" pastr "{\\plain " pstr "}\\par}\n"
531 }
532
533 /*
534 * rtf_ftn_type(type, postype)
535 * define the type(s) of footnotes/endnotes to use
536 */
537 proc rtf_ftn_type(type, postype)
538 {
539 "\\fet" d(type)
540 if (eq(type, 1)) {
541 if (eq(postype, 0)) {
542 "\\enddoc\\aenddoc"
543 } elsif (eq(postype, 1)) {
544 "\\endnotes\\aendnotes"
545 }
546 } elsif (eq(type, 2)) {
547 if (eq(postype, 0)) {
548 "\\aenddoc"
549 } elsif (eq(postype, 1)) {
550 "\\aendnotes"
551 }
552 }
553 "\n"
554 }
555
556 /*
557 * rtf_ftn_tag(tag)
558 * if tag is not null, it is a user defined tag
559 * if null, do an automatic generation of the tag
560 * In all cases, output it superscripted
561 */
562 proc rtf_ftn_tag(tag)
563 {
564 if (tag) {
565 set(rtf_ftn_last_tag, tag)
566 } else {
567 set(rtf_ftn_last_tag, "\\chftn")
568 }
569 "{\\up6 " rtf_ftn_last_tag "}"
570 }
571
572 /*
573 * rtf_ftn_start(tag)
574 * start a possibly tagged footnote
575 * must be closed with rtf_ftn_end()
576 */
577 proc rtf_ftn_start(tag)
578 {
579 if (rtf_ftn_state) {
580 call rtf_ftn_end()
581 }
582 call rtf_ftn_tag(tag)
583 "{\*\footnote\\pard\\plain\\s3\\fs"
584 d(getel(rtf_pointsize, 3))
585 "\\li-540\\fi540\\tx540 "
586 rtf_ftn_last_tag
587 "\tab "
588 set(rtf_ftn_state, 1)
589 }
590 /*
591 * rtf_ftn_end()
592 * close an open footnote.
593 */
594 proc rtf_ftn_end()
595 {
596 if (rtf_ftn_state) {
597 "}\n"
598 set(rtf_ftn_state, 0)
599 }
600 }
601
602 /*
603 * rtf_tab(type)
604 * issue a tab of appropriate type
605 */
606 proc rtf_tab(type)
607 {
608 if (eq(type, 0)) {
609 "\\tab "
610 } elsif (eq(type, 1)) {
611 "\\tqr "
612 } elsif (eq(type, 2)) {
613 "\\tqc "
614 }
615 }
616
617 /*
618 * rtf_bold(on)
619 * turn bold on/off
620 */
621 proc rtf_bold(on)
622 {
623 if (on) {
624 "\\b "
625 } else {
626 "\\b0 "
627 }
628 }
629
630 /*
631 * rtf_italic(on)
632 * turn italic on/off
633 */
634 proc rtf_italic(on)
635 {
636 if (on) {
637 "\\i "
638 } else {
639 "\\i0 "
640 }
641 }
642
643 /*
644 * rtf_underline(type)
645 * turn underline on/off
646 * if type == 0 off
647 * 1 == continuous, 2 == double, 3 == word, 4 == dotted
648 */
649 proc rtf_underline(type)
650 {
651 if (type) {
652 if (eq(type, 1)) { "\\ul " }
653 elsif (eq(type, 2)) { "\\uldb " }
654 elsif (eq(type, 3)) { "\\ulw " }
655 elsif (eq(type, 4)) { "\\uld " }
656 } else {
657 "\\ul0 "
658 }
659 }
660
661 /*
662 * rtf_super(on)
663 * turn superscript on/off
664 */
665 proc rtf_super(on)
666 {
667 if (on) {
668 "{\\up6"
669 if (ps, getel(rtf_pointsize, 3)) {
670 set(ps, sub(ps, 3))
671 "\\fs" d(ps)
672 }
673 " "
674 } else {
675 "}"
676 }
677 }
678
679 /*
680 * rtf_toc_entry(level, text)
681 * enter text as a Table of Contents entry at level
682 */
683 proc rtf_toc_entry(level, text)
684 {
685 "{\\tc\\tcl" d(level)
686 "{\\v " text "}}"
687 }
688
689 /*
690 * one(val)
691 * similar to incr() but returns the new value
692 */
693 func one(val)
694 {
695 return (add(val, 1))
696 }