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