Annotation of mandoc/tbl_term.c, Revision 1.55
1.55 ! schwarze 1: /* $Id: tbl_term.c,v 1.54 2017/06/17 14:55:30 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.53 schwarze 31: #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \
32: (cp)->pos == TBL_CELL_DHORIZ)
33:
1.11 kristaps 34: static size_t term_tbl_len(size_t, void *);
35: static size_t term_tbl_strlen(const char *, void *);
1.46 schwarze 36: static size_t term_tbl_sulen(const struct roffsu *, void *);
1.12 kristaps 37: static void tbl_char(struct termp *, char, size_t);
1.25 schwarze 38: static void tbl_data(struct termp *, const struct tbl_opts *,
1.53 schwarze 39: const struct tbl_cell *,
1.27 schwarze 40: const struct tbl_dat *,
1.11 kristaps 41: const struct roffcol *);
1.27 schwarze 42: static void tbl_literal(struct termp *, const struct tbl_dat *,
1.11 kristaps 43: const struct roffcol *);
1.27 schwarze 44: static void tbl_number(struct termp *, const struct tbl_opts *,
45: const struct tbl_dat *,
1.11 kristaps 46: const struct roffcol *);
1.33 schwarze 47: static void tbl_hrule(struct termp *, const struct tbl_span *, int);
1.29 schwarze 48: static void tbl_word(struct termp *, const struct tbl_dat *);
1.11 kristaps 49:
50:
51: static size_t
1.46 schwarze 52: term_tbl_sulen(const struct roffsu *su, void *arg)
53: {
1.52 schwarze 54: return term_hen((const struct termp *)arg, su);
1.46 schwarze 55: }
56:
57: static size_t
1.11 kristaps 58: term_tbl_strlen(const char *p, void *arg)
59: {
1.42 schwarze 60: return term_strlen((const struct termp *)arg, p);
1.11 kristaps 61: }
62:
63: static size_t
64: term_tbl_len(size_t sz, void *arg)
65: {
1.42 schwarze 66: return term_len((const struct termp *)arg, sz);
1.11 kristaps 67: }
1.1 kristaps 68:
69: void
70: term_tbl(struct termp *tp, const struct tbl_span *sp)
71: {
1.53 schwarze 72: const struct tbl_cell *cp, *cpn, *cpp;
1.11 kristaps 73: const struct tbl_dat *dp;
1.34 schwarze 74: static size_t offset;
1.47 schwarze 75: size_t coloff, tsz;
76: int ic, horiz, spans, vert, more;
77: char fc;
1.39 schwarze 78:
1.4 kristaps 79: /* Inhibit printing of spaces: we do padding ourselves. */
80:
1.47 schwarze 81: tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
1.4 kristaps 82:
83: /*
1.11 kristaps 84: * The first time we're invoked for a given table block,
85: * calculate the table widths and decimal positions.
1.4 kristaps 86: */
87:
1.37 schwarze 88: if (tp->tbl.cols == NULL) {
1.11 kristaps 89: tp->tbl.len = term_tbl_len;
90: tp->tbl.slen = term_tbl_strlen;
1.46 schwarze 91: tp->tbl.sulen = term_tbl_sulen;
1.11 kristaps 92: tp->tbl.arg = tp;
1.4 kristaps 93:
1.48 schwarze 94: tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
1.54 schwarze 95:
96: /* Tables leak .ta settings to subsequent text. */
97:
98: term_tab_set(tp, NULL);
99: coloff = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
100: sp->opts->lvert;
101: for (ic = 0; ic < sp->opts->cols; ic++) {
102: coloff += tp->tbl.cols[ic].width;
103: term_tab_iset(coloff);
1.55 ! schwarze 104: coloff += tp->tbl.cols[ic].spacing;
1.54 schwarze 105: }
1.1 kristaps 106:
1.34 schwarze 107: /* Center the table as a whole. */
108:
1.45 schwarze 109: offset = tp->tcol->offset;
1.34 schwarze 110: if (sp->opts->opts & TBL_OPT_CENTRE) {
111: tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
112: ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
1.55 ! schwarze 113: for (ic = 0; ic + 1 < sp->opts->cols; ic++)
! 114: tsz += tp->tbl.cols[ic].width +
! 115: tp->tbl.cols[ic].spacing;
! 116: if (sp->opts->cols)
! 117: tsz += tp->tbl.cols[sp->opts->cols - 1].width;
1.45 schwarze 118: if (offset + tsz > tp->tcol->rmargin)
1.34 schwarze 119: tsz -= 1;
1.45 schwarze 120: tp->tcol->offset = offset + tp->tcol->rmargin > tsz ?
121: (offset + tp->tcol->rmargin - tsz) / 2 : 0;
1.34 schwarze 122: }
123:
1.33 schwarze 124: /* Horizontal frame at the start of boxed tables. */
1.4 kristaps 125:
1.33 schwarze 126: if (sp->opts->opts & TBL_OPT_DBOX)
1.53 schwarze 127: tbl_hrule(tp, sp, 3);
128: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
1.33 schwarze 129: tbl_hrule(tp, sp, 2);
1.21 schwarze 130: }
1.1 kristaps 131:
1.47 schwarze 132: /* Set up the columns. */
1.1 kristaps 133:
1.47 schwarze 134: tp->flags |= TERMP_MULTICOL;
135: horiz = 0;
136: switch (sp->pos) {
137: case TBL_SPAN_HORIZ:
138: case TBL_SPAN_DHORIZ:
139: horiz = 1;
140: term_setcol(tp, 1);
141: break;
142: case TBL_SPAN_DATA:
143: term_setcol(tp, sp->opts->cols + 2);
144: coloff = tp->tcol->offset;
145:
146: /* Set up a column for a left vertical frame. */
147:
148: if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
149: sp->opts->lvert)
150: coloff++;
151: tp->tcol->rmargin = coloff;
1.33 schwarze 152:
1.47 schwarze 153: /* Set up the data columns. */
1.1 kristaps 154:
1.47 schwarze 155: dp = sp->first;
156: spans = 0;
157: for (ic = 0; ic < sp->opts->cols; ic++) {
158: if (spans == 0) {
159: tp->tcol++;
160: tp->tcol->offset = coloff;
161: }
162: coloff += tp->tbl.cols[ic].width;
163: tp->tcol->rmargin = coloff;
164: if (ic + 1 < sp->opts->cols)
1.55 ! schwarze 165: coloff += tp->tbl.cols[ic].spacing;
1.47 schwarze 166: if (spans) {
167: spans--;
168: continue;
169: }
170: if (dp == NULL)
171: continue;
172: spans = dp->spans;
173: dp = dp->next;
174: }
175:
176: /* Set up a column for a right vertical frame. */
177:
178: tp->tcol++;
1.55 ! schwarze 179: tp->tcol->offset = coloff + 1;
1.53 schwarze 180: tp->tcol->rmargin = tp->maxrmargin;
1.47 schwarze 181:
182: /* Spans may have reduced the number of columns. */
183:
184: tp->lasttcol = tp->tcol - tp->tcols;
185:
186: /* Fill the buffers for all data columns. */
1.4 kristaps 187:
1.47 schwarze 188: tp->tcol = tp->tcols;
1.53 schwarze 189: cp = cpn = sp->layout->first;
1.4 kristaps 190: dp = sp->first;
1.16 kristaps 191: spans = 0;
1.36 schwarze 192: for (ic = 0; ic < sp->opts->cols; ic++) {
1.53 schwarze 193: if (cpn != NULL) {
194: cp = cpn;
195: cpn = cpn->next;
196: }
1.47 schwarze 197: if (spans) {
198: spans--;
199: continue;
200: }
201: tp->tcol++;
202: tp->col = 0;
1.53 schwarze 203: tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
1.47 schwarze 204: if (dp == NULL)
205: continue;
206: spans = dp->spans;
207: dp = dp->next;
208: }
209: break;
210: }
211:
212: do {
213: /* Print the vertical frame at the start of each row. */
214:
215: tp->tcol = tp->tcols;
216: fc = '\0';
217: if (sp->layout->vert ||
1.53 schwarze 218: (sp->next != NULL && sp->next->layout->vert &&
219: sp->next->pos == TBL_SPAN_DATA) ||
220: (sp->prev != NULL && sp->prev->layout->vert &&
221: (horiz || (IS_HORIZ(sp->layout->first) &&
222: !IS_HORIZ(sp->prev->layout->first)))) ||
1.47 schwarze 223: sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
1.53 schwarze 224: fc = horiz || IS_HORIZ(sp->layout->first) ? '+' : '|';
1.47 schwarze 225: else if (horiz && sp->opts->lvert)
226: fc = '-';
227: if (fc != '\0') {
228: (*tp->advance)(tp, tp->tcols->offset);
229: (*tp->letter)(tp, fc);
230: tp->viscol = tp->tcol->offset + 1;
231: }
1.22 schwarze 232:
1.47 schwarze 233: /* Print the data cells. */
1.21 schwarze 234:
1.47 schwarze 235: more = 0;
236: if (horiz) {
237: tbl_hrule(tp, sp, 0);
238: term_flushln(tp);
239: } else {
240: cp = sp->layout->first;
1.53 schwarze 241: cpn = sp->next == NULL ? NULL :
242: sp->next->layout->first;
243: cpp = sp->prev == NULL ? NULL :
244: sp->prev->layout->first;
1.47 schwarze 245: dp = sp->first;
246: spans = 0;
247: for (ic = 0; ic < sp->opts->cols; ic++) {
248:
1.53 schwarze 249: /*
250: * Figure out whether to print a
251: * vertical line after this cell
252: * and advance to next layout cell.
253: */
1.47 schwarze 254:
255: if (cp != NULL) {
256: vert = cp->vert;
1.53 schwarze 257: switch (cp->pos) {
258: case TBL_CELL_HORIZ:
259: fc = '-';
260: break;
261: case TBL_CELL_DHORIZ:
262: fc = '=';
263: break;
264: default:
265: fc = ' ';
266: break;
267: }
268: } else {
269: vert = 0;
270: fc = ' ';
271: }
272: if (cpp != NULL) {
273: if (vert == 0 &&
274: cp != NULL &&
275: ((IS_HORIZ(cp) &&
276: !IS_HORIZ(cpp)) ||
277: (cp->next != NULL &&
278: cpp->next != NULL &&
279: IS_HORIZ(cp->next) &&
280: !IS_HORIZ(cpp->next))))
281: vert = cpp->vert;
282: cpp = cpp->next;
283: }
284: if (vert == 0 &&
285: sp->opts->opts & TBL_OPT_ALLBOX)
286: vert = 1;
287: if (cpn != NULL) {
288: if (vert == 0)
289: vert = cpn->vert;
290: cpn = cpn->next;
291: }
292: if (cp != NULL)
1.47 schwarze 293: cp = cp->next;
1.51 schwarze 294:
1.53 schwarze 295: /*
296: * Skip later cells in a span,
297: * figure out whether to start a span,
298: * and advance to next data cell.
299: */
1.51 schwarze 300:
301: if (spans) {
302: spans--;
303: continue;
304: }
305: if (dp != NULL) {
306: spans = dp->spans;
307: dp = dp->next;
308: }
309:
1.53 schwarze 310: /*
311: * Print one line of text in the cell
312: * and remember whether there is more.
313: */
1.51 schwarze 314:
315: tp->tcol++;
316: if (tp->tcol->col < tp->tcol->lastcol)
317: term_flushln(tp);
318: if (tp->tcol->col < tp->tcol->lastcol)
319: more = 1;
320:
321: /*
322: * Vertical frames between data cells,
323: * but not after the last column.
324: */
325:
1.53 schwarze 326: if (fc == ' ' && ((vert == 0 &&
327: (cp == NULL || !IS_HORIZ(cp))) ||
328: tp->tcol + 1 == tp->tcols + tp->lasttcol))
329: continue;
330:
1.55 ! schwarze 331: if (tp->viscol < tp->tcol->rmargin) {
1.53 schwarze 332: (*tp->advance)(tp, tp->tcol->rmargin
333: - tp->viscol);
334: tp->viscol = tp->tcol->rmargin;
335: }
1.55 ! schwarze 336: while (tp->viscol < tp->tcol->rmargin +
! 337: tp->tbl.cols[ic].spacing / 2) {
1.53 schwarze 338: (*tp->letter)(tp, fc);
339: tp->viscol++;
340: }
341:
1.51 schwarze 342: if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
343: continue;
1.47 schwarze 344:
1.53 schwarze 345: if (fc == ' ' && cp != NULL) {
346: switch (cp->pos) {
347: case TBL_CELL_HORIZ:
348: fc = '-';
349: break;
350: case TBL_CELL_DHORIZ:
351: fc = '=';
352: break;
353: default:
354: break;
355: }
356: }
1.55 ! schwarze 357: if (tp->tbl.cols[ic].spacing) {
! 358: (*tp->letter)(tp, fc == ' ' ? '|' :
! 359: vert ? '+' : fc);
! 360: tp->viscol++;
! 361: }
1.53 schwarze 362:
363: if (fc != ' ') {
364: if (cp != NULL &&
365: cp->pos == TBL_CELL_HORIZ)
366: fc = '-';
367: else if (cp != NULL &&
368: cp->pos == TBL_CELL_DHORIZ)
369: fc = '=';
370: else
371: fc = ' ';
1.47 schwarze 372: }
1.55 ! schwarze 373: if (tp->tbl.cols[ic].spacing > 2 &&
! 374: (vert > 1 || fc != ' ')) {
1.53 schwarze 375: (*tp->letter)(tp, fc == ' ' ? '|' :
376: vert > 1 ? '+' : fc);
1.47 schwarze 377: tp->viscol++;
1.33 schwarze 378: }
1.47 schwarze 379: }
380: }
1.1 kristaps 381:
1.47 schwarze 382: /* Print the vertical frame at the end of each row. */
1.16 kristaps 383:
1.47 schwarze 384: fc = '\0';
1.53 schwarze 385: if ((sp->layout->last->vert &&
386: sp->layout->last->col + 1 == sp->opts->cols) ||
387: (sp->next != NULL &&
388: sp->next->layout->last->vert &&
389: sp->next->layout->last->col + 1 == sp->opts->cols) ||
390: (sp->prev != NULL &&
391: sp->prev->layout->last->vert &&
392: sp->prev->layout->last->col + 1 == sp->opts->cols &&
393: (horiz || (IS_HORIZ(sp->layout->last) &&
394: !IS_HORIZ(sp->prev->layout->last)))) ||
1.47 schwarze 395: (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
1.53 schwarze 396: fc = horiz || IS_HORIZ(sp->layout->last) ? '+' : '|';
1.47 schwarze 397: else if (horiz && sp->opts->rvert)
398: fc = '-';
399: if (fc != '\0') {
1.53 schwarze 400: if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
401: sp->layout->last->col + 1 < sp->opts->cols)) {
1.47 schwarze 402: tp->tcol++;
403: (*tp->advance)(tp,
404: tp->tcol->offset > tp->viscol ?
405: tp->tcol->offset - tp->viscol : 1);
406: }
407: (*tp->letter)(tp, fc);
408: }
409: (*tp->endline)(tp);
410: tp->viscol = 0;
411: } while (more);
1.1 kristaps 412:
1.4 kristaps 413: /*
1.53 schwarze 414: * Clean up after this row. If it is the last line
415: * of the table, print the box line and clean up
416: * column data; otherwise, print the allbox line.
1.4 kristaps 417: */
418:
1.47 schwarze 419: term_setcol(tp, 1);
420: tp->flags &= ~TERMP_MULTICOL;
421: tp->tcol->rmargin = tp->maxrmargin;
1.37 schwarze 422: if (sp->next == NULL) {
1.33 schwarze 423: if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
1.53 schwarze 424: tbl_hrule(tp, sp, 2);
1.24 schwarze 425: tp->skipvsp = 1;
426: }
1.33 schwarze 427: if (sp->opts->opts & TBL_OPT_DBOX) {
1.53 schwarze 428: tbl_hrule(tp, sp, 3);
1.24 schwarze 429: tp->skipvsp = 2;
430: }
1.11 kristaps 431: assert(tp->tbl.cols);
432: free(tp->tbl.cols);
433: tp->tbl.cols = NULL;
1.45 schwarze 434: tp->tcol->offset = offset;
1.50 schwarze 435: } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
436: (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
437: sp->next->next != NULL))
1.49 schwarze 438: tbl_hrule(tp, sp, 1);
439:
1.47 schwarze 440: tp->flags &= ~TERMP_NONOSPACE;
1.1 kristaps 441: }
442:
1.21 schwarze 443: /*
1.33 schwarze 444: * Kinds of horizontal rulers:
445: * 0: inside the table (single or double line with crossings)
1.53 schwarze 446: * 1: inside the table (single or double line with crossings and ends)
447: * 2: inner frame (single line with crossings and ends)
448: * 3: outer frame (single line without crossings with ends)
1.21 schwarze 449: */
1.1 kristaps 450: static void
1.33 schwarze 451: tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
1.1 kristaps 452: {
1.53 schwarze 453: const struct tbl_cell *cp, *cpn, *cpp;
1.55 ! schwarze 454: const struct roffcol *col;
1.33 schwarze 455: int vert;
456: char line, cross;
457:
1.53 schwarze 458: line = (kind < 2 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
459: cross = (kind < 3) ? '+' : '-';
1.33 schwarze 460:
461: if (kind)
462: term_word(tp, "+");
1.53 schwarze 463: cp = sp->layout->first;
464: cpp = kind || sp->prev == NULL ? NULL : sp->prev->layout->first;
465: if (cpp == cp)
466: cpp = NULL;
467: cpn = kind > 1 || sp->next == NULL ? NULL : sp->next->layout->first;
468: if (cpn == cp)
469: cpn = NULL;
1.33 schwarze 470: for (;;) {
1.55 ! schwarze 471: col = tp->tbl.cols + cp->col;
! 472: tbl_char(tp, line, col->width + col->spacing / 2);
1.53 schwarze 473: vert = cp->vert;
474: if ((cp = cp->next) == NULL)
1.33 schwarze 475: break;
1.53 schwarze 476: if (cpp != NULL) {
477: if (vert < cpp->vert)
478: vert = cpp->vert;
479: cpp = cpp->next;
480: }
481: if (cpn != NULL) {
482: if (vert < cpn->vert)
483: vert = cpn->vert;
484: cpn = cpn->next;
1.33 schwarze 485: }
1.49 schwarze 486: if (sp->opts->opts & TBL_OPT_ALLBOX && !vert)
487: vert = 1;
1.55 ! schwarze 488: if (col->spacing)
! 489: tbl_char(tp, vert ? cross : line, 1);
! 490: if (col->spacing > 2)
! 491: tbl_char(tp, vert > 1 ? cross : line, 1);
! 492: if (col->spacing > 4)
! 493: tbl_char(tp, line, (col->spacing - 3) / 2);
1.22 schwarze 494: }
1.33 schwarze 495: if (kind) {
496: term_word(tp, "+");
497: term_flushln(tp);
1.22 schwarze 498: }
1.1 kristaps 499: }
500:
501: static void
1.25 schwarze 502: tbl_data(struct termp *tp, const struct tbl_opts *opts,
1.53 schwarze 503: const struct tbl_cell *cp, const struct tbl_dat *dp,
504: const struct roffcol *col)
1.1 kristaps 505: {
1.53 schwarze 506: switch (cp->pos) {
507: case TBL_CELL_HORIZ:
508: tbl_char(tp, '-', col->width);
509: return;
510: case TBL_CELL_DHORIZ:
511: tbl_char(tp, '=', col->width);
512: return;
513: default:
514: break;
515: }
1.1 kristaps 516:
1.35 schwarze 517: if (dp == NULL) {
1.11 kristaps 518: tbl_char(tp, ASCII_NBRSP, col->width);
1.1 kristaps 519: return;
520: }
521:
522: switch (dp->pos) {
1.27 schwarze 523: case TBL_DATA_NONE:
1.11 kristaps 524: tbl_char(tp, ASCII_NBRSP, col->width);
1.10 kristaps 525: return;
1.27 schwarze 526: case TBL_DATA_HORIZ:
527: case TBL_DATA_NHORIZ:
1.11 kristaps 528: tbl_char(tp, '-', col->width);
1.7 kristaps 529: return;
1.27 schwarze 530: case TBL_DATA_NDHORIZ:
531: case TBL_DATA_DHORIZ:
1.11 kristaps 532: tbl_char(tp, '=', col->width);
1.1 kristaps 533: return;
534: default:
535: break;
536: }
1.27 schwarze 537:
1.53 schwarze 538: switch (cp->pos) {
1.27 schwarze 539: case TBL_CELL_LONG:
540: case TBL_CELL_CENTRE:
541: case TBL_CELL_LEFT:
542: case TBL_CELL_RIGHT:
1.11 kristaps 543: tbl_literal(tp, dp, col);
1.1 kristaps 544: break;
1.27 schwarze 545: case TBL_CELL_NUMBER:
1.25 schwarze 546: tbl_number(tp, opts, dp, col);
1.18 kristaps 547: break;
1.27 schwarze 548: case TBL_CELL_DOWN:
1.18 kristaps 549: tbl_char(tp, ASCII_NBRSP, col->width);
1.1 kristaps 550: break;
551: default:
552: abort();
553: }
554: }
555:
556: static void
1.12 kristaps 557: tbl_char(struct termp *tp, char c, size_t len)
1.1 kristaps 558: {
1.12 kristaps 559: size_t i, sz;
560: char cp[2];
561:
562: cp[0] = c;
563: cp[1] = '\0';
1.1 kristaps 564:
1.3 kristaps 565: sz = term_strlen(tp, cp);
566:
567: for (i = 0; i < len; i += sz)
1.1 kristaps 568: term_word(tp, cp);
569: }
570:
571: static void
1.27 schwarze 572: tbl_literal(struct termp *tp, const struct tbl_dat *dp,
1.11 kristaps 573: const struct roffcol *col)
1.1 kristaps 574: {
1.36 schwarze 575: size_t len, padl, padr, width;
576: int ic, spans;
1.1 kristaps 577:
1.17 kristaps 578: assert(dp->string);
1.21 schwarze 579: len = term_strlen(tp, dp->string);
1.23 schwarze 580: width = col->width;
1.36 schwarze 581: ic = dp->layout->col;
582: spans = dp->spans;
583: while (spans--)
584: width += tp->tbl.cols[++ic].width + 3;
1.23 schwarze 585:
586: padr = width > len ? width - len : 0;
1.21 schwarze 587: padl = 0;
1.1 kristaps 588:
1.16 kristaps 589: switch (dp->layout->pos) {
1.27 schwarze 590: case TBL_CELL_LONG:
1.21 schwarze 591: padl = term_len(tp, 1);
592: padr = padr > padl ? padr - padl : 0;
1.1 kristaps 593: break;
1.27 schwarze 594: case TBL_CELL_CENTRE:
1.21 schwarze 595: if (2 > padr)
1.19 schwarze 596: break;
1.21 schwarze 597: padl = padr / 2;
1.19 schwarze 598: padr -= padl;
1.1 kristaps 599: break;
1.27 schwarze 600: case TBL_CELL_RIGHT:
1.21 schwarze 601: padl = padr;
602: padr = 0;
1.1 kristaps 603: break;
604: default:
605: break;
606: }
607:
608: tbl_char(tp, ASCII_NBRSP, padl);
1.29 schwarze 609: tbl_word(tp, dp);
1.21 schwarze 610: tbl_char(tp, ASCII_NBRSP, padr);
1.1 kristaps 611: }
612:
613: static void
1.25 schwarze 614: tbl_number(struct termp *tp, const struct tbl_opts *opts,
1.2 kristaps 615: const struct tbl_dat *dp,
1.11 kristaps 616: const struct roffcol *col)
1.1 kristaps 617: {
1.11 kristaps 618: char *cp;
619: char buf[2];
620: size_t sz, psz, ssz, d, padl;
621: int i;
1.1 kristaps 622:
623: /*
624: * See calc_data_number(). Left-pad by taking the offset of our
625: * and the maximum decimal; right-pad by the remaining amount.
626: */
627:
1.17 kristaps 628: assert(dp->string);
1.2 kristaps 629:
1.17 kristaps 630: sz = term_strlen(tp, dp->string);
1.2 kristaps 631:
1.25 schwarze 632: buf[0] = opts->decimal;
1.11 kristaps 633: buf[1] = '\0';
1.2 kristaps 634:
1.11 kristaps 635: psz = term_strlen(tp, buf);
1.10 kristaps 636:
1.35 schwarze 637: if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
1.17 kristaps 638: for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
639: buf[0] = dp->string[i];
1.5 kristaps 640: ssz += term_strlen(tp, buf);
641: }
642: d = ssz + psz;
643: } else
644: d = sz + psz;
1.2 kristaps 645:
1.32 schwarze 646: if (col->decimal > d && col->width > sz) {
647: padl = col->decimal - d;
648: if (padl + sz > col->width)
649: padl = col->width - sz;
650: tbl_char(tp, ASCII_NBRSP, padl);
651: } else
652: padl = 0;
1.29 schwarze 653: tbl_word(tp, dp);
1.21 schwarze 654: if (col->width > sz + padl)
655: tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
1.2 kristaps 656: }
657:
1.29 schwarze 658: static void
659: tbl_word(struct termp *tp, const struct tbl_dat *dp)
660: {
1.38 schwarze 661: int prev_font;
1.29 schwarze 662:
1.38 schwarze 663: prev_font = tp->fonti;
1.29 schwarze 664: if (dp->layout->flags & TBL_CELL_BOLD)
665: term_fontpush(tp, TERMFONT_BOLD);
666: else if (dp->layout->flags & TBL_CELL_ITALIC)
667: term_fontpush(tp, TERMFONT_UNDER);
668:
669: term_word(tp, dp->string);
670:
671: term_fontpopq(tp, prev_font);
672: }
CVSweb