]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.261 2017/06/01 19:05:37 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
*p
, 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);
42 term_free(struct termp
*p
)
51 term_begin(struct termp
*p
, term_margin head
,
52 term_margin foot
, const struct roff_meta
*arg
)
62 term_end(struct termp
*p
)
69 * Flush a chunk of text. By default, break the output line each time
70 * the right margin is reached, and continue output on the next line
71 * at the same offset as the chunk itself. By default, also break the
72 * output line at the end of the chunk.
73 * The following flags may be specified:
75 * - TERMP_NOBREAK: Do not break the output line at the right margin,
76 * but only at the max right margin. Also, do not break the output
77 * line at the end of the chunk, such that the next call can pad to
78 * the next column. However, if less than p->trailspace blanks,
79 * which can be 0, 1, or 2, remain to the right margin, the line
81 * - TERMP_BRTRSP: Consider trailing whitespace significant
82 * when deciding whether the chunk fits or not.
83 * - TERMP_BRIND: If the chunk does not fit and the output line has
84 * to be broken, start the next line at the right margin instead
85 * of at the offset. Used together with TERMP_NOBREAK for the tags
86 * in various kinds of tagged lists.
87 * - TERMP_DANGLE: Do not break the output line at the right margin,
88 * append the next chunk after it even if this one is too long.
89 * To be used together with TERMP_NOBREAK.
90 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
91 * the next chunk if this column is not full.
94 term_flushln(struct termp
*p
)
96 size_t i
; /* current input position in p->buf */
97 int ntab
; /* number of tabs to prepend */
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->buf */
104 size_t jhy
; /* last hyph before overflow w/r/t j */
105 size_t maxvis
; /* output position of visible boundary */
108 * First, establish the maximum columns of "visible" content.
109 * This is usually the difference between the right-margin and
110 * an indentation, but can be, for tagged lists or columns, a
111 * small set of values.
113 * The following unsigned-signed subtractions look strange,
114 * but they are actually correct. If the int p->overstep
115 * is negative, it gets sign extended. Subtracting that
116 * very large size_t effectively adds a small number to dv.
118 dv
= p
->rmargin
> p
->offset
? p
->rmargin
- p
->offset
: 0;
119 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
121 if (p
->flags
& TERMP_NOBREAK
) {
122 dv
= p
->maxrmargin
> p
->offset
?
123 p
->maxrmargin
- p
->offset
: 0;
124 bp
= (int)dv
> p
->overstep
?
125 dv
- (size_t)p
->overstep
: 0;
130 * Calculate the required amount of padding.
132 vbl
= p
->offset
+ p
->overstep
> p
->viscol
?
133 p
->offset
+ p
->overstep
- p
->viscol
: 0;
140 * Handle literal tab characters: collapse all
141 * subsequent tabs into a single huge set of spaces.
144 while (i
< p
->col
&& p
->buf
[i
] == '\t') {
145 vend
= term_tab_next(vis
);
153 * Count up visible word characters. Control sequences
154 * (starting with the CSI) aren't counted. A space
155 * generates a non-printing word, which is valid (the
156 * space is printed according to regular spacing rules).
159 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
160 if (' ' == p
->buf
[j
] || '\t' == p
->buf
[j
])
163 /* Back over the last printed character. */
164 if (8 == p
->buf
[j
]) {
166 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
171 /* Break at the hyphen point if we overrun. */
172 if (vend
> vis
&& vend
< bp
&&
173 (ASCII_HYPH
== p
->buf
[j
] ||
174 ASCII_BREAK
== p
->buf
[j
]))
178 * Hyphenation now decided, put back a real
179 * hyphen such that we get the correct width.
181 if (ASCII_HYPH
== p
->buf
[j
])
184 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
188 * Find out whether we would exceed the right margin.
189 * If so, break to the next line.
191 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
196 /* Use pending tabs on the new line. */
200 vbl
= term_tab_next(vbl
);
202 /* Re-establish indentation. */
204 if (p
->flags
& TERMP_BRIND
) {
206 vend
+= p
->rmargin
- p
->offset
;
211 * Remove the p->overstep width.
212 * Again, if p->overstep is negative,
213 * sign extension does the right thing.
216 bp
+= (size_t)p
->overstep
;
220 /* Write out the [remaining] word. */
221 for ( ; i
< p
->col
; i
++) {
222 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
224 if ('\t' == p
->buf
[i
])
226 if (' ' == p
->buf
[i
]) {
228 while (i
< p
->col
&& ' ' == p
->buf
[i
])
230 dv
= (i
- j
) * (*p
->width
)(p
, ' ');
235 if (ASCII_NBRSP
== p
->buf
[i
]) {
236 vbl
+= (*p
->width
)(p
, ' ');
239 if (ASCII_BREAK
== p
->buf
[i
])
243 * Now we definitely know there will be
244 * printable characters to output,
245 * so write preceding white space now.
248 (*p
->advance
)(p
, vbl
);
253 (*p
->letter
)(p
, p
->buf
[i
]);
255 p
->viscol
-= (*p
->width
)(p
, p
->buf
[i
-1]);
257 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
263 * If there was trailing white space, it was not printed;
264 * so reset the cursor position accordingly.
273 p
->flags
&= ~(TERMP_BACKAFTER
| TERMP_BACKBEFORE
);
275 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
281 if (TERMP_HANG
& p
->flags
) {
282 p
->overstep
+= (int)(p
->offset
+ vis
- p
->rmargin
+
283 p
->trailspace
* (*p
->width
)(p
, ' '));
286 * If we have overstepped the margin, temporarily move
287 * it to the right and flag the rest of the line to be
289 * If there is a request to keep the columns together,
290 * allow negative overstep when the column is not full.
292 if (p
->trailspace
&& p
->overstep
< 0)
296 } else if (TERMP_DANGLE
& p
->flags
)
299 /* Trailing whitespace is significant in some columns. */
300 if (vis
&& vbl
&& (TERMP_BRTRSP
& p
->flags
))
303 /* If the column was overrun, break the line. */
304 if (maxvis
< vis
+ p
->trailspace
* (*p
->width
)(p
, ' ')) {
311 * A newline only breaks an existing line; it won't assert vertical
312 * space. All data in the output buffer is flushed prior to the newline
316 term_newln(struct termp
*p
)
319 p
->flags
|= TERMP_NOSPACE
;
320 if (p
->col
|| p
->viscol
)
325 * Asserts a vertical space (a full, empty line-break between lines).
326 * Note that if used twice, this will cause two blank spaces and so on.
327 * All data in the output buffer is flushed prior to the newline
331 term_vspace(struct termp
*p
)
342 /* Swap current and previous font; for \fP and .ft P */
344 term_fontlast(struct termp
*p
)
349 p
->fontl
= p
->fontq
[p
->fonti
];
350 p
->fontq
[p
->fonti
] = f
;
353 /* Set font, save current, discard previous; for \f, .ft, .B etc. */
355 term_fontrepl(struct termp
*p
, enum termfont f
)
358 p
->fontl
= p
->fontq
[p
->fonti
];
359 p
->fontq
[p
->fonti
] = f
;
362 /* Set font, save previous. */
364 term_fontpush(struct termp
*p
, enum termfont f
)
367 p
->fontl
= p
->fontq
[p
->fonti
];
368 if (++p
->fonti
== p
->fontsz
) {
370 p
->fontq
= mandoc_reallocarray(p
->fontq
,
371 p
->fontsz
, sizeof(*p
->fontq
));
373 p
->fontq
[p
->fonti
] = f
;
376 /* Flush to make the saved pointer current again. */
378 term_fontpopq(struct termp
*p
, int i
)
386 /* Pop one font off the stack. */
388 term_fontpop(struct termp
*p
)
396 * Handle pwords, partial words, which may be either a single word or a
397 * phrase that cannot be broken down (such as a literal string). This
398 * handles word styling.
401 term_word(struct termp
*p
, const char *word
)
404 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
405 const char *seq
, *cp
;
410 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
411 if ( ! (TERMP_KEEP
& p
->flags
)) {
413 if (TERMP_SENTENCE
& p
->flags
)
416 bufferc(p
, ASCII_NBRSP
);
418 if (TERMP_PREKEEP
& p
->flags
)
419 p
->flags
|= TERMP_KEEP
;
421 if ( ! (p
->flags
& TERMP_NONOSPACE
))
422 p
->flags
&= ~TERMP_NOSPACE
;
424 p
->flags
|= TERMP_NOSPACE
;
426 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_NONEWLINE
);
429 while ('\0' != *word
) {
431 if (TERMP_NBRWORD
& p
->flags
) {
437 ssz
= strcspn(word
, "\\ ");
439 ssz
= strcspn(word
, "\\");
440 encode(p
, word
, ssz
);
446 esc
= mandoc_escape(&word
, &seq
, &sz
);
447 if (ESCAPE_ERROR
== esc
)
452 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
454 case ESCAPE_NUMBERED
:
455 uc
= mchars_num2char(seq
, sz
);
460 if (p
->enc
== TERMENC_ASCII
) {
461 cp
= mchars_spec2str(seq
, sz
, &ssz
);
465 uc
= mchars_spec2cp(seq
, sz
);
470 case ESCAPE_FONTBOLD
:
471 term_fontrepl(p
, TERMFONT_BOLD
);
473 case ESCAPE_FONTITALIC
:
474 term_fontrepl(p
, TERMFONT_UNDER
);
477 term_fontrepl(p
, TERMFONT_BI
);
480 case ESCAPE_FONTROMAN
:
481 term_fontrepl(p
, TERMFONT_NONE
);
483 case ESCAPE_FONTPREV
:
487 if (p
->flags
& TERMP_BACKAFTER
)
488 p
->flags
&= ~TERMP_BACKAFTER
;
489 else if (*word
== '\0')
490 p
->flags
|= (TERMP_NOSPACE
| TERMP_NONEWLINE
);
493 if (a2roffsu(seq
, &su
, SCALE_EM
) == 0)
495 uc
= term_hspan(p
, &su
) / 24;
498 bufferc(p
, ASCII_NBRSP
);
499 else if (p
->col
> (size_t)(-uc
))
504 if (p
->offset
> (size_t)(-uc
)) {
513 case ESCAPE_SKIPCHAR
:
514 p
->flags
|= TERMP_BACKAFTER
;
516 case ESCAPE_OVERSTRIKE
:
520 mandoc_escape(&seq
, NULL
, NULL
);
525 if (p
->flags
& TERMP_BACKBEFORE
)
526 p
->flags
|= TERMP_BACKAFTER
;
528 p
->flags
|= TERMP_BACKBEFORE
;
531 /* Trim trailing backspace/blank pair. */
533 (p
->buf
[p
->col
- 1] == ' ' ||
534 p
->buf
[p
->col
- 1] == '\t'))
542 * Common handling for Unicode and numbered
543 * character escape sequences.
546 if (p
->enc
== TERMENC_ASCII
) {
547 cp
= ascii_uc2str(uc
);
548 encode(p
, cp
, strlen(cp
));
550 if ((uc
< 0x20 && uc
!= 0x09) ||
551 (uc
> 0x7E && uc
< 0xA0))
556 p
->flags
&= ~TERMP_NBRWORD
;
560 adjbuf(struct termp
*p
, size_t sz
)
565 while (sz
>= p
->maxcols
)
568 p
->buf
= mandoc_reallocarray(p
->buf
, p
->maxcols
, sizeof(int));
572 bufferc(struct termp
*p
, char c
)
575 if (p
->col
+ 1 >= p
->maxcols
)
576 adjbuf(p
, p
->col
+ 1);
578 p
->buf
[p
->col
++] = c
;
583 * Do this for a single (probably unicode) value.
584 * Does not check for non-decorated glyphs.
587 encode1(struct termp
*p
, int c
)
591 if (p
->col
+ 7 >= p
->maxcols
)
592 adjbuf(p
, p
->col
+ 7);
594 f
= (c
== ASCII_HYPH
|| c
> 127 || isgraph(c
)) ?
595 p
->fontq
[p
->fonti
] : TERMFONT_NONE
;
597 if (p
->flags
& TERMP_BACKBEFORE
) {
598 if (p
->buf
[p
->col
- 1] == ' ' || p
->buf
[p
->col
- 1] == '\t')
601 p
->buf
[p
->col
++] = 8;
602 p
->flags
&= ~TERMP_BACKBEFORE
;
604 if (TERMFONT_UNDER
== f
|| TERMFONT_BI
== f
) {
605 p
->buf
[p
->col
++] = '_';
606 p
->buf
[p
->col
++] = 8;
608 if (TERMFONT_BOLD
== f
|| TERMFONT_BI
== f
) {
610 p
->buf
[p
->col
++] = '-';
612 p
->buf
[p
->col
++] = c
;
613 p
->buf
[p
->col
++] = 8;
615 p
->buf
[p
->col
++] = c
;
616 if (p
->flags
& TERMP_BACKAFTER
) {
617 p
->flags
|= TERMP_BACKBEFORE
;
618 p
->flags
&= ~TERMP_BACKAFTER
;
623 encode(struct termp
*p
, const char *word
, size_t sz
)
627 if (p
->col
+ 2 + (sz
* 5) >= p
->maxcols
)
628 adjbuf(p
, p
->col
+ 2 + (sz
* 5));
630 for (i
= 0; i
< sz
; i
++) {
631 if (ASCII_HYPH
== word
[i
] ||
632 isgraph((unsigned char)word
[i
]))
635 p
->buf
[p
->col
++] = word
[i
];
638 * Postpone the effect of \z while handling
639 * an overstrike sequence from ascii_uc2str().
642 if (word
[i
] == '\b' &&
643 (p
->flags
& TERMP_BACKBEFORE
)) {
644 p
->flags
&= ~TERMP_BACKBEFORE
;
645 p
->flags
|= TERMP_BACKAFTER
;
652 term_setwidth(struct termp
*p
, const char *wstr
)
672 if (a2roffsu(wstr
, &su
, SCALE_MAX
))
673 width
= term_hspan(p
, &su
);
677 (*p
->setwidth
)(p
, iop
, width
);
681 term_len(const struct termp
*p
, size_t sz
)
684 return (*p
->width
)(p
, ' ') * sz
;
688 cond_width(const struct termp
*p
, int c
, int *skip
)
695 return (*p
->width
)(p
, c
);
699 term_strlen(const struct termp
*p
, const char *cp
)
703 const char *seq
, *rhs
;
705 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
709 * Account for escaped sequences within string length
710 * calculations. This follows the logic in term_word() as we
711 * must calculate the width of produced strings.
716 while ('\0' != *cp
) {
717 rsz
= strcspn(cp
, rej
);
718 for (i
= 0; i
< rsz
; i
++)
719 sz
+= cond_width(p
, *cp
++, &skip
);
724 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
725 if (ESCAPE_ERROR
== esc
)
732 uc
= mchars_num2uc(seq
+ 1, ssz
- 1);
734 case ESCAPE_NUMBERED
:
735 uc
= mchars_num2char(seq
, ssz
);
740 if (p
->enc
== TERMENC_ASCII
) {
741 rhs
= mchars_spec2str(seq
, ssz
, &rsz
);
745 uc
= mchars_spec2cp(seq
, ssz
);
747 sz
+= cond_width(p
, uc
, &skip
);
750 case ESCAPE_SKIPCHAR
:
753 case ESCAPE_OVERSTRIKE
:
758 mandoc_escape(&seq
, NULL
, NULL
);
761 i
= (*p
->width
)(p
, *seq
++);
772 * Common handling for Unicode and numbered
773 * character escape sequences.
777 if (p
->enc
== TERMENC_ASCII
) {
778 rhs
= ascii_uc2str(uc
);
781 if ((uc
< 0x20 && uc
!= 0x09) ||
782 (uc
> 0x7E && uc
< 0xA0))
784 sz
+= cond_width(p
, uc
, &skip
);
795 * Common handling for all escape sequences
796 * printing more than one character.
799 for (i
= 0; i
< rsz
; i
++)
800 sz
+= (*p
->width
)(p
, *rhs
++);
803 sz
+= cond_width(p
, ' ', &skip
);
807 sz
+= cond_width(p
, '-', &skip
);
819 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
826 r
= su
->scale
/ 40.0;
829 r
= su
->scale
* 6.0 / 2.54;
832 r
= su
->scale
* 65536.0 / 40.0;
838 r
= su
->scale
* 0.006;
844 r
= su
->scale
/ 12.0;
856 ri
= r
> 0.0 ? r
+ 0.4995 : r
- 0.4995;
857 return ri
< 66 ? ri
: 1;
861 * Convert a scaling width to basic units, rounding down.
864 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
867 return (*p
->hspan
)(p
, su
);