Annotation of mandoc/tbl_term.c, Revision 1.4
1.4 ! kristaps 1: /* $Id: tbl_term.c,v 1.3 2011/01/03 14:45:59 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.3 kristaps 400: char *decp;
1.1 kristaps 401: int d, padl, sz;
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:
408: sz = (int)strlen(dp->string);
409:
1.3 kristaps 410: if (NULL == (decp = strchr(dp->string, tbl->decimal))) {
1.1 kristaps 411: d = sz + 1;
412: } else {
413: d = (int)(decp - dp->string) + 1;
414: }
415:
1.2 kristaps 416: assert(d <= tblp->decimal);
417: assert(sz - d <= tblp->width - tblp->decimal);
1.1 kristaps 418:
1.2 kristaps 419: padl = tblp->decimal - d + 1;
420: assert(tblp->width - sz - padl);
1.1 kristaps 421:
422: tbl_char(tp, ASCII_NBRSP, padl);
423: term_word(tp, dp->string);
1.2 kristaps 424: tbl_char(tp, ASCII_NBRSP, tblp->width - sz - padl);
425: }
426:
427: static void
1.3 kristaps 428: tbl_calc(struct termp *tp, const struct tbl_span *sp)
1.2 kristaps 429: {
430: const struct tbl_dat *dp;
431: const struct tbl_head *hp;
432: struct termp_tbl *p;
433:
434: /* Calculate width as the max of column cells' widths. */
435:
436: hp = sp->head;
437:
438: for ( ; sp; sp = sp->next) {
439: switch (sp->pos) {
440: case (TBL_DATA_HORIZ):
441: /* FALLTHROUGH */
442: case (TBL_DATA_DHORIZ):
443: continue;
444: default:
445: break;
446: }
447: for (dp = sp->first; dp; dp = dp->next) {
448: if (NULL == dp->layout)
449: continue;
1.3 kristaps 450: p = &tp->tbl[dp->layout->head->ident];
451: tbl_calc_data(tp, sp->tbl, dp, p);
1.2 kristaps 452: }
453: }
454:
455: /* Calculate width as the simple spanner value. */
456:
457: for ( ; hp; hp = hp->next)
458: switch (hp->pos) {
459: case (TBL_HEAD_VERT):
1.3 kristaps 460: tp->tbl[hp->ident].width = term_len(tp, 1);
1.2 kristaps 461: break;
462: case (TBL_HEAD_DVERT):
1.3 kristaps 463: tp->tbl[hp->ident].width = term_len(tp, 2);
1.2 kristaps 464: break;
465: default:
466: break;
467: }
468: }
469:
470: static void
1.3 kristaps 471: tbl_calc_data(struct termp *tp, const struct tbl *tbl,
472: const struct tbl_dat *dp, struct termp_tbl *tblp)
1.2 kristaps 473: {
474:
475: /* Branch down into data sub-types. */
476:
477: switch (dp->layout->pos) {
478: case (TBL_CELL_HORIZ):
479: /* FALLTHROUGH */
480: case (TBL_CELL_DHORIZ):
481: tblp->width = 1;
482: break;
483: case (TBL_CELL_LONG):
484: /* FALLTHROUGH */
485: case (TBL_CELL_CENTRE):
486: /* FALLTHROUGH */
487: case (TBL_CELL_LEFT):
488: /* FALLTHROUGH */
489: case (TBL_CELL_RIGHT):
1.3 kristaps 490: tbl_calc_data_literal(tp, dp, tblp);
1.2 kristaps 491: break;
492: case (TBL_CELL_NUMBER):
1.3 kristaps 493: tbl_calc_data_number(tp, tbl, dp, tblp);
1.2 kristaps 494: break;
495: default:
496: abort();
497: /* NOTREACHED */
498: }
499: }
500:
501: static void
1.3 kristaps 502: tbl_calc_data_number(struct termp *tp, const struct tbl *tbl,
1.2 kristaps 503: const struct tbl_dat *dp, struct termp_tbl *tblp)
504: {
505: int sz, d;
1.3 kristaps 506: char *cp;
1.2 kristaps 507:
508: /*
509: * First calculate number width and decimal place (last + 1 for
510: * no-decimal numbers). If the stored decimal is subsequent
511: * ours, make our size longer by that difference
512: * (right-"shifting"); similarly, if ours is subsequent the
513: * stored, then extend the stored size by the difference.
514: * Finally, re-assign the stored values.
515: */
516:
517: /* TODO: use spacing modifier. */
518:
519: assert(dp->string);
520: sz = (int)strlen(dp->string);
521:
1.3 kristaps 522: if (NULL == (cp = strchr(dp->string, tbl->decimal)))
1.2 kristaps 523: d = sz + 1;
524: else
525: d = (int)(cp - dp->string) + 1;
526:
527: sz += 2;
528:
529: if (tblp->decimal > d) {
530: sz += tblp->decimal - d;
531: d = tblp->decimal;
532: } else
533: tblp->width += d - tblp->decimal;
534:
535: if (sz > tblp->width)
536: tblp->width = sz;
537: if (d > tblp->decimal)
538: tblp->decimal = d;
539: }
540:
541: static void
1.3 kristaps 542: tbl_calc_data_literal(struct termp *tp,
543: const struct tbl_dat *dp,
544: struct termp_tbl *tblp)
1.2 kristaps 545: {
546: int sz, bufsz;
547:
548: /*
549: * Calculate our width and use the spacing, with a minimum
550: * spacing dictated by position (centre, e.g,. gets a space on
551: * either side, while right/left get a single adjacent space).
552: */
553:
554: assert(dp->string);
1.3 kristaps 555: sz = term_strlen(tp, dp->string);
1.2 kristaps 556:
557: switch (dp->layout->pos) {
558: case (TBL_CELL_LONG):
559: /* FALLTHROUGH */
560: case (TBL_CELL_CENTRE):
561: bufsz = 2;
562: break;
563: default:
564: bufsz = 1;
565: break;
566: }
567:
568: if (dp->layout->spacing)
569: bufsz = bufsz > dp->layout->spacing ?
570: bufsz : dp->layout->spacing;
571:
572: sz += bufsz;
573: if (tblp->width < sz)
574: tblp->width = sz;
1.1 kristaps 575: }
CVSweb