]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.150 2010/06/26 15:36:37 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 */
137 int jhy
; /* last hyphen before line overflow */
141 * First, establish the maximum columns of "visible" content.
142 * This is usually the difference between the right-margin and
143 * an indentation, but can be, for tagged lists or columns, a
144 * small set of values.
147 assert(p
->offset
< p
->rmargin
);
149 maxvis
= (int)(p
->rmargin
- p
->offset
) - p
->overstep
< 0 ?
151 0 : p
->rmargin
- p
->offset
- p
->overstep
;
152 mmax
= (int)(p
->maxrmargin
- p
->offset
) - p
->overstep
< 0 ?
154 0 : p
->maxrmargin
- p
->offset
- p
->overstep
;
156 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
159 * Indent the first line of a paragraph.
161 vbl
= p
->flags
& TERMP_NOLPAD
? 0 : p
->offset
;
164 * FIXME: if bp is zero, we still output the first word before
169 while (i
< (int)p
->col
) {
172 * Handle literal tab characters.
174 for (j
= i
; j
< (int)p
->col
; j
++) {
175 if ('\t' != p
->buf
[j
])
177 vend
= (vis
/p
->tabwidth
+1)*p
->tabwidth
;
183 * Count up visible word characters. Control sequences
184 * (starting with the CSI) aren't counted. A space
185 * generates a non-printing word, which is valid (the
186 * space is printed according to regular spacing rules).
190 for (jhy
= 0; j
< (int)p
->col
; j
++) {
191 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
193 if (8 != p
->buf
[j
]) {
194 if (vend
> vis
&& vend
< bp
&&
195 ASCII_HYPH
== p
->buf
[j
])
203 * Find out whether we would exceed the right margin.
204 * If so, break to the next line.
206 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
209 if (TERMP_NOBREAK
& p
->flags
) {
210 p
->viscol
= p
->rmargin
;
211 (*p
->advance
)(p
, p
->rmargin
);
212 vend
+= p
->rmargin
- p
->offset
;
218 /* Remove the p->overstep width. */
220 bp
+= (int)/* LINTED */
226 * Skip leading tabs, they were handled above.
228 while (i
< (int)p
->col
&& '\t' == p
->buf
[i
])
231 /* Write out the [remaining] word. */
232 for ( ; i
< (int)p
->col
; i
++) {
233 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
235 if ('\t' == p
->buf
[i
])
237 if (' ' == p
->buf
[i
]) {
238 while (' ' == p
->buf
[i
]) {
244 if (ASCII_NBRSP
== p
->buf
[i
]) {
250 * Now we definitely know there will be
251 * printable characters to output,
252 * so write preceding white space now.
255 (*p
->advance
)(p
, vbl
);
260 if (ASCII_HYPH
== p
->buf
[i
])
261 (*p
->letter
)(p
, '-');
263 (*p
->letter
)(p
, p
->buf
[i
]);
274 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
280 if (TERMP_HANG
& p
->flags
) {
281 /* We need one blank after the tag. */
282 p
->overstep
= /* LINTED */
286 * Behave exactly the same way as groff:
287 * If we have overstepped the margin, temporarily move
288 * it to the right and flag the rest of the line to be
290 * If we landed right at the margin, be happy.
291 * If we are one step before the margin, temporarily
292 * move it one step LEFT and flag the rest of the line
295 if (p
->overstep
>= -1) {
296 assert((int)maxvis
+ p
->overstep
>= 0);
298 maxvis
+= p
->overstep
;
302 } else if (TERMP_DANGLE
& p
->flags
)
306 if (maxvis
> vis
+ /* LINTED */
307 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0)) {
308 p
->viscol
+= maxvis
- vis
;
309 (*p
->advance
)(p
, maxvis
- vis
);
310 vis
+= (maxvis
- vis
);
311 } else { /* ...or newline break. */
313 p
->viscol
= p
->rmargin
;
314 (*p
->advance
)(p
, p
->rmargin
);
320 * A newline only breaks an existing line; it won't assert vertical
321 * space. All data in the output buffer is flushed prior to the newline
325 term_newln(struct termp
*p
)
328 p
->flags
|= TERMP_NOSPACE
;
329 if (0 == p
->col
&& 0 == p
->viscol
) {
330 p
->flags
&= ~TERMP_NOLPAD
;
334 p
->flags
&= ~TERMP_NOLPAD
;
339 * Asserts a vertical space (a full, empty line-break between lines).
340 * Note that if used twice, this will cause two blank spaces and so on.
341 * All data in the output buffer is flushed prior to the newline
345 term_vspace(struct termp
*p
)
355 spec(struct termp
*p
, const char *word
, size_t len
)
360 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
367 res(struct termp
*p
, const char *word
, size_t len
)
372 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
379 term_fontlast(struct termp
*p
)
384 p
->fontl
= p
->fontq
[p
->fonti
];
385 p
->fontq
[p
->fonti
] = f
;
390 term_fontrepl(struct termp
*p
, enum termfont f
)
393 p
->fontl
= p
->fontq
[p
->fonti
];
394 p
->fontq
[p
->fonti
] = f
;
399 term_fontpush(struct termp
*p
, enum termfont f
)
402 assert(p
->fonti
+ 1 < 10);
403 p
->fontl
= p
->fontq
[p
->fonti
];
404 p
->fontq
[++p
->fonti
] = f
;
409 term_fontq(struct termp
*p
)
412 return(&p
->fontq
[p
->fonti
]);
417 term_fonttop(struct termp
*p
)
420 return(p
->fontq
[p
->fonti
]);
425 term_fontpopq(struct termp
*p
, const void *key
)
428 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
430 assert(p
->fonti
>= 0);
435 term_fontpop(struct termp
*p
)
444 * Handle pwords, partial words, which may be either a single word or a
445 * phrase that cannot be broken down (such as a literal string). This
446 * handles word styling.
449 term_word(struct termp
*p
, const char *word
)
451 const char *sv
, *seq
;
458 if (word
[0] && '\0' == word
[1])
475 if ( ! (TERMP_IGNDELIM
& p
->flags
))
476 p
->flags
|= TERMP_NOSPACE
;
482 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
484 if (TERMP_SENTENCE
& p
->flags
)
488 if ( ! (p
->flags
& TERMP_NONOSPACE
))
489 p
->flags
&= ~TERMP_NOSPACE
;
491 p
->flags
&= ~TERMP_SENTENCE
;
493 /* FIXME: use strcspn. */
503 sz
= a2roffdeco(&deco
, &seq
, &ssz
);
506 case (DECO_RESERVED
):
513 term_fontrepl(p
, TERMFONT_BOLD
);
516 term_fontrepl(p
, TERMFONT_UNDER
);
519 term_fontrepl(p
, TERMFONT_NONE
);
521 case (DECO_PREVIOUS
):
529 if (DECO_NOSPACE
== deco
&& '\0' == *word
)
530 p
->flags
|= TERMP_NOSPACE
;
534 * Note that we don't process the pipe: the parser sees it as
535 * punctuation, but we don't in terms of typography.
537 if (sv
[0] && 0 == sv
[1])
542 p
->flags
|= TERMP_NOSPACE
;
551 adjbuf(struct termp
*p
, size_t sz
)
556 while (sz
>= p
->maxcols
)
559 p
->buf
= realloc(p
->buf
, p
->maxcols
);
560 if (NULL
== p
->buf
) {
568 buffera(struct termp
*p
, const char *word
, size_t sz
)
571 if (p
->col
+ sz
>= p
->maxcols
)
572 adjbuf(p
, p
->col
+ sz
);
574 memcpy(&p
->buf
[(int)p
->col
], word
, sz
);
580 bufferc(struct termp
*p
, char c
)
583 if (p
->col
+ 1 >= p
->maxcols
)
584 adjbuf(p
, p
->col
+ 1);
586 p
->buf
[(int)p
->col
++] = c
;
591 encode(struct termp
*p
, const char *word
, size_t sz
)
597 * Encode and buffer a string of characters. If the current
598 * font mode is unset, buffer directly, else encode then buffer
599 * character by character.
602 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
603 buffera(p
, word
, sz
);
607 for (i
= 0; i
< (int)sz
; i
++) {
608 if ( ! isgraph((u_char
)word
[i
])) {
613 if (TERMFONT_UNDER
== f
)
625 term_len(const struct termp
*p
, size_t sz
)
628 return((*p
->width
)(p
, ' ') * sz
);
633 term_strlen(const struct termp
*p
, const char *cp
)
637 for (sz
= 0; *cp
; cp
++)
638 sz
+= (*p
->width
)(p
, *cp
);
645 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
663 r
= su
->scale
/ 1000;
675 return(/* LINTED */(size_t)
681 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
685 /* XXX: CM, IN, and PT are approximations. */
692 /* XXX: this is an approximation. */
696 r
= (10 * su
->scale
) / 6;
699 r
= (10 * su
->scale
) / 72;
702 r
= su
->scale
/ 1000; /* FIXME: double-check. */
705 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
714 return((size_t)/* LINTED */