]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.161 2010/07/16 22:33:30 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 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>
37 static void spec(struct termp
*, const char *, size_t);
38 static void res(struct termp
*, const char *, size_t);
39 static void buffera(struct termp
*, const char *, size_t);
40 static void bufferc(struct termp
*, char);
41 static void adjbuf(struct termp
*p
, size_t);
42 static void encode(struct termp
*, const char *, size_t);
46 term_free(struct termp
*p
)
52 chars_free(p
->symtab
);
59 term_begin(struct termp
*p
, term_margin head
,
60 term_margin foot
, const void *arg
)
71 term_end(struct termp
*p
)
79 term_alloc(enum termenc enc
)
83 p
= calloc(1, sizeof(struct termp
));
95 * Flush a line of text. A "line" is loosely defined as being something
96 * that should be followed by a newline, regardless of whether it's
97 * broken apart by newlines getting there. A line can also be a
98 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
99 * not have a trailing newline.
101 * The following flags may be specified:
103 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
104 * offset value. This is useful when doing columnar lists where the
105 * prior column has right-padded.
107 * - TERMP_NOBREAK: this is the most important and is used when making
108 * columns. In short: don't print a newline and instead pad to the
109 * right margin. Used in conjunction with TERMP_NOLPAD.
111 * - TERMP_TWOSPACE: when padding, make sure there are at least two
112 * space characters of padding. Otherwise, rather break the line.
114 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
115 * the line is overrun, and don't pad-right if it's underrun.
117 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
118 * overruning, instead save the position and continue at that point
119 * when the next invocation.
121 * In-line line breaking:
123 * If TERMP_NOBREAK is specified and the line overruns the right
124 * margin, it will break and pad-right to the right margin after
125 * writing. If maxrmargin is violated, it will break and continue
126 * writing from the right-margin, which will lead to the above scenario
127 * upon exit. Otherwise, the line will break at the right margin.
130 term_flushln(struct termp
*p
)
132 int i
; /* current input position in p->buf */
133 size_t vis
; /* current visual position on output */
134 size_t vbl
; /* number of blanks to prepend to output */
135 size_t vend
; /* end of word visual position on output */
136 size_t bp
; /* visual right border position */
137 int j
; /* temporary loop index for p->buf */
138 int jhy
; /* last hyph before overflow w/r/t j */
139 size_t maxvis
; /* output position of visible boundary */
140 size_t mmax
; /* used in calculating bp */
143 * First, establish the maximum columns of "visible" content.
144 * This is usually the difference between the right-margin and
145 * an indentation, but can be, for tagged lists or columns, a
146 * small set of values.
149 assert(p
->offset
< p
->rmargin
);
151 maxvis
= (int)(p
->rmargin
- p
->offset
) - p
->overstep
< 0 ?
153 0 : p
->rmargin
- p
->offset
- p
->overstep
;
154 mmax
= (int)(p
->maxrmargin
- p
->offset
) - p
->overstep
< 0 ?
156 0 : p
->maxrmargin
- p
->offset
- p
->overstep
;
158 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
161 * Indent the first line of a paragraph.
163 vbl
= p
->flags
& TERMP_NOLPAD
? 0 : p
->offset
;
167 while (i
< (int)p
->col
) {
169 * Handle literal tab characters: collapse all
170 * subsequent tabs into a single huge set of spaces.
172 for (j
= i
; j
< (int)p
->col
; j
++) {
173 if ('\t' != p
->buf
[j
])
175 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
181 * Count up visible word characters. Control sequences
182 * (starting with the CSI) aren't counted. A space
183 * generates a non-printing word, which is valid (the
184 * space is printed according to regular spacing rules).
188 for (jhy
= 0; j
< (int)p
->col
; j
++) {
189 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
192 /* Back over the the last printed character. */
193 if (8 == p
->buf
[j
]) {
195 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
200 /* Break at the hyphen point if we overrun. */
201 if (vend
> vis
&& vend
< bp
&&
202 ASCII_HYPH
== p
->buf
[j
])
205 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
209 * Find out whether we would exceed the right margin.
210 * If so, break to the next line.
212 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
215 if (TERMP_NOBREAK
& p
->flags
) {
216 p
->viscol
= p
->rmargin
;
217 (*p
->advance
)(p
, p
->rmargin
);
218 vend
+= p
->rmargin
- p
->offset
;
224 /* Remove the p->overstep width. */
226 bp
+= (int)/* LINTED */
232 * Skip leading tabs, they were handled above.
234 while (i
< (int)p
->col
&& '\t' == p
->buf
[i
])
237 /* Write out the [remaining] word. */
238 for ( ; i
< (int)p
->col
; i
++) {
239 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
241 if ('\t' == p
->buf
[i
])
243 if (' ' == p
->buf
[i
]) {
244 while (' ' == p
->buf
[i
]) {
245 vbl
+= (*p
->width
)(p
, p
->buf
[i
]);
250 if (ASCII_NBRSP
== p
->buf
[i
]) {
251 vbl
+= (*p
->width
)(p
, ' ');
256 * Now we definitely know there will be
257 * printable characters to output,
258 * so write preceding white space now.
261 (*p
->advance
)(p
, vbl
);
266 if (ASCII_HYPH
== p
->buf
[i
]) {
267 (*p
->letter
)(p
, '-');
268 p
->viscol
+= (*p
->width
)(p
, '-');
270 (*p
->letter
)(p
, p
->buf
[i
]);
271 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
281 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
287 if (TERMP_HANG
& p
->flags
) {
288 /* We need one blank after the tag. */
289 p
->overstep
= /* LINTED */
290 vis
- maxvis
+ (*p
->width
)(p
, ' ');
293 * Behave exactly the same way as groff:
294 * If we have overstepped the margin, temporarily move
295 * it to the right and flag the rest of the line to be
297 * If we landed right at the margin, be happy.
298 * If we are one step before the margin, temporarily
299 * move it one step LEFT and flag the rest of the line
302 if (p
->overstep
>= -1) {
303 assert((int)maxvis
+ p
->overstep
>= 0);
305 maxvis
+= p
->overstep
;
309 } else if (TERMP_DANGLE
& p
->flags
)
313 if (maxvis
> vis
+ /* LINTED */
314 ((TERMP_TWOSPACE
& p
->flags
) ?
315 (*p
->width
)(p
, ' ') : 0)) {
316 p
->viscol
+= maxvis
- vis
;
317 (*p
->advance
)(p
, maxvis
- vis
);
318 vis
+= (maxvis
- vis
);
319 } else { /* ...or newline break. */
321 p
->viscol
= p
->rmargin
;
322 (*p
->advance
)(p
, p
->rmargin
);
328 * A newline only breaks an existing line; it won't assert vertical
329 * space. All data in the output buffer is flushed prior to the newline
333 term_newln(struct termp
*p
)
336 p
->flags
|= TERMP_NOSPACE
;
337 if (0 == p
->col
&& 0 == p
->viscol
) {
338 p
->flags
&= ~TERMP_NOLPAD
;
342 p
->flags
&= ~TERMP_NOLPAD
;
347 * Asserts a vertical space (a full, empty line-break between lines).
348 * Note that if used twice, this will cause two blank spaces and so on.
349 * All data in the output buffer is flushed prior to the newline
353 term_vspace(struct termp
*p
)
363 spec(struct termp
*p
, const char *word
, size_t len
)
368 rhs
= chars_spec2str(p
->symtab
, word
, len
, &sz
);
375 res(struct termp
*p
, const char *word
, size_t len
)
380 rhs
= chars_res2str(p
->symtab
, word
, len
, &sz
);
387 term_fontlast(struct termp
*p
)
392 p
->fontl
= p
->fontq
[p
->fonti
];
393 p
->fontq
[p
->fonti
] = f
;
398 term_fontrepl(struct termp
*p
, enum termfont f
)
401 p
->fontl
= p
->fontq
[p
->fonti
];
402 p
->fontq
[p
->fonti
] = f
;
407 term_fontpush(struct termp
*p
, enum termfont f
)
410 assert(p
->fonti
+ 1 < 10);
411 p
->fontl
= p
->fontq
[p
->fonti
];
412 p
->fontq
[++p
->fonti
] = f
;
417 term_fontq(struct termp
*p
)
420 return(&p
->fontq
[p
->fonti
]);
425 term_fonttop(struct termp
*p
)
428 return(p
->fontq
[p
->fonti
]);
433 term_fontpopq(struct termp
*p
, const void *key
)
436 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
438 assert(p
->fonti
>= 0);
443 term_fontpop(struct termp
*p
)
452 * Handle pwords, partial words, which may be either a single word or a
453 * phrase that cannot be broken down (such as a literal string). This
454 * handles word styling.
457 term_word(struct termp
*p
, const char *word
)
459 const char *sv
, *seq
;
466 if (word
[0] && '\0' == word
[1])
483 if ( ! (TERMP_IGNDELIM
& p
->flags
))
484 p
->flags
|= TERMP_NOSPACE
;
490 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
491 if ( ! (TERMP_KEEP
& p
->flags
)) {
492 if (TERMP_PREKEEP
& p
->flags
)
493 p
->flags
|= TERMP_KEEP
;
495 if (TERMP_SENTENCE
& p
->flags
)
498 bufferc(p
, ASCII_NBRSP
);
501 if ( ! (p
->flags
& TERMP_NONOSPACE
))
502 p
->flags
&= ~TERMP_NOSPACE
;
504 p
->flags
&= ~TERMP_SENTENCE
;
506 /* FIXME: use strcspn. */
516 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
519 case (DECO_RESERVED
):
526 term_fontrepl(p
, TERMFONT_BOLD
);
529 term_fontrepl(p
, TERMFONT_UNDER
);
532 term_fontrepl(p
, TERMFONT_NONE
);
534 case (DECO_PREVIOUS
):
542 if (DECO_NOSPACE
== deco
&& '\0' == *word
)
543 p
->flags
|= TERMP_NOSPACE
;
547 * Note that we don't process the pipe: the parser sees it as
548 * punctuation, but we don't in terms of typography.
550 if (sv
[0] && 0 == sv
[1])
555 p
->flags
|= TERMP_NOSPACE
;
564 adjbuf(struct termp
*p
, size_t sz
)
569 while (sz
>= p
->maxcols
)
572 p
->buf
= realloc(p
->buf
, p
->maxcols
);
573 if (NULL
== p
->buf
) {
581 buffera(struct termp
*p
, const char *word
, size_t sz
)
584 if (p
->col
+ sz
>= p
->maxcols
)
585 adjbuf(p
, p
->col
+ sz
);
587 memcpy(&p
->buf
[(int)p
->col
], word
, sz
);
593 bufferc(struct termp
*p
, char c
)
596 if (p
->col
+ 1 >= p
->maxcols
)
597 adjbuf(p
, p
->col
+ 1);
599 p
->buf
[(int)p
->col
++] = c
;
604 encode(struct termp
*p
, const char *word
, size_t sz
)
610 * Encode and buffer a string of characters. If the current
611 * font mode is unset, buffer directly, else encode then buffer
612 * character by character.
615 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
616 buffera(p
, word
, sz
);
620 for (i
= 0; i
< (int)sz
; i
++) {
621 if ( ! isgraph((u_char
)word
[i
])) {
626 if (TERMFONT_UNDER
== f
)
638 term_len(const struct termp
*p
, size_t sz
)
641 return((*p
->width
)(p
, ' ') * sz
);
646 term_strlen(const struct termp
*p
, const char *cp
)
650 for (sz
= 0; *cp
; cp
++)
651 sz
+= (*p
->width
)(p
, *cp
);
659 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
677 r
= su
->scale
/ 1000;
689 return(/* LINTED */(size_t)
695 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
699 v
= ((*p
->hspan
)(p
, su
));
702 return((size_t) /* LINTED */