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