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