]> git.cameronkatri.com Git - mandoc.git/blob - term.c
Missing prototype for getsubopt() on NetBSD fixed.
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.144 2010/06/08 09:20:08 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <getopt.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "mandoc.h"
33 #include "chars.h"
34 #include "out.h"
35 #include "term.h"
36 #include "man.h"
37 #include "mdoc.h"
38 #include "main.h"
39
40 #define PS_CHAR_WIDTH 6
41 #define PS_CHAR_HEIGHT 12
42 #define PS_CHAR_TOPMARG (792 - 24)
43 #define PS_CHAR_TOP (PS_CHAR_TOPMARG - 36)
44 #define PS_CHAR_LEFT 36
45 #define PS_CHAR_BOTMARG 24
46 #define PS_CHAR_BOT (PS_CHAR_BOTMARG + 36)
47
48 static struct termp *alloc(char *, enum termenc, enum termtype);
49 static void term_free(struct termp *);
50 static void spec(struct termp *, const char *, size_t);
51 static void res(struct termp *, const char *, size_t);
52 static void buffera(struct termp *, const char *, size_t);
53 static void bufferc(struct termp *, char);
54 static void adjbuf(struct termp *p, size_t);
55 static void encode(struct termp *, const char *, size_t);
56 static void advance(struct termp *, size_t);
57 static void endline(struct termp *);
58 static void letter(struct termp *, char);
59 static void pageopen(struct termp *);
60
61
62 void *
63 ascii_alloc(char *outopts)
64 {
65
66 return(alloc(outopts, TERMENC_ASCII, TERMTYPE_CHAR));
67 }
68
69
70 void *
71 ps_alloc(void)
72 {
73
74 return(alloc(NULL, TERMENC_ASCII, TERMTYPE_PS));
75 }
76
77
78 void
79 terminal_free(void *arg)
80 {
81
82 term_free((struct termp *)arg);
83 }
84
85
86 static void
87 term_free(struct termp *p)
88 {
89
90 if (p->buf)
91 free(p->buf);
92 if (p->symtab)
93 chars_free(p->symtab);
94 free(p);
95 }
96
97
98 /*
99 * Push a single letter into our output engine.
100 */
101 static void
102 letter(struct termp *p, char c)
103 {
104
105 if (TERMTYPE_CHAR == p->type) {
106 /*
107 * If using the terminal device, just push the letter
108 * out into the screen.
109 */
110 putchar(c);
111 return;
112 }
113
114 if ( ! (PS_INLINE & p->psstate)) {
115 /*
116 * If we're not in a PostScript "word" context, then
117 * open one now at the current cursor.
118 */
119 printf("%zu %zu moveto\n", p->pscol, p->psrow);
120 putchar('(');
121 p->psstate |= PS_INLINE;
122 }
123
124 /*
125 * We need to escape these characters as per the PostScript
126 * specification. We would also escape non-graphable characters
127 * (like tabs), but none of them would get to this point and
128 * it's superfluous to abort() on them.
129 */
130
131 switch (c) {
132 case ('('):
133 /* FALLTHROUGH */
134 case (')'):
135 /* FALLTHROUGH */
136 case ('\\'):
137 putchar('\\');
138 break;
139 default:
140 break;
141 }
142
143 /* Write the character and adjust where we are on the page. */
144 putchar(c);
145 p->pscol += PS_CHAR_WIDTH;
146 }
147
148
149 /*
150 * Begin a "terminal" context. Since terminal encompasses PostScript,
151 * the actual terminal, etc., there are a few things we can do here.
152 */
153 void
154 term_begin(struct termp *p, term_margin head,
155 term_margin foot, const void *arg)
156 {
157
158 p->headf = head;
159 p->footf = foot;
160 p->argf = arg;
161
162 if (TERMTYPE_CHAR == p->type) {
163 /* Emit the header and be done. */
164 (*p->headf)(p, p->argf);
165 return;
166 }
167
168 /*
169 * Emit the standard PostScript prologue, set our initial page
170 * position, then run pageopen() on the initial page.
171 */
172
173 printf("%s\n", "%!PS");
174 printf("%s\n", "/Courier");
175 printf("%s\n", "10 selectfont");
176
177 p->pspage = 1;
178 p->psstate = 0;
179 pageopen(p);
180 }
181
182
183 /*
184 * Open a page. This is only used for -Tps at the moment. It opens a
185 * page context, printing the header and the footer. THE OUTPUT BUFFER
186 * MUST BE EMPTY. If it is not, output will ghost on the next line and
187 * we'll be all gross and out of state.
188 */
189 static void
190 pageopen(struct termp *p)
191 {
192
193 assert(TERMTYPE_PS == p->type);
194 assert(0 == p->psstate);
195
196 p->pscol = PS_CHAR_LEFT;
197 p->psrow = PS_CHAR_TOPMARG;
198 p->psstate |= PS_MARGINS;
199
200 (*p->headf)(p, p->argf);
201 endline(p);
202
203 p->psstate &= ~PS_MARGINS;
204 assert(0 == p->psstate);
205
206 p->pscol = PS_CHAR_LEFT;
207 p->psrow = PS_CHAR_BOTMARG;
208 p->psstate |= PS_MARGINS;
209
210 (*p->footf)(p, p->argf);
211 endline(p);
212
213 p->psstate &= ~PS_MARGINS;
214 assert(0 == p->psstate);
215
216 p->pscol = PS_CHAR_LEFT;
217 p->psrow = PS_CHAR_TOP;
218
219 }
220
221
222 void
223 term_end(struct termp *p)
224 {
225
226 if (TERMTYPE_CHAR == p->type) {
227 (*p->footf)(p, p->argf);
228 return;
229 }
230
231 printf("%s\n", "%%END");
232 }
233
234
235 static void
236 endline(struct termp *p)
237 {
238
239 if (TERMTYPE_CHAR == p->type) {
240 putchar('\n');
241 return;
242 }
243
244 if (PS_INLINE & p->psstate) {
245 printf(") show\n");
246 p->psstate &= ~PS_INLINE;
247 }
248
249 if (PS_MARGINS & p->psstate)
250 return;
251
252 p->pscol = PS_CHAR_LEFT;
253 if (p->psrow >= PS_CHAR_HEIGHT + PS_CHAR_BOT) {
254 p->psrow -= PS_CHAR_HEIGHT;
255 return;
256 }
257
258 /*
259 * XXX: can't run pageopen() until we're certain a flushln() has
260 * occured, else the buf will reopen in an awkward state on the
261 * next line.
262 */
263 printf("showpage\n");
264 p->psrow = PS_CHAR_TOP;
265 }
266
267
268 /*
269 * Advance the output engine by a certain amount of whitespace.
270 */
271 static void
272 advance(struct termp *p, size_t len)
273 {
274 size_t i;
275
276 if (TERMTYPE_CHAR == p->type) {
277 /* Just print whitespace on the terminal. */
278 for (i = 0; i < len; i++)
279 putchar(' ');
280 return;
281 }
282
283 if (PS_INLINE & p->psstate) {
284 /* Dump out any existing line scope. */
285 printf(") show\n");
286 p->psstate &= ~PS_INLINE;
287 }
288
289 p->pscol += len ? len * PS_CHAR_WIDTH : 0;
290 }
291
292
293 static struct termp *
294 alloc(char *outopts, enum termenc enc, enum termtype type)
295 {
296 struct termp *p;
297 const char *toks[2];
298 char *v;
299 size_t width;
300
301 toks[0] = "width";
302 toks[1] = NULL;
303
304 p = calloc(1, sizeof(struct termp));
305 if (NULL == p) {
306 perror(NULL);
307 exit(EXIT_FAILURE);
308 }
309
310 p->type = type;
311 p->tabwidth = 5;
312 p->enc = enc;
313
314 width = 80;
315
316 while (outopts && *outopts)
317 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
318 case (0):
319 width = (size_t)atoi(v);
320 break;
321 default:
322 break;
323 }
324
325 /* Enforce some lower boundary. */
326 if (width < 60)
327 width = 60;
328 p->defrmargin = width - 2;
329 return(p);
330 }
331
332
333 /*
334 * Flush a line of text. A "line" is loosely defined as being something
335 * that should be followed by a newline, regardless of whether it's
336 * broken apart by newlines getting there. A line can also be a
337 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
338 * not have a trailing newline.
339 *
340 * The following flags may be specified:
341 *
342 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
343 * offset value. This is useful when doing columnar lists where the
344 * prior column has right-padded.
345 *
346 * - TERMP_NOBREAK: this is the most important and is used when making
347 * columns. In short: don't print a newline and instead pad to the
348 * right margin. Used in conjunction with TERMP_NOLPAD.
349 *
350 * - TERMP_TWOSPACE: when padding, make sure there are at least two
351 * space characters of padding. Otherwise, rather break the line.
352 *
353 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
354 * the line is overrun, and don't pad-right if it's underrun.
355 *
356 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
357 * overruning, instead save the position and continue at that point
358 * when the next invocation.
359 *
360 * In-line line breaking:
361 *
362 * If TERMP_NOBREAK is specified and the line overruns the right
363 * margin, it will break and pad-right to the right margin after
364 * writing. If maxrmargin is violated, it will break and continue
365 * writing from the right-margin, which will lead to the above scenario
366 * upon exit. Otherwise, the line will break at the right margin.
367 */
368 void
369 term_flushln(struct termp *p)
370 {
371 int i; /* current input position in p->buf */
372 size_t vis; /* current visual position on output */
373 size_t vbl; /* number of blanks to prepend to output */
374 size_t vend; /* end of word visual position on output */
375 size_t bp; /* visual right border position */
376 int j; /* temporary loop index */
377 int jhy; /* last hyphen before line overflow */
378 size_t maxvis, mmax;
379
380 /*
381 * First, establish the maximum columns of "visible" content.
382 * This is usually the difference between the right-margin and
383 * an indentation, but can be, for tagged lists or columns, a
384 * small set of values.
385 */
386
387 assert(p->offset < p->rmargin);
388
389 maxvis = (int)(p->rmargin - p->offset) - p->overstep < 0 ?
390 /* LINTED */
391 0 : p->rmargin - p->offset - p->overstep;
392 mmax = (int)(p->maxrmargin - p->offset) - p->overstep < 0 ?
393 /* LINTED */
394 0 : p->maxrmargin - p->offset - p->overstep;
395
396 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
397
398 /*
399 * Indent the first line of a paragraph.
400 */
401 vbl = p->flags & TERMP_NOLPAD ? 0 : p->offset;
402
403 /*
404 * FIXME: if bp is zero, we still output the first word before
405 * breaking the line.
406 */
407
408 vis = vend = i = 0;
409 while (i < (int)p->col) {
410
411 /*
412 * Handle literal tab characters.
413 */
414 for (j = i; j < (int)p->col; j++) {
415 if ('\t' != p->buf[j])
416 break;
417 vend = (vis/p->tabwidth+1)*p->tabwidth;
418 vbl += vend - vis;
419 vis = vend;
420 }
421
422 /*
423 * Count up visible word characters. Control sequences
424 * (starting with the CSI) aren't counted. A space
425 * generates a non-printing word, which is valid (the
426 * space is printed according to regular spacing rules).
427 */
428
429 /* LINTED */
430 for (jhy = 0; j < (int)p->col; j++) {
431 if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j])
432 break;
433 if (8 != p->buf[j]) {
434 if (vend > vis && vend < bp &&
435 ASCII_HYPH == p->buf[j])
436 jhy = j;
437 vend++;
438 } else
439 vend--;
440 }
441
442 /*
443 * Find out whether we would exceed the right margin.
444 * If so, break to the next line.
445 */
446 if (vend > bp && 0 == jhy && vis > 0) {
447 vend -= vis;
448 endline(p);
449 if (TERMP_NOBREAK & p->flags) {
450 p->viscol = p->rmargin;
451 advance(p, p->rmargin);
452 vend += p->rmargin - p->offset;
453 } else {
454 p->viscol = 0;
455 vbl = p->offset;
456 }
457
458 /* Remove the p->overstep width. */
459
460 bp += (int)/* LINTED */
461 p->overstep;
462 p->overstep = 0;
463 }
464
465 /*
466 * Skip leading tabs, they were handled above.
467 */
468 while (i < (int)p->col && '\t' == p->buf[i])
469 i++;
470
471 /* Write out the [remaining] word. */
472 for ( ; i < (int)p->col; i++) {
473 if (vend > bp && jhy > 0 && i > jhy)
474 break;
475 if ('\t' == p->buf[i])
476 break;
477 if (' ' == p->buf[i]) {
478 while (' ' == p->buf[i]) {
479 vbl++;
480 i++;
481 }
482 break;
483 }
484 if (ASCII_NBRSP == p->buf[i]) {
485 vbl++;
486 continue;
487 }
488
489 /*
490 * Now we definitely know there will be
491 * printable characters to output,
492 * so write preceding white space now.
493 */
494 if (vbl) {
495 advance(p, vbl);
496 p->viscol += vbl;
497 vbl = 0;
498 }
499
500 if (ASCII_HYPH == p->buf[i])
501 letter(p, '-');
502 else
503 letter(p, p->buf[i]);
504
505 p->viscol += 1;
506 }
507 vend += vbl;
508 vis = vend;
509 }
510
511 p->col = 0;
512 p->overstep = 0;
513
514 if ( ! (TERMP_NOBREAK & p->flags)) {
515 p->viscol = 0;
516 endline(p);
517 return;
518 }
519
520 if (TERMP_HANG & p->flags) {
521 /* We need one blank after the tag. */
522 p->overstep = /* LINTED */
523 vis - maxvis + 1;
524
525 /*
526 * Behave exactly the same way as groff:
527 * If we have overstepped the margin, temporarily move
528 * it to the right and flag the rest of the line to be
529 * shorter.
530 * If we landed right at the margin, be happy.
531 * If we are one step before the margin, temporarily
532 * move it one step LEFT and flag the rest of the line
533 * to be longer.
534 */
535 if (p->overstep >= -1) {
536 assert((int)maxvis + p->overstep >= 0);
537 /* LINTED */
538 maxvis += p->overstep;
539 } else
540 p->overstep = 0;
541
542 } else if (TERMP_DANGLE & p->flags)
543 return;
544
545 /* Right-pad. */
546 if (maxvis > vis + /* LINTED */
547 ((TERMP_TWOSPACE & p->flags) ? 1 : 0)) {
548 p->viscol += maxvis - vis;
549 advance(p, maxvis - vis);
550 vis += (maxvis - vis);
551 } else { /* ...or newline break. */
552 endline(p);
553 p->viscol = p->rmargin;
554 advance(p, p->rmargin);
555 }
556 }
557
558
559 /*
560 * A newline only breaks an existing line; it won't assert vertical
561 * space. All data in the output buffer is flushed prior to the newline
562 * assertion.
563 */
564 void
565 term_newln(struct termp *p)
566 {
567
568 p->flags |= TERMP_NOSPACE;
569 if (0 == p->col && 0 == p->viscol) {
570 p->flags &= ~TERMP_NOLPAD;
571 return;
572 }
573 term_flushln(p);
574 p->flags &= ~TERMP_NOLPAD;
575 }
576
577
578 /*
579 * Asserts a vertical space (a full, empty line-break between lines).
580 * Note that if used twice, this will cause two blank spaces and so on.
581 * All data in the output buffer is flushed prior to the newline
582 * assertion.
583 */
584 void
585 term_vspace(struct termp *p)
586 {
587
588 term_newln(p);
589 p->viscol = 0;
590 endline(p);
591 }
592
593
594 static void
595 spec(struct termp *p, const char *word, size_t len)
596 {
597 const char *rhs;
598 size_t sz;
599
600 rhs = chars_a2ascii(p->symtab, word, len, &sz);
601 if (rhs)
602 encode(p, rhs, sz);
603 }
604
605
606 static void
607 res(struct termp *p, const char *word, size_t len)
608 {
609 const char *rhs;
610 size_t sz;
611
612 rhs = chars_a2res(p->symtab, word, len, &sz);
613 if (rhs)
614 encode(p, rhs, sz);
615 }
616
617
618 void
619 term_fontlast(struct termp *p)
620 {
621 enum termfont f;
622
623 f = p->fontl;
624 p->fontl = p->fontq[p->fonti];
625 p->fontq[p->fonti] = f;
626 }
627
628
629 void
630 term_fontrepl(struct termp *p, enum termfont f)
631 {
632
633 p->fontl = p->fontq[p->fonti];
634 p->fontq[p->fonti] = f;
635 }
636
637
638 void
639 term_fontpush(struct termp *p, enum termfont f)
640 {
641
642 assert(p->fonti + 1 < 10);
643 p->fontl = p->fontq[p->fonti];
644 p->fontq[++p->fonti] = f;
645 }
646
647
648 const void *
649 term_fontq(struct termp *p)
650 {
651
652 return(&p->fontq[p->fonti]);
653 }
654
655
656 enum termfont
657 term_fonttop(struct termp *p)
658 {
659
660 return(p->fontq[p->fonti]);
661 }
662
663
664 void
665 term_fontpopq(struct termp *p, const void *key)
666 {
667
668 while (p->fonti >= 0 && key != &p->fontq[p->fonti])
669 p->fonti--;
670 assert(p->fonti >= 0);
671 }
672
673
674 void
675 term_fontpop(struct termp *p)
676 {
677
678 assert(p->fonti);
679 p->fonti--;
680 }
681
682
683 /*
684 * Handle pwords, partial words, which may be either a single word or a
685 * phrase that cannot be broken down (such as a literal string). This
686 * handles word styling.
687 */
688 void
689 term_word(struct termp *p, const char *word)
690 {
691 const char *sv, *seq;
692 int sz;
693 size_t ssz;
694 enum roffdeco deco;
695
696 sv = word;
697
698 if (word[0] && '\0' == word[1])
699 switch (word[0]) {
700 case('.'):
701 /* FALLTHROUGH */
702 case(','):
703 /* FALLTHROUGH */
704 case(';'):
705 /* FALLTHROUGH */
706 case(':'):
707 /* FALLTHROUGH */
708 case('?'):
709 /* FALLTHROUGH */
710 case('!'):
711 /* FALLTHROUGH */
712 case(')'):
713 /* FALLTHROUGH */
714 case(']'):
715 if ( ! (TERMP_IGNDELIM & p->flags))
716 p->flags |= TERMP_NOSPACE;
717 break;
718 default:
719 break;
720 }
721
722 if ( ! (TERMP_NOSPACE & p->flags)) {
723 bufferc(p, ' ');
724 if (TERMP_SENTENCE & p->flags)
725 bufferc(p, ' ');
726 }
727
728 if ( ! (p->flags & TERMP_NONOSPACE))
729 p->flags &= ~TERMP_NOSPACE;
730
731 p->flags &= ~TERMP_SENTENCE;
732
733 /* FIXME: use strcspn. */
734
735 while (*word) {
736 if ('\\' != *word) {
737 encode(p, word, 1);
738 word++;
739 continue;
740 }
741
742 seq = ++word;
743 sz = a2roffdeco(&deco, &seq, &ssz);
744
745 switch (deco) {
746 case (DECO_RESERVED):
747 res(p, seq, ssz);
748 break;
749 case (DECO_SPECIAL):
750 spec(p, seq, ssz);
751 break;
752 case (DECO_BOLD):
753 term_fontrepl(p, TERMFONT_BOLD);
754 break;
755 case (DECO_ITALIC):
756 term_fontrepl(p, TERMFONT_UNDER);
757 break;
758 case (DECO_ROMAN):
759 term_fontrepl(p, TERMFONT_NONE);
760 break;
761 case (DECO_PREVIOUS):
762 term_fontlast(p);
763 break;
764 default:
765 break;
766 }
767
768 word += sz;
769 if (DECO_NOSPACE == deco && '\0' == *word)
770 p->flags |= TERMP_NOSPACE;
771 }
772
773 /*
774 * Note that we don't process the pipe: the parser sees it as
775 * punctuation, but we don't in terms of typography.
776 */
777 if (sv[0] && 0 == sv[1])
778 switch (sv[0]) {
779 case('('):
780 /* FALLTHROUGH */
781 case('['):
782 p->flags |= TERMP_NOSPACE;
783 break;
784 default:
785 break;
786 }
787 }
788
789
790 static void
791 adjbuf(struct termp *p, size_t sz)
792 {
793
794 if (0 == p->maxcols)
795 p->maxcols = 1024;
796 while (sz >= p->maxcols)
797 p->maxcols <<= 2;
798
799 p->buf = realloc(p->buf, p->maxcols);
800 if (NULL == p->buf) {
801 perror(NULL);
802 exit(EXIT_FAILURE);
803 }
804 }
805
806
807 static void
808 buffera(struct termp *p, const char *word, size_t sz)
809 {
810
811 if (p->col + sz >= p->maxcols)
812 adjbuf(p, p->col + sz);
813
814 memcpy(&p->buf[(int)p->col], word, sz);
815 p->col += sz;
816 }
817
818
819 static void
820 bufferc(struct termp *p, char c)
821 {
822
823 if (p->col + 1 >= p->maxcols)
824 adjbuf(p, p->col + 1);
825
826 p->buf[(int)p->col++] = c;
827 }
828
829
830 static void
831 encode(struct termp *p, const char *word, size_t sz)
832 {
833 enum termfont f;
834 int i;
835
836 /*
837 * Encode and buffer a string of characters. If the current
838 * font mode is unset, buffer directly, else encode then buffer
839 * character by character.
840 */
841
842 if (TERMTYPE_PS == p->type) {
843 buffera(p, word, sz);
844 return;
845 } else if (TERMFONT_NONE == (f = term_fonttop(p))) {
846 buffera(p, word, sz);
847 return;
848 }
849
850 for (i = 0; i < (int)sz; i++) {
851 if ( ! isgraph((u_char)word[i])) {
852 bufferc(p, word[i]);
853 continue;
854 }
855
856 if (TERMFONT_UNDER == f)
857 bufferc(p, '_');
858 else
859 bufferc(p, word[i]);
860
861 bufferc(p, 8);
862 bufferc(p, word[i]);
863 }
864 }
865
866
867 size_t
868 term_vspan(const struct roffsu *su)
869 {
870 double r;
871
872 switch (su->unit) {
873 case (SCALE_CM):
874 r = su->scale * 2;
875 break;
876 case (SCALE_IN):
877 r = su->scale * 6;
878 break;
879 case (SCALE_PC):
880 r = su->scale;
881 break;
882 case (SCALE_PT):
883 r = su->scale / 8;
884 break;
885 case (SCALE_MM):
886 r = su->scale / 1000;
887 break;
888 case (SCALE_VS):
889 r = su->scale;
890 break;
891 default:
892 r = su->scale - 1;
893 break;
894 }
895
896 if (r < 0.0)
897 r = 0.0;
898 return(/* LINTED */(size_t)
899 r);
900 }
901
902
903 size_t
904 term_hspan(const struct roffsu *su)
905 {
906 double r;
907
908 /* XXX: CM, IN, and PT are approximations. */
909
910 switch (su->unit) {
911 case (SCALE_CM):
912 r = 4 * su->scale;
913 break;
914 case (SCALE_IN):
915 /* XXX: this is an approximation. */
916 r = 10 * su->scale;
917 break;
918 case (SCALE_PC):
919 r = (10 * su->scale) / 6;
920 break;
921 case (SCALE_PT):
922 r = (10 * su->scale) / 72;
923 break;
924 case (SCALE_MM):
925 r = su->scale / 1000; /* FIXME: double-check. */
926 break;
927 case (SCALE_VS):
928 r = su->scale * 2 - 1; /* FIXME: double-check. */
929 break;
930 default:
931 r = su->scale;
932 break;
933 }
934
935 if (r < 0.0)
936 r = 0.0;
937 return((size_t)/* LINTED */
938 r);
939 }
940
941