]> git.cameronkatri.com Git - mandoc.git/blob - term.c
Improve -Tascii output for Unicode escape sequences: For the first 512
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.229 2014/10/26 17:12:03 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 if (p->buf)
46 free(p->buf);
47 if (p->symtab)
48 mchars_free(p->symtab);
49
50 free(p);
51 }
52
53 void
54 term_begin(struct termp *p, term_margin head,
55 term_margin foot, const void *arg)
56 {
57
58 p->headf = head;
59 p->footf = foot;
60 p->argf = arg;
61 (*p->begin)(p);
62 }
63
64 void
65 term_end(struct termp *p)
66 {
67
68 (*p->end)(p);
69 }
70
71 /*
72 * Flush a chunk of text. By default, break the output line each time
73 * the right margin is reached, and continue output on the next line
74 * at the same offset as the chunk itself. By default, also break the
75 * output line at the end of the chunk.
76 * The following flags may be specified:
77 *
78 * - TERMP_NOBREAK: Do not break the output line at the right margin,
79 * but only at the max right margin. Also, do not break the output
80 * line at the end of the chunk, such that the next call can pad to
81 * the next column. However, if less than p->trailspace blanks,
82 * which can be 0, 1, or 2, remain to the right margin, the line
83 * will be broken.
84 * - TERMP_BRIND: If the chunk does not fit and the output line has
85 * to be broken, start the next line at the right margin instead
86 * of at the offset. Used together with TERMP_NOBREAK for the tags
87 * in various kinds of tagged lists.
88 * - TERMP_DANGLE: Do not break the output line at the right margin,
89 * append the next chunk after it even if this one is too long.
90 * To be used together with TERMP_NOBREAK.
91 * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
92 * the next chunk if this column is not full.
93 */
94 void
95 term_flushln(struct termp *p)
96 {
97 size_t i; /* current input position in p->buf */
98 int ntab; /* number of tabs to prepend */
99 size_t vis; /* current visual position on output */
100 size_t vbl; /* number of blanks to prepend to output */
101 size_t vend; /* end of word visual position on output */
102 size_t bp; /* visual right border position */
103 size_t dv; /* temporary for visual pos calculations */
104 size_t j; /* temporary loop index for p->buf */
105 size_t jhy; /* last hyph before overflow w/r/t j */
106 size_t maxvis; /* output position of visible boundary */
107 size_t mmax; /* used in calculating bp */
108
109 /*
110 * First, establish the maximum columns of "visible" content.
111 * This is usually the difference between the right-margin and
112 * an indentation, but can be, for tagged lists or columns, a
113 * small set of values.
114 *
115 * The following unsigned-signed subtractions look strange,
116 * but they are actually correct. If the int p->overstep
117 * is negative, it gets sign extended. Subtracting that
118 * very large size_t effectively adds a small number to dv.
119 */
120 assert (p->rmargin >= p->offset);
121 dv = p->rmargin - p->offset;
122 maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
123 dv = p->maxrmargin - p->offset;
124 mmax = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
125
126 bp = TERMP_NOBREAK & p->flags ? mmax : 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 = p->rmargin;
196 vend += p->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)
262 vis -= vbl;
263
264 p->col = 0;
265 p->overstep = 0;
266
267 if ( ! (TERMP_NOBREAK & p->flags)) {
268 p->viscol = 0;
269 (*p->endline)(p);
270 return;
271 }
272
273 if (TERMP_HANG & p->flags) {
274 p->overstep = (int)(vis - maxvis +
275 p->trailspace * (*p->width)(p, ' '));
276
277 /*
278 * If we have overstepped the margin, temporarily move
279 * it to the right and flag the rest of the line to be
280 * shorter.
281 * If there is a request to keep the columns together,
282 * allow negative overstep when the column is not full.
283 */
284 if (p->trailspace && p->overstep < 0)
285 p->overstep = 0;
286 return;
287
288 } else if (TERMP_DANGLE & p->flags)
289 return;
290
291 /* If the column was overrun, break the line. */
292 if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
293 (*p->endline)(p);
294 p->viscol = 0;
295 }
296 }
297
298 /*
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
301 * assertion.
302 */
303 void
304 term_newln(struct termp *p)
305 {
306
307 p->flags |= TERMP_NOSPACE;
308 if (p->col || p->viscol)
309 term_flushln(p);
310 }
311
312 /*
313 * Asserts a vertical space (a full, empty line-break between lines).
314 * Note that if used twice, this will cause two blank spaces and so on.
315 * All data in the output buffer is flushed prior to the newline
316 * assertion.
317 */
318 void
319 term_vspace(struct termp *p)
320 {
321
322 term_newln(p);
323 p->viscol = 0;
324 if (0 < p->skipvsp)
325 p->skipvsp--;
326 else
327 (*p->endline)(p);
328 }
329
330 void
331 term_fontlast(struct termp *p)
332 {
333 enum termfont f;
334
335 f = p->fontl;
336 p->fontl = p->fontq[p->fonti];
337 p->fontq[p->fonti] = f;
338 }
339
340 void
341 term_fontrepl(struct termp *p, enum termfont f)
342 {
343
344 p->fontl = p->fontq[p->fonti];
345 p->fontq[p->fonti] = f;
346 }
347
348 void
349 term_fontpush(struct termp *p, enum termfont f)
350 {
351
352 assert(p->fonti + 1 < 10);
353 p->fontl = p->fontq[p->fonti];
354 p->fontq[++p->fonti] = f;
355 }
356
357 const void *
358 term_fontq(struct termp *p)
359 {
360
361 return(&p->fontq[p->fonti]);
362 }
363
364 enum termfont
365 term_fonttop(struct termp *p)
366 {
367
368 return(p->fontq[p->fonti]);
369 }
370
371 void
372 term_fontpopq(struct termp *p, const void *key)
373 {
374
375 while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti))
376 p->fonti--;
377 assert(p->fonti >= 0);
378 }
379
380 void
381 term_fontpop(struct termp *p)
382 {
383
384 assert(p->fonti);
385 p->fonti--;
386 }
387
388 /*
389 * Handle pwords, partial words, which may be either a single word or a
390 * phrase that cannot be broken down (such as a literal string). This
391 * handles word styling.
392 */
393 void
394 term_word(struct termp *p, const char *word)
395 {
396 const char nbrsp[2] = { ASCII_NBRSP, 0 };
397 const char *seq, *cp;
398 char c;
399 int sz, uc;
400 size_t ssz;
401 enum mandoc_esc esc;
402
403 if ( ! (TERMP_NOSPACE & p->flags)) {
404 if ( ! (TERMP_KEEP & p->flags)) {
405 bufferc(p, ' ');
406 if (TERMP_SENTENCE & p->flags)
407 bufferc(p, ' ');
408 } else
409 bufferc(p, ASCII_NBRSP);
410 }
411 if (TERMP_PREKEEP & p->flags)
412 p->flags |= TERMP_KEEP;
413
414 if ( ! (p->flags & TERMP_NONOSPACE))
415 p->flags &= ~TERMP_NOSPACE;
416 else
417 p->flags |= TERMP_NOSPACE;
418
419 p->flags &= ~TERMP_SENTENCE;
420
421 while ('\0' != *word) {
422 if ('\\' != *word) {
423 if (TERMP_SKIPCHAR & p->flags) {
424 p->flags &= ~TERMP_SKIPCHAR;
425 word++;
426 continue;
427 }
428 if (TERMP_NBRWORD & p->flags) {
429 if (' ' == *word) {
430 encode(p, nbrsp, 1);
431 word++;
432 continue;
433 }
434 ssz = strcspn(word, "\\ ");
435 } else
436 ssz = strcspn(word, "\\");
437 encode(p, word, ssz);
438 word += (int)ssz;
439 continue;
440 }
441
442 word++;
443 esc = mandoc_escape(&word, &seq, &sz);
444 if (ESCAPE_ERROR == esc)
445 continue;
446
447 switch (esc) {
448 case ESCAPE_UNICODE:
449 uc = mchars_num2uc(seq + 1, sz - 1);
450 if (p->enc == TERMENC_ASCII) {
451 cp = ascii_uc2str(uc);
452 encode(p, cp, strlen(cp));
453 } else
454 encode1(p, uc);
455 break;
456 case ESCAPE_NUMBERED:
457 c = mchars_num2char(seq, sz);
458 if ('\0' != c)
459 encode(p, &c, 1);
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, "<?>", 3);
467 else
468 encode(p, cp, ssz);
469 } else {
470 uc = mchars_spec2cp(p->symtab, seq, sz);
471 if (uc <= 0)
472 uc = 0xFFFD;
473 encode1(p, uc);
474 }
475 break;
476 case ESCAPE_FONTBOLD:
477 term_fontrepl(p, TERMFONT_BOLD);
478 break;
479 case ESCAPE_FONTITALIC:
480 term_fontrepl(p, TERMFONT_UNDER);
481 break;
482 case ESCAPE_FONTBI:
483 term_fontrepl(p, TERMFONT_BI);
484 break;
485 case ESCAPE_FONT:
486 /* FALLTHROUGH */
487 case ESCAPE_FONTROMAN:
488 term_fontrepl(p, TERMFONT_NONE);
489 break;
490 case ESCAPE_FONTPREV:
491 term_fontlast(p);
492 break;
493 case ESCAPE_NOSPACE:
494 if (TERMP_SKIPCHAR & p->flags)
495 p->flags &= ~TERMP_SKIPCHAR;
496 else if ('\0' == *word)
497 p->flags |= TERMP_NOSPACE;
498 break;
499 case ESCAPE_SKIPCHAR:
500 p->flags |= TERMP_SKIPCHAR;
501 break;
502 default:
503 break;
504 }
505 }
506 p->flags &= ~TERMP_NBRWORD;
507 }
508
509 static void
510 adjbuf(struct termp *p, size_t sz)
511 {
512
513 if (0 == p->maxcols)
514 p->maxcols = 1024;
515 while (sz >= p->maxcols)
516 p->maxcols <<= 2;
517
518 p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
519 }
520
521 static void
522 bufferc(struct termp *p, char c)
523 {
524
525 if (p->col + 1 >= p->maxcols)
526 adjbuf(p, p->col + 1);
527
528 p->buf[p->col++] = c;
529 }
530
531 /*
532 * See encode().
533 * Do this for a single (probably unicode) value.
534 * Does not check for non-decorated glyphs.
535 */
536 static void
537 encode1(struct termp *p, int c)
538 {
539 enum termfont f;
540
541 if (TERMP_SKIPCHAR & p->flags) {
542 p->flags &= ~TERMP_SKIPCHAR;
543 return;
544 }
545
546 if (p->col + 6 >= p->maxcols)
547 adjbuf(p, p->col + 6);
548
549 f = term_fonttop(p);
550
551 if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
552 p->buf[p->col++] = '_';
553 p->buf[p->col++] = 8;
554 }
555 if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
556 if (ASCII_HYPH == c)
557 p->buf[p->col++] = '-';
558 else
559 p->buf[p->col++] = c;
560 p->buf[p->col++] = 8;
561 }
562 p->buf[p->col++] = c;
563 }
564
565 static void
566 encode(struct termp *p, const char *word, size_t sz)
567 {
568 size_t i;
569
570 if (TERMP_SKIPCHAR & p->flags) {
571 p->flags &= ~TERMP_SKIPCHAR;
572 return;
573 }
574
575 /*
576 * Encode and buffer a string of characters. If the current
577 * font mode is unset, buffer directly, else encode then buffer
578 * character by character.
579 */
580
581 if (TERMFONT_NONE == term_fonttop(p)) {
582 if (p->col + sz >= p->maxcols)
583 adjbuf(p, p->col + sz);
584 for (i = 0; i < sz; i++)
585 p->buf[p->col++] = word[i];
586 return;
587 }
588
589 /* Pre-buffer, assuming worst-case. */
590
591 if (p->col + 1 + (sz * 5) >= p->maxcols)
592 adjbuf(p, p->col + 1 + (sz * 5));
593
594 for (i = 0; i < sz; i++) {
595 if (ASCII_HYPH == word[i] ||
596 isgraph((unsigned char)word[i]))
597 encode1(p, word[i]);
598 else
599 p->buf[p->col++] = word[i];
600 }
601 }
602
603 void
604 term_setwidth(struct termp *p, const char *wstr)
605 {
606 struct roffsu su;
607 size_t width;
608 int iop;
609
610 iop = 0;
611 width = 0;
612 if (NULL != wstr) {
613 switch (*wstr) {
614 case '+':
615 iop = 1;
616 wstr++;
617 break;
618 case '-':
619 iop = -1;
620 wstr++;
621 break;
622 default:
623 break;
624 }
625 if (a2roffsu(wstr, &su, SCALE_MAX))
626 width = term_hspan(p, &su);
627 else
628 iop = 0;
629 }
630 (*p->setwidth)(p, iop, width);
631 }
632
633 size_t
634 term_len(const struct termp *p, size_t sz)
635 {
636
637 return((*p->width)(p, ' ') * sz);
638 }
639
640 static size_t
641 cond_width(const struct termp *p, int c, int *skip)
642 {
643
644 if (*skip) {
645 (*skip) = 0;
646 return(0);
647 } else
648 return((*p->width)(p, c));
649 }
650
651 size_t
652 term_strlen(const struct termp *p, const char *cp)
653 {
654 size_t sz, rsz, i;
655 int ssz, skip, c;
656 const char *seq, *rhs;
657 enum mandoc_esc esc;
658 static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
659 ASCII_BREAK, '\0' };
660
661 /*
662 * Account for escaped sequences within string length
663 * calculations. This follows the logic in term_word() as we
664 * must calculate the width of produced strings.
665 */
666
667 sz = 0;
668 skip = 0;
669 while ('\0' != *cp) {
670 rsz = strcspn(cp, rej);
671 for (i = 0; i < rsz; i++)
672 sz += cond_width(p, *cp++, &skip);
673
674 switch (*cp) {
675 case '\\':
676 cp++;
677 esc = mandoc_escape(&cp, &seq, &ssz);
678 if (ESCAPE_ERROR == esc)
679 continue;
680
681 rhs = NULL;
682
683 switch (esc) {
684 case ESCAPE_UNICODE:
685 c = mchars_num2uc(seq + 1, sz - 1);
686 if (p->enc == TERMENC_ASCII) {
687 rhs = ascii_uc2str(c);
688 rsz = strlen(rhs);
689 } else
690 sz += cond_width(p, c, &skip);
691 break;
692 case ESCAPE_NUMBERED:
693 c = mchars_num2char(seq, ssz);
694 if ('\0' != c)
695 sz += cond_width(p, c, &skip);
696 break;
697 case ESCAPE_SPECIAL:
698 if (p->enc == TERMENC_ASCII) {
699 rhs = mchars_spec2str(p->symtab,
700 seq, ssz, &rsz);
701 if (rhs == NULL) {
702 rhs = "<?>";
703 rsz = 3;
704 }
705 } else {
706 c = mchars_spec2cp(p->symtab,
707 seq, ssz);
708 if (c <= 0)
709 c = 0xFFFD;
710 sz += cond_width(p, c, &skip);
711 }
712 break;
713 case ESCAPE_SKIPCHAR:
714 skip = 1;
715 break;
716 default:
717 break;
718 }
719
720 if (NULL == rhs)
721 break;
722
723 if (skip) {
724 skip = 0;
725 break;
726 }
727
728 for (i = 0; i < rsz; i++)
729 sz += (*p->width)(p, *rhs++);
730 break;
731 case ASCII_NBRSP:
732 sz += cond_width(p, ' ', &skip);
733 cp++;
734 break;
735 case ASCII_HYPH:
736 sz += cond_width(p, '-', &skip);
737 cp++;
738 /* FALLTHROUGH */
739 case ASCII_BREAK:
740 break;
741 default:
742 break;
743 }
744 }
745
746 return(sz);
747 }
748
749 size_t
750 term_vspan(const struct termp *p, const struct roffsu *su)
751 {
752 double r;
753
754 switch (su->unit) {
755 case SCALE_CM:
756 r = su->scale * 2.0;
757 break;
758 case SCALE_IN:
759 r = su->scale * 6.0;
760 break;
761 case SCALE_PC:
762 r = su->scale;
763 break;
764 case SCALE_PT:
765 r = su->scale / 8.0;
766 break;
767 case SCALE_MM:
768 r = su->scale / 1000.0;
769 break;
770 case SCALE_VS:
771 r = su->scale;
772 break;
773 default:
774 r = su->scale - 1.0;
775 break;
776 }
777
778 if (r < 0.0)
779 r = 0.0;
780 return((size_t)(r + 0.0005));
781 }
782
783 size_t
784 term_hspan(const struct termp *p, const struct roffsu *su)
785 {
786 double v;
787
788 v = (*p->hspan)(p, su);
789 if (v < 0.0)
790 v = 0.0;
791 return((size_t)(v + 0.0005));
792 }