]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.221 2014/04/08 07:13:12 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.
22 #include <sys/types.h>
31 #include "mandoc_aux.h"
36 static size_t cond_width(const struct termp
*, int, int *);
37 static void adjbuf(struct termp
*p
, size_t);
38 static void bufferc(struct termp
*, char);
39 static void encode(struct termp
*, const char *, size_t);
40 static void encode1(struct termp
*, int);
43 term_free(struct termp
*p
)
49 mchars_free(p
->symtab
);
56 term_begin(struct termp
*p
, term_margin head
,
57 term_margin foot
, const void *arg
)
68 term_end(struct termp
*p
)
75 * Flush a chunk of text. By default, break the output line each time
76 * the right margin is reached, and continue output on the next line
77 * at the same offset as the chunk itself. By default, also break the
78 * output line at the end of the chunk.
79 * The following flags may be specified:
81 * - TERMP_NOBREAK: Do not break the output line at the right margin,
82 * but only at the max right margin. Also, do not break the output
83 * line at the end of the chunk, such that the next call can pad to
84 * the next column. However, if less than p->trailspace blanks,
85 * which can be 0, 1, or 2, remain to the right margin, the line
87 * - TERMP_BRIND: If the chunk does not fit and the output line has
88 * to be broken, start the next line at the right margin instead
89 * of at the offset. Used together with TERMP_NOBREAK for the tags
90 * in various kinds of tagged lists.
91 * - TERMP_DANGLE: Do not break the output line at the right margin,
92 * append the next chunk after it even if this one is too long.
93 * To be used together with TERMP_NOBREAK.
94 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
95 * the next chunk if this column is not full.
98 term_flushln(struct termp
*p
)
100 size_t i
; /* current input position in p->buf */
101 int ntab
; /* number of tabs to prepend */
102 size_t vis
; /* current visual position on output */
103 size_t vbl
; /* number of blanks to prepend to output */
104 size_t vend
; /* end of word visual position on output */
105 size_t bp
; /* visual right border position */
106 size_t dv
; /* temporary for visual pos calculations */
107 size_t j
; /* temporary loop index for p->buf */
108 size_t jhy
; /* last hyph before overflow w/r/t j */
109 size_t maxvis
; /* output position of visible boundary */
110 size_t mmax
; /* used in calculating bp */
113 * First, establish the maximum columns of "visible" content.
114 * This is usually the difference between the right-margin and
115 * an indentation, but can be, for tagged lists or columns, a
116 * small set of values.
118 * The following unsigned-signed subtractions look strange,
119 * but they are actually correct. If the int p->overstep
120 * is negative, it gets sign extended. Subtracting that
121 * very large size_t effectively adds a small number to dv.
123 assert (p
->rmargin
>= p
->offset
);
124 dv
= p
->rmargin
- p
->offset
;
125 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
126 dv
= p
->maxrmargin
- p
->offset
;
127 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
129 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
132 * Calculate the required amount of padding.
134 vbl
= p
->offset
+ p
->overstep
> p
->viscol
?
135 p
->offset
+ p
->overstep
- p
->viscol
: 0;
142 * Handle literal tab characters: collapse all
143 * subsequent tabs into a single huge set of spaces.
146 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
147 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
155 * Count up visible word characters. Control sequences
156 * (starting with the CSI) aren't counted. A space
157 * generates a non-printing word, which is valid (the
158 * space is printed according to regular spacing rules).
161 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
162 if (' ' == p
->buf
[j
] || '\t' == p
->buf
[j
])
165 /* Back over the the last printed character. */
166 if (8 == p
->buf
[j
]) {
168 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
173 /* Break at the hyphen point if we overrun. */
174 if (vend
> vis
&& vend
< bp
&&
175 (ASCII_HYPH
== p
->buf
[j
] ||
176 ASCII_BREAK
== p
->buf
[j
]))
180 * Hyphenation now decided, put back a real
181 * hyphen such that we get the correct width.
183 if (ASCII_HYPH
== p
->buf
[j
])
186 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
190 * Find out whether we would exceed the right margin.
191 * If so, break to the next line.
193 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
197 if (TERMP_BRIND
& p
->flags
) {
199 vend
+= p
->rmargin
- p
->offset
;
203 /* use pending tabs on the new line */
206 vbl
+= ntab
* p
->tabwidth
;
209 * Remove the p->overstep width.
210 * Again, if p->overstep is negative,
211 * sign extension does the right thing.
214 bp
+= (size_t)p
->overstep
;
218 /* Write out the [remaining] word. */
219 for ( ; i
< p
->col
; i
++) {
220 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
222 if ('\t' == p
->buf
[i
])
224 if (' ' == p
->buf
[i
]) {
226 while (' ' == p
->buf
[i
])
228 dv
= (i
- j
) * (*p
->width
)(p
, ' ');
233 if (ASCII_NBRSP
== p
->buf
[i
]) {
234 vbl
+= (*p
->width
)(p
, ' ');
237 if (ASCII_BREAK
== p
->buf
[i
])
241 * Now we definitely know there will be
242 * printable characters to output,
243 * so write preceding white space now.
246 (*p
->advance
)(p
, vbl
);
251 (*p
->letter
)(p
, p
->buf
[i
]);
253 p
->viscol
-= (*p
->width
)(p
, p
->buf
[i
-1]);
255 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
261 * If there was trailing white space, it was not printed;
262 * so reset the cursor position accordingly.
270 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
276 if (TERMP_HANG
& p
->flags
) {
277 p
->overstep
= (int)(vis
- maxvis
+
278 p
->trailspace
* (*p
->width
)(p
, ' '));
281 * If we have overstepped the margin, temporarily move
282 * it to the right and flag the rest of the line to be
284 * If there is a request to keep the columns together,
285 * allow negative overstep when the column is not full.
287 if (p
->trailspace
&& p
->overstep
< 0)
291 } else if (TERMP_DANGLE
& p
->flags
)
294 /* If the column was overrun, break the line. */
295 if (maxvis
< vis
+ p
->trailspace
* (*p
->width
)(p
, ' ')) {
303 * A newline only breaks an existing line; it won't assert vertical
304 * space. All data in the output buffer is flushed prior to the newline
308 term_newln(struct termp
*p
)
311 p
->flags
|= TERMP_NOSPACE
;
312 if (p
->col
|| p
->viscol
)
318 * Asserts a vertical space (a full, empty line-break between lines).
319 * Note that if used twice, this will cause two blank spaces and so on.
320 * All data in the output buffer is flushed prior to the newline
324 term_vspace(struct termp
*p
)
336 term_fontlast(struct termp
*p
)
341 p
->fontl
= p
->fontq
[p
->fonti
];
342 p
->fontq
[p
->fonti
] = f
;
347 term_fontrepl(struct termp
*p
, enum termfont f
)
350 p
->fontl
= p
->fontq
[p
->fonti
];
351 p
->fontq
[p
->fonti
] = f
;
356 term_fontpush(struct termp
*p
, enum termfont f
)
359 assert(p
->fonti
+ 1 < 10);
360 p
->fontl
= p
->fontq
[p
->fonti
];
361 p
->fontq
[++p
->fonti
] = f
;
366 term_fontq(struct termp
*p
)
369 return(&p
->fontq
[p
->fonti
]);
374 term_fonttop(struct termp
*p
)
377 return(p
->fontq
[p
->fonti
]);
382 term_fontpopq(struct termp
*p
, const void *key
)
385 while (p
->fonti
>= 0 && key
< (void *)(p
->fontq
+ p
->fonti
))
387 assert(p
->fonti
>= 0);
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
)
407 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
408 const char *seq
, *cp
;
414 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
415 if ( ! (TERMP_KEEP
& p
->flags
)) {
417 if (TERMP_SENTENCE
& p
->flags
)
420 bufferc(p
, ASCII_NBRSP
);
422 if (TERMP_PREKEEP
& p
->flags
)
423 p
->flags
|= TERMP_KEEP
;
425 if ( ! (p
->flags
& TERMP_NONOSPACE
))
426 p
->flags
&= ~TERMP_NOSPACE
;
428 p
->flags
|= TERMP_NOSPACE
;
430 p
->flags
&= ~TERMP_SENTENCE
;
432 while ('\0' != *word
) {
434 if (TERMP_SKIPCHAR
& p
->flags
) {
435 p
->flags
&= ~TERMP_SKIPCHAR
;
439 if (TERMP_NBRWORD
& p
->flags
) {
445 ssz
= strcspn(word
, "\\ ");
447 ssz
= strcspn(word
, "\\");
448 encode(p
, word
, ssz
);
454 esc
= mandoc_escape(&word
, &seq
, &sz
);
455 if (ESCAPE_ERROR
== esc
)
458 if (TERMENC_ASCII
!= p
->enc
)
460 case (ESCAPE_UNICODE
):
461 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
466 case (ESCAPE_SPECIAL
):
467 uc
= mchars_spec2cp(p
->symtab
, seq
, sz
);
477 case (ESCAPE_UNICODE
):
480 case (ESCAPE_NUMBERED
):
481 c
= mchars_num2char(seq
, sz
);
485 case (ESCAPE_SPECIAL
):
486 cp
= mchars_spec2str(p
->symtab
, seq
, sz
, &ssz
);
492 case (ESCAPE_FONTBOLD
):
493 term_fontrepl(p
, TERMFONT_BOLD
);
495 case (ESCAPE_FONTITALIC
):
496 term_fontrepl(p
, TERMFONT_UNDER
);
498 case (ESCAPE_FONTBI
):
499 term_fontrepl(p
, TERMFONT_BI
);
503 case (ESCAPE_FONTROMAN
):
504 term_fontrepl(p
, TERMFONT_NONE
);
506 case (ESCAPE_FONTPREV
):
509 case (ESCAPE_NOSPACE
):
510 if (TERMP_SKIPCHAR
& p
->flags
)
511 p
->flags
&= ~TERMP_SKIPCHAR
;
512 else if ('\0' == *word
)
513 p
->flags
|= TERMP_NOSPACE
;
515 case (ESCAPE_SKIPCHAR
):
516 p
->flags
|= TERMP_SKIPCHAR
;
522 p
->flags
&= ~TERMP_NBRWORD
;
526 adjbuf(struct termp
*p
, size_t sz
)
531 while (sz
>= p
->maxcols
)
534 p
->buf
= mandoc_realloc(p
->buf
, sizeof(int) * p
->maxcols
);
538 bufferc(struct termp
*p
, char c
)
541 if (p
->col
+ 1 >= p
->maxcols
)
542 adjbuf(p
, p
->col
+ 1);
544 p
->buf
[p
->col
++] = c
;
549 * Do this for a single (probably unicode) value.
550 * Does not check for non-decorated glyphs.
553 encode1(struct termp
*p
, int c
)
557 if (TERMP_SKIPCHAR
& p
->flags
) {
558 p
->flags
&= ~TERMP_SKIPCHAR
;
562 if (p
->col
+ 6 >= p
->maxcols
)
563 adjbuf(p
, p
->col
+ 6);
567 if (TERMFONT_UNDER
== f
|| TERMFONT_BI
== f
) {
568 p
->buf
[p
->col
++] = '_';
569 p
->buf
[p
->col
++] = 8;
571 if (TERMFONT_BOLD
== f
|| TERMFONT_BI
== f
) {
573 p
->buf
[p
->col
++] = '-';
575 p
->buf
[p
->col
++] = c
;
576 p
->buf
[p
->col
++] = 8;
578 p
->buf
[p
->col
++] = c
;
582 encode(struct termp
*p
, const char *word
, size_t sz
)
586 if (TERMP_SKIPCHAR
& p
->flags
) {
587 p
->flags
&= ~TERMP_SKIPCHAR
;
592 * Encode and buffer a string of characters. If the current
593 * font mode is unset, buffer directly, else encode then buffer
594 * character by character.
597 if (TERMFONT_NONE
== term_fonttop(p
)) {
598 if (p
->col
+ sz
>= p
->maxcols
)
599 adjbuf(p
, p
->col
+ sz
);
600 for (i
= 0; i
< sz
; i
++)
601 p
->buf
[p
->col
++] = word
[i
];
605 /* Pre-buffer, assuming worst-case. */
607 if (p
->col
+ 1 + (sz
* 5) >= p
->maxcols
)
608 adjbuf(p
, p
->col
+ 1 + (sz
* 5));
610 for (i
= 0; i
< sz
; i
++) {
611 if (ASCII_HYPH
== word
[i
] ||
612 isgraph((unsigned char)word
[i
]))
615 p
->buf
[p
->col
++] = word
[i
];
620 term_setwidth(struct termp
*p
, const char *wstr
)
641 if (a2roffsu(wstr
, &su
, SCALE_MAX
))
642 width
= term_hspan(p
, &su
);
646 (*p
->setwidth
)(p
, iop
, width
);
650 term_len(const struct termp
*p
, size_t sz
)
653 return((*p
->width
)(p
, ' ') * sz
);
657 cond_width(const struct termp
*p
, int c
, int *skip
)
664 return((*p
->width
)(p
, c
));
668 term_strlen(const struct termp
*p
, const char *cp
)
672 const char *seq
, *rhs
;
674 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
678 * Account for escaped sequences within string length
679 * calculations. This follows the logic in term_word() as we
680 * must calculate the width of produced strings.
685 while ('\0' != *cp
) {
686 rsz
= strcspn(cp
, rej
);
687 for (i
= 0; i
< rsz
; i
++)
688 sz
+= cond_width(p
, *cp
++, &skip
);
693 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
694 if (ESCAPE_ERROR
== esc
)
697 if (TERMENC_ASCII
!= p
->enc
)
699 case (ESCAPE_UNICODE
):
704 sz
+= cond_width(p
, c
, &skip
);
706 case (ESCAPE_SPECIAL
):
708 (p
->symtab
, seq
, ssz
);
711 sz
+= cond_width(p
, c
, &skip
);
720 case (ESCAPE_UNICODE
):
721 sz
+= cond_width(p
, '?', &skip
);
723 case (ESCAPE_NUMBERED
):
724 c
= mchars_num2char(seq
, ssz
);
726 sz
+= cond_width(p
, c
, &skip
);
728 case (ESCAPE_SPECIAL
):
729 rhs
= mchars_spec2str
730 (p
->symtab
, seq
, ssz
, &rsz
);
738 case (ESCAPE_SKIPCHAR
):
753 for (i
= 0; i
< rsz
; i
++)
754 sz
+= (*p
->width
)(p
, *rhs
++);
757 sz
+= cond_width(p
, ' ', &skip
);
761 sz
+= cond_width(p
, '-', &skip
);
776 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
794 r
= su
->scale
/ 1000;
806 return(/* LINTED */(size_t)
811 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
815 v
= ((*p
->hspan
)(p
, su
));
818 return((size_t) /* LINTED */