]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.191 2011/05/15 22:29:50 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 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);
41 term_free(struct termp
*p
)
47 mchars_free(p
->symtab
);
54 term_begin(struct termp
*p
, term_margin head
,
55 term_margin foot
, const void *arg
)
66 term_end(struct termp
*p
)
74 term_alloc(enum termenc enc
)
78 p
= mandoc_calloc(1, sizeof(struct termp
));
85 * Flush a line of text. A "line" is loosely defined as being something
86 * that should be followed by a newline, regardless of whether it's
87 * broken apart by newlines getting there. A line can also be a
88 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
89 * not have a trailing newline.
91 * The following flags may be specified:
93 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
94 * offset value. This is useful when doing columnar lists where the
95 * prior column has right-padded.
97 * - TERMP_NOBREAK: this is the most important and is used when making
98 * columns. In short: don't print a newline and instead pad to the
99 * right margin. Used in conjunction with TERMP_NOLPAD.
101 * - TERMP_TWOSPACE: when padding, make sure there are at least two
102 * space characters of padding. Otherwise, rather break the line.
104 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
105 * the line is overrun, and don't pad-right if it's underrun.
107 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
108 * overruning, instead save the position and continue at that point
109 * when the next invocation.
111 * In-line line breaking:
113 * If TERMP_NOBREAK is specified and the line overruns the right
114 * margin, it will break and pad-right to the right margin after
115 * writing. If maxrmargin is violated, it will break and continue
116 * writing from the right-margin, which will lead to the above scenario
117 * upon exit. Otherwise, the line will break at the right margin.
120 term_flushln(struct termp
*p
)
122 int i
; /* current input position in p->buf */
123 size_t vis
; /* current visual position on output */
124 size_t vbl
; /* number of blanks to prepend to output */
125 size_t vend
; /* end of word visual position on output */
126 size_t bp
; /* visual right border position */
127 size_t dv
; /* temporary for visual pos calculations */
128 int j
; /* temporary loop index for p->buf */
129 int jhy
; /* last hyph before overflow w/r/t j */
130 size_t maxvis
; /* output position of visible boundary */
131 size_t mmax
; /* used in calculating bp */
134 * First, establish the maximum columns of "visible" content.
135 * This is usually the difference between the right-margin and
136 * an indentation, but can be, for tagged lists or columns, a
137 * small set of values.
139 assert (p
->rmargin
>= p
->offset
);
140 dv
= p
->rmargin
- p
->offset
;
141 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
142 dv
= p
->maxrmargin
- p
->offset
;
143 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
145 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
148 * Indent the first line of a paragraph.
150 vbl
= p
->flags
& TERMP_NOLPAD
? (size_t)0 : p
->offset
;
157 * Handle literal tab characters: collapse all
158 * subsequent tabs into a single huge set of spaces.
160 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
161 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
168 * Count up visible word characters. Control sequences
169 * (starting with the CSI) aren't counted. A space
170 * generates a non-printing word, which is valid (the
171 * space is printed according to regular spacing rules).
174 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
175 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
178 /* Back over the the last printed character. */
179 if (8 == p
->buf
[j
]) {
181 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
186 /* Break at the hyphen point if we overrun. */
187 if (vend
> vis
&& vend
< bp
&&
188 ASCII_HYPH
== p
->buf
[j
])
191 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
195 * Find out whether we would exceed the right margin.
196 * If so, break to the next line.
198 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
201 if (TERMP_NOBREAK
& p
->flags
) {
202 p
->viscol
= p
->rmargin
;
203 (*p
->advance
)(p
, p
->rmargin
);
204 vend
+= p
->rmargin
- p
->offset
;
210 /* Remove the p->overstep width. */
212 bp
+= (size_t)p
->overstep
;
216 /* Write out the [remaining] word. */
217 for ( ; i
< p
->col
; i
++) {
218 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
220 if ('\t' == p
->buf
[i
])
222 if (' ' == p
->buf
[i
]) {
224 while (' ' == p
->buf
[i
])
226 dv
= (size_t)(i
- j
) * (*p
->width
)(p
, ' ');
231 if (ASCII_NBRSP
== p
->buf
[i
]) {
232 vbl
+= (*p
->width
)(p
, ' ');
237 * Now we definitely know there will be
238 * printable characters to output,
239 * so write preceding white space now.
242 (*p
->advance
)(p
, vbl
);
247 if (ASCII_HYPH
== p
->buf
[i
]) {
248 (*p
->letter
)(p
, '-');
249 p
->viscol
+= (*p
->width
)(p
, '-');
251 (*p
->letter
)(p
, p
->buf
[i
]);
252 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
259 * If there was trailing white space, it was not printed;
260 * so reset the cursor position accordingly.
267 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
273 if (TERMP_HANG
& p
->flags
) {
274 /* We need one blank after the tag. */
275 p
->overstep
= (int)(vis
- maxvis
+ (*p
->width
)(p
, ' '));
278 * Behave exactly the same way as groff:
279 * If we have overstepped the margin, temporarily move
280 * it to the right and flag the rest of the line to be
282 * If we landed right at the margin, be happy.
283 * If we are one step before the margin, temporarily
284 * move it one step LEFT and flag the rest of the line
287 if (p
->overstep
>= -1) {
288 assert((int)maxvis
+ p
->overstep
>= 0);
289 maxvis
+= (size_t)p
->overstep
;
293 } else if (TERMP_DANGLE
& p
->flags
)
298 ((TERMP_TWOSPACE
& p
->flags
) ? (*p
->width
)(p
, ' ') : 0)) {
299 p
->viscol
+= maxvis
- vis
;
300 (*p
->advance
)(p
, maxvis
- vis
);
301 vis
+= (maxvis
- vis
);
302 } else { /* ...or newline break. */
304 p
->viscol
= p
->rmargin
;
305 (*p
->advance
)(p
, p
->rmargin
);
311 * A newline only breaks an existing line; it won't assert vertical
312 * space. All data in the output buffer is flushed prior to the newline
316 term_newln(struct termp
*p
)
319 p
->flags
|= TERMP_NOSPACE
;
320 if (0 == p
->col
&& 0 == p
->viscol
) {
321 p
->flags
&= ~TERMP_NOLPAD
;
325 p
->flags
&= ~TERMP_NOLPAD
;
330 * Asserts a vertical space (a full, empty line-break between lines).
331 * Note that if used twice, this will cause two blank spaces and so on.
332 * All data in the output buffer is flushed prior to the newline
336 term_vspace(struct termp
*p
)
345 term_fontlast(struct termp
*p
)
350 p
->fontl
= p
->fontq
[p
->fonti
];
351 p
->fontq
[p
->fonti
] = f
;
356 term_fontrepl(struct termp
*p
, enum termfont f
)
359 p
->fontl
= p
->fontq
[p
->fonti
];
360 p
->fontq
[p
->fonti
] = f
;
365 term_fontpush(struct termp
*p
, enum termfont f
)
368 assert(p
->fonti
+ 1 < 10);
369 p
->fontl
= p
->fontq
[p
->fonti
];
370 p
->fontq
[++p
->fonti
] = f
;
375 term_fontq(struct termp
*p
)
378 return(&p
->fontq
[p
->fonti
]);
383 term_fonttop(struct termp
*p
)
386 return(p
->fontq
[p
->fonti
]);
391 term_fontpopq(struct termp
*p
, const void *key
)
394 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
396 assert(p
->fonti
>= 0);
401 term_fontpop(struct termp
*p
)
409 * Handle pwords, partial words, which may be either a single word or a
410 * phrase that cannot be broken down (such as a literal string). This
411 * handles word styling.
414 term_word(struct termp
*p
, const char *word
)
416 const char *seq
, *cp
;
422 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
423 if ( ! (TERMP_KEEP
& p
->flags
)) {
424 if (TERMP_PREKEEP
& p
->flags
)
425 p
->flags
|= TERMP_KEEP
;
427 if (TERMP_SENTENCE
& p
->flags
)
430 bufferc(p
, ASCII_NBRSP
);
433 if ( ! (p
->flags
& TERMP_NONOSPACE
))
434 p
->flags
&= ~TERMP_NOSPACE
;
436 p
->flags
|= TERMP_NOSPACE
;
438 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_IGNDELIM
);
440 while ('\0' != *word
) {
441 if ((ssz
= strcspn(word
, "\\")) > 0)
442 encode(p
, word
, ssz
);
449 esc
= mandoc_escape(&word
, &seq
, &sz
);
450 if (ESCAPE_ERROR
== esc
)
454 case (ESCAPE_NUMBERED
):
455 if ('\0' != (c
= mchars_num2char(seq
, sz
)))
458 case (ESCAPE_PREDEF
):
459 cp
= mchars_res2str(p
->symtab
, seq
, sz
, &ssz
);
463 case (ESCAPE_SPECIAL
):
464 cp
= mchars_spec2str(p
->symtab
, seq
, sz
, &ssz
);
470 case (ESCAPE_FONTBOLD
):
471 term_fontrepl(p
, TERMFONT_BOLD
);
473 case (ESCAPE_FONTITALIC
):
474 term_fontrepl(p
, TERMFONT_UNDER
);
476 case (ESCAPE_FONTROMAN
):
477 term_fontrepl(p
, TERMFONT_NONE
);
479 case (ESCAPE_FONTPREV
):
482 case (ESCAPE_NOSPACE
):
484 p
->flags
|= TERMP_NOSPACE
;
493 adjbuf(struct termp
*p
, int sz
)
498 while (sz
>= p
->maxcols
)
501 p
->buf
= mandoc_realloc
502 (p
->buf
, sizeof(int) * (size_t)p
->maxcols
);
506 bufferc(struct termp
*p
, char c
)
509 if (p
->col
+ 1 >= p
->maxcols
)
510 adjbuf(p
, p
->col
+ 1);
512 p
->buf
[p
->col
++] = c
;
516 encode(struct termp
*p
, const char *word
, size_t sz
)
525 * Encode and buffer a string of characters. If the current
526 * font mode is unset, buffer directly, else encode then buffer
527 * character by character.
530 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
531 if (p
->col
+ len
>= p
->maxcols
)
532 adjbuf(p
, p
->col
+ len
);
533 for (i
= 0; i
< len
; i
++)
534 p
->buf
[p
->col
++] = word
[i
];
538 /* Pre-buffer, assuming worst-case. */
540 if (p
->col
+ 1 + (len
* 3) >= p
->maxcols
)
541 adjbuf(p
, p
->col
+ 1 + (len
* 3));
543 for (i
= 0; i
< len
; i
++) {
544 if ( ! isgraph((unsigned char)word
[i
])) {
545 p
->buf
[p
->col
++] = word
[i
];
549 if (TERMFONT_UNDER
== f
)
550 p
->buf
[p
->col
++] = '_';
552 p
->buf
[p
->col
++] = word
[i
];
554 p
->buf
[p
->col
++] = 8;
555 p
->buf
[p
->col
++] = word
[i
];
560 term_len(const struct termp
*p
, size_t sz
)
563 return((*p
->width
)(p
, ' ') * sz
);
568 term_strlen(const struct termp
*p
, const char *cp
)
572 const char *seq
, *rhs
;
573 static const char rej
[] = { '\\', ASCII_HYPH
, ASCII_NBRSP
, '\0' };
576 * Account for escaped sequences within string length
577 * calculations. This follows the logic in term_word() as we
578 * must calculate the width of produced strings.
582 while ('\0' != *cp
) {
583 rsz
= strcspn(cp
, rej
);
584 for (i
= 0; i
< rsz
; i
++)
585 sz
+= (*p
->width
)(p
, *cp
++);
591 switch (mandoc_escape(&cp
, &seq
, &ssz
)) {
594 case (ESCAPE_NUMBERED
):
595 c
= mchars_num2char(seq
, ssz
);
597 sz
+= (*p
->width
)(p
, c
);
599 case (ESCAPE_PREDEF
):
601 (p
->symtab
, seq
, ssz
, &rsz
);
603 case (ESCAPE_SPECIAL
):
604 rhs
= mchars_spec2str
605 (p
->symtab
, seq
, ssz
, &rsz
);
620 for (i
= 0; i
< rsz
; i
++)
621 sz
+= (*p
->width
)(p
, *rhs
++);
624 sz
+= (*p
->width
)(p
, ' ');
628 sz
+= (*p
->width
)(p
, '-');
641 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
659 r
= su
->scale
/ 1000;
671 return(/* LINTED */(size_t)
676 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
680 v
= ((*p
->hspan
)(p
, su
));
683 return((size_t) /* LINTED */