]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
b97b9be3956ef47dc09832b3c1cc9cd4cf7e3c9a
1 /* $Id: term.c,v 1.269 2017/06/12 19:05:47 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_setcol(struct termp
*p
, size_t maxtcol
)
45 if (maxtcol
> p
->maxtcol
) {
46 p
->tcols
= mandoc_recallocarray(p
->tcols
,
47 p
->maxtcol
, maxtcol
, sizeof(*p
->tcols
));
50 p
->lasttcol
= maxtcol
- 1;
55 term_free(struct termp
*p
)
57 for (p
->tcol
= p
->tcols
; p
->tcol
< p
->tcols
+ p
->maxtcol
; p
->tcol
++)
65 term_begin(struct termp
*p
, term_margin head
,
66 term_margin foot
, const struct roff_meta
*arg
)
76 term_end(struct termp
*p
)
83 * Flush a chunk of text. By default, break the output line each time
84 * the right margin is reached, and continue output on the next line
85 * at the same offset as the chunk itself. By default, also break the
86 * output line at the end of the chunk.
87 * The following flags may be specified:
89 * - TERMP_NOBREAK: Do not break the output line at the right margin,
90 * but only at the max right margin. Also, do not break the output
91 * line at the end of the chunk, such that the next call can pad to
92 * the next column. However, if less than p->trailspace blanks,
93 * which can be 0, 1, or 2, remain to the right margin, the line
95 * - TERMP_BRTRSP: Consider trailing whitespace significant
96 * when deciding whether the chunk fits or not.
97 * - TERMP_BRIND: If the chunk does not fit and the output line has
98 * to be broken, start the next line at the right margin instead
99 * of at the offset. Used together with TERMP_NOBREAK for the tags
100 * in various kinds of tagged lists.
101 * - TERMP_HANG: Do not break the output line at the right margin,
102 * append the next chunk after it even if this one is too long.
103 * To be used together with TERMP_NOBREAK.
104 * - TERMP_NOPAD: Start writing at the current position,
105 * do not pad with blank characters up to the offset.
108 term_flushln(struct termp
*p
)
110 size_t vis
; /* current visual position on output */
111 size_t vbl
; /* number of blanks to prepend to output */
112 size_t vend
; /* end of word visual position on output */
113 size_t bp
; /* visual right border position */
114 size_t dv
; /* temporary for visual pos calculations */
115 size_t j
; /* temporary loop index for p->tcol->buf */
116 size_t jhy
; /* last hyph before overflow w/r/t j */
117 size_t maxvis
; /* output position of visible boundary */
118 int ntab
; /* number of tabs to prepend */
120 vbl
= (p
->flags
& TERMP_NOPAD
) || p
->tcol
->offset
< p
->viscol
?
121 0 : p
->tcol
->offset
- p
->viscol
;
122 if (p
->minbl
&& vbl
< p
->minbl
)
124 maxvis
= p
->tcol
->rmargin
> p
->viscol
+ vbl
?
125 p
->tcol
->rmargin
- p
->viscol
- vbl
: 0;
126 bp
= !(p
->flags
& TERMP_NOBREAK
) ? maxvis
:
127 p
->maxrmargin
> p
->viscol
+ vbl
?
128 p
->maxrmargin
- p
->viscol
- vbl
: 0;
131 if ((p
->flags
&& TERMP_MULTICOL
) == 0)
133 while (p
->tcol
->col
< p
->tcol
->lastcol
) {
136 * Handle literal tab characters: collapse all
137 * subsequent tabs into a single huge set of spaces.
141 while (p
->tcol
->col
< p
->tcol
->lastcol
&&
142 p
->tcol
->buf
[p
->tcol
->col
] == '\t') {
143 vend
= term_tab_next(vis
);
151 * Count up visible word characters. Control sequences
152 * (starting with the CSI) aren't counted. A space
153 * generates a non-printing word, which is valid (the
154 * space is printed according to regular spacing rules).
158 for (j
= p
->tcol
->col
; j
< p
->tcol
->lastcol
; j
++) {
159 if (p
->tcol
->buf
[j
] == ' ' || p
->tcol
->buf
[j
] == '\t')
162 /* Back over the last printed character. */
163 if (p
->tcol
->buf
[j
] == '\b') {
165 vend
-= (*p
->width
)(p
, p
->tcol
->buf
[j
- 1]);
170 /* Break at the hyphen point if we overrun. */
171 if (vend
> vis
&& vend
< bp
&&
172 (p
->tcol
->buf
[j
] == ASCII_HYPH
||
173 p
->tcol
->buf
[j
] == ASCII_BREAK
))
177 * Hyphenation now decided, put back a real
178 * hyphen such that we get the correct width.
180 if (p
->tcol
->buf
[j
] == ASCII_HYPH
)
181 p
->tcol
->buf
[j
] = '-';
183 vend
+= (*p
->width
)(p
, p
->tcol
->buf
[j
]);
187 * Find out whether we would exceed the right margin.
188 * If so, break to the next line.
191 if (vend
> bp
&& jhy
== 0 && vis
> 0 &&
192 (p
->flags
& TERMP_BRNEVER
) == 0) {
193 if (p
->flags
& TERMP_MULTICOL
)
199 /* Use pending tabs on the new line. */
203 vbl
= term_tab_next(vbl
);
205 /* Re-establish indentation. */
207 if (p
->flags
& TERMP_BRIND
)
208 vbl
+= p
->tcol
->rmargin
;
210 vbl
+= p
->tcol
->offset
;
211 maxvis
= p
->tcol
->rmargin
> vbl
?
212 p
->tcol
->rmargin
- vbl
: 0;
213 bp
= !(p
->flags
& TERMP_NOBREAK
) ? maxvis
:
214 p
->maxrmargin
> vbl
? p
->maxrmargin
- vbl
: 0;
218 * Write out the rest of the word.
221 for ( ; p
->tcol
->col
< p
->tcol
->lastcol
; p
->tcol
->col
++) {
222 if (vend
> bp
&& jhy
> 0 && p
->tcol
->col
> jhy
)
224 if (p
->tcol
->buf
[p
->tcol
->col
] == '\t')
226 if (p
->tcol
->buf
[p
->tcol
->col
] == ' ') {
228 while (p
->tcol
->col
< p
->tcol
->lastcol
&&
229 p
->tcol
->buf
[p
->tcol
->col
] == ' ')
231 dv
= (p
->tcol
->col
- j
) * (*p
->width
)(p
, ' ');
236 if (p
->tcol
->buf
[p
->tcol
->col
] == ASCII_NBRSP
) {
237 vbl
+= (*p
->width
)(p
, ' ');
240 if (p
->tcol
->buf
[p
->tcol
->col
] == ASCII_BREAK
)
244 * Now we definitely know there will be
245 * printable characters to output,
246 * so write preceding white space now.
249 (*p
->advance
)(p
, vbl
);
254 (*p
->letter
)(p
, p
->tcol
->buf
[p
->tcol
->col
]);
255 if (p
->tcol
->buf
[p
->tcol
->col
] == '\b')
256 p
->viscol
-= (*p
->width
)(p
,
257 p
->tcol
->buf
[p
->tcol
->col
- 1]);
259 p
->viscol
+= (*p
->width
)(p
,
260 p
->tcol
->buf
[p
->tcol
->col
]);
266 * If there was trailing white space, it was not printed;
267 * so reset the cursor position accordingly.
275 p
->col
= p
->tcol
->col
= p
->tcol
->lastcol
= 0;
276 p
->minbl
= p
->trailspace
;
277 p
->flags
&= ~(TERMP_BACKAFTER
| TERMP_BACKBEFORE
| TERMP_NOPAD
);
279 if (p
->flags
& TERMP_MULTICOL
)
282 /* Trailing whitespace is significant in some columns. */
284 if (vis
&& vbl
&& (TERMP_BRTRSP
& p
->flags
))
287 /* If the column was overrun, break the line. */
288 if ((p
->flags
& TERMP_NOBREAK
) == 0 ||
289 ((p
->flags
& TERMP_HANG
) == 0 &&
290 vis
+ p
->trailspace
* (*p
->width
)(p
, ' ') > maxvis
))
295 endline(struct termp
*p
)
297 if ((p
->flags
& (TERMP_NEWMC
| TERMP_ENDMC
)) == TERMP_ENDMC
) {
299 p
->flags
&= ~TERMP_ENDMC
;
302 if (p
->viscol
&& p
->maxrmargin
>= p
->viscol
)
303 (*p
->advance
)(p
, p
->maxrmargin
- p
->viscol
+ 1);
304 p
->flags
|= TERMP_NOBUF
| TERMP_NOSPACE
;
306 p
->flags
&= ~(TERMP_NOBUF
| TERMP_NEWMC
);
314 * A newline only breaks an existing line; it won't assert vertical
315 * space. All data in the output buffer is flushed prior to the newline
319 term_newln(struct termp
*p
)
322 p
->flags
|= TERMP_NOSPACE
;
323 if (p
->tcol
->lastcol
|| p
->viscol
)
328 * Asserts a vertical space (a full, empty line-break between lines).
329 * Note that if used twice, this will cause two blank spaces and so on.
330 * All data in the output buffer is flushed prior to the newline
334 term_vspace(struct termp
*p
)
346 /* Swap current and previous font; for \fP and .ft P */
348 term_fontlast(struct termp
*p
)
353 p
->fontl
= p
->fontq
[p
->fonti
];
354 p
->fontq
[p
->fonti
] = f
;
357 /* Set font, save current, discard previous; for \f, .ft, .B etc. */
359 term_fontrepl(struct termp
*p
, enum termfont f
)
362 p
->fontl
= p
->fontq
[p
->fonti
];
363 p
->fontq
[p
->fonti
] = f
;
366 /* Set font, save previous. */
368 term_fontpush(struct termp
*p
, enum termfont f
)
371 p
->fontl
= p
->fontq
[p
->fonti
];
372 if (++p
->fonti
== p
->fontsz
) {
374 p
->fontq
= mandoc_reallocarray(p
->fontq
,
375 p
->fontsz
, sizeof(*p
->fontq
));
377 p
->fontq
[p
->fonti
] = f
;
380 /* Flush to make the saved pointer current again. */
382 term_fontpopq(struct termp
*p
, int i
)
390 /* Pop one font off the stack. */
392 term_fontpop(struct termp
*p
)
400 * Handle pwords, partial words, which may be either a single word or a
401 * phrase that cannot be broken down (such as a literal string). This
402 * handles word styling.
405 term_word(struct termp
*p
, const char *word
)
408 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
409 const char *seq
, *cp
;
411 size_t csz
, lsz
, ssz
;
414 if ((p
->flags
& TERMP_NOBUF
) == 0) {
415 if ((p
->flags
& TERMP_NOSPACE
) == 0) {
416 if ((p
->flags
& TERMP_KEEP
) == 0) {
418 if (p
->flags
& TERMP_SENTENCE
)
421 bufferc(p
, ASCII_NBRSP
);
423 if (p
->flags
& TERMP_PREKEEP
)
424 p
->flags
|= TERMP_KEEP
;
425 if (p
->flags
& TERMP_NONOSPACE
)
426 p
->flags
|= TERMP_NOSPACE
;
428 p
->flags
&= ~TERMP_NOSPACE
;
429 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_NONEWLINE
);
433 while ('\0' != *word
) {
435 if (TERMP_NBRWORD
& p
->flags
) {
441 ssz
= strcspn(word
, "\\ ");
443 ssz
= strcspn(word
, "\\");
444 encode(p
, word
, ssz
);
450 esc
= mandoc_escape(&word
, &seq
, &sz
);
451 if (ESCAPE_ERROR
== esc
)
456 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
458 case ESCAPE_NUMBERED
:
459 uc
= mchars_num2char(seq
, sz
);
464 if (p
->enc
== TERMENC_ASCII
) {
465 cp
= mchars_spec2str(seq
, sz
, &ssz
);
469 uc
= mchars_spec2cp(seq
, sz
);
474 case ESCAPE_FONTBOLD
:
475 term_fontrepl(p
, TERMFONT_BOLD
);
477 case ESCAPE_FONTITALIC
:
478 term_fontrepl(p
, TERMFONT_UNDER
);
481 term_fontrepl(p
, TERMFONT_BI
);
484 case ESCAPE_FONTROMAN
:
485 term_fontrepl(p
, TERMFONT_NONE
);
487 case ESCAPE_FONTPREV
:
491 if (p
->flags
& TERMP_BACKAFTER
)
492 p
->flags
&= ~TERMP_BACKAFTER
;
493 else if (*word
== '\0')
494 p
->flags
|= (TERMP_NOSPACE
| TERMP_NONEWLINE
);
497 if (a2roffsu(seq
, &su
, SCALE_EM
) == NULL
)
499 uc
= term_hspan(p
, &su
) / 24;
502 bufferc(p
, ASCII_NBRSP
);
503 else if (p
->col
> (size_t)(-uc
))
508 if (p
->tcol
->offset
> (size_t)(-uc
)) {
510 p
->tcol
->offset
+= uc
;
512 p
->ti
-= p
->tcol
->offset
;
518 if ((seq
= a2roffsu(seq
, &su
, SCALE_EM
)) == NULL
)
520 uc
= term_hspan(p
, &su
) / 24;
522 if (p
->tcol
->rmargin
<= p
->tcol
->offset
)
524 lsz
= p
->tcol
->rmargin
- p
->tcol
->offset
;
529 else if (*seq
== '\\') {
531 esc
= mandoc_escape(&seq
, &cp
, &sz
);
534 uc
= mchars_num2uc(cp
+ 1, sz
- 1);
536 case ESCAPE_NUMBERED
:
537 uc
= mchars_num2char(cp
, sz
);
540 uc
= mchars_spec2cp(cp
, sz
);
548 if (uc
< 0x20 || (uc
> 0x7E && uc
< 0xA0))
550 if (p
->enc
== TERMENC_ASCII
) {
551 cp
= ascii_uc2str(uc
);
552 csz
= term_strlen(p
, cp
);
555 csz
= (*p
->width
)(p
, uc
);
557 if (p
->enc
== TERMENC_ASCII
)
564 case ESCAPE_SKIPCHAR
:
565 p
->flags
|= TERMP_BACKAFTER
;
567 case ESCAPE_OVERSTRIKE
:
571 mandoc_escape(&seq
, NULL
, NULL
);
576 if (p
->flags
& TERMP_BACKBEFORE
)
577 p
->flags
|= TERMP_BACKAFTER
;
579 p
->flags
|= TERMP_BACKBEFORE
;
582 /* Trim trailing backspace/blank pair. */
583 if (p
->tcol
->lastcol
> 2 &&
584 (p
->tcol
->buf
[p
->tcol
->lastcol
- 1] == ' ' ||
585 p
->tcol
->buf
[p
->tcol
->lastcol
- 1] == '\t'))
586 p
->tcol
->lastcol
-= 2;
587 if (p
->col
> p
->tcol
->lastcol
)
588 p
->col
= p
->tcol
->lastcol
;
595 * Common handling for Unicode and numbered
596 * character escape sequences.
599 if (p
->enc
== TERMENC_ASCII
) {
600 cp
= ascii_uc2str(uc
);
601 encode(p
, cp
, strlen(cp
));
603 if ((uc
< 0x20 && uc
!= 0x09) ||
604 (uc
> 0x7E && uc
< 0xA0))
609 p
->flags
&= ~TERMP_NBRWORD
;
613 adjbuf(struct termp_col
*c
, size_t sz
)
617 while (c
->maxcols
<= sz
)
619 c
->buf
= mandoc_reallocarray(c
->buf
, c
->maxcols
, sizeof(*c
->buf
));
623 bufferc(struct termp
*p
, char c
)
625 if (p
->flags
& TERMP_NOBUF
) {
629 if (p
->col
+ 1 >= p
->tcol
->maxcols
)
630 adjbuf(p
->tcol
, p
->col
+ 1);
631 if (p
->tcol
->lastcol
<= p
->col
|| (c
!= ' ' && c
!= ASCII_NBRSP
))
632 p
->tcol
->buf
[p
->col
] = c
;
633 if (p
->tcol
->lastcol
< ++p
->col
)
634 p
->tcol
->lastcol
= p
->col
;
639 * Do this for a single (probably unicode) value.
640 * Does not check for non-decorated glyphs.
643 encode1(struct termp
*p
, int c
)
647 if (p
->flags
& TERMP_NOBUF
) {
652 if (p
->col
+ 7 >= p
->tcol
->maxcols
)
653 adjbuf(p
->tcol
, p
->col
+ 7);
655 f
= (c
== ASCII_HYPH
|| c
> 127 || isgraph(c
)) ?
656 p
->fontq
[p
->fonti
] : TERMFONT_NONE
;
658 if (p
->flags
& TERMP_BACKBEFORE
) {
659 if (p
->tcol
->buf
[p
->col
- 1] == ' ' ||
660 p
->tcol
->buf
[p
->col
- 1] == '\t')
663 p
->tcol
->buf
[p
->col
++] = '\b';
664 p
->flags
&= ~TERMP_BACKBEFORE
;
666 if (f
== TERMFONT_UNDER
|| f
== TERMFONT_BI
) {
667 p
->tcol
->buf
[p
->col
++] = '_';
668 p
->tcol
->buf
[p
->col
++] = '\b';
670 if (f
== TERMFONT_BOLD
|| f
== TERMFONT_BI
) {
672 p
->tcol
->buf
[p
->col
++] = '-';
674 p
->tcol
->buf
[p
->col
++] = c
;
675 p
->tcol
->buf
[p
->col
++] = '\b';
677 if (p
->tcol
->lastcol
<= p
->col
|| (c
!= ' ' && c
!= ASCII_NBRSP
))
678 p
->tcol
->buf
[p
->col
] = c
;
679 if (p
->tcol
->lastcol
< ++p
->col
)
680 p
->tcol
->lastcol
= p
->col
;
681 if (p
->flags
& TERMP_BACKAFTER
) {
682 p
->flags
|= TERMP_BACKBEFORE
;
683 p
->flags
&= ~TERMP_BACKAFTER
;
688 encode(struct termp
*p
, const char *word
, size_t sz
)
692 if (p
->flags
& TERMP_NOBUF
) {
693 for (i
= 0; i
< sz
; i
++)
694 (*p
->letter
)(p
, word
[i
]);
698 if (p
->col
+ 2 + (sz
* 5) >= p
->tcol
->maxcols
)
699 adjbuf(p
->tcol
, p
->col
+ 2 + (sz
* 5));
701 for (i
= 0; i
< sz
; i
++) {
702 if (ASCII_HYPH
== word
[i
] ||
703 isgraph((unsigned char)word
[i
]))
706 if (p
->tcol
->lastcol
<= p
->col
||
707 (word
[i
] != ' ' && word
[i
] != ASCII_NBRSP
))
708 p
->tcol
->buf
[p
->col
] = word
[i
];
712 * Postpone the effect of \z while handling
713 * an overstrike sequence from ascii_uc2str().
716 if (word
[i
] == '\b' &&
717 (p
->flags
& TERMP_BACKBEFORE
)) {
718 p
->flags
&= ~TERMP_BACKBEFORE
;
719 p
->flags
|= TERMP_BACKAFTER
;
723 if (p
->tcol
->lastcol
< p
->col
)
724 p
->tcol
->lastcol
= p
->col
;
728 term_setwidth(struct termp
*p
, const char *wstr
)
748 if (a2roffsu(wstr
, &su
, SCALE_MAX
) != NULL
)
749 width
= term_hspan(p
, &su
);
753 (*p
->setwidth
)(p
, iop
, width
);
757 term_len(const struct termp
*p
, size_t sz
)
760 return (*p
->width
)(p
, ' ') * sz
;
764 cond_width(const struct termp
*p
, int c
, int *skip
)
771 return (*p
->width
)(p
, c
);
775 term_strlen(const struct termp
*p
, const char *cp
)
779 const char *seq
, *rhs
;
781 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
785 * Account for escaped sequences within string length
786 * calculations. This follows the logic in term_word() as we
787 * must calculate the width of produced strings.
792 while ('\0' != *cp
) {
793 rsz
= strcspn(cp
, rej
);
794 for (i
= 0; i
< rsz
; i
++)
795 sz
+= cond_width(p
, *cp
++, &skip
);
800 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
801 if (ESCAPE_ERROR
== esc
)
808 uc
= mchars_num2uc(seq
+ 1, ssz
- 1);
810 case ESCAPE_NUMBERED
:
811 uc
= mchars_num2char(seq
, ssz
);
816 if (p
->enc
== TERMENC_ASCII
) {
817 rhs
= mchars_spec2str(seq
, ssz
, &rsz
);
821 uc
= mchars_spec2cp(seq
, ssz
);
823 sz
+= cond_width(p
, uc
, &skip
);
826 case ESCAPE_SKIPCHAR
:
829 case ESCAPE_OVERSTRIKE
:
834 mandoc_escape(&seq
, NULL
, NULL
);
837 i
= (*p
->width
)(p
, *seq
++);
848 * Common handling for Unicode and numbered
849 * character escape sequences.
853 if (p
->enc
== TERMENC_ASCII
) {
854 rhs
= ascii_uc2str(uc
);
857 if ((uc
< 0x20 && uc
!= 0x09) ||
858 (uc
> 0x7E && uc
< 0xA0))
860 sz
+= cond_width(p
, uc
, &skip
);
871 * Common handling for all escape sequences
872 * printing more than one character.
875 for (i
= 0; i
< rsz
; i
++)
876 sz
+= (*p
->width
)(p
, *rhs
++);
879 sz
+= cond_width(p
, ' ', &skip
);
883 sz
+= cond_width(p
, '-', &skip
);
895 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
902 r
= su
->scale
/ 40.0;
905 r
= su
->scale
* 6.0 / 2.54;
908 r
= su
->scale
* 65536.0 / 40.0;
914 r
= su
->scale
* 0.006;
920 r
= su
->scale
/ 12.0;
932 ri
= r
> 0.0 ? r
+ 0.4995 : r
- 0.4995;
933 return ri
< 66 ? ri
: 1;
937 * Convert a scaling width to basic units, rounding down.
940 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
943 return (*p
->hspan
)(p
, su
);