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