]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.134 2010/05/15 16:18:23 joerg 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
, size_t);
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);
48 ascii_alloc(size_t width
)
51 return(term_alloc(TERMENC_ASCII
, width
));
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
, size_t width
)
81 p
= calloc(1, sizeof(struct termp
));
87 /* Enforce some lower boundary. */
90 p
->defrmargin
= width
- 2;
96 * Flush a line of text. A "line" is loosely defined as being something
97 * that should be followed by a newline, regardless of whether it's
98 * broken apart by newlines getting there. A line can also be a
99 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
100 * not have a trailing newline.
102 * The following flags may be specified:
104 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
105 * offset value. This is useful when doing columnar lists where the
106 * prior column has right-padded.
108 * - TERMP_NOBREAK: this is the most important and is used when making
109 * columns. In short: don't print a newline and instead pad to the
110 * right margin. Used in conjunction with TERMP_NOLPAD.
112 * - TERMP_TWOSPACE: when padding, make sure there are at least two
113 * space characters of padding. Otherwise, rather break the line.
115 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
116 * the line is overrun, and don't pad-right if it's underrun.
118 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
119 * overruning, instead save the position and continue at that point
120 * when the next invocation.
122 * In-line line breaking:
124 * If TERMP_NOBREAK is specified and the line overruns the right
125 * margin, it will break and pad-right to the right margin after
126 * writing. If maxrmargin is violated, it will break and continue
127 * writing from the right-margin, which will lead to the above scenario
128 * upon exit. Otherwise, the line will break at the right margin.
131 term_flushln(struct termp
*p
)
133 int i
; /* current input position in p->buf */
134 size_t vis
; /* current visual position on output */
135 size_t vbl
; /* number of blanks to prepend to output */
136 size_t vsz
; /* visual characters to write to output */
137 size_t bp
; /* visual right border position */
138 int j
; /* temporary loop index */
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 * FIXME: if bp is zero, we still output the first word before
167 * If in the standard case (left-justified), then begin with our
168 * indentation, otherwise (columns, etc.) just start spitting
172 if ( ! (p
->flags
& TERMP_NOLPAD
))
174 for (j
= 0; j
< (int)p
->offset
; j
++)
177 for (i
= 0; i
< (int)p
->col
; i
++) {
179 * Count up visible word characters. Control sequences
180 * (starting with the CSI) aren't counted. A space
181 * generates a non-printing word, which is valid (the
182 * space is printed according to regular spacing rules).
186 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
187 if (j
&& ' ' == p
->buf
[j
])
196 * Choose the number of blanks to prepend: no blank at the
197 * beginning of a line, one between words -- but do not
198 * actually write them yet.
201 vbl
= (size_t)(0 == vis
? 0 : 1);
204 * Find out whether we would exceed the right margin.
205 * If so, break to the next line. Otherwise, write the chosen
209 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
211 if (TERMP_NOBREAK
& p
->flags
) {
212 for (j
= 0; j
< (int)p
->rmargin
; j
++)
214 vis
= p
->rmargin
- p
->offset
;
216 for (j
= 0; j
< (int)p
->offset
; j
++)
221 /* Remove the p->overstep width. */
223 bp
+= (int)/* LINTED */
227 for (j
= 0; j
< (int)vbl
; j
++)
232 /* Write out the [remaining] word. */
233 for ( ; i
< (int)p
->col
; i
++)
234 if (' ' == p
->buf
[i
])
236 else if (31 == p
->buf
[i
])
247 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
252 if (TERMP_HANG
& p
->flags
) {
253 /* We need one blank after the tag. */
254 p
->overstep
= /* LINTED */
258 * Behave exactly the same way as groff:
259 * If we have overstepped the margin, temporarily move
260 * it to the right and flag the rest of the line to be
262 * If we landed right at the margin, be happy.
263 * If we are one step before the margin, temporarily
264 * move it one step LEFT and flag the rest of the line
267 if (p
->overstep
>= -1) {
268 assert((int)maxvis
+ p
->overstep
>= 0);
270 maxvis
+= p
->overstep
;
274 } else if (TERMP_DANGLE
& p
->flags
)
278 if (maxvis
> vis
+ /* LINTED */
279 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0))
280 for ( ; vis
< maxvis
; vis
++)
282 else { /* ...or newline break. */
284 for (i
= 0; i
< (int)p
->rmargin
; i
++)
291 * A newline only breaks an existing line; it won't assert vertical
292 * space. All data in the output buffer is flushed prior to the newline
296 term_newln(struct termp
*p
)
299 p
->flags
|= TERMP_NOSPACE
;
301 p
->flags
&= ~TERMP_NOLPAD
;
305 p
->flags
&= ~TERMP_NOLPAD
;
310 * Asserts a vertical space (a full, empty line-break between lines).
311 * Note that if used twice, this will cause two blank spaces and so on.
312 * All data in the output buffer is flushed prior to the newline
316 term_vspace(struct termp
*p
)
325 spec(struct termp
*p
, const char *word
, size_t len
)
330 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
337 res(struct termp
*p
, const char *word
, size_t len
)
342 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
349 term_fontlast(struct termp
*p
)
354 p
->fontl
= p
->fontq
[p
->fonti
];
355 p
->fontq
[p
->fonti
] = f
;
360 term_fontrepl(struct termp
*p
, enum termfont f
)
363 p
->fontl
= p
->fontq
[p
->fonti
];
364 p
->fontq
[p
->fonti
] = f
;
369 term_fontpush(struct termp
*p
, enum termfont f
)
372 assert(p
->fonti
+ 1 < 10);
373 p
->fontl
= p
->fontq
[p
->fonti
];
374 p
->fontq
[++p
->fonti
] = f
;
379 term_fontq(struct termp
*p
)
382 return(&p
->fontq
[p
->fonti
]);
387 term_fonttop(struct termp
*p
)
390 return(p
->fontq
[p
->fonti
]);
395 term_fontpopq(struct termp
*p
, const void *key
)
398 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
400 assert(p
->fonti
>= 0);
405 term_fontpop(struct termp
*p
)
414 * Handle pwords, partial words, which may be either a single word or a
415 * phrase that cannot be broken down (such as a literal string). This
416 * handles word styling.
419 term_word(struct termp
*p
, const char *word
)
421 const char *sv
, *seq
;
428 if (word
[0] && '\0' == word
[1])
445 if ( ! (TERMP_IGNDELIM
& p
->flags
))
446 p
->flags
|= TERMP_NOSPACE
;
452 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
454 if (TERMP_SENTENCE
& p
->flags
)
458 if ( ! (p
->flags
& TERMP_NONOSPACE
))
459 p
->flags
&= ~TERMP_NOSPACE
;
461 p
->flags
&= ~TERMP_SENTENCE
;
463 /* FIXME: use strcspn. */
473 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
476 case (DECO_RESERVED
):
483 term_fontrepl(p
, TERMFONT_BOLD
);
486 term_fontrepl(p
, TERMFONT_UNDER
);
489 term_fontrepl(p
, TERMFONT_NONE
);
491 case (DECO_PREVIOUS
):
499 if (DECO_NOSPACE
== deco
&& '\0' == *word
)
500 p
->flags
|= TERMP_NOSPACE
;
504 * Note that we don't process the pipe: the parser sees it as
505 * punctuation, but we don't in terms of typography.
507 if (sv
[0] && 0 == sv
[1])
512 p
->flags
|= TERMP_NOSPACE
;
521 adjbuf(struct termp
*p
, size_t sz
)
526 while (sz
>= p
->maxcols
)
529 p
->buf
= realloc(p
->buf
, p
->maxcols
);
530 if (NULL
== p
->buf
) {
538 buffera(struct termp
*p
, const char *word
, size_t sz
)
541 if (p
->col
+ sz
>= p
->maxcols
)
542 adjbuf(p
, p
->col
+ sz
);
544 memcpy(&p
->buf
[(int)p
->col
], word
, sz
);
550 bufferc(struct termp
*p
, char c
)
553 if (p
->col
+ 1 >= p
->maxcols
)
554 adjbuf(p
, p
->col
+ 1);
556 p
->buf
[(int)p
->col
++] = c
;
561 encode(struct termp
*p
, const char *word
, size_t sz
)
567 * Encode and buffer a string of characters. If the current
568 * font mode is unset, buffer directly, else encode then buffer
569 * character by character.
572 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
573 buffera(p
, word
, sz
);
577 for (i
= 0; i
< (int)sz
; i
++) {
578 if ( ! isgraph((u_char
)word
[i
])) {
583 if (TERMFONT_UNDER
== f
)
595 term_vspan(const struct roffsu
*su
)
613 r
= su
->scale
/ 1000;
625 return(/* LINTED */(size_t)
631 term_hspan(const struct roffsu
*su
)
635 /* XXX: CM, IN, and PT are approximations. */
642 /* XXX: this is an approximation. */
646 r
= (10 * su
->scale
) / 6;
649 r
= (10 * su
->scale
) / 72;
652 r
= su
->scale
/ 1000; /* FIXME: double-check. */
655 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
664 return((size_t)/* LINTED */