1 /* $Vendor-Id: term.c,v 1.201 2011/09/21 09:57:13 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 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 void adjbuf(struct termp
*p
, int);
37 static void bufferc(struct termp
*, char);
38 static void encode(struct termp
*, const char *, size_t);
39 static void encode1(struct termp
*, int);
42 term_free(struct termp
*p
)
48 mchars_free(p
->symtab
);
55 term_begin(struct termp
*p
, term_margin head
,
56 term_margin foot
, const void *arg
)
67 term_end(struct termp
*p
)
74 * Flush a line of text. A "line" is loosely defined as being something
75 * that should be followed by a newline, regardless of whether it's
76 * broken apart by newlines getting there. A line can also be a
77 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
78 * not have a trailing newline.
80 * The following flags may be specified:
82 * - TERMP_NOBREAK: this is the most important and is used when making
83 * columns. In short: don't print a newline and instead expect the
84 * next call to do the padding up to the start of the next column.
86 * - TERMP_TWOSPACE: make sure there is room for at least two space
87 * characters of padding. Otherwise, rather break the line.
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 int i
; /* current input position in p->buf */
108 size_t vis
; /* current visual position on output */
109 size_t vbl
; /* number of blanks to prepend to output */
110 size_t vend
; /* end of word visual position on output */
111 size_t bp
; /* visual right border position */
112 size_t dv
; /* temporary for visual pos calculations */
113 int j
; /* temporary loop index for p->buf */
114 int jhy
; /* last hyph before overflow w/r/t j */
115 size_t maxvis
; /* output position of visible boundary */
116 size_t mmax
; /* used in calculating bp */
119 * First, establish the maximum columns of "visible" content.
120 * This is usually the difference between the right-margin and
121 * an indentation, but can be, for tagged lists or columns, a
122 * small set of values.
124 assert (p
->rmargin
>= p
->offset
);
125 dv
= p
->rmargin
- p
->offset
;
126 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
127 dv
= p
->maxrmargin
- p
->offset
;
128 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
130 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
133 * Calculate the required amount of padding.
135 vbl
= p
->offset
+ p
->overstep
> p
->viscol
?
136 p
->offset
+ p
->overstep
- p
->viscol
: 0;
143 * Handle literal tab characters: collapse all
144 * 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
;
154 * Count up visible word characters. Control sequences
155 * (starting with the CSI) aren't counted. A space
156 * generates a non-printing word, which is valid (the
157 * space is printed according to regular spacing rules).
160 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
161 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
164 /* Back over the the last printed character. */
165 if (8 == p
->buf
[j
]) {
167 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
172 /* Break at the hyphen point if we overrun. */
173 if (vend
> vis
&& vend
< bp
&&
174 ASCII_HYPH
== p
->buf
[j
])
177 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
181 * Find out whether we would exceed the right margin.
182 * If so, break to the next line.
184 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
188 if (TERMP_NOBREAK
& p
->flags
) {
190 vend
+= p
->rmargin
- p
->offset
;
194 /* Remove the p->overstep width. */
196 bp
+= (size_t)p
->overstep
;
200 /* Write out the [remaining] word. */
201 for ( ; i
< p
->col
; i
++) {
202 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
204 if ('\t' == p
->buf
[i
])
206 if (' ' == p
->buf
[i
]) {
208 while (' ' == p
->buf
[i
])
210 dv
= (size_t)(i
- j
) * (*p
->width
)(p
, ' ');
215 if (ASCII_NBRSP
== p
->buf
[i
]) {
216 vbl
+= (*p
->width
)(p
, ' ');
221 * Now we definitely know there will be
222 * printable characters to output,
223 * so write preceding white space now.
226 (*p
->advance
)(p
, vbl
);
231 if (ASCII_HYPH
== p
->buf
[i
]) {
232 (*p
->letter
)(p
, '-');
233 p
->viscol
+= (*p
->width
)(p
, '-');
237 (*p
->letter
)(p
, p
->buf
[i
]);
239 p
->viscol
-= (*p
->width
)(p
, p
->buf
[i
-1]);
241 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
247 * If there was trailing white space, it was not printed;
248 * so reset the cursor position accordingly.
256 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
262 if (TERMP_HANG
& p
->flags
) {
263 /* We need one blank after the tag. */
264 p
->overstep
= (int)(vis
- maxvis
+ (*p
->width
)(p
, ' '));
267 * Behave exactly the same way as groff:
268 * If we have overstepped the margin, temporarily move
269 * it to the right and flag the rest of the line to be
271 * If we landed right at the margin, be happy.
272 * If we are one step before the margin, temporarily
273 * move it one step LEFT and flag the rest of the line
276 if (p
->overstep
< -1)
280 } else if (TERMP_DANGLE
& p
->flags
)
283 /* If the column was overrun, break the line. */
285 ((TERMP_TWOSPACE
& p
->flags
) ? (*p
->width
)(p
, ' ') : 0)) {
293 * A newline only breaks an existing line; it won't assert vertical
294 * space. All data in the output buffer is flushed prior to the newline
298 term_newln(struct termp
*p
)
301 p
->flags
|= TERMP_NOSPACE
;
302 if (p
->col
|| p
->viscol
)
308 * Asserts a vertical space (a full, empty line-break between lines).
309 * Note that if used twice, this will cause two blank spaces and so on.
310 * All data in the output buffer is flushed prior to the newline
314 term_vspace(struct termp
*p
)
323 term_fontlast(struct termp
*p
)
328 p
->fontl
= p
->fontq
[p
->fonti
];
329 p
->fontq
[p
->fonti
] = f
;
334 term_fontrepl(struct termp
*p
, enum termfont f
)
337 p
->fontl
= p
->fontq
[p
->fonti
];
338 p
->fontq
[p
->fonti
] = f
;
343 term_fontpush(struct termp
*p
, enum termfont f
)
346 assert(p
->fonti
+ 1 < 10);
347 p
->fontl
= p
->fontq
[p
->fonti
];
348 p
->fontq
[++p
->fonti
] = f
;
353 term_fontq(struct termp
*p
)
356 return(&p
->fontq
[p
->fonti
]);
361 term_fonttop(struct termp
*p
)
364 return(p
->fontq
[p
->fonti
]);
369 term_fontpopq(struct termp
*p
, const void *key
)
372 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
374 assert(p
->fonti
>= 0);
379 term_fontpop(struct termp
*p
)
387 * Handle pwords, partial words, which may be either a single word or a
388 * phrase that cannot be broken down (such as a literal string). This
389 * handles word styling.
392 term_word(struct termp
*p
, const char *word
)
394 const char *seq
, *cp
;
400 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
401 if ( ! (TERMP_KEEP
& p
->flags
)) {
402 if (TERMP_PREKEEP
& p
->flags
)
403 p
->flags
|= TERMP_KEEP
;
405 if (TERMP_SENTENCE
& p
->flags
)
408 bufferc(p
, ASCII_NBRSP
);
411 if ( ! (p
->flags
& TERMP_NONOSPACE
))
412 p
->flags
&= ~TERMP_NOSPACE
;
414 p
->flags
|= TERMP_NOSPACE
;
416 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_IGNDELIM
);
418 while ('\0' != *word
) {
419 if ((ssz
= strcspn(word
, "\\")) > 0)
420 encode(p
, word
, ssz
);
427 esc
= mandoc_escape(&word
, &seq
, &sz
);
428 if (ESCAPE_ERROR
== esc
)
431 if (TERMENC_ASCII
!= p
->enc
)
433 case (ESCAPE_UNICODE
):
434 uc
= mchars_num2uc(seq
+ 1, sz
- 1);
439 case (ESCAPE_SPECIAL
):
440 uc
= mchars_spec2cp(p
->symtab
, seq
, sz
);
450 case (ESCAPE_UNICODE
):
453 case (ESCAPE_NUMBERED
):
454 c
= mchars_num2char(seq
, sz
);
458 case (ESCAPE_SPECIAL
):
459 cp
= mchars_spec2str(p
->symtab
, seq
, sz
, &ssz
);
465 case (ESCAPE_FONTBOLD
):
466 term_fontrepl(p
, TERMFONT_BOLD
);
468 case (ESCAPE_FONTITALIC
):
469 term_fontrepl(p
, TERMFONT_UNDER
);
473 case (ESCAPE_FONTROMAN
):
474 term_fontrepl(p
, TERMFONT_NONE
);
476 case (ESCAPE_FONTPREV
):
479 case (ESCAPE_NOSPACE
):
481 p
->flags
|= TERMP_NOSPACE
;
490 adjbuf(struct termp
*p
, int sz
)
495 while (sz
>= p
->maxcols
)
498 p
->buf
= mandoc_realloc
499 (p
->buf
, sizeof(int) * (size_t)p
->maxcols
);
503 bufferc(struct termp
*p
, char c
)
506 if (p
->col
+ 1 >= p
->maxcols
)
507 adjbuf(p
, p
->col
+ 1);
509 p
->buf
[p
->col
++] = c
;
514 * Do this for a single (probably unicode) value.
515 * Does not check for non-decorated glyphs.
518 encode1(struct termp
*p
, int c
)
522 if (p
->col
+ 4 >= p
->maxcols
)
523 adjbuf(p
, p
->col
+ 4);
527 if (TERMFONT_NONE
== f
) {
528 p
->buf
[p
->col
++] = c
;
530 } else if (TERMFONT_UNDER
== f
) {
531 p
->buf
[p
->col
++] = '_';
533 p
->buf
[p
->col
++] = c
;
535 p
->buf
[p
->col
++] = 8;
536 p
->buf
[p
->col
++] = c
;
540 encode(struct termp
*p
, const char *word
, size_t sz
)
549 * Encode and buffer a string of characters. If the current
550 * font mode is unset, buffer directly, else encode then buffer
551 * character by character.
554 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
555 if (p
->col
+ len
>= p
->maxcols
)
556 adjbuf(p
, p
->col
+ len
);
557 for (i
= 0; i
< len
; i
++)
558 p
->buf
[p
->col
++] = word
[i
];
562 /* Pre-buffer, assuming worst-case. */
564 if (p
->col
+ 1 + (len
* 3) >= p
->maxcols
)
565 adjbuf(p
, p
->col
+ 1 + (len
* 3));
567 for (i
= 0; i
< len
; i
++) {
568 if (ASCII_HYPH
!= word
[i
] &&
569 ! isgraph((unsigned char)word
[i
])) {
570 p
->buf
[p
->col
++] = word
[i
];
574 if (TERMFONT_UNDER
== f
)
575 p
->buf
[p
->col
++] = '_';
576 else if (ASCII_HYPH
== word
[i
])
577 p
->buf
[p
->col
++] = '-';
579 p
->buf
[p
->col
++] = word
[i
];
581 p
->buf
[p
->col
++] = 8;
582 p
->buf
[p
->col
++] = word
[i
];
587 term_len(const struct termp
*p
, size_t sz
)
590 return((*p
->width
)(p
, ' ') * sz
);
595 term_strlen(const struct termp
*p
, const char *cp
)
599 const char *seq
, *rhs
;
601 static const char rej
[] = { '\\', ASCII_HYPH
, ASCII_NBRSP
, '\0' };
604 * Account for escaped sequences within string length
605 * calculations. This follows the logic in term_word() as we
606 * must calculate the width of produced strings.
610 while ('\0' != *cp
) {
611 rsz
= strcspn(cp
, rej
);
612 for (i
= 0; i
< rsz
; i
++)
613 sz
+= (*p
->width
)(p
, *cp
++);
619 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
620 if (ESCAPE_ERROR
== esc
)
623 if (TERMENC_ASCII
!= p
->enc
)
625 case (ESCAPE_UNICODE
):
630 sz
+= (*p
->width
)(p
, c
);
632 case (ESCAPE_SPECIAL
):
634 (p
->symtab
, seq
, ssz
);
637 sz
+= (*p
->width
)(p
, c
);
646 case (ESCAPE_UNICODE
):
647 sz
+= (*p
->width
)(p
, '?');
649 case (ESCAPE_NUMBERED
):
650 c
= mchars_num2char(seq
, ssz
);
652 sz
+= (*p
->width
)(p
, c
);
654 case (ESCAPE_SPECIAL
):
655 rhs
= mchars_spec2str
656 (p
->symtab
, seq
, ssz
, &rsz
);
671 for (i
= 0; i
< rsz
; i
++)
672 sz
+= (*p
->width
)(p
, *rhs
++);
675 sz
+= (*p
->width
)(p
, ' ');
679 sz
+= (*p
->width
)(p
, '-');
692 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
710 r
= su
->scale
/ 1000;
722 return(/* LINTED */(size_t)
727 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
731 v
= ((*p
->hspan
)(p
, su
));
734 return((size_t) /* LINTED */