Annotation of mandoc/tbl_term.c, Revision 1.8
1.8 ! kristaps 1: /* $Id: tbl_term.c,v 1.7 2011/01/04 12:06:21 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: /*
210: * Print out the horizontal part of a frame or double frame. A
211: * double frame has an unbroken `-' outer line the width of the
212: * table, bordered by `+'. The frame (or inner frame, in the
213: * case of the double frame) is a `-' bordered by `+' and broken
214: * by `+' whenever a span is encountered.
215: */
216:
217: if (TBL_OPT_DBOX & sp->tbl->opts) {
218: term_word(tp, "+");
1.2 kristaps 219: for (hp = sp->head; hp; hp = hp->next) {
220: width = tp->tbl[hp->ident].width;
221: tbl_char(tp, '-', width);
222: }
1.1 kristaps 223: term_word(tp, "+");
224: term_flushln(tp);
225: }
226:
227: term_word(tp, "+");
228: for (hp = sp->head; hp; hp = hp->next) {
1.2 kristaps 229: width = tp->tbl[hp->ident].width;
1.1 kristaps 230: switch (hp->pos) {
231: case (TBL_HEAD_DATA):
1.2 kristaps 232: tbl_char(tp, '-', width);
1.1 kristaps 233: break;
234: default:
1.2 kristaps 235: tbl_char(tp, '+', width);
1.1 kristaps 236: break;
237: }
238: }
239: term_word(tp, "+");
240: term_flushln(tp);
241: }
242:
243: static void
244: tbl_data(struct termp *tp, const struct tbl *tbl,
1.2 kristaps 245: const struct tbl_dat *dp,
246: const struct termp_tbl *tbp)
1.1 kristaps 247: {
248: enum tbl_cellt pos;
249:
250: if (NULL == dp) {
1.2 kristaps 251: tbl_char(tp, ASCII_NBRSP, tbp->width);
1.1 kristaps 252: return;
253: }
254:
255: switch (dp->pos) {
256: case (TBL_DATA_HORIZ):
257: /* FALLTHROUGH */
1.7 kristaps 258: case (TBL_DATA_NHORIZ):
259: tbl_char(tp, '-', tbp->width);
260: return;
261: case (TBL_DATA_NDHORIZ):
262: /* FALLTHROUGH */
1.1 kristaps 263: case (TBL_DATA_DHORIZ):
1.7 kristaps 264: tbl_char(tp, '=', tbp->width);
1.1 kristaps 265: return;
266: default:
267: break;
268: }
269:
270: pos = dp->layout ? dp->layout->pos : TBL_CELL_LEFT;
271:
272: switch (pos) {
273: case (TBL_CELL_HORIZ):
1.7 kristaps 274: tbl_char(tp, '-', tbp->width);
275: break;
1.1 kristaps 276: case (TBL_CELL_DHORIZ):
1.7 kristaps 277: tbl_char(tp, '=', tbp->width);
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: static inline void
322: tbl_char(struct termp *tp, char c, int len)
323: {
1.3 kristaps 324: int i, sz;
1.1 kristaps 325: char cp[2];
326:
327: cp[0] = c;
328: cp[1] = '\0';
329:
1.3 kristaps 330: sz = term_strlen(tp, cp);
331:
332: for (i = 0; i < len; i += sz)
1.1 kristaps 333: term_word(tp, cp);
334: }
335:
336: static void
1.2 kristaps 337: tbl_data_literal(struct termp *tp,
338: const struct tbl_dat *dp,
339: const struct termp_tbl *tblp)
1.1 kristaps 340: {
1.3 kristaps 341: int padl, padr, ssz;
1.1 kristaps 342: enum tbl_cellt pos;
343:
344: padl = padr = 0;
345:
346: pos = dp->layout ? dp->layout->pos : TBL_CELL_LEFT;
1.3 kristaps 347: ssz = term_len(tp, 1);
1.1 kristaps 348:
349: switch (pos) {
350: case (TBL_CELL_LONG):
1.3 kristaps 351: padl = ssz;
352: padr = tblp->width - term_strlen(tp, dp->string) - ssz;
1.1 kristaps 353: break;
354: case (TBL_CELL_CENTRE):
1.3 kristaps 355: padl = tblp->width - term_strlen(tp, dp->string);
1.1 kristaps 356: if (padl % 2)
357: padr++;
358: padl /= 2;
359: padr += padl;
360: break;
361: case (TBL_CELL_RIGHT):
1.3 kristaps 362: padl = tblp->width - term_strlen(tp, dp->string);
1.1 kristaps 363: break;
364: default:
1.3 kristaps 365: padr = tblp->width - term_strlen(tp, dp->string);
1.1 kristaps 366: break;
367: }
368:
369: tbl_char(tp, ASCII_NBRSP, padl);
370: term_word(tp, dp->string);
371: tbl_char(tp, ASCII_NBRSP, padr);
372: }
373:
374: static void
375: tbl_data_number(struct termp *tp, const struct tbl *tbl,
1.2 kristaps 376: const struct tbl_dat *dp,
377: const struct termp_tbl *tblp)
1.1 kristaps 378: {
1.5 kristaps 379: char *decp, buf[2];
380: int d, padl, sz, psz, ssz, i;
1.1 kristaps 381:
382: /*
383: * See calc_data_number(). Left-pad by taking the offset of our
384: * and the maximum decimal; right-pad by the remaining amount.
385: */
386:
1.5 kristaps 387: sz = term_strlen(tp, dp->string);
388: psz = term_strlen(tp, ".");
1.1 kristaps 389:
1.5 kristaps 390: if (NULL != (decp = strchr(dp->string, tbl->decimal))) {
391: buf[1] = '\0';
392: for (ssz = i = 0; decp != &dp->string[i]; i++) {
393: buf[0] = dp->string[i];
394: ssz += term_strlen(tp, buf);
395: }
396: d = ssz + psz;
397: } else
398: d = sz + psz;
1.1 kristaps 399:
1.2 kristaps 400: assert(d <= tblp->decimal);
401: assert(sz - d <= tblp->width - tblp->decimal);
1.1 kristaps 402:
1.5 kristaps 403: padl = tblp->decimal - d + term_len(tp, 1);
1.2 kristaps 404: assert(tblp->width - sz - padl);
1.1 kristaps 405:
406: tbl_char(tp, ASCII_NBRSP, padl);
407: term_word(tp, dp->string);
1.2 kristaps 408: tbl_char(tp, ASCII_NBRSP, tblp->width - sz - padl);
409: }
410:
411: static void
1.3 kristaps 412: tbl_calc(struct termp *tp, const struct tbl_span *sp)
1.2 kristaps 413: {
414: const struct tbl_dat *dp;
415: const struct tbl_head *hp;
416: struct termp_tbl *p;
417:
418: /* Calculate width as the max of column cells' widths. */
419:
420: hp = sp->head;
421:
422: for ( ; sp; sp = sp->next) {
423: switch (sp->pos) {
424: case (TBL_DATA_HORIZ):
425: /* FALLTHROUGH */
426: case (TBL_DATA_DHORIZ):
427: continue;
428: default:
429: break;
430: }
431: for (dp = sp->first; dp; dp = dp->next) {
432: if (NULL == dp->layout)
433: continue;
1.3 kristaps 434: p = &tp->tbl[dp->layout->head->ident];
435: tbl_calc_data(tp, sp->tbl, dp, p);
1.2 kristaps 436: }
437: }
438:
439: /* Calculate width as the simple spanner value. */
440:
441: for ( ; hp; hp = hp->next)
442: switch (hp->pos) {
443: case (TBL_HEAD_VERT):
1.3 kristaps 444: tp->tbl[hp->ident].width = term_len(tp, 1);
1.2 kristaps 445: break;
446: case (TBL_HEAD_DVERT):
1.3 kristaps 447: tp->tbl[hp->ident].width = term_len(tp, 2);
1.2 kristaps 448: break;
449: default:
450: break;
451: }
452: }
453:
454: static void
1.3 kristaps 455: tbl_calc_data(struct termp *tp, const struct tbl *tbl,
456: const struct tbl_dat *dp, struct termp_tbl *tblp)
1.2 kristaps 457: {
458:
459: /* Branch down into data sub-types. */
460:
461: switch (dp->layout->pos) {
462: case (TBL_CELL_HORIZ):
463: /* FALLTHROUGH */
464: case (TBL_CELL_DHORIZ):
1.8 ! kristaps 465: tblp->width = term_len(tp, 1);
1.2 kristaps 466: break;
467: case (TBL_CELL_LONG):
468: /* FALLTHROUGH */
469: case (TBL_CELL_CENTRE):
470: /* FALLTHROUGH */
471: case (TBL_CELL_LEFT):
472: /* FALLTHROUGH */
473: case (TBL_CELL_RIGHT):
1.3 kristaps 474: tbl_calc_data_literal(tp, dp, tblp);
1.2 kristaps 475: break;
476: case (TBL_CELL_NUMBER):
1.3 kristaps 477: tbl_calc_data_number(tp, tbl, dp, tblp);
1.2 kristaps 478: break;
479: default:
480: abort();
481: /* NOTREACHED */
482: }
483: }
484:
485: static void
1.3 kristaps 486: tbl_calc_data_number(struct termp *tp, const struct tbl *tbl,
1.2 kristaps 487: const struct tbl_dat *dp, struct termp_tbl *tblp)
488: {
1.5 kristaps 489: int sz, d, psz, i, ssz;
490: char *cp, buf[2];
1.2 kristaps 491:
492: /*
493: * First calculate number width and decimal place (last + 1 for
494: * no-decimal numbers). If the stored decimal is subsequent
495: * ours, make our size longer by that difference
496: * (right-"shifting"); similarly, if ours is subsequent the
497: * stored, then extend the stored size by the difference.
498: * Finally, re-assign the stored values.
499: */
500:
501: /* TODO: use spacing modifier. */
502:
503: assert(dp->string);
1.5 kristaps 504: sz = term_strlen(tp, dp->string);
505: psz = term_strlen(tp, ".");
1.2 kristaps 506:
1.5 kristaps 507: if (NULL != (cp = strchr(dp->string, tbl->decimal))) {
508: buf[1] = '\0';
509: for (ssz = i = 0; cp != &dp->string[i]; i++) {
510: buf[0] = dp->string[i];
511: ssz += term_strlen(tp, buf);
512: }
513: d = ssz + psz;
514: } else
515: d = sz + psz;
1.2 kristaps 516:
1.5 kristaps 517: sz += term_len(tp, 2);
1.2 kristaps 518:
519: if (tblp->decimal > d) {
520: sz += tblp->decimal - d;
521: d = tblp->decimal;
522: } else
523: tblp->width += d - tblp->decimal;
524:
525: if (sz > tblp->width)
526: tblp->width = sz;
527: if (d > tblp->decimal)
528: tblp->decimal = d;
529: }
530:
531: static void
1.3 kristaps 532: tbl_calc_data_literal(struct termp *tp,
533: const struct tbl_dat *dp,
534: struct termp_tbl *tblp)
1.2 kristaps 535: {
1.8 ! kristaps 536: int sz, bufsz, spsz;
1.2 kristaps 537:
538: /*
539: * Calculate our width and use the spacing, with a minimum
540: * spacing dictated by position (centre, e.g,. gets a space on
541: * either side, while right/left get a single adjacent space).
542: */
543:
544: assert(dp->string);
1.3 kristaps 545: sz = term_strlen(tp, dp->string);
1.2 kristaps 546:
547: switch (dp->layout->pos) {
548: case (TBL_CELL_LONG):
549: /* FALLTHROUGH */
550: case (TBL_CELL_CENTRE):
1.8 ! kristaps 551: bufsz = term_len(tp, 2);
1.2 kristaps 552: break;
553: default:
1.8 ! kristaps 554: bufsz = term_len(tp, 1);
1.2 kristaps 555: break;
556: }
557:
1.8 ! kristaps 558: spsz = 0;
1.2 kristaps 559: if (dp->layout->spacing)
1.8 ! kristaps 560: spsz = term_len(tp, dp->layout->spacing);
! 561:
! 562: if (spsz)
! 563: bufsz = bufsz > spsz ? bufsz : spsz;
1.2 kristaps 564:
565: sz += bufsz;
566: if (tblp->width < sz)
567: tblp->width = sz;
1.1 kristaps 568: }
CVSweb