]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.237 2014/12/02 10:08:06 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
)
50 term_begin(struct termp
*p
, term_margin head
,
51 term_margin foot
, const void *arg
)
61 term_end(struct termp
*p
)
68 * Flush a chunk of text. By default, break the output line each time
69 * the right margin is reached, and continue output on the next line
70 * at the same offset as the chunk itself. By default, also break the
71 * output line at the end of the chunk.
72 * The following flags may be specified:
74 * - TERMP_NOBREAK: Do not break the output line at the right margin,
75 * but only at the max right margin. Also, do not break the output
76 * line at the end of the chunk, such that the next call can pad to
77 * the next column. However, if less than p->trailspace blanks,
78 * which can be 0, 1, or 2, remain to the right margin, the line
80 * - TERMP_BRIND: If the chunk does not fit and the output line has
81 * to be broken, start the next line at the right margin instead
82 * of at the offset. Used together with TERMP_NOBREAK for the tags
83 * in various kinds of tagged lists.
84 * - TERMP_DANGLE: Do not break the output line at the right margin,
85 * append the next chunk after it even if this one is too long.
86 * To be used together with TERMP_NOBREAK.
87 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
88 * the next chunk if this column is not full.
91 term_flushln(struct termp
*p
)
93 size_t i
; /* current input position in p->buf */
94 int ntab
; /* number of tabs to prepend */
95 size_t vis
; /* current visual position on output */
96 size_t vbl
; /* number of blanks to prepend to output */
97 size_t vend
; /* end of word visual position on output */
98 size_t bp
; /* visual right border position */
99 size_t dv
; /* temporary for visual pos calculations */
100 size_t j
; /* temporary loop index for p->buf */
101 size_t jhy
; /* last hyph before overflow w/r/t j */
102 size_t maxvis
; /* output position of visible boundary */
103 size_t rmargin
; /* the rightmost of the two margins */
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 rmargin
= p
->rmargin
> p
->offset
? p
->rmargin
: p
->offset
;
117 dv
= p
->rmargin
- p
->offset
;
118 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
120 if (p
->flags
& TERMP_NOBREAK
) {
121 dv
= p
->maxrmargin
> p
->offset
?
122 p
->maxrmargin
- p
->offset
: 0;
123 bp
= (int)dv
> p
->overstep
?
124 dv
- (size_t)p
->overstep
: 0;
129 * Calculate the required amount of padding.
131 vbl
= p
->offset
+ p
->overstep
> p
->viscol
?
132 p
->offset
+ p
->overstep
- p
->viscol
: 0;
139 * Handle literal tab characters: collapse all
140 * subsequent tabs into a single huge set of spaces.
143 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
144 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
152 * Count up visible word characters. Control sequences
153 * (starting with the CSI) aren't counted. A space
154 * generates a non-printing word, which is valid (the
155 * space is printed according to regular spacing rules).
158 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
159 if (' ' == p
->buf
[j
] || '\t' == p
->buf
[j
])
162 /* Back over the the last printed character. */
163 if (8 == p
->buf
[j
]) {
165 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
170 /* Break at the hyphen point if we overrun. */
171 if (vend
> vis
&& vend
< bp
&&
172 (ASCII_HYPH
== p
->buf
[j
] ||
173 ASCII_BREAK
== p
->buf
[j
]))
177 * Hyphenation now decided, put back a real
178 * hyphen such that we get the correct width.
180 if (ASCII_HYPH
== p
->buf
[j
])
183 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
187 * Find out whether we would exceed the right margin.
188 * If so, break to the next line.
190 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
194 if (TERMP_BRIND
& p
->flags
) {
196 vend
+= rmargin
- p
->offset
;
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)(vis
- maxvis
+
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
)
333 term_fontlast(struct termp
*p
)
338 p
->fontl
= p
->fontq
[p
->fonti
];
339 p
->fontq
[p
->fonti
] = f
;
343 term_fontrepl(struct termp
*p
, enum termfont f
)
346 p
->fontl
= p
->fontq
[p
->fonti
];
347 p
->fontq
[p
->fonti
] = f
;
351 term_fontpush(struct termp
*p
, enum termfont f
)
354 assert(p
->fonti
+ 1 < 10);
355 p
->fontl
= p
->fontq
[p
->fonti
];
356 p
->fontq
[++p
->fonti
] = f
;
360 term_fontq(struct termp
*p
)
363 return(&p
->fontq
[p
->fonti
]);
367 term_fonttop(struct termp
*p
)
370 return(p
->fontq
[p
->fonti
]);
374 term_fontpopq(struct termp
*p
, const void *key
)
377 while (p
->fonti
>= 0 && key
< (void *)(p
->fontq
+ p
->fonti
))
379 assert(p
->fonti
>= 0);
383 term_fontpop(struct termp
*p
)
391 * Handle pwords, partial words, which may be either a single word or a
392 * phrase that cannot be broken down (such as a literal string). This
393 * handles word styling.
396 term_word(struct termp
*p
, const char *word
)
398 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
399 const char *seq
, *cp
;
404 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
405 if ( ! (TERMP_KEEP
& p
->flags
)) {
407 if (TERMP_SENTENCE
& p
->flags
)
410 bufferc(p
, ASCII_NBRSP
);
412 if (TERMP_PREKEEP
& p
->flags
)
413 p
->flags
|= TERMP_KEEP
;
415 if ( ! (p
->flags
& TERMP_NONOSPACE
))
416 p
->flags
&= ~TERMP_NOSPACE
;
418 p
->flags
|= TERMP_NOSPACE
;
420 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_NONEWLINE
);
422 while ('\0' != *word
) {
424 if (TERMP_SKIPCHAR
& p
->flags
) {
425 p
->flags
&= ~TERMP_SKIPCHAR
;
429 if (TERMP_NBRWORD
& p
->flags
) {
435 ssz
= strcspn(word
, "\\ ");
437 ssz
= strcspn(word
, "\\");
438 encode(p
, word
, ssz
);
444 esc
= mandoc_escape(&word
, &seq
, &sz
);
445 if (ESCAPE_ERROR
== esc
)
450 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
452 case ESCAPE_NUMBERED
:
453 uc
= mchars_num2char(seq
, sz
);
458 if (p
->enc
== TERMENC_ASCII
) {
459 cp
= mchars_spec2str(p
->symtab
,
464 uc
= mchars_spec2cp(p
->symtab
, seq
, sz
);
469 case ESCAPE_FONTBOLD
:
470 term_fontrepl(p
, TERMFONT_BOLD
);
472 case ESCAPE_FONTITALIC
:
473 term_fontrepl(p
, TERMFONT_UNDER
);
476 term_fontrepl(p
, TERMFONT_BI
);
480 case ESCAPE_FONTROMAN
:
481 term_fontrepl(p
, TERMFONT_NONE
);
483 case ESCAPE_FONTPREV
:
487 if (TERMP_SKIPCHAR
& p
->flags
)
488 p
->flags
&= ~TERMP_SKIPCHAR
;
489 else if ('\0' == *word
)
490 p
->flags
|= (TERMP_NOSPACE
| TERMP_NONEWLINE
);
492 case ESCAPE_SKIPCHAR
:
493 p
->flags
|= TERMP_SKIPCHAR
;
500 * Common handling for Unicode and numbered
501 * character escape sequences.
504 if (p
->enc
== TERMENC_ASCII
) {
505 cp
= ascii_uc2str(uc
);
506 encode(p
, cp
, strlen(cp
));
508 if ((uc
< 0x20 && uc
!= 0x09) ||
509 (uc
> 0x7E && uc
< 0xA0))
514 p
->flags
&= ~TERMP_NBRWORD
;
518 adjbuf(struct termp
*p
, size_t sz
)
523 while (sz
>= p
->maxcols
)
526 p
->buf
= mandoc_reallocarray(p
->buf
, p
->maxcols
, sizeof(int));
530 bufferc(struct termp
*p
, char c
)
533 if (p
->col
+ 1 >= p
->maxcols
)
534 adjbuf(p
, p
->col
+ 1);
536 p
->buf
[p
->col
++] = c
;
541 * Do this for a single (probably unicode) value.
542 * Does not check for non-decorated glyphs.
545 encode1(struct termp
*p
, int c
)
549 if (TERMP_SKIPCHAR
& p
->flags
) {
550 p
->flags
&= ~TERMP_SKIPCHAR
;
554 if (p
->col
+ 6 >= p
->maxcols
)
555 adjbuf(p
, p
->col
+ 6);
559 if (TERMFONT_UNDER
== f
|| TERMFONT_BI
== f
) {
560 p
->buf
[p
->col
++] = '_';
561 p
->buf
[p
->col
++] = 8;
563 if (TERMFONT_BOLD
== f
|| TERMFONT_BI
== f
) {
565 p
->buf
[p
->col
++] = '-';
567 p
->buf
[p
->col
++] = c
;
568 p
->buf
[p
->col
++] = 8;
570 p
->buf
[p
->col
++] = c
;
574 encode(struct termp
*p
, const char *word
, size_t sz
)
578 if (TERMP_SKIPCHAR
& p
->flags
) {
579 p
->flags
&= ~TERMP_SKIPCHAR
;
584 * Encode and buffer a string of characters. If the current
585 * font mode is unset, buffer directly, else encode then buffer
586 * character by character.
589 if (TERMFONT_NONE
== term_fonttop(p
)) {
590 if (p
->col
+ sz
>= p
->maxcols
)
591 adjbuf(p
, p
->col
+ sz
);
592 for (i
= 0; i
< sz
; i
++)
593 p
->buf
[p
->col
++] = word
[i
];
597 /* Pre-buffer, assuming worst-case. */
599 if (p
->col
+ 1 + (sz
* 5) >= p
->maxcols
)
600 adjbuf(p
, p
->col
+ 1 + (sz
* 5));
602 for (i
= 0; i
< sz
; i
++) {
603 if (ASCII_HYPH
== word
[i
] ||
604 isgraph((unsigned char)word
[i
]))
607 p
->buf
[p
->col
++] = word
[i
];
612 term_setwidth(struct termp
*p
, const char *wstr
)
633 if (a2roffsu(wstr
, &su
, SCALE_MAX
))
634 width
= term_hspan(p
, &su
);
638 (*p
->setwidth
)(p
, iop
, width
);
642 term_len(const struct termp
*p
, size_t sz
)
645 return((*p
->width
)(p
, ' ') * sz
);
649 cond_width(const struct termp
*p
, int c
, int *skip
)
656 return((*p
->width
)(p
, c
));
660 term_strlen(const struct termp
*p
, const char *cp
)
664 const char *seq
, *rhs
;
666 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
670 * Account for escaped sequences within string length
671 * calculations. This follows the logic in term_word() as we
672 * must calculate the width of produced strings.
677 while ('\0' != *cp
) {
678 rsz
= strcspn(cp
, rej
);
679 for (i
= 0; i
< rsz
; i
++)
680 sz
+= cond_width(p
, *cp
++, &skip
);
685 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
686 if (ESCAPE_ERROR
== esc
)
693 uc
= mchars_num2uc(seq
+ 1, ssz
- 1);
695 case ESCAPE_NUMBERED
:
696 uc
= mchars_num2char(seq
, ssz
);
701 if (p
->enc
== TERMENC_ASCII
) {
702 rhs
= mchars_spec2str(p
->symtab
,
707 uc
= mchars_spec2cp(p
->symtab
,
710 sz
+= cond_width(p
, uc
, &skip
);
713 case ESCAPE_SKIPCHAR
:
721 * Common handling for Unicode and numbered
722 * character escape sequences.
726 if (p
->enc
== TERMENC_ASCII
) {
727 rhs
= ascii_uc2str(uc
);
730 if ((uc
< 0x20 && uc
!= 0x09) ||
731 (uc
> 0x7E && uc
< 0xA0))
733 sz
+= cond_width(p
, uc
, &skip
);
744 * Common handling for all escape sequences
745 * printing more than one character.
748 for (i
= 0; i
< rsz
; i
++)
749 sz
+= (*p
->width
)(p
, *rhs
++);
752 sz
+= cond_width(p
, ' ', &skip
);
756 sz
+= cond_width(p
, '-', &skip
);
770 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
788 r
= su
->scale
/ 1000.0;
800 return((size_t)(r
+ 0.0005));
804 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
808 v
= (*p
->hspan
)(p
, su
);
811 return((size_t)(v
+ 0.0005));