]> git.cameronkatri.com Git - mandoc.git/blob - term.c
Make the character table available to libroff so it can check the
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.231 2014/10/28 17:36:19 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 mmax; /* used in calculating bp */
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 assert (p->rmargin >= p->offset);
117 dv = p->rmargin - p->offset;
118 maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
119 dv = p->maxrmargin - p->offset;
120 mmax = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
121
122 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
123
124 /*
125 * Calculate the required amount of padding.
126 */
127 vbl = p->offset + p->overstep > p->viscol ?
128 p->offset + p->overstep - p->viscol : 0;
129
130 vis = vend = 0;
131 i = 0;
132
133 while (i < p->col) {
134 /*
135 * Handle literal tab characters: collapse all
136 * subsequent tabs into a single huge set of spaces.
137 */
138 ntab = 0;
139 while (i < p->col && '\t' == p->buf[i]) {
140 vend = (vis / p->tabwidth + 1) * p->tabwidth;
141 vbl += vend - vis;
142 vis = vend;
143 ntab++;
144 i++;
145 }
146
147 /*
148 * Count up visible word characters. Control sequences
149 * (starting with the CSI) aren't counted. A space
150 * generates a non-printing word, which is valid (the
151 * space is printed according to regular spacing rules).
152 */
153
154 for (j = i, jhy = 0; j < p->col; j++) {
155 if (' ' == p->buf[j] || '\t' == p->buf[j])
156 break;
157
158 /* Back over the the last printed character. */
159 if (8 == p->buf[j]) {
160 assert(j);
161 vend -= (*p->width)(p, p->buf[j - 1]);
162 continue;
163 }
164
165 /* Regular word. */
166 /* Break at the hyphen point if we overrun. */
167 if (vend > vis && vend < bp &&
168 (ASCII_HYPH == p->buf[j] ||
169 ASCII_BREAK == p->buf[j]))
170 jhy = j;
171
172 /*
173 * Hyphenation now decided, put back a real
174 * hyphen such that we get the correct width.
175 */
176 if (ASCII_HYPH == p->buf[j])
177 p->buf[j] = '-';
178
179 vend += (*p->width)(p, p->buf[j]);
180 }
181
182 /*
183 * Find out whether we would exceed the right margin.
184 * If so, break to the next line.
185 */
186 if (vend > bp && 0 == jhy && vis > 0) {
187 vend -= vis;
188 (*p->endline)(p);
189 p->viscol = 0;
190 if (TERMP_BRIND & p->flags) {
191 vbl = p->rmargin;
192 vend += p->rmargin - p->offset;
193 } else
194 vbl = p->offset;
195
196 /* use pending tabs on the new line */
197
198 if (0 < ntab)
199 vbl += ntab * p->tabwidth;
200
201 /*
202 * Remove the p->overstep width.
203 * Again, if p->overstep is negative,
204 * sign extension does the right thing.
205 */
206
207 bp += (size_t)p->overstep;
208 p->overstep = 0;
209 }
210
211 /* Write out the [remaining] word. */
212 for ( ; i < p->col; i++) {
213 if (vend > bp && jhy > 0 && i > jhy)
214 break;
215 if ('\t' == p->buf[i])
216 break;
217 if (' ' == p->buf[i]) {
218 j = i;
219 while (i < p->col && ' ' == p->buf[i])
220 i++;
221 dv = (i - j) * (*p->width)(p, ' ');
222 vbl += dv;
223 vend += dv;
224 break;
225 }
226 if (ASCII_NBRSP == p->buf[i]) {
227 vbl += (*p->width)(p, ' ');
228 continue;
229 }
230 if (ASCII_BREAK == p->buf[i])
231 continue;
232
233 /*
234 * Now we definitely know there will be
235 * printable characters to output,
236 * so write preceding white space now.
237 */
238 if (vbl) {
239 (*p->advance)(p, vbl);
240 p->viscol += vbl;
241 vbl = 0;
242 }
243
244 (*p->letter)(p, p->buf[i]);
245 if (8 == p->buf[i])
246 p->viscol -= (*p->width)(p, p->buf[i-1]);
247 else
248 p->viscol += (*p->width)(p, p->buf[i]);
249 }
250 vis = vend;
251 }
252
253 /*
254 * If there was trailing white space, it was not printed;
255 * so reset the cursor position accordingly.
256 */
257 if (vis)
258 vis -= vbl;
259
260 p->col = 0;
261 p->overstep = 0;
262
263 if ( ! (TERMP_NOBREAK & p->flags)) {
264 p->viscol = 0;
265 (*p->endline)(p);
266 return;
267 }
268
269 if (TERMP_HANG & p->flags) {
270 p->overstep = (int)(vis - maxvis +
271 p->trailspace * (*p->width)(p, ' '));
272
273 /*
274 * If we have overstepped the margin, temporarily move
275 * it to the right and flag the rest of the line to be
276 * shorter.
277 * If there is a request to keep the columns together,
278 * allow negative overstep when the column is not full.
279 */
280 if (p->trailspace && p->overstep < 0)
281 p->overstep = 0;
282 return;
283
284 } else if (TERMP_DANGLE & p->flags)
285 return;
286
287 /* If the column was overrun, break the line. */
288 if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
289 (*p->endline)(p);
290 p->viscol = 0;
291 }
292 }
293
294 /*
295 * A newline only breaks an existing line; it won't assert vertical
296 * space. All data in the output buffer is flushed prior to the newline
297 * assertion.
298 */
299 void
300 term_newln(struct termp *p)
301 {
302
303 p->flags |= TERMP_NOSPACE;
304 if (p->col || p->viscol)
305 term_flushln(p);
306 }
307
308 /*
309 * Asserts a vertical space (a full, empty line-break between lines).
310 * Note that if used twice, this will cause two blank spaces and so on.
311 * All data in the output buffer is flushed prior to the newline
312 * assertion.
313 */
314 void
315 term_vspace(struct termp *p)
316 {
317
318 term_newln(p);
319 p->viscol = 0;
320 if (0 < p->skipvsp)
321 p->skipvsp--;
322 else
323 (*p->endline)(p);
324 }
325
326 void
327 term_fontlast(struct termp *p)
328 {
329 enum termfont f;
330
331 f = p->fontl;
332 p->fontl = p->fontq[p->fonti];
333 p->fontq[p->fonti] = f;
334 }
335
336 void
337 term_fontrepl(struct termp *p, enum termfont f)
338 {
339
340 p->fontl = p->fontq[p->fonti];
341 p->fontq[p->fonti] = f;
342 }
343
344 void
345 term_fontpush(struct termp *p, enum termfont f)
346 {
347
348 assert(p->fonti + 1 < 10);
349 p->fontl = p->fontq[p->fonti];
350 p->fontq[++p->fonti] = f;
351 }
352
353 const void *
354 term_fontq(struct termp *p)
355 {
356
357 return(&p->fontq[p->fonti]);
358 }
359
360 enum termfont
361 term_fonttop(struct termp *p)
362 {
363
364 return(p->fontq[p->fonti]);
365 }
366
367 void
368 term_fontpopq(struct termp *p, const void *key)
369 {
370
371 while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
372 p->fonti--;
373 assert(p->fonti >= 0);
374 }
375
376 void
377 term_fontpop(struct termp *p)
378 {
379
380 assert(p->fonti);
381 p->fonti--;
382 }
383
384 /*
385 * Handle pwords, partial words, which may be either a single word or a
386 * phrase that cannot be broken down (such as a literal string). This
387 * handles word styling.
388 */
389 void
390 term_word(struct termp *p, const char *word)
391 {
392 const char nbrsp[2] = { ASCII_NBRSP, 0 };
393 const char *seq, *cp;
394 char c;
395 int sz, uc;
396 size_t ssz;
397 enum mandoc_esc esc;
398
399 if ( ! (TERMP_NOSPACE & p->flags)) {
400 if ( ! (TERMP_KEEP & p->flags)) {
401 bufferc(p, ' ');
402 if (TERMP_SENTENCE & p->flags)
403 bufferc(p, ' ');
404 } else
405 bufferc(p, ASCII_NBRSP);
406 }
407 if (TERMP_PREKEEP & p->flags)
408 p->flags |= TERMP_KEEP;
409
410 if ( ! (p->flags & TERMP_NONOSPACE))
411 p->flags &= ~TERMP_NOSPACE;
412 else
413 p->flags |= TERMP_NOSPACE;
414
415 p->flags &= ~TERMP_SENTENCE;
416
417 while ('\0' != *word) {
418 if ('\\' != *word) {
419 if (TERMP_SKIPCHAR & p->flags) {
420 p->flags &= ~TERMP_SKIPCHAR;
421 word++;
422 continue;
423 }
424 if (TERMP_NBRWORD & p->flags) {
425 if (' ' == *word) {
426 encode(p, nbrsp, 1);
427 word++;
428 continue;
429 }
430 ssz = strcspn(word, "\\ ");
431 } else
432 ssz = strcspn(word, "\\");
433 encode(p, word, ssz);
434 word += (int)ssz;
435 continue;
436 }
437
438 word++;
439 esc = mandoc_escape(&word, &seq, &sz);
440 if (ESCAPE_ERROR == esc)
441 continue;
442
443 switch (esc) {
444 case ESCAPE_UNICODE:
445 uc = mchars_num2uc(seq + 1, sz - 1);
446 if (p->enc == TERMENC_ASCII) {
447 cp = ascii_uc2str(uc);
448 encode(p, cp, strlen(cp));
449 } else
450 encode1(p, uc);
451 break;
452 case ESCAPE_NUMBERED:
453 c = mchars_num2char(seq, sz);
454 if ('\0' != c)
455 encode(p, &c, 1);
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, "<?>", 3);
463 else
464 encode(p, cp, ssz);
465 } else {
466 uc = mchars_spec2cp(p->symtab, seq, sz);
467 if (uc > 0)
468 encode1(p, uc);
469 }
470 break;
471 case ESCAPE_FONTBOLD:
472 term_fontrepl(p, TERMFONT_BOLD);
473 break;
474 case ESCAPE_FONTITALIC:
475 term_fontrepl(p, TERMFONT_UNDER);
476 break;
477 case ESCAPE_FONTBI:
478 term_fontrepl(p, TERMFONT_BI);
479 break;
480 case ESCAPE_FONT:
481 /* FALLTHROUGH */
482 case ESCAPE_FONTROMAN:
483 term_fontrepl(p, TERMFONT_NONE);
484 break;
485 case ESCAPE_FONTPREV:
486 term_fontlast(p);
487 break;
488 case ESCAPE_NOSPACE:
489 if (TERMP_SKIPCHAR & p->flags)
490 p->flags &= ~TERMP_SKIPCHAR;
491 else if ('\0' == *word)
492 p->flags |= TERMP_NOSPACE;
493 break;
494 case ESCAPE_SKIPCHAR:
495 p->flags |= TERMP_SKIPCHAR;
496 break;
497 default:
498 break;
499 }
500 }
501 p->flags &= ~TERMP_NBRWORD;
502 }
503
504 static void
505 adjbuf(struct termp *p, size_t sz)
506 {
507
508 if (0 == p->maxcols)
509 p->maxcols = 1024;
510 while (sz >= p->maxcols)
511 p->maxcols <<= 2;
512
513 p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
514 }
515
516 static void
517 bufferc(struct termp *p, char c)
518 {
519
520 if (p->col + 1 >= p->maxcols)
521 adjbuf(p, p->col + 1);
522
523 p->buf[p->col++] = c;
524 }
525
526 /*
527 * See encode().
528 * Do this for a single (probably unicode) value.
529 * Does not check for non-decorated glyphs.
530 */
531 static void
532 encode1(struct termp *p, int c)
533 {
534 enum termfont f;
535
536 if (TERMP_SKIPCHAR & p->flags) {
537 p->flags &= ~TERMP_SKIPCHAR;
538 return;
539 }
540
541 if (p->col + 6 >= p->maxcols)
542 adjbuf(p, p->col + 6);
543
544 f = term_fonttop(p);
545
546 if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
547 p->buf[p->col++] = '_';
548 p->buf[p->col++] = 8;
549 }
550 if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
551 if (ASCII_HYPH == c)
552 p->buf[p->col++] = '-';
553 else
554 p->buf[p->col++] = c;
555 p->buf[p->col++] = 8;
556 }
557 p->buf[p->col++] = c;
558 }
559
560 static void
561 encode(struct termp *p, const char *word, size_t sz)
562 {
563 size_t i;
564
565 if (TERMP_SKIPCHAR & p->flags) {
566 p->flags &= ~TERMP_SKIPCHAR;
567 return;
568 }
569
570 /*
571 * Encode and buffer a string of characters. If the current
572 * font mode is unset, buffer directly, else encode then buffer
573 * character by character.
574 */
575
576 if (TERMFONT_NONE == term_fonttop(p)) {
577 if (p->col + sz >= p->maxcols)
578 adjbuf(p, p->col + sz);
579 for (i = 0; i < sz; i++)
580 p->buf[p->col++] = word[i];
581 return;
582 }
583
584 /* Pre-buffer, assuming worst-case. */
585
586 if (p->col + 1 + (sz * 5) >= p->maxcols)
587 adjbuf(p, p->col + 1 + (sz * 5));
588
589 for (i = 0; i < sz; i++) {
590 if (ASCII_HYPH == word[i] ||
591 isgraph((unsigned char)word[i]))
592 encode1(p, word[i]);
593 else
594 p->buf[p->col++] = word[i];
595 }
596 }
597
598 void
599 term_setwidth(struct termp *p, const char *wstr)
600 {
601 struct roffsu su;
602 size_t width;
603 int iop;
604
605 iop = 0;
606 width = 0;
607 if (NULL != wstr) {
608 switch (*wstr) {
609 case '+':
610 iop = 1;
611 wstr++;
612 break;
613 case '-':
614 iop = -1;
615 wstr++;
616 break;
617 default:
618 break;
619 }
620 if (a2roffsu(wstr, &su, SCALE_MAX))
621 width = term_hspan(p, &su);
622 else
623 iop = 0;
624 }
625 (*p->setwidth)(p, iop, width);
626 }
627
628 size_t
629 term_len(const struct termp *p, size_t sz)
630 {
631
632 return((*p->width)(p, ' ') * sz);
633 }
634
635 static size_t
636 cond_width(const struct termp *p, int c, int *skip)
637 {
638
639 if (*skip) {
640 (*skip) = 0;
641 return(0);
642 } else
643 return((*p->width)(p, c));
644 }
645
646 size_t
647 term_strlen(const struct termp *p, const char *cp)
648 {
649 size_t sz, rsz, i;
650 int ssz, skip, c;
651 const char *seq, *rhs;
652 enum mandoc_esc esc;
653 static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
654 ASCII_BREAK, '\0' };
655
656 /*
657 * Account for escaped sequences within string length
658 * calculations. This follows the logic in term_word() as we
659 * must calculate the width of produced strings.
660 */
661
662 sz = 0;
663 skip = 0;
664 while ('\0' != *cp) {
665 rsz = strcspn(cp, rej);
666 for (i = 0; i < rsz; i++)
667 sz += cond_width(p, *cp++, &skip);
668
669 switch (*cp) {
670 case '\\':
671 cp++;
672 esc = mandoc_escape(&cp, &seq, &ssz);
673 if (ESCAPE_ERROR == esc)
674 continue;
675
676 rhs = NULL;
677
678 switch (esc) {
679 case ESCAPE_UNICODE:
680 c = mchars_num2uc(seq + 1, sz - 1);
681 if (p->enc == TERMENC_ASCII) {
682 rhs = ascii_uc2str(c);
683 rsz = strlen(rhs);
684 } else
685 sz += cond_width(p, c, &skip);
686 break;
687 case ESCAPE_NUMBERED:
688 c = mchars_num2char(seq, ssz);
689 if ('\0' != c)
690 sz += cond_width(p, c, &skip);
691 break;
692 case ESCAPE_SPECIAL:
693 if (p->enc == TERMENC_ASCII) {
694 rhs = mchars_spec2str(p->symtab,
695 seq, ssz, &rsz);
696 if (rhs == NULL) {
697 rhs = "<?>";
698 rsz = 3;
699 }
700 } else {
701 c = mchars_spec2cp(p->symtab,
702 seq, ssz);
703 if (c > 0)
704 sz += cond_width(p, c, &skip);
705 }
706 break;
707 case ESCAPE_SKIPCHAR:
708 skip = 1;
709 break;
710 default:
711 break;
712 }
713
714 if (NULL == rhs)
715 break;
716
717 if (skip) {
718 skip = 0;
719 break;
720 }
721
722 for (i = 0; i < rsz; i++)
723 sz += (*p->width)(p, *rhs++);
724 break;
725 case ASCII_NBRSP:
726 sz += cond_width(p, ' ', &skip);
727 cp++;
728 break;
729 case ASCII_HYPH:
730 sz += cond_width(p, '-', &skip);
731 cp++;
732 /* FALLTHROUGH */
733 case ASCII_BREAK:
734 break;
735 default:
736 break;
737 }
738 }
739
740 return(sz);
741 }
742
743 size_t
744 term_vspan(const struct termp *p, const struct roffsu *su)
745 {
746 double r;
747
748 switch (su->unit) {
749 case SCALE_CM:
750 r = su->scale * 2.0;
751 break;
752 case SCALE_IN:
753 r = su->scale * 6.0;
754 break;
755 case SCALE_PC:
756 r = su->scale;
757 break;
758 case SCALE_PT:
759 r = su->scale / 8.0;
760 break;
761 case SCALE_MM:
762 r = su->scale / 1000.0;
763 break;
764 case SCALE_VS:
765 r = su->scale;
766 break;
767 default:
768 r = su->scale - 1.0;
769 break;
770 }
771
772 if (r < 0.0)
773 r = 0.0;
774 return((size_t)(r + 0.0005));
775 }
776
777 size_t
778 term_hspan(const struct termp *p, const struct roffsu *su)
779 {
780 double v;
781
782 v = (*p->hspan)(p, su);
783 if (v < 0.0)
784 v = 0.0;
785 return((size_t)(v + 0.0005));
786 }