1 /* $OpenBSD: tbl_term.c,v 1.58 2019/03/18 08:00:26 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2011-2019 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 <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 "tbl.h" 28 #include "out.h" 29 #include "term.h" 30 31 #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \ 32 (cp)->pos == TBL_CELL_DHORIZ) 33 34 35 static size_t term_tbl_len(size_t, void *); 36 static size_t term_tbl_strlen(const char *, void *); 37 static size_t term_tbl_sulen(const struct roffsu *, void *); 38 static void tbl_data(struct termp *, const struct tbl_opts *, 39 const struct tbl_cell *, 40 const struct tbl_dat *, 41 const struct roffcol *); 42 static void tbl_direct_border(struct termp *, int, size_t); 43 static void tbl_fill_border(struct termp *, int, size_t); 44 static void tbl_fill_char(struct termp *, char, size_t); 45 static void tbl_fill_string(struct termp *, const char *, size_t); 46 static void tbl_hrule(struct termp *, const struct tbl_span *, 47 const struct tbl_span *, const struct tbl_span *, 48 int); 49 static void tbl_literal(struct termp *, const struct tbl_dat *, 50 const struct roffcol *); 51 static void tbl_number(struct termp *, const struct tbl_opts *, 52 const struct tbl_dat *, 53 const struct roffcol *); 54 static void tbl_word(struct termp *, const struct tbl_dat *); 55 56 57 /* 58 * The following border-character tables are indexed 59 * by ternary (3-based) numbers, as opposed to binary or decimal. 60 * Each ternary digit describes the line width in one direction: 61 * 0 means no line, 1 single or light line, 2 double or heavy line. 62 */ 63 64 /* Positional values of the four directions. */ 65 #define BRIGHT 1 66 #define BDOWN 3 67 #define BLEFT (3 * 3) 68 #define BUP (3 * 3 * 3) 69 #define BHORIZ (BLEFT + BRIGHT) 70 71 /* Code points to use for each combination of widths. */ 72 static const int borders_utf8[81] = { 73 0x0020, 0x2576, 0x257a, /* 000 right */ 74 0x2577, 0x250c, 0x250d, /* 001 down */ 75 0x257b, 0x250e, 0x250f, /* 002 */ 76 0x2574, 0x2500, 0x257c, /* 010 left */ 77 0x2510, 0x252c, 0x252e, /* 011 left down */ 78 0x2512, 0x2530, 0x2532, /* 012 */ 79 0x2578, 0x257e, 0x2501, /* 020 left */ 80 0x2511, 0x252d, 0x252f, /* 021 left down */ 81 0x2513, 0x2531, 0x2533, /* 022 */ 82 0x2575, 0x2514, 0x2515, /* 100 up */ 83 0x2502, 0x251c, 0x251d, /* 101 up down */ 84 0x257d, 0x251f, 0x2522, /* 102 */ 85 0x2518, 0x2534, 0x2536, /* 110 up left */ 86 0x2524, 0x253c, 0x253e, /* 111 all */ 87 0x2527, 0x2541, 0x2546, /* 112 */ 88 0x2519, 0x2535, 0x2537, /* 120 up left */ 89 0x2525, 0x253d, 0x253f, /* 121 all */ 90 0x252a, 0x2545, 0x2548, /* 122 */ 91 0x2579, 0x2516, 0x2517, /* 200 up */ 92 0x257f, 0x251e, 0x2521, /* 201 up down */ 93 0x2503, 0x2520, 0x2523, /* 202 */ 94 0x251a, 0x2538, 0x253a, /* 210 up left */ 95 0x2526, 0x2540, 0x2544, /* 211 all */ 96 0x2528, 0x2542, 0x254a, /* 212 */ 97 0x251b, 0x2539, 0x253b, /* 220 up left */ 98 0x2529, 0x2543, 0x2547, /* 221 all */ 99 0x252b, 0x2549, 0x254b, /* 222 */ 100 }; 101 102 /* ASCII approximations for these code points, compatible with groff. */ 103 static const int borders_ascii[81] = { 104 ' ', '-', '=', /* 000 right */ 105 '|', '+', '+', /* 001 down */ 106 '|', '+', '+', /* 002 */ 107 '-', '-', '=', /* 010 left */ 108 '+', '+', '+', /* 011 left down */ 109 '+', '+', '+', /* 012 */ 110 '=', '=', '=', /* 020 left */ 111 '+', '+', '+', /* 021 left down */ 112 '+', '+', '+', /* 022 */ 113 '|', '+', '+', /* 100 up */ 114 '|', '+', '+', /* 101 up down */ 115 '|', '+', '+', /* 102 */ 116 '+', '+', '+', /* 110 up left */ 117 '+', '+', '+', /* 111 all */ 118 '+', '+', '+', /* 112 */ 119 '+', '+', '+', /* 120 up left */ 120 '+', '+', '+', /* 121 all */ 121 '+', '+', '+', /* 122 */ 122 '|', '+', '+', /* 200 up */ 123 '|', '+', '+', /* 201 up down */ 124 '|', '+', '+', /* 202 */ 125 '+', '+', '+', /* 210 up left */ 126 '+', '+', '+', /* 211 all */ 127 '+', '+', '+', /* 212 */ 128 '+', '+', '+', /* 220 up left */ 129 '+', '+', '+', /* 221 all */ 130 '+', '+', '+', /* 222 */ 131 }; 132 133 /* Either of the above according to the selected output encoding. */ 134 static const int *borders_locale; 135 136 137 static size_t 138 term_tbl_sulen(const struct roffsu *su, void *arg) 139 { 140 int i; 141 142 i = term_hen((const struct termp *)arg, su); 143 return i > 0 ? i : 0; 144 } 145 146 static size_t 147 term_tbl_strlen(const char *p, void *arg) 148 { 149 return term_strlen((const struct termp *)arg, p); 150 } 151 152 static size_t 153 term_tbl_len(size_t sz, void *arg) 154 { 155 return term_len((const struct termp *)arg, sz); 156 } 157 158 159 void 160 term_tbl(struct termp *tp, const struct tbl_span *sp) 161 { 162 const struct tbl_cell *cp, *cpn, *cpp, *cps; 163 const struct tbl_dat *dp; 164 static size_t offset; 165 size_t save_offset; 166 size_t coloff, tsz; 167 int hspans, ic, more; 168 int dvert, fc, horiz, lhori, rhori, uvert; 169 170 /* Inhibit printing of spaces: we do padding ourselves. */ 171 172 tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE; 173 save_offset = tp->tcol->offset; 174 175 /* 176 * The first time we're invoked for a given table block, 177 * calculate the table widths and decimal positions. 178 */ 179 180 if (tp->tbl.cols == NULL) { 181 borders_locale = tp->enc == TERMENC_UTF8 ? 182 borders_utf8 : borders_ascii; 183 184 tp->tbl.len = term_tbl_len; 185 tp->tbl.slen = term_tbl_strlen; 186 tp->tbl.sulen = term_tbl_sulen; 187 tp->tbl.arg = tp; 188 189 tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin); 190 191 /* Tables leak .ta settings to subsequent text. */ 192 193 term_tab_set(tp, NULL); 194 coloff = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) || 195 sp->opts->lvert; 196 for (ic = 0; ic < sp->opts->cols; ic++) { 197 coloff += tp->tbl.cols[ic].width; 198 term_tab_iset(coloff); 199 coloff += tp->tbl.cols[ic].spacing; 200 } 201 202 /* Center the table as a whole. */ 203 204 offset = tp->tcol->offset; 205 if (sp->opts->opts & TBL_OPT_CENTRE) { 206 tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) 207 ? 2 : !!sp->opts->lvert + !!sp->opts->rvert; 208 for (ic = 0; ic + 1 < sp->opts->cols; ic++) 209 tsz += tp->tbl.cols[ic].width + 210 tp->tbl.cols[ic].spacing; 211 if (sp->opts->cols) 212 tsz += tp->tbl.cols[sp->opts->cols - 1].width; 213 if (offset + tsz > tp->tcol->rmargin) 214 tsz -= 1; 215 offset = offset + tp->tcol->rmargin > tsz ? 216 (offset + tp->tcol->rmargin - tsz) / 2 : 0; 217 tp->tcol->offset = offset; 218 } 219 220 /* Horizontal frame at the start of boxed tables. */ 221 222 if (tp->enc == TERMENC_ASCII && 223 sp->opts->opts & TBL_OPT_DBOX) 224 tbl_hrule(tp, NULL, sp, sp, TBL_OPT_DBOX); 225 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) 226 tbl_hrule(tp, NULL, sp, sp, TBL_OPT_BOX); 227 } 228 229 /* Set up the columns. */ 230 231 tp->flags |= TERMP_MULTICOL; 232 tp->tcol->offset = offset; 233 horiz = 0; 234 switch (sp->pos) { 235 case TBL_SPAN_HORIZ: 236 case TBL_SPAN_DHORIZ: 237 horiz = 1; 238 term_setcol(tp, 1); 239 break; 240 case TBL_SPAN_DATA: 241 term_setcol(tp, sp->opts->cols + 2); 242 coloff = tp->tcol->offset; 243 244 /* Set up a column for a left vertical frame. */ 245 246 if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) || 247 sp->opts->lvert) 248 coloff++; 249 tp->tcol->rmargin = coloff; 250 251 /* Set up the data columns. */ 252 253 dp = sp->first; 254 hspans = 0; 255 for (ic = 0; ic < sp->opts->cols; ic++) { 256 if (hspans == 0) { 257 tp->tcol++; 258 tp->tcol->offset = coloff; 259 } 260 coloff += tp->tbl.cols[ic].width; 261 tp->tcol->rmargin = coloff; 262 if (ic + 1 < sp->opts->cols) 263 coloff += tp->tbl.cols[ic].spacing; 264 if (hspans) { 265 hspans--; 266 continue; 267 } 268 if (dp == NULL) 269 continue; 270 hspans = dp->hspans; 271 if (ic || sp->layout->first->pos != TBL_CELL_SPAN) 272 dp = dp->next; 273 } 274 275 /* Set up a column for a right vertical frame. */ 276 277 tp->tcol++; 278 tp->tcol->offset = coloff + 1; 279 tp->tcol->rmargin = tp->maxrmargin; 280 281 /* Spans may have reduced the number of columns. */ 282 283 tp->lasttcol = tp->tcol - tp->tcols; 284 285 /* Fill the buffers for all data columns. */ 286 287 tp->tcol = tp->tcols; 288 cp = cpn = sp->layout->first; 289 dp = sp->first; 290 hspans = 0; 291 for (ic = 0; ic < sp->opts->cols; ic++) { 292 if (cpn != NULL) { 293 cp = cpn; 294 cpn = cpn->next; 295 } 296 if (hspans) { 297 hspans--; 298 continue; 299 } 300 tp->tcol++; 301 tp->col = 0; 302 tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic); 303 if (dp == NULL) 304 continue; 305 hspans = dp->hspans; 306 if (cp->pos != TBL_CELL_SPAN) 307 dp = dp->next; 308 } 309 break; 310 } 311 312 do { 313 /* Print the vertical frame at the start of each row. */ 314 315 tp->tcol = tp->tcols; 316 uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 : 317 sp->opts->opts & TBL_OPT_BOX ? 1 : 0; 318 if (sp->pos == TBL_SPAN_DATA && uvert < sp->layout->vert) 319 uvert = dvert = sp->layout->vert; 320 if (sp->next != NULL && sp->next->pos == TBL_SPAN_DATA && 321 dvert < sp->next->layout->vert) 322 dvert = sp->next->layout->vert; 323 if (sp->prev != NULL && uvert < sp->prev->layout->vert && 324 (horiz || (IS_HORIZ(sp->layout->first) && 325 !IS_HORIZ(sp->prev->layout->first)))) 326 uvert = sp->prev->layout->vert; 327 rhori = sp->pos == TBL_SPAN_DHORIZ || 328 (sp->first != NULL && sp->first->pos == TBL_DATA_DHORIZ) || 329 sp->layout->first->pos == TBL_CELL_DHORIZ ? 2 : 330 sp->pos == TBL_SPAN_HORIZ || 331 (sp->first != NULL && sp->first->pos == TBL_DATA_HORIZ) || 332 sp->layout->first->pos == TBL_CELL_HORIZ ? 1 : 0; 333 fc = BUP * uvert + BDOWN * dvert + BRIGHT * rhori; 334 if (uvert > 0 || dvert > 0 || (horiz && sp->opts->lvert)) { 335 (*tp->advance)(tp, tp->tcols->offset); 336 tp->viscol = tp->tcol->offset; 337 tbl_direct_border(tp, fc, 1); 338 } 339 340 /* Print the data cells. */ 341 342 more = 0; 343 if (horiz) 344 tbl_hrule(tp, sp->prev, sp, sp->next, 0); 345 else { 346 cp = sp->layout->first; 347 cpn = sp->next == NULL ? NULL : 348 sp->next->layout->first; 349 cpp = sp->prev == NULL ? NULL : 350 sp->prev->layout->first; 351 dp = sp->first; 352 hspans = 0; 353 for (ic = 0; ic < sp->opts->cols; ic++) { 354 355 /* 356 * Figure out whether to print a 357 * vertical line after this cell 358 * and advance to next layout cell. 359 */ 360 361 uvert = dvert = fc = 0; 362 if (cp != NULL) { 363 cps = cp; 364 while (cps->next != NULL && 365 cps->next->pos == TBL_CELL_SPAN) 366 cps = cps->next; 367 if (sp->pos == TBL_SPAN_DATA) 368 uvert = dvert = cps->vert; 369 switch (cp->pos) { 370 case TBL_CELL_HORIZ: 371 fc = BHORIZ; 372 break; 373 case TBL_CELL_DHORIZ: 374 fc = BHORIZ * 2; 375 break; 376 default: 377 break; 378 } 379 } 380 if (cpp != NULL) { 381 if (uvert < cpp->vert && 382 cp != NULL && 383 ((IS_HORIZ(cp) && 384 !IS_HORIZ(cpp)) || 385 (cp->next != NULL && 386 cpp->next != NULL && 387 IS_HORIZ(cp->next) && 388 !IS_HORIZ(cpp->next)))) 389 uvert = cpp->vert; 390 cpp = cpp->next; 391 } 392 if (sp->opts->opts & TBL_OPT_ALLBOX) { 393 if (uvert == 0) 394 uvert = 1; 395 if (dvert == 0) 396 dvert = 1; 397 } 398 if (cpn != NULL) { 399 if (dvert == 0 || 400 (dvert < cpn->vert && 401 tp->enc == TERMENC_UTF8)) 402 dvert = cpn->vert; 403 cpn = cpn->next; 404 } 405 406 lhori = (cp != NULL && 407 cp->pos == TBL_CELL_DHORIZ) || 408 (dp != NULL && 409 dp->pos == TBL_DATA_DHORIZ) ? 2 : 410 (cp != NULL && 411 cp->pos == TBL_CELL_HORIZ) || 412 (dp != NULL && 413 dp->pos == TBL_DATA_HORIZ) ? 1 : 0; 414 415 /* 416 * Skip later cells in a span, 417 * figure out whether to start a span, 418 * and advance to next data cell. 419 */ 420 421 if (hspans) { 422 hspans--; 423 cp = cp->next; 424 continue; 425 } 426 if (dp != NULL) { 427 hspans = dp->hspans; 428 if (ic || sp->layout->first->pos 429 != TBL_CELL_SPAN) 430 dp = dp->next; 431 } 432 433 /* 434 * Print one line of text in the cell 435 * and remember whether there is more. 436 */ 437 438 tp->tcol++; 439 if (tp->tcol->col < tp->tcol->lastcol) 440 term_flushln(tp); 441 if (tp->tcol->col < tp->tcol->lastcol) 442 more = 1; 443 444 /* 445 * Vertical frames between data cells, 446 * but not after the last column. 447 */ 448 449 if (fc == 0 && 450 ((uvert == 0 && dvert == 0 && 451 cp != NULL && (cp->next == NULL || 452 !IS_HORIZ(cp->next))) || 453 tp->tcol + 1 == 454 tp->tcols + tp->lasttcol)) { 455 if (cp != NULL) 456 cp = cp->next; 457 continue; 458 } 459 460 if (tp->viscol < tp->tcol->rmargin) { 461 (*tp->advance)(tp, tp->tcol->rmargin 462 - tp->viscol); 463 tp->viscol = tp->tcol->rmargin; 464 } 465 while (tp->viscol < tp->tcol->rmargin + 466 tp->tbl.cols[ic].spacing / 2) 467 tbl_direct_border(tp, 468 BHORIZ * lhori, 1); 469 470 if (tp->tcol + 1 == tp->tcols + tp->lasttcol) 471 continue; 472 473 if (cp != NULL) 474 cp = cp->next; 475 476 rhori = (cp != NULL && 477 cp->pos == TBL_CELL_DHORIZ) || 478 (dp != NULL && 479 dp->pos == TBL_DATA_DHORIZ) ? 2 : 480 (cp != NULL && 481 cp->pos == TBL_CELL_HORIZ) || 482 (dp != NULL && 483 dp->pos == TBL_DATA_HORIZ) ? 1 : 0; 484 485 if (tp->tbl.cols[ic].spacing) 486 tbl_direct_border(tp, 487 BLEFT * lhori + BRIGHT * rhori + 488 BUP * uvert + BDOWN * dvert, 1); 489 490 if (tp->enc == TERMENC_UTF8) 491 uvert = dvert = 0; 492 493 if (tp->tbl.cols[ic].spacing > 2 && 494 (uvert > 1 || dvert > 1 || rhori)) 495 tbl_direct_border(tp, 496 BHORIZ * rhori + 497 BUP * (uvert > 1) + 498 BDOWN * (dvert > 1), 1); 499 } 500 } 501 502 /* Print the vertical frame at the end of each row. */ 503 504 uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 : 505 sp->opts->opts & TBL_OPT_BOX ? 1 : 0; 506 if (sp->pos == TBL_SPAN_DATA && 507 uvert < sp->layout->last->vert && 508 sp->layout->last->col + 1 == sp->opts->cols) 509 uvert = dvert = sp->layout->last->vert; 510 if (sp->next != NULL && 511 dvert < sp->next->layout->last->vert && 512 sp->next->layout->last->col + 1 == sp->opts->cols) 513 dvert = sp->next->layout->last->vert; 514 if (sp->prev != NULL && 515 uvert < sp->prev->layout->last->vert && 516 sp->prev->layout->last->col + 1 == sp->opts->cols && 517 (horiz || (IS_HORIZ(sp->layout->last) && 518 !IS_HORIZ(sp->prev->layout->last)))) 519 uvert = sp->prev->layout->last->vert; 520 lhori = sp->pos == TBL_SPAN_DHORIZ || 521 (sp->last != NULL && 522 sp->last->pos == TBL_DATA_DHORIZ && 523 sp->last->layout->col + 1 == sp->opts->cols) || 524 (sp->layout->last->pos == TBL_CELL_DHORIZ && 525 sp->layout->last->col + 1 == sp->opts->cols) ? 2 : 526 sp->pos == TBL_SPAN_HORIZ || 527 (sp->last != NULL && 528 sp->last->pos == TBL_DATA_HORIZ && 529 sp->last->layout->col + 1 == sp->opts->cols) || 530 (sp->layout->last->pos == TBL_CELL_HORIZ && 531 sp->layout->last->col + 1 == sp->opts->cols) ? 1 : 0; 532 fc = BUP * uvert + BDOWN * dvert + BLEFT * lhori; 533 if (uvert > 0 || dvert > 0 || (horiz && sp->opts->rvert)) { 534 if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 || 535 sp->layout->last->col + 1 < sp->opts->cols)) { 536 tp->tcol++; 537 do { 538 tbl_direct_border(tp, 539 BHORIZ * lhori, 1); 540 } while (tp->viscol < tp->tcol->offset); 541 } 542 tbl_direct_border(tp, fc, 1); 543 } 544 (*tp->endline)(tp); 545 tp->viscol = 0; 546 } while (more); 547 548 /* 549 * Clean up after this row. If it is the last line 550 * of the table, print the box line and clean up 551 * column data; otherwise, print the allbox line. 552 */ 553 554 term_setcol(tp, 1); 555 tp->flags &= ~TERMP_MULTICOL; 556 tp->tcol->rmargin = tp->maxrmargin; 557 if (sp->next == NULL) { 558 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) { 559 tbl_hrule(tp, sp, sp, NULL, TBL_OPT_BOX); 560 tp->skipvsp = 1; 561 } 562 if (tp->enc == TERMENC_ASCII && 563 sp->opts->opts & TBL_OPT_DBOX) { 564 tbl_hrule(tp, sp, sp, NULL, TBL_OPT_DBOX); 565 tp->skipvsp = 2; 566 } 567 assert(tp->tbl.cols); 568 free(tp->tbl.cols); 569 tp->tbl.cols = NULL; 570 } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX && 571 (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA || 572 sp->next->next != NULL)) 573 tbl_hrule(tp, sp, sp, sp->next, TBL_OPT_ALLBOX); 574 575 tp->tcol->offset = save_offset; 576 tp->flags &= ~TERMP_NONOSPACE; 577 } 578 579 static void 580 tbl_hrule(struct termp *tp, const struct tbl_span *spp, 581 const struct tbl_span *sp, const struct tbl_span *spn, int flags) 582 { 583 const struct tbl_cell *cpp; /* Layout cell above this line. */ 584 const struct tbl_cell *cp; /* Layout cell in this line. */ 585 const struct tbl_cell *cpn; /* Layout cell below this line. */ 586 const struct tbl_dat *dpn; /* Data cell below this line. */ 587 const struct roffcol *col; /* Contains width and spacing. */ 588 int opts; /* For the table as a whole. */ 589 int bw; /* Box line width. */ 590 int hw; /* Horizontal line width. */ 591 int lw, rw; /* Left and right line widths. */ 592 int uw, dw; /* Vertical line widths. */ 593 594 cpp = spp == NULL ? NULL : spp->layout->first; 595 cp = sp == NULL ? NULL : sp->layout->first; 596 cpn = spn == NULL ? NULL : spn->layout->first; 597 dpn = NULL; 598 if (spn != NULL) { 599 if (spn->pos == TBL_SPAN_DATA) 600 dpn = spn->first; 601 else if (spn->next != NULL) 602 dpn = spn->next->first; 603 } 604 opts = sp->opts->opts; 605 bw = opts & TBL_OPT_DBOX ? (tp->enc == TERMENC_UTF8 ? 2 : 1) : 606 opts & (TBL_OPT_BOX | TBL_OPT_ALLBOX) ? 1 : 0; 607 hw = flags == TBL_OPT_DBOX || flags == TBL_OPT_BOX ? bw : 608 sp->pos == TBL_SPAN_DHORIZ ? 2 : 1; 609 610 /* Print the left end of the line. */ 611 612 if (tp->viscol == 0) { 613 (*tp->advance)(tp, tp->tcols->offset); 614 tp->viscol = tp->tcols->offset; 615 } 616 if (flags != 0) 617 tbl_direct_border(tp, 618 (spp == NULL ? 0 : BUP * bw) + 619 (spn == NULL ? 0 : BDOWN * bw) + 620 (spp == NULL || cpn == NULL || 621 cpn->pos != TBL_CELL_DOWN ? BRIGHT * hw : 0), 1); 622 623 for (;;) { 624 col = tp->tbl.cols + cp->col; 625 626 /* Print the horizontal line inside this column. */ 627 628 lw = cpp == NULL || cpn == NULL || 629 (cpn->pos != TBL_CELL_DOWN && 630 (dpn == NULL || dpn->string == NULL || 631 strcmp(dpn->string, "\\^") != 0)) 632 ? hw : 0; 633 tbl_direct_border(tp, BHORIZ * lw, 634 col->width + col->spacing / 2); 635 636 /* 637 * Figure out whether a vertical line is crossing 638 * at the end of this column, 639 * and advance to the next column. 640 */ 641 642 uw = dw = 0; 643 if (cpp != NULL) { 644 if (flags != TBL_OPT_DBOX) { 645 uw = cpp->vert; 646 if (uw == 0 && opts & TBL_OPT_ALLBOX) 647 uw = 1; 648 } 649 cpp = cpp->next; 650 } 651 if (cp != NULL) 652 cp = cp->next; 653 if (cpn != NULL) { 654 if (flags != TBL_OPT_DBOX) { 655 dw = cpn->vert; 656 if (dw == 0 && opts & TBL_OPT_ALLBOX) 657 dw = 1; 658 } 659 cpn = cpn->next; 660 while (dpn != NULL && dpn->layout != cpn) 661 dpn = dpn->next; 662 } 663 if (cpp == NULL && cp == NULL && cpn == NULL) 664 break; 665 666 /* Vertical lines do not cross spanned cells. */ 667 668 if (cpp != NULL && cpp->pos == TBL_CELL_SPAN) 669 uw = 0; 670 if (cpn != NULL && cpn->pos == TBL_CELL_SPAN) 671 dw = 0; 672 673 /* The horizontal line inside the next column. */ 674 675 rw = cpp == NULL || cpn == NULL || 676 (cpn->pos != TBL_CELL_DOWN && 677 (dpn == NULL || dpn->string == NULL || 678 strcmp(dpn->string, "\\^") != 0)) 679 ? hw : 0; 680 681 /* The line crossing at the end of this column. */ 682 683 if (col->spacing) 684 tbl_direct_border(tp, BLEFT * lw + 685 BRIGHT * rw + BUP * uw + BDOWN * dw, 1); 686 687 /* 688 * In ASCII output, a crossing may print two characters. 689 */ 690 691 if (tp->enc != TERMENC_ASCII || (uw < 2 && dw < 2)) 692 uw = dw = 0; 693 if (col->spacing > 2) 694 tbl_direct_border(tp, 695 BHORIZ * rw + BUP * uw + BDOWN * dw, 1); 696 697 /* Padding before the start of the next column. */ 698 699 if (col->spacing > 4) 700 tbl_direct_border(tp, 701 BHORIZ * rw, (col->spacing - 3) / 2); 702 } 703 704 /* Print the right end of the line. */ 705 706 if (flags != 0) { 707 tbl_direct_border(tp, 708 (spp == NULL ? 0 : BUP * bw) + 709 (spn == NULL ? 0 : BDOWN * bw) + 710 (spp == NULL || spn == NULL || 711 spn->layout->last->pos != TBL_CELL_DOWN ? 712 BLEFT * hw : 0), 1); 713 (*tp->endline)(tp); 714 tp->viscol = 0; 715 } 716 } 717 718 static void 719 tbl_data(struct termp *tp, const struct tbl_opts *opts, 720 const struct tbl_cell *cp, const struct tbl_dat *dp, 721 const struct roffcol *col) 722 { 723 switch (cp->pos) { 724 case TBL_CELL_HORIZ: 725 tbl_fill_border(tp, BHORIZ, col->width); 726 return; 727 case TBL_CELL_DHORIZ: 728 tbl_fill_border(tp, BHORIZ * 2, col->width); 729 return; 730 default: 731 break; 732 } 733 734 if (dp == NULL) 735 return; 736 737 switch (dp->pos) { 738 case TBL_DATA_NONE: 739 return; 740 case TBL_DATA_HORIZ: 741 case TBL_DATA_NHORIZ: 742 tbl_fill_border(tp, BHORIZ, col->width); 743 return; 744 case TBL_DATA_NDHORIZ: 745 case TBL_DATA_DHORIZ: 746 tbl_fill_border(tp, BHORIZ * 2, col->width); 747 return; 748 default: 749 break; 750 } 751 752 switch (cp->pos) { 753 case TBL_CELL_LONG: 754 case TBL_CELL_CENTRE: 755 case TBL_CELL_LEFT: 756 case TBL_CELL_RIGHT: 757 tbl_literal(tp, dp, col); 758 break; 759 case TBL_CELL_NUMBER: 760 tbl_number(tp, opts, dp, col); 761 break; 762 case TBL_CELL_DOWN: 763 case TBL_CELL_SPAN: 764 break; 765 default: 766 abort(); 767 } 768 } 769 770 static void 771 tbl_fill_string(struct termp *tp, const char *cp, size_t len) 772 { 773 size_t i, sz; 774 775 sz = term_strlen(tp, cp); 776 for (i = 0; i < len; i += sz) 777 term_word(tp, cp); 778 } 779 780 static void 781 tbl_fill_char(struct termp *tp, char c, size_t len) 782 { 783 char cp[2]; 784 785 cp[0] = c; 786 cp[1] = '\0'; 787 tbl_fill_string(tp, cp, len); 788 } 789 790 static void 791 tbl_fill_border(struct termp *tp, int c, size_t len) 792 { 793 char buf[12]; 794 795 if ((c = borders_locale[c]) > 127) { 796 (void)snprintf(buf, sizeof(buf), "\\[u%04x]", c); 797 tbl_fill_string(tp, buf, len); 798 } else 799 tbl_fill_char(tp, c, len); 800 } 801 802 static void 803 tbl_direct_border(struct termp *tp, int c, size_t len) 804 { 805 size_t i, sz; 806 807 c = borders_locale[c]; 808 sz = (*tp->width)(tp, c); 809 for (i = 0; i < len; i += sz) { 810 (*tp->letter)(tp, c); 811 tp->viscol += sz; 812 } 813 } 814 815 static void 816 tbl_literal(struct termp *tp, const struct tbl_dat *dp, 817 const struct roffcol *col) 818 { 819 size_t len, padl, padr, width; 820 int ic, hspans; 821 822 assert(dp->string); 823 len = term_strlen(tp, dp->string); 824 width = col->width; 825 ic = dp->layout->col; 826 hspans = dp->hspans; 827 while (hspans--) 828 width += tp->tbl.cols[++ic].width + 3; 829 830 padr = width > len ? width - len : 0; 831 padl = 0; 832 833 switch (dp->layout->pos) { 834 case TBL_CELL_LONG: 835 padl = term_len(tp, 1); 836 padr = padr > padl ? padr - padl : 0; 837 break; 838 case TBL_CELL_CENTRE: 839 if (2 > padr) 840 break; 841 padl = padr / 2; 842 padr -= padl; 843 break; 844 case TBL_CELL_RIGHT: 845 padl = padr; 846 padr = 0; 847 break; 848 default: 849 break; 850 } 851 852 tbl_fill_char(tp, ASCII_NBRSP, padl); 853 tbl_word(tp, dp); 854 tbl_fill_char(tp, ASCII_NBRSP, padr); 855 } 856 857 static void 858 tbl_number(struct termp *tp, const struct tbl_opts *opts, 859 const struct tbl_dat *dp, 860 const struct roffcol *col) 861 { 862 const char *cp, *lastdigit, *lastpoint; 863 size_t intsz, padl, totsz; 864 char buf[2]; 865 866 /* 867 * Almost the same code as in tblcalc_number(): 868 * First find the position of the decimal point. 869 */ 870 871 assert(dp->string); 872 lastdigit = lastpoint = NULL; 873 for (cp = dp->string; cp[0] != '\0'; cp++) { 874 if (cp[0] == '\\' && cp[1] == '&') { 875 lastdigit = lastpoint = cp; 876 break; 877 } else if (cp[0] == opts->decimal && 878 (isdigit((unsigned char)cp[1]) || 879 (cp > dp->string && isdigit((unsigned char)cp[-1])))) 880 lastpoint = cp; 881 else if (isdigit((unsigned char)cp[0])) 882 lastdigit = cp; 883 } 884 885 /* Then measure both widths. */ 886 887 padl = 0; 888 totsz = term_strlen(tp, dp->string); 889 if (lastdigit != NULL) { 890 if (lastpoint == NULL) 891 lastpoint = lastdigit + 1; 892 intsz = 0; 893 buf[1] = '\0'; 894 for (cp = dp->string; cp < lastpoint; cp++) { 895 buf[0] = cp[0]; 896 intsz += term_strlen(tp, buf); 897 } 898 899 /* 900 * Pad left to match the decimal position, 901 * but avoid exceeding the total column width. 902 */ 903 904 if (col->decimal > intsz && col->width > totsz) { 905 padl = col->decimal - intsz; 906 if (padl + totsz > col->width) 907 padl = col->width - totsz; 908 } 909 910 /* If it is not a number, simply center the string. */ 911 912 } else if (col->width > totsz) 913 padl = (col->width - totsz) / 2; 914 915 tbl_fill_char(tp, ASCII_NBRSP, padl); 916 tbl_word(tp, dp); 917 918 /* Pad right to fill the column. */ 919 920 if (col->width > padl + totsz) 921 tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz); 922 } 923 924 static void 925 tbl_word(struct termp *tp, const struct tbl_dat *dp) 926 { 927 int prev_font; 928 929 prev_font = tp->fonti; 930 if (dp->layout->flags & TBL_CELL_BOLD) 931 term_fontpush(tp, TERMFONT_BOLD); 932 else if (dp->layout->flags & TBL_CELL_ITALIC) 933 term_fontpush(tp, TERMFONT_UNDER); 934 935 term_word(tp, dp->string); 936 937 term_fontpopq(tp, prev_font); 938 } 939