]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.203 2012/05/31 22:29:13 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012 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>
36 static size_t cond_width(const struct termp
*, int, int *);
37 static void adjbuf(struct termp
*p
, int);
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.
87 * - TERMP_TWOSPACE: make sure there is room for at least two space
88 * characters of padding. Otherwise, rather break the line.
90 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
91 * the line is overrun, and don't pad-right if it's underrun.
93 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
94 * overrunning, instead save the position and continue at that point
95 * when the next invocation.
97 * In-line line breaking:
99 * If TERMP_NOBREAK is specified and the line overruns the right
100 * margin, it will break and pad-right to the right margin after
101 * writing. If maxrmargin is violated, it will break and continue
102 * writing from the right-margin, which will lead to the above scenario
103 * upon exit. Otherwise, the line will break at the right margin.
106 term_flushln(struct termp
*p
)
108 int i
; /* current input position in p->buf */
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 int j
; /* temporary loop index for p->buf */
115 int 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 assert (p
->rmargin
>= p
->offset
);
126 dv
= p
->rmargin
- p
->offset
;
127 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
128 dv
= p
->maxrmargin
- p
->offset
;
129 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
131 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
134 * Calculate the required amount of padding.
136 vbl
= p
->offset
+ p
->overstep
> p
->viscol
?
137 p
->offset
+ p
->overstep
- p
->viscol
: 0;
144 * Handle literal tab characters: collapse all
145 * subsequent tabs into a single huge set of spaces.
147 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
148 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 ((j
&& ' ' == 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
])
178 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
182 * Find out whether we would exceed the right margin.
183 * If so, break to the next line.
185 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
189 if (TERMP_NOBREAK
& p
->flags
) {
191 vend
+= p
->rmargin
- p
->offset
;
195 /* Remove the p->overstep width. */
197 bp
+= (size_t)p
->overstep
;
201 /* Write out the [remaining] word. */
202 for ( ; i
< p
->col
; i
++) {
203 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
205 if ('\t' == p
->buf
[i
])
207 if (' ' == p
->buf
[i
]) {
209 while (' ' == p
->buf
[i
])
211 dv
= (size_t)(i
- j
) * (*p
->width
)(p
, ' ');
216 if (ASCII_NBRSP
== p
->buf
[i
]) {
217 vbl
+= (*p
->width
)(p
, ' ');
222 * Now we definitely know there will be
223 * printable characters to output,
224 * so write preceding white space now.
227 (*p
->advance
)(p
, vbl
);
232 if (ASCII_HYPH
== p
->buf
[i
]) {
233 (*p
->letter
)(p
, '-');
234 p
->viscol
+= (*p
->width
)(p
, '-');
238 (*p
->letter
)(p
, p
->buf
[i
]);
240 p
->viscol
-= (*p
->width
)(p
, p
->buf
[i
-1]);
242 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
248 * If there was trailing white space, it was not printed;
249 * so reset the cursor position accordingly.
257 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
263 if (TERMP_HANG
& p
->flags
) {
264 /* We need one blank after the tag. */
265 p
->overstep
= (int)(vis
- maxvis
+ (*p
->width
)(p
, ' '));
268 * Behave exactly the same way as groff:
269 * If we have overstepped the margin, temporarily move
270 * it to the right and flag the rest of the line to be
272 * If we landed right at the margin, be happy.
273 * If we are one step before the margin, temporarily
274 * move it one step LEFT and flag the rest of the line
277 if (p
->overstep
< -1)
281 } else if (TERMP_DANGLE
& p
->flags
)
284 /* If the column was overrun, break the line. */
286 ((TERMP_TWOSPACE
& p
->flags
) ? (*p
->width
)(p
, ' ') : 0)) {
294 * A newline only breaks an existing line; it won't assert vertical
295 * space. All data in the output buffer is flushed prior to the newline
299 term_newln(struct termp
*p
)
302 p
->flags
|= TERMP_NOSPACE
;
303 if (p
->col
|| p
->viscol
)
309 * Asserts a vertical space (a full, empty line-break between lines).
310 * Note that if used twice, this will cause two blank spaces and so on.
311 * All data in the output buffer is flushed prior to the newline
315 term_vspace(struct termp
*p
)
327 term_fontlast(struct termp
*p
)
332 p
->fontl
= p
->fontq
[p
->fonti
];
333 p
->fontq
[p
->fonti
] = f
;
338 term_fontrepl(struct termp
*p
, enum termfont f
)
341 p
->fontl
= p
->fontq
[p
->fonti
];
342 p
->fontq
[p
->fonti
] = f
;
347 term_fontpush(struct termp
*p
, enum termfont f
)
350 assert(p
->fonti
+ 1 < 10);
351 p
->fontl
= p
->fontq
[p
->fonti
];
352 p
->fontq
[++p
->fonti
] = f
;
357 term_fontq(struct termp
*p
)
360 return(&p
->fontq
[p
->fonti
]);
365 term_fonttop(struct termp
*p
)
368 return(p
->fontq
[p
->fonti
]);
373 term_fontpopq(struct termp
*p
, const void *key
)
376 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
378 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 *seq
, *cp
;
404 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
405 if ( ! (TERMP_KEEP
& p
->flags
)) {
406 if (TERMP_PREKEEP
& p
->flags
)
407 p
->flags
|= TERMP_KEEP
;
409 if (TERMP_SENTENCE
& p
->flags
)
412 bufferc(p
, ASCII_NBRSP
);
415 if ( ! (p
->flags
& TERMP_NONOSPACE
))
416 p
->flags
&= ~TERMP_NOSPACE
;
418 p
->flags
|= TERMP_NOSPACE
;
420 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_IGNDELIM
);
422 while ('\0' != *word
) {
424 if (TERMP_SKIPCHAR
& p
->flags
) {
425 p
->flags
&= ~TERMP_SKIPCHAR
;
429 ssz
= strcspn(word
, "\\");
430 encode(p
, word
, ssz
);
436 esc
= mandoc_escape(&word
, &seq
, &sz
);
437 if (ESCAPE_ERROR
== esc
)
440 if (TERMENC_ASCII
!= p
->enc
)
442 case (ESCAPE_UNICODE
):
443 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
448 case (ESCAPE_SPECIAL
):
449 uc
= mchars_spec2cp(p
->symtab
, seq
, sz
);
459 case (ESCAPE_UNICODE
):
462 case (ESCAPE_NUMBERED
):
463 c
= mchars_num2char(seq
, sz
);
467 case (ESCAPE_SPECIAL
):
468 cp
= mchars_spec2str(p
->symtab
, seq
, sz
, &ssz
);
474 case (ESCAPE_FONTBOLD
):
475 term_fontrepl(p
, TERMFONT_BOLD
);
477 case (ESCAPE_FONTITALIC
):
478 term_fontrepl(p
, TERMFONT_UNDER
);
482 case (ESCAPE_FONTROMAN
):
483 term_fontrepl(p
, TERMFONT_NONE
);
485 case (ESCAPE_FONTPREV
):
488 case (ESCAPE_NOSPACE
):
489 if (TERMP_SKIPCHAR
& p
->flags
)
490 p
->flags
&= ~TERMP_SKIPCHAR
;
491 else if ('\0' == *word
)
492 p
->flags
|= TERMP_NOSPACE
;
494 case (ESCAPE_SKIPCHAR
):
495 p
->flags
|= TERMP_SKIPCHAR
;
504 adjbuf(struct termp
*p
, int sz
)
509 while (sz
>= p
->maxcols
)
512 p
->buf
= mandoc_realloc
513 (p
->buf
, sizeof(int) * (size_t)p
->maxcols
);
517 bufferc(struct termp
*p
, char c
)
520 if (p
->col
+ 1 >= p
->maxcols
)
521 adjbuf(p
, p
->col
+ 1);
523 p
->buf
[p
->col
++] = c
;
528 * Do this for a single (probably unicode) value.
529 * Does not check for non-decorated glyphs.
532 encode1(struct termp
*p
, int c
)
536 if (TERMP_SKIPCHAR
& p
->flags
) {
537 p
->flags
&= ~TERMP_SKIPCHAR
;
541 if (p
->col
+ 4 >= p
->maxcols
)
542 adjbuf(p
, p
->col
+ 4);
546 if (TERMFONT_NONE
== f
) {
547 p
->buf
[p
->col
++] = c
;
549 } else if (TERMFONT_UNDER
== f
) {
550 p
->buf
[p
->col
++] = '_';
552 p
->buf
[p
->col
++] = c
;
554 p
->buf
[p
->col
++] = 8;
555 p
->buf
[p
->col
++] = c
;
559 encode(struct termp
*p
, const char *word
, size_t sz
)
564 if (TERMP_SKIPCHAR
& p
->flags
) {
565 p
->flags
&= ~TERMP_SKIPCHAR
;
573 * Encode and buffer a string of characters. If the current
574 * font mode is unset, buffer directly, else encode then buffer
575 * character by character.
578 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
579 if (p
->col
+ len
>= p
->maxcols
)
580 adjbuf(p
, p
->col
+ len
);
581 for (i
= 0; i
< len
; i
++)
582 p
->buf
[p
->col
++] = word
[i
];
586 /* Pre-buffer, assuming worst-case. */
588 if (p
->col
+ 1 + (len
* 3) >= p
->maxcols
)
589 adjbuf(p
, p
->col
+ 1 + (len
* 3));
591 for (i
= 0; i
< len
; i
++) {
592 if (ASCII_HYPH
!= word
[i
] &&
593 ! isgraph((unsigned char)word
[i
])) {
594 p
->buf
[p
->col
++] = word
[i
];
598 if (TERMFONT_UNDER
== f
)
599 p
->buf
[p
->col
++] = '_';
600 else if (ASCII_HYPH
== word
[i
])
601 p
->buf
[p
->col
++] = '-';
603 p
->buf
[p
->col
++] = word
[i
];
605 p
->buf
[p
->col
++] = 8;
606 p
->buf
[p
->col
++] = word
[i
];
611 term_len(const struct termp
*p
, size_t sz
)
614 return((*p
->width
)(p
, ' ') * sz
);
618 cond_width(const struct termp
*p
, int c
, int *skip
)
625 return((*p
->width
)(p
, c
));
629 term_strlen(const struct termp
*p
, const char *cp
)
633 const char *seq
, *rhs
;
635 static const char rej
[] = { '\\', ASCII_HYPH
, ASCII_NBRSP
, '\0' };
638 * Account for escaped sequences within string length
639 * calculations. This follows the logic in term_word() as we
640 * must calculate the width of produced strings.
645 while ('\0' != *cp
) {
646 rsz
= strcspn(cp
, rej
);
647 for (i
= 0; i
< rsz
; i
++)
648 sz
+= cond_width(p
, *cp
++, &skip
);
654 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
655 if (ESCAPE_ERROR
== esc
)
658 if (TERMENC_ASCII
!= p
->enc
)
660 case (ESCAPE_UNICODE
):
665 sz
+= cond_width(p
, c
, &skip
);
667 case (ESCAPE_SPECIAL
):
669 (p
->symtab
, seq
, ssz
);
672 sz
+= cond_width(p
, c
, &skip
);
681 case (ESCAPE_UNICODE
):
682 sz
+= cond_width(p
, '?', &skip
);
684 case (ESCAPE_NUMBERED
):
685 c
= mchars_num2char(seq
, ssz
);
687 sz
+= cond_width(p
, c
, &skip
);
689 case (ESCAPE_SPECIAL
):
690 rhs
= mchars_spec2str
691 (p
->symtab
, seq
, ssz
, &rsz
);
699 case (ESCAPE_SKIPCHAR
):
714 for (i
= 0; i
< rsz
; i
++)
715 sz
+= (*p
->width
)(p
, *rhs
++);
718 sz
+= cond_width(p
, ' ', &skip
);
722 sz
+= cond_width(p
, '-', &skip
);
735 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
753 r
= su
->scale
/ 1000;
765 return(/* LINTED */(size_t)
770 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
774 v
= ((*p
->hspan
)(p
, su
));
777 return((size_t) /* LINTED */