]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.259 2017/01/08 18:16:58 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
&& '\t' == p
->buf
[i
]) {
145 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
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) {
195 if (TERMP_BRIND
& p
->flags
) {
202 /* use pending tabs on the new line */
205 vbl
+= ntab
* p
->tabwidth
;
208 * Remove the p->overstep width.
209 * Again, if p->overstep is negative,
210 * sign extension does the right thing.
213 bp
+= (size_t)p
->overstep
;
217 /* Write out the [remaining] word. */
218 for ( ; i
< p
->col
; i
++) {
219 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
221 if ('\t' == p
->buf
[i
])
223 if (' ' == p
->buf
[i
]) {
225 while (i
< p
->col
&& ' ' == p
->buf
[i
])
227 dv
= (i
- j
) * (*p
->width
)(p
, ' ');
232 if (ASCII_NBRSP
== p
->buf
[i
]) {
233 vbl
+= (*p
->width
)(p
, ' ');
236 if (ASCII_BREAK
== p
->buf
[i
])
240 * Now we definitely know there will be
241 * printable characters to output,
242 * so write preceding white space now.
245 (*p
->advance
)(p
, vbl
);
250 (*p
->letter
)(p
, p
->buf
[i
]);
252 p
->viscol
-= (*p
->width
)(p
, p
->buf
[i
-1]);
254 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
260 * If there was trailing white space, it was not printed;
261 * so reset the cursor position accordingly.
270 p
->flags
&= ~(TERMP_BACKAFTER
| TERMP_BACKBEFORE
);
272 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
278 if (TERMP_HANG
& p
->flags
) {
279 p
->overstep
+= (int)(p
->offset
+ vis
- p
->rmargin
+
280 p
->trailspace
* (*p
->width
)(p
, ' '));
283 * If we have overstepped the margin, temporarily move
284 * it to the right and flag the rest of the line to be
286 * If there is a request to keep the columns together,
287 * allow negative overstep when the column is not full.
289 if (p
->trailspace
&& p
->overstep
< 0)
293 } else if (TERMP_DANGLE
& p
->flags
)
296 /* Trailing whitespace is significant in some columns. */
297 if (vis
&& vbl
&& (TERMP_BRTRSP
& p
->flags
))
300 /* If the column was overrun, break the line. */
301 if (maxvis
< vis
+ p
->trailspace
* (*p
->width
)(p
, ' ')) {
308 * A newline only breaks an existing line; it won't assert vertical
309 * space. All data in the output buffer is flushed prior to the newline
313 term_newln(struct termp
*p
)
316 p
->flags
|= TERMP_NOSPACE
;
317 if (p
->col
|| p
->viscol
)
322 * Asserts a vertical space (a full, empty line-break between lines).
323 * Note that if used twice, this will cause two blank spaces and so on.
324 * All data in the output buffer is flushed prior to the newline
328 term_vspace(struct termp
*p
)
339 /* Swap current and previous font; for \fP and .ft P */
341 term_fontlast(struct termp
*p
)
346 p
->fontl
= p
->fontq
[p
->fonti
];
347 p
->fontq
[p
->fonti
] = f
;
350 /* Set font, save current, discard previous; for \f, .ft, .B etc. */
352 term_fontrepl(struct termp
*p
, enum termfont f
)
355 p
->fontl
= p
->fontq
[p
->fonti
];
356 p
->fontq
[p
->fonti
] = f
;
359 /* Set font, save previous. */
361 term_fontpush(struct termp
*p
, enum termfont f
)
364 p
->fontl
= p
->fontq
[p
->fonti
];
365 if (++p
->fonti
== p
->fontsz
) {
367 p
->fontq
= mandoc_reallocarray(p
->fontq
,
368 p
->fontsz
, sizeof(*p
->fontq
));
370 p
->fontq
[p
->fonti
] = f
;
373 /* Flush to make the saved pointer current again. */
375 term_fontpopq(struct termp
*p
, int i
)
383 /* Pop one font off the stack. */
385 term_fontpop(struct termp
*p
)
393 * Handle pwords, partial words, which may be either a single word or a
394 * phrase that cannot be broken down (such as a literal string). This
395 * handles word styling.
398 term_word(struct termp
*p
, const char *word
)
400 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
401 const char *seq
, *cp
;
406 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
407 if ( ! (TERMP_KEEP
& p
->flags
)) {
409 if (TERMP_SENTENCE
& p
->flags
)
412 bufferc(p
, ASCII_NBRSP
);
414 if (TERMP_PREKEEP
& p
->flags
)
415 p
->flags
|= TERMP_KEEP
;
417 if ( ! (p
->flags
& TERMP_NONOSPACE
))
418 p
->flags
&= ~TERMP_NOSPACE
;
420 p
->flags
|= TERMP_NOSPACE
;
422 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_NONEWLINE
);
425 while ('\0' != *word
) {
427 if (TERMP_NBRWORD
& p
->flags
) {
433 ssz
= strcspn(word
, "\\ ");
435 ssz
= strcspn(word
, "\\");
436 encode(p
, word
, ssz
);
442 esc
= mandoc_escape(&word
, &seq
, &sz
);
443 if (ESCAPE_ERROR
== esc
)
448 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
450 case ESCAPE_NUMBERED
:
451 uc
= mchars_num2char(seq
, sz
);
456 if (p
->enc
== TERMENC_ASCII
) {
457 cp
= mchars_spec2str(seq
, sz
, &ssz
);
461 uc
= mchars_spec2cp(seq
, sz
);
466 case ESCAPE_FONTBOLD
:
467 term_fontrepl(p
, TERMFONT_BOLD
);
469 case ESCAPE_FONTITALIC
:
470 term_fontrepl(p
, TERMFONT_UNDER
);
473 term_fontrepl(p
, TERMFONT_BI
);
476 case ESCAPE_FONTROMAN
:
477 term_fontrepl(p
, TERMFONT_NONE
);
479 case ESCAPE_FONTPREV
:
483 if (p
->flags
& TERMP_BACKAFTER
)
484 p
->flags
&= ~TERMP_BACKAFTER
;
485 else if (*word
== '\0')
486 p
->flags
|= (TERMP_NOSPACE
| TERMP_NONEWLINE
);
488 case ESCAPE_SKIPCHAR
:
489 p
->flags
|= TERMP_BACKAFTER
;
491 case ESCAPE_OVERSTRIKE
:
495 mandoc_escape(&seq
, NULL
, NULL
);
500 if (p
->flags
& TERMP_BACKBEFORE
)
501 p
->flags
|= TERMP_BACKAFTER
;
503 p
->flags
|= TERMP_BACKBEFORE
;
506 /* Trim trailing backspace/blank pair. */
508 (p
->buf
[p
->col
- 1] == ' ' ||
509 p
->buf
[p
->col
- 1] == '\t'))
517 * Common handling for Unicode and numbered
518 * character escape sequences.
521 if (p
->enc
== TERMENC_ASCII
) {
522 cp
= ascii_uc2str(uc
);
523 encode(p
, cp
, strlen(cp
));
525 if ((uc
< 0x20 && uc
!= 0x09) ||
526 (uc
> 0x7E && uc
< 0xA0))
531 p
->flags
&= ~TERMP_NBRWORD
;
535 adjbuf(struct termp
*p
, size_t sz
)
540 while (sz
>= p
->maxcols
)
543 p
->buf
= mandoc_reallocarray(p
->buf
, p
->maxcols
, sizeof(int));
547 bufferc(struct termp
*p
, char c
)
550 if (p
->col
+ 1 >= p
->maxcols
)
551 adjbuf(p
, p
->col
+ 1);
553 p
->buf
[p
->col
++] = c
;
558 * Do this for a single (probably unicode) value.
559 * Does not check for non-decorated glyphs.
562 encode1(struct termp
*p
, int c
)
566 if (p
->col
+ 7 >= p
->maxcols
)
567 adjbuf(p
, p
->col
+ 7);
569 f
= (c
== ASCII_HYPH
|| c
> 127 || isgraph(c
)) ?
570 p
->fontq
[p
->fonti
] : TERMFONT_NONE
;
572 if (p
->flags
& TERMP_BACKBEFORE
) {
573 if (p
->buf
[p
->col
- 1] == ' ' || p
->buf
[p
->col
- 1] == '\t')
576 p
->buf
[p
->col
++] = 8;
577 p
->flags
&= ~TERMP_BACKBEFORE
;
579 if (TERMFONT_UNDER
== f
|| TERMFONT_BI
== f
) {
580 p
->buf
[p
->col
++] = '_';
581 p
->buf
[p
->col
++] = 8;
583 if (TERMFONT_BOLD
== f
|| TERMFONT_BI
== f
) {
585 p
->buf
[p
->col
++] = '-';
587 p
->buf
[p
->col
++] = c
;
588 p
->buf
[p
->col
++] = 8;
590 p
->buf
[p
->col
++] = c
;
591 if (p
->flags
& TERMP_BACKAFTER
) {
592 p
->flags
|= TERMP_BACKBEFORE
;
593 p
->flags
&= ~TERMP_BACKAFTER
;
598 encode(struct termp
*p
, const char *word
, size_t sz
)
602 if (p
->col
+ 2 + (sz
* 5) >= p
->maxcols
)
603 adjbuf(p
, p
->col
+ 2 + (sz
* 5));
605 for (i
= 0; i
< sz
; i
++) {
606 if (ASCII_HYPH
== word
[i
] ||
607 isgraph((unsigned char)word
[i
]))
610 p
->buf
[p
->col
++] = word
[i
];
613 * Postpone the effect of \z while handling
614 * an overstrike sequence from ascii_uc2str().
617 if (word
[i
] == '\b' &&
618 (p
->flags
& TERMP_BACKBEFORE
)) {
619 p
->flags
&= ~TERMP_BACKBEFORE
;
620 p
->flags
|= TERMP_BACKAFTER
;
627 term_setwidth(struct termp
*p
, const char *wstr
)
647 if (a2roffsu(wstr
, &su
, SCALE_MAX
))
648 width
= term_hspan(p
, &su
);
652 (*p
->setwidth
)(p
, iop
, width
);
656 term_len(const struct termp
*p
, size_t sz
)
659 return (*p
->width
)(p
, ' ') * sz
;
663 cond_width(const struct termp
*p
, int c
, int *skip
)
670 return (*p
->width
)(p
, c
);
674 term_strlen(const struct termp
*p
, const char *cp
)
678 const char *seq
, *rhs
;
680 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
684 * Account for escaped sequences within string length
685 * calculations. This follows the logic in term_word() as we
686 * must calculate the width of produced strings.
691 while ('\0' != *cp
) {
692 rsz
= strcspn(cp
, rej
);
693 for (i
= 0; i
< rsz
; i
++)
694 sz
+= cond_width(p
, *cp
++, &skip
);
699 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
700 if (ESCAPE_ERROR
== esc
)
707 uc
= mchars_num2uc(seq
+ 1, ssz
- 1);
709 case ESCAPE_NUMBERED
:
710 uc
= mchars_num2char(seq
, ssz
);
715 if (p
->enc
== TERMENC_ASCII
) {
716 rhs
= mchars_spec2str(seq
, ssz
, &rsz
);
720 uc
= mchars_spec2cp(seq
, ssz
);
722 sz
+= cond_width(p
, uc
, &skip
);
725 case ESCAPE_SKIPCHAR
:
728 case ESCAPE_OVERSTRIKE
:
733 mandoc_escape(&seq
, NULL
, NULL
);
736 i
= (*p
->width
)(p
, *seq
++);
747 * Common handling for Unicode and numbered
748 * character escape sequences.
752 if (p
->enc
== TERMENC_ASCII
) {
753 rhs
= ascii_uc2str(uc
);
756 if ((uc
< 0x20 && uc
!= 0x09) ||
757 (uc
> 0x7E && uc
< 0xA0))
759 sz
+= cond_width(p
, uc
, &skip
);
770 * Common handling for all escape sequences
771 * printing more than one character.
774 for (i
= 0; i
< rsz
; i
++)
775 sz
+= (*p
->width
)(p
, *rhs
++);
778 sz
+= cond_width(p
, ' ', &skip
);
782 sz
+= cond_width(p
, '-', &skip
);
794 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
801 r
= su
->scale
/ 40.0;
804 r
= su
->scale
* 6.0 / 2.54;
807 r
= su
->scale
* 65536.0 / 40.0;
813 r
= su
->scale
* 0.006;
819 r
= su
->scale
/ 12.0;
831 ri
= r
> 0.0 ? r
+ 0.4995 : r
- 0.4995;
832 return ri
< 66 ? ri
: 1;
836 * Convert a scaling width to basic units, rounding down.
839 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
842 return (*p
->hspan
)(p
, su
);