]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.125 2009/11/12 05:50:12 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.
31 static struct termp
*term_alloc(enum termenc
);
32 static void term_free(struct termp
*);
33 static void spec(struct termp
*, const char *, size_t);
34 static void res(struct termp
*, const char *, size_t);
35 static void buffera(struct termp
*, const char *, size_t);
36 static void bufferc(struct termp
*, char);
37 static void adjbuf(struct termp
*p
, size_t);
38 static void encode(struct termp
*, const char *, size_t);
45 return(term_alloc(TERMENC_ASCII
));
50 terminal_free(void *arg
)
53 term_free((struct termp
*)arg
);
58 term_free(struct termp
*p
)
64 chars_free(p
->symtab
);
71 term_alloc(enum termenc enc
)
75 p
= calloc(1, sizeof(struct termp
));
87 * Flush a line of text. A "line" is loosely defined as being something
88 * that should be followed by a newline, regardless of whether it's
89 * broken apart by newlines getting there. A line can also be a
90 * fragment of a columnar list.
92 * Specifically, a line is whatever's in p->buf of length p->col, which
93 * is zeroed after this function returns.
95 * The usage of termp:flags is as follows:
97 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
98 * offset value. This is useful when doing columnar lists where the
99 * prior column has right-padded.
101 * - TERMP_NOBREAK: this is the most important and is used when making
102 * columns. In short: don't print a newline and instead pad to the
103 * right margin. Used in conjunction with TERMP_NOLPAD.
105 * - TERMP_TWOSPACE: when padding, make sure there are at least two
106 * space characters of padding. Otherwise, rather break the line.
108 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
109 * the line is overrun, and don't pad-right if it's underrun.
111 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
112 * overruning, instead save the position and continue at that point
113 * when the next invocation.
115 * In-line line breaking:
117 * If TERMP_NOBREAK is specified and the line overruns the right
118 * margin, it will break and pad-right to the right margin after
119 * writing. If maxrmargin is violated, it will break and continue
120 * writing from the right-margin, which will lead to the above scenario
121 * upon exit. Otherwise, the line will break at the right margin.
124 term_flushln(struct termp
*p
)
126 int i
; /* current input position in p->buf */
127 size_t vis
; /* current visual position on output */
128 size_t vbl
; /* number of blanks to prepend to output */
129 size_t vsz
; /* visual characters to write to output */
130 size_t bp
; /* visual right border position */
131 int j
; /* temporary loop index */
133 static int overstep
= 0;
136 * First, establish the maximum columns of "visible" content.
137 * This is usually the difference between the right-margin and
138 * an indentation, but can be, for tagged lists or columns, a
139 * small set of values.
142 assert(p
->offset
< p
->rmargin
);
144 maxvis
= (int)(p
->rmargin
- p
->offset
) - overstep
< 0 ?
146 0 : p
->rmargin
- p
->offset
- overstep
;
147 mmax
= (int)(p
->maxrmargin
- p
->offset
) - overstep
< 0 ?
149 0 : p
->maxrmargin
- p
->offset
- overstep
;
151 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
154 * FIXME: if bp is zero, we still output the first word before
161 * If in the standard case (left-justified), then begin with our
162 * indentation, otherwise (columns, etc.) just start spitting
166 if ( ! (p
->flags
& TERMP_NOLPAD
))
168 for (j
= 0; j
< (int)p
->offset
; j
++)
171 for (i
= 0; i
< (int)p
->col
; i
++) {
173 * Count up visible word characters. Control sequences
174 * (starting with the CSI) aren't counted. A space
175 * generates a non-printing word, which is valid (the
176 * space is printed according to regular spacing rules).
180 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
181 if (j
&& ' ' == p
->buf
[j
])
183 else if (8 == p
->buf
[j
])
190 * Choose the number of blanks to prepend: no blank at the
191 * beginning of a line, one between words -- but do not
192 * actually write them yet.
194 vbl
= (size_t)(0 == vis
? 0 : 1);
197 * Find out whether we would exceed the right margin.
198 * If so, break to the next line. (TODO: hyphenate)
199 * Otherwise, write the chosen number of blanks now.
201 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
203 if (TERMP_NOBREAK
& p
->flags
) {
204 for (j
= 0; j
< (int)p
->rmargin
; j
++)
206 vis
= p
->rmargin
- p
->offset
;
208 for (j
= 0; j
< (int)p
->offset
; j
++)
212 /* Remove the overstep width. */
213 bp
+= (int)/* LINTED */
217 for (j
= 0; j
< (int)vbl
; j
++)
223 * Finally, write out the word.
225 for ( ; i
< (int)p
->col
; i
++) {
226 if (' ' == p
->buf
[i
])
229 /* The unit sep. is a non-breaking space. */
241 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
246 if (TERMP_HANG
& p
->flags
) {
247 /* We need one blank after the tag. */
248 overstep
= /* LINTED */
252 * Behave exactly the same way as groff:
253 * If we have overstepped the margin, temporarily move
254 * it to the right and flag the rest of the line to be
256 * If we landed right at the margin, be happy.
257 * If we are one step before the margin, temporarily
258 * move it one step LEFT and flag the rest of the line
261 if (overstep
>= -1) {
262 assert((int)maxvis
+ overstep
>= 0);
268 } else if (TERMP_DANGLE
& p
->flags
)
272 if (maxvis
> vis
+ /* LINTED */
273 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0))
274 for ( ; vis
< maxvis
; vis
++)
276 else { /* ...or newline break. */
278 for (i
= 0; i
< (int)p
->rmargin
; i
++)
285 * A newline only breaks an existing line; it won't assert vertical
286 * space. All data in the output buffer is flushed prior to the newline
290 term_newln(struct termp
*p
)
293 p
->flags
|= TERMP_NOSPACE
;
295 p
->flags
&= ~TERMP_NOLPAD
;
299 p
->flags
&= ~TERMP_NOLPAD
;
304 * Asserts a vertical space (a full, empty line-break between lines).
305 * Note that if used twice, this will cause two blank spaces and so on.
306 * All data in the output buffer is flushed prior to the newline
310 term_vspace(struct termp
*p
)
319 spec(struct termp
*p
, const char *word
, size_t len
)
324 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
331 res(struct termp
*p
, const char *word
, size_t len
)
336 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
343 term_fontlast(struct termp
*p
)
348 p
->fontl
= p
->fontq
[p
->fonti
];
349 p
->fontq
[p
->fonti
] = f
;
354 term_fontrepl(struct termp
*p
, enum termfont f
)
357 p
->fontl
= p
->fontq
[p
->fonti
];
358 p
->fontq
[p
->fonti
] = f
;
363 term_fontpush(struct termp
*p
, enum termfont f
)
366 assert(p
->fonti
+ 1 < 10);
367 p
->fontl
= p
->fontq
[p
->fonti
];
368 p
->fontq
[++p
->fonti
] = f
;
373 term_fontq(struct termp
*p
)
376 return(&p
->fontq
[p
->fonti
]);
381 term_fonttop(struct termp
*p
)
384 return(p
->fontq
[p
->fonti
]);
389 term_fontpopq(struct termp
*p
, const void *key
)
392 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
394 assert(p
->fonti
>= 0);
399 term_fontpop(struct termp
*p
)
408 * Handle pwords, partial words, which may be either a single word or a
409 * phrase that cannot be broken down (such as a literal string). This
410 * handles word styling.
413 term_word(struct termp
*p
, const char *word
)
415 const char *sv
, *seq
;
422 if (word
[0] && '\0' == word
[1])
441 if ( ! (TERMP_IGNDELIM
& p
->flags
))
442 p
->flags
|= TERMP_NOSPACE
;
448 if ( ! (TERMP_NOSPACE
& p
->flags
))
451 if ( ! (p
->flags
& TERMP_NONOSPACE
))
452 p
->flags
&= ~TERMP_NOSPACE
;
454 /* FIXME: use strcspn. */
464 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
467 case (DECO_RESERVED
):
474 term_fontrepl(p
, TERMFONT_BOLD
);
477 term_fontrepl(p
, TERMFONT_UNDER
);
480 term_fontrepl(p
, TERMFONT_NONE
);
482 case (DECO_PREVIOUS
):
491 if (sv
[0] && 0 == sv
[1])
498 p
->flags
|= TERMP_NOSPACE
;
507 adjbuf(struct termp
*p
, size_t sz
)
512 while (sz
>= p
->maxcols
)
515 p
->buf
= realloc(p
->buf
, p
->maxcols
);
516 if (NULL
== p
->buf
) {
524 buffera(struct termp
*p
, const char *word
, size_t sz
)
527 if (p
->col
+ sz
>= p
->maxcols
)
528 adjbuf(p
, p
->col
+ sz
);
530 memcpy(&p
->buf
[p
->col
], word
, sz
);
536 bufferc(struct termp
*p
, char c
)
539 if (p
->col
+ 1 >= p
->maxcols
)
540 adjbuf(p
, p
->col
+ 1);
542 p
->buf
[p
->col
++] = c
;
547 encode(struct termp
*p
, const char *word
, size_t sz
)
553 * Encode and buffer a string of characters. If the current
554 * font mode is unset, buffer directly, else encode then buffer
555 * character by character.
558 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
559 buffera(p
, word
, sz
);
563 for (i
= 0; i
< (int)sz
; i
++) {
564 if ( ! isgraph((u_char
)word
[i
])) {
569 if (TERMFONT_UNDER
== f
)
581 term_vspan(const struct roffsu
*su
)
599 r
= su
->scale
/ 1000;
611 return(/* LINTED */(size_t)
617 term_hspan(const struct roffsu
*su
)
621 /* XXX: CM, IN, and PT are approximations. */
628 /* XXX: this is an approximation. */
632 r
= (10 * su
->scale
) / 6;
635 r
= (10 * su
->scale
) / 72;
638 r
= su
->scale
/ 1000; /* FIXME: double-check. */
641 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
650 return((size_t)/* LINTED */