]> git.cameronkatri.com Git - mandoc.git/blob - term.c
b97b9be3956ef47dc09832b3c1cc9cd4cf7e3c9a
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.269 2017/06/12 19:05:47 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2017 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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_col *, 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 static void endline(struct termp *);
40
41
42 void
43 term_setcol(struct termp *p, size_t maxtcol)
44 {
45 if (maxtcol > p->maxtcol) {
46 p->tcols = mandoc_recallocarray(p->tcols,
47 p->maxtcol, maxtcol, sizeof(*p->tcols));
48 p->maxtcol = maxtcol;
49 }
50 p->lasttcol = maxtcol - 1;
51 p->tcol = p->tcols;
52 }
53
54 void
55 term_free(struct termp *p)
56 {
57 for (p->tcol = p->tcols; p->tcol < p->tcols + p->maxtcol; p->tcol++)
58 free(p->tcol->buf);
59 free(p->tcols);
60 free(p->fontq);
61 free(p);
62 }
63
64 void
65 term_begin(struct termp *p, term_margin head,
66 term_margin foot, const struct roff_meta *arg)
67 {
68
69 p->headf = head;
70 p->footf = foot;
71 p->argf = arg;
72 (*p->begin)(p);
73 }
74
75 void
76 term_end(struct termp *p)
77 {
78
79 (*p->end)(p);
80 }
81
82 /*
83 * Flush a chunk of text. By default, break the output line each time
84 * the right margin is reached, and continue output on the next line
85 * at the same offset as the chunk itself. By default, also break the
86 * output line at the end of the chunk.
87 * The following flags may be specified:
88 *
89 * - TERMP_NOBREAK: Do not break the output line at the right margin,
90 * but only at the max right margin. Also, do not break the output
91 * line at the end of the chunk, such that the next call can pad to
92 * the next column. However, if less than p->trailspace blanks,
93 * which can be 0, 1, or 2, remain to the right margin, the line
94 * will be broken.
95 * - TERMP_BRTRSP: Consider trailing whitespace significant
96 * when deciding whether the chunk fits or not.
97 * - TERMP_BRIND: If the chunk does not fit and the output line has
98 * to be broken, start the next line at the right margin instead
99 * of at the offset. Used together with TERMP_NOBREAK for the tags
100 * in various kinds of tagged lists.
101 * - TERMP_HANG: Do not break the output line at the right margin,
102 * append the next chunk after it even if this one is too long.
103 * To be used together with TERMP_NOBREAK.
104 * - TERMP_NOPAD: Start writing at the current position,
105 * do not pad with blank characters up to the offset.
106 */
107 void
108 term_flushln(struct termp *p)
109 {
110 size_t vis; /* current visual position on output */
111 size_t vbl; /* number of blanks to prepend to output */
112 size_t vend; /* end of word visual position on output */
113 size_t bp; /* visual right border position */
114 size_t dv; /* temporary for visual pos calculations */
115 size_t j; /* temporary loop index for p->tcol->buf */
116 size_t jhy; /* last hyph before overflow w/r/t j */
117 size_t maxvis; /* output position of visible boundary */
118 int ntab; /* number of tabs to prepend */
119
120 vbl = (p->flags & TERMP_NOPAD) || p->tcol->offset < p->viscol ?
121 0 : p->tcol->offset - p->viscol;
122 if (p->minbl && vbl < p->minbl)
123 vbl = p->minbl;
124 maxvis = p->tcol->rmargin > p->viscol + vbl ?
125 p->tcol->rmargin - p->viscol - vbl : 0;
126 bp = !(p->flags & TERMP_NOBREAK) ? maxvis :
127 p->maxrmargin > p->viscol + vbl ?
128 p->maxrmargin - p->viscol - vbl : 0;
129 vis = vend = 0;
130
131 if ((p->flags && TERMP_MULTICOL) == 0)
132 p->tcol->col = 0;
133 while (p->tcol->col < p->tcol->lastcol) {
134
135 /*
136 * Handle literal tab characters: collapse all
137 * subsequent tabs into a single huge set of spaces.
138 */
139
140 ntab = 0;
141 while (p->tcol->col < p->tcol->lastcol &&
142 p->tcol->buf[p->tcol->col] == '\t') {
143 vend = term_tab_next(vis);
144 vbl += vend - vis;
145 vis = vend;
146 ntab++;
147 p->tcol->col++;
148 }
149
150 /*
151 * Count up visible word characters. Control sequences
152 * (starting with the CSI) aren't counted. A space
153 * generates a non-printing word, which is valid (the
154 * space is printed according to regular spacing rules).
155 */
156
157 jhy = 0;
158 for (j = p->tcol->col; j < p->tcol->lastcol; j++) {
159 if (p->tcol->buf[j] == ' ' || p->tcol->buf[j] == '\t')
160 break;
161
162 /* Back over the last printed character. */
163 if (p->tcol->buf[j] == '\b') {
164 assert(j);
165 vend -= (*p->width)(p, p->tcol->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 (p->tcol->buf[j] == ASCII_HYPH||
173 p->tcol->buf[j] == ASCII_BREAK))
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 (p->tcol->buf[j] == ASCII_HYPH)
181 p->tcol->buf[j] = '-';
182
183 vend += (*p->width)(p, p->tcol->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
191 if (vend > bp && jhy == 0 && vis > 0 &&
192 (p->flags & TERMP_BRNEVER) == 0) {
193 if (p->flags & TERMP_MULTICOL)
194 return;
195
196 endline(p);
197 vend -= vis;
198
199 /* Use pending tabs on the new line. */
200
201 vbl = 0;
202 while (ntab--)
203 vbl = term_tab_next(vbl);
204
205 /* Re-establish indentation. */
206
207 if (p->flags & TERMP_BRIND)
208 vbl += p->tcol->rmargin;
209 else
210 vbl += p->tcol->offset;
211 maxvis = p->tcol->rmargin > vbl ?
212 p->tcol->rmargin - vbl : 0;
213 bp = !(p->flags & TERMP_NOBREAK) ? maxvis :
214 p->maxrmargin > vbl ? p->maxrmargin - vbl : 0;
215 }
216
217 /*
218 * Write out the rest of the word.
219 */
220
221 for ( ; p->tcol->col < p->tcol->lastcol; p->tcol->col++) {
222 if (vend > bp && jhy > 0 && p->tcol->col > jhy)
223 break;
224 if (p->tcol->buf[p->tcol->col] == '\t')
225 break;
226 if (p->tcol->buf[p->tcol->col] == ' ') {
227 j = p->tcol->col;
228 while (p->tcol->col < p->tcol->lastcol &&
229 p->tcol->buf[p->tcol->col] == ' ')
230 p->tcol->col++;
231 dv = (p->tcol->col - j) * (*p->width)(p, ' ');
232 vbl += dv;
233 vend += dv;
234 break;
235 }
236 if (p->tcol->buf[p->tcol->col] == ASCII_NBRSP) {
237 vbl += (*p->width)(p, ' ');
238 continue;
239 }
240 if (p->tcol->buf[p->tcol->col] == ASCII_BREAK)
241 continue;
242
243 /*
244 * Now we definitely know there will be
245 * printable characters to output,
246 * so write preceding white space now.
247 */
248 if (vbl) {
249 (*p->advance)(p, vbl);
250 p->viscol += vbl;
251 vbl = 0;
252 }
253
254 (*p->letter)(p, p->tcol->buf[p->tcol->col]);
255 if (p->tcol->buf[p->tcol->col] == '\b')
256 p->viscol -= (*p->width)(p,
257 p->tcol->buf[p->tcol->col - 1]);
258 else
259 p->viscol += (*p->width)(p,
260 p->tcol->buf[p->tcol->col]);
261 }
262 vis = vend;
263 }
264
265 /*
266 * If there was trailing white space, it was not printed;
267 * so reset the cursor position accordingly.
268 */
269
270 if (vis > vbl)
271 vis -= vbl;
272 else
273 vis = 0;
274
275 p->col = p->tcol->col = p->tcol->lastcol = 0;
276 p->minbl = p->trailspace;
277 p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE | TERMP_NOPAD);
278
279 if (p->flags & TERMP_MULTICOL)
280 return;
281
282 /* Trailing whitespace is significant in some columns. */
283
284 if (vis && vbl && (TERMP_BRTRSP & p->flags))
285 vis += vbl;
286
287 /* If the column was overrun, break the line. */
288 if ((p->flags & TERMP_NOBREAK) == 0 ||
289 ((p->flags & TERMP_HANG) == 0 &&
290 vis + p->trailspace * (*p->width)(p, ' ') > maxvis))
291 endline(p);
292 }
293
294 static void
295 endline(struct termp *p)
296 {
297 if ((p->flags & (TERMP_NEWMC | TERMP_ENDMC)) == TERMP_ENDMC) {
298 p->mc = NULL;
299 p->flags &= ~TERMP_ENDMC;
300 }
301 if (p->mc != NULL) {
302 if (p->viscol && p->maxrmargin >= p->viscol)
303 (*p->advance)(p, p->maxrmargin - p->viscol + 1);
304 p->flags |= TERMP_NOBUF | TERMP_NOSPACE;
305 term_word(p, p->mc);
306 p->flags &= ~(TERMP_NOBUF | TERMP_NEWMC);
307 }
308 p->viscol = 0;
309 p->minbl = 0;
310 (*p->endline)(p);
311 }
312
313 /*
314 * A newline only breaks an existing line; it won't assert vertical
315 * space. All data in the output buffer is flushed prior to the newline
316 * assertion.
317 */
318 void
319 term_newln(struct termp *p)
320 {
321
322 p->flags |= TERMP_NOSPACE;
323 if (p->tcol->lastcol || p->viscol)
324 term_flushln(p);
325 }
326
327 /*
328 * Asserts a vertical space (a full, empty line-break between lines).
329 * Note that if used twice, this will cause two blank spaces and so on.
330 * All data in the output buffer is flushed prior to the newline
331 * assertion.
332 */
333 void
334 term_vspace(struct termp *p)
335 {
336
337 term_newln(p);
338 p->viscol = 0;
339 p->minbl = 0;
340 if (0 < p->skipvsp)
341 p->skipvsp--;
342 else
343 (*p->endline)(p);
344 }
345
346 /* Swap current and previous font; for \fP and .ft P */
347 void
348 term_fontlast(struct termp *p)
349 {
350 enum termfont f;
351
352 f = p->fontl;
353 p->fontl = p->fontq[p->fonti];
354 p->fontq[p->fonti] = f;
355 }
356
357 /* Set font, save current, discard previous; for \f, .ft, .B etc. */
358 void
359 term_fontrepl(struct termp *p, enum termfont f)
360 {
361
362 p->fontl = p->fontq[p->fonti];
363 p->fontq[p->fonti] = f;
364 }
365
366 /* Set font, save previous. */
367 void
368 term_fontpush(struct termp *p, enum termfont f)
369 {
370
371 p->fontl = p->fontq[p->fonti];
372 if (++p->fonti == p->fontsz) {
373 p->fontsz += 8;
374 p->fontq = mandoc_reallocarray(p->fontq,
375 p->fontsz, sizeof(*p->fontq));
376 }
377 p->fontq[p->fonti] = f;
378 }
379
380 /* Flush to make the saved pointer current again. */
381 void
382 term_fontpopq(struct termp *p, int i)
383 {
384
385 assert(i >= 0);
386 if (p->fonti > i)
387 p->fonti = i;
388 }
389
390 /* Pop one font off the stack. */
391 void
392 term_fontpop(struct termp *p)
393 {
394
395 assert(p->fonti);
396 p->fonti--;
397 }
398
399 /*
400 * Handle pwords, partial words, which may be either a single word or a
401 * phrase that cannot be broken down (such as a literal string). This
402 * handles word styling.
403 */
404 void
405 term_word(struct termp *p, const char *word)
406 {
407 struct roffsu su;
408 const char nbrsp[2] = { ASCII_NBRSP, 0 };
409 const char *seq, *cp;
410 int sz, uc;
411 size_t csz, lsz, ssz;
412 enum mandoc_esc esc;
413
414 if ((p->flags & TERMP_NOBUF) == 0) {
415 if ((p->flags & TERMP_NOSPACE) == 0) {
416 if ((p->flags & TERMP_KEEP) == 0) {
417 bufferc(p, ' ');
418 if (p->flags & TERMP_SENTENCE)
419 bufferc(p, ' ');
420 } else
421 bufferc(p, ASCII_NBRSP);
422 }
423 if (p->flags & TERMP_PREKEEP)
424 p->flags |= TERMP_KEEP;
425 if (p->flags & TERMP_NONOSPACE)
426 p->flags |= TERMP_NOSPACE;
427 else
428 p->flags &= ~TERMP_NOSPACE;
429 p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
430 p->skipvsp = 0;
431 }
432
433 while ('\0' != *word) {
434 if ('\\' != *word) {
435 if (TERMP_NBRWORD & p->flags) {
436 if (' ' == *word) {
437 encode(p, nbrsp, 1);
438 word++;
439 continue;
440 }
441 ssz = strcspn(word, "\\ ");
442 } else
443 ssz = strcspn(word, "\\");
444 encode(p, word, ssz);
445 word += (int)ssz;
446 continue;
447 }
448
449 word++;
450 esc = mandoc_escape(&word, &seq, &sz);
451 if (ESCAPE_ERROR == esc)
452 continue;
453
454 switch (esc) {
455 case ESCAPE_UNICODE:
456 uc = mchars_num2uc(seq + 1, sz - 1);
457 break;
458 case ESCAPE_NUMBERED:
459 uc = mchars_num2char(seq, sz);
460 if (uc < 0)
461 continue;
462 break;
463 case ESCAPE_SPECIAL:
464 if (p->enc == TERMENC_ASCII) {
465 cp = mchars_spec2str(seq, sz, &ssz);
466 if (cp != NULL)
467 encode(p, cp, ssz);
468 } else {
469 uc = mchars_spec2cp(seq, sz);
470 if (uc > 0)
471 encode1(p, uc);
472 }
473 continue;
474 case ESCAPE_FONTBOLD:
475 term_fontrepl(p, TERMFONT_BOLD);
476 continue;
477 case ESCAPE_FONTITALIC:
478 term_fontrepl(p, TERMFONT_UNDER);
479 continue;
480 case ESCAPE_FONTBI:
481 term_fontrepl(p, TERMFONT_BI);
482 continue;
483 case ESCAPE_FONT:
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 (p->flags & TERMP_BACKAFTER)
492 p->flags &= ~TERMP_BACKAFTER;
493 else if (*word == '\0')
494 p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
495 continue;
496 case ESCAPE_HORIZ:
497 if (a2roffsu(seq, &su, SCALE_EM) == NULL)
498 continue;
499 uc = term_hspan(p, &su) / 24;
500 if (uc > 0)
501 while (uc-- > 0)
502 bufferc(p, ASCII_NBRSP);
503 else if (p->col > (size_t)(-uc))
504 p->col += uc;
505 else {
506 uc += p->col;
507 p->col = 0;
508 if (p->tcol->offset > (size_t)(-uc)) {
509 p->ti += uc;
510 p->tcol->offset += uc;
511 } else {
512 p->ti -= p->tcol->offset;
513 p->tcol->offset = 0;
514 }
515 }
516 continue;
517 case ESCAPE_HLINE:
518 if ((seq = a2roffsu(seq, &su, SCALE_EM)) == NULL)
519 continue;
520 uc = term_hspan(p, &su) / 24;
521 if (uc <= 0) {
522 if (p->tcol->rmargin <= p->tcol->offset)
523 continue;
524 lsz = p->tcol->rmargin - p->tcol->offset;
525 } else
526 lsz = uc;
527 if (*seq == '\0')
528 uc = -1;
529 else if (*seq == '\\') {
530 seq++;
531 esc = mandoc_escape(&seq, &cp, &sz);
532 switch (esc) {
533 case ESCAPE_UNICODE:
534 uc = mchars_num2uc(cp + 1, sz - 1);
535 break;
536 case ESCAPE_NUMBERED:
537 uc = mchars_num2char(cp, sz);
538 break;
539 case ESCAPE_SPECIAL:
540 uc = mchars_spec2cp(cp, sz);
541 break;
542 default:
543 uc = -1;
544 break;
545 }
546 } else
547 uc = *seq;
548 if (uc < 0x20 || (uc > 0x7E && uc < 0xA0))
549 uc = '_';
550 if (p->enc == TERMENC_ASCII) {
551 cp = ascii_uc2str(uc);
552 csz = term_strlen(p, cp);
553 ssz = strlen(cp);
554 } else
555 csz = (*p->width)(p, uc);
556 while (lsz >= csz) {
557 if (p->enc == TERMENC_ASCII)
558 encode(p, cp, ssz);
559 else
560 encode1(p, uc);
561 lsz -= csz;
562 }
563 continue;
564 case ESCAPE_SKIPCHAR:
565 p->flags |= TERMP_BACKAFTER;
566 continue;
567 case ESCAPE_OVERSTRIKE:
568 cp = seq + sz;
569 while (seq < cp) {
570 if (*seq == '\\') {
571 mandoc_escape(&seq, NULL, NULL);
572 continue;
573 }
574 encode1(p, *seq++);
575 if (seq < cp) {
576 if (p->flags & TERMP_BACKBEFORE)
577 p->flags |= TERMP_BACKAFTER;
578 else
579 p->flags |= TERMP_BACKBEFORE;
580 }
581 }
582 /* Trim trailing backspace/blank pair. */
583 if (p->tcol->lastcol > 2 &&
584 (p->tcol->buf[p->tcol->lastcol - 1] == ' ' ||
585 p->tcol->buf[p->tcol->lastcol - 1] == '\t'))
586 p->tcol->lastcol -= 2;
587 if (p->col > p->tcol->lastcol)
588 p->col = p->tcol->lastcol;
589 continue;
590 default:
591 continue;
592 }
593
594 /*
595 * Common handling for Unicode and numbered
596 * character escape sequences.
597 */
598
599 if (p->enc == TERMENC_ASCII) {
600 cp = ascii_uc2str(uc);
601 encode(p, cp, strlen(cp));
602 } else {
603 if ((uc < 0x20 && uc != 0x09) ||
604 (uc > 0x7E && uc < 0xA0))
605 uc = 0xFFFD;
606 encode1(p, uc);
607 }
608 }
609 p->flags &= ~TERMP_NBRWORD;
610 }
611
612 static void
613 adjbuf(struct termp_col *c, size_t sz)
614 {
615 if (c->maxcols == 0)
616 c->maxcols = 1024;
617 while (c->maxcols <= sz)
618 c->maxcols <<= 2;
619 c->buf = mandoc_reallocarray(c->buf, c->maxcols, sizeof(*c->buf));
620 }
621
622 static void
623 bufferc(struct termp *p, char c)
624 {
625 if (p->flags & TERMP_NOBUF) {
626 (*p->letter)(p, c);
627 return;
628 }
629 if (p->col + 1 >= p->tcol->maxcols)
630 adjbuf(p->tcol, p->col + 1);
631 if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
632 p->tcol->buf[p->col] = c;
633 if (p->tcol->lastcol < ++p->col)
634 p->tcol->lastcol = p->col;
635 }
636
637 /*
638 * See encode().
639 * Do this for a single (probably unicode) value.
640 * Does not check for non-decorated glyphs.
641 */
642 static void
643 encode1(struct termp *p, int c)
644 {
645 enum termfont f;
646
647 if (p->flags & TERMP_NOBUF) {
648 (*p->letter)(p, c);
649 return;
650 }
651
652 if (p->col + 7 >= p->tcol->maxcols)
653 adjbuf(p->tcol, p->col + 7);
654
655 f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
656 p->fontq[p->fonti] : TERMFONT_NONE;
657
658 if (p->flags & TERMP_BACKBEFORE) {
659 if (p->tcol->buf[p->col - 1] == ' ' ||
660 p->tcol->buf[p->col - 1] == '\t')
661 p->col--;
662 else
663 p->tcol->buf[p->col++] = '\b';
664 p->flags &= ~TERMP_BACKBEFORE;
665 }
666 if (f == TERMFONT_UNDER || f == TERMFONT_BI) {
667 p->tcol->buf[p->col++] = '_';
668 p->tcol->buf[p->col++] = '\b';
669 }
670 if (f == TERMFONT_BOLD || f == TERMFONT_BI) {
671 if (c == ASCII_HYPH)
672 p->tcol->buf[p->col++] = '-';
673 else
674 p->tcol->buf[p->col++] = c;
675 p->tcol->buf[p->col++] = '\b';
676 }
677 if (p->tcol->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP))
678 p->tcol->buf[p->col] = c;
679 if (p->tcol->lastcol < ++p->col)
680 p->tcol->lastcol = p->col;
681 if (p->flags & TERMP_BACKAFTER) {
682 p->flags |= TERMP_BACKBEFORE;
683 p->flags &= ~TERMP_BACKAFTER;
684 }
685 }
686
687 static void
688 encode(struct termp *p, const char *word, size_t sz)
689 {
690 size_t i;
691
692 if (p->flags & TERMP_NOBUF) {
693 for (i = 0; i < sz; i++)
694 (*p->letter)(p, word[i]);
695 return;
696 }
697
698 if (p->col + 2 + (sz * 5) >= p->tcol->maxcols)
699 adjbuf(p->tcol, p->col + 2 + (sz * 5));
700
701 for (i = 0; i < sz; i++) {
702 if (ASCII_HYPH == word[i] ||
703 isgraph((unsigned char)word[i]))
704 encode1(p, word[i]);
705 else {
706 if (p->tcol->lastcol <= p->col ||
707 (word[i] != ' ' && word[i] != ASCII_NBRSP))
708 p->tcol->buf[p->col] = word[i];
709 p->col++;
710
711 /*
712 * Postpone the effect of \z while handling
713 * an overstrike sequence from ascii_uc2str().
714 */
715
716 if (word[i] == '\b' &&
717 (p->flags & TERMP_BACKBEFORE)) {
718 p->flags &= ~TERMP_BACKBEFORE;
719 p->flags |= TERMP_BACKAFTER;
720 }
721 }
722 }
723 if (p->tcol->lastcol < p->col)
724 p->tcol->lastcol = p->col;
725 }
726
727 void
728 term_setwidth(struct termp *p, const char *wstr)
729 {
730 struct roffsu su;
731 int iop, width;
732
733 iop = 0;
734 width = 0;
735 if (NULL != wstr) {
736 switch (*wstr) {
737 case '+':
738 iop = 1;
739 wstr++;
740 break;
741 case '-':
742 iop = -1;
743 wstr++;
744 break;
745 default:
746 break;
747 }
748 if (a2roffsu(wstr, &su, SCALE_MAX) != NULL)
749 width = term_hspan(p, &su);
750 else
751 iop = 0;
752 }
753 (*p->setwidth)(p, iop, width);
754 }
755
756 size_t
757 term_len(const struct termp *p, size_t sz)
758 {
759
760 return (*p->width)(p, ' ') * sz;
761 }
762
763 static size_t
764 cond_width(const struct termp *p, int c, int *skip)
765 {
766
767 if (*skip) {
768 (*skip) = 0;
769 return 0;
770 } else
771 return (*p->width)(p, c);
772 }
773
774 size_t
775 term_strlen(const struct termp *p, const char *cp)
776 {
777 size_t sz, rsz, i;
778 int ssz, skip, uc;
779 const char *seq, *rhs;
780 enum mandoc_esc esc;
781 static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
782 ASCII_BREAK, '\0' };
783
784 /*
785 * Account for escaped sequences within string length
786 * calculations. This follows the logic in term_word() as we
787 * must calculate the width of produced strings.
788 */
789
790 sz = 0;
791 skip = 0;
792 while ('\0' != *cp) {
793 rsz = strcspn(cp, rej);
794 for (i = 0; i < rsz; i++)
795 sz += cond_width(p, *cp++, &skip);
796
797 switch (*cp) {
798 case '\\':
799 cp++;
800 esc = mandoc_escape(&cp, &seq, &ssz);
801 if (ESCAPE_ERROR == esc)
802 continue;
803
804 rhs = NULL;
805
806 switch (esc) {
807 case ESCAPE_UNICODE:
808 uc = mchars_num2uc(seq + 1, ssz - 1);
809 break;
810 case ESCAPE_NUMBERED:
811 uc = mchars_num2char(seq, ssz);
812 if (uc < 0)
813 continue;
814 break;
815 case ESCAPE_SPECIAL:
816 if (p->enc == TERMENC_ASCII) {
817 rhs = mchars_spec2str(seq, ssz, &rsz);
818 if (rhs != NULL)
819 break;
820 } else {
821 uc = mchars_spec2cp(seq, ssz);
822 if (uc > 0)
823 sz += cond_width(p, uc, &skip);
824 }
825 continue;
826 case ESCAPE_SKIPCHAR:
827 skip = 1;
828 continue;
829 case ESCAPE_OVERSTRIKE:
830 rsz = 0;
831 rhs = seq + ssz;
832 while (seq < rhs) {
833 if (*seq == '\\') {
834 mandoc_escape(&seq, NULL, NULL);
835 continue;
836 }
837 i = (*p->width)(p, *seq++);
838 if (rsz < i)
839 rsz = i;
840 }
841 sz += rsz;
842 continue;
843 default:
844 continue;
845 }
846
847 /*
848 * Common handling for Unicode and numbered
849 * character escape sequences.
850 */
851
852 if (rhs == NULL) {
853 if (p->enc == TERMENC_ASCII) {
854 rhs = ascii_uc2str(uc);
855 rsz = strlen(rhs);
856 } else {
857 if ((uc < 0x20 && uc != 0x09) ||
858 (uc > 0x7E && uc < 0xA0))
859 uc = 0xFFFD;
860 sz += cond_width(p, uc, &skip);
861 continue;
862 }
863 }
864
865 if (skip) {
866 skip = 0;
867 break;
868 }
869
870 /*
871 * Common handling for all escape sequences
872 * printing more than one character.
873 */
874
875 for (i = 0; i < rsz; i++)
876 sz += (*p->width)(p, *rhs++);
877 break;
878 case ASCII_NBRSP:
879 sz += cond_width(p, ' ', &skip);
880 cp++;
881 break;
882 case ASCII_HYPH:
883 sz += cond_width(p, '-', &skip);
884 cp++;
885 break;
886 default:
887 break;
888 }
889 }
890
891 return sz;
892 }
893
894 int
895 term_vspan(const struct termp *p, const struct roffsu *su)
896 {
897 double r;
898 int ri;
899
900 switch (su->unit) {
901 case SCALE_BU:
902 r = su->scale / 40.0;
903 break;
904 case SCALE_CM:
905 r = su->scale * 6.0 / 2.54;
906 break;
907 case SCALE_FS:
908 r = su->scale * 65536.0 / 40.0;
909 break;
910 case SCALE_IN:
911 r = su->scale * 6.0;
912 break;
913 case SCALE_MM:
914 r = su->scale * 0.006;
915 break;
916 case SCALE_PC:
917 r = su->scale;
918 break;
919 case SCALE_PT:
920 r = su->scale / 12.0;
921 break;
922 case SCALE_EN:
923 case SCALE_EM:
924 r = su->scale * 0.6;
925 break;
926 case SCALE_VS:
927 r = su->scale;
928 break;
929 default:
930 abort();
931 }
932 ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
933 return ri < 66 ? ri : 1;
934 }
935
936 /*
937 * Convert a scaling width to basic units, rounding down.
938 */
939 int
940 term_hspan(const struct termp *p, const struct roffsu *su)
941 {
942
943 return (*p->hspan)(p, su);
944 }