]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
92a673770bd237e447dee781507a6f3355c87f8d
1 /* $Id: term.c,v 1.188 2011/05/14 18:15:20 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 spec(struct termp
*, const char *, size_t);
37 static void res(struct termp
*, const char *, size_t);
38 static void bufferc(struct termp
*, char);
39 static void adjbuf(struct termp
*p
, int);
40 static void encode(struct termp
*, const char *, size_t);
44 term_free(struct termp
*p
)
50 mchars_free(p
->symtab
);
57 term_begin(struct termp
*p
, term_margin head
,
58 term_margin foot
, const void *arg
)
69 term_end(struct termp
*p
)
77 term_alloc(enum termenc enc
)
81 p
= mandoc_calloc(1, sizeof(struct termp
));
88 * Flush a line of text. A "line" is loosely defined as being something
89 * that should be followed by a newline, regardless of whether it's
90 * broken apart by newlines getting there. A line can also be a
91 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
92 * not have a trailing newline.
94 * The following flags may be specified:
96 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
97 * offset value. This is useful when doing columnar lists where the
98 * prior column has right-padded.
100 * - TERMP_NOBREAK: this is the most important and is used when making
101 * columns. In short: don't print a newline and instead pad to the
102 * right margin. Used in conjunction with TERMP_NOLPAD.
104 * - TERMP_TWOSPACE: when padding, make sure there are at least two
105 * space characters of padding. Otherwise, rather break the line.
107 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
108 * the line is overrun, and don't pad-right if it's underrun.
110 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
111 * overruning, instead save the position and continue at that point
112 * when the next invocation.
114 * In-line line breaking:
116 * If TERMP_NOBREAK is specified and the line overruns the right
117 * margin, it will break and pad-right to the right margin after
118 * writing. If maxrmargin is violated, it will break and continue
119 * writing from the right-margin, which will lead to the above scenario
120 * upon exit. Otherwise, the line will break at the right margin.
123 term_flushln(struct termp
*p
)
125 int i
; /* current input position in p->buf */
126 size_t vis
; /* current visual position on output */
127 size_t vbl
; /* number of blanks to prepend to output */
128 size_t vend
; /* end of word visual position on output */
129 size_t bp
; /* visual right border position */
130 size_t dv
; /* temporary for visual pos calculations */
131 int j
; /* temporary loop index for p->buf */
132 int jhy
; /* last hyph before overflow w/r/t j */
133 size_t maxvis
; /* output position of visible boundary */
134 size_t mmax
; /* used in calculating bp */
137 * First, establish the maximum columns of "visible" content.
138 * This is usually the difference between the right-margin and
139 * an indentation, but can be, for tagged lists or columns, a
140 * small set of values.
142 assert (p
->rmargin
>= p
->offset
);
143 dv
= p
->rmargin
- p
->offset
;
144 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
145 dv
= p
->maxrmargin
- p
->offset
;
146 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
148 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
151 * Indent the first line of a paragraph.
153 vbl
= p
->flags
& TERMP_NOLPAD
? (size_t)0 : p
->offset
;
160 * Handle literal tab characters: collapse all
161 * subsequent tabs into a single huge set of spaces.
163 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
164 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
171 * Count up visible word characters. Control sequences
172 * (starting with the CSI) aren't counted. A space
173 * generates a non-printing word, which is valid (the
174 * space is printed according to regular spacing rules).
177 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
178 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
181 /* Back over the the last printed character. */
182 if (8 == p
->buf
[j
]) {
184 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
189 /* Break at the hyphen point if we overrun. */
190 if (vend
> vis
&& vend
< bp
&&
191 ASCII_HYPH
== p
->buf
[j
])
194 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
198 * Find out whether we would exceed the right margin.
199 * If so, break to the next line.
201 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
204 if (TERMP_NOBREAK
& p
->flags
) {
205 p
->viscol
= p
->rmargin
;
206 (*p
->advance
)(p
, p
->rmargin
);
207 vend
+= p
->rmargin
- p
->offset
;
213 /* Remove the p->overstep width. */
215 bp
+= (size_t)p
->overstep
;
219 /* Write out the [remaining] word. */
220 for ( ; i
< p
->col
; i
++) {
221 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
223 if ('\t' == p
->buf
[i
])
225 if (' ' == p
->buf
[i
]) {
227 while (' ' == p
->buf
[i
])
229 dv
= (size_t)(i
- j
) * (*p
->width
)(p
, ' ');
234 if (ASCII_NBRSP
== p
->buf
[i
]) {
235 vbl
+= (*p
->width
)(p
, ' ');
240 * Now we definitely know there will be
241 * printable characters to output,
242 * so write preceding white space now.
245 (*p
->advance
)(p
, vbl
);
250 if (ASCII_HYPH
== p
->buf
[i
]) {
251 (*p
->letter
)(p
, '-');
252 p
->viscol
+= (*p
->width
)(p
, '-');
254 (*p
->letter
)(p
, p
->buf
[i
]);
255 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
262 * If there was trailing white space, it was not printed;
263 * so reset the cursor position accordingly.
270 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
276 if (TERMP_HANG
& p
->flags
) {
277 /* We need one blank after the tag. */
278 p
->overstep
= (int)(vis
- maxvis
+ (*p
->width
)(p
, ' '));
281 * Behave exactly the same way as groff:
282 * If we have overstepped the margin, temporarily move
283 * it to the right and flag the rest of the line to be
285 * If we landed right at the margin, be happy.
286 * If we are one step before the margin, temporarily
287 * move it one step LEFT and flag the rest of the line
290 if (p
->overstep
>= -1) {
291 assert((int)maxvis
+ p
->overstep
>= 0);
292 maxvis
+= (size_t)p
->overstep
;
296 } else if (TERMP_DANGLE
& p
->flags
)
301 ((TERMP_TWOSPACE
& p
->flags
) ? (*p
->width
)(p
, ' ') : 0)) {
302 p
->viscol
+= maxvis
- vis
;
303 (*p
->advance
)(p
, maxvis
- vis
);
304 vis
+= (maxvis
- vis
);
305 } else { /* ...or newline break. */
307 p
->viscol
= p
->rmargin
;
308 (*p
->advance
)(p
, p
->rmargin
);
314 * A newline only breaks an existing line; it won't assert vertical
315 * space. All data in the output buffer is flushed prior to the newline
319 term_newln(struct termp
*p
)
322 p
->flags
|= TERMP_NOSPACE
;
323 if (0 == p
->col
&& 0 == p
->viscol
) {
324 p
->flags
&= ~TERMP_NOLPAD
;
328 p
->flags
&= ~TERMP_NOLPAD
;
333 * Asserts a vertical space (a full, empty line-break between lines).
334 * Note that if used twice, this will cause two blank spaces and so on.
335 * All data in the output buffer is flushed prior to the newline
339 term_vspace(struct termp
*p
)
349 numbered(struct termp
*p
, const char *word
, size_t len
)
353 if ('\0' != (c
= mchars_num2char(word
, len
)))
359 spec(struct termp
*p
, const char *word
, size_t len
)
364 rhs
= mchars_spec2str(p
->symtab
, word
, len
, &sz
);
368 encode(p
, word
, len
);
373 res(struct termp
*p
, const char *word
, size_t len
)
378 rhs
= mchars_res2str(p
->symtab
, word
, len
, &sz
);
385 term_fontlast(struct termp
*p
)
390 p
->fontl
= p
->fontq
[p
->fonti
];
391 p
->fontq
[p
->fonti
] = f
;
396 term_fontrepl(struct termp
*p
, enum termfont f
)
399 p
->fontl
= p
->fontq
[p
->fonti
];
400 p
->fontq
[p
->fonti
] = f
;
405 term_fontpush(struct termp
*p
, enum termfont f
)
408 assert(p
->fonti
+ 1 < 10);
409 p
->fontl
= p
->fontq
[p
->fonti
];
410 p
->fontq
[++p
->fonti
] = f
;
415 term_fontq(struct termp
*p
)
418 return(&p
->fontq
[p
->fonti
]);
423 term_fonttop(struct termp
*p
)
426 return(p
->fontq
[p
->fonti
]);
431 term_fontpopq(struct termp
*p
, const void *key
)
434 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
436 assert(p
->fonti
>= 0);
441 term_fontpop(struct termp
*p
)
450 * Handle pwords, partial words, which may be either a single word or a
451 * phrase that cannot be broken down (such as a literal string). This
452 * handles word styling.
455 term_word(struct termp
*p
, const char *word
)
462 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
463 if ( ! (TERMP_KEEP
& p
->flags
)) {
464 if (TERMP_PREKEEP
& p
->flags
)
465 p
->flags
|= TERMP_KEEP
;
467 if (TERMP_SENTENCE
& p
->flags
)
470 bufferc(p
, ASCII_NBRSP
);
473 if ( ! (p
->flags
& TERMP_NONOSPACE
))
474 p
->flags
&= ~TERMP_NOSPACE
;
476 p
->flags
|= TERMP_NOSPACE
;
478 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_IGNDELIM
);
480 while ('\0' != *word
) {
481 if ((ssz
= strcspn(word
, "\\")) > 0)
482 encode(p
, word
, ssz
);
489 esc
= mandoc_escape(&word
, &seq
, &sz
);
490 if (ESCAPE_ERROR
== esc
)
494 case (ESCAPE_NUMBERED
):
495 numbered(p
, seq
, sz
);
497 case (ESCAPE_PREDEF
):
500 case (ESCAPE_SPECIAL
):
503 case (ESCAPE_FONTBOLD
):
504 term_fontrepl(p
, TERMFONT_BOLD
);
506 case (ESCAPE_FONTITALIC
):
507 term_fontrepl(p
, TERMFONT_UNDER
);
509 case (ESCAPE_FONTROMAN
):
510 term_fontrepl(p
, TERMFONT_NONE
);
512 case (ESCAPE_FONTPREV
):
515 case (ESCAPE_NOSPACE
):
517 p
->flags
|= TERMP_NOSPACE
;
527 adjbuf(struct termp
*p
, int sz
)
532 while (sz
>= p
->maxcols
)
535 p
->buf
= mandoc_realloc
536 (p
->buf
, sizeof(int) * (size_t)p
->maxcols
);
541 bufferc(struct termp
*p
, char c
)
544 if (p
->col
+ 1 >= p
->maxcols
)
545 adjbuf(p
, p
->col
+ 1);
547 p
->buf
[p
->col
++] = c
;
552 encode(struct termp
*p
, const char *word
, size_t sz
)
561 * Encode and buffer a string of characters. If the current
562 * font mode is unset, buffer directly, else encode then buffer
563 * character by character.
566 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
567 if (p
->col
+ len
>= p
->maxcols
)
568 adjbuf(p
, p
->col
+ len
);
569 for (i
= 0; i
< len
; i
++)
570 p
->buf
[p
->col
++] = word
[i
];
574 /* Pre-buffer, assuming worst-case. */
576 if (p
->col
+ 1 + (len
* 3) >= p
->maxcols
)
577 adjbuf(p
, p
->col
+ 1 + (len
* 3));
579 for (i
= 0; i
< len
; i
++) {
580 if ( ! isgraph((unsigned char)word
[i
])) {
581 p
->buf
[p
->col
++] = word
[i
];
585 if (TERMFONT_UNDER
== f
)
586 p
->buf
[p
->col
++] = '_';
588 p
->buf
[p
->col
++] = word
[i
];
590 p
->buf
[p
->col
++] = 8;
591 p
->buf
[p
->col
++] = word
[i
];
597 term_len(const struct termp
*p
, size_t sz
)
600 return((*p
->width
)(p
, ' ') * sz
);
605 term_strlen(const struct termp
*p
, const char *cp
)
610 const char *seq
, *rhs
;
613 * Account for escaped sequences within string length
614 * calculations. This follows the logic in term_word() as we
615 * must calculate the width of produced strings.
623 esc
= mandoc_escape(&cp
, &seq
, &ssz
);
624 if (ESCAPE_ERROR
== esc
)
628 case (ESCAPE_PREDEF
):
630 (p
->symtab
, seq
, ssz
, &rsz
);
632 case (ESCAPE_SPECIAL
):
633 rhs
= mchars_spec2str
634 (p
->symtab
, seq
, ssz
, &rsz
);
650 for (i
= 0; i
< rsz
; i
++)
651 sz
+= (*p
->width
)(p
, *rhs
++);
654 sz
+= (*p
->width
)(p
, ' ');
658 sz
+= (*p
->width
)(p
, '-');
662 sz
+= (*p
->width
)(p
, *cp
++);
672 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
690 r
= su
->scale
/ 1000;
702 return(/* LINTED */(size_t)
708 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
712 v
= ((*p
->hspan
)(p
, su
));
715 return((size_t) /* LINTED */