]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.219 2014/03/30 21:28:01 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 line of text. A "line" is loosely defined as being something
76 * that should be followed by a newline, regardless of whether it's
77 * broken apart by newlines getting there. A line can also be a
78 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
79 * not have a trailing newline.
81 * The following flags may be specified:
83 * - TERMP_NOBREAK: this is the most important and is used when making
84 * columns. In short: don't print a newline and instead expect the
85 * next call to do the padding up to the start of the next column.
86 * p->trailspace may be set to 0, 1, or 2, depending on how many
87 * space characters are required at the end of the column.
89 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
90 * the line is overrun, and don't pad-right if it's underrun.
92 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
93 * overrunning, instead save the position and continue at that point
94 * when the next invocation.
96 * In-line line breaking:
98 * If TERMP_NOBREAK is specified and the line overruns the right
99 * margin, it will break and pad-right to the right margin after
100 * writing. If maxrmargin is violated, it will break and continue
101 * writing from the right-margin, which will lead to the above scenario
102 * upon exit. Otherwise, the line will break at the right margin.
105 term_flushln(struct termp
*p
)
107 size_t i
; /* current input position in p->buf */
108 int ntab
; /* number of tabs to prepend */
109 size_t vis
; /* current visual position on output */
110 size_t vbl
; /* number of blanks to prepend to output */
111 size_t vend
; /* end of word visual position on output */
112 size_t bp
; /* visual right border position */
113 size_t dv
; /* temporary for visual pos calculations */
114 size_t j
; /* temporary loop index for p->buf */
115 size_t jhy
; /* last hyph before overflow w/r/t j */
116 size_t maxvis
; /* output position of visible boundary */
117 size_t mmax
; /* used in calculating bp */
120 * First, establish the maximum columns of "visible" content.
121 * This is usually the difference between the right-margin and
122 * an indentation, but can be, for tagged lists or columns, a
123 * small set of values.
125 * The following unsigned-signed subtractions look strange,
126 * but they are actually correct. If the int p->overstep
127 * is negative, it gets sign extended. Subtracting that
128 * very large size_t effectively adds a small number to dv.
130 assert (p
->rmargin
>= p
->offset
);
131 dv
= p
->rmargin
- p
->offset
;
132 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
133 dv
= p
->maxrmargin
- p
->offset
;
134 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
136 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
139 * Calculate the required amount of padding.
141 vbl
= p
->offset
+ p
->overstep
> p
->viscol
?
142 p
->offset
+ p
->overstep
- p
->viscol
: 0;
149 * Handle literal tab characters: collapse all
150 * subsequent tabs into a single huge set of spaces.
153 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
154 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
162 * Count up visible word characters. Control sequences
163 * (starting with the CSI) aren't counted. A space
164 * generates a non-printing word, which is valid (the
165 * space is printed according to regular spacing rules).
168 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
169 if (' ' == p
->buf
[j
] || '\t' == p
->buf
[j
])
172 /* Back over the the last printed character. */
173 if (8 == p
->buf
[j
]) {
175 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
180 /* Break at the hyphen point if we overrun. */
181 if (vend
> vis
&& vend
< bp
&&
182 (ASCII_HYPH
== p
->buf
[j
] ||
183 ASCII_BREAK
== p
->buf
[j
]))
187 * Hyphenation now decided, put back a real
188 * hyphen such that we get the correct width.
190 if (ASCII_HYPH
== p
->buf
[j
])
193 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
197 * Find out whether we would exceed the right margin.
198 * If so, break to the next line.
200 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
204 if (TERMP_NOBREAK
& p
->flags
) {
206 vend
+= p
->rmargin
- p
->offset
;
210 /* use pending tabs on the new line */
213 vbl
+= ntab
* p
->tabwidth
;
216 * Remove the p->overstep width.
217 * Again, if p->overstep is negative,
218 * sign extension does the right thing.
221 bp
+= (size_t)p
->overstep
;
225 /* Write out the [remaining] word. */
226 for ( ; i
< p
->col
; i
++) {
227 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
229 if ('\t' == p
->buf
[i
])
231 if (' ' == p
->buf
[i
]) {
233 while (' ' == p
->buf
[i
])
235 dv
= (i
- j
) * (*p
->width
)(p
, ' ');
240 if (ASCII_NBRSP
== p
->buf
[i
]) {
241 vbl
+= (*p
->width
)(p
, ' ');
244 if (ASCII_BREAK
== p
->buf
[i
])
248 * Now we definitely know there will be
249 * printable characters to output,
250 * so write preceding white space now.
253 (*p
->advance
)(p
, vbl
);
258 (*p
->letter
)(p
, p
->buf
[i
]);
260 p
->viscol
-= (*p
->width
)(p
, p
->buf
[i
-1]);
262 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
268 * If there was trailing white space, it was not printed;
269 * so reset the cursor position accordingly.
277 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
283 if (TERMP_HANG
& p
->flags
) {
284 p
->overstep
= (int)(vis
- maxvis
+
285 p
->trailspace
* (*p
->width
)(p
, ' '));
288 * If we have overstepped the margin, temporarily move
289 * it to the right and flag the rest of the line to be
291 * If there is a request to keep the columns together,
292 * allow negative overstep when the column is not full.
294 if (p
->trailspace
&& p
->overstep
< 0)
298 } else if (TERMP_DANGLE
& p
->flags
)
301 /* If the column was overrun, break the line. */
302 if (maxvis
< vis
+ p
->trailspace
* (*p
->width
)(p
, ' ')) {
310 * A newline only breaks an existing line; it won't assert vertical
311 * space. All data in the output buffer is flushed prior to the newline
315 term_newln(struct termp
*p
)
318 p
->flags
|= TERMP_NOSPACE
;
319 if (p
->col
|| p
->viscol
)
325 * Asserts a vertical space (a full, empty line-break between lines).
326 * Note that if used twice, this will cause two blank spaces and so on.
327 * All data in the output buffer is flushed prior to the newline
331 term_vspace(struct termp
*p
)
343 term_fontlast(struct termp
*p
)
348 p
->fontl
= p
->fontq
[p
->fonti
];
349 p
->fontq
[p
->fonti
] = f
;
354 term_fontrepl(struct termp
*p
, enum termfont f
)
357 p
->fontl
= p
->fontq
[p
->fonti
];
358 p
->fontq
[p
->fonti
] = f
;
363 term_fontpush(struct termp
*p
, enum termfont f
)
366 assert(p
->fonti
+ 1 < 10);
367 p
->fontl
= p
->fontq
[p
->fonti
];
368 p
->fontq
[++p
->fonti
] = f
;
373 term_fontq(struct termp
*p
)
376 return(&p
->fontq
[p
->fonti
]);
381 term_fonttop(struct termp
*p
)
384 return(p
->fontq
[p
->fonti
]);
389 term_fontpopq(struct termp
*p
, const void *key
)
392 while (p
->fonti
>= 0 && key
< (void *)(p
->fontq
+ p
->fonti
))
394 assert(p
->fonti
>= 0);
399 term_fontpop(struct termp
*p
)
407 * Handle pwords, partial words, which may be either a single word or a
408 * phrase that cannot be broken down (such as a literal string). This
409 * handles word styling.
412 term_word(struct termp
*p
, const char *word
)
414 const char nbrsp
[2] = { ASCII_NBRSP
, 0 };
415 const char *seq
, *cp
;
421 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
422 if ( ! (TERMP_KEEP
& p
->flags
)) {
424 if (TERMP_SENTENCE
& p
->flags
)
427 bufferc(p
, ASCII_NBRSP
);
429 if (TERMP_PREKEEP
& p
->flags
)
430 p
->flags
|= TERMP_KEEP
;
432 if ( ! (p
->flags
& TERMP_NONOSPACE
))
433 p
->flags
&= ~TERMP_NOSPACE
;
435 p
->flags
|= TERMP_NOSPACE
;
437 p
->flags
&= ~TERMP_SENTENCE
;
439 while ('\0' != *word
) {
441 if (TERMP_SKIPCHAR
& p
->flags
) {
442 p
->flags
&= ~TERMP_SKIPCHAR
;
446 if (TERMP_NBRWORD
& p
->flags
) {
452 ssz
= strcspn(word
, "\\ ");
454 ssz
= strcspn(word
, "\\");
455 encode(p
, word
, ssz
);
461 esc
= mandoc_escape(&word
, &seq
, &sz
);
462 if (ESCAPE_ERROR
== esc
)
465 if (TERMENC_ASCII
!= p
->enc
)
467 case (ESCAPE_UNICODE
):
468 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
473 case (ESCAPE_SPECIAL
):
474 uc
= mchars_spec2cp(p
->symtab
, seq
, sz
);
484 case (ESCAPE_UNICODE
):
487 case (ESCAPE_NUMBERED
):
488 c
= mchars_num2char(seq
, sz
);
492 case (ESCAPE_SPECIAL
):
493 cp
= mchars_spec2str(p
->symtab
, seq
, sz
, &ssz
);
499 case (ESCAPE_FONTBOLD
):
500 term_fontrepl(p
, TERMFONT_BOLD
);
502 case (ESCAPE_FONTITALIC
):
503 term_fontrepl(p
, TERMFONT_UNDER
);
505 case (ESCAPE_FONTBI
):
506 term_fontrepl(p
, TERMFONT_BI
);
510 case (ESCAPE_FONTROMAN
):
511 term_fontrepl(p
, TERMFONT_NONE
);
513 case (ESCAPE_FONTPREV
):
516 case (ESCAPE_NOSPACE
):
517 if (TERMP_SKIPCHAR
& p
->flags
)
518 p
->flags
&= ~TERMP_SKIPCHAR
;
519 else if ('\0' == *word
)
520 p
->flags
|= TERMP_NOSPACE
;
522 case (ESCAPE_SKIPCHAR
):
523 p
->flags
|= TERMP_SKIPCHAR
;
529 p
->flags
&= ~TERMP_NBRWORD
;
533 adjbuf(struct termp
*p
, size_t sz
)
538 while (sz
>= p
->maxcols
)
541 p
->buf
= mandoc_realloc(p
->buf
, sizeof(int) * p
->maxcols
);
545 bufferc(struct termp
*p
, char c
)
548 if (p
->col
+ 1 >= p
->maxcols
)
549 adjbuf(p
, p
->col
+ 1);
551 p
->buf
[p
->col
++] = c
;
556 * Do this for a single (probably unicode) value.
557 * Does not check for non-decorated glyphs.
560 encode1(struct termp
*p
, int c
)
564 if (TERMP_SKIPCHAR
& p
->flags
) {
565 p
->flags
&= ~TERMP_SKIPCHAR
;
569 if (p
->col
+ 6 >= p
->maxcols
)
570 adjbuf(p
, p
->col
+ 6);
574 if (TERMFONT_UNDER
== f
|| TERMFONT_BI
== f
) {
575 p
->buf
[p
->col
++] = '_';
576 p
->buf
[p
->col
++] = 8;
578 if (TERMFONT_BOLD
== f
|| TERMFONT_BI
== f
) {
580 p
->buf
[p
->col
++] = '-';
582 p
->buf
[p
->col
++] = c
;
583 p
->buf
[p
->col
++] = 8;
585 p
->buf
[p
->col
++] = c
;
589 encode(struct termp
*p
, const char *word
, size_t sz
)
593 if (TERMP_SKIPCHAR
& p
->flags
) {
594 p
->flags
&= ~TERMP_SKIPCHAR
;
599 * Encode and buffer a string of characters. If the current
600 * font mode is unset, buffer directly, else encode then buffer
601 * character by character.
604 if (TERMFONT_NONE
== term_fonttop(p
)) {
605 if (p
->col
+ sz
>= p
->maxcols
)
606 adjbuf(p
, p
->col
+ sz
);
607 for (i
= 0; i
< sz
; i
++)
608 p
->buf
[p
->col
++] = word
[i
];
612 /* Pre-buffer, assuming worst-case. */
614 if (p
->col
+ 1 + (sz
* 5) >= p
->maxcols
)
615 adjbuf(p
, p
->col
+ 1 + (sz
* 5));
617 for (i
= 0; i
< sz
; i
++) {
618 if (ASCII_HYPH
== word
[i
] ||
619 isgraph((unsigned char)word
[i
]))
622 p
->buf
[p
->col
++] = word
[i
];
627 term_setwidth(struct termp
*p
, const char *wstr
)
647 if ( ! a2roffsu(wstr
, &su
, SCALE_MAX
)) {
652 width
= (NULL
!= wstr
) ? term_hspan(p
, &su
) : 0;
653 (*p
->setwidth
)(p
, iop
, width
);
657 term_len(const struct termp
*p
, size_t sz
)
660 return((*p
->width
)(p
, ' ') * sz
);
664 cond_width(const struct termp
*p
, int c
, int *skip
)
671 return((*p
->width
)(p
, c
));
675 term_strlen(const struct termp
*p
, const char *cp
)
679 const char *seq
, *rhs
;
681 static const char rej
[] = { '\\', ASCII_NBRSP
, ASCII_HYPH
,
685 * Account for escaped sequences within string length
686 * calculations. This follows the logic in term_word() as we
687 * must calculate the width of produced strings.
692 while ('\0' != *cp
) {
693 rsz
= strcspn(cp
, rej
);
694 for (i
= 0; i
< rsz
; i
++)
695 sz
+= cond_width(p
, *cp
++, &skip
);
700 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
701 if (ESCAPE_ERROR
== esc
)
704 if (TERMENC_ASCII
!= p
->enc
)
706 case (ESCAPE_UNICODE
):
711 sz
+= cond_width(p
, c
, &skip
);
713 case (ESCAPE_SPECIAL
):
715 (p
->symtab
, seq
, ssz
);
718 sz
+= cond_width(p
, c
, &skip
);
727 case (ESCAPE_UNICODE
):
728 sz
+= cond_width(p
, '?', &skip
);
730 case (ESCAPE_NUMBERED
):
731 c
= mchars_num2char(seq
, ssz
);
733 sz
+= cond_width(p
, c
, &skip
);
735 case (ESCAPE_SPECIAL
):
736 rhs
= mchars_spec2str
737 (p
->symtab
, seq
, ssz
, &rsz
);
745 case (ESCAPE_SKIPCHAR
):
760 for (i
= 0; i
< rsz
; i
++)
761 sz
+= (*p
->width
)(p
, *rhs
++);
764 sz
+= cond_width(p
, ' ', &skip
);
768 sz
+= cond_width(p
, '-', &skip
);
783 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
801 r
= su
->scale
/ 1000;
813 return(/* LINTED */(size_t)
818 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
822 v
= ((*p
->hspan
)(p
, su
));
825 return((size_t) /* LINTED */