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