]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.228 2014/08/18 21:07:53 kristaps 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
)
48 mchars_free(p
->symtab
);
54 term_begin(struct termp
*p
, term_margin head
,
55 term_margin foot
, const void *arg
)
65 term_end(struct termp
*p
)
72 * Flush a chunk of text. By default, break the output line each time
73 * the right margin is reached, and continue output on the next line
74 * at the same offset as the chunk itself. By default, also break the
75 * output line at the end of the chunk.
76 * The following flags may be specified:
78 * - TERMP_NOBREAK: Do not break the output line at the right margin,
79 * but only at the max right margin. Also, do not break the output
80 * line at the end of the chunk, such that the next call can pad to
81 * the next column. However, if less than p->trailspace blanks,
82 * which can be 0, 1, or 2, remain to the right margin, the line
84 * - TERMP_BRIND: If the chunk does not fit and the output line has
85 * to be broken, start the next line at the right margin instead
86 * of at the offset. Used together with TERMP_NOBREAK for the tags
87 * in various kinds of tagged lists.
88 * - TERMP_DANGLE: Do not break the output line at the right margin,
89 * append the next chunk after it even if this one is too long.
90 * To be used together with TERMP_NOBREAK.
91 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
92 * the next chunk if this column is not full.
95 term_flushln(struct termp
*p
)
97 size_t i
; /* current input position in p->buf */
98 int ntab
; /* number of tabs to prepend */
99 size_t vis
; /* current visual position on output */
100 size_t vbl
; /* number of blanks to prepend to output */
101 size_t vend
; /* end of word visual position on output */
102 size_t bp
; /* visual right border position */
103 size_t dv
; /* temporary for visual pos calculations */
104 size_t j
; /* temporary loop index for p->buf */
105 size_t jhy
; /* last hyph before overflow w/r/t j */
106 size_t maxvis
; /* output position of visible boundary */
107 size_t mmax
; /* used in calculating bp */
110 * First, establish the maximum columns of "visible" content.
111 * This is usually the difference between the right-margin and
112 * an indentation, but can be, for tagged lists or columns, a
113 * small set of values.
115 * The following unsigned-signed subtractions look strange,
116 * but they are actually correct. If the int p->overstep
117 * is negative, it gets sign extended. Subtracting that
118 * very large size_t effectively adds a small number to dv.
120 assert (p
->rmargin
>= p
->offset
);
121 dv
= p
->rmargin
- p
->offset
;
122 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
123 dv
= p
->maxrmargin
- p
->offset
;
124 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
126 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
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
+= p
->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.
267 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
273 if (TERMP_HANG
& p
->flags
) {
274 p
->overstep
= (int)(vis
- maxvis
+
275 p
->trailspace
* (*p
->width
)(p
, ' '));
278 * If we have overstepped the margin, temporarily move
279 * it to the right and flag the rest of the line to be
281 * If there is a request to keep the columns together,
282 * allow negative overstep when the column is not full.
284 if (p
->trailspace
&& p
->overstep
< 0)
288 } else if (TERMP_DANGLE
& p
->flags
)
291 /* If the column was overrun, break the line. */
292 if (maxvis
< vis
+ p
->trailspace
* (*p
->width
)(p
, ' ')) {
299 * A newline only breaks an existing line; it won't assert vertical
300 * space. All data in the output buffer is flushed prior to the newline
304 term_newln(struct termp
*p
)
307 p
->flags
|= TERMP_NOSPACE
;
308 if (p
->col
|| p
->viscol
)
313 * Asserts a vertical space (a full, empty line-break between lines).
314 * Note that if used twice, this will cause two blank spaces and so on.
315 * All data in the output buffer is flushed prior to the newline
319 term_vspace(struct termp
*p
)
331 term_fontlast(struct termp
*p
)
336 p
->fontl
= p
->fontq
[p
->fonti
];
337 p
->fontq
[p
->fonti
] = f
;
341 term_fontrepl(struct termp
*p
, enum termfont f
)
344 p
->fontl
= p
->fontq
[p
->fonti
];
345 p
->fontq
[p
->fonti
] = f
;
349 term_fontpush(struct termp
*p
, enum termfont f
)
352 assert(p
->fonti
+ 1 < 10);
353 p
->fontl
= p
->fontq
[p
->fonti
];
354 p
->fontq
[++p
->fonti
] = f
;
358 term_fontq(struct termp
*p
)
361 return(&p
->fontq
[p
->fonti
]);
365 term_fonttop(struct termp
*p
)
368 return(p
->fontq
[p
->fonti
]);
372 term_fontpopq(struct termp
*p
, const void *key
)
375 while (p
->fonti
>= 0 && key
< (void *)(p
->fontq
+ p
->fonti
))
377 assert(p
->fonti
>= 0);
381 term_fontpop(struct termp
*p
)
389 * Handle pwords, partial words, which may be either a single word or a
390 * phrase that cannot be broken down (such as a literal string). This
391 * handles word styling.
394 term_word(struct termp
*p
, const char *word
)
396 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
397 const char *seq
, *cp
;
403 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
404 if ( ! (TERMP_KEEP
& p
->flags
)) {
406 if (TERMP_SENTENCE
& p
->flags
)
409 bufferc(p
, ASCII_NBRSP
);
411 if (TERMP_PREKEEP
& p
->flags
)
412 p
->flags
|= TERMP_KEEP
;
414 if ( ! (p
->flags
& TERMP_NONOSPACE
))
415 p
->flags
&= ~TERMP_NOSPACE
;
417 p
->flags
|= TERMP_NOSPACE
;
419 p
->flags
&= ~TERMP_SENTENCE
;
421 while ('\0' != *word
) {
423 if (TERMP_SKIPCHAR
& p
->flags
) {
424 p
->flags
&= ~TERMP_SKIPCHAR
;
428 if (TERMP_NBRWORD
& p
->flags
) {
434 ssz
= strcspn(word
, "\\ ");
436 ssz
= strcspn(word
, "\\");
437 encode(p
, word
, ssz
);
443 esc
= mandoc_escape(&word
, &seq
, &sz
);
444 if (ESCAPE_ERROR
== esc
)
447 if (TERMENC_ASCII
!= p
->enc
)
450 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
456 uc
= mchars_spec2cp(p
->symtab
, seq
, sz
);
469 case ESCAPE_NUMBERED
:
470 c
= mchars_num2char(seq
, sz
);
475 cp
= mchars_spec2str(p
->symtab
, seq
, sz
, &ssz
);
481 case ESCAPE_FONTBOLD
:
482 term_fontrepl(p
, TERMFONT_BOLD
);
484 case ESCAPE_FONTITALIC
:
485 term_fontrepl(p
, TERMFONT_UNDER
);
488 term_fontrepl(p
, TERMFONT_BI
);
492 case ESCAPE_FONTROMAN
:
493 term_fontrepl(p
, TERMFONT_NONE
);
495 case ESCAPE_FONTPREV
:
499 if (TERMP_SKIPCHAR
& p
->flags
)
500 p
->flags
&= ~TERMP_SKIPCHAR
;
501 else if ('\0' == *word
)
502 p
->flags
|= TERMP_NOSPACE
;
504 case ESCAPE_SKIPCHAR
:
505 p
->flags
|= TERMP_SKIPCHAR
;
511 p
->flags
&= ~TERMP_NBRWORD
;
515 adjbuf(struct termp
*p
, size_t sz
)
520 while (sz
>= p
->maxcols
)
523 p
->buf
= mandoc_reallocarray(p
->buf
, p
->maxcols
, sizeof(int));
527 bufferc(struct termp
*p
, char c
)
530 if (p
->col
+ 1 >= p
->maxcols
)
531 adjbuf(p
, p
->col
+ 1);
533 p
->buf
[p
->col
++] = c
;
538 * Do this for a single (probably unicode) value.
539 * Does not check for non-decorated glyphs.
542 encode1(struct termp
*p
, int c
)
546 if (TERMP_SKIPCHAR
& p
->flags
) {
547 p
->flags
&= ~TERMP_SKIPCHAR
;
551 if (p
->col
+ 6 >= p
->maxcols
)
552 adjbuf(p
, p
->col
+ 6);
556 if (TERMFONT_UNDER
== f
|| TERMFONT_BI
== f
) {
557 p
->buf
[p
->col
++] = '_';
558 p
->buf
[p
->col
++] = 8;
560 if (TERMFONT_BOLD
== f
|| TERMFONT_BI
== f
) {
562 p
->buf
[p
->col
++] = '-';
564 p
->buf
[p
->col
++] = c
;
565 p
->buf
[p
->col
++] = 8;
567 p
->buf
[p
->col
++] = c
;
571 encode(struct termp
*p
, const char *word
, size_t sz
)
575 if (TERMP_SKIPCHAR
& p
->flags
) {
576 p
->flags
&= ~TERMP_SKIPCHAR
;
581 * Encode and buffer a string of characters. If the current
582 * font mode is unset, buffer directly, else encode then buffer
583 * character by character.
586 if (TERMFONT_NONE
== term_fonttop(p
)) {
587 if (p
->col
+ sz
>= p
->maxcols
)
588 adjbuf(p
, p
->col
+ sz
);
589 for (i
= 0; i
< sz
; i
++)
590 p
->buf
[p
->col
++] = word
[i
];
594 /* Pre-buffer, assuming worst-case. */
596 if (p
->col
+ 1 + (sz
* 5) >= p
->maxcols
)
597 adjbuf(p
, p
->col
+ 1 + (sz
* 5));
599 for (i
= 0; i
< sz
; i
++) {
600 if (ASCII_HYPH
== word
[i
] ||
601 isgraph((unsigned char)word
[i
]))
604 p
->buf
[p
->col
++] = word
[i
];
609 term_setwidth(struct termp
*p
, const char *wstr
)
630 if (a2roffsu(wstr
, &su
, SCALE_MAX
))
631 width
= term_hspan(p
, &su
);
635 (*p
->setwidth
)(p
, iop
, width
);
639 term_len(const struct termp
*p
, size_t sz
)
642 return((*p
->width
)(p
, ' ') * sz
);
646 cond_width(const struct termp
*p
, int c
, int *skip
)
653 return((*p
->width
)(p
, c
));
657 term_strlen(const struct termp
*p
, const char *cp
)
661 const char *seq
, *rhs
;
663 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
667 * Account for escaped sequences within string length
668 * calculations. This follows the logic in term_word() as we
669 * must calculate the width of produced strings.
674 while ('\0' != *cp
) {
675 rsz
= strcspn(cp
, rej
);
676 for (i
= 0; i
< rsz
; i
++)
677 sz
+= cond_width(p
, *cp
++, &skip
);
682 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
683 if (ESCAPE_ERROR
== esc
)
686 if (TERMENC_ASCII
!= p
->enc
)
689 c
= mchars_num2uc(seq
+ 1,
693 sz
+= cond_width(p
, c
, &skip
);
696 c
= mchars_spec2cp(p
->symtab
,
700 sz
+= cond_width(p
, c
, &skip
);
710 sz
+= cond_width(p
, '?', &skip
);
712 case ESCAPE_NUMBERED
:
713 c
= mchars_num2char(seq
, ssz
);
715 sz
+= cond_width(p
, c
, &skip
);
718 rhs
= mchars_spec2str(p
->symtab
,
727 case ESCAPE_SKIPCHAR
:
742 for (i
= 0; i
< rsz
; i
++)
743 sz
+= (*p
->width
)(p
, *rhs
++);
746 sz
+= cond_width(p
, ' ', &skip
);
750 sz
+= cond_width(p
, '-', &skip
);
764 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
782 r
= su
->scale
/ 1000.0;
794 return((size_t)(r
+ 0.0005));
798 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
802 v
= (*p
->hspan
)(p
, su
);
805 return((size_t)(v
+ 0.0005));