]> git.cameronkatri.com Git - mandoc.git/blob - term.c
in the SYNOPSIS, add .Fo and first .Fn arguments to the names table
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.237 2014/12/02 10:08:06 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
5 *
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.
9 *
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.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "mandoc.h"
29 #include "mandoc_aux.h"
30 #include "out.h"
31 #include "term.h"
32 #include "main.h"
33
34 static size_t cond_width(const struct termp *, int, int *);
35 static void adjbuf(struct termp *p, size_t);
36 static void bufferc(struct termp *, char);
37 static void encode(struct termp *, const char *, size_t);
38 static void encode1(struct termp *, int);
39
40
41 void
42 term_free(struct termp *p)
43 {
44
45 free(p->buf);
46 free(p);
47 }
48
49 void
50 term_begin(struct termp *p, term_margin head,
51 term_margin foot, const void *arg)
52 {
53
54 p->headf = head;
55 p->footf = foot;
56 p->argf = arg;
57 (*p->begin)(p);
58 }
59
60 void
61 term_end(struct termp *p)
62 {
63
64 (*p->end)(p);
65 }
66
67 /*
68 * Flush a chunk of text. By default, break the output line each time
69 * the right margin is reached, and continue output on the next line
70 * at the same offset as the chunk itself. By default, also break the
71 * output line at the end of the chunk.
72 * The following flags may be specified:
73 *
74 * - TERMP_NOBREAK: Do not break the output line at the right margin,
75 * but only at the max right margin. Also, do not break the output
76 * line at the end of the chunk, such that the next call can pad to
77 * the next column. However, if less than p->trailspace blanks,
78 * which can be 0, 1, or 2, remain to the right margin, the line
79 * will be broken.
80 * - TERMP_BRIND: If the chunk does not fit and the output line has
81 * to be broken, start the next line at the right margin instead
82 * of at the offset. Used together with TERMP_NOBREAK for the tags
83 * in various kinds of tagged lists.
84 * - TERMP_DANGLE: Do not break the output line at the right margin,
85 * append the next chunk after it even if this one is too long.
86 * To be used together with TERMP_NOBREAK.
87 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
88 * the next chunk if this column is not full.
89 */
90 void
91 term_flushln(struct termp *p)
92 {
93 size_t i; /* current input position in p->buf */
94 int ntab; /* number of tabs to prepend */
95 size_t vis; /* current visual position on output */
96 size_t vbl; /* number of blanks to prepend to output */
97 size_t vend; /* end of word visual position on output */
98 size_t bp; /* visual right border position */
99 size_t dv; /* temporary for visual pos calculations */
100 size_t j; /* temporary loop index for p->buf */
101 size_t jhy; /* last hyph before overflow w/r/t j */
102 size_t maxvis; /* output position of visible boundary */
103 size_t rmargin; /* the rightmost of the two margins */
104
105 /*
106 * First, establish the maximum columns of "visible" content.
107 * This is usually the difference between the right-margin and
108 * an indentation, but can be, for tagged lists or columns, a
109 * small set of values.
110 *
111 * The following unsigned-signed subtractions look strange,
112 * but they are actually correct. If the int p->overstep
113 * is negative, it gets sign extended. Subtracting that
114 * very large size_t effectively adds a small number to dv.
115 */
116 rmargin = p->rmargin > p->offset ? p->rmargin : p->offset;
117 dv = p->rmargin - p->offset;
118 maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
119
120 if (p->flags & TERMP_NOBREAK) {
121 dv = p->maxrmargin > p->offset ?
122 p->maxrmargin - p->offset : 0;
123 bp = (int)dv > p->overstep ?
124 dv - (size_t)p->overstep : 0;
125 } else
126 bp = maxvis;
127
128 /*
129 * Calculate the required amount of padding.
130 */
131 vbl = p->offset + p->overstep > p->viscol ?
132 p->offset + p->overstep - p->viscol : 0;
133
134 vis = vend = 0;
135 i = 0;
136
137 while (i < p->col) {
138 /*
139 * Handle literal tab characters: collapse all
140 * subsequent tabs into a single huge set of spaces.
141 */
142 ntab = 0;
143 while (i < p->col && '\t' == p->buf[i]) {
144 vend = (vis / p->tabwidth + 1) * p->tabwidth;
145 vbl += vend - vis;
146 vis = vend;
147 ntab++;
148 i++;
149 }
150
151 /*
152 * Count up visible word characters. Control sequences
153 * (starting with the CSI) aren't counted. A space
154 * generates a non-printing word, which is valid (the
155 * space is printed according to regular spacing rules).
156 */
157
158 for (j = i, jhy = 0; j < p->col; j++) {
159 if (' ' == p->buf[j] || '\t' == p->buf[j])
160 break;
161
162 /* Back over the the last printed character. */
163 if (8 == p->buf[j]) {
164 assert(j);
165 vend -= (*p->width)(p, p->buf[j - 1]);
166 continue;
167 }
168
169 /* Regular word. */
170 /* Break at the hyphen point if we overrun. */
171 if (vend > vis && vend < bp &&
172 (ASCII_HYPH == p->buf[j] ||
173 ASCII_BREAK == p->buf[j]))
174 jhy = j;
175
176 /*
177 * Hyphenation now decided, put back a real
178 * hyphen such that we get the correct width.
179 */
180 if (ASCII_HYPH == p->buf[j])
181 p->buf[j] = '-';
182
183 vend += (*p->width)(p, p->buf[j]);
184 }
185
186 /*
187 * Find out whether we would exceed the right margin.
188 * If so, break to the next line.
189 */
190 if (vend > bp && 0 == jhy && vis > 0) {
191 vend -= vis;
192 (*p->endline)(p);
193 p->viscol = 0;
194 if (TERMP_BRIND & p->flags) {
195 vbl = rmargin;
196 vend += rmargin - p->offset;
197 } else
198 vbl = p->offset;
199
200 /* use pending tabs on the new line */
201
202 if (0 < ntab)
203 vbl += ntab * p->tabwidth;
204
205 /*
206 * Remove the p->overstep width.
207 * Again, if p->overstep is negative,
208 * sign extension does the right thing.
209 */
210
211 bp += (size_t)p->overstep;
212 p->overstep = 0;
213 }
214
215 /* Write out the [remaining] word. */
216 for ( ; i < p->col; i++) {
217 if (vend > bp && jhy > 0 && i > jhy)
218 break;
219 if ('\t' == p->buf[i])
220 break;
221 if (' ' == p->buf[i]) {
222 j = i;
223 while (i < p->col && ' ' == p->buf[i])
224 i++;
225 dv = (i - j) * (*p->width)(p, ' ');
226 vbl += dv;
227 vend += dv;
228 break;
229 }
230 if (ASCII_NBRSP == p->buf[i]) {
231 vbl += (*p->width)(p, ' ');
232 continue;
233 }
234 if (ASCII_BREAK == p->buf[i])
235 continue;
236
237 /*
238 * Now we definitely know there will be
239 * printable characters to output,
240 * so write preceding white space now.
241 */
242 if (vbl) {
243 (*p->advance)(p, vbl);
244 p->viscol += vbl;
245 vbl = 0;
246 }
247
248 (*p->letter)(p, p->buf[i]);
249 if (8 == p->buf[i])
250 p->viscol -= (*p->width)(p, p->buf[i-1]);
251 else
252 p->viscol += (*p->width)(p, p->buf[i]);
253 }
254 vis = vend;
255 }
256
257 /*
258 * If there was trailing white space, it was not printed;
259 * so reset the cursor position accordingly.
260 */
261 if (vis > vbl)
262 vis -= vbl;
263 else
264 vis = 0;
265
266 p->col = 0;
267 p->overstep = 0;
268
269 if ( ! (TERMP_NOBREAK & p->flags)) {
270 p->viscol = 0;
271 (*p->endline)(p);
272 return;
273 }
274
275 if (TERMP_HANG & p->flags) {
276 p->overstep = (int)(vis - maxvis +
277 p->trailspace * (*p->width)(p, ' '));
278
279 /*
280 * If we have overstepped the margin, temporarily move
281 * it to the right and flag the rest of the line to be
282 * shorter.
283 * If there is a request to keep the columns together,
284 * allow negative overstep when the column is not full.
285 */
286 if (p->trailspace && p->overstep < 0)
287 p->overstep = 0;
288 return;
289
290 } else if (TERMP_DANGLE & p->flags)
291 return;
292
293 /* If the column was overrun, break the line. */
294 if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
295 (*p->endline)(p);
296 p->viscol = 0;
297 }
298 }
299
300 /*
301 * A newline only breaks an existing line; it won't assert vertical
302 * space. All data in the output buffer is flushed prior to the newline
303 * assertion.
304 */
305 void
306 term_newln(struct termp *p)
307 {
308
309 p->flags |= TERMP_NOSPACE;
310 if (p->col || p->viscol)
311 term_flushln(p);
312 }
313
314 /*
315 * Asserts a vertical space (a full, empty line-break between lines).
316 * Note that if used twice, this will cause two blank spaces and so on.
317 * All data in the output buffer is flushed prior to the newline
318 * assertion.
319 */
320 void
321 term_vspace(struct termp *p)
322 {
323
324 term_newln(p);
325 p->viscol = 0;
326 if (0 < p->skipvsp)
327 p->skipvsp--;
328 else
329 (*p->endline)(p);
330 }
331
332 void
333 term_fontlast(struct termp *p)
334 {
335 enum termfont f;
336
337 f = p->fontl;
338 p->fontl = p->fontq[p->fonti];
339 p->fontq[p->fonti] = f;
340 }
341
342 void
343 term_fontrepl(struct termp *p, enum termfont f)
344 {
345
346 p->fontl = p->fontq[p->fonti];
347 p->fontq[p->fonti] = f;
348 }
349
350 void
351 term_fontpush(struct termp *p, enum termfont f)
352 {
353
354 assert(p->fonti + 1 < 10);
355 p->fontl = p->fontq[p->fonti];
356 p->fontq[++p->fonti] = f;
357 }
358
359 const void *
360 term_fontq(struct termp *p)
361 {
362
363 return(&p->fontq[p->fonti]);
364 }
365
366 enum termfont
367 term_fonttop(struct termp *p)
368 {
369
370 return(p->fontq[p->fonti]);
371 }
372
373 void
374 term_fontpopq(struct termp *p, const void *key)
375 {
376
377 while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
378 p->fonti--;
379 assert(p->fonti >= 0);
380 }
381
382 void
383 term_fontpop(struct termp *p)
384 {
385
386 assert(p->fonti);
387 p->fonti--;
388 }
389
390 /*
391 * Handle pwords, partial words, which may be either a single word or a
392 * phrase that cannot be broken down (such as a literal string). This
393 * handles word styling.
394 */
395 void
396 term_word(struct termp *p, const char *word)
397 {
398 const char nbrsp[2] = { ASCII_NBRSP, 0 };
399 const char *seq, *cp;
400 int sz, uc;
401 size_t ssz;
402 enum mandoc_esc esc;
403
404 if ( ! (TERMP_NOSPACE & p->flags)) {
405 if ( ! (TERMP_KEEP & p->flags)) {
406 bufferc(p, ' ');
407 if (TERMP_SENTENCE & p->flags)
408 bufferc(p, ' ');
409 } else
410 bufferc(p, ASCII_NBRSP);
411 }
412 if (TERMP_PREKEEP & p->flags)
413 p->flags |= TERMP_KEEP;
414
415 if ( ! (p->flags & TERMP_NONOSPACE))
416 p->flags &= ~TERMP_NOSPACE;
417 else
418 p->flags |= TERMP_NOSPACE;
419
420 p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
421
422 while ('\0' != *word) {
423 if ('\\' != *word) {
424 if (TERMP_SKIPCHAR & p->flags) {
425 p->flags &= ~TERMP_SKIPCHAR;
426 word++;
427 continue;
428 }
429 if (TERMP_NBRWORD & p->flags) {
430 if (' ' == *word) {
431 encode(p, nbrsp, 1);
432 word++;
433 continue;
434 }
435 ssz = strcspn(word, "\\ ");
436 } else
437 ssz = strcspn(word, "\\");
438 encode(p, word, ssz);
439 word += (int)ssz;
440 continue;
441 }
442
443 word++;
444 esc = mandoc_escape(&word, &seq, &sz);
445 if (ESCAPE_ERROR == esc)
446 continue;
447
448 switch (esc) {
449 case ESCAPE_UNICODE:
450 uc = mchars_num2uc(seq + 1, sz - 1);
451 break;
452 case ESCAPE_NUMBERED:
453 uc = mchars_num2char(seq, sz);
454 if (uc < 0)
455 continue;
456 break;
457 case ESCAPE_SPECIAL:
458 if (p->enc == TERMENC_ASCII) {
459 cp = mchars_spec2str(p->symtab,
460 seq, sz, &ssz);
461 if (cp != NULL)
462 encode(p, cp, ssz);
463 } else {
464 uc = mchars_spec2cp(p->symtab, seq, sz);
465 if (uc > 0)
466 encode1(p, uc);
467 }
468 continue;
469 case ESCAPE_FONTBOLD:
470 term_fontrepl(p, TERMFONT_BOLD);
471 continue;
472 case ESCAPE_FONTITALIC:
473 term_fontrepl(p, TERMFONT_UNDER);
474 continue;
475 case ESCAPE_FONTBI:
476 term_fontrepl(p, TERMFONT_BI);
477 continue;
478 case ESCAPE_FONT:
479 /* FALLTHROUGH */
480 case ESCAPE_FONTROMAN:
481 term_fontrepl(p, TERMFONT_NONE);
482 continue;
483 case ESCAPE_FONTPREV:
484 term_fontlast(p);
485 continue;
486 case ESCAPE_NOSPACE:
487 if (TERMP_SKIPCHAR & p->flags)
488 p->flags &= ~TERMP_SKIPCHAR;
489 else if ('\0' == *word)
490 p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
491 continue;
492 case ESCAPE_SKIPCHAR:
493 p->flags |= TERMP_SKIPCHAR;
494 continue;
495 default:
496 continue;
497 }
498
499 /*
500 * Common handling for Unicode and numbered
501 * character escape sequences.
502 */
503
504 if (p->enc == TERMENC_ASCII) {
505 cp = ascii_uc2str(uc);
506 encode(p, cp, strlen(cp));
507 } else {
508 if ((uc < 0x20 && uc != 0x09) ||
509 (uc > 0x7E && uc < 0xA0))
510 uc = 0xFFFD;
511 encode1(p, uc);
512 }
513 }
514 p->flags &= ~TERMP_NBRWORD;
515 }
516
517 static void
518 adjbuf(struct termp *p, size_t sz)
519 {
520
521 if (0 == p->maxcols)
522 p->maxcols = 1024;
523 while (sz >= p->maxcols)
524 p->maxcols <<= 2;
525
526 p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
527 }
528
529 static void
530 bufferc(struct termp *p, char c)
531 {
532
533 if (p->col + 1 >= p->maxcols)
534 adjbuf(p, p->col + 1);
535
536 p->buf[p->col++] = c;
537 }
538
539 /*
540 * See encode().
541 * Do this for a single (probably unicode) value.
542 * Does not check for non-decorated glyphs.
543 */
544 static void
545 encode1(struct termp *p, int c)
546 {
547 enum termfont f;
548
549 if (TERMP_SKIPCHAR & p->flags) {
550 p->flags &= ~TERMP_SKIPCHAR;
551 return;
552 }
553
554 if (p->col + 6 >= p->maxcols)
555 adjbuf(p, p->col + 6);
556
557 f = term_fonttop(p);
558
559 if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
560 p->buf[p->col++] = '_';
561 p->buf[p->col++] = 8;
562 }
563 if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
564 if (ASCII_HYPH == c)
565 p->buf[p->col++] = '-';
566 else
567 p->buf[p->col++] = c;
568 p->buf[p->col++] = 8;
569 }
570 p->buf[p->col++] = c;
571 }
572
573 static void
574 encode(struct termp *p, const char *word, size_t sz)
575 {
576 size_t i;
577
578 if (TERMP_SKIPCHAR & p->flags) {
579 p->flags &= ~TERMP_SKIPCHAR;
580 return;
581 }
582
583 /*
584 * Encode and buffer a string of characters. If the current
585 * font mode is unset, buffer directly, else encode then buffer
586 * character by character.
587 */
588
589 if (TERMFONT_NONE == term_fonttop(p)) {
590 if (p->col + sz >= p->maxcols)
591 adjbuf(p, p->col + sz);
592 for (i = 0; i < sz; i++)
593 p->buf[p->col++] = word[i];
594 return;
595 }
596
597 /* Pre-buffer, assuming worst-case. */
598
599 if (p->col + 1 + (sz * 5) >= p->maxcols)
600 adjbuf(p, p->col + 1 + (sz * 5));
601
602 for (i = 0; i < sz; i++) {
603 if (ASCII_HYPH == word[i] ||
604 isgraph((unsigned char)word[i]))
605 encode1(p, word[i]);
606 else
607 p->buf[p->col++] = word[i];
608 }
609 }
610
611 void
612 term_setwidth(struct termp *p, const char *wstr)
613 {
614 struct roffsu su;
615 size_t width;
616 int iop;
617
618 iop = 0;
619 width = 0;
620 if (NULL != wstr) {
621 switch (*wstr) {
622 case '+':
623 iop = 1;
624 wstr++;
625 break;
626 case '-':
627 iop = -1;
628 wstr++;
629 break;
630 default:
631 break;
632 }
633 if (a2roffsu(wstr, &su, SCALE_MAX))
634 width = term_hspan(p, &su);
635 else
636 iop = 0;
637 }
638 (*p->setwidth)(p, iop, width);
639 }
640
641 size_t
642 term_len(const struct termp *p, size_t sz)
643 {
644
645 return((*p->width)(p, ' ') * sz);
646 }
647
648 static size_t
649 cond_width(const struct termp *p, int c, int *skip)
650 {
651
652 if (*skip) {
653 (*skip) = 0;
654 return(0);
655 } else
656 return((*p->width)(p, c));
657 }
658
659 size_t
660 term_strlen(const struct termp *p, const char *cp)
661 {
662 size_t sz, rsz, i;
663 int ssz, skip, uc;
664 const char *seq, *rhs;
665 enum mandoc_esc esc;
666 static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
667 ASCII_BREAK, '\0' };
668
669 /*
670 * Account for escaped sequences within string length
671 * calculations. This follows the logic in term_word() as we
672 * must calculate the width of produced strings.
673 */
674
675 sz = 0;
676 skip = 0;
677 while ('\0' != *cp) {
678 rsz = strcspn(cp, rej);
679 for (i = 0; i < rsz; i++)
680 sz += cond_width(p, *cp++, &skip);
681
682 switch (*cp) {
683 case '\\':
684 cp++;
685 esc = mandoc_escape(&cp, &seq, &ssz);
686 if (ESCAPE_ERROR == esc)
687 continue;
688
689 rhs = NULL;
690
691 switch (esc) {
692 case ESCAPE_UNICODE:
693 uc = mchars_num2uc(seq + 1, ssz - 1);
694 break;
695 case ESCAPE_NUMBERED:
696 uc = mchars_num2char(seq, ssz);
697 if (uc < 0)
698 continue;
699 break;
700 case ESCAPE_SPECIAL:
701 if (p->enc == TERMENC_ASCII) {
702 rhs = mchars_spec2str(p->symtab,
703 seq, ssz, &rsz);
704 if (rhs != NULL)
705 break;
706 } else {
707 uc = mchars_spec2cp(p->symtab,
708 seq, ssz);
709 if (uc > 0)
710 sz += cond_width(p, uc, &skip);
711 }
712 continue;
713 case ESCAPE_SKIPCHAR:
714 skip = 1;
715 continue;
716 default:
717 continue;
718 }
719
720 /*
721 * Common handling for Unicode and numbered
722 * character escape sequences.
723 */
724
725 if (rhs == NULL) {
726 if (p->enc == TERMENC_ASCII) {
727 rhs = ascii_uc2str(uc);
728 rsz = strlen(rhs);
729 } else {
730 if ((uc < 0x20 && uc != 0x09) ||
731 (uc > 0x7E && uc < 0xA0))
732 uc = 0xFFFD;
733 sz += cond_width(p, uc, &skip);
734 continue;
735 }
736 }
737
738 if (skip) {
739 skip = 0;
740 break;
741 }
742
743 /*
744 * Common handling for all escape sequences
745 * printing more than one character.
746 */
747
748 for (i = 0; i < rsz; i++)
749 sz += (*p->width)(p, *rhs++);
750 break;
751 case ASCII_NBRSP:
752 sz += cond_width(p, ' ', &skip);
753 cp++;
754 break;
755 case ASCII_HYPH:
756 sz += cond_width(p, '-', &skip);
757 cp++;
758 /* FALLTHROUGH */
759 case ASCII_BREAK:
760 break;
761 default:
762 break;
763 }
764 }
765
766 return(sz);
767 }
768
769 size_t
770 term_vspan(const struct termp *p, const struct roffsu *su)
771 {
772 double r;
773
774 switch (su->unit) {
775 case SCALE_CM:
776 r = su->scale * 2.0;
777 break;
778 case SCALE_IN:
779 r = su->scale * 6.0;
780 break;
781 case SCALE_PC:
782 r = su->scale;
783 break;
784 case SCALE_PT:
785 r = su->scale / 8.0;
786 break;
787 case SCALE_MM:
788 r = su->scale / 1000.0;
789 break;
790 case SCALE_VS:
791 r = su->scale;
792 break;
793 default:
794 r = su->scale - 1.0;
795 break;
796 }
797
798 if (r < 0.0)
799 r = 0.0;
800 return((size_t)(r + 0.0005));
801 }
802
803 size_t
804 term_hspan(const struct termp *p, const struct roffsu *su)
805 {
806 double v;
807
808 v = (*p->hspan)(p, su);
809 if (v < 0.0)
810 v = 0.0;
811 return((size_t)(v + 0.0005));
812 }