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