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