]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.130 2010/04/03 12:46:35 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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>
37 static struct termp
*term_alloc(enum termenc
);
38 static void term_free(struct termp
*);
39 static void spec(struct termp
*, const char *, size_t);
40 static void res(struct termp
*, const char *, size_t);
41 static void buffera(struct termp
*, const char *, size_t);
42 static void bufferc(struct termp
*, char);
43 static void adjbuf(struct termp
*p
, size_t);
44 static void encode(struct termp
*, const char *, size_t);
51 return(term_alloc(TERMENC_ASCII
));
56 terminal_free(void *arg
)
59 term_free((struct termp
*)arg
);
64 term_free(struct termp
*p
)
70 chars_free(p
->symtab
);
77 term_alloc(enum termenc enc
)
81 p
= calloc(1, sizeof(struct termp
));
92 * Flush a line of text. A "line" is loosely defined as being something
93 * that should be followed by a newline, regardless of whether it's
94 * broken apart by newlines getting there. A line can also be a
95 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
96 * not have a trailing newline.
98 * The following flags may be specified:
100 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
101 * offset value. This is useful when doing columnar lists where the
102 * prior column has right-padded.
104 * - TERMP_NOBREAK: this is the most important and is used when making
105 * columns. In short: don't print a newline and instead pad to the
106 * right margin. Used in conjunction with TERMP_NOLPAD.
108 * - TERMP_TWOSPACE: when padding, make sure there are at least two
109 * space characters of padding. Otherwise, rather break the line.
111 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
112 * the line is overrun, and don't pad-right if it's underrun.
114 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
115 * overruning, instead save the position and continue at that point
116 * when the next invocation.
118 * In-line line breaking:
120 * If TERMP_NOBREAK is specified and the line overruns the right
121 * margin, it will break and pad-right to the right margin after
122 * writing. If maxrmargin is violated, it will break and continue
123 * writing from the right-margin, which will lead to the above scenario
124 * upon exit. Otherwise, the line will break at the right margin.
127 term_flushln(struct termp
*p
)
129 int i
; /* current input position in p->buf */
130 size_t vis
; /* current visual position on output */
131 size_t vbl
; /* number of blanks to prepend to output */
132 size_t vsz
; /* visual characters to write to output */
133 size_t bp
; /* visual right border position */
134 size_t hyph
; /* visible position of hyphen */
135 int j
; /* temporary loop index */
139 * First, establish the maximum columns of "visible" content.
140 * This is usually the difference between the right-margin and
141 * an indentation, but can be, for tagged lists or columns, a
142 * small set of values.
145 assert(p
->offset
< p
->rmargin
);
147 maxvis
= (int)(p
->rmargin
- p
->offset
) - p
->overstep
< 0 ?
149 0 : p
->rmargin
- p
->offset
- p
->overstep
;
150 mmax
= (int)(p
->maxrmargin
- p
->offset
) - p
->overstep
< 0 ?
152 0 : p
->maxrmargin
- p
->offset
- p
->overstep
;
154 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
157 * FIXME: if bp is zero, we still output the first word before
164 * If in the standard case (left-justified), then begin with our
165 * indentation, otherwise (columns, etc.) just start spitting
169 if ( ! (p
->flags
& TERMP_NOLPAD
))
171 for (j
= 0; j
< (int)p
->offset
; j
++)
174 for (i
= 0; i
< (int)p
->col
; i
++) {
176 * Count up visible word characters. Control sequences
177 * (starting with the CSI) aren't counted. A space
178 * generates a non-printing word, which is valid (the
179 * space is printed according to regular spacing rules).
180 * Collect the number of printable characters until the
181 * first hyphen, if found. Hyphens aren't included if
182 * they're the first character (so `Fl' doesn't break)
183 * or second consecutive character (`Fl -').
187 for (j
= i
, vsz
= 0, hyph
= 0; j
< (int)p
->col
; j
++) {
188 if (j
&& ' ' == p
->buf
[j
])
194 if (j
> i
&& '-' == p
->buf
[j
] && 0 == hyph
)
195 if ('-' != p
->buf
[j
- 1])
200 * Choose the number of blanks to prepend: no blank at the
201 * beginning of a line, one between words -- but do not
202 * actually write them yet.
205 vbl
= (size_t)(0 == vis
? 0 : 1);
208 * Find out whether we would exceed the right margin.
209 * If so, break to the next line, possibly after
210 * emittign character up to a hyphen. Otherwise, write
211 * the chosen number of blanks.
214 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
216 * Has a hyphen been found before the breakpoint
219 if (hyph
&& vis
+ vbl
+ hyph
<= bp
) {
220 /* First prepend blanks. */
221 for (j
= 0; j
< (int)vbl
; j
++)
224 /* Emit up to the character. */
232 } while ('-' != p
->buf
[i
++]);
234 /* Emit trailing decoration. */
235 if (8 == p
->buf
[i
]) {
237 putchar(p
->buf
[i
+ 1]);
242 if (TERMP_NOBREAK
& p
->flags
) {
243 for (j
= 0; j
< (int)p
->rmargin
; j
++)
245 vis
= p
->rmargin
- p
->offset
;
247 for (j
= 0; j
< (int)p
->offset
; j
++)
252 /* Remove the p->overstep width. */
254 bp
+= (int)/* LINTED */
258 for (j
= 0; j
< (int)vbl
; j
++)
263 /* Write out the [remaining] word. */
264 for ( ; i
< (int)p
->col
; i
++)
265 if (' ' == p
->buf
[i
])
267 else if (31 == p
->buf
[i
])
278 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
283 if (TERMP_HANG
& p
->flags
) {
284 /* We need one blank after the tag. */
285 p
->overstep
= /* LINTED */
289 * Behave exactly the same way as groff:
290 * If we have overstepped the margin, temporarily move
291 * it to the right and flag the rest of the line to be
293 * If we landed right at the margin, be happy.
294 * If we are one step before the margin, temporarily
295 * move it one step LEFT and flag the rest of the line
298 if (p
->overstep
>= -1) {
299 assert((int)maxvis
+ p
->overstep
>= 0);
301 maxvis
+= p
->overstep
;
305 } else if (TERMP_DANGLE
& p
->flags
)
309 if (maxvis
> vis
+ /* LINTED */
310 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0))
311 for ( ; vis
< maxvis
; vis
++)
313 else { /* ...or newline break. */
315 for (i
= 0; i
< (int)p
->rmargin
; i
++)
322 * A newline only breaks an existing line; it won't assert vertical
323 * space. All data in the output buffer is flushed prior to the newline
327 term_newln(struct termp
*p
)
330 p
->flags
|= TERMP_NOSPACE
;
332 p
->flags
&= ~TERMP_NOLPAD
;
336 p
->flags
&= ~TERMP_NOLPAD
;
341 * Asserts a vertical space (a full, empty line-break between lines).
342 * Note that if used twice, this will cause two blank spaces and so on.
343 * All data in the output buffer is flushed prior to the newline
347 term_vspace(struct termp
*p
)
356 spec(struct termp
*p
, const char *word
, size_t len
)
361 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
368 res(struct termp
*p
, const char *word
, size_t len
)
373 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
380 term_fontlast(struct termp
*p
)
385 p
->fontl
= p
->fontq
[p
->fonti
];
386 p
->fontq
[p
->fonti
] = f
;
391 term_fontrepl(struct termp
*p
, enum termfont f
)
394 p
->fontl
= p
->fontq
[p
->fonti
];
395 p
->fontq
[p
->fonti
] = f
;
400 term_fontpush(struct termp
*p
, enum termfont f
)
403 assert(p
->fonti
+ 1 < 10);
404 p
->fontl
= p
->fontq
[p
->fonti
];
405 p
->fontq
[++p
->fonti
] = f
;
410 term_fontq(struct termp
*p
)
413 return(&p
->fontq
[p
->fonti
]);
418 term_fonttop(struct termp
*p
)
421 return(p
->fontq
[p
->fonti
]);
426 term_fontpopq(struct termp
*p
, const void *key
)
429 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
431 assert(p
->fonti
>= 0);
436 term_fontpop(struct termp
*p
)
445 * Handle pwords, partial words, which may be either a single word or a
446 * phrase that cannot be broken down (such as a literal string). This
447 * handles word styling.
450 term_word(struct termp
*p
, const char *word
)
452 const char *sv
, *seq
;
459 if (word
[0] && '\0' == word
[1])
476 if ( ! (TERMP_IGNDELIM
& p
->flags
))
477 p
->flags
|= TERMP_NOSPACE
;
483 if ( ! (TERMP_NOSPACE
& p
->flags
))
486 if ( ! (p
->flags
& TERMP_NONOSPACE
))
487 p
->flags
&= ~TERMP_NOSPACE
;
489 /* FIXME: use strcspn. */
499 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
502 case (DECO_RESERVED
):
509 term_fontrepl(p
, TERMFONT_BOLD
);
512 term_fontrepl(p
, TERMFONT_UNDER
);
515 term_fontrepl(p
, TERMFONT_NONE
);
517 case (DECO_PREVIOUS
):
525 if (DECO_NOSPACE
== deco
&& '\0' == *word
)
526 p
->flags
|= TERMP_NOSPACE
;
529 if (sv
[0] && 0 == sv
[1])
536 p
->flags
|= TERMP_NOSPACE
;
545 adjbuf(struct termp
*p
, size_t sz
)
550 while (sz
>= p
->maxcols
)
553 p
->buf
= realloc(p
->buf
, p
->maxcols
);
554 if (NULL
== p
->buf
) {
562 buffera(struct termp
*p
, const char *word
, size_t sz
)
565 if (p
->col
+ sz
>= p
->maxcols
)
566 adjbuf(p
, p
->col
+ sz
);
568 memcpy(&p
->buf
[(int)p
->col
], word
, sz
);
574 bufferc(struct termp
*p
, char c
)
577 if (p
->col
+ 1 >= p
->maxcols
)
578 adjbuf(p
, p
->col
+ 1);
580 p
->buf
[(int)p
->col
++] = c
;
585 encode(struct termp
*p
, const char *word
, size_t sz
)
591 * Encode and buffer a string of characters. If the current
592 * font mode is unset, buffer directly, else encode then buffer
593 * character by character.
596 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
597 buffera(p
, word
, sz
);
601 for (i
= 0; i
< (int)sz
; i
++) {
602 if ( ! isgraph((u_char
)word
[i
])) {
607 if (TERMFONT_UNDER
== f
)
619 term_vspan(const struct roffsu
*su
)
637 r
= su
->scale
/ 1000;
649 return(/* LINTED */(size_t)
655 term_hspan(const struct roffsu
*su
)
659 /* XXX: CM, IN, and PT are approximations. */
666 /* XXX: this is an approximation. */
670 r
= (10 * su
->scale
) / 6;
673 r
= (10 * su
->scale
) / 72;
676 r
= su
->scale
/ 1000; /* FIXME: double-check. */
679 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
688 return((size_t)/* LINTED */