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