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