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