]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.242 2014/12/24 23:32:42 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 void *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_BRIND: If the chunk does not fit and the output line has
82 * to be broken, start the next line at the right margin instead
83 * of at the offset. Used together with TERMP_NOBREAK for the tags
84 * in various kinds of tagged lists.
85 * - TERMP_DANGLE: Do not break the output line at the right margin,
86 * append the next chunk after it even if this one is too long.
87 * To be used together with TERMP_NOBREAK.
88 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
89 * the next chunk if this column is not full.
92 term_flushln(struct termp
*p
)
94 size_t i
; /* current input position in p->buf */
95 int ntab
; /* number of tabs to prepend */
96 size_t vis
; /* current visual position on output */
97 size_t vbl
; /* number of blanks to prepend to output */
98 size_t vend
; /* end of word visual position on output */
99 size_t bp
; /* visual right border position */
100 size_t dv
; /* temporary for visual pos calculations */
101 size_t j
; /* temporary loop index for p->buf */
102 size_t jhy
; /* last hyph before overflow w/r/t j */
103 size_t maxvis
; /* output position of visible boundary */
106 * First, establish the maximum columns of "visible" content.
107 * This is usually the difference between the right-margin and
108 * an indentation, but can be, for tagged lists or columns, a
109 * small set of values.
111 * The following unsigned-signed subtractions look strange,
112 * but they are actually correct. If the int p->overstep
113 * is negative, it gets sign extended. Subtracting that
114 * very large size_t effectively adds a small number to dv.
116 dv
= p
->rmargin
> p
->offset
? p
->rmargin
- p
->offset
: 0;
117 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
119 if (p
->flags
& TERMP_NOBREAK
) {
120 dv
= p
->maxrmargin
> p
->offset
?
121 p
->maxrmargin
- p
->offset
: 0;
122 bp
= (int)dv
> p
->overstep
?
123 dv
- (size_t)p
->overstep
: 0;
128 * Calculate the required amount of padding.
130 vbl
= p
->offset
+ p
->overstep
> p
->viscol
?
131 p
->offset
+ p
->overstep
- p
->viscol
: 0;
138 * Handle literal tab characters: collapse all
139 * subsequent tabs into a single huge set of spaces.
142 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
143 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
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).
157 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
158 if (' ' == p
->buf
[j
] || '\t' == p
->buf
[j
])
161 /* Back over the the last printed character. */
162 if (8 == p
->buf
[j
]) {
164 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
169 /* Break at the hyphen point if we overrun. */
170 if (vend
> vis
&& vend
< bp
&&
171 (ASCII_HYPH
== p
->buf
[j
] ||
172 ASCII_BREAK
== p
->buf
[j
]))
176 * Hyphenation now decided, put back a real
177 * hyphen such that we get the correct width.
179 if (ASCII_HYPH
== p
->buf
[j
])
182 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
186 * Find out whether we would exceed the right margin.
187 * If so, break to the next line.
189 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
193 if (TERMP_BRIND
& p
->flags
) {
200 /* use pending tabs on the new line */
203 vbl
+= ntab
* p
->tabwidth
;
206 * Remove the p->overstep width.
207 * Again, if p->overstep is negative,
208 * sign extension does the right thing.
211 bp
+= (size_t)p
->overstep
;
215 /* Write out the [remaining] word. */
216 for ( ; i
< p
->col
; i
++) {
217 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
219 if ('\t' == p
->buf
[i
])
221 if (' ' == p
->buf
[i
]) {
223 while (i
< p
->col
&& ' ' == p
->buf
[i
])
225 dv
= (i
- j
) * (*p
->width
)(p
, ' ');
230 if (ASCII_NBRSP
== p
->buf
[i
]) {
231 vbl
+= (*p
->width
)(p
, ' ');
234 if (ASCII_BREAK
== p
->buf
[i
])
238 * Now we definitely know there will be
239 * printable characters to output,
240 * so write preceding white space now.
243 (*p
->advance
)(p
, vbl
);
248 (*p
->letter
)(p
, p
->buf
[i
]);
250 p
->viscol
-= (*p
->width
)(p
, p
->buf
[i
-1]);
252 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
258 * If there was trailing white space, it was not printed;
259 * so reset the cursor position accordingly.
269 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
275 if (TERMP_HANG
& p
->flags
) {
276 p
->overstep
+= (int)(p
->offset
+ vis
- p
->rmargin
+
277 p
->trailspace
* (*p
->width
)(p
, ' '));
280 * If we have overstepped the margin, temporarily move
281 * it to the right and flag the rest of the line to be
283 * If there is a request to keep the columns together,
284 * allow negative overstep when the column is not full.
286 if (p
->trailspace
&& p
->overstep
< 0)
290 } else if (TERMP_DANGLE
& p
->flags
)
293 /* If the column was overrun, break the line. */
294 if (maxvis
< vis
+ p
->trailspace
* (*p
->width
)(p
, ' ')) {
301 * A newline only breaks an existing line; it won't assert vertical
302 * space. All data in the output buffer is flushed prior to the newline
306 term_newln(struct termp
*p
)
309 p
->flags
|= TERMP_NOSPACE
;
310 if (p
->col
|| p
->viscol
)
315 * Asserts a vertical space (a full, empty line-break between lines).
316 * Note that if used twice, this will cause two blank spaces and so on.
317 * All data in the output buffer is flushed prior to the newline
321 term_vspace(struct termp
*p
)
332 /* Swap current and previous font; for \fP and .ft P */
334 term_fontlast(struct termp
*p
)
339 p
->fontl
= p
->fontq
[p
->fonti
];
340 p
->fontq
[p
->fonti
] = f
;
343 /* Set font, save current, discard previous; for \f, .ft, .B etc. */
345 term_fontrepl(struct termp
*p
, enum termfont f
)
348 p
->fontl
= p
->fontq
[p
->fonti
];
349 p
->fontq
[p
->fonti
] = f
;
352 /* Set font, save previous. */
354 term_fontpush(struct termp
*p
, enum termfont f
)
357 p
->fontl
= p
->fontq
[p
->fonti
];
358 if (++p
->fonti
== p
->fontsz
) {
360 p
->fontq
= mandoc_reallocarray(p
->fontq
,
361 p
->fontsz
, sizeof(enum termfont
*));
363 p
->fontq
[p
->fonti
] = f
;
366 /* Retrieve pointer to current font. */
367 const enum termfont
*
368 term_fontq(struct termp
*p
)
371 return(&p
->fontq
[p
->fonti
]);
374 /* Flush to make the saved pointer current again. */
376 term_fontpopq(struct termp
*p
, const enum termfont
*key
)
379 while (p
->fonti
>= 0 && key
< p
->fontq
+ p
->fonti
)
381 assert(p
->fonti
>= 0);
384 /* Pop one font off the stack. */
386 term_fontpop(struct termp
*p
)
394 * Handle pwords, partial words, which may be either a single word or a
395 * phrase that cannot be broken down (such as a literal string). This
396 * handles word styling.
399 term_word(struct termp
*p
, const char *word
)
401 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
402 const char *seq
, *cp
;
407 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
408 if ( ! (TERMP_KEEP
& p
->flags
)) {
410 if (TERMP_SENTENCE
& p
->flags
)
413 bufferc(p
, ASCII_NBRSP
);
415 if (TERMP_PREKEEP
& p
->flags
)
416 p
->flags
|= TERMP_KEEP
;
418 if ( ! (p
->flags
& TERMP_NONOSPACE
))
419 p
->flags
&= ~TERMP_NOSPACE
;
421 p
->flags
|= TERMP_NOSPACE
;
423 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_NONEWLINE
);
425 while ('\0' != *word
) {
427 if (TERMP_SKIPCHAR
& p
->flags
) {
428 p
->flags
&= ~TERMP_SKIPCHAR
;
432 if (TERMP_NBRWORD
& p
->flags
) {
438 ssz
= strcspn(word
, "\\ ");
440 ssz
= strcspn(word
, "\\");
441 encode(p
, word
, ssz
);
447 esc
= mandoc_escape(&word
, &seq
, &sz
);
448 if (ESCAPE_ERROR
== esc
)
453 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
455 case ESCAPE_NUMBERED
:
456 uc
= mchars_num2char(seq
, sz
);
461 if (p
->enc
== TERMENC_ASCII
) {
462 cp
= mchars_spec2str(p
->symtab
,
467 uc
= mchars_spec2cp(p
->symtab
, seq
, sz
);
472 case ESCAPE_FONTBOLD
:
473 term_fontrepl(p
, TERMFONT_BOLD
);
475 case ESCAPE_FONTITALIC
:
476 term_fontrepl(p
, TERMFONT_UNDER
);
479 term_fontrepl(p
, TERMFONT_BI
);
483 case ESCAPE_FONTROMAN
:
484 term_fontrepl(p
, TERMFONT_NONE
);
486 case ESCAPE_FONTPREV
:
490 if (TERMP_SKIPCHAR
& p
->flags
)
491 p
->flags
&= ~TERMP_SKIPCHAR
;
492 else if ('\0' == *word
)
493 p
->flags
|= (TERMP_NOSPACE
| TERMP_NONEWLINE
);
495 case ESCAPE_SKIPCHAR
:
496 p
->flags
|= TERMP_SKIPCHAR
;
503 * Common handling for Unicode and numbered
504 * character escape sequences.
507 if (p
->enc
== TERMENC_ASCII
) {
508 cp
= ascii_uc2str(uc
);
509 encode(p
, cp
, strlen(cp
));
511 if ((uc
< 0x20 && uc
!= 0x09) ||
512 (uc
> 0x7E && uc
< 0xA0))
517 p
->flags
&= ~TERMP_NBRWORD
;
521 adjbuf(struct termp
*p
, size_t sz
)
526 while (sz
>= p
->maxcols
)
529 p
->buf
= mandoc_reallocarray(p
->buf
, p
->maxcols
, sizeof(int));
533 bufferc(struct termp
*p
, char c
)
536 if (p
->col
+ 1 >= p
->maxcols
)
537 adjbuf(p
, p
->col
+ 1);
539 p
->buf
[p
->col
++] = c
;
544 * Do this for a single (probably unicode) value.
545 * Does not check for non-decorated glyphs.
548 encode1(struct termp
*p
, int c
)
552 if (TERMP_SKIPCHAR
& p
->flags
) {
553 p
->flags
&= ~TERMP_SKIPCHAR
;
557 if (p
->col
+ 6 >= p
->maxcols
)
558 adjbuf(p
, p
->col
+ 6);
562 if (TERMFONT_UNDER
== f
|| TERMFONT_BI
== f
) {
563 p
->buf
[p
->col
++] = '_';
564 p
->buf
[p
->col
++] = 8;
566 if (TERMFONT_BOLD
== f
|| TERMFONT_BI
== f
) {
568 p
->buf
[p
->col
++] = '-';
570 p
->buf
[p
->col
++] = c
;
571 p
->buf
[p
->col
++] = 8;
573 p
->buf
[p
->col
++] = c
;
577 encode(struct termp
*p
, const char *word
, size_t sz
)
581 if (TERMP_SKIPCHAR
& p
->flags
) {
582 p
->flags
&= ~TERMP_SKIPCHAR
;
587 * Encode and buffer a string of characters. If the current
588 * font mode is unset, buffer directly, else encode then buffer
589 * character by character.
592 if (*term_fontq(p
) == TERMFONT_NONE
) {
593 if (p
->col
+ sz
>= p
->maxcols
)
594 adjbuf(p
, p
->col
+ sz
);
595 for (i
= 0; i
< sz
; i
++)
596 p
->buf
[p
->col
++] = word
[i
];
600 /* Pre-buffer, assuming worst-case. */
602 if (p
->col
+ 1 + (sz
* 5) >= p
->maxcols
)
603 adjbuf(p
, p
->col
+ 1 + (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
];
615 term_setwidth(struct termp
*p
, const char *wstr
)
636 if (a2roffsu(wstr
, &su
, SCALE_MAX
))
637 width
= term_hspan(p
, &su
);
641 (*p
->setwidth
)(p
, iop
, width
);
645 term_len(const struct termp
*p
, size_t sz
)
648 return((*p
->width
)(p
, ' ') * sz
);
652 cond_width(const struct termp
*p
, int c
, int *skip
)
659 return((*p
->width
)(p
, c
));
663 term_strlen(const struct termp
*p
, const char *cp
)
667 const char *seq
, *rhs
;
669 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
673 * Account for escaped sequences within string length
674 * calculations. This follows the logic in term_word() as we
675 * must calculate the width of produced strings.
680 while ('\0' != *cp
) {
681 rsz
= strcspn(cp
, rej
);
682 for (i
= 0; i
< rsz
; i
++)
683 sz
+= cond_width(p
, *cp
++, &skip
);
688 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
689 if (ESCAPE_ERROR
== esc
)
696 uc
= mchars_num2uc(seq
+ 1, ssz
- 1);
698 case ESCAPE_NUMBERED
:
699 uc
= mchars_num2char(seq
, ssz
);
704 if (p
->enc
== TERMENC_ASCII
) {
705 rhs
= mchars_spec2str(p
->symtab
,
710 uc
= mchars_spec2cp(p
->symtab
,
713 sz
+= cond_width(p
, uc
, &skip
);
716 case ESCAPE_SKIPCHAR
:
724 * Common handling for Unicode and numbered
725 * character escape sequences.
729 if (p
->enc
== TERMENC_ASCII
) {
730 rhs
= ascii_uc2str(uc
);
733 if ((uc
< 0x20 && uc
!= 0x09) ||
734 (uc
> 0x7E && uc
< 0xA0))
736 sz
+= cond_width(p
, uc
, &skip
);
747 * Common handling for all escape sequences
748 * printing more than one character.
751 for (i
= 0; i
< rsz
; i
++)
752 sz
+= (*p
->width
)(p
, *rhs
++);
755 sz
+= cond_width(p
, ' ', &skip
);
759 sz
+= cond_width(p
, '-', &skip
);
773 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
780 r
= su
->scale
/ 40.0;
783 r
= su
->scale
* 6.0 / 2.54;
786 r
= su
->scale
* 65536.0 / 40.0;
792 r
= su
->scale
* 0.006;
798 r
= su
->scale
/ 12.0;
812 ri
= r
> 0.0 ? r
+ 0.4995 : r
- 0.4995;
813 return(ri
< 66 ? ri
: 1);
817 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
821 v
= (*p
->hspan
)(p
, su
);
822 return(v
> 0.0 ? v
+ 0.0005 : v
- 0.0005);