Annotation of src/usr.bin/mandoc/tbl_term.c, Revision 1.38
1.38 ! schwarze 1: /* $OpenBSD: tbl_term.c,v 1.37 2017/06/12 20:44:57 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: if (spans == 0) {
221: tp->tcol++;
222: if (dp != NULL) {
223: spans = dp->spans;
224: dp = dp->next;
225: }
226: if (tp->tcol->col < tp->tcol->lastcol)
227: term_flushln(tp);
228: if (tp->tcol->col < tp->tcol->lastcol)
229: more = 1;
230: if (tp->tcol + 1 ==
231: tp->tcols + tp->lasttcol)
232: continue;
233: } else
234: spans--;
235:
236: /* Vertical frames between data cells. */
237:
238: if (cp != NULL) {
239: vert = cp->vert;
240: cp = cp->next;
241: } else
242: vert = 0;
1.38 ! schwarze 243: if (vert == 0 &&
! 244: sp->opts->opts & TBL_OPT_ALLBOX)
1.37 schwarze 245: vert = 1;
1.35 schwarze 246: if (vert == 0)
247: continue;
248:
249: if (tp->tcol->rmargin + 1 > tp->viscol) {
250: (*tp->advance)(tp, tp->tcol->rmargin
251: + 1 - tp->viscol);
252: tp->viscol = tp->tcol->rmargin + 1;
253: }
254: while (vert--) {
255: (*tp->letter)(tp, '|');
256: tp->viscol++;
1.21 schwarze 257: }
1.35 schwarze 258: }
259: }
1.5 schwarze 260:
1.35 schwarze 261: /* Print the vertical frame at the end of each row. */
1.7 schwarze 262:
1.35 schwarze 263: fc = '\0';
264: if (sp->layout->last->vert ||
265: (sp->prev != NULL && sp->prev->layout->last->vert) ||
266: (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
267: fc = horiz ? '+' : '|';
268: else if (horiz && sp->opts->rvert)
269: fc = '-';
270: if (fc != '\0') {
271: if (horiz == 0) {
272: tp->tcol++;
273: (*tp->advance)(tp,
274: tp->tcol->offset > tp->viscol ?
275: tp->tcol->offset - tp->viscol : 1);
276: }
277: (*tp->letter)(tp, fc);
278: }
279: (*tp->endline)(tp);
280: tp->viscol = 0;
281: } while (more);
1.1 schwarze 282:
1.5 schwarze 283: /*
284: * If we're the last row, clean up after ourselves: clear the
285: * existing table configuration and set it to NULL.
286: */
1.1 schwarze 287:
1.35 schwarze 288: term_setcol(tp, 1);
289: tp->flags &= ~TERMP_MULTICOL;
290: tp->tcol->rmargin = tp->maxrmargin;
1.25 schwarze 291: if (sp->next == NULL) {
1.21 schwarze 292: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
293: tbl_hrule(tp, sp, 1);
1.13 schwarze 294: tp->skipvsp = 1;
295: }
1.21 schwarze 296: if (sp->opts->opts & TBL_OPT_DBOX) {
297: tbl_hrule(tp, sp, 2);
1.13 schwarze 298: tp->skipvsp = 2;
299: }
1.6 schwarze 300: assert(tp->tbl.cols);
301: free(tp->tbl.cols);
302: tp->tbl.cols = NULL;
1.33 schwarze 303: tp->tcol->offset = offset;
1.38 ! schwarze 304: } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
! 305: (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
! 306: sp->next->next != NULL))
1.37 schwarze 307: tbl_hrule(tp, sp, 1);
308:
1.35 schwarze 309: tp->flags &= ~TERMP_NONOSPACE;
1.1 schwarze 310: }
311:
1.10 schwarze 312: /*
1.21 schwarze 313: * Kinds of horizontal rulers:
314: * 0: inside the table (single or double line with crossings)
315: * 1: inner frame (single line with crossings and ends)
316: * 2: outer frame (single line without crossings with ends)
1.10 schwarze 317: */
1.1 schwarze 318: static void
1.21 schwarze 319: tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
1.1 schwarze 320: {
1.21 schwarze 321: const struct tbl_cell *c1, *c2;
322: int vert;
323: char line, cross;
324:
325: line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
326: cross = (kind < 2) ? '+' : '-';
327:
328: if (kind)
329: term_word(tp, "+");
330: c1 = sp->layout->first;
331: c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
332: if (c2 == c1)
333: c2 = NULL;
334: for (;;) {
1.24 schwarze 335: tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
1.21 schwarze 336: vert = c1->vert;
337: if ((c1 = c1->next) == NULL)
338: break;
339: if (c2 != NULL) {
340: if (vert < c2->vert)
341: vert = c2->vert;
342: c2 = c2->next;
343: }
1.37 schwarze 344: if (sp->opts->opts & TBL_OPT_ALLBOX && !vert)
345: vert = 1;
1.21 schwarze 346: if (vert)
347: tbl_char(tp, cross, vert);
348: if (vert < 2)
349: tbl_char(tp, line, 2 - vert);
1.11 schwarze 350: }
1.21 schwarze 351: if (kind) {
352: term_word(tp, "+");
353: term_flushln(tp);
1.11 schwarze 354: }
1.1 schwarze 355: }
356:
357: static void
1.14 schwarze 358: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.16 schwarze 359: const struct tbl_dat *dp,
360: const struct roffcol *col)
1.1 schwarze 361: {
362:
1.23 schwarze 363: if (dp == NULL) {
1.6 schwarze 364: tbl_char(tp, ASCII_NBRSP, col->width);
1.1 schwarze 365: return;
1.5 schwarze 366: }
1.1 schwarze 367:
1.5 schwarze 368: switch (dp->pos) {
1.16 schwarze 369: case TBL_DATA_NONE:
1.6 schwarze 370: tbl_char(tp, ASCII_NBRSP, col->width);
1.5 schwarze 371: return;
1.16 schwarze 372: case TBL_DATA_HORIZ:
373: case TBL_DATA_NHORIZ:
1.6 schwarze 374: tbl_char(tp, '-', col->width);
1.5 schwarze 375: return;
1.16 schwarze 376: case TBL_DATA_NDHORIZ:
377: case TBL_DATA_DHORIZ:
1.6 schwarze 378: tbl_char(tp, '=', col->width);
1.5 schwarze 379: return;
1.1 schwarze 380: default:
381: break;
382: }
1.16 schwarze 383:
1.7 schwarze 384: switch (dp->layout->pos) {
1.16 schwarze 385: case TBL_CELL_HORIZ:
1.6 schwarze 386: tbl_char(tp, '-', col->width);
1.5 schwarze 387: break;
1.16 schwarze 388: case TBL_CELL_DHORIZ:
1.6 schwarze 389: tbl_char(tp, '=', col->width);
1.1 schwarze 390: break;
1.16 schwarze 391: case TBL_CELL_LONG:
392: case TBL_CELL_CENTRE:
393: case TBL_CELL_LEFT:
394: case TBL_CELL_RIGHT:
1.6 schwarze 395: tbl_literal(tp, dp, col);
1.1 schwarze 396: break;
1.16 schwarze 397: case TBL_CELL_NUMBER:
1.14 schwarze 398: tbl_number(tp, opts, dp, col);
1.4 schwarze 399: break;
1.16 schwarze 400: case TBL_CELL_DOWN:
1.7 schwarze 401: tbl_char(tp, ASCII_NBRSP, col->width);
402: break;
1.1 schwarze 403: default:
404: abort();
405: }
406: }
407:
408: static void
1.6 schwarze 409: tbl_char(struct termp *tp, char c, size_t len)
1.5 schwarze 410: {
1.6 schwarze 411: size_t i, sz;
1.5 schwarze 412: char cp[2];
1.1 schwarze 413:
1.5 schwarze 414: cp[0] = c;
415: cp[1] = '\0';
1.1 schwarze 416:
1.5 schwarze 417: sz = term_strlen(tp, cp);
1.1 schwarze 418:
1.5 schwarze 419: for (i = 0; i < len; i += sz)
420: term_word(tp, cp);
1.1 schwarze 421: }
422:
423: static void
1.16 schwarze 424: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.6 schwarze 425: const struct roffcol *col)
1.1 schwarze 426: {
1.24 schwarze 427: size_t len, padl, padr, width;
428: int ic, spans;
1.1 schwarze 429:
1.7 schwarze 430: assert(dp->string);
1.10 schwarze 431: len = term_strlen(tp, dp->string);
1.12 schwarze 432: width = col->width;
1.24 schwarze 433: ic = dp->layout->col;
434: spans = dp->spans;
435: while (spans--)
436: width += tp->tbl.cols[++ic].width + 3;
1.12 schwarze 437:
438: padr = width > len ? width - len : 0;
1.10 schwarze 439: padl = 0;
1.5 schwarze 440:
1.7 schwarze 441: switch (dp->layout->pos) {
1.16 schwarze 442: case TBL_CELL_LONG:
1.10 schwarze 443: padl = term_len(tp, 1);
444: padr = padr > padl ? padr - padl : 0;
1.1 schwarze 445: break;
1.16 schwarze 446: case TBL_CELL_CENTRE:
1.10 schwarze 447: if (2 > padr)
1.8 schwarze 448: break;
1.10 schwarze 449: padl = padr / 2;
1.8 schwarze 450: padr -= padl;
1.1 schwarze 451: break;
1.16 schwarze 452: case TBL_CELL_RIGHT:
1.10 schwarze 453: padl = padr;
454: padr = 0;
1.1 schwarze 455: break;
456: default:
457: break;
458: }
459:
1.5 schwarze 460: tbl_char(tp, ASCII_NBRSP, padl);
1.17 schwarze 461: tbl_word(tp, dp);
1.10 schwarze 462: tbl_char(tp, ASCII_NBRSP, padr);
1.1 schwarze 463: }
464:
1.5 schwarze 465: static void
1.14 schwarze 466: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.5 schwarze 467: const struct tbl_dat *dp,
1.6 schwarze 468: const struct roffcol *col)
1.5 schwarze 469: {
1.6 schwarze 470: char *cp;
471: char buf[2];
472: size_t sz, psz, ssz, d, padl;
473: int i;
1.5 schwarze 474:
475: /*
476: * See calc_data_number(). Left-pad by taking the offset of our
477: * and the maximum decimal; right-pad by the remaining amount.
478: */
479:
1.7 schwarze 480: assert(dp->string);
1.5 schwarze 481:
1.7 schwarze 482: sz = term_strlen(tp, dp->string);
1.5 schwarze 483:
1.14 schwarze 484: buf[0] = opts->decimal;
1.6 schwarze 485: buf[1] = '\0';
1.5 schwarze 486:
1.6 schwarze 487: psz = term_strlen(tp, buf);
1.5 schwarze 488:
1.23 schwarze 489: if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
1.7 schwarze 490: for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
491: buf[0] = dp->string[i];
1.5 schwarze 492: ssz += term_strlen(tp, buf);
493: }
494: d = ssz + psz;
495: } else
496: d = sz + psz;
497:
1.20 schwarze 498: if (col->decimal > d && col->width > sz) {
499: padl = col->decimal - d;
500: if (padl + sz > col->width)
501: padl = col->width - sz;
502: tbl_char(tp, ASCII_NBRSP, padl);
503: } else
504: padl = 0;
1.17 schwarze 505: tbl_word(tp, dp);
1.10 schwarze 506: if (col->width > sz + padl)
507: tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.5 schwarze 508: }
1.1 schwarze 509:
1.17 schwarze 510: static void
511: tbl_word(struct termp *tp, const struct tbl_dat *dp)
512: {
1.26 schwarze 513: int prev_font;
1.17 schwarze 514:
1.26 schwarze 515: prev_font = tp->fonti;
1.17 schwarze 516: if (dp->layout->flags & TBL_CELL_BOLD)
517: term_fontpush(tp, TERMFONT_BOLD);
518: else if (dp->layout->flags & TBL_CELL_ITALIC)
519: term_fontpush(tp, TERMFONT_UNDER);
520:
521: term_word(tp, dp->string);
522:
523: term_fontpopq(tp, prev_font);
524: }