]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
922385ae90c64e38180c388ac26850bc84b8960a
1 /* $Id: term.c,v 1.193 2011/05/17 14:38:34 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
36 static void adjbuf(struct termp
*p
, int);
37 static void bufferc(struct termp
*, char);
38 static void encode(struct termp
*, const char *, size_t);
41 term_free(struct termp
*p
)
47 mchars_free(p
->symtab
);
54 term_begin(struct termp
*p
, term_margin head
,
55 term_margin foot
, const void *arg
)
66 term_end(struct termp
*p
)
73 * Flush a line of text. A "line" is loosely defined as being something
74 * that should be followed by a newline, regardless of whether it's
75 * broken apart by newlines getting there. A line can also be a
76 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
77 * not have a trailing newline.
79 * The following flags may be specified:
81 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
82 * offset value. This is useful when doing columnar lists where the
83 * prior column has right-padded.
85 * - TERMP_NOBREAK: this is the most important and is used when making
86 * columns. In short: don't print a newline and instead pad to the
87 * right margin. Used in conjunction with TERMP_NOLPAD.
89 * - TERMP_TWOSPACE: when padding, make sure there are at least two
90 * space characters of padding. Otherwise, rather break the line.
92 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
93 * the line is overrun, and don't pad-right if it's underrun.
95 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
96 * overruning, instead save the position and continue at that point
97 * when the next invocation.
99 * In-line line breaking:
101 * If TERMP_NOBREAK is specified and the line overruns the right
102 * margin, it will break and pad-right to the right margin after
103 * writing. If maxrmargin is violated, it will break and continue
104 * writing from the right-margin, which will lead to the above scenario
105 * upon exit. Otherwise, the line will break at the right margin.
108 term_flushln(struct termp
*p
)
110 int i
; /* current input position in p->buf */
111 size_t vis
; /* current visual position on output */
112 size_t vbl
; /* number of blanks to prepend to output */
113 size_t vend
; /* end of word visual position on output */
114 size_t bp
; /* visual right border position */
115 size_t dv
; /* temporary for visual pos calculations */
116 int j
; /* temporary loop index for p->buf */
117 int jhy
; /* last hyph before overflow w/r/t j */
118 size_t maxvis
; /* output position of visible boundary */
119 size_t mmax
; /* used in calculating bp */
122 * First, establish the maximum columns of "visible" content.
123 * This is usually the difference between the right-margin and
124 * an indentation, but can be, for tagged lists or columns, a
125 * small set of values.
127 assert (p
->rmargin
>= p
->offset
);
128 dv
= p
->rmargin
- p
->offset
;
129 maxvis
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
130 dv
= p
->maxrmargin
- p
->offset
;
131 mmax
= (int)dv
> p
->overstep
? dv
- (size_t)p
->overstep
: 0;
133 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
136 * Indent the first line of a paragraph.
138 vbl
= p
->flags
& TERMP_NOLPAD
? (size_t)0 : p
->offset
;
145 * Handle literal tab characters: collapse all
146 * subsequent tabs into a single huge set of spaces.
148 while (i
< p
->col
&& '\t' == p
->buf
[i
]) {
149 vend
= (vis
/ p
->tabwidth
+ 1) * p
->tabwidth
;
156 * Count up visible word characters. Control sequences
157 * (starting with the CSI) aren't counted. A space
158 * generates a non-printing word, which is valid (the
159 * space is printed according to regular spacing rules).
162 for (j
= i
, jhy
= 0; j
< p
->col
; j
++) {
163 if ((j
&& ' ' == p
->buf
[j
]) || '\t' == p
->buf
[j
])
166 /* Back over the the last printed character. */
167 if (8 == p
->buf
[j
]) {
169 vend
-= (*p
->width
)(p
, p
->buf
[j
- 1]);
174 /* Break at the hyphen point if we overrun. */
175 if (vend
> vis
&& vend
< bp
&&
176 ASCII_HYPH
== p
->buf
[j
])
179 vend
+= (*p
->width
)(p
, p
->buf
[j
]);
183 * Find out whether we would exceed the right margin.
184 * If so, break to the next line.
186 if (vend
> bp
&& 0 == jhy
&& vis
> 0) {
189 if (TERMP_NOBREAK
& p
->flags
) {
190 p
->viscol
= p
->rmargin
;
191 (*p
->advance
)(p
, p
->rmargin
);
192 vend
+= p
->rmargin
- p
->offset
;
198 /* Remove the p->overstep width. */
200 bp
+= (size_t)p
->overstep
;
204 /* Write out the [remaining] word. */
205 for ( ; i
< p
->col
; i
++) {
206 if (vend
> bp
&& jhy
> 0 && i
> jhy
)
208 if ('\t' == p
->buf
[i
])
210 if (' ' == p
->buf
[i
]) {
212 while (' ' == p
->buf
[i
])
214 dv
= (size_t)(i
- j
) * (*p
->width
)(p
, ' ');
219 if (ASCII_NBRSP
== p
->buf
[i
]) {
220 vbl
+= (*p
->width
)(p
, ' ');
225 * Now we definitely know there will be
226 * printable characters to output,
227 * so write preceding white space now.
230 (*p
->advance
)(p
, vbl
);
235 if (ASCII_HYPH
== p
->buf
[i
]) {
236 (*p
->letter
)(p
, '-');
237 p
->viscol
+= (*p
->width
)(p
, '-');
239 (*p
->letter
)(p
, p
->buf
[i
]);
240 p
->viscol
+= (*p
->width
)(p
, p
->buf
[i
]);
247 * If there was trailing white space, it was not printed;
248 * so reset the cursor position accordingly.
255 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
261 if (TERMP_HANG
& p
->flags
) {
262 /* We need one blank after the tag. */
263 p
->overstep
= (int)(vis
- maxvis
+ (*p
->width
)(p
, ' '));
266 * Behave exactly the same way as groff:
267 * If we have overstepped the margin, temporarily move
268 * it to the right and flag the rest of the line to be
270 * If we landed right at the margin, be happy.
271 * If we are one step before the margin, temporarily
272 * move it one step LEFT and flag the rest of the line
275 if (p
->overstep
>= -1) {
276 assert((int)maxvis
+ p
->overstep
>= 0);
277 maxvis
+= (size_t)p
->overstep
;
281 } else if (TERMP_DANGLE
& p
->flags
)
286 ((TERMP_TWOSPACE
& p
->flags
) ? (*p
->width
)(p
, ' ') : 0)) {
287 p
->viscol
+= maxvis
- vis
;
288 (*p
->advance
)(p
, maxvis
- vis
);
289 vis
+= (maxvis
- vis
);
290 } else { /* ...or newline break. */
292 p
->viscol
= p
->rmargin
;
293 (*p
->advance
)(p
, p
->rmargin
);
299 * A newline only breaks an existing line; it won't assert vertical
300 * space. All data in the output buffer is flushed prior to the newline
304 term_newln(struct termp
*p
)
307 p
->flags
|= TERMP_NOSPACE
;
308 if (0 == p
->col
&& 0 == p
->viscol
) {
309 p
->flags
&= ~TERMP_NOLPAD
;
313 p
->flags
&= ~TERMP_NOLPAD
;
318 * Asserts a vertical space (a full, empty line-break between lines).
319 * Note that if used twice, this will cause two blank spaces and so on.
320 * All data in the output buffer is flushed prior to the newline
324 term_vspace(struct termp
*p
)
333 term_fontlast(struct termp
*p
)
338 p
->fontl
= p
->fontq
[p
->fonti
];
339 p
->fontq
[p
->fonti
] = f
;
344 term_fontrepl(struct termp
*p
, enum termfont f
)
347 p
->fontl
= p
->fontq
[p
->fonti
];
348 p
->fontq
[p
->fonti
] = f
;
353 term_fontpush(struct termp
*p
, enum termfont f
)
356 assert(p
->fonti
+ 1 < 10);
357 p
->fontl
= p
->fontq
[p
->fonti
];
358 p
->fontq
[++p
->fonti
] = f
;
363 term_fontq(struct termp
*p
)
366 return(&p
->fontq
[p
->fonti
]);
371 term_fonttop(struct termp
*p
)
374 return(p
->fontq
[p
->fonti
]);
379 term_fontpopq(struct termp
*p
, const void *key
)
382 while (p
->fonti
>= 0 && key
!= &p
->fontq
[p
->fonti
])
384 assert(p
->fonti
>= 0);
389 term_fontpop(struct termp
*p
)
397 * Handle pwords, partial words, which may be either a single word or a
398 * phrase that cannot be broken down (such as a literal string). This
399 * handles word styling.
402 term_word(struct termp
*p
, const char *word
)
404 const char *seq
, *cp
;
410 if ( ! (TERMP_NOSPACE
& p
->flags
)) {
411 if ( ! (TERMP_KEEP
& p
->flags
)) {
412 if (TERMP_PREKEEP
& p
->flags
)
413 p
->flags
|= TERMP_KEEP
;
415 if (TERMP_SENTENCE
& p
->flags
)
418 bufferc(p
, ASCII_NBRSP
);
421 if ( ! (p
->flags
& TERMP_NONOSPACE
))
422 p
->flags
&= ~TERMP_NOSPACE
;
424 p
->flags
|= TERMP_NOSPACE
;
426 p
->flags
&= ~(TERMP_SENTENCE
| TERMP_IGNDELIM
);
428 while ('\0' != *word
) {
429 if ((ssz
= strcspn(word
, "\\")) > 0)
430 encode(p
, word
, ssz
);
437 esc
= mandoc_escape(&word
, &seq
, &sz
);
438 if (ESCAPE_ERROR
== esc
)
442 case (ESCAPE_UNICODE
):
445 case (ESCAPE_NUMBERED
):
446 if ('\0' != (c
= mchars_num2char(seq
, sz
)))
449 case (ESCAPE_PREDEF
):
450 cp
= mchars_res2str(p
->symtab
, seq
, sz
, &ssz
);
454 case (ESCAPE_SPECIAL
):
455 cp
= mchars_spec2str(p
->symtab
, seq
, sz
, &ssz
);
461 case (ESCAPE_FONTBOLD
):
462 term_fontrepl(p
, TERMFONT_BOLD
);
464 case (ESCAPE_FONTITALIC
):
465 term_fontrepl(p
, TERMFONT_UNDER
);
467 case (ESCAPE_FONTROMAN
):
468 term_fontrepl(p
, TERMFONT_NONE
);
470 case (ESCAPE_FONTPREV
):
473 case (ESCAPE_NOSPACE
):
475 p
->flags
|= TERMP_NOSPACE
;
484 adjbuf(struct termp
*p
, int sz
)
489 while (sz
>= p
->maxcols
)
492 p
->buf
= mandoc_realloc
493 (p
->buf
, sizeof(int) * (size_t)p
->maxcols
);
497 bufferc(struct termp
*p
, char c
)
500 if (p
->col
+ 1 >= p
->maxcols
)
501 adjbuf(p
, p
->col
+ 1);
503 p
->buf
[p
->col
++] = c
;
507 encode(struct termp
*p
, const char *word
, size_t sz
)
516 * Encode and buffer a string of characters. If the current
517 * font mode is unset, buffer directly, else encode then buffer
518 * character by character.
521 if (TERMFONT_NONE
== (f
= term_fonttop(p
))) {
522 if (p
->col
+ len
>= p
->maxcols
)
523 adjbuf(p
, p
->col
+ len
);
524 for (i
= 0; i
< len
; i
++)
525 p
->buf
[p
->col
++] = word
[i
];
529 /* Pre-buffer, assuming worst-case. */
531 if (p
->col
+ 1 + (len
* 3) >= p
->maxcols
)
532 adjbuf(p
, p
->col
+ 1 + (len
* 3));
534 for (i
= 0; i
< len
; i
++) {
535 if ( ! isgraph((unsigned char)word
[i
])) {
536 p
->buf
[p
->col
++] = word
[i
];
540 if (TERMFONT_UNDER
== f
)
541 p
->buf
[p
->col
++] = '_';
543 p
->buf
[p
->col
++] = word
[i
];
545 p
->buf
[p
->col
++] = 8;
546 p
->buf
[p
->col
++] = word
[i
];
551 term_len(const struct termp
*p
, size_t sz
)
554 return((*p
->width
)(p
, ' ') * sz
);
559 term_strlen(const struct termp
*p
, const char *cp
)
563 const char *seq
, *rhs
;
564 static const char rej
[] = { '\\', ASCII_HYPH
, ASCII_NBRSP
, '\0' };
567 * Account for escaped sequences within string length
568 * calculations. This follows the logic in term_word() as we
569 * must calculate the width of produced strings.
573 while ('\0' != *cp
) {
574 rsz
= strcspn(cp
, rej
);
575 for (i
= 0; i
< rsz
; i
++)
576 sz
+= (*p
->width
)(p
, *cp
++);
583 switch (mandoc_escape(&cp
, &seq
, &ssz
)) {
586 case (ESCAPE_UNICODE
):
589 case (ESCAPE_NUMBERED
):
591 c
= mchars_num2char(seq
, ssz
);
593 sz
+= (*p
->width
)(p
, c
);
595 case (ESCAPE_PREDEF
):
597 (p
->symtab
, seq
, ssz
, &rsz
);
599 case (ESCAPE_SPECIAL
):
600 rhs
= mchars_spec2str
601 (p
->symtab
, seq
, ssz
, &rsz
);
616 for (i
= 0; i
< rsz
; i
++)
617 sz
+= (*p
->width
)(p
, *rhs
++);
620 sz
+= (*p
->width
)(p
, ' ');
624 sz
+= (*p
->width
)(p
, '-');
637 term_vspan(const struct termp
*p
, const struct roffsu
*su
)
655 r
= su
->scale
/ 1000;
667 return(/* LINTED */(size_t)
672 term_hspan(const struct termp
*p
, const struct roffsu
*su
)
676 v
= ((*p
->hspan
)(p
, su
));
679 return((size_t) /* LINTED */