Annotation of mandoc/tbl_term.c, Revision 1.37
1.37 ! schwarze 1: /* $Id: tbl_term.c,v 1.36 2015/01/30 04:11:50 schwarze Exp $ */
1.1 kristaps 2: /*
1.20 kristaps 3: * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.33 schwarze 4: * Copyright (c) 2011, 2012, 2014, 2015 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.12 kristaps 33: static void tbl_char(struct termp *, char, size_t);
1.25 schwarze 34: static void tbl_data(struct termp *, const struct tbl_opts *,
1.27 schwarze 35: const struct tbl_dat *,
1.11 kristaps 36: const struct roffcol *);
1.27 schwarze 37: static void tbl_literal(struct termp *, const struct tbl_dat *,
1.11 kristaps 38: const struct roffcol *);
1.27 schwarze 39: static void tbl_number(struct termp *, const struct tbl_opts *,
40: const struct tbl_dat *,
1.11 kristaps 41: const struct roffcol *);
1.33 schwarze 42: static void tbl_hrule(struct termp *, const struct tbl_span *, int);
1.29 schwarze 43: static void tbl_word(struct termp *, const struct tbl_dat *);
1.11 kristaps 44:
45:
46: static size_t
47: term_tbl_strlen(const char *p, void *arg)
48: {
49:
50: return(term_strlen((const struct termp *)arg, p));
51: }
52:
53: static size_t
54: term_tbl_len(size_t sz, void *arg)
55: {
56:
57: return(term_len((const struct termp *)arg, sz));
58: }
1.1 kristaps 59:
60: void
61: term_tbl(struct termp *tp, const struct tbl_span *sp)
62: {
1.33 schwarze 63: const struct tbl_cell *cp;
1.11 kristaps 64: const struct tbl_dat *dp;
1.34 schwarze 65: static size_t offset;
66: size_t rmargin, maxrmargin, tsz;
1.36 schwarze 67: int ic, horiz, spans, vert;
1.11 kristaps 68:
69: rmargin = tp->rmargin;
70: maxrmargin = tp->maxrmargin;
71:
72: tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
1.1 kristaps 73:
1.4 kristaps 74: /* Inhibit printing of spaces: we do padding ourselves. */
75:
76: tp->flags |= TERMP_NONOSPACE;
77: tp->flags |= TERMP_NOSPACE;
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: term_flushln(tp);
86:
87: tp->tbl.len = term_tbl_len;
88: tp->tbl.slen = term_tbl_strlen;
89: tp->tbl.arg = tp;
1.4 kristaps 90:
1.30 schwarze 91: tblcalc(&tp->tbl, sp, rmargin - tp->offset);
1.1 kristaps 92:
1.34 schwarze 93: /* Center the table as a whole. */
94:
95: offset = tp->offset;
96: if (sp->opts->opts & TBL_OPT_CENTRE) {
97: tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
98: ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
1.36 schwarze 99: for (ic = 0; ic < sp->opts->cols; ic++)
100: tsz += tp->tbl.cols[ic].width + 3;
1.34 schwarze 101: tsz -= 3;
102: if (offset + tsz > rmargin)
103: tsz -= 1;
104: tp->offset = (offset + rmargin > tsz) ?
105: (offset + rmargin - tsz) / 2 : 0;
106: }
107:
1.33 schwarze 108: /* Horizontal frame at the start of boxed tables. */
1.4 kristaps 109:
1.33 schwarze 110: if (sp->opts->opts & TBL_OPT_DBOX)
111: tbl_hrule(tp, sp, 2);
112: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
113: tbl_hrule(tp, sp, 1);
1.21 schwarze 114: }
1.1 kristaps 115:
1.4 kristaps 116: /* Vertical frame at the start of each row. */
1.1 kristaps 117:
1.33 schwarze 118: horiz = sp->pos == TBL_SPAN_HORIZ || sp->pos == TBL_SPAN_DHORIZ;
119:
120: if (sp->layout->vert ||
121: (sp->prev != NULL && sp->prev->layout->vert) ||
122: sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
123: term_word(tp, horiz ? "+" : "|");
124: else if (sp->opts->lvert)
125: tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
1.1 kristaps 126:
1.4 kristaps 127: /*
128: * Now print the actual data itself depending on the span type.
1.36 schwarze 129: * Match data cells to column numbers.
1.4 kristaps 130: */
131:
1.33 schwarze 132: if (sp->pos == TBL_SPAN_DATA) {
133: cp = sp->layout->first;
1.4 kristaps 134: dp = sp->first;
1.16 kristaps 135: spans = 0;
1.36 schwarze 136: for (ic = 0; ic < sp->opts->cols; ic++) {
1.22 schwarze 137:
1.27 schwarze 138: /*
1.33 schwarze 139: * Remeber whether we need a vertical bar
140: * after this cell.
1.16 kristaps 141: */
1.11 kristaps 142:
1.33 schwarze 143: vert = cp == NULL ? 0 : cp->vert;
1.21 schwarze 144:
1.33 schwarze 145: /*
146: * Print the data and advance to the next cell.
147: */
1.21 schwarze 148:
1.33 schwarze 149: if (spans == 0) {
1.36 schwarze 150: tbl_data(tp, sp->opts, dp, tp->tbl.cols + ic);
1.33 schwarze 151: if (dp != NULL) {
152: spans = dp->spans;
153: dp = dp->next;
154: }
155: } else
156: spans--;
157: if (cp != NULL)
158: cp = cp->next;
1.1 kristaps 159:
1.27 schwarze 160: /*
1.33 schwarze 161: * Separate columns, except in the middle
162: * of spans and after the last cell.
1.16 kristaps 163: */
164:
1.36 schwarze 165: if (ic + 1 == sp->opts->cols || spans)
1.33 schwarze 166: continue;
167:
168: tbl_char(tp, ASCII_NBRSP, 1);
169: if (vert > 0)
170: tbl_char(tp, '|', vert);
1.36 schwarze 171: if (vert < 2)
1.33 schwarze 172: tbl_char(tp, ASCII_NBRSP, 2 - vert);
1.1 kristaps 173: }
1.33 schwarze 174: } else if (horiz)
175: tbl_hrule(tp, sp, 0);
1.1 kristaps 176:
1.21 schwarze 177: /* Vertical frame at the end of each row. */
178:
1.33 schwarze 179: if (sp->layout->last->vert ||
180: (sp->prev != NULL && sp->prev->layout->last->vert) ||
181: (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
182: term_word(tp, horiz ? "+" : " |");
183: else if (sp->opts->rvert)
184: tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
1.1 kristaps 185: term_flushln(tp);
186:
1.4 kristaps 187: /*
188: * If we're the last row, clean up after ourselves: clear the
189: * existing table configuration and set it to NULL.
190: */
191:
1.37 ! schwarze 192: if (sp->next == NULL) {
1.33 schwarze 193: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
194: tbl_hrule(tp, sp, 1);
1.24 schwarze 195: tp->skipvsp = 1;
196: }
1.33 schwarze 197: if (sp->opts->opts & TBL_OPT_DBOX) {
198: tbl_hrule(tp, sp, 2);
1.24 schwarze 199: tp->skipvsp = 2;
200: }
1.11 kristaps 201: assert(tp->tbl.cols);
202: free(tp->tbl.cols);
203: tp->tbl.cols = NULL;
1.34 schwarze 204: tp->offset = offset;
1.2 kristaps 205: }
1.1 kristaps 206:
207: tp->flags &= ~TERMP_NONOSPACE;
1.11 kristaps 208: tp->rmargin = rmargin;
209: tp->maxrmargin = maxrmargin;
1.1 kristaps 210: }
211:
1.21 schwarze 212: /*
1.33 schwarze 213: * Kinds of horizontal rulers:
214: * 0: inside the table (single or double line with crossings)
215: * 1: inner frame (single line with crossings and ends)
216: * 2: outer frame (single line without crossings with ends)
1.21 schwarze 217: */
1.1 kristaps 218: static void
1.33 schwarze 219: tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
1.1 kristaps 220: {
1.33 schwarze 221: const struct tbl_cell *c1, *c2;
222: int vert;
223: char line, cross;
224:
225: line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
226: cross = (kind < 2) ? '+' : '-';
227:
228: if (kind)
229: term_word(tp, "+");
230: c1 = sp->layout->first;
231: c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
232: if (c2 == c1)
233: c2 = NULL;
234: for (;;) {
1.36 schwarze 235: tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
1.33 schwarze 236: vert = c1->vert;
237: if ((c1 = c1->next) == NULL)
238: break;
239: if (c2 != NULL) {
240: if (vert < c2->vert)
241: vert = c2->vert;
242: c2 = c2->next;
243: }
244: if (vert)
245: tbl_char(tp, cross, vert);
246: if (vert < 2)
247: tbl_char(tp, line, 2 - vert);
1.22 schwarze 248: }
1.33 schwarze 249: if (kind) {
250: term_word(tp, "+");
251: term_flushln(tp);
1.22 schwarze 252: }
1.1 kristaps 253: }
254:
255: static void
1.25 schwarze 256: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.27 schwarze 257: const struct tbl_dat *dp,
258: const struct roffcol *col)
1.1 kristaps 259: {
260:
1.35 schwarze 261: if (dp == NULL) {
1.11 kristaps 262: tbl_char(tp, ASCII_NBRSP, col->width);
1.1 kristaps 263: return;
264: }
265:
266: switch (dp->pos) {
1.27 schwarze 267: case TBL_DATA_NONE:
1.11 kristaps 268: tbl_char(tp, ASCII_NBRSP, col->width);
1.10 kristaps 269: return;
1.27 schwarze 270: case TBL_DATA_HORIZ:
1.1 kristaps 271: /* FALLTHROUGH */
1.27 schwarze 272: case TBL_DATA_NHORIZ:
1.11 kristaps 273: tbl_char(tp, '-', col->width);
1.7 kristaps 274: return;
1.27 schwarze 275: case TBL_DATA_NDHORIZ:
1.7 kristaps 276: /* FALLTHROUGH */
1.27 schwarze 277: case TBL_DATA_DHORIZ:
1.11 kristaps 278: tbl_char(tp, '=', col->width);
1.1 kristaps 279: return;
280: default:
281: break;
282: }
1.27 schwarze 283:
1.16 kristaps 284: switch (dp->layout->pos) {
1.27 schwarze 285: case TBL_CELL_HORIZ:
1.11 kristaps 286: tbl_char(tp, '-', col->width);
1.7 kristaps 287: break;
1.27 schwarze 288: case TBL_CELL_DHORIZ:
1.11 kristaps 289: tbl_char(tp, '=', col->width);
1.1 kristaps 290: break;
1.27 schwarze 291: case TBL_CELL_LONG:
1.1 kristaps 292: /* FALLTHROUGH */
1.27 schwarze 293: case TBL_CELL_CENTRE:
1.1 kristaps 294: /* FALLTHROUGH */
1.27 schwarze 295: case TBL_CELL_LEFT:
1.1 kristaps 296: /* FALLTHROUGH */
1.27 schwarze 297: case TBL_CELL_RIGHT:
1.11 kristaps 298: tbl_literal(tp, dp, col);
1.1 kristaps 299: break;
1.27 schwarze 300: case TBL_CELL_NUMBER:
1.25 schwarze 301: tbl_number(tp, opts, dp, col);
1.18 kristaps 302: break;
1.27 schwarze 303: case TBL_CELL_DOWN:
1.18 kristaps 304: tbl_char(tp, ASCII_NBRSP, col->width);
1.1 kristaps 305: break;
306: default:
307: abort();
308: /* NOTREACHED */
309: }
310: }
311:
312: static void
1.12 kristaps 313: tbl_char(struct termp *tp, char c, size_t len)
1.1 kristaps 314: {
1.12 kristaps 315: size_t i, sz;
316: char cp[2];
317:
318: cp[0] = c;
319: cp[1] = '\0';
1.1 kristaps 320:
1.3 kristaps 321: sz = term_strlen(tp, cp);
322:
323: for (i = 0; i < len; i += sz)
1.1 kristaps 324: term_word(tp, cp);
325: }
326:
327: static void
1.27 schwarze 328: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.11 kristaps 329: const struct roffcol *col)
1.1 kristaps 330: {
1.36 schwarze 331: size_t len, padl, padr, width;
332: int ic, spans;
1.1 kristaps 333:
1.17 kristaps 334: assert(dp->string);
1.21 schwarze 335: len = term_strlen(tp, dp->string);
1.23 schwarze 336: width = col->width;
1.36 schwarze 337: ic = dp->layout->col;
338: spans = dp->spans;
339: while (spans--)
340: width += tp->tbl.cols[++ic].width + 3;
1.23 schwarze 341:
342: padr = width > len ? width - len : 0;
1.21 schwarze 343: padl = 0;
1.1 kristaps 344:
1.16 kristaps 345: switch (dp->layout->pos) {
1.27 schwarze 346: case TBL_CELL_LONG:
1.21 schwarze 347: padl = term_len(tp, 1);
348: padr = padr > padl ? padr - padl : 0;
1.1 kristaps 349: break;
1.27 schwarze 350: case TBL_CELL_CENTRE:
1.21 schwarze 351: if (2 > padr)
1.19 schwarze 352: break;
1.21 schwarze 353: padl = padr / 2;
1.19 schwarze 354: padr -= padl;
1.1 kristaps 355: break;
1.27 schwarze 356: case TBL_CELL_RIGHT:
1.21 schwarze 357: padl = padr;
358: padr = 0;
1.1 kristaps 359: break;
360: default:
361: break;
362: }
363:
364: tbl_char(tp, ASCII_NBRSP, padl);
1.29 schwarze 365: tbl_word(tp, dp);
1.21 schwarze 366: tbl_char(tp, ASCII_NBRSP, padr);
1.1 kristaps 367: }
368:
369: static void
1.25 schwarze 370: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2 kristaps 371: const struct tbl_dat *dp,
1.11 kristaps 372: const struct roffcol *col)
1.1 kristaps 373: {
1.11 kristaps 374: char *cp;
375: char buf[2];
376: size_t sz, psz, ssz, d, padl;
377: int i;
1.1 kristaps 378:
379: /*
380: * See calc_data_number(). Left-pad by taking the offset of our
381: * and the maximum decimal; right-pad by the remaining amount.
382: */
383:
1.17 kristaps 384: assert(dp->string);
1.2 kristaps 385:
1.17 kristaps 386: sz = term_strlen(tp, dp->string);
1.2 kristaps 387:
1.25 schwarze 388: buf[0] = opts->decimal;
1.11 kristaps 389: buf[1] = '\0';
1.2 kristaps 390:
1.11 kristaps 391: psz = term_strlen(tp, buf);
1.10 kristaps 392:
1.35 schwarze 393: if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
1.17 kristaps 394: for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
395: buf[0] = dp->string[i];
1.5 kristaps 396: ssz += term_strlen(tp, buf);
397: }
398: d = ssz + psz;
399: } else
400: d = sz + psz;
1.2 kristaps 401:
1.32 schwarze 402: if (col->decimal > d && col->width > sz) {
403: padl = col->decimal - d;
404: if (padl + sz > col->width)
405: padl = col->width - sz;
406: tbl_char(tp, ASCII_NBRSP, padl);
407: } else
408: padl = 0;
1.29 schwarze 409: tbl_word(tp, dp);
1.21 schwarze 410: if (col->width > sz + padl)
411: tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.2 kristaps 412: }
413:
1.29 schwarze 414: static void
415: tbl_word(struct termp *tp, const struct tbl_dat *dp)
416: {
417: const void *prev_font;
418:
419: prev_font = term_fontq(tp);
420: if (dp->layout->flags & TBL_CELL_BOLD)
421: term_fontpush(tp, TERMFONT_BOLD);
422: else if (dp->layout->flags & TBL_CELL_ITALIC)
423: term_fontpush(tp, TERMFONT_UNDER);
424:
425: term_word(tp, dp->string);
426:
427: term_fontpopq(tp, prev_font);
428: }
CVSweb