Annotation of src/usr.bin/mandoc/tbl_term.c, Revision 1.65
1.65 ! schwarze 1: /* $OpenBSD: tbl_term.c,v 1.64 2022/04/08 16:53:40 schwarze Exp $ */
1.1 schwarze 2: /*
1.65 ! schwarze 3: * Copyright (c) 2011-2022 Ingo Schwarze <schwarze@openbsd.org>
1.9 schwarze 4: * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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 *,
1.57 schwarze 47: const struct tbl_span *, const struct tbl_span *,
48: int);
1.16 schwarze 49: static void tbl_literal(struct termp *, const struct tbl_dat *,
1.6 schwarze 50: const struct roffcol *);
1.16 schwarze 51: static void tbl_number(struct termp *, const struct tbl_opts *,
52: const struct tbl_dat *,
1.6 schwarze 53: const struct roffcol *);
1.17 schwarze 54: static void tbl_word(struct termp *, const struct tbl_dat *);
1.1 schwarze 55:
1.6 schwarze 56:
1.50 schwarze 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:
1.6 schwarze 137: static size_t
1.34 schwarze 138: term_tbl_sulen(const struct roffsu *su, void *arg)
139: {
1.45 schwarze 140: int i;
141:
142: i = term_hen((const struct termp *)arg, su);
143: return i > 0 ? i : 0;
1.34 schwarze 144: }
145:
146: static size_t
1.6 schwarze 147: term_tbl_strlen(const char *p, void *arg)
148: {
1.30 schwarze 149: return term_strlen((const struct termp *)arg, p);
1.6 schwarze 150: }
151:
152: static size_t
153: term_tbl_len(size_t sz, void *arg)
154: {
1.30 schwarze 155: return term_len((const struct termp *)arg, sz);
1.6 schwarze 156: }
1.5 schwarze 157:
1.50 schwarze 158:
1.5 schwarze 159: void
160: term_tbl(struct termp *tp, const struct tbl_span *sp)
161: {
1.50 schwarze 162: const struct tbl_cell *cp, *cpn, *cpp, *cps;
1.6 schwarze 163: const struct tbl_dat *dp;
1.22 schwarze 164: static size_t offset;
1.60 schwarze 165: size_t save_offset;
1.35 schwarze 166: size_t coloff, tsz;
1.50 schwarze 167: int hspans, ic, more;
1.56 schwarze 168: int dvert, fc, horiz, lhori, rhori, uvert;
1.27 schwarze 169:
1.5 schwarze 170: /* Inhibit printing of spaces: we do padding ourselves. */
171:
1.35 schwarze 172: tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
1.55 schwarze 173: save_offset = tp->tcol->offset;
1.1 schwarze 174:
175: /*
1.6 schwarze 176: * The first time we're invoked for a given table block,
177: * calculate the table widths and decimal positions.
1.1 schwarze 178: */
179:
1.25 schwarze 180: if (tp->tbl.cols == NULL) {
1.50 schwarze 181: borders_locale = tp->enc == TERMENC_UTF8 ?
182: borders_utf8 : borders_ascii;
183:
1.6 schwarze 184: tp->tbl.len = term_tbl_len;
185: tp->tbl.slen = term_tbl_strlen;
1.34 schwarze 186: tp->tbl.sulen = term_tbl_sulen;
1.6 schwarze 187: tp->tbl.arg = tp;
1.5 schwarze 188:
1.36 schwarze 189: tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
1.2 schwarze 190:
1.22 schwarze 191: /* Center the table as a whole. */
192:
1.33 schwarze 193: offset = tp->tcol->offset;
1.22 schwarze 194: if (sp->opts->opts & TBL_OPT_CENTRE) {
195: tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
196: ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
1.43 schwarze 197: for (ic = 0; ic + 1 < sp->opts->cols; ic++)
198: tsz += tp->tbl.cols[ic].width +
199: tp->tbl.cols[ic].spacing;
200: if (sp->opts->cols)
201: tsz += tp->tbl.cols[sp->opts->cols - 1].width;
1.33 schwarze 202: if (offset + tsz > tp->tcol->rmargin)
1.22 schwarze 203: tsz -= 1;
1.55 schwarze 204: offset = offset + tp->tcol->rmargin > tsz ?
1.33 schwarze 205: (offset + tp->tcol->rmargin - tsz) / 2 : 0;
1.55 schwarze 206: tp->tcol->offset = offset;
1.22 schwarze 207: }
208:
1.21 schwarze 209: /* Horizontal frame at the start of boxed tables. */
1.1 schwarze 210:
1.50 schwarze 211: if (tp->enc == TERMENC_ASCII &&
212: sp->opts->opts & TBL_OPT_DBOX)
1.57 schwarze 213: tbl_hrule(tp, NULL, sp, sp, TBL_OPT_DBOX);
1.41 schwarze 214: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
1.57 schwarze 215: tbl_hrule(tp, NULL, sp, sp, TBL_OPT_BOX);
1.10 schwarze 216: }
1.5 schwarze 217:
1.35 schwarze 218: /* Set up the columns. */
1.5 schwarze 219:
1.35 schwarze 220: tp->flags |= TERMP_MULTICOL;
1.55 schwarze 221: tp->tcol->offset = offset;
1.35 schwarze 222: horiz = 0;
223: switch (sp->pos) {
224: case TBL_SPAN_HORIZ:
225: case TBL_SPAN_DHORIZ:
226: horiz = 1;
227: term_setcol(tp, 1);
228: break;
229: case TBL_SPAN_DATA:
230: term_setcol(tp, sp->opts->cols + 2);
231: coloff = tp->tcol->offset;
232:
233: /* Set up a column for a left vertical frame. */
234:
235: if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
236: sp->opts->lvert)
237: coloff++;
238: tp->tcol->rmargin = coloff;
1.21 schwarze 239:
1.35 schwarze 240: /* Set up the data columns. */
1.1 schwarze 241:
1.35 schwarze 242: dp = sp->first;
1.49 schwarze 243: hspans = 0;
1.35 schwarze 244: for (ic = 0; ic < sp->opts->cols; ic++) {
1.49 schwarze 245: if (hspans == 0) {
1.35 schwarze 246: tp->tcol++;
247: tp->tcol->offset = coloff;
248: }
249: coloff += tp->tbl.cols[ic].width;
250: tp->tcol->rmargin = coloff;
251: if (ic + 1 < sp->opts->cols)
1.43 schwarze 252: coloff += tp->tbl.cols[ic].spacing;
1.49 schwarze 253: if (hspans) {
254: hspans--;
1.35 schwarze 255: continue;
256: }
1.61 schwarze 257: if (dp != NULL &&
258: (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
259: hspans = dp->hspans;
1.44 schwarze 260: dp = dp->next;
1.61 schwarze 261: }
1.35 schwarze 262: }
263:
264: /* Set up a column for a right vertical frame. */
265:
266: tp->tcol++;
1.43 schwarze 267: tp->tcol->offset = coloff + 1;
1.41 schwarze 268: tp->tcol->rmargin = tp->maxrmargin;
1.35 schwarze 269:
270: /* Spans may have reduced the number of columns. */
271:
272: tp->lasttcol = tp->tcol - tp->tcols;
273:
274: /* Fill the buffers for all data columns. */
1.1 schwarze 275:
1.35 schwarze 276: tp->tcol = tp->tcols;
1.41 schwarze 277: cp = cpn = sp->layout->first;
1.5 schwarze 278: dp = sp->first;
1.49 schwarze 279: hspans = 0;
1.24 schwarze 280: for (ic = 0; ic < sp->opts->cols; ic++) {
1.41 schwarze 281: if (cpn != NULL) {
282: cp = cpn;
283: cpn = cpn->next;
284: }
1.49 schwarze 285: if (hspans) {
286: hspans--;
1.35 schwarze 287: continue;
288: }
289: tp->tcol++;
290: tp->col = 0;
1.65 ! schwarze 291: tp->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE);
1.41 schwarze 292: tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
1.61 schwarze 293: if (dp != NULL &&
294: (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
295: hspans = dp->hspans;
1.44 schwarze 296: dp = dp->next;
1.61 schwarze 297: }
1.35 schwarze 298: }
299: break;
300: }
301:
302: do {
303: /* Print the vertical frame at the start of each row. */
304:
305: tp->tcol = tp->tcols;
1.50 schwarze 306: uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
307: sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
308: if (sp->pos == TBL_SPAN_DATA && uvert < sp->layout->vert)
309: uvert = dvert = sp->layout->vert;
310: if (sp->next != NULL && sp->next->pos == TBL_SPAN_DATA &&
311: dvert < sp->next->layout->vert)
312: dvert = sp->next->layout->vert;
313: if (sp->prev != NULL && uvert < sp->prev->layout->vert &&
314: (horiz || (IS_HORIZ(sp->layout->first) &&
315: !IS_HORIZ(sp->prev->layout->first))))
316: uvert = sp->prev->layout->vert;
1.56 schwarze 317: rhori = sp->pos == TBL_SPAN_DHORIZ ||
318: (sp->first != NULL && sp->first->pos == TBL_DATA_DHORIZ) ||
1.50 schwarze 319: sp->layout->first->pos == TBL_CELL_DHORIZ ? 2 :
320: sp->pos == TBL_SPAN_HORIZ ||
1.56 schwarze 321: (sp->first != NULL && sp->first->pos == TBL_DATA_HORIZ) ||
1.50 schwarze 322: sp->layout->first->pos == TBL_CELL_HORIZ ? 1 : 0;
1.56 schwarze 323: fc = BUP * uvert + BDOWN * dvert + BRIGHT * rhori;
1.50 schwarze 324: if (uvert > 0 || dvert > 0 || (horiz && sp->opts->lvert)) {
1.35 schwarze 325: (*tp->advance)(tp, tp->tcols->offset);
1.50 schwarze 326: tp->viscol = tp->tcol->offset;
327: tbl_direct_border(tp, fc, 1);
1.35 schwarze 328: }
1.11 schwarze 329:
1.35 schwarze 330: /* Print the data cells. */
1.10 schwarze 331:
1.35 schwarze 332: more = 0;
1.50 schwarze 333: if (horiz)
1.57 schwarze 334: tbl_hrule(tp, sp->prev, sp, sp->next, 0);
1.50 schwarze 335: else {
1.35 schwarze 336: cp = sp->layout->first;
1.41 schwarze 337: cpn = sp->next == NULL ? NULL :
338: sp->next->layout->first;
339: cpp = sp->prev == NULL ? NULL :
340: sp->prev->layout->first;
1.35 schwarze 341: dp = sp->first;
1.49 schwarze 342: hspans = 0;
1.35 schwarze 343: for (ic = 0; ic < sp->opts->cols; ic++) {
344:
1.41 schwarze 345: /*
346: * Figure out whether to print a
347: * vertical line after this cell
348: * and advance to next layout cell.
349: */
1.35 schwarze 350:
1.50 schwarze 351: uvert = dvert = fc = 0;
1.35 schwarze 352: if (cp != NULL) {
1.50 schwarze 353: cps = cp;
354: while (cps->next != NULL &&
355: cps->next->pos == TBL_CELL_SPAN)
356: cps = cps->next;
357: if (sp->pos == TBL_SPAN_DATA)
358: uvert = dvert = cps->vert;
1.41 schwarze 359: switch (cp->pos) {
360: case TBL_CELL_HORIZ:
1.50 schwarze 361: fc = BHORIZ;
1.41 schwarze 362: break;
363: case TBL_CELL_DHORIZ:
1.50 schwarze 364: fc = BHORIZ * 2;
1.41 schwarze 365: break;
366: default:
367: break;
368: }
369: }
370: if (cpp != NULL) {
1.50 schwarze 371: if (uvert < cpp->vert &&
1.41 schwarze 372: cp != NULL &&
373: ((IS_HORIZ(cp) &&
374: !IS_HORIZ(cpp)) ||
375: (cp->next != NULL &&
376: cpp->next != NULL &&
377: IS_HORIZ(cp->next) &&
378: !IS_HORIZ(cpp->next))))
1.50 schwarze 379: uvert = cpp->vert;
1.41 schwarze 380: cpp = cpp->next;
381: }
1.50 schwarze 382: if (sp->opts->opts & TBL_OPT_ALLBOX) {
383: if (uvert == 0)
384: uvert = 1;
385: if (dvert == 0)
386: dvert = 1;
387: }
1.41 schwarze 388: if (cpn != NULL) {
1.50 schwarze 389: if (dvert == 0 ||
390: (dvert < cpn->vert &&
391: tp->enc == TERMENC_UTF8))
392: dvert = cpn->vert;
1.41 schwarze 393: cpn = cpn->next;
394: }
1.39 schwarze 395:
1.56 schwarze 396: lhori = (cp != NULL &&
397: cp->pos == TBL_CELL_DHORIZ) ||
398: (dp != NULL &&
399: dp->pos == TBL_DATA_DHORIZ) ? 2 :
400: (cp != NULL &&
401: cp->pos == TBL_CELL_HORIZ) ||
402: (dp != NULL &&
403: dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
404:
1.41 schwarze 405: /*
406: * Skip later cells in a span,
407: * figure out whether to start a span,
408: * and advance to next data cell.
409: */
1.39 schwarze 410:
1.49 schwarze 411: if (hspans) {
412: hspans--;
1.50 schwarze 413: cp = cp->next;
1.39 schwarze 414: continue;
415: }
1.61 schwarze 416: if (dp != NULL && (ic ||
417: sp->layout->first->pos != TBL_CELL_SPAN)) {
1.49 schwarze 418: hspans = dp->hspans;
1.61 schwarze 419: dp = dp->next;
1.39 schwarze 420: }
421:
1.41 schwarze 422: /*
423: * Print one line of text in the cell
424: * and remember whether there is more.
425: */
1.39 schwarze 426:
427: tp->tcol++;
428: if (tp->tcol->col < tp->tcol->lastcol)
429: term_flushln(tp);
430: if (tp->tcol->col < tp->tcol->lastcol)
431: more = 1;
432:
433: /*
434: * Vertical frames between data cells,
435: * but not after the last column.
436: */
437:
1.51 schwarze 438: if (fc == 0 &&
439: ((uvert == 0 && dvert == 0 &&
440: cp != NULL && (cp->next == NULL ||
1.50 schwarze 441: !IS_HORIZ(cp->next))) ||
1.51 schwarze 442: tp->tcol + 1 ==
443: tp->tcols + tp->lasttcol)) {
444: if (cp != NULL)
445: cp = cp->next;
1.41 schwarze 446: continue;
1.50 schwarze 447: }
1.41 schwarze 448:
1.43 schwarze 449: if (tp->viscol < tp->tcol->rmargin) {
1.41 schwarze 450: (*tp->advance)(tp, tp->tcol->rmargin
451: - tp->viscol);
452: tp->viscol = tp->tcol->rmargin;
453: }
1.43 schwarze 454: while (tp->viscol < tp->tcol->rmargin +
1.50 schwarze 455: tp->tbl.cols[ic].spacing / 2)
1.56 schwarze 456: tbl_direct_border(tp,
457: BHORIZ * lhori, 1);
1.41 schwarze 458:
1.39 schwarze 459: if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
460: continue;
1.35 schwarze 461:
1.56 schwarze 462: if (cp != NULL)
1.50 schwarze 463: cp = cp->next;
1.56 schwarze 464:
465: rhori = (cp != NULL &&
466: cp->pos == TBL_CELL_DHORIZ) ||
467: (dp != NULL &&
468: dp->pos == TBL_DATA_DHORIZ) ? 2 :
469: (cp != NULL &&
470: cp->pos == TBL_CELL_HORIZ) ||
471: (dp != NULL &&
472: dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
473:
1.50 schwarze 474: if (tp->tbl.cols[ic].spacing)
1.56 schwarze 475: tbl_direct_border(tp,
476: BLEFT * lhori + BRIGHT * rhori +
1.50 schwarze 477: BUP * uvert + BDOWN * dvert, 1);
478:
479: if (tp->enc == TERMENC_UTF8)
480: uvert = dvert = 0;
1.41 schwarze 481:
1.43 schwarze 482: if (tp->tbl.cols[ic].spacing > 2 &&
1.56 schwarze 483: (uvert > 1 || dvert > 1 || rhori))
484: tbl_direct_border(tp,
485: BHORIZ * rhori +
1.50 schwarze 486: BUP * (uvert > 1) +
487: BDOWN * (dvert > 1), 1);
1.35 schwarze 488: }
489: }
1.5 schwarze 490:
1.35 schwarze 491: /* Print the vertical frame at the end of each row. */
1.7 schwarze 492:
1.50 schwarze 493: uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
494: sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
495: if (sp->pos == TBL_SPAN_DATA &&
496: uvert < sp->layout->last->vert &&
497: sp->layout->last->col + 1 == sp->opts->cols)
498: uvert = dvert = sp->layout->last->vert;
499: if (sp->next != NULL &&
500: dvert < sp->next->layout->last->vert &&
501: sp->next->layout->last->col + 1 == sp->opts->cols)
502: dvert = sp->next->layout->last->vert;
503: if (sp->prev != NULL &&
504: uvert < sp->prev->layout->last->vert &&
505: sp->prev->layout->last->col + 1 == sp->opts->cols &&
506: (horiz || (IS_HORIZ(sp->layout->last) &&
507: !IS_HORIZ(sp->prev->layout->last))))
508: uvert = sp->prev->layout->last->vert;
1.56 schwarze 509: lhori = sp->pos == TBL_SPAN_DHORIZ ||
510: (sp->last != NULL &&
511: sp->last->pos == TBL_DATA_DHORIZ &&
512: sp->last->layout->col + 1 == sp->opts->cols) ||
1.50 schwarze 513: (sp->layout->last->pos == TBL_CELL_DHORIZ &&
514: sp->layout->last->col + 1 == sp->opts->cols) ? 2 :
515: sp->pos == TBL_SPAN_HORIZ ||
1.56 schwarze 516: (sp->last != NULL &&
517: sp->last->pos == TBL_DATA_HORIZ &&
518: sp->last->layout->col + 1 == sp->opts->cols) ||
1.50 schwarze 519: (sp->layout->last->pos == TBL_CELL_HORIZ &&
520: sp->layout->last->col + 1 == sp->opts->cols) ? 1 : 0;
1.56 schwarze 521: fc = BUP * uvert + BDOWN * dvert + BLEFT * lhori;
1.50 schwarze 522: if (uvert > 0 || dvert > 0 || (horiz && sp->opts->rvert)) {
1.41 schwarze 523: if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
524: sp->layout->last->col + 1 < sp->opts->cols)) {
1.35 schwarze 525: tp->tcol++;
1.56 schwarze 526: do {
527: tbl_direct_border(tp,
528: BHORIZ * lhori, 1);
529: } while (tp->viscol < tp->tcol->offset);
1.35 schwarze 530: }
1.50 schwarze 531: tbl_direct_border(tp, fc, 1);
1.35 schwarze 532: }
533: (*tp->endline)(tp);
534: tp->viscol = 0;
535: } while (more);
1.1 schwarze 536:
1.5 schwarze 537: /*
1.41 schwarze 538: * Clean up after this row. If it is the last line
539: * of the table, print the box line and clean up
540: * column data; otherwise, print the allbox line.
1.5 schwarze 541: */
1.1 schwarze 542:
1.35 schwarze 543: term_setcol(tp, 1);
544: tp->flags &= ~TERMP_MULTICOL;
545: tp->tcol->rmargin = tp->maxrmargin;
1.25 schwarze 546: if (sp->next == NULL) {
1.21 schwarze 547: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
1.57 schwarze 548: tbl_hrule(tp, sp, sp, NULL, TBL_OPT_BOX);
1.13 schwarze 549: tp->skipvsp = 1;
550: }
1.50 schwarze 551: if (tp->enc == TERMENC_ASCII &&
552: sp->opts->opts & TBL_OPT_DBOX) {
1.57 schwarze 553: tbl_hrule(tp, sp, sp, NULL, TBL_OPT_DBOX);
1.13 schwarze 554: tp->skipvsp = 2;
555: }
1.6 schwarze 556: assert(tp->tbl.cols);
557: free(tp->tbl.cols);
558: tp->tbl.cols = NULL;
1.38 schwarze 559: } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
560: (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
561: sp->next->next != NULL))
1.57 schwarze 562: tbl_hrule(tp, sp, sp, sp->next, TBL_OPT_ALLBOX);
1.37 schwarze 563:
1.55 schwarze 564: tp->tcol->offset = save_offset;
1.35 schwarze 565: tp->flags &= ~TERMP_NONOSPACE;
1.1 schwarze 566: }
567:
568: static void
1.52 schwarze 569: tbl_hrule(struct termp *tp, const struct tbl_span *spp,
1.57 schwarze 570: const struct tbl_span *sp, const struct tbl_span *spn, int flags)
1.1 schwarze 571: {
1.53 schwarze 572: const struct tbl_cell *cpp; /* Layout cell above this line. */
1.57 schwarze 573: const struct tbl_cell *cp; /* Layout cell in this line. */
1.53 schwarze 574: const struct tbl_cell *cpn; /* Layout cell below this line. */
575: const struct tbl_dat *dpn; /* Data cell below this line. */
1.52 schwarze 576: const struct roffcol *col; /* Contains width and spacing. */
577: int opts; /* For the table as a whole. */
578: int bw; /* Box line width. */
579: int hw; /* Horizontal line width. */
580: int lw, rw; /* Left and right line widths. */
581: int uw, dw; /* Vertical line widths. */
582:
583: cpp = spp == NULL ? NULL : spp->layout->first;
1.57 schwarze 584: cp = sp == NULL ? NULL : sp->layout->first;
1.52 schwarze 585: cpn = spn == NULL ? NULL : spn->layout->first;
1.53 schwarze 586: dpn = NULL;
587: if (spn != NULL) {
588: if (spn->pos == TBL_SPAN_DATA)
589: dpn = spn->first;
590: else if (spn->next != NULL)
591: dpn = spn->next->first;
592: }
1.57 schwarze 593: opts = sp->opts->opts;
1.52 schwarze 594: bw = opts & TBL_OPT_DBOX ? (tp->enc == TERMENC_UTF8 ? 2 : 1) :
595: opts & (TBL_OPT_BOX | TBL_OPT_ALLBOX) ? 1 : 0;
596: hw = flags == TBL_OPT_DBOX || flags == TBL_OPT_BOX ? bw :
1.57 schwarze 597: sp->pos == TBL_SPAN_DHORIZ ? 2 : 1;
1.52 schwarze 598:
599: /* Print the left end of the line. */
600:
1.50 schwarze 601: if (tp->viscol == 0) {
602: (*tp->advance)(tp, tp->tcols->offset);
603: tp->viscol = tp->tcols->offset;
604: }
1.52 schwarze 605: if (flags != 0)
606: tbl_direct_border(tp,
607: (spp == NULL ? 0 : BUP * bw) +
608: (spn == NULL ? 0 : BDOWN * bw) +
609: (spp == NULL || cpn == NULL ||
610: cpn->pos != TBL_CELL_DOWN ? BRIGHT * hw : 0), 1);
611:
1.59 schwarze 612: col = tp->tbl.cols;
1.21 schwarze 613: for (;;) {
1.59 schwarze 614: if (cp == NULL)
615: col++;
616: else
617: col = tp->tbl.cols + cp->col;
1.52 schwarze 618:
619: /* Print the horizontal line inside this column. */
620:
621: lw = cpp == NULL || cpn == NULL ||
1.53 schwarze 622: (cpn->pos != TBL_CELL_DOWN &&
1.58 schwarze 623: (dpn == NULL || dpn->string == NULL ||
624: strcmp(dpn->string, "\\^") != 0))
1.53 schwarze 625: ? hw : 0;
1.52 schwarze 626: tbl_direct_border(tp, BHORIZ * lw,
627: col->width + col->spacing / 2);
628:
629: /*
630: * Figure out whether a vertical line is crossing
631: * at the end of this column,
632: * and advance to the next column.
633: */
634:
635: uw = dw = 0;
1.41 schwarze 636: if (cpp != NULL) {
1.52 schwarze 637: if (flags != TBL_OPT_DBOX) {
638: uw = cpp->vert;
639: if (uw == 0 && opts & TBL_OPT_ALLBOX)
640: uw = 1;
641: }
1.41 schwarze 642: cpp = cpp->next;
1.59 schwarze 643: } else if (spp != NULL && opts & TBL_OPT_ALLBOX)
644: uw = 1;
1.57 schwarze 645: if (cp != NULL)
646: cp = cp->next;
1.41 schwarze 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.59 schwarze 656: } else if (spn != NULL && opts & TBL_OPT_ALLBOX)
657: dw = 1;
658: if (col + 1 == tp->tbl.cols + sp->opts->cols)
1.52 schwarze 659: break;
660:
661: /* Vertical lines do not cross spanned cells. */
662:
663: if (cpp != NULL && cpp->pos == TBL_CELL_SPAN)
664: uw = 0;
665: if (cpn != NULL && cpn->pos == TBL_CELL_SPAN)
666: dw = 0;
667:
668: /* The horizontal line inside the next column. */
669:
670: rw = cpp == NULL || cpn == NULL ||
1.53 schwarze 671: (cpn->pos != TBL_CELL_DOWN &&
1.58 schwarze 672: (dpn == NULL || dpn->string == NULL ||
673: strcmp(dpn->string, "\\^") != 0))
1.53 schwarze 674: ? hw : 0;
1.52 schwarze 675:
676: /* The line crossing at the end of this column. */
677:
1.43 schwarze 678: if (col->spacing)
1.52 schwarze 679: tbl_direct_border(tp, BLEFT * lw +
680: BRIGHT * rw + BUP * uw + BDOWN * dw, 1);
681:
682: /*
683: * In ASCII output, a crossing may print two characters.
684: */
685:
686: if (tp->enc != TERMENC_ASCII || (uw < 2 && dw < 2))
687: uw = dw = 0;
1.43 schwarze 688: if (col->spacing > 2)
1.52 schwarze 689: tbl_direct_border(tp,
690: BHORIZ * rw + BUP * uw + BDOWN * dw, 1);
691:
692: /* Padding before the start of the next column. */
693:
1.43 schwarze 694: if (col->spacing > 4)
1.52 schwarze 695: tbl_direct_border(tp,
696: BHORIZ * rw, (col->spacing - 3) / 2);
1.11 schwarze 697: }
1.52 schwarze 698:
699: /* Print the right end of the line. */
700:
701: if (flags != 0) {
702: tbl_direct_border(tp,
703: (spp == NULL ? 0 : BUP * bw) +
704: (spn == NULL ? 0 : BDOWN * bw) +
705: (spp == NULL || spn == NULL ||
706: spn->layout->last->pos != TBL_CELL_DOWN ?
707: BLEFT * hw : 0), 1);
1.50 schwarze 708: (*tp->endline)(tp);
709: tp->viscol = 0;
1.11 schwarze 710: }
1.1 schwarze 711: }
712:
713: static void
1.14 schwarze 714: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.41 schwarze 715: const struct tbl_cell *cp, const struct tbl_dat *dp,
716: const struct roffcol *col)
1.1 schwarze 717: {
1.41 schwarze 718: switch (cp->pos) {
719: case TBL_CELL_HORIZ:
1.50 schwarze 720: tbl_fill_border(tp, BHORIZ, col->width);
1.41 schwarze 721: return;
722: case TBL_CELL_DHORIZ:
1.50 schwarze 723: tbl_fill_border(tp, BHORIZ * 2, col->width);
1.41 schwarze 724: return;
725: default:
726: break;
727: }
1.1 schwarze 728:
1.44 schwarze 729: if (dp == NULL)
1.1 schwarze 730: return;
731:
1.5 schwarze 732: switch (dp->pos) {
1.16 schwarze 733: case TBL_DATA_NONE:
1.5 schwarze 734: return;
1.16 schwarze 735: case TBL_DATA_HORIZ:
736: case TBL_DATA_NHORIZ:
1.50 schwarze 737: tbl_fill_border(tp, BHORIZ, col->width);
1.5 schwarze 738: return;
1.16 schwarze 739: case TBL_DATA_NDHORIZ:
740: case TBL_DATA_DHORIZ:
1.50 schwarze 741: tbl_fill_border(tp, BHORIZ * 2, col->width);
1.5 schwarze 742: return;
1.1 schwarze 743: default:
744: break;
745: }
1.16 schwarze 746:
1.41 schwarze 747: switch (cp->pos) {
1.16 schwarze 748: case TBL_CELL_LONG:
749: case TBL_CELL_CENTRE:
750: case TBL_CELL_LEFT:
751: case TBL_CELL_RIGHT:
1.6 schwarze 752: tbl_literal(tp, dp, col);
1.1 schwarze 753: break;
1.16 schwarze 754: case TBL_CELL_NUMBER:
1.14 schwarze 755: tbl_number(tp, opts, dp, col);
1.4 schwarze 756: break;
1.16 schwarze 757: case TBL_CELL_DOWN:
1.44 schwarze 758: case TBL_CELL_SPAN:
1.7 schwarze 759: break;
1.1 schwarze 760: default:
761: abort();
762: }
763: }
764:
765: static void
1.50 schwarze 766: tbl_fill_string(struct termp *tp, const char *cp, size_t len)
767: {
768: size_t i, sz;
769:
770: sz = term_strlen(tp, cp);
771: for (i = 0; i < len; i += sz)
772: term_word(tp, cp);
773: }
774:
775: static void
776: tbl_fill_char(struct termp *tp, char c, size_t len)
1.5 schwarze 777: {
1.50 schwarze 778: char cp[2];
1.1 schwarze 779:
1.5 schwarze 780: cp[0] = c;
781: cp[1] = '\0';
1.50 schwarze 782: tbl_fill_string(tp, cp, len);
783: }
1.1 schwarze 784:
1.50 schwarze 785: static void
786: tbl_fill_border(struct termp *tp, int c, size_t len)
787: {
788: char buf[12];
789:
790: if ((c = borders_locale[c]) > 127) {
791: (void)snprintf(buf, sizeof(buf), "\\[u%04x]", c);
792: tbl_fill_string(tp, buf, len);
793: } else
794: tbl_fill_char(tp, c, len);
795: }
796:
797: static void
798: tbl_direct_border(struct termp *tp, int c, size_t len)
799: {
800: size_t i, sz;
1.1 schwarze 801:
1.50 schwarze 802: c = borders_locale[c];
803: sz = (*tp->width)(tp, c);
804: for (i = 0; i < len; i += sz) {
805: (*tp->letter)(tp, c);
806: tp->viscol += sz;
807: }
1.1 schwarze 808: }
809:
810: static void
1.16 schwarze 811: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.6 schwarze 812: const struct roffcol *col)
1.1 schwarze 813: {
1.24 schwarze 814: size_t len, padl, padr, width;
1.49 schwarze 815: int ic, hspans;
1.1 schwarze 816:
1.7 schwarze 817: assert(dp->string);
1.10 schwarze 818: len = term_strlen(tp, dp->string);
1.12 schwarze 819: width = col->width;
1.24 schwarze 820: ic = dp->layout->col;
1.49 schwarze 821: hspans = dp->hspans;
1.64 schwarze 822: while (hspans--) {
823: width += tp->tbl.cols[ic].spacing;
824: ic++;
825: width += tp->tbl.cols[ic].width;
826: }
1.12 schwarze 827:
828: padr = width > len ? width - len : 0;
1.10 schwarze 829: padl = 0;
1.5 schwarze 830:
1.7 schwarze 831: switch (dp->layout->pos) {
1.16 schwarze 832: case TBL_CELL_LONG:
1.10 schwarze 833: padl = term_len(tp, 1);
834: padr = padr > padl ? padr - padl : 0;
1.1 schwarze 835: break;
1.16 schwarze 836: case TBL_CELL_CENTRE:
1.10 schwarze 837: if (2 > padr)
1.8 schwarze 838: break;
1.10 schwarze 839: padl = padr / 2;
1.8 schwarze 840: padr -= padl;
1.1 schwarze 841: break;
1.16 schwarze 842: case TBL_CELL_RIGHT:
1.10 schwarze 843: padl = padr;
844: padr = 0;
1.1 schwarze 845: break;
846: default:
847: break;
848: }
849:
1.50 schwarze 850: tbl_fill_char(tp, ASCII_NBRSP, padl);
1.17 schwarze 851: tbl_word(tp, dp);
1.50 schwarze 852: tbl_fill_char(tp, ASCII_NBRSP, padr);
1.1 schwarze 853: }
854:
1.5 schwarze 855: static void
1.14 schwarze 856: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.5 schwarze 857: const struct tbl_dat *dp,
1.6 schwarze 858: const struct roffcol *col)
1.5 schwarze 859: {
1.48 schwarze 860: const char *cp, *lastdigit, *lastpoint;
861: size_t intsz, padl, totsz;
1.6 schwarze 862: char buf[2];
1.5 schwarze 863:
864: /*
1.48 schwarze 865: * Almost the same code as in tblcalc_number():
866: * First find the position of the decimal point.
1.5 schwarze 867: */
868:
1.7 schwarze 869: assert(dp->string);
1.48 schwarze 870: lastdigit = lastpoint = NULL;
871: for (cp = dp->string; cp[0] != '\0'; cp++) {
872: if (cp[0] == '\\' && cp[1] == '&') {
873: lastdigit = lastpoint = cp;
874: break;
875: } else if (cp[0] == opts->decimal &&
876: (isdigit((unsigned char)cp[1]) ||
877: (cp > dp->string && isdigit((unsigned char)cp[-1]))))
878: lastpoint = cp;
879: else if (isdigit((unsigned char)cp[0]))
880: lastdigit = cp;
881: }
882:
883: /* Then measure both widths. */
1.5 schwarze 884:
1.48 schwarze 885: padl = 0;
886: totsz = term_strlen(tp, dp->string);
887: if (lastdigit != NULL) {
888: if (lastpoint == NULL)
889: lastpoint = lastdigit + 1;
890: intsz = 0;
891: buf[1] = '\0';
892: for (cp = dp->string; cp < lastpoint; cp++) {
893: buf[0] = cp[0];
894: intsz += term_strlen(tp, buf);
895: }
896:
897: /*
898: * Pad left to match the decimal position,
899: * but avoid exceeding the total column width.
900: */
901:
902: if (col->decimal > intsz && col->width > totsz) {
903: padl = col->decimal - intsz;
904: if (padl + totsz > col->width)
905: padl = col->width - totsz;
906: }
1.5 schwarze 907:
1.48 schwarze 908: /* If it is not a number, simply center the string. */
1.5 schwarze 909:
1.48 schwarze 910: } else if (col->width > totsz)
911: padl = (col->width - totsz) / 2;
912:
1.50 schwarze 913: tbl_fill_char(tp, ASCII_NBRSP, padl);
1.17 schwarze 914: tbl_word(tp, dp);
1.48 schwarze 915:
916: /* Pad right to fill the column. */
917:
918: if (col->width > padl + totsz)
1.50 schwarze 919: tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz);
1.5 schwarze 920: }
1.1 schwarze 921:
1.17 schwarze 922: static void
923: tbl_word(struct termp *tp, const struct tbl_dat *dp)
924: {
1.26 schwarze 925: int prev_font;
1.17 schwarze 926:
1.26 schwarze 927: prev_font = tp->fonti;
1.63 schwarze 928: switch (dp->layout->font) {
929: case ESCAPE_FONTBI:
930: term_fontpush(tp, TERMFONT_BI);
931: break;
932: case ESCAPE_FONTBOLD:
933: case ESCAPE_FONTCB:
934: term_fontpush(tp, TERMFONT_BOLD);
935: break;
936: case ESCAPE_FONTITALIC:
937: case ESCAPE_FONTCI:
938: term_fontpush(tp, TERMFONT_UNDER);
939: break;
940: case ESCAPE_FONTROMAN:
941: case ESCAPE_FONTCR:
942: break;
943: default:
944: abort();
945: }
1.17 schwarze 946:
947: term_word(tp, dp->string);
948:
949: term_fontpopq(tp, prev_font);
950: }