]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.205 2012/07/16 21:30:42 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 int ntab
; /* number of tabs to prepend */
110 size_t vis
; /* current visual position on output */
111 size_t vbl
; /* number of blanks to prepend to output */
112 size_t vend
; /* end of word visual position on output */
113 size_t bp
; /* visual right border position */
114 size_t dv
; /* temporary for visual pos calculations */
115 int j
; /* temporary loop index for p->buf */
116 int jhy
; /* last hyph before overflow w/r/t j */
117 size_t maxvis
; /* output position of visible boundary */
118 size_t mmax
; /* used in calculating bp */
121 * First, establish the maximum columns of "visible" content.
122 * This is usually the difference between the right-margin and
123 * an indentation, but can be, for tagged lists or columns, a
124 * small set of values.
126 assert (p
->rmargin
>= p
->offset
);
127 dv
= p
->rmargin
- p
->offset
;
128 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
129 dv
= p
->maxrmargin
- p
->offset
;
130 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
132 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
135 * Calculate the required amount of padding.
137 vbl
= p
->offset
+ p
->overstep
> p
->viscol
?
138 p
->offset
+ p
->overstep
- p
->viscol
: 0;
145 * Handle literal tab characters: collapse all
146 * subsequent tabs into a single huge set of spaces.
149 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
150 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
158 * Count up visible word characters. Control sequences
159 * (starting with the CSI) aren't counted. A space
160 * generates a non-printing word, which is valid (the
161 * space is printed according to regular spacing rules).
164 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
165 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
168 /* Back over the the last printed character. */
169 if (8 == p
->buf
[j
]) {
171 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
176 /* Break at the hyphen point if we overrun. */
177 if (vend
> vis
&& vend
< bp
&&
178 ASCII_HYPH
== p
->buf
[j
])
181 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
185 * Find out whether we would exceed the right margin.
186 * If so, break to the next line.
188 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
192 if (TERMP_NOBREAK
& p
->flags
) {
194 vend
+= p
->rmargin
- p
->offset
;
198 /* use pending tabs on the new line */
201 vbl
+= ntab
* p
->tabwidth
;
203 /* Remove the p->overstep width. */
205 bp
+= (size_t)p
->overstep
;
209 /* Write out the [remaining] word. */
210 for ( ; i
< p
->col
; i
++) {
211 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
213 if ('\t' == p
->buf
[i
])
215 if (' ' == p
->buf
[i
]) {
217 while (' ' == p
->buf
[i
])
219 dv
= (size_t)(i
- j
) * (*p
->width
)(p
, ' ');
224 if (ASCII_NBRSP
== p
->buf
[i
]) {
225 vbl
+= (*p
->width
)(p
, ' ');
230 * Now we definitely know there will be
231 * printable characters to output,
232 * so write preceding white space now.
235 (*p
->advance
)(p
, vbl
);
240 if (ASCII_HYPH
== p
->buf
[i
]) {
241 (*p
->letter
)(p
, '-');
242 p
->viscol
+= (*p
->width
)(p
, '-');
246 (*p
->letter
)(p
, p
->buf
[i
]);
248 p
->viscol
-= (*p
->width
)(p
, p
->buf
[i
-1]);
250 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
256 * If there was trailing white space, it was not printed;
257 * so reset the cursor position accordingly.
265 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
271 if (TERMP_HANG
& p
->flags
) {
272 /* We need one blank after the tag. */
273 p
->overstep
= (int)(vis
- maxvis
+ (*p
->width
)(p
, ' '));
276 * If we have overstepped the margin, temporarily move
277 * it to the right and flag the rest of the line to be
284 } else if (TERMP_DANGLE
& p
->flags
)
287 /* If the column was overrun, break the line. */
289 ((TERMP_TWOSPACE
& p
->flags
) ? (*p
->width
)(p
, ' ') : 0)) {
297 * A newline only breaks an existing line; it won't assert vertical
298 * space. All data in the output buffer is flushed prior to the newline
302 term_newln(struct termp
*p
)
305 p
->flags
|= TERMP_NOSPACE
;
306 if (p
->col
|| p
->viscol
)
312 * Asserts a vertical space (a full, empty line-break between lines).
313 * Note that if used twice, this will cause two blank spaces and so on.
314 * All data in the output buffer is flushed prior to the newline
318 term_vspace(struct termp
*p
)
330 term_fontlast(struct termp
*p
)
335 p
->fontl
= p
->fontq
[p
->fonti
];
336 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
;
350 term_fontpush(struct termp
*p
, enum termfont f
)
353 assert(p
->fonti
+ 1 < 10);
354 p
->fontl
= p
->fontq
[p
->fonti
];
355 p
->fontq
[++p
->fonti
] = f
;
360 term_fontq(struct termp
*p
)
363 return(&p
->fontq
[p
->fonti
]);
368 term_fonttop(struct termp
*p
)
371 return(p
->fontq
[p
->fonti
]);
376 term_fontpopq(struct termp
*p
, const void *key
)
379 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
381 assert(p
->fonti
>= 0);
386 term_fontpop(struct termp
*p
)
394 * Handle pwords, partial words, which may be either a single word or a
395 * phrase that cannot be broken down (such as a literal string). This
396 * handles word styling.
399 term_word(struct termp
*p
, const char *word
)
401 const char *seq
, *cp
;
407 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
408 if ( ! (TERMP_KEEP
& p
->flags
)) {
409 if (TERMP_PREKEEP
& p
->flags
)
410 p
->flags
|= TERMP_KEEP
;
412 if (TERMP_SENTENCE
& p
->flags
)
415 bufferc(p
, ASCII_NBRSP
);
418 if ( ! (p
->flags
& TERMP_NONOSPACE
))
419 p
->flags
&= ~TERMP_NOSPACE
;
421 p
->flags
|= TERMP_NOSPACE
;
423 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_IGNDELIM
);
425 while ('\0' != *word
) {
427 if (TERMP_SKIPCHAR
& p
->flags
) {
428 p
->flags
&= ~TERMP_SKIPCHAR
;
432 ssz
= strcspn(word
, "\\");
433 encode(p
, word
, ssz
);
439 esc
= mandoc_escape(&word
, &seq
, &sz
);
440 if (ESCAPE_ERROR
== esc
)
443 if (TERMENC_ASCII
!= p
->enc
)
445 case (ESCAPE_UNICODE
):
446 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
451 case (ESCAPE_SPECIAL
):
452 uc
= mchars_spec2cp(p
->symtab
, seq
, sz
);
462 case (ESCAPE_UNICODE
):
465 case (ESCAPE_NUMBERED
):
466 c
= mchars_num2char(seq
, sz
);
470 case (ESCAPE_SPECIAL
):
471 cp
= mchars_spec2str(p
->symtab
, seq
, sz
, &ssz
);
477 case (ESCAPE_FONTBOLD
):
478 term_fontrepl(p
, TERMFONT_BOLD
);
480 case (ESCAPE_FONTITALIC
):
481 term_fontrepl(p
, TERMFONT_UNDER
);
485 case (ESCAPE_FONTROMAN
):
486 term_fontrepl(p
, TERMFONT_NONE
);
488 case (ESCAPE_FONTPREV
):
491 case (ESCAPE_NOSPACE
):
492 if (TERMP_SKIPCHAR
& p
->flags
)
493 p
->flags
&= ~TERMP_SKIPCHAR
;
494 else if ('\0' == *word
)
495 p
->flags
|= TERMP_NOSPACE
;
497 case (ESCAPE_SKIPCHAR
):
498 p
->flags
|= TERMP_SKIPCHAR
;
507 adjbuf(struct termp
*p
, int sz
)
512 while (sz
>= p
->maxcols
)
515 p
->buf
= mandoc_realloc
516 (p
->buf
, sizeof(int) * (size_t)p
->maxcols
);
520 bufferc(struct termp
*p
, char c
)
523 if (p
->col
+ 1 >= p
->maxcols
)
524 adjbuf(p
, p
->col
+ 1);
526 p
->buf
[p
->col
++] = c
;
531 * Do this for a single (probably unicode) value.
532 * Does not check for non-decorated glyphs.
535 encode1(struct termp
*p
, int c
)
539 if (TERMP_SKIPCHAR
& p
->flags
) {
540 p
->flags
&= ~TERMP_SKIPCHAR
;
544 if (p
->col
+ 4 >= p
->maxcols
)
545 adjbuf(p
, p
->col
+ 4);
549 if (TERMFONT_NONE
== f
) {
550 p
->buf
[p
->col
++] = c
;
552 } else if (TERMFONT_UNDER
== f
) {
553 p
->buf
[p
->col
++] = '_';
555 p
->buf
[p
->col
++] = c
;
557 p
->buf
[p
->col
++] = 8;
558 p
->buf
[p
->col
++] = c
;
562 encode(struct termp
*p
, const char *word
, size_t sz
)
567 if (TERMP_SKIPCHAR
& p
->flags
) {
568 p
->flags
&= ~TERMP_SKIPCHAR
;
576 * Encode and buffer a string of characters. If the current
577 * font mode is unset, buffer directly, else encode then buffer
578 * character by character.
581 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
582 if (p
->col
+ len
>= p
->maxcols
)
583 adjbuf(p
, p
->col
+ len
);
584 for (i
= 0; i
< len
; i
++)
585 p
->buf
[p
->col
++] = word
[i
];
589 /* Pre-buffer, assuming worst-case. */
591 if (p
->col
+ 1 + (len
* 3) >= p
->maxcols
)
592 adjbuf(p
, p
->col
+ 1 + (len
* 3));
594 for (i
= 0; i
< len
; i
++) {
595 if (ASCII_HYPH
!= word
[i
] &&
596 ! isgraph((unsigned char)word
[i
])) {
597 p
->buf
[p
->col
++] = word
[i
];
601 if (TERMFONT_UNDER
== f
)
602 p
->buf
[p
->col
++] = '_';
603 else if (ASCII_HYPH
== word
[i
])
604 p
->buf
[p
->col
++] = '-';
606 p
->buf
[p
->col
++] = word
[i
];
608 p
->buf
[p
->col
++] = 8;
609 p
->buf
[p
->col
++] = word
[i
];
614 term_len(const struct termp
*p
, size_t sz
)
617 return((*p
->width
)(p
, ' ') * sz
);
621 cond_width(const struct termp
*p
, int c
, int *skip
)
628 return((*p
->width
)(p
, c
));
632 term_strlen(const struct termp
*p
, const char *cp
)
636 const char *seq
, *rhs
;
638 static const char rej
[] = { '\\', ASCII_HYPH
, ASCII_NBRSP
, '\0' };
641 * Account for escaped sequences within string length
642 * calculations. This follows the logic in term_word() as we
643 * must calculate the width of produced strings.
648 while ('\0' != *cp
) {
649 rsz
= strcspn(cp
, rej
);
650 for (i
= 0; i
< rsz
; i
++)
651 sz
+= cond_width(p
, *cp
++, &skip
);
657 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
658 if (ESCAPE_ERROR
== esc
)
661 if (TERMENC_ASCII
!= p
->enc
)
663 case (ESCAPE_UNICODE
):
668 sz
+= cond_width(p
, c
, &skip
);
670 case (ESCAPE_SPECIAL
):
672 (p
->symtab
, seq
, ssz
);
675 sz
+= cond_width(p
, c
, &skip
);
684 case (ESCAPE_UNICODE
):
685 sz
+= cond_width(p
, '?', &skip
);
687 case (ESCAPE_NUMBERED
):
688 c
= mchars_num2char(seq
, ssz
);
690 sz
+= cond_width(p
, c
, &skip
);
692 case (ESCAPE_SPECIAL
):
693 rhs
= mchars_spec2str
694 (p
->symtab
, seq
, ssz
, &rsz
);
702 case (ESCAPE_SKIPCHAR
):
717 for (i
= 0; i
< rsz
; i
++)
718 sz
+= (*p
->width
)(p
, *rhs
++);
721 sz
+= cond_width(p
, ' ', &skip
);
725 sz
+= cond_width(p
, '-', &skip
);
738 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
756 r
= su
->scale
/ 1000;
768 return(/* LINTED */(size_t)
773 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
777 v
= ((*p
->hspan
)(p
, su
));
780 return((size_t) /* LINTED */