]> git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
Fix operator precedence according to Brian W. Kernighan and Lorinda
[mandoc.git] / tbl_term.c
1 /* $Id: tbl_term.c,v 1.55 2017/06/27 18:25:02 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 += tp->tbl.cols[ic].spacing;
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 + 1 < sp->opts->cols; ic++)
114 tsz += tp->tbl.cols[ic].width +
115 tp->tbl.cols[ic].spacing;
116 if (sp->opts->cols)
117 tsz += tp->tbl.cols[sp->opts->cols - 1].width;
118 if (offset + tsz > tp->tcol->rmargin)
119 tsz -= 1;
120 tp->tcol->offset = offset + tp->tcol->rmargin > tsz ?
121 (offset + tp->tcol->rmargin - tsz) / 2 : 0;
122 }
123
124 /* Horizontal frame at the start of boxed tables. */
125
126 if (sp->opts->opts & TBL_OPT_DBOX)
127 tbl_hrule(tp, sp, 3);
128 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
129 tbl_hrule(tp, sp, 2);
130 }
131
132 /* Set up the columns. */
133
134 tp->flags |= TERMP_MULTICOL;
135 horiz = 0;
136 switch (sp->pos) {
137 case TBL_SPAN_HORIZ:
138 case TBL_SPAN_DHORIZ:
139 horiz = 1;
140 term_setcol(tp, 1);
141 break;
142 case TBL_SPAN_DATA:
143 term_setcol(tp, sp->opts->cols + 2);
144 coloff = tp->tcol->offset;
145
146 /* Set up a column for a left vertical frame. */
147
148 if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
149 sp->opts->lvert)
150 coloff++;
151 tp->tcol->rmargin = coloff;
152
153 /* Set up the data columns. */
154
155 dp = sp->first;
156 spans = 0;
157 for (ic = 0; ic < sp->opts->cols; ic++) {
158 if (spans == 0) {
159 tp->tcol++;
160 tp->tcol->offset = coloff;
161 }
162 coloff += tp->tbl.cols[ic].width;
163 tp->tcol->rmargin = coloff;
164 if (ic + 1 < sp->opts->cols)
165 coloff += tp->tbl.cols[ic].spacing;
166 if (spans) {
167 spans--;
168 continue;
169 }
170 if (dp == NULL)
171 continue;
172 spans = dp->spans;
173 dp = dp->next;
174 }
175
176 /* Set up a column for a right vertical frame. */
177
178 tp->tcol++;
179 tp->tcol->offset = coloff + 1;
180 tp->tcol->rmargin = tp->maxrmargin;
181
182 /* Spans may have reduced the number of columns. */
183
184 tp->lasttcol = tp->tcol - tp->tcols;
185
186 /* Fill the buffers for all data columns. */
187
188 tp->tcol = tp->tcols;
189 cp = cpn = sp->layout->first;
190 dp = sp->first;
191 spans = 0;
192 for (ic = 0; ic < sp->opts->cols; ic++) {
193 if (cpn != NULL) {
194 cp = cpn;
195 cpn = cpn->next;
196 }
197 if (spans) {
198 spans--;
199 continue;
200 }
201 tp->tcol++;
202 tp->col = 0;
203 tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
204 if (dp == NULL)
205 continue;
206 spans = dp->spans;
207 dp = dp->next;
208 }
209 break;
210 }
211
212 do {
213 /* Print the vertical frame at the start of each row. */
214
215 tp->tcol = tp->tcols;
216 fc = '\0';
217 if (sp->layout->vert ||
218 (sp->next != NULL && sp->next->layout->vert &&
219 sp->next->pos == TBL_SPAN_DATA) ||
220 (sp->prev != NULL && sp->prev->layout->vert &&
221 (horiz || (IS_HORIZ(sp->layout->first) &&
222 !IS_HORIZ(sp->prev->layout->first)))) ||
223 sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
224 fc = horiz || IS_HORIZ(sp->layout->first) ? '+' : '|';
225 else if (horiz && sp->opts->lvert)
226 fc = '-';
227 if (fc != '\0') {
228 (*tp->advance)(tp, tp->tcols->offset);
229 (*tp->letter)(tp, fc);
230 tp->viscol = tp->tcol->offset + 1;
231 }
232
233 /* Print the data cells. */
234
235 more = 0;
236 if (horiz) {
237 tbl_hrule(tp, sp, 0);
238 term_flushln(tp);
239 } else {
240 cp = sp->layout->first;
241 cpn = sp->next == NULL ? NULL :
242 sp->next->layout->first;
243 cpp = sp->prev == NULL ? NULL :
244 sp->prev->layout->first;
245 dp = sp->first;
246 spans = 0;
247 for (ic = 0; ic < sp->opts->cols; ic++) {
248
249 /*
250 * Figure out whether to print a
251 * vertical line after this cell
252 * and advance to next layout cell.
253 */
254
255 if (cp != NULL) {
256 vert = cp->vert;
257 switch (cp->pos) {
258 case TBL_CELL_HORIZ:
259 fc = '-';
260 break;
261 case TBL_CELL_DHORIZ:
262 fc = '=';
263 break;
264 default:
265 fc = ' ';
266 break;
267 }
268 } else {
269 vert = 0;
270 fc = ' ';
271 }
272 if (cpp != NULL) {
273 if (vert == 0 &&
274 cp != NULL &&
275 ((IS_HORIZ(cp) &&
276 !IS_HORIZ(cpp)) ||
277 (cp->next != NULL &&
278 cpp->next != NULL &&
279 IS_HORIZ(cp->next) &&
280 !IS_HORIZ(cpp->next))))
281 vert = cpp->vert;
282 cpp = cpp->next;
283 }
284 if (vert == 0 &&
285 sp->opts->opts & TBL_OPT_ALLBOX)
286 vert = 1;
287 if (cpn != NULL) {
288 if (vert == 0)
289 vert = cpn->vert;
290 cpn = cpn->next;
291 }
292 if (cp != NULL)
293 cp = cp->next;
294
295 /*
296 * Skip later cells in a span,
297 * figure out whether to start a span,
298 * and advance to next data cell.
299 */
300
301 if (spans) {
302 spans--;
303 continue;
304 }
305 if (dp != NULL) {
306 spans = dp->spans;
307 dp = dp->next;
308 }
309
310 /*
311 * Print one line of text in the cell
312 * and remember whether there is more.
313 */
314
315 tp->tcol++;
316 if (tp->tcol->col < tp->tcol->lastcol)
317 term_flushln(tp);
318 if (tp->tcol->col < tp->tcol->lastcol)
319 more = 1;
320
321 /*
322 * Vertical frames between data cells,
323 * but not after the last column.
324 */
325
326 if (fc == ' ' && ((vert == 0 &&
327 (cp == NULL || !IS_HORIZ(cp))) ||
328 tp->tcol + 1 == tp->tcols + tp->lasttcol))
329 continue;
330
331 if (tp->viscol < tp->tcol->rmargin) {
332 (*tp->advance)(tp, tp->tcol->rmargin
333 - tp->viscol);
334 tp->viscol = tp->tcol->rmargin;
335 }
336 while (tp->viscol < tp->tcol->rmargin +
337 tp->tbl.cols[ic].spacing / 2) {
338 (*tp->letter)(tp, fc);
339 tp->viscol++;
340 }
341
342 if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
343 continue;
344
345 if (fc == ' ' && cp != NULL) {
346 switch (cp->pos) {
347 case TBL_CELL_HORIZ:
348 fc = '-';
349 break;
350 case TBL_CELL_DHORIZ:
351 fc = '=';
352 break;
353 default:
354 break;
355 }
356 }
357 if (tp->tbl.cols[ic].spacing) {
358 (*tp->letter)(tp, fc == ' ' ? '|' :
359 vert ? '+' : fc);
360 tp->viscol++;
361 }
362
363 if (fc != ' ') {
364 if (cp != NULL &&
365 cp->pos == TBL_CELL_HORIZ)
366 fc = '-';
367 else if (cp != NULL &&
368 cp->pos == TBL_CELL_DHORIZ)
369 fc = '=';
370 else
371 fc = ' ';
372 }
373 if (tp->tbl.cols[ic].spacing > 2 &&
374 (vert > 1 || fc != ' ')) {
375 (*tp->letter)(tp, fc == ' ' ? '|' :
376 vert > 1 ? '+' : fc);
377 tp->viscol++;
378 }
379 }
380 }
381
382 /* Print the vertical frame at the end of each row. */
383
384 fc = '\0';
385 if ((sp->layout->last->vert &&
386 sp->layout->last->col + 1 == sp->opts->cols) ||
387 (sp->next != NULL &&
388 sp->next->layout->last->vert &&
389 sp->next->layout->last->col + 1 == sp->opts->cols) ||
390 (sp->prev != NULL &&
391 sp->prev->layout->last->vert &&
392 sp->prev->layout->last->col + 1 == sp->opts->cols &&
393 (horiz || (IS_HORIZ(sp->layout->last) &&
394 !IS_HORIZ(sp->prev->layout->last)))) ||
395 (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
396 fc = horiz || IS_HORIZ(sp->layout->last) ? '+' : '|';
397 else if (horiz && sp->opts->rvert)
398 fc = '-';
399 if (fc != '\0') {
400 if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
401 sp->layout->last->col + 1 < sp->opts->cols)) {
402 tp->tcol++;
403 (*tp->advance)(tp,
404 tp->tcol->offset > tp->viscol ?
405 tp->tcol->offset - tp->viscol : 1);
406 }
407 (*tp->letter)(tp, fc);
408 }
409 (*tp->endline)(tp);
410 tp->viscol = 0;
411 } while (more);
412
413 /*
414 * Clean up after this row. If it is the last line
415 * of the table, print the box line and clean up
416 * column data; otherwise, print the allbox line.
417 */
418
419 term_setcol(tp, 1);
420 tp->flags &= ~TERMP_MULTICOL;
421 tp->tcol->rmargin = tp->maxrmargin;
422 if (sp->next == NULL) {
423 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
424 tbl_hrule(tp, sp, 2);
425 tp->skipvsp = 1;
426 }
427 if (sp->opts->opts & TBL_OPT_DBOX) {
428 tbl_hrule(tp, sp, 3);
429 tp->skipvsp = 2;
430 }
431 assert(tp->tbl.cols);
432 free(tp->tbl.cols);
433 tp->tbl.cols = NULL;
434 tp->tcol->offset = offset;
435 } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
436 (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
437 sp->next->next != NULL))
438 tbl_hrule(tp, sp, 1);
439
440 tp->flags &= ~TERMP_NONOSPACE;
441 }
442
443 /*
444 * Kinds of horizontal rulers:
445 * 0: inside the table (single or double line with crossings)
446 * 1: inside the table (single or double line with crossings and ends)
447 * 2: inner frame (single line with crossings and ends)
448 * 3: outer frame (single line without crossings with ends)
449 */
450 static void
451 tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
452 {
453 const struct tbl_cell *cp, *cpn, *cpp;
454 const struct roffcol *col;
455 int vert;
456 char line, cross;
457
458 line = (kind < 2 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
459 cross = (kind < 3) ? '+' : '-';
460
461 if (kind)
462 term_word(tp, "+");
463 cp = sp->layout->first;
464 cpp = kind || sp->prev == NULL ? NULL : sp->prev->layout->first;
465 if (cpp == cp)
466 cpp = NULL;
467 cpn = kind > 1 || sp->next == NULL ? NULL : sp->next->layout->first;
468 if (cpn == cp)
469 cpn = NULL;
470 for (;;) {
471 col = tp->tbl.cols + cp->col;
472 tbl_char(tp, line, col->width + col->spacing / 2);
473 vert = cp->vert;
474 if ((cp = cp->next) == NULL)
475 break;
476 if (cpp != NULL) {
477 if (vert < cpp->vert)
478 vert = cpp->vert;
479 cpp = cpp->next;
480 }
481 if (cpn != NULL) {
482 if (vert < cpn->vert)
483 vert = cpn->vert;
484 cpn = cpn->next;
485 }
486 if (sp->opts->opts & TBL_OPT_ALLBOX && !vert)
487 vert = 1;
488 if (col->spacing)
489 tbl_char(tp, vert ? cross : line, 1);
490 if (col->spacing > 2)
491 tbl_char(tp, vert > 1 ? cross : line, 1);
492 if (col->spacing > 4)
493 tbl_char(tp, line, (col->spacing - 3) / 2);
494 }
495 if (kind) {
496 term_word(tp, "+");
497 term_flushln(tp);
498 }
499 }
500
501 static void
502 tbl_data(struct termp *tp, const struct tbl_opts *opts,
503 const struct tbl_cell *cp, const struct tbl_dat *dp,
504 const struct roffcol *col)
505 {
506 switch (cp->pos) {
507 case TBL_CELL_HORIZ:
508 tbl_char(tp, '-', col->width);
509 return;
510 case TBL_CELL_DHORIZ:
511 tbl_char(tp, '=', col->width);
512 return;
513 default:
514 break;
515 }
516
517 if (dp == NULL) {
518 tbl_char(tp, ASCII_NBRSP, col->width);
519 return;
520 }
521
522 switch (dp->pos) {
523 case TBL_DATA_NONE:
524 tbl_char(tp, ASCII_NBRSP, col->width);
525 return;
526 case TBL_DATA_HORIZ:
527 case TBL_DATA_NHORIZ:
528 tbl_char(tp, '-', col->width);
529 return;
530 case TBL_DATA_NDHORIZ:
531 case TBL_DATA_DHORIZ:
532 tbl_char(tp, '=', col->width);
533 return;
534 default:
535 break;
536 }
537
538 switch (cp->pos) {
539 case TBL_CELL_LONG:
540 case TBL_CELL_CENTRE:
541 case TBL_CELL_LEFT:
542 case TBL_CELL_RIGHT:
543 tbl_literal(tp, dp, col);
544 break;
545 case TBL_CELL_NUMBER:
546 tbl_number(tp, opts, dp, col);
547 break;
548 case TBL_CELL_DOWN:
549 tbl_char(tp, ASCII_NBRSP, col->width);
550 break;
551 default:
552 abort();
553 }
554 }
555
556 static void
557 tbl_char(struct termp *tp, char c, size_t len)
558 {
559 size_t i, sz;
560 char cp[2];
561
562 cp[0] = c;
563 cp[1] = '\0';
564
565 sz = term_strlen(tp, cp);
566
567 for (i = 0; i < len; i += sz)
568 term_word(tp, cp);
569 }
570
571 static void
572 tbl_literal(struct termp *tp, const struct tbl_dat *dp,
573 const struct roffcol *col)
574 {
575 size_t len, padl, padr, width;
576 int ic, spans;
577
578 assert(dp->string);
579 len = term_strlen(tp, dp->string);
580 width = col->width;
581 ic = dp->layout->col;
582 spans = dp->spans;
583 while (spans--)
584 width += tp->tbl.cols[++ic].width + 3;
585
586 padr = width > len ? width - len : 0;
587 padl = 0;
588
589 switch (dp->layout->pos) {
590 case TBL_CELL_LONG:
591 padl = term_len(tp, 1);
592 padr = padr > padl ? padr - padl : 0;
593 break;
594 case TBL_CELL_CENTRE:
595 if (2 > padr)
596 break;
597 padl = padr / 2;
598 padr -= padl;
599 break;
600 case TBL_CELL_RIGHT:
601 padl = padr;
602 padr = 0;
603 break;
604 default:
605 break;
606 }
607
608 tbl_char(tp, ASCII_NBRSP, padl);
609 tbl_word(tp, dp);
610 tbl_char(tp, ASCII_NBRSP, padr);
611 }
612
613 static void
614 tbl_number(struct termp *tp, const struct tbl_opts *opts,
615 const struct tbl_dat *dp,
616 const struct roffcol *col)
617 {
618 char *cp;
619 char buf[2];
620 size_t sz, psz, ssz, d, padl;
621 int i;
622
623 /*
624 * See calc_data_number(). Left-pad by taking the offset of our
625 * and the maximum decimal; right-pad by the remaining amount.
626 */
627
628 assert(dp->string);
629
630 sz = term_strlen(tp, dp->string);
631
632 buf[0] = opts->decimal;
633 buf[1] = '\0';
634
635 psz = term_strlen(tp, buf);
636
637 if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
638 for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
639 buf[0] = dp->string[i];
640 ssz += term_strlen(tp, buf);
641 }
642 d = ssz + psz;
643 } else
644 d = sz + psz;
645
646 if (col->decimal > d && col->width > sz) {
647 padl = col->decimal - d;
648 if (padl + sz > col->width)
649 padl = col->width - sz;
650 tbl_char(tp, ASCII_NBRSP, padl);
651 } else
652 padl = 0;
653 tbl_word(tp, dp);
654 if (col->width > sz + padl)
655 tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
656 }
657
658 static void
659 tbl_word(struct termp *tp, const struct tbl_dat *dp)
660 {
661 int prev_font;
662
663 prev_font = tp->fonti;
664 if (dp->layout->flags & TBL_CELL_BOLD)
665 term_fontpush(tp, TERMFONT_BOLD);
666 else if (dp->layout->flags & TBL_CELL_ITALIC)
667 term_fontpush(tp, TERMFONT_UNDER);
668
669 term_word(tp, dp->string);
670
671 term_fontpopq(tp, prev_font);
672 }