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