Annotation of mandoc/tbl_term.c, Revision 1.7
1.7 ! kristaps 1: /* $Id: tbl_term.c,v 1.6 2011/01/03 16:04:41 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
21: #include <assert.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
25:
26: #include "mandoc.h"
27: #include "out.h"
28: #include "term.h"
29:
30: /* FIXME: `n' modifier doesn't always do the right thing. */
31: /* FIXME: `n' modifier doesn't use the cell-spacing buffer. */
32:
33: static inline void tbl_char(struct termp *, char, int);
34: static void tbl_hframe(struct termp *,
35: const struct tbl_span *);
36: static void tbl_data_number(struct termp *,
37: const struct tbl *,
1.2 kristaps 38: const struct tbl_dat *,
39: const struct termp_tbl *);
1.1 kristaps 40: static void tbl_data_literal(struct termp *,
1.2 kristaps 41: const struct tbl_dat *,
42: const struct termp_tbl *);
1.1 kristaps 43: static void tbl_data(struct termp *, const struct tbl *,
1.2 kristaps 44: const struct tbl_dat *,
45: const struct termp_tbl *);
1.1 kristaps 46: static void tbl_spanner(struct termp *,
47: const struct tbl_head *);
48: static void tbl_hrule(struct termp *,
49: const struct tbl_span *);
1.2 kristaps 50: static void tbl_vframe(struct termp *,
51: const struct tbl *);
1.3 kristaps 52: static void tbl_calc(struct termp *,
53: const struct tbl_span *);
54: static void tbl_calc_data(struct termp *,
55: const struct tbl *,
56: const struct tbl_dat *,
1.2 kristaps 57: struct termp_tbl *);
1.3 kristaps 58: static void tbl_calc_data_literal(struct termp *,
1.2 kristaps 59: const struct tbl_dat *,
60: struct termp_tbl *);
1.3 kristaps 61: static void tbl_calc_data_number(struct termp *,
62: const struct tbl *,
1.2 kristaps 63: const struct tbl_dat *,
64: struct termp_tbl *);
1.1 kristaps 65:
66: void
67: term_tbl(struct termp *tp, const struct tbl_span *sp)
68: {
69: const struct tbl_head *hp;
70: const struct tbl_dat *dp;
71:
1.4 kristaps 72: /* Inhibit printing of spaces: we do padding ourselves. */
73:
74: tp->flags |= TERMP_NONOSPACE;
75: tp->flags |= TERMP_NOSPACE;
76:
77: /*
78: * The first time we're invoked for a given table block, create
79: * the termp_tbl structure. This contains the column
80: * configuration for the entire table, e.g., table-wide column
81: * width, decimal point, etc.
82: */
83:
1.2 kristaps 84: if (TBL_SPAN_FIRST & sp->flags) {
85: assert(NULL == tp->tbl);
86: tp->tbl = calloc
87: (sp->tbl->cols, sizeof(struct termp_tbl));
88: if (NULL == tp->tbl) {
89: perror(NULL);
90: exit(EXIT_FAILURE);
91: }
1.3 kristaps 92: tbl_calc(tp, sp);
1.4 kristaps 93:
94: /* Flush out any preceding data. */
1.1 kristaps 95: term_flushln(tp);
1.2 kristaps 96: }
1.1 kristaps 97:
1.4 kristaps 98: /* Horizontal frame at the start of boxed tables. */
99:
1.1 kristaps 100: if (TBL_SPAN_FIRST & sp->flags)
101: tbl_hframe(tp, sp);
102:
1.4 kristaps 103: /* Vertical frame at the start of each row. */
1.1 kristaps 104:
105: tbl_vframe(tp, sp->tbl);
106:
1.4 kristaps 107: /*
108: * Now print the actual data itself depending on the span type.
109: * Spanner spans get a horizontal rule; data spanners have their
110: * data printed by matching data to header.
111: */
112:
1.1 kristaps 113: switch (sp->pos) {
114: case (TBL_SPAN_HORIZ):
115: /* FALLTHROUGH */
116: case (TBL_SPAN_DHORIZ):
117: tbl_hrule(tp, sp);
118: break;
1.4 kristaps 119: case (TBL_SPAN_DATA):
120: /* Iterate over template headers. */
121: dp = sp->first;
122: for (hp = sp->head; hp; hp = hp->next) {
123: switch (hp->pos) {
124: case (TBL_HEAD_VERT):
125: /* FALLTHROUGH */
126: case (TBL_HEAD_DVERT):
127: tbl_spanner(tp, hp);
128: continue;
129: case (TBL_HEAD_DATA):
130: break;
131: }
132: tbl_data(tp, sp->tbl, dp,
133: &tp->tbl[hp->ident]);
1.1 kristaps 134:
1.4 kristaps 135: /* Go to the next data cell. */
1.1 kristaps 136: if (dp)
137: dp = dp->next;
138: }
1.4 kristaps 139: break;
1.1 kristaps 140: }
141:
142: tbl_vframe(tp, sp->tbl);
143: term_flushln(tp);
144:
1.4 kristaps 145: /*
146: * If we're the last row, clean up after ourselves: clear the
147: * existing table configuration and set it to NULL.
148: */
149:
1.2 kristaps 150: if (TBL_SPAN_LAST & sp->flags) {
1.1 kristaps 151: tbl_hframe(tp, sp);
1.2 kristaps 152: assert(tp->tbl);
153: free(tp->tbl);
154: tp->tbl = NULL;
155: }
1.1 kristaps 156:
157: tp->flags &= ~TERMP_NONOSPACE;
158:
159: }
160:
161: static void
162: tbl_hrule(struct termp *tp, const struct tbl_span *sp)
163: {
164: const struct tbl_head *hp;
165: char c;
1.2 kristaps 166: int width;
1.1 kristaps 167:
168: /*
169: * An hrule extends across the entire table and is demarked by a
170: * standalone `_' or whatnot in lieu of a table row. Spanning
171: * headers are marked by a `+', as are table boundaries.
172: */
173:
174: c = '-';
175: if (TBL_SPAN_DHORIZ == sp->pos)
176: c = '=';
177:
178: /* FIXME: don't use `+' between data and a spanner! */
179:
180: for (hp = sp->head; hp; hp = hp->next) {
1.2 kristaps 181: width = tp->tbl[hp->ident].width;
1.1 kristaps 182: switch (hp->pos) {
183: case (TBL_HEAD_DATA):
1.2 kristaps 184: tbl_char(tp, c, width);
1.1 kristaps 185: break;
186: case (TBL_HEAD_DVERT):
1.2 kristaps 187: tbl_char(tp, '+', width);
1.1 kristaps 188: /* FALLTHROUGH */
189: case (TBL_HEAD_VERT):
1.2 kristaps 190: tbl_char(tp, '+', width);
1.1 kristaps 191: break;
192: default:
193: abort();
194: /* NOTREACHED */
195: }
196: }
197: }
198:
199: static void
200: tbl_hframe(struct termp *tp, const struct tbl_span *sp)
201: {
202: const struct tbl_head *hp;
1.2 kristaps 203: int width;
1.1 kristaps 204:
205: if ( ! (TBL_OPT_BOX & sp->tbl->opts ||
206: TBL_OPT_DBOX & sp->tbl->opts))
207: return;
208:
209: tp->flags |= TERMP_NONOSPACE;
210: tp->flags |= TERMP_NOSPACE;
211:
212: /*
213: * Print out the horizontal part of a frame or double frame. A
214: * double frame has an unbroken `-' outer line the width of the
215: * table, bordered by `+'. The frame (or inner frame, in the
216: * case of the double frame) is a `-' bordered by `+' and broken
217: * by `+' whenever a span is encountered.
218: */
219:
220: if (TBL_OPT_DBOX & sp->tbl->opts) {
221: term_word(tp, "+");
1.2 kristaps 222: for (hp = sp->head; hp; hp = hp->next) {
223: width = tp->tbl[hp->ident].width;
224: tbl_char(tp, '-', width);
225: }
1.1 kristaps 226: term_word(tp, "+");
227: term_flushln(tp);
228: }
229:
230: term_word(tp, "+");
231: for (hp = sp->head; hp; hp = hp->next) {
1.2 kristaps 232: width = tp->tbl[hp->ident].width;
1.1 kristaps 233: switch (hp->pos) {
234: case (TBL_HEAD_DATA):
1.2 kristaps 235: tbl_char(tp, '-', width);
1.1 kristaps 236: break;
237: default:
1.2 kristaps 238: tbl_char(tp, '+', width);
1.1 kristaps 239: break;
240: }
241: }
242: term_word(tp, "+");
243: term_flushln(tp);
244: }
245:
246: static void
247: tbl_data(struct termp *tp, const struct tbl *tbl,
1.2 kristaps 248: const struct tbl_dat *dp,
249: const struct termp_tbl *tbp)
1.1 kristaps 250: {
251: enum tbl_cellt pos;
252:
253: if (NULL == dp) {
1.2 kristaps 254: tbl_char(tp, ASCII_NBRSP, tbp->width);
1.1 kristaps 255: return;
256: }
257:
258: switch (dp->pos) {
259: case (TBL_DATA_HORIZ):
260: /* FALLTHROUGH */
1.7 ! kristaps 261: case (TBL_DATA_NHORIZ):
! 262: tbl_char(tp, '-', tbp->width);
! 263: return;
! 264: case (TBL_DATA_NDHORIZ):
! 265: /* FALLTHROUGH */
1.1 kristaps 266: case (TBL_DATA_DHORIZ):
1.7 ! kristaps 267: tbl_char(tp, '=', tbp->width);
1.1 kristaps 268: return;
269: default:
270: break;
271: }
272:
273: pos = dp->layout ? dp->layout->pos : TBL_CELL_LEFT;
274:
275: switch (pos) {
276: case (TBL_CELL_HORIZ):
1.7 ! kristaps 277: tbl_char(tp, '-', tbp->width);
! 278: break;
1.1 kristaps 279: case (TBL_CELL_DHORIZ):
1.7 ! kristaps 280: tbl_char(tp, '=', tbp->width);
1.1 kristaps 281: break;
282: case (TBL_CELL_LONG):
283: /* FALLTHROUGH */
284: case (TBL_CELL_CENTRE):
285: /* FALLTHROUGH */
286: case (TBL_CELL_LEFT):
287: /* FALLTHROUGH */
288: case (TBL_CELL_RIGHT):
1.2 kristaps 289: tbl_data_literal(tp, dp, tbp);
1.1 kristaps 290: break;
291: case (TBL_CELL_NUMBER):
1.2 kristaps 292: tbl_data_number(tp, tbl, dp, tbp);
1.1 kristaps 293: break;
294: default:
295: abort();
296: /* NOTREACHED */
297: }
298: }
299: static void
300: tbl_spanner(struct termp *tp, const struct tbl_head *hp)
301: {
302:
303: switch (hp->pos) {
304: case (TBL_HEAD_VERT):
305: term_word(tp, "|");
306: break;
307: case (TBL_HEAD_DVERT):
308: term_word(tp, "||");
309: break;
310: default:
311: break;
312: }
313: }
314:
315: static void
316: tbl_vframe(struct termp *tp, const struct tbl *tbl)
317: {
318: /* Always just a single vertical line. */
319:
320: if (TBL_OPT_BOX & tbl->opts || TBL_OPT_DBOX & tbl->opts)
321: term_word(tp, "|");
322: }
323:
324: static inline void
325: tbl_char(struct termp *tp, char c, int len)
326: {
1.3 kristaps 327: int i, sz;
1.1 kristaps 328: char cp[2];
329:
330: cp[0] = c;
331: cp[1] = '\0';
332:
1.3 kristaps 333: sz = term_strlen(tp, cp);
334:
335: for (i = 0; i < len; i += sz)
1.1 kristaps 336: term_word(tp, cp);
337: }
338:
339: static void
1.2 kristaps 340: tbl_data_literal(struct termp *tp,
341: const struct tbl_dat *dp,
342: const struct termp_tbl *tblp)
1.1 kristaps 343: {
1.3 kristaps 344: int padl, padr, ssz;
1.1 kristaps 345: enum tbl_cellt pos;
346:
347: padl = padr = 0;
348:
349: pos = dp->layout ? dp->layout->pos : TBL_CELL_LEFT;
1.3 kristaps 350: ssz = term_len(tp, 1);
1.1 kristaps 351:
352: switch (pos) {
353: case (TBL_CELL_LONG):
1.3 kristaps 354: padl = ssz;
355: padr = tblp->width - term_strlen(tp, dp->string) - ssz;
1.1 kristaps 356: break;
357: case (TBL_CELL_CENTRE):
1.3 kristaps 358: padl = tblp->width - term_strlen(tp, dp->string);
1.1 kristaps 359: if (padl % 2)
360: padr++;
361: padl /= 2;
362: padr += padl;
363: break;
364: case (TBL_CELL_RIGHT):
1.3 kristaps 365: padl = tblp->width - term_strlen(tp, dp->string);
1.1 kristaps 366: break;
367: default:
1.3 kristaps 368: padr = tblp->width - term_strlen(tp, dp->string);
1.1 kristaps 369: break;
370: }
371:
372: tbl_char(tp, ASCII_NBRSP, padl);
373: term_word(tp, dp->string);
374: tbl_char(tp, ASCII_NBRSP, padr);
375: }
376:
377: static void
378: tbl_data_number(struct termp *tp, const struct tbl *tbl,
1.2 kristaps 379: const struct tbl_dat *dp,
380: const struct termp_tbl *tblp)
1.1 kristaps 381: {
1.5 kristaps 382: char *decp, buf[2];
383: int d, padl, sz, psz, ssz, 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.5 kristaps 390: sz = term_strlen(tp, dp->string);
391: psz = term_strlen(tp, ".");
1.1 kristaps 392:
1.5 kristaps 393: if (NULL != (decp = strchr(dp->string, tbl->decimal))) {
394: buf[1] = '\0';
395: for (ssz = i = 0; decp != &dp->string[i]; i++) {
396: buf[0] = dp->string[i];
397: ssz += term_strlen(tp, buf);
398: }
399: d = ssz + psz;
400: } else
401: d = sz + psz;
1.1 kristaps 402:
1.2 kristaps 403: assert(d <= tblp->decimal);
404: assert(sz - d <= tblp->width - tblp->decimal);
1.1 kristaps 405:
1.5 kristaps 406: padl = tblp->decimal - d + term_len(tp, 1);
1.2 kristaps 407: assert(tblp->width - sz - padl);
1.1 kristaps 408:
409: tbl_char(tp, ASCII_NBRSP, padl);
410: term_word(tp, dp->string);
1.2 kristaps 411: tbl_char(tp, ASCII_NBRSP, tblp->width - sz - padl);
412: }
413:
414: static void
1.3 kristaps 415: tbl_calc(struct termp *tp, const struct tbl_span *sp)
1.2 kristaps 416: {
417: const struct tbl_dat *dp;
418: const struct tbl_head *hp;
419: struct termp_tbl *p;
420:
421: /* Calculate width as the max of column cells' widths. */
422:
423: hp = sp->head;
424:
425: for ( ; sp; sp = sp->next) {
426: switch (sp->pos) {
427: case (TBL_DATA_HORIZ):
428: /* FALLTHROUGH */
429: case (TBL_DATA_DHORIZ):
430: continue;
431: default:
432: break;
433: }
434: for (dp = sp->first; dp; dp = dp->next) {
435: if (NULL == dp->layout)
436: continue;
1.3 kristaps 437: p = &tp->tbl[dp->layout->head->ident];
438: tbl_calc_data(tp, sp->tbl, dp, p);
1.2 kristaps 439: }
440: }
441:
442: /* Calculate width as the simple spanner value. */
443:
444: for ( ; hp; hp = hp->next)
445: switch (hp->pos) {
446: case (TBL_HEAD_VERT):
1.3 kristaps 447: tp->tbl[hp->ident].width = term_len(tp, 1);
1.2 kristaps 448: break;
449: case (TBL_HEAD_DVERT):
1.3 kristaps 450: tp->tbl[hp->ident].width = term_len(tp, 2);
1.2 kristaps 451: break;
452: default:
453: break;
454: }
455: }
456:
457: static void
1.3 kristaps 458: tbl_calc_data(struct termp *tp, const struct tbl *tbl,
459: const struct tbl_dat *dp, struct termp_tbl *tblp)
1.2 kristaps 460: {
461:
462: /* Branch down into data sub-types. */
463:
464: switch (dp->layout->pos) {
465: case (TBL_CELL_HORIZ):
466: /* FALLTHROUGH */
467: case (TBL_CELL_DHORIZ):
468: tblp->width = 1;
469: break;
470: case (TBL_CELL_LONG):
471: /* FALLTHROUGH */
472: case (TBL_CELL_CENTRE):
473: /* FALLTHROUGH */
474: case (TBL_CELL_LEFT):
475: /* FALLTHROUGH */
476: case (TBL_CELL_RIGHT):
1.3 kristaps 477: tbl_calc_data_literal(tp, dp, tblp);
1.2 kristaps 478: break;
479: case (TBL_CELL_NUMBER):
1.3 kristaps 480: tbl_calc_data_number(tp, tbl, dp, tblp);
1.2 kristaps 481: break;
482: default:
483: abort();
484: /* NOTREACHED */
485: }
486: }
487:
488: static void
1.3 kristaps 489: tbl_calc_data_number(struct termp *tp, const struct tbl *tbl,
1.2 kristaps 490: const struct tbl_dat *dp, struct termp_tbl *tblp)
491: {
1.5 kristaps 492: int sz, d, psz, i, ssz;
493: char *cp, buf[2];
1.2 kristaps 494:
495: /*
496: * First calculate number width and decimal place (last + 1 for
497: * no-decimal numbers). If the stored decimal is subsequent
498: * ours, make our size longer by that difference
499: * (right-"shifting"); similarly, if ours is subsequent the
500: * stored, then extend the stored size by the difference.
501: * Finally, re-assign the stored values.
502: */
503:
504: /* TODO: use spacing modifier. */
505:
506: assert(dp->string);
1.5 kristaps 507: sz = term_strlen(tp, dp->string);
508: psz = term_strlen(tp, ".");
1.2 kristaps 509:
1.5 kristaps 510: if (NULL != (cp = strchr(dp->string, tbl->decimal))) {
511: buf[1] = '\0';
512: for (ssz = i = 0; cp != &dp->string[i]; i++) {
513: buf[0] = dp->string[i];
514: ssz += term_strlen(tp, buf);
515: }
516: d = ssz + psz;
517: } else
518: d = sz + psz;
1.2 kristaps 519:
1.5 kristaps 520: sz += term_len(tp, 2);
1.2 kristaps 521:
522: if (tblp->decimal > d) {
523: sz += tblp->decimal - d;
524: d = tblp->decimal;
525: } else
526: tblp->width += d - tblp->decimal;
527:
528: if (sz > tblp->width)
529: tblp->width = sz;
530: if (d > tblp->decimal)
531: tblp->decimal = d;
532: }
533:
534: static void
1.3 kristaps 535: tbl_calc_data_literal(struct termp *tp,
536: const struct tbl_dat *dp,
537: struct termp_tbl *tblp)
1.2 kristaps 538: {
539: int sz, bufsz;
540:
541: /*
542: * Calculate our width and use the spacing, with a minimum
543: * spacing dictated by position (centre, e.g,. gets a space on
544: * either side, while right/left get a single adjacent space).
545: */
546:
547: assert(dp->string);
1.3 kristaps 548: sz = term_strlen(tp, dp->string);
1.2 kristaps 549:
550: switch (dp->layout->pos) {
551: case (TBL_CELL_LONG):
552: /* FALLTHROUGH */
553: case (TBL_CELL_CENTRE):
554: bufsz = 2;
555: break;
556: default:
557: bufsz = 1;
558: break;
559: }
560:
561: if (dp->layout->spacing)
562: bufsz = bufsz > dp->layout->spacing ?
563: bufsz : dp->layout->spacing;
564:
565: sz += bufsz;
566: if (tblp->width < sz)
567: tblp->width = sz;
1.1 kristaps 568: }
CVSweb