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