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