Annotation of mandoc/tbl_term.c, Revision 1.79
1.79 ! schwarze 1: /* $Id: tbl_term.c,v 1.78 2022/04/26 14:52:05 schwarze Exp $ */
1.1 kristaps 2: /*
1.78 schwarze 3: * Copyright (c) 2011-2022 Ingo Schwarze <schwarze@openbsd.org>
1.20 kristaps 4: * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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:
1.77 schwarze 28: #if DEBUG_MEMORY
29: #include "mandoc_dbg.h"
30: #endif
1.1 kristaps 31: #include "mandoc.h"
1.66 schwarze 32: #include "tbl.h"
1.1 kristaps 33: #include "out.h"
34: #include "term.h"
35:
1.53 schwarze 36: #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \
37: (cp)->pos == TBL_CELL_DHORIZ)
38:
1.62 schwarze 39:
1.11 kristaps 40: static size_t term_tbl_len(size_t, void *);
41: static size_t term_tbl_strlen(const char *, void *);
1.46 schwarze 42: static size_t term_tbl_sulen(const struct roffsu *, void *);
1.25 schwarze 43: static void tbl_data(struct termp *, const struct tbl_opts *,
1.53 schwarze 44: const struct tbl_cell *,
1.27 schwarze 45: const struct tbl_dat *,
1.11 kristaps 46: const struct roffcol *);
1.62 schwarze 47: static void tbl_direct_border(struct termp *, int, size_t);
48: static void tbl_fill_border(struct termp *, int, size_t);
49: static void tbl_fill_char(struct termp *, char, size_t);
50: static void tbl_fill_string(struct termp *, const char *, size_t);
1.64 schwarze 51: static void tbl_hrule(struct termp *, const struct tbl_span *,
1.69 schwarze 52: const struct tbl_span *, const struct tbl_span *,
53: int);
1.27 schwarze 54: static void tbl_literal(struct termp *, const struct tbl_dat *,
1.11 kristaps 55: const struct roffcol *);
1.27 schwarze 56: static void tbl_number(struct termp *, const struct tbl_opts *,
57: const struct tbl_dat *,
1.11 kristaps 58: const struct roffcol *);
1.29 schwarze 59: static void tbl_word(struct termp *, const struct tbl_dat *);
1.11 kristaps 60:
61:
1.62 schwarze 62: /*
63: * The following border-character tables are indexed
64: * by ternary (3-based) numbers, as opposed to binary or decimal.
65: * Each ternary digit describes the line width in one direction:
66: * 0 means no line, 1 single or light line, 2 double or heavy line.
67: */
68:
69: /* Positional values of the four directions. */
70: #define BRIGHT 1
71: #define BDOWN 3
72: #define BLEFT (3 * 3)
73: #define BUP (3 * 3 * 3)
74: #define BHORIZ (BLEFT + BRIGHT)
75:
76: /* Code points to use for each combination of widths. */
77: static const int borders_utf8[81] = {
78: 0x0020, 0x2576, 0x257a, /* 000 right */
79: 0x2577, 0x250c, 0x250d, /* 001 down */
80: 0x257b, 0x250e, 0x250f, /* 002 */
81: 0x2574, 0x2500, 0x257c, /* 010 left */
82: 0x2510, 0x252c, 0x252e, /* 011 left down */
83: 0x2512, 0x2530, 0x2532, /* 012 */
84: 0x2578, 0x257e, 0x2501, /* 020 left */
85: 0x2511, 0x252d, 0x252f, /* 021 left down */
86: 0x2513, 0x2531, 0x2533, /* 022 */
87: 0x2575, 0x2514, 0x2515, /* 100 up */
88: 0x2502, 0x251c, 0x251d, /* 101 up down */
89: 0x257d, 0x251f, 0x2522, /* 102 */
90: 0x2518, 0x2534, 0x2536, /* 110 up left */
91: 0x2524, 0x253c, 0x253e, /* 111 all */
92: 0x2527, 0x2541, 0x2546, /* 112 */
93: 0x2519, 0x2535, 0x2537, /* 120 up left */
94: 0x2525, 0x253d, 0x253f, /* 121 all */
95: 0x252a, 0x2545, 0x2548, /* 122 */
96: 0x2579, 0x2516, 0x2517, /* 200 up */
97: 0x257f, 0x251e, 0x2521, /* 201 up down */
98: 0x2503, 0x2520, 0x2523, /* 202 */
99: 0x251a, 0x2538, 0x253a, /* 210 up left */
100: 0x2526, 0x2540, 0x2544, /* 211 all */
101: 0x2528, 0x2542, 0x254a, /* 212 */
102: 0x251b, 0x2539, 0x253b, /* 220 up left */
103: 0x2529, 0x2543, 0x2547, /* 221 all */
104: 0x252b, 0x2549, 0x254b, /* 222 */
105: };
106:
107: /* ASCII approximations for these code points, compatible with groff. */
108: static const int borders_ascii[81] = {
109: ' ', '-', '=', /* 000 right */
110: '|', '+', '+', /* 001 down */
111: '|', '+', '+', /* 002 */
112: '-', '-', '=', /* 010 left */
113: '+', '+', '+', /* 011 left down */
114: '+', '+', '+', /* 012 */
115: '=', '=', '=', /* 020 left */
116: '+', '+', '+', /* 021 left down */
117: '+', '+', '+', /* 022 */
118: '|', '+', '+', /* 100 up */
119: '|', '+', '+', /* 101 up down */
120: '|', '+', '+', /* 102 */
121: '+', '+', '+', /* 110 up left */
122: '+', '+', '+', /* 111 all */
123: '+', '+', '+', /* 112 */
124: '+', '+', '+', /* 120 up left */
125: '+', '+', '+', /* 121 all */
126: '+', '+', '+', /* 122 */
127: '|', '+', '+', /* 200 up */
128: '|', '+', '+', /* 201 up down */
129: '|', '+', '+', /* 202 */
130: '+', '+', '+', /* 210 up left */
131: '+', '+', '+', /* 211 all */
132: '+', '+', '+', /* 212 */
133: '+', '+', '+', /* 220 up left */
134: '+', '+', '+', /* 221 all */
135: '+', '+', '+', /* 222 */
136: };
137:
138: /* Either of the above according to the selected output encoding. */
139: static const int *borders_locale;
140:
141:
1.11 kristaps 142: static size_t
1.46 schwarze 143: term_tbl_sulen(const struct roffsu *su, void *arg)
144: {
1.57 schwarze 145: int i;
146:
147: i = term_hen((const struct termp *)arg, su);
148: return i > 0 ? i : 0;
1.46 schwarze 149: }
150:
151: static size_t
1.11 kristaps 152: term_tbl_strlen(const char *p, void *arg)
153: {
1.42 schwarze 154: return term_strlen((const struct termp *)arg, p);
1.11 kristaps 155: }
156:
157: static size_t
158: term_tbl_len(size_t sz, void *arg)
159: {
1.42 schwarze 160: return term_len((const struct termp *)arg, sz);
1.11 kristaps 161: }
1.1 kristaps 162:
1.62 schwarze 163:
1.1 kristaps 164: void
165: term_tbl(struct termp *tp, const struct tbl_span *sp)
166: {
1.62 schwarze 167: const struct tbl_cell *cp, *cpn, *cpp, *cps;
1.11 kristaps 168: const struct tbl_dat *dp;
1.34 schwarze 169: static size_t offset;
1.72 schwarze 170: size_t save_offset;
1.47 schwarze 171: size_t coloff, tsz;
1.62 schwarze 172: int hspans, ic, more;
1.68 schwarze 173: int dvert, fc, horiz, lhori, rhori, uvert;
1.39 schwarze 174:
1.4 kristaps 175: /* Inhibit printing of spaces: we do padding ourselves. */
176:
1.47 schwarze 177: tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
1.67 schwarze 178: save_offset = tp->tcol->offset;
1.4 kristaps 179:
180: /*
1.11 kristaps 181: * The first time we're invoked for a given table block,
182: * calculate the table widths and decimal positions.
1.4 kristaps 183: */
184:
1.37 schwarze 185: if (tp->tbl.cols == NULL) {
1.62 schwarze 186: borders_locale = tp->enc == TERMENC_UTF8 ?
187: borders_utf8 : borders_ascii;
188:
1.11 kristaps 189: tp->tbl.len = term_tbl_len;
190: tp->tbl.slen = term_tbl_strlen;
1.46 schwarze 191: tp->tbl.sulen = term_tbl_sulen;
1.11 kristaps 192: tp->tbl.arg = tp;
1.4 kristaps 193:
1.48 schwarze 194: tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
1.1 kristaps 195:
1.34 schwarze 196: /* Center the table as a whole. */
197:
1.45 schwarze 198: offset = tp->tcol->offset;
1.34 schwarze 199: if (sp->opts->opts & TBL_OPT_CENTRE) {
200: tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
201: ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
1.55 schwarze 202: for (ic = 0; ic + 1 < sp->opts->cols; ic++)
203: tsz += tp->tbl.cols[ic].width +
204: tp->tbl.cols[ic].spacing;
205: if (sp->opts->cols)
206: tsz += tp->tbl.cols[sp->opts->cols - 1].width;
1.45 schwarze 207: if (offset + tsz > tp->tcol->rmargin)
1.34 schwarze 208: tsz -= 1;
1.67 schwarze 209: offset = offset + tp->tcol->rmargin > tsz ?
1.45 schwarze 210: (offset + tp->tcol->rmargin - tsz) / 2 : 0;
1.67 schwarze 211: tp->tcol->offset = offset;
1.34 schwarze 212: }
213:
1.33 schwarze 214: /* Horizontal frame at the start of boxed tables. */
1.4 kristaps 215:
1.62 schwarze 216: if (tp->enc == TERMENC_ASCII &&
217: sp->opts->opts & TBL_OPT_DBOX)
1.69 schwarze 218: tbl_hrule(tp, NULL, sp, sp, TBL_OPT_DBOX);
1.53 schwarze 219: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
1.69 schwarze 220: tbl_hrule(tp, NULL, sp, sp, TBL_OPT_BOX);
1.21 schwarze 221: }
1.1 kristaps 222:
1.47 schwarze 223: /* Set up the columns. */
1.1 kristaps 224:
1.47 schwarze 225: tp->flags |= TERMP_MULTICOL;
1.67 schwarze 226: tp->tcol->offset = offset;
1.47 schwarze 227: horiz = 0;
228: switch (sp->pos) {
229: case TBL_SPAN_HORIZ:
230: case TBL_SPAN_DHORIZ:
231: horiz = 1;
232: term_setcol(tp, 1);
233: break;
234: case TBL_SPAN_DATA:
235: term_setcol(tp, sp->opts->cols + 2);
236: coloff = tp->tcol->offset;
237:
238: /* Set up a column for a left vertical frame. */
239:
240: if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
241: sp->opts->lvert)
242: coloff++;
243: tp->tcol->rmargin = coloff;
1.33 schwarze 244:
1.47 schwarze 245: /* Set up the data columns. */
1.1 kristaps 246:
1.47 schwarze 247: dp = sp->first;
1.61 schwarze 248: hspans = 0;
1.47 schwarze 249: for (ic = 0; ic < sp->opts->cols; ic++) {
1.61 schwarze 250: if (hspans == 0) {
1.47 schwarze 251: tp->tcol++;
252: tp->tcol->offset = coloff;
253: }
254: coloff += tp->tbl.cols[ic].width;
255: tp->tcol->rmargin = coloff;
256: if (ic + 1 < sp->opts->cols)
1.55 schwarze 257: coloff += tp->tbl.cols[ic].spacing;
1.61 schwarze 258: if (hspans) {
259: hspans--;
1.47 schwarze 260: continue;
261: }
1.73 schwarze 262: if (dp != NULL &&
263: (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
264: hspans = dp->hspans;
1.56 schwarze 265: dp = dp->next;
1.73 schwarze 266: }
1.47 schwarze 267: }
268:
269: /* Set up a column for a right vertical frame. */
270:
271: tp->tcol++;
1.55 schwarze 272: tp->tcol->offset = coloff + 1;
1.53 schwarze 273: tp->tcol->rmargin = tp->maxrmargin;
1.47 schwarze 274:
275: /* Spans may have reduced the number of columns. */
276:
277: tp->lasttcol = tp->tcol - tp->tcols;
278:
279: /* Fill the buffers for all data columns. */
1.4 kristaps 280:
1.47 schwarze 281: tp->tcol = tp->tcols;
1.53 schwarze 282: cp = cpn = sp->layout->first;
1.4 kristaps 283: dp = sp->first;
1.61 schwarze 284: hspans = 0;
1.36 schwarze 285: for (ic = 0; ic < sp->opts->cols; ic++) {
1.53 schwarze 286: if (cpn != NULL) {
287: cp = cpn;
288: cpn = cpn->next;
289: }
1.61 schwarze 290: if (hspans) {
291: hspans--;
1.47 schwarze 292: continue;
293: }
294: tp->tcol++;
295: tp->col = 0;
1.78 schwarze 296: tp->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE);
1.53 schwarze 297: tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
1.73 schwarze 298: if (dp != NULL &&
299: (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
300: hspans = dp->hspans;
1.56 schwarze 301: dp = dp->next;
1.73 schwarze 302: }
1.47 schwarze 303: }
304: break;
305: }
306:
307: do {
308: /* Print the vertical frame at the start of each row. */
309:
310: tp->tcol = tp->tcols;
1.62 schwarze 311: uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
312: sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
313: if (sp->pos == TBL_SPAN_DATA && uvert < sp->layout->vert)
314: uvert = dvert = sp->layout->vert;
315: if (sp->next != NULL && sp->next->pos == TBL_SPAN_DATA &&
316: dvert < sp->next->layout->vert)
317: dvert = sp->next->layout->vert;
318: if (sp->prev != NULL && uvert < sp->prev->layout->vert &&
319: (horiz || (IS_HORIZ(sp->layout->first) &&
320: !IS_HORIZ(sp->prev->layout->first))))
321: uvert = sp->prev->layout->vert;
1.68 schwarze 322: rhori = sp->pos == TBL_SPAN_DHORIZ ||
323: (sp->first != NULL && sp->first->pos == TBL_DATA_DHORIZ) ||
1.62 schwarze 324: sp->layout->first->pos == TBL_CELL_DHORIZ ? 2 :
325: sp->pos == TBL_SPAN_HORIZ ||
1.68 schwarze 326: (sp->first != NULL && sp->first->pos == TBL_DATA_HORIZ) ||
1.62 schwarze 327: sp->layout->first->pos == TBL_CELL_HORIZ ? 1 : 0;
1.68 schwarze 328: fc = BUP * uvert + BDOWN * dvert + BRIGHT * rhori;
1.62 schwarze 329: if (uvert > 0 || dvert > 0 || (horiz && sp->opts->lvert)) {
1.47 schwarze 330: (*tp->advance)(tp, tp->tcols->offset);
1.62 schwarze 331: tp->viscol = tp->tcol->offset;
332: tbl_direct_border(tp, fc, 1);
1.47 schwarze 333: }
1.22 schwarze 334:
1.47 schwarze 335: /* Print the data cells. */
1.21 schwarze 336:
1.47 schwarze 337: more = 0;
1.62 schwarze 338: if (horiz)
1.69 schwarze 339: tbl_hrule(tp, sp->prev, sp, sp->next, 0);
1.62 schwarze 340: else {
1.47 schwarze 341: cp = sp->layout->first;
1.53 schwarze 342: cpn = sp->next == NULL ? NULL :
343: sp->next->layout->first;
344: cpp = sp->prev == NULL ? NULL :
345: sp->prev->layout->first;
1.47 schwarze 346: dp = sp->first;
1.61 schwarze 347: hspans = 0;
1.47 schwarze 348: for (ic = 0; ic < sp->opts->cols; ic++) {
349:
1.53 schwarze 350: /*
351: * Figure out whether to print a
352: * vertical line after this cell
353: * and advance to next layout cell.
354: */
1.47 schwarze 355:
1.62 schwarze 356: uvert = dvert = fc = 0;
1.47 schwarze 357: if (cp != NULL) {
1.62 schwarze 358: cps = cp;
359: while (cps->next != NULL &&
360: cps->next->pos == TBL_CELL_SPAN)
361: cps = cps->next;
362: if (sp->pos == TBL_SPAN_DATA)
363: uvert = dvert = cps->vert;
1.53 schwarze 364: switch (cp->pos) {
365: case TBL_CELL_HORIZ:
1.62 schwarze 366: fc = BHORIZ;
1.53 schwarze 367: break;
368: case TBL_CELL_DHORIZ:
1.62 schwarze 369: fc = BHORIZ * 2;
1.53 schwarze 370: break;
371: default:
372: break;
373: }
374: }
375: if (cpp != NULL) {
1.62 schwarze 376: if (uvert < cpp->vert &&
1.53 schwarze 377: cp != NULL &&
378: ((IS_HORIZ(cp) &&
379: !IS_HORIZ(cpp)) ||
380: (cp->next != NULL &&
381: cpp->next != NULL &&
382: IS_HORIZ(cp->next) &&
383: !IS_HORIZ(cpp->next))))
1.62 schwarze 384: uvert = cpp->vert;
1.53 schwarze 385: cpp = cpp->next;
386: }
1.62 schwarze 387: if (sp->opts->opts & TBL_OPT_ALLBOX) {
388: if (uvert == 0)
389: uvert = 1;
390: if (dvert == 0)
391: dvert = 1;
392: }
1.53 schwarze 393: if (cpn != NULL) {
1.62 schwarze 394: if (dvert == 0 ||
395: (dvert < cpn->vert &&
396: tp->enc == TERMENC_UTF8))
397: dvert = cpn->vert;
1.53 schwarze 398: cpn = cpn->next;
399: }
1.51 schwarze 400:
1.68 schwarze 401: lhori = (cp != NULL &&
402: cp->pos == TBL_CELL_DHORIZ) ||
403: (dp != NULL &&
404: dp->pos == TBL_DATA_DHORIZ) ? 2 :
405: (cp != NULL &&
406: cp->pos == TBL_CELL_HORIZ) ||
407: (dp != NULL &&
408: dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
409:
1.53 schwarze 410: /*
411: * Skip later cells in a span,
412: * figure out whether to start a span,
413: * and advance to next data cell.
414: */
1.51 schwarze 415:
1.61 schwarze 416: if (hspans) {
417: hspans--;
1.62 schwarze 418: cp = cp->next;
1.51 schwarze 419: continue;
420: }
1.73 schwarze 421: if (dp != NULL && (ic ||
422: sp->layout->first->pos != TBL_CELL_SPAN)) {
1.61 schwarze 423: hspans = dp->hspans;
1.73 schwarze 424: dp = dp->next;
1.51 schwarze 425: }
426:
1.53 schwarze 427: /*
428: * Print one line of text in the cell
429: * and remember whether there is more.
430: */
1.51 schwarze 431:
432: tp->tcol++;
433: if (tp->tcol->col < tp->tcol->lastcol)
434: term_flushln(tp);
435: if (tp->tcol->col < tp->tcol->lastcol)
436: more = 1;
437:
438: /*
439: * Vertical frames between data cells,
440: * but not after the last column.
441: */
442:
1.63 schwarze 443: if (fc == 0 &&
444: ((uvert == 0 && dvert == 0 &&
445: cp != NULL && (cp->next == NULL ||
1.62 schwarze 446: !IS_HORIZ(cp->next))) ||
1.63 schwarze 447: tp->tcol + 1 ==
448: tp->tcols + tp->lasttcol)) {
449: if (cp != NULL)
450: cp = cp->next;
1.53 schwarze 451: continue;
1.62 schwarze 452: }
1.53 schwarze 453:
1.55 schwarze 454: if (tp->viscol < tp->tcol->rmargin) {
1.53 schwarze 455: (*tp->advance)(tp, tp->tcol->rmargin
456: - tp->viscol);
457: tp->viscol = tp->tcol->rmargin;
458: }
1.55 schwarze 459: while (tp->viscol < tp->tcol->rmargin +
1.62 schwarze 460: tp->tbl.cols[ic].spacing / 2)
1.68 schwarze 461: tbl_direct_border(tp,
462: BHORIZ * lhori, 1);
1.53 schwarze 463:
1.51 schwarze 464: if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
465: continue;
1.47 schwarze 466:
1.68 schwarze 467: if (cp != NULL)
1.62 schwarze 468: cp = cp->next;
1.68 schwarze 469:
470: rhori = (cp != NULL &&
471: cp->pos == TBL_CELL_DHORIZ) ||
472: (dp != NULL &&
473: dp->pos == TBL_DATA_DHORIZ) ? 2 :
474: (cp != NULL &&
475: cp->pos == TBL_CELL_HORIZ) ||
476: (dp != NULL &&
477: dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
478:
1.62 schwarze 479: if (tp->tbl.cols[ic].spacing)
1.68 schwarze 480: tbl_direct_border(tp,
481: BLEFT * lhori + BRIGHT * rhori +
1.62 schwarze 482: BUP * uvert + BDOWN * dvert, 1);
483:
484: if (tp->enc == TERMENC_UTF8)
485: uvert = dvert = 0;
1.53 schwarze 486:
1.55 schwarze 487: if (tp->tbl.cols[ic].spacing > 2 &&
1.68 schwarze 488: (uvert > 1 || dvert > 1 || rhori))
489: tbl_direct_border(tp,
490: BHORIZ * rhori +
1.62 schwarze 491: BUP * (uvert > 1) +
492: BDOWN * (dvert > 1), 1);
1.47 schwarze 493: }
494: }
1.1 kristaps 495:
1.47 schwarze 496: /* Print the vertical frame at the end of each row. */
1.16 kristaps 497:
1.62 schwarze 498: uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
499: sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
500: if (sp->pos == TBL_SPAN_DATA &&
501: uvert < sp->layout->last->vert &&
502: sp->layout->last->col + 1 == sp->opts->cols)
503: uvert = dvert = sp->layout->last->vert;
504: if (sp->next != NULL &&
505: dvert < sp->next->layout->last->vert &&
506: sp->next->layout->last->col + 1 == sp->opts->cols)
507: dvert = sp->next->layout->last->vert;
508: if (sp->prev != NULL &&
509: uvert < sp->prev->layout->last->vert &&
510: sp->prev->layout->last->col + 1 == sp->opts->cols &&
511: (horiz || (IS_HORIZ(sp->layout->last) &&
512: !IS_HORIZ(sp->prev->layout->last))))
513: uvert = sp->prev->layout->last->vert;
1.68 schwarze 514: lhori = sp->pos == TBL_SPAN_DHORIZ ||
515: (sp->last != NULL &&
516: sp->last->pos == TBL_DATA_DHORIZ &&
517: sp->last->layout->col + 1 == sp->opts->cols) ||
1.62 schwarze 518: (sp->layout->last->pos == TBL_CELL_DHORIZ &&
519: sp->layout->last->col + 1 == sp->opts->cols) ? 2 :
520: sp->pos == TBL_SPAN_HORIZ ||
1.68 schwarze 521: (sp->last != NULL &&
522: sp->last->pos == TBL_DATA_HORIZ &&
523: sp->last->layout->col + 1 == sp->opts->cols) ||
1.62 schwarze 524: (sp->layout->last->pos == TBL_CELL_HORIZ &&
525: sp->layout->last->col + 1 == sp->opts->cols) ? 1 : 0;
1.68 schwarze 526: fc = BUP * uvert + BDOWN * dvert + BLEFT * lhori;
1.62 schwarze 527: if (uvert > 0 || dvert > 0 || (horiz && sp->opts->rvert)) {
1.53 schwarze 528: if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
529: sp->layout->last->col + 1 < sp->opts->cols)) {
1.47 schwarze 530: tp->tcol++;
1.68 schwarze 531: do {
532: tbl_direct_border(tp,
533: BHORIZ * lhori, 1);
534: } while (tp->viscol < tp->tcol->offset);
1.47 schwarze 535: }
1.62 schwarze 536: tbl_direct_border(tp, fc, 1);
1.47 schwarze 537: }
538: (*tp->endline)(tp);
539: tp->viscol = 0;
540: } while (more);
1.1 kristaps 541:
1.4 kristaps 542: /*
1.53 schwarze 543: * Clean up after this row. If it is the last line
544: * of the table, print the box line and clean up
545: * column data; otherwise, print the allbox line.
1.4 kristaps 546: */
547:
1.47 schwarze 548: term_setcol(tp, 1);
549: tp->flags &= ~TERMP_MULTICOL;
550: tp->tcol->rmargin = tp->maxrmargin;
1.37 schwarze 551: if (sp->next == NULL) {
1.79 ! schwarze 552: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
1.69 schwarze 553: tbl_hrule(tp, sp, sp, NULL, TBL_OPT_BOX);
1.62 schwarze 554: if (tp->enc == TERMENC_ASCII &&
1.79 ! schwarze 555: sp->opts->opts & TBL_OPT_DBOX)
1.69 schwarze 556: tbl_hrule(tp, sp, sp, NULL, TBL_OPT_DBOX);
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;
1.76 schwarze 823: while (hspans--) {
824: width += tp->tbl.cols[ic].spacing;
825: ic++;
826: width += tp->tbl.cols[ic].width;
827: }
1.23 schwarze 828:
829: padr = width > len ? width - len : 0;
1.21 schwarze 830: padl = 0;
1.1 kristaps 831:
1.16 kristaps 832: switch (dp->layout->pos) {
1.27 schwarze 833: case TBL_CELL_LONG:
1.21 schwarze 834: padl = term_len(tp, 1);
835: padr = padr > padl ? padr - padl : 0;
1.1 kristaps 836: break;
1.27 schwarze 837: case TBL_CELL_CENTRE:
1.21 schwarze 838: if (2 > padr)
1.19 schwarze 839: break;
1.21 schwarze 840: padl = padr / 2;
1.19 schwarze 841: padr -= padl;
1.1 kristaps 842: break;
1.27 schwarze 843: case TBL_CELL_RIGHT:
1.21 schwarze 844: padl = padr;
845: padr = 0;
1.1 kristaps 846: break;
847: default:
848: break;
849: }
850:
1.62 schwarze 851: tbl_fill_char(tp, ASCII_NBRSP, padl);
1.29 schwarze 852: tbl_word(tp, dp);
1.62 schwarze 853: tbl_fill_char(tp, ASCII_NBRSP, padr);
1.1 kristaps 854: }
855:
856: static void
1.25 schwarze 857: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2 kristaps 858: const struct tbl_dat *dp,
1.11 kristaps 859: const struct roffcol *col)
1.1 kristaps 860: {
1.60 schwarze 861: const char *cp, *lastdigit, *lastpoint;
862: size_t intsz, padl, totsz;
1.11 kristaps 863: char buf[2];
1.1 kristaps 864:
865: /*
1.60 schwarze 866: * Almost the same code as in tblcalc_number():
867: * First find the position of the decimal point.
1.1 kristaps 868: */
869:
1.17 kristaps 870: assert(dp->string);
1.60 schwarze 871: lastdigit = lastpoint = NULL;
872: for (cp = dp->string; cp[0] != '\0'; cp++) {
873: if (cp[0] == '\\' && cp[1] == '&') {
874: lastdigit = lastpoint = cp;
875: break;
876: } else if (cp[0] == opts->decimal &&
877: (isdigit((unsigned char)cp[1]) ||
878: (cp > dp->string && isdigit((unsigned char)cp[-1]))))
879: lastpoint = cp;
880: else if (isdigit((unsigned char)cp[0]))
881: lastdigit = cp;
882: }
883:
884: /* Then measure both widths. */
1.2 kristaps 885:
1.60 schwarze 886: padl = 0;
887: totsz = term_strlen(tp, dp->string);
888: if (lastdigit != NULL) {
889: if (lastpoint == NULL)
890: lastpoint = lastdigit + 1;
891: intsz = 0;
892: buf[1] = '\0';
893: for (cp = dp->string; cp < lastpoint; cp++) {
894: buf[0] = cp[0];
895: intsz += term_strlen(tp, buf);
896: }
897:
898: /*
899: * Pad left to match the decimal position,
900: * but avoid exceeding the total column width.
901: */
902:
903: if (col->decimal > intsz && col->width > totsz) {
904: padl = col->decimal - intsz;
905: if (padl + totsz > col->width)
906: padl = col->width - totsz;
907: }
1.2 kristaps 908:
1.60 schwarze 909: /* If it is not a number, simply center the string. */
1.2 kristaps 910:
1.60 schwarze 911: } else if (col->width > totsz)
912: padl = (col->width - totsz) / 2;
913:
1.62 schwarze 914: tbl_fill_char(tp, ASCII_NBRSP, padl);
1.29 schwarze 915: tbl_word(tp, dp);
1.60 schwarze 916:
917: /* Pad right to fill the column. */
918:
919: if (col->width > padl + totsz)
1.62 schwarze 920: tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz);
1.2 kristaps 921: }
922:
1.29 schwarze 923: static void
924: tbl_word(struct termp *tp, const struct tbl_dat *dp)
925: {
1.38 schwarze 926: int prev_font;
1.29 schwarze 927:
1.38 schwarze 928: prev_font = tp->fonti;
1.75 schwarze 929: switch (dp->layout->font) {
930: case ESCAPE_FONTBI:
931: term_fontpush(tp, TERMFONT_BI);
932: break;
933: case ESCAPE_FONTBOLD:
934: case ESCAPE_FONTCB:
935: term_fontpush(tp, TERMFONT_BOLD);
936: break;
937: case ESCAPE_FONTITALIC:
938: case ESCAPE_FONTCI:
939: term_fontpush(tp, TERMFONT_UNDER);
940: break;
941: case ESCAPE_FONTROMAN:
942: case ESCAPE_FONTCR:
943: break;
944: default:
945: abort();
946: }
1.29 schwarze 947:
948: term_word(tp, dp->string);
949:
950: term_fontpopq(tp, prev_font);
951: }
CVSweb