]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.267 2017/06/07 20:01:19 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
29 #include "mandoc_aux.h"
34 static size_t cond_width(const struct termp
*, int, int *);
35 static void adjbuf(struct termp_col
*, size_t);
36 static void bufferc(struct termp
*, char);
37 static void encode(struct termp
*, const char *, size_t);
38 static void encode1(struct termp
*, int);
39 static void endline(struct termp
*);
43 term_free(struct termp
*p
)
45 for (p
->tcol
= p
->tcols
; p
->tcol
< p
->tcols
+ p
->maxtcol
; p
->tcol
++)
53 term_begin(struct termp
*p
, term_margin head
,
54 term_margin foot
, const struct roff_meta
*arg
)
64 term_end(struct termp
*p
)
71 * Flush a chunk of text. By default, break the output line each time
72 * the right margin is reached, and continue output on the next line
73 * at the same offset as the chunk itself. By default, also break the
74 * output line at the end of the chunk.
75 * The following flags may be specified:
77 * - TERMP_NOBREAK: Do not break the output line at the right margin,
78 * but only at the max right margin. Also, do not break the output
79 * line at the end of the chunk, such that the next call can pad to
80 * the next column. However, if less than p->trailspace blanks,
81 * which can be 0, 1, or 2, remain to the right margin, the line
83 * - TERMP_BRTRSP: Consider trailing whitespace significant
84 * when deciding whether the chunk fits or not.
85 * - TERMP_BRIND: If the chunk does not fit and the output line has
86 * to be broken, start the next line at the right margin instead
87 * of at the offset. Used together with TERMP_NOBREAK for the tags
88 * in various kinds of tagged lists.
89 * - TERMP_HANG: Do not break the output line at the right margin,
90 * append the next chunk after it even if this one is too long.
91 * To be used together with TERMP_NOBREAK.
92 * - TERMP_NOPAD: Start writing at the current position,
93 * do not pad with blank characters up to the offset.
96 term_flushln(struct termp
*p
)
98 size_t vis
; /* current visual position on output */
99 size_t vbl
; /* number of blanks to prepend to output */
100 size_t vend
; /* end of word visual position on output */
101 size_t bp
; /* visual right border position */
102 size_t dv
; /* temporary for visual pos calculations */
103 size_t j
; /* temporary loop index for p->tcol->buf */
104 size_t jhy
; /* last hyph before overflow w/r/t j */
105 size_t maxvis
; /* output position of visible boundary */
106 int ntab
; /* number of tabs to prepend */
108 vbl
= (p
->flags
& TERMP_NOPAD
) || p
->tcol
->offset
< p
->viscol
?
109 0 : p
->tcol
->offset
- p
->viscol
;
110 if (p
->minbl
&& vbl
< p
->minbl
)
112 maxvis
= p
->tcol
->rmargin
> p
->viscol
+ vbl
?
113 p
->tcol
->rmargin
- p
->viscol
- vbl
: 0;
114 bp
= !(p
->flags
& TERMP_NOBREAK
) ? maxvis
:
115 p
->maxrmargin
> p
->viscol
+ vbl
?
116 p
->maxrmargin
- p
->viscol
- vbl
: 0;
119 if (p
->lasttcol
== 0)
121 while (p
->tcol
->col
< p
->lastcol
) {
124 * Handle literal tab characters: collapse all
125 * subsequent tabs into a single huge set of spaces.
129 while (p
->tcol
->col
< p
->lastcol
&&
130 p
->tcol
->buf
[p
->tcol
->col
] == '\t') {
131 vend
= term_tab_next(vis
);
139 * Count up visible word characters. Control sequences
140 * (starting with the CSI) aren't counted. A space
141 * generates a non-printing word, which is valid (the
142 * space is printed according to regular spacing rules).
146 for (j
= p
->tcol
->col
; j
< p
->lastcol
; j
++) {
147 if (p
->tcol
->buf
[j
] == ' ' || p
->tcol
->buf
[j
] == '\t')
150 /* Back over the last printed character. */
151 if (p
->tcol
->buf
[j
] == '\b') {
153 vend
-= (*p
->width
)(p
, p
->tcol
->buf
[j
- 1]);
158 /* Break at the hyphen point if we overrun. */
159 if (vend
> vis
&& vend
< bp
&&
160 (p
->tcol
->buf
[j
] == ASCII_HYPH
||
161 p
->tcol
->buf
[j
] == ASCII_BREAK
))
165 * Hyphenation now decided, put back a real
166 * hyphen such that we get the correct width.
168 if (p
->tcol
->buf
[j
] == ASCII_HYPH
)
169 p
->tcol
->buf
[j
] = '-';
171 vend
+= (*p
->width
)(p
, p
->tcol
->buf
[j
]);
175 * Find out whether we would exceed the right margin.
176 * If so, break to the next line.
179 if (vend
> bp
&& jhy
== 0 && vis
> 0 &&
180 (p
->flags
& TERMP_BRNEVER
) == 0) {
187 /* Use pending tabs on the new line. */
191 vbl
= term_tab_next(vbl
);
193 /* Re-establish indentation. */
195 if (p
->flags
& TERMP_BRIND
)
196 vbl
+= p
->tcol
->rmargin
;
198 vbl
+= p
->tcol
->offset
;
199 maxvis
= p
->tcol
->rmargin
> vbl
?
200 p
->tcol
->rmargin
- vbl
: 0;
201 bp
= !(p
->flags
& TERMP_NOBREAK
) ? maxvis
:
202 p
->maxrmargin
> vbl
? p
->maxrmargin
- vbl
: 0;
206 * Write out the rest of the word.
209 for ( ; p
->tcol
->col
< p
->lastcol
; p
->tcol
->col
++) {
210 if (vend
> bp
&& jhy
> 0 && p
->tcol
->col
> jhy
)
212 if (p
->tcol
->buf
[p
->tcol
->col
] == '\t')
214 if (p
->tcol
->buf
[p
->tcol
->col
] == ' ') {
216 while (p
->tcol
->col
< p
->lastcol
&&
217 p
->tcol
->buf
[p
->tcol
->col
] == ' ')
219 dv
= (p
->tcol
->col
- j
) * (*p
->width
)(p
, ' ');
224 if (p
->tcol
->buf
[p
->tcol
->col
] == ASCII_NBRSP
) {
225 vbl
+= (*p
->width
)(p
, ' ');
228 if (p
->tcol
->buf
[p
->tcol
->col
] == ASCII_BREAK
)
232 * Now we definitely know there will be
233 * printable characters to output,
234 * so write preceding white space now.
237 (*p
->advance
)(p
, vbl
);
242 (*p
->letter
)(p
, p
->tcol
->buf
[p
->tcol
->col
]);
243 if (p
->tcol
->buf
[p
->tcol
->col
] == '\b')
244 p
->viscol
-= (*p
->width
)(p
,
245 p
->tcol
->buf
[p
->tcol
->col
- 1]);
247 p
->viscol
+= (*p
->width
)(p
,
248 p
->tcol
->buf
[p
->tcol
->col
]);
254 * If there was trailing white space, it was not printed;
255 * so reset the cursor position accordingly.
263 p
->col
= p
->lastcol
= 0;
264 p
->minbl
= p
->trailspace
;
265 p
->flags
&= ~(TERMP_BACKAFTER
| TERMP_BACKBEFORE
| TERMP_NOPAD
);
267 /* Trailing whitespace is significant in some columns. */
269 if (vis
&& vbl
&& (TERMP_BRTRSP
& p
->flags
))
272 /* If the column was overrun, break the line. */
273 if ((p
->flags
& TERMP_NOBREAK
) == 0 ||
274 ((p
->flags
& TERMP_HANG
) == 0 &&
275 vis
+ p
->trailspace
* (*p
->width
)(p
, ' ') > maxvis
))
280 endline(struct termp
*p
)
282 if ((p
->flags
& (TERMP_NEWMC
| TERMP_ENDMC
)) == TERMP_ENDMC
) {
284 p
->flags
&= ~TERMP_ENDMC
;
287 if (p
->viscol
&& p
->maxrmargin
>= p
->viscol
)
288 (*p
->advance
)(p
, p
->maxrmargin
- p
->viscol
+ 1);
289 p
->flags
|= TERMP_NOBUF
| TERMP_NOSPACE
;
291 p
->flags
&= ~(TERMP_NOBUF
| TERMP_NEWMC
);
299 * A newline only breaks an existing line; it won't assert vertical
300 * space. All data in the output buffer is flushed prior to the newline
304 term_newln(struct termp
*p
)
307 p
->flags
|= TERMP_NOSPACE
;
308 if (p
->lastcol
|| p
->viscol
)
313 * Asserts a vertical space (a full, empty line-break between lines).
314 * Note that if used twice, this will cause two blank spaces and so on.
315 * All data in the output buffer is flushed prior to the newline
319 term_vspace(struct termp
*p
)
331 /* Swap current and previous font; for \fP and .ft P */
333 term_fontlast(struct termp
*p
)
338 p
->fontl
= p
->fontq
[p
->fonti
];
339 p
->fontq
[p
->fonti
] = f
;
342 /* Set font, save current, discard previous; for \f, .ft, .B etc. */
344 term_fontrepl(struct termp
*p
, enum termfont f
)
347 p
->fontl
= p
->fontq
[p
->fonti
];
348 p
->fontq
[p
->fonti
] = f
;
351 /* Set font, save previous. */
353 term_fontpush(struct termp
*p
, enum termfont f
)
356 p
->fontl
= p
->fontq
[p
->fonti
];
357 if (++p
->fonti
== p
->fontsz
) {
359 p
->fontq
= mandoc_reallocarray(p
->fontq
,
360 p
->fontsz
, sizeof(*p
->fontq
));
362 p
->fontq
[p
->fonti
] = f
;
365 /* Flush to make the saved pointer current again. */
367 term_fontpopq(struct termp
*p
, int i
)
375 /* Pop one font off the stack. */
377 term_fontpop(struct termp
*p
)
385 * Handle pwords, partial words, which may be either a single word or a
386 * phrase that cannot be broken down (such as a literal string). This
387 * handles word styling.
390 term_word(struct termp
*p
, const char *word
)
393 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
394 const char *seq
, *cp
;
396 size_t csz
, lsz
, ssz
;
399 if ((p
->flags
& TERMP_NOBUF
) == 0) {
400 if ((p
->flags
& TERMP_NOSPACE
) == 0) {
401 if ((p
->flags
& TERMP_KEEP
) == 0) {
403 if (p
->flags
& TERMP_SENTENCE
)
406 bufferc(p
, ASCII_NBRSP
);
408 if (p
->flags
& TERMP_PREKEEP
)
409 p
->flags
|= TERMP_KEEP
;
410 if (p
->flags
& TERMP_NONOSPACE
)
411 p
->flags
|= TERMP_NOSPACE
;
413 p
->flags
&= ~TERMP_NOSPACE
;
414 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_NONEWLINE
);
418 while ('\0' != *word
) {
420 if (TERMP_NBRWORD
& p
->flags
) {
426 ssz
= strcspn(word
, "\\ ");
428 ssz
= strcspn(word
, "\\");
429 encode(p
, word
, ssz
);
435 esc
= mandoc_escape(&word
, &seq
, &sz
);
436 if (ESCAPE_ERROR
== esc
)
441 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
443 case ESCAPE_NUMBERED
:
444 uc
= mchars_num2char(seq
, sz
);
449 if (p
->enc
== TERMENC_ASCII
) {
450 cp
= mchars_spec2str(seq
, sz
, &ssz
);
454 uc
= mchars_spec2cp(seq
, sz
);
459 case ESCAPE_FONTBOLD
:
460 term_fontrepl(p
, TERMFONT_BOLD
);
462 case ESCAPE_FONTITALIC
:
463 term_fontrepl(p
, TERMFONT_UNDER
);
466 term_fontrepl(p
, TERMFONT_BI
);
469 case ESCAPE_FONTROMAN
:
470 term_fontrepl(p
, TERMFONT_NONE
);
472 case ESCAPE_FONTPREV
:
476 if (p
->flags
& TERMP_BACKAFTER
)
477 p
->flags
&= ~TERMP_BACKAFTER
;
478 else if (*word
== '\0')
479 p
->flags
|= (TERMP_NOSPACE
| TERMP_NONEWLINE
);
482 if (a2roffsu(seq
, &su
, SCALE_EM
) == 0)
484 uc
= term_hspan(p
, &su
) / 24;
487 bufferc(p
, ASCII_NBRSP
);
488 else if (p
->col
> (size_t)(-uc
))
493 if (p
->tcol
->offset
> (size_t)(-uc
)) {
495 p
->tcol
->offset
+= uc
;
497 p
->ti
-= p
->tcol
->offset
;
503 if (a2roffsu(seq
, &su
, SCALE_EM
) == 0)
505 uc
= term_hspan(p
, &su
) / 24;
507 if (p
->tcol
->rmargin
<= p
->tcol
->offset
)
509 lsz
= p
->tcol
->rmargin
- p
->tcol
->offset
;
513 strchr(" %&()*+-./0123456789:<=>", *seq
)) {
517 if (sz
&& strchr("cifMmnPpuv", *seq
)) {
523 else if (*seq
== '\\') {
525 esc
= mandoc_escape(&seq
, &cp
, &sz
);
528 uc
= mchars_num2uc(cp
+ 1, sz
- 1);
530 case ESCAPE_NUMBERED
:
531 uc
= mchars_num2char(cp
, sz
);
534 uc
= mchars_spec2cp(cp
, sz
);
542 if (uc
< 0x20 || (uc
> 0x7E && uc
< 0xA0))
544 if (p
->enc
== TERMENC_ASCII
) {
545 cp
= ascii_uc2str(uc
);
546 csz
= term_strlen(p
, cp
);
549 csz
= (*p
->width
)(p
, uc
);
551 if (p
->enc
== TERMENC_ASCII
)
558 case ESCAPE_SKIPCHAR
:
559 p
->flags
|= TERMP_BACKAFTER
;
561 case ESCAPE_OVERSTRIKE
:
565 mandoc_escape(&seq
, NULL
, NULL
);
570 if (p
->flags
& TERMP_BACKBEFORE
)
571 p
->flags
|= TERMP_BACKAFTER
;
573 p
->flags
|= TERMP_BACKBEFORE
;
576 /* Trim trailing backspace/blank pair. */
577 if (p
->lastcol
> 2 &&
578 (p
->tcol
->buf
[p
->lastcol
- 1] == ' ' ||
579 p
->tcol
->buf
[p
->lastcol
- 1] == '\t'))
581 if (p
->col
> p
->lastcol
)
589 * Common handling for Unicode and numbered
590 * character escape sequences.
593 if (p
->enc
== TERMENC_ASCII
) {
594 cp
= ascii_uc2str(uc
);
595 encode(p
, cp
, strlen(cp
));
597 if ((uc
< 0x20 && uc
!= 0x09) ||
598 (uc
> 0x7E && uc
< 0xA0))
603 p
->flags
&= ~TERMP_NBRWORD
;
607 adjbuf(struct termp_col
*c
, size_t sz
)
611 while (c
->maxcols
<= sz
)
613 c
->buf
= mandoc_reallocarray(c
->buf
, c
->maxcols
, sizeof(*c
->buf
));
617 bufferc(struct termp
*p
, char c
)
619 if (p
->flags
& TERMP_NOBUF
) {
623 if (p
->col
+ 1 >= p
->tcol
->maxcols
)
624 adjbuf(p
->tcol
, p
->col
+ 1);
625 if (p
->lastcol
<= p
->col
|| (c
!= ' ' && c
!= ASCII_NBRSP
))
626 p
->tcol
->buf
[p
->col
] = c
;
627 if (p
->lastcol
< ++p
->col
)
633 * Do this for a single (probably unicode) value.
634 * Does not check for non-decorated glyphs.
637 encode1(struct termp
*p
, int c
)
641 if (p
->flags
& TERMP_NOBUF
) {
646 if (p
->col
+ 7 >= p
->tcol
->maxcols
)
647 adjbuf(p
->tcol
, p
->col
+ 7);
649 f
= (c
== ASCII_HYPH
|| c
> 127 || isgraph(c
)) ?
650 p
->fontq
[p
->fonti
] : TERMFONT_NONE
;
652 if (p
->flags
& TERMP_BACKBEFORE
) {
653 if (p
->tcol
->buf
[p
->col
- 1] == ' ' ||
654 p
->tcol
->buf
[p
->col
- 1] == '\t')
657 p
->tcol
->buf
[p
->col
++] = '\b';
658 p
->flags
&= ~TERMP_BACKBEFORE
;
660 if (f
== TERMFONT_UNDER
|| f
== TERMFONT_BI
) {
661 p
->tcol
->buf
[p
->col
++] = '_';
662 p
->tcol
->buf
[p
->col
++] = '\b';
664 if (f
== TERMFONT_BOLD
|| f
== TERMFONT_BI
) {
666 p
->tcol
->buf
[p
->col
++] = '-';
668 p
->tcol
->buf
[p
->col
++] = c
;
669 p
->tcol
->buf
[p
->col
++] = '\b';
671 if (p
->lastcol
<= p
->col
|| (c
!= ' ' && c
!= ASCII_NBRSP
))
672 p
->tcol
->buf
[p
->col
] = c
;
673 if (p
->lastcol
< ++p
->col
)
675 if (p
->flags
& TERMP_BACKAFTER
) {
676 p
->flags
|= TERMP_BACKBEFORE
;
677 p
->flags
&= ~TERMP_BACKAFTER
;
682 encode(struct termp
*p
, const char *word
, size_t sz
)
686 if (p
->flags
& TERMP_NOBUF
) {
687 for (i
= 0; i
< sz
; i
++)
688 (*p
->letter
)(p
, word
[i
]);
692 if (p
->col
+ 2 + (sz
* 5) >= p
->tcol
->maxcols
)
693 adjbuf(p
->tcol
, p
->col
+ 2 + (sz
* 5));
695 for (i
= 0; i
< sz
; i
++) {
696 if (ASCII_HYPH
== word
[i
] ||
697 isgraph((unsigned char)word
[i
]))
700 if (p
->lastcol
<= p
->col
||
701 (word
[i
] != ' ' && word
[i
] != ASCII_NBRSP
))
702 p
->tcol
->buf
[p
->col
] = word
[i
];
706 * Postpone the effect of \z while handling
707 * an overstrike sequence from ascii_uc2str().
710 if (word
[i
] == '\b' &&
711 (p
->flags
& TERMP_BACKBEFORE
)) {
712 p
->flags
&= ~TERMP_BACKBEFORE
;
713 p
->flags
|= TERMP_BACKAFTER
;
717 if (p
->lastcol
< p
->col
)
722 term_setwidth(struct termp
*p
, const char *wstr
)
742 if (a2roffsu(wstr
, &su
, SCALE_MAX
))
743 width
= term_hspan(p
, &su
);
747 (*p
->setwidth
)(p
, iop
, width
);
751 term_len(const struct termp
*p
, size_t sz
)
754 return (*p
->width
)(p
, ' ') * sz
;
758 cond_width(const struct termp
*p
, int c
, int *skip
)
765 return (*p
->width
)(p
, c
);
769 term_strlen(const struct termp
*p
, const char *cp
)
773 const char *seq
, *rhs
;
775 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
779 * Account for escaped sequences within string length
780 * calculations. This follows the logic in term_word() as we
781 * must calculate the width of produced strings.
786 while ('\0' != *cp
) {
787 rsz
= strcspn(cp
, rej
);
788 for (i
= 0; i
< rsz
; i
++)
789 sz
+= cond_width(p
, *cp
++, &skip
);
794 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
795 if (ESCAPE_ERROR
== esc
)
802 uc
= mchars_num2uc(seq
+ 1, ssz
- 1);
804 case ESCAPE_NUMBERED
:
805 uc
= mchars_num2char(seq
, ssz
);
810 if (p
->enc
== TERMENC_ASCII
) {
811 rhs
= mchars_spec2str(seq
, ssz
, &rsz
);
815 uc
= mchars_spec2cp(seq
, ssz
);
817 sz
+= cond_width(p
, uc
, &skip
);
820 case ESCAPE_SKIPCHAR
:
823 case ESCAPE_OVERSTRIKE
:
828 mandoc_escape(&seq
, NULL
, NULL
);
831 i
= (*p
->width
)(p
, *seq
++);
842 * Common handling for Unicode and numbered
843 * character escape sequences.
847 if (p
->enc
== TERMENC_ASCII
) {
848 rhs
= ascii_uc2str(uc
);
851 if ((uc
< 0x20 && uc
!= 0x09) ||
852 (uc
> 0x7E && uc
< 0xA0))
854 sz
+= cond_width(p
, uc
, &skip
);
865 * Common handling for all escape sequences
866 * printing more than one character.
869 for (i
= 0; i
< rsz
; i
++)
870 sz
+= (*p
->width
)(p
, *rhs
++);
873 sz
+= cond_width(p
, ' ', &skip
);
877 sz
+= cond_width(p
, '-', &skip
);
889 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
896 r
= su
->scale
/ 40.0;
899 r
= su
->scale
* 6.0 / 2.54;
902 r
= su
->scale
* 65536.0 / 40.0;
908 r
= su
->scale
* 0.006;
914 r
= su
->scale
/ 12.0;
926 ri
= r
> 0.0 ? r
+ 0.4995 : r
- 0.4995;
927 return ri
< 66 ? ri
: 1;
931 * Convert a scaling width to basic units, rounding down.
934 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
937 return (*p
->hspan
)(p
, su
);