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