]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.156 2010/06/30 12:30:36 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #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 buffera(struct termp
*, const char *, size_t);
39 static void bufferc(struct termp
*, char);
40 static void adjbuf(struct termp
*p
, size_t);
41 static void encode(struct termp
*, const char *, size_t);
45 term_free(struct termp
*p
)
51 chars_free(p
->symtab
);
58 term_begin(struct termp
*p
, term_margin head
,
59 term_margin foot
, const void *arg
)
70 term_end(struct termp
*p
)
78 term_alloc(enum termenc enc
)
82 p
= calloc(1, sizeof(struct termp
));
94 * Flush a line of text. A "line" is loosely defined as being something
95 * that should be followed by a newline, regardless of whether it's
96 * broken apart by newlines getting there. A line can also be a
97 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
98 * not have a trailing newline.
100 * The following flags may be specified:
102 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
103 * offset value. This is useful when doing columnar lists where the
104 * prior column has right-padded.
106 * - TERMP_NOBREAK: this is the most important and is used when making
107 * columns. In short: don't print a newline and instead pad to the
108 * right margin. Used in conjunction with TERMP_NOLPAD.
110 * - TERMP_TWOSPACE: when padding, make sure there are at least two
111 * space characters of padding. Otherwise, rather break the line.
113 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
114 * the line is overrun, and don't pad-right if it's underrun.
116 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
117 * overruning, instead save the position and continue at that point
118 * when the next invocation.
120 * In-line line breaking:
122 * If TERMP_NOBREAK is specified and the line overruns the right
123 * margin, it will break and pad-right to the right margin after
124 * writing. If maxrmargin is violated, it will break and continue
125 * writing from the right-margin, which will lead to the above scenario
126 * upon exit. Otherwise, the line will break at the right margin.
129 term_flushln(struct termp
*p
)
131 int i
; /* current input position in p->buf */
132 size_t vis
; /* current visual position on output */
133 size_t vbl
; /* number of blanks to prepend to output */
134 size_t vend
; /* end of word visual position on output */
135 size_t bp
; /* visual right border position */
136 int j
; /* temporary loop index for p->buf */
137 int jhy
; /* last hyph before overflow w/r/t j */
138 size_t maxvis
; /* output position of visible boundary */
139 size_t mmax
; /* used in calculating bp */
142 * First, establish the maximum columns of "visible" content.
143 * This is usually the difference between the right-margin and
144 * an indentation, but can be, for tagged lists or columns, a
145 * small set of values.
148 assert(p
->offset
< p
->rmargin
);
150 maxvis
= (int)(p
->rmargin
- p
->offset
) - p
->overstep
< 0 ?
152 0 : p
->rmargin
- p
->offset
- p
->overstep
;
153 mmax
= (int)(p
->maxrmargin
- p
->offset
) - p
->overstep
< 0 ?
155 0 : p
->maxrmargin
- p
->offset
- p
->overstep
;
157 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
160 * Indent the first line of a paragraph.
162 vbl
= p
->flags
& TERMP_NOLPAD
? 0 : p
->offset
;
166 while (i
< (int)p
->col
) {
168 * Handle literal tab characters: collapse all
169 * subsequent tabs into a single huge set of spaces.
171 for (j
= i
; j
< (int)p
->col
; j
++) {
172 if ('\t' != p
->buf
[j
])
174 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
180 * Count up visible word characters. Control sequences
181 * (starting with the CSI) aren't counted. A space
182 * generates a non-printing word, which is valid (the
183 * space is printed according to regular spacing rules).
187 for (jhy
= 0; j
< (int)p
->col
; j
++) {
188 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
191 /* Back over the the last printed character. */
192 if (8 == p
->buf
[j
]) {
194 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
199 /* Break at the hyphen point if we overrun. */
200 if (vend
> vis
&& vend
< bp
&&
201 ASCII_HYPH
== p
->buf
[j
])
204 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
208 * Find out whether we would exceed the right margin.
209 * If so, break to the next line.
211 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
214 if (TERMP_NOBREAK
& p
->flags
) {
215 p
->viscol
= p
->rmargin
;
216 (*p
->advance
)(p
, p
->rmargin
);
217 vend
+= p
->rmargin
- p
->offset
;
223 /* Remove the p->overstep width. */
225 bp
+= (int)/* LINTED */
231 * Skip leading tabs, they were handled above.
233 while (i
< (int)p
->col
&& '\t' == p
->buf
[i
])
236 /* Write out the [remaining] word. */
237 for ( ; i
< (int)p
->col
; i
++) {
238 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
240 if ('\t' == p
->buf
[i
])
242 if (' ' == p
->buf
[i
]) {
243 while (' ' == p
->buf
[i
]) {
244 vbl
+= (*p
->width
)(p
, p
->buf
[i
]);
249 if (ASCII_NBRSP
== p
->buf
[i
]) {
250 vbl
+= (*p
->width
)(p
, ' ');
255 * Now we definitely know there will be
256 * printable characters to output,
257 * so write preceding white space now.
260 (*p
->advance
)(p
, vbl
);
265 if (ASCII_HYPH
== p
->buf
[i
]) {
266 (*p
->letter
)(p
, '-');
267 p
->viscol
+= (*p
->width
)(p
, '-');
269 (*p
->letter
)(p
, p
->buf
[i
]);
270 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
280 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
286 if (TERMP_HANG
& p
->flags
) {
287 /* We need one blank after the tag. */
288 p
->overstep
= /* LINTED */
289 vis
- maxvis
+ (*p
->width
)(p
, ' ');
292 * Behave exactly the same way as groff:
293 * If we have overstepped the margin, temporarily move
294 * it to the right and flag the rest of the line to be
296 * If we landed right at the margin, be happy.
297 * If we are one step before the margin, temporarily
298 * move it one step LEFT and flag the rest of the line
301 if (p
->overstep
>= -1) {
302 assert((int)maxvis
+ p
->overstep
>= 0);
304 maxvis
+= p
->overstep
;
308 } else if (TERMP_DANGLE
& p
->flags
)
312 if (maxvis
> vis
+ /* LINTED */
313 ((TERMP_TWOSPACE
& p
->flags
) ?
314 (*p
->width
)(p
, ' ') : 0)) {
315 p
->viscol
+= maxvis
- vis
;
316 (*p
->advance
)(p
, maxvis
- vis
);
317 vis
+= (maxvis
- vis
);
318 } else { /* ...or newline break. */
320 p
->viscol
= p
->rmargin
;
321 (*p
->advance
)(p
, p
->rmargin
);
327 * A newline only breaks an existing line; it won't assert vertical
328 * space. All data in the output buffer is flushed prior to the newline
332 term_newln(struct termp
*p
)
335 p
->flags
|= TERMP_NOSPACE
;
336 if (0 == p
->col
&& 0 == p
->viscol
) {
337 p
->flags
&= ~TERMP_NOLPAD
;
341 p
->flags
&= ~TERMP_NOLPAD
;
346 * Asserts a vertical space (a full, empty line-break between lines).
347 * Note that if used twice, this will cause two blank spaces and so on.
348 * All data in the output buffer is flushed prior to the newline
352 term_vspace(struct termp
*p
)
362 spec(struct termp
*p
, const char *word
, size_t len
)
367 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
374 res(struct termp
*p
, const char *word
, size_t len
)
379 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
386 term_fontlast(struct termp
*p
)
391 p
->fontl
= p
->fontq
[p
->fonti
];
392 p
->fontq
[p
->fonti
] = f
;
397 term_fontrepl(struct termp
*p
, enum termfont f
)
400 p
->fontl
= p
->fontq
[p
->fonti
];
401 p
->fontq
[p
->fonti
] = f
;
406 term_fontpush(struct termp
*p
, enum termfont f
)
409 assert(p
->fonti
+ 1 < 10);
410 p
->fontl
= p
->fontq
[p
->fonti
];
411 p
->fontq
[++p
->fonti
] = f
;
416 term_fontq(struct termp
*p
)
419 return(&p
->fontq
[p
->fonti
]);
424 term_fonttop(struct termp
*p
)
427 return(p
->fontq
[p
->fonti
]);
432 term_fontpopq(struct termp
*p
, const void *key
)
435 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
437 assert(p
->fonti
>= 0);
442 term_fontpop(struct termp
*p
)
451 * Handle pwords, partial words, which may be either a single word or a
452 * phrase that cannot be broken down (such as a literal string). This
453 * handles word styling.
456 term_word(struct termp
*p
, const char *word
)
458 const char *sv
, *seq
;
465 if (word
[0] && '\0' == word
[1])
482 if ( ! (TERMP_IGNDELIM
& p
->flags
))
483 p
->flags
|= TERMP_NOSPACE
;
489 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
490 if ( ! (TERMP_KEEP
& p
->flags
)) {
491 if (TERMP_PREKEEP
& p
->flags
)
492 p
->flags
|= TERMP_KEEP
;
494 if (TERMP_SENTENCE
& p
->flags
)
497 bufferc(p
, ASCII_NBRSP
);
500 if ( ! (p
->flags
& TERMP_NONOSPACE
))
501 p
->flags
&= ~TERMP_NOSPACE
;
503 p
->flags
&= ~TERMP_SENTENCE
;
505 /* FIXME: use strcspn. */
515 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
518 case (DECO_RESERVED
):
525 term_fontrepl(p
, TERMFONT_BOLD
);
528 term_fontrepl(p
, TERMFONT_UNDER
);
531 term_fontrepl(p
, TERMFONT_NONE
);
533 case (DECO_PREVIOUS
):
541 if (DECO_NOSPACE
== deco
&& '\0' == *word
)
542 p
->flags
|= TERMP_NOSPACE
;
546 * Note that we don't process the pipe: the parser sees it as
547 * punctuation, but we don't in terms of typography.
549 if (sv
[0] && 0 == sv
[1])
554 p
->flags
|= TERMP_NOSPACE
;
563 adjbuf(struct termp
*p
, size_t sz
)
568 while (sz
>= p
->maxcols
)
571 p
->buf
= realloc(p
->buf
, p
->maxcols
);
572 if (NULL
== p
->buf
) {
580 buffera(struct termp
*p
, const char *word
, size_t sz
)
583 if (p
->col
+ sz
>= p
->maxcols
)
584 adjbuf(p
, p
->col
+ sz
);
586 memcpy(&p
->buf
[(int)p
->col
], word
, sz
);
592 bufferc(struct termp
*p
, char c
)
595 if (p
->col
+ 1 >= p
->maxcols
)
596 adjbuf(p
, p
->col
+ 1);
598 p
->buf
[(int)p
->col
++] = c
;
603 encode(struct termp
*p
, const char *word
, size_t sz
)
609 * Encode and buffer a string of characters. If the current
610 * font mode is unset, buffer directly, else encode then buffer
611 * character by character.
614 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
615 buffera(p
, word
, sz
);
619 for (i
= 0; i
< (int)sz
; i
++) {
620 if ( ! isgraph((u_char
)word
[i
])) {
625 if (TERMFONT_UNDER
== f
)
637 term_len(const struct termp
*p
, size_t sz
)
640 return((*p
->width
)(p
, ' ') * sz
);
645 term_strlen(const struct termp
*p
, const char *cp
)
649 for (sz
= 0; *cp
; cp
++)
650 sz
+= (*p
->width
)(p
, *cp
);
657 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
675 r
= su
->scale
/ 1000;
687 return(/* LINTED */(size_t)
693 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
697 v
= ((*p
->hspan
)(p
, su
));
700 return((size_t) /* LINTED */