Annotation of src/usr.bin/mandoc/tbl_term.c, Revision 1.39
1.39 ! schwarze 1: /* $OpenBSD: tbl_term.c,v 1.38 2017/06/12 22:48:52 schwarze Exp $ */
1.1 schwarze 2: /*
1.9 schwarze 3: * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.32 schwarze 4: * Copyright (c) 2011,2012,2014,2015,2017 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.17 schwarze 18: #include <sys/types.h>
19:
1.1 schwarze 20: #include <assert.h>
21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
1.5 schwarze 25: #include "mandoc.h"
1.2 schwarze 26: #include "out.h"
27: #include "term.h"
1.1 schwarze 28:
1.6 schwarze 29: static size_t term_tbl_len(size_t, void *);
30: static size_t term_tbl_strlen(const char *, void *);
1.34 schwarze 31: static size_t term_tbl_sulen(const struct roffsu *, void *);
1.6 schwarze 32: static void tbl_char(struct termp *, char, size_t);
1.14 schwarze 33: static void tbl_data(struct termp *, const struct tbl_opts *,
1.16 schwarze 34: const struct tbl_dat *,
1.6 schwarze 35: const struct roffcol *);
1.16 schwarze 36: static void tbl_literal(struct termp *, const struct tbl_dat *,
1.6 schwarze 37: const struct roffcol *);
1.16 schwarze 38: static void tbl_number(struct termp *, const struct tbl_opts *,
39: const struct tbl_dat *,
1.6 schwarze 40: const struct roffcol *);
1.21 schwarze 41: static void tbl_hrule(struct termp *, const struct tbl_span *, int);
1.17 schwarze 42: static void tbl_word(struct termp *, const struct tbl_dat *);
1.1 schwarze 43:
1.6 schwarze 44:
45: static size_t
1.34 schwarze 46: term_tbl_sulen(const struct roffsu *su, void *arg)
47: {
48: return term_hspan((const struct termp *)arg, su) / 24;
49: }
50:
51: static size_t
1.6 schwarze 52: term_tbl_strlen(const char *p, void *arg)
53: {
1.30 schwarze 54: return term_strlen((const struct termp *)arg, p);
1.6 schwarze 55: }
56:
57: static size_t
58: term_tbl_len(size_t sz, void *arg)
59: {
1.30 schwarze 60: return term_len((const struct termp *)arg, sz);
1.6 schwarze 61: }
1.5 schwarze 62:
63: void
64: term_tbl(struct termp *tp, const struct tbl_span *sp)
65: {
1.21 schwarze 66: const struct tbl_cell *cp;
1.6 schwarze 67: const struct tbl_dat *dp;
1.22 schwarze 68: static size_t offset;
1.35 schwarze 69: size_t coloff, tsz;
70: int ic, horiz, spans, vert, more;
71: char fc;
1.27 schwarze 72:
1.5 schwarze 73: /* Inhibit printing of spaces: we do padding ourselves. */
74:
1.35 schwarze 75: tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
1.1 schwarze 76:
77: /*
1.6 schwarze 78: * The first time we're invoked for a given table block,
79: * calculate the table widths and decimal positions.
1.1 schwarze 80: */
81:
1.25 schwarze 82: if (tp->tbl.cols == NULL) {
1.6 schwarze 83: tp->tbl.len = term_tbl_len;
84: tp->tbl.slen = term_tbl_strlen;
1.34 schwarze 85: tp->tbl.sulen = term_tbl_sulen;
1.6 schwarze 86: tp->tbl.arg = tp;
1.5 schwarze 87:
1.36 schwarze 88: tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
1.2 schwarze 89:
1.22 schwarze 90: /* Center the table as a whole. */
91:
1.33 schwarze 92: offset = tp->tcol->offset;
1.22 schwarze 93: if (sp->opts->opts & TBL_OPT_CENTRE) {
94: tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
95: ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
1.24 schwarze 96: for (ic = 0; ic < sp->opts->cols; ic++)
97: tsz += tp->tbl.cols[ic].width + 3;
1.22 schwarze 98: tsz -= 3;
1.33 schwarze 99: if (offset + tsz > tp->tcol->rmargin)
1.22 schwarze 100: tsz -= 1;
1.33 schwarze 101: tp->tcol->offset = offset + tp->tcol->rmargin > tsz ?
102: (offset + tp->tcol->rmargin - tsz) / 2 : 0;
1.22 schwarze 103: }
104:
1.21 schwarze 105: /* Horizontal frame at the start of boxed tables. */
1.1 schwarze 106:
1.21 schwarze 107: if (sp->opts->opts & TBL_OPT_DBOX)
108: tbl_hrule(tp, sp, 2);
109: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
110: tbl_hrule(tp, sp, 1);
1.10 schwarze 111: }
1.5 schwarze 112:
1.35 schwarze 113: /* Set up the columns. */
1.5 schwarze 114:
1.35 schwarze 115: tp->flags |= TERMP_MULTICOL;
116: horiz = 0;
117: switch (sp->pos) {
118: case TBL_SPAN_HORIZ:
119: case TBL_SPAN_DHORIZ:
120: horiz = 1;
121: term_setcol(tp, 1);
122: break;
123: case TBL_SPAN_DATA:
124: term_setcol(tp, sp->opts->cols + 2);
125: coloff = tp->tcol->offset;
126:
127: /* Set up a column for a left vertical frame. */
128:
129: if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
130: sp->opts->lvert)
131: coloff++;
132: tp->tcol->rmargin = coloff;
1.21 schwarze 133:
1.35 schwarze 134: /* Set up the data columns. */
1.1 schwarze 135:
1.35 schwarze 136: dp = sp->first;
137: spans = 0;
138: for (ic = 0; ic < sp->opts->cols; ic++) {
139: if (spans == 0) {
140: tp->tcol++;
141: tp->tcol->offset = coloff;
142: }
143: coloff += tp->tbl.cols[ic].width;
144: tp->tcol->rmargin = coloff;
145: coloff++;
146: if (ic + 1 < sp->opts->cols)
147: coloff += 2;
148: if (spans) {
149: spans--;
150: continue;
151: }
152: if (dp == NULL)
153: continue;
154: spans = dp->spans;
155: dp = dp->next;
156: }
157:
158: /* Set up a column for a right vertical frame. */
159:
160: tp->tcol++;
161: tp->tcol->offset = coloff;
162: if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
163: sp->opts->rvert)
164: coloff++;
165: tp->tcol->rmargin = coloff;
166:
167: /* Spans may have reduced the number of columns. */
168:
169: tp->lasttcol = tp->tcol - tp->tcols;
170:
171: /* Fill the buffers for all data columns. */
1.1 schwarze 172:
1.35 schwarze 173: tp->tcol = tp->tcols;
1.5 schwarze 174: dp = sp->first;
1.7 schwarze 175: spans = 0;
1.24 schwarze 176: for (ic = 0; ic < sp->opts->cols; ic++) {
1.35 schwarze 177: if (spans) {
178: spans--;
179: continue;
180: }
181: tp->tcol++;
182: tp->col = 0;
183: tbl_data(tp, sp->opts, dp, tp->tbl.cols + ic);
184: if (dp == NULL)
185: continue;
186: spans = dp->spans;
187: dp = dp->next;
188: }
189: break;
190: }
191:
192: do {
193: /* Print the vertical frame at the start of each row. */
194:
195: tp->tcol = tp->tcols;
196: fc = '\0';
197: if (sp->layout->vert ||
198: (sp->prev != NULL && sp->prev->layout->vert) ||
199: sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
200: fc = horiz ? '+' : '|';
201: else if (horiz && sp->opts->lvert)
202: fc = '-';
203: if (fc != '\0') {
204: (*tp->advance)(tp, tp->tcols->offset);
205: (*tp->letter)(tp, fc);
206: tp->viscol = tp->tcol->offset + 1;
207: }
1.11 schwarze 208:
1.35 schwarze 209: /* Print the data cells. */
1.10 schwarze 210:
1.35 schwarze 211: more = 0;
212: if (horiz) {
213: tbl_hrule(tp, sp, 0);
214: term_flushln(tp);
215: } else {
216: cp = sp->layout->first;
217: dp = sp->first;
218: spans = 0;
219: for (ic = 0; ic < sp->opts->cols; ic++) {
220:
1.39 ! schwarze 221: /* Advance to next layout cell. */
1.35 schwarze 222:
223: if (cp != NULL) {
224: vert = cp->vert;
225: cp = cp->next;
226: } else
227: vert = 0;
1.39 ! schwarze 228:
! 229: /* Skip later cells in a span. */
! 230:
! 231: if (spans) {
! 232: spans--;
! 233: continue;
! 234: }
! 235:
! 236: /* Advance to next data cell. */
! 237:
! 238: if (dp != NULL) {
! 239: spans = dp->spans;
! 240: dp = dp->next;
! 241: }
! 242:
! 243: /* Print one line of text in the cell. */
! 244:
! 245: tp->tcol++;
! 246: if (tp->tcol->col < tp->tcol->lastcol)
! 247: term_flushln(tp);
! 248: if (tp->tcol->col < tp->tcol->lastcol)
! 249: more = 1;
! 250:
! 251: /*
! 252: * Vertical frames between data cells,
! 253: * but not after the last column.
! 254: */
! 255:
! 256: if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
! 257: continue;
1.38 schwarze 258: if (vert == 0 &&
259: sp->opts->opts & TBL_OPT_ALLBOX)
1.37 schwarze 260: vert = 1;
1.35 schwarze 261: if (vert == 0)
262: continue;
263:
264: if (tp->tcol->rmargin + 1 > tp->viscol) {
265: (*tp->advance)(tp, tp->tcol->rmargin
266: + 1 - tp->viscol);
267: tp->viscol = tp->tcol->rmargin + 1;
268: }
269: while (vert--) {
270: (*tp->letter)(tp, '|');
271: tp->viscol++;
1.21 schwarze 272: }
1.35 schwarze 273: }
274: }
1.5 schwarze 275:
1.35 schwarze 276: /* Print the vertical frame at the end of each row. */
1.7 schwarze 277:
1.35 schwarze 278: fc = '\0';
279: if (sp->layout->last->vert ||
280: (sp->prev != NULL && sp->prev->layout->last->vert) ||
281: (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
282: fc = horiz ? '+' : '|';
283: else if (horiz && sp->opts->rvert)
284: fc = '-';
285: if (fc != '\0') {
286: if (horiz == 0) {
287: tp->tcol++;
288: (*tp->advance)(tp,
289: tp->tcol->offset > tp->viscol ?
290: tp->tcol->offset - tp->viscol : 1);
291: }
292: (*tp->letter)(tp, fc);
293: }
294: (*tp->endline)(tp);
295: tp->viscol = 0;
296: } while (more);
1.1 schwarze 297:
1.5 schwarze 298: /*
299: * If we're the last row, clean up after ourselves: clear the
300: * existing table configuration and set it to NULL.
301: */
1.1 schwarze 302:
1.35 schwarze 303: term_setcol(tp, 1);
304: tp->flags &= ~TERMP_MULTICOL;
305: tp->tcol->rmargin = tp->maxrmargin;
1.25 schwarze 306: if (sp->next == NULL) {
1.21 schwarze 307: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
308: tbl_hrule(tp, sp, 1);
1.13 schwarze 309: tp->skipvsp = 1;
310: }
1.21 schwarze 311: if (sp->opts->opts & TBL_OPT_DBOX) {
312: tbl_hrule(tp, sp, 2);
1.13 schwarze 313: tp->skipvsp = 2;
314: }
1.6 schwarze 315: assert(tp->tbl.cols);
316: free(tp->tbl.cols);
317: tp->tbl.cols = NULL;
1.33 schwarze 318: tp->tcol->offset = offset;
1.38 schwarze 319: } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
320: (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
321: sp->next->next != NULL))
1.37 schwarze 322: tbl_hrule(tp, sp, 1);
323:
1.35 schwarze 324: tp->flags &= ~TERMP_NONOSPACE;
1.1 schwarze 325: }
326:
1.10 schwarze 327: /*
1.21 schwarze 328: * Kinds of horizontal rulers:
329: * 0: inside the table (single or double line with crossings)
330: * 1: inner frame (single line with crossings and ends)
331: * 2: outer frame (single line without crossings with ends)
1.10 schwarze 332: */
1.1 schwarze 333: static void
1.21 schwarze 334: tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
1.1 schwarze 335: {
1.21 schwarze 336: const struct tbl_cell *c1, *c2;
337: int vert;
338: char line, cross;
339:
340: line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
341: cross = (kind < 2) ? '+' : '-';
342:
343: if (kind)
344: term_word(tp, "+");
345: c1 = sp->layout->first;
346: c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
347: if (c2 == c1)
348: c2 = NULL;
349: for (;;) {
1.24 schwarze 350: tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
1.21 schwarze 351: vert = c1->vert;
352: if ((c1 = c1->next) == NULL)
353: break;
354: if (c2 != NULL) {
355: if (vert < c2->vert)
356: vert = c2->vert;
357: c2 = c2->next;
358: }
1.37 schwarze 359: if (sp->opts->opts & TBL_OPT_ALLBOX && !vert)
360: vert = 1;
1.21 schwarze 361: if (vert)
362: tbl_char(tp, cross, vert);
363: if (vert < 2)
364: tbl_char(tp, line, 2 - vert);
1.11 schwarze 365: }
1.21 schwarze 366: if (kind) {
367: term_word(tp, "+");
368: term_flushln(tp);
1.11 schwarze 369: }
1.1 schwarze 370: }
371:
372: static void
1.14 schwarze 373: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.16 schwarze 374: const struct tbl_dat *dp,
375: const struct roffcol *col)
1.1 schwarze 376: {
377:
1.23 schwarze 378: if (dp == NULL) {
1.6 schwarze 379: tbl_char(tp, ASCII_NBRSP, col->width);
1.1 schwarze 380: return;
1.5 schwarze 381: }
1.1 schwarze 382:
1.5 schwarze 383: switch (dp->pos) {
1.16 schwarze 384: case TBL_DATA_NONE:
1.6 schwarze 385: tbl_char(tp, ASCII_NBRSP, col->width);
1.5 schwarze 386: return;
1.16 schwarze 387: case TBL_DATA_HORIZ:
388: case TBL_DATA_NHORIZ:
1.6 schwarze 389: tbl_char(tp, '-', col->width);
1.5 schwarze 390: return;
1.16 schwarze 391: case TBL_DATA_NDHORIZ:
392: case TBL_DATA_DHORIZ:
1.6 schwarze 393: tbl_char(tp, '=', col->width);
1.5 schwarze 394: return;
1.1 schwarze 395: default:
396: break;
397: }
1.16 schwarze 398:
1.7 schwarze 399: switch (dp->layout->pos) {
1.16 schwarze 400: case TBL_CELL_HORIZ:
1.6 schwarze 401: tbl_char(tp, '-', col->width);
1.5 schwarze 402: break;
1.16 schwarze 403: case TBL_CELL_DHORIZ:
1.6 schwarze 404: tbl_char(tp, '=', col->width);
1.1 schwarze 405: break;
1.16 schwarze 406: case TBL_CELL_LONG:
407: case TBL_CELL_CENTRE:
408: case TBL_CELL_LEFT:
409: case TBL_CELL_RIGHT:
1.6 schwarze 410: tbl_literal(tp, dp, col);
1.1 schwarze 411: break;
1.16 schwarze 412: case TBL_CELL_NUMBER:
1.14 schwarze 413: tbl_number(tp, opts, dp, col);
1.4 schwarze 414: break;
1.16 schwarze 415: case TBL_CELL_DOWN:
1.7 schwarze 416: tbl_char(tp, ASCII_NBRSP, col->width);
417: break;
1.1 schwarze 418: default:
419: abort();
420: }
421: }
422:
423: static void
1.6 schwarze 424: tbl_char(struct termp *tp, char c, size_t len)
1.5 schwarze 425: {
1.6 schwarze 426: size_t i, sz;
1.5 schwarze 427: char cp[2];
1.1 schwarze 428:
1.5 schwarze 429: cp[0] = c;
430: cp[1] = '\0';
1.1 schwarze 431:
1.5 schwarze 432: sz = term_strlen(tp, cp);
1.1 schwarze 433:
1.5 schwarze 434: for (i = 0; i < len; i += sz)
435: term_word(tp, cp);
1.1 schwarze 436: }
437:
438: static void
1.16 schwarze 439: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.6 schwarze 440: const struct roffcol *col)
1.1 schwarze 441: {
1.24 schwarze 442: size_t len, padl, padr, width;
443: int ic, spans;
1.1 schwarze 444:
1.7 schwarze 445: assert(dp->string);
1.10 schwarze 446: len = term_strlen(tp, dp->string);
1.12 schwarze 447: width = col->width;
1.24 schwarze 448: ic = dp->layout->col;
449: spans = dp->spans;
450: while (spans--)
451: width += tp->tbl.cols[++ic].width + 3;
1.12 schwarze 452:
453: padr = width > len ? width - len : 0;
1.10 schwarze 454: padl = 0;
1.5 schwarze 455:
1.7 schwarze 456: switch (dp->layout->pos) {
1.16 schwarze 457: case TBL_CELL_LONG:
1.10 schwarze 458: padl = term_len(tp, 1);
459: padr = padr > padl ? padr - padl : 0;
1.1 schwarze 460: break;
1.16 schwarze 461: case TBL_CELL_CENTRE:
1.10 schwarze 462: if (2 > padr)
1.8 schwarze 463: break;
1.10 schwarze 464: padl = padr / 2;
1.8 schwarze 465: padr -= padl;
1.1 schwarze 466: break;
1.16 schwarze 467: case TBL_CELL_RIGHT:
1.10 schwarze 468: padl = padr;
469: padr = 0;
1.1 schwarze 470: break;
471: default:
472: break;
473: }
474:
1.5 schwarze 475: tbl_char(tp, ASCII_NBRSP, padl);
1.17 schwarze 476: tbl_word(tp, dp);
1.10 schwarze 477: tbl_char(tp, ASCII_NBRSP, padr);
1.1 schwarze 478: }
479:
1.5 schwarze 480: static void
1.14 schwarze 481: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.5 schwarze 482: const struct tbl_dat *dp,
1.6 schwarze 483: const struct roffcol *col)
1.5 schwarze 484: {
1.6 schwarze 485: char *cp;
486: char buf[2];
487: size_t sz, psz, ssz, d, padl;
488: int i;
1.5 schwarze 489:
490: /*
491: * See calc_data_number(). Left-pad by taking the offset of our
492: * and the maximum decimal; right-pad by the remaining amount.
493: */
494:
1.7 schwarze 495: assert(dp->string);
1.5 schwarze 496:
1.7 schwarze 497: sz = term_strlen(tp, dp->string);
1.5 schwarze 498:
1.14 schwarze 499: buf[0] = opts->decimal;
1.6 schwarze 500: buf[1] = '\0';
1.5 schwarze 501:
1.6 schwarze 502: psz = term_strlen(tp, buf);
1.5 schwarze 503:
1.23 schwarze 504: if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
1.7 schwarze 505: for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
506: buf[0] = dp->string[i];
1.5 schwarze 507: ssz += term_strlen(tp, buf);
508: }
509: d = ssz + psz;
510: } else
511: d = sz + psz;
512:
1.20 schwarze 513: if (col->decimal > d && col->width > sz) {
514: padl = col->decimal - d;
515: if (padl + sz > col->width)
516: padl = col->width - sz;
517: tbl_char(tp, ASCII_NBRSP, padl);
518: } else
519: padl = 0;
1.17 schwarze 520: tbl_word(tp, dp);
1.10 schwarze 521: if (col->width > sz + padl)
522: tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.5 schwarze 523: }
1.1 schwarze 524:
1.17 schwarze 525: static void
526: tbl_word(struct termp *tp, const struct tbl_dat *dp)
527: {
1.26 schwarze 528: int prev_font;
1.17 schwarze 529:
1.26 schwarze 530: prev_font = tp->fonti;
1.17 schwarze 531: if (dp->layout->flags & TBL_CELL_BOLD)
532: term_fontpush(tp, TERMFONT_BOLD);
533: else if (dp->layout->flags & TBL_CELL_ITALIC)
534: term_fontpush(tp, TERMFONT_UNDER);
535:
536: term_word(tp, dp->string);
537:
538: term_fontpopq(tp, prev_font);
539: }