]> git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
ac826a5cb363a2c98a104a0d957cc6d8d4f80f37
[mandoc.git] / tbl_term.c
1 /* $Id: tbl_term.c,v 1.77 2022/04/14 16:43:44 schwarze Exp $ */
2 /*
3 * Copyright (c) 2011-2021 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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 <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #if DEBUG_MEMORY
29 #include "mandoc_dbg.h"
30 #endif
31 #include "mandoc.h"
32 #include "tbl.h"
33 #include "out.h"
34 #include "term.h"
35
36 #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \
37 (cp)->pos == TBL_CELL_DHORIZ)
38
39
40 static size_t term_tbl_len(size_t, void *);
41 static size_t term_tbl_strlen(const char *, void *);
42 static size_t term_tbl_sulen(const struct roffsu *, void *);
43 static void tbl_data(struct termp *, const struct tbl_opts *,
44 const struct tbl_cell *,
45 const struct tbl_dat *,
46 const struct roffcol *);
47 static void tbl_direct_border(struct termp *, int, size_t);
48 static void tbl_fill_border(struct termp *, int, size_t);
49 static void tbl_fill_char(struct termp *, char, size_t);
50 static void tbl_fill_string(struct termp *, const char *, size_t);
51 static void tbl_hrule(struct termp *, const struct tbl_span *,
52 const struct tbl_span *, const struct tbl_span *,
53 int);
54 static void tbl_literal(struct termp *, const struct tbl_dat *,
55 const struct roffcol *);
56 static void tbl_number(struct termp *, const struct tbl_opts *,
57 const struct tbl_dat *,
58 const struct roffcol *);
59 static void tbl_word(struct termp *, const struct tbl_dat *);
60
61
62 /*
63 * The following border-character tables are indexed
64 * by ternary (3-based) numbers, as opposed to binary or decimal.
65 * Each ternary digit describes the line width in one direction:
66 * 0 means no line, 1 single or light line, 2 double or heavy line.
67 */
68
69 /* Positional values of the four directions. */
70 #define BRIGHT 1
71 #define BDOWN 3
72 #define BLEFT (3 * 3)
73 #define BUP (3 * 3 * 3)
74 #define BHORIZ (BLEFT + BRIGHT)
75
76 /* Code points to use for each combination of widths. */
77 static const int borders_utf8[81] = {
78 0x0020, 0x2576, 0x257a, /* 000 right */
79 0x2577, 0x250c, 0x250d, /* 001 down */
80 0x257b, 0x250e, 0x250f, /* 002 */
81 0x2574, 0x2500, 0x257c, /* 010 left */
82 0x2510, 0x252c, 0x252e, /* 011 left down */
83 0x2512, 0x2530, 0x2532, /* 012 */
84 0x2578, 0x257e, 0x2501, /* 020 left */
85 0x2511, 0x252d, 0x252f, /* 021 left down */
86 0x2513, 0x2531, 0x2533, /* 022 */
87 0x2575, 0x2514, 0x2515, /* 100 up */
88 0x2502, 0x251c, 0x251d, /* 101 up down */
89 0x257d, 0x251f, 0x2522, /* 102 */
90 0x2518, 0x2534, 0x2536, /* 110 up left */
91 0x2524, 0x253c, 0x253e, /* 111 all */
92 0x2527, 0x2541, 0x2546, /* 112 */
93 0x2519, 0x2535, 0x2537, /* 120 up left */
94 0x2525, 0x253d, 0x253f, /* 121 all */
95 0x252a, 0x2545, 0x2548, /* 122 */
96 0x2579, 0x2516, 0x2517, /* 200 up */
97 0x257f, 0x251e, 0x2521, /* 201 up down */
98 0x2503, 0x2520, 0x2523, /* 202 */
99 0x251a, 0x2538, 0x253a, /* 210 up left */
100 0x2526, 0x2540, 0x2544, /* 211 all */
101 0x2528, 0x2542, 0x254a, /* 212 */
102 0x251b, 0x2539, 0x253b, /* 220 up left */
103 0x2529, 0x2543, 0x2547, /* 221 all */
104 0x252b, 0x2549, 0x254b, /* 222 */
105 };
106
107 /* ASCII approximations for these code points, compatible with groff. */
108 static const int borders_ascii[81] = {
109 ' ', '-', '=', /* 000 right */
110 '|', '+', '+', /* 001 down */
111 '|', '+', '+', /* 002 */
112 '-', '-', '=', /* 010 left */
113 '+', '+', '+', /* 011 left down */
114 '+', '+', '+', /* 012 */
115 '=', '=', '=', /* 020 left */
116 '+', '+', '+', /* 021 left down */
117 '+', '+', '+', /* 022 */
118 '|', '+', '+', /* 100 up */
119 '|', '+', '+', /* 101 up down */
120 '|', '+', '+', /* 102 */
121 '+', '+', '+', /* 110 up left */
122 '+', '+', '+', /* 111 all */
123 '+', '+', '+', /* 112 */
124 '+', '+', '+', /* 120 up left */
125 '+', '+', '+', /* 121 all */
126 '+', '+', '+', /* 122 */
127 '|', '+', '+', /* 200 up */
128 '|', '+', '+', /* 201 up down */
129 '|', '+', '+', /* 202 */
130 '+', '+', '+', /* 210 up left */
131 '+', '+', '+', /* 211 all */
132 '+', '+', '+', /* 212 */
133 '+', '+', '+', /* 220 up left */
134 '+', '+', '+', /* 221 all */
135 '+', '+', '+', /* 222 */
136 };
137
138 /* Either of the above according to the selected output encoding. */
139 static const int *borders_locale;
140
141
142 static size_t
143 term_tbl_sulen(const struct roffsu *su, void *arg)
144 {
145 int i;
146
147 i = term_hen((const struct termp *)arg, su);
148 return i > 0 ? i : 0;
149 }
150
151 static size_t
152 term_tbl_strlen(const char *p, void *arg)
153 {
154 return term_strlen((const struct termp *)arg, p);
155 }
156
157 static size_t
158 term_tbl_len(size_t sz, void *arg)
159 {
160 return term_len((const struct termp *)arg, sz);
161 }
162
163
164 void
165 term_tbl(struct termp *tp, const struct tbl_span *sp)
166 {
167 const struct tbl_cell *cp, *cpn, *cpp, *cps;
168 const struct tbl_dat *dp;
169 static size_t offset;
170 size_t save_offset;
171 size_t coloff, tsz;
172 int hspans, ic, more;
173 int dvert, fc, horiz, lhori, rhori, uvert;
174
175 /* Inhibit printing of spaces: we do padding ourselves. */
176
177 tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
178 save_offset = tp->tcol->offset;
179
180 /*
181 * The first time we're invoked for a given table block,
182 * calculate the table widths and decimal positions.
183 */
184
185 if (tp->tbl.cols == NULL) {
186 borders_locale = tp->enc == TERMENC_UTF8 ?
187 borders_utf8 : borders_ascii;
188
189 tp->tbl.len = term_tbl_len;
190 tp->tbl.slen = term_tbl_strlen;
191 tp->tbl.sulen = term_tbl_sulen;
192 tp->tbl.arg = tp;
193
194 tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
195
196 /* Center the table as a whole. */
197
198 offset = tp->tcol->offset;
199 if (sp->opts->opts & TBL_OPT_CENTRE) {
200 tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
201 ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
202 for (ic = 0; ic + 1 < sp->opts->cols; ic++)
203 tsz += tp->tbl.cols[ic].width +
204 tp->tbl.cols[ic].spacing;
205 if (sp->opts->cols)
206 tsz += tp->tbl.cols[sp->opts->cols - 1].width;
207 if (offset + tsz > tp->tcol->rmargin)
208 tsz -= 1;
209 offset = offset + tp->tcol->rmargin > tsz ?
210 (offset + tp->tcol->rmargin - tsz) / 2 : 0;
211 tp->tcol->offset = offset;
212 }
213
214 /* Horizontal frame at the start of boxed tables. */
215
216 if (tp->enc == TERMENC_ASCII &&
217 sp->opts->opts & TBL_OPT_DBOX)
218 tbl_hrule(tp, NULL, sp, sp, TBL_OPT_DBOX);
219 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
220 tbl_hrule(tp, NULL, sp, sp, TBL_OPT_BOX);
221 }
222
223 /* Set up the columns. */
224
225 tp->flags |= TERMP_MULTICOL;
226 tp->tcol->offset = offset;
227 horiz = 0;
228 switch (sp->pos) {
229 case TBL_SPAN_HORIZ:
230 case TBL_SPAN_DHORIZ:
231 horiz = 1;
232 term_setcol(tp, 1);
233 break;
234 case TBL_SPAN_DATA:
235 term_setcol(tp, sp->opts->cols + 2);
236 coloff = tp->tcol->offset;
237
238 /* Set up a column for a left vertical frame. */
239
240 if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
241 sp->opts->lvert)
242 coloff++;
243 tp->tcol->rmargin = coloff;
244
245 /* Set up the data columns. */
246
247 dp = sp->first;
248 hspans = 0;
249 for (ic = 0; ic < sp->opts->cols; ic++) {
250 if (hspans == 0) {
251 tp->tcol++;
252 tp->tcol->offset = coloff;
253 }
254 coloff += tp->tbl.cols[ic].width;
255 tp->tcol->rmargin = coloff;
256 if (ic + 1 < sp->opts->cols)
257 coloff += tp->tbl.cols[ic].spacing;
258 if (hspans) {
259 hspans--;
260 continue;
261 }
262 if (dp != NULL &&
263 (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
264 hspans = dp->hspans;
265 dp = dp->next;
266 }
267 }
268
269 /* Set up a column for a right vertical frame. */
270
271 tp->tcol++;
272 tp->tcol->offset = coloff + 1;
273 tp->tcol->rmargin = tp->maxrmargin;
274
275 /* Spans may have reduced the number of columns. */
276
277 tp->lasttcol = tp->tcol - tp->tcols;
278
279 /* Fill the buffers for all data columns. */
280
281 tp->tcol = tp->tcols;
282 cp = cpn = sp->layout->first;
283 dp = sp->first;
284 hspans = 0;
285 for (ic = 0; ic < sp->opts->cols; ic++) {
286 if (cpn != NULL) {
287 cp = cpn;
288 cpn = cpn->next;
289 }
290 if (hspans) {
291 hspans--;
292 continue;
293 }
294 tp->tcol++;
295 tp->col = 0;
296 tbl_data(tp, sp->opts, cp, dp, tp->tbl.cols + ic);
297 if (dp != NULL &&
298 (ic || sp->layout->first->pos != TBL_CELL_SPAN)) {
299 hspans = dp->hspans;
300 dp = dp->next;
301 }
302 }
303 break;
304 }
305
306 do {
307 /* Print the vertical frame at the start of each row. */
308
309 tp->tcol = tp->tcols;
310 uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
311 sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
312 if (sp->pos == TBL_SPAN_DATA && uvert < sp->layout->vert)
313 uvert = dvert = sp->layout->vert;
314 if (sp->next != NULL && sp->next->pos == TBL_SPAN_DATA &&
315 dvert < sp->next->layout->vert)
316 dvert = sp->next->layout->vert;
317 if (sp->prev != NULL && uvert < sp->prev->layout->vert &&
318 (horiz || (IS_HORIZ(sp->layout->first) &&
319 !IS_HORIZ(sp->prev->layout->first))))
320 uvert = sp->prev->layout->vert;
321 rhori = sp->pos == TBL_SPAN_DHORIZ ||
322 (sp->first != NULL && sp->first->pos == TBL_DATA_DHORIZ) ||
323 sp->layout->first->pos == TBL_CELL_DHORIZ ? 2 :
324 sp->pos == TBL_SPAN_HORIZ ||
325 (sp->first != NULL && sp->first->pos == TBL_DATA_HORIZ) ||
326 sp->layout->first->pos == TBL_CELL_HORIZ ? 1 : 0;
327 fc = BUP * uvert + BDOWN * dvert + BRIGHT * rhori;
328 if (uvert > 0 || dvert > 0 || (horiz && sp->opts->lvert)) {
329 (*tp->advance)(tp, tp->tcols->offset);
330 tp->viscol = tp->tcol->offset;
331 tbl_direct_border(tp, fc, 1);
332 }
333
334 /* Print the data cells. */
335
336 more = 0;
337 if (horiz)
338 tbl_hrule(tp, sp->prev, sp, sp->next, 0);
339 else {
340 cp = sp->layout->first;
341 cpn = sp->next == NULL ? NULL :
342 sp->next->layout->first;
343 cpp = sp->prev == NULL ? NULL :
344 sp->prev->layout->first;
345 dp = sp->first;
346 hspans = 0;
347 for (ic = 0; ic < sp->opts->cols; ic++) {
348
349 /*
350 * Figure out whether to print a
351 * vertical line after this cell
352 * and advance to next layout cell.
353 */
354
355 uvert = dvert = fc = 0;
356 if (cp != NULL) {
357 cps = cp;
358 while (cps->next != NULL &&
359 cps->next->pos == TBL_CELL_SPAN)
360 cps = cps->next;
361 if (sp->pos == TBL_SPAN_DATA)
362 uvert = dvert = cps->vert;
363 switch (cp->pos) {
364 case TBL_CELL_HORIZ:
365 fc = BHORIZ;
366 break;
367 case TBL_CELL_DHORIZ:
368 fc = BHORIZ * 2;
369 break;
370 default:
371 break;
372 }
373 }
374 if (cpp != NULL) {
375 if (uvert < cpp->vert &&
376 cp != NULL &&
377 ((IS_HORIZ(cp) &&
378 !IS_HORIZ(cpp)) ||
379 (cp->next != NULL &&
380 cpp->next != NULL &&
381 IS_HORIZ(cp->next) &&
382 !IS_HORIZ(cpp->next))))
383 uvert = cpp->vert;
384 cpp = cpp->next;
385 }
386 if (sp->opts->opts & TBL_OPT_ALLBOX) {
387 if (uvert == 0)
388 uvert = 1;
389 if (dvert == 0)
390 dvert = 1;
391 }
392 if (cpn != NULL) {
393 if (dvert == 0 ||
394 (dvert < cpn->vert &&
395 tp->enc == TERMENC_UTF8))
396 dvert = cpn->vert;
397 cpn = cpn->next;
398 }
399
400 lhori = (cp != NULL &&
401 cp->pos == TBL_CELL_DHORIZ) ||
402 (dp != NULL &&
403 dp->pos == TBL_DATA_DHORIZ) ? 2 :
404 (cp != NULL &&
405 cp->pos == TBL_CELL_HORIZ) ||
406 (dp != NULL &&
407 dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
408
409 /*
410 * Skip later cells in a span,
411 * figure out whether to start a span,
412 * and advance to next data cell.
413 */
414
415 if (hspans) {
416 hspans--;
417 cp = cp->next;
418 continue;
419 }
420 if (dp != NULL && (ic ||
421 sp->layout->first->pos != TBL_CELL_SPAN)) {
422 hspans = dp->hspans;
423 dp = dp->next;
424 }
425
426 /*
427 * Print one line of text in the cell
428 * and remember whether there is more.
429 */
430
431 tp->tcol++;
432 if (tp->tcol->col < tp->tcol->lastcol)
433 term_flushln(tp);
434 if (tp->tcol->col < tp->tcol->lastcol)
435 more = 1;
436
437 /*
438 * Vertical frames between data cells,
439 * but not after the last column.
440 */
441
442 if (fc == 0 &&
443 ((uvert == 0 && dvert == 0 &&
444 cp != NULL && (cp->next == NULL ||
445 !IS_HORIZ(cp->next))) ||
446 tp->tcol + 1 ==
447 tp->tcols + tp->lasttcol)) {
448 if (cp != NULL)
449 cp = cp->next;
450 continue;
451 }
452
453 if (tp->viscol < tp->tcol->rmargin) {
454 (*tp->advance)(tp, tp->tcol->rmargin
455 - tp->viscol);
456 tp->viscol = tp->tcol->rmargin;
457 }
458 while (tp->viscol < tp->tcol->rmargin +
459 tp->tbl.cols[ic].spacing / 2)
460 tbl_direct_border(tp,
461 BHORIZ * lhori, 1);
462
463 if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
464 continue;
465
466 if (cp != NULL)
467 cp = cp->next;
468
469 rhori = (cp != NULL &&
470 cp->pos == TBL_CELL_DHORIZ) ||
471 (dp != NULL &&
472 dp->pos == TBL_DATA_DHORIZ) ? 2 :
473 (cp != NULL &&
474 cp->pos == TBL_CELL_HORIZ) ||
475 (dp != NULL &&
476 dp->pos == TBL_DATA_HORIZ) ? 1 : 0;
477
478 if (tp->tbl.cols[ic].spacing)
479 tbl_direct_border(tp,
480 BLEFT * lhori + BRIGHT * rhori +
481 BUP * uvert + BDOWN * dvert, 1);
482
483 if (tp->enc == TERMENC_UTF8)
484 uvert = dvert = 0;
485
486 if (tp->tbl.cols[ic].spacing > 2 &&
487 (uvert > 1 || dvert > 1 || rhori))
488 tbl_direct_border(tp,
489 BHORIZ * rhori +
490 BUP * (uvert > 1) +
491 BDOWN * (dvert > 1), 1);
492 }
493 }
494
495 /* Print the vertical frame at the end of each row. */
496
497 uvert = dvert = sp->opts->opts & TBL_OPT_DBOX ? 2 :
498 sp->opts->opts & TBL_OPT_BOX ? 1 : 0;
499 if (sp->pos == TBL_SPAN_DATA &&
500 uvert < sp->layout->last->vert &&
501 sp->layout->last->col + 1 == sp->opts->cols)
502 uvert = dvert = sp->layout->last->vert;
503 if (sp->next != NULL &&
504 dvert < sp->next->layout->last->vert &&
505 sp->next->layout->last->col + 1 == sp->opts->cols)
506 dvert = sp->next->layout->last->vert;
507 if (sp->prev != NULL &&
508 uvert < sp->prev->layout->last->vert &&
509 sp->prev->layout->last->col + 1 == sp->opts->cols &&
510 (horiz || (IS_HORIZ(sp->layout->last) &&
511 !IS_HORIZ(sp->prev->layout->last))))
512 uvert = sp->prev->layout->last->vert;
513 lhori = sp->pos == TBL_SPAN_DHORIZ ||
514 (sp->last != NULL &&
515 sp->last->pos == TBL_DATA_DHORIZ &&
516 sp->last->layout->col + 1 == sp->opts->cols) ||
517 (sp->layout->last->pos == TBL_CELL_DHORIZ &&
518 sp->layout->last->col + 1 == sp->opts->cols) ? 2 :
519 sp->pos == TBL_SPAN_HORIZ ||
520 (sp->last != NULL &&
521 sp->last->pos == TBL_DATA_HORIZ &&
522 sp->last->layout->col + 1 == sp->opts->cols) ||
523 (sp->layout->last->pos == TBL_CELL_HORIZ &&
524 sp->layout->last->col + 1 == sp->opts->cols) ? 1 : 0;
525 fc = BUP * uvert + BDOWN * dvert + BLEFT * lhori;
526 if (uvert > 0 || dvert > 0 || (horiz && sp->opts->rvert)) {
527 if (horiz == 0 && (IS_HORIZ(sp->layout->last) == 0 ||
528 sp->layout->last->col + 1 < sp->opts->cols)) {
529 tp->tcol++;
530 do {
531 tbl_direct_border(tp,
532 BHORIZ * lhori, 1);
533 } while (tp->viscol < tp->tcol->offset);
534 }
535 tbl_direct_border(tp, fc, 1);
536 }
537 (*tp->endline)(tp);
538 tp->viscol = 0;
539 } while (more);
540
541 /*
542 * Clean up after this row. If it is the last line
543 * of the table, print the box line and clean up
544 * column data; otherwise, print the allbox line.
545 */
546
547 term_setcol(tp, 1);
548 tp->flags &= ~TERMP_MULTICOL;
549 tp->tcol->rmargin = tp->maxrmargin;
550 if (sp->next == NULL) {
551 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
552 tbl_hrule(tp, sp, sp, NULL, TBL_OPT_BOX);
553 tp->skipvsp = 1;
554 }
555 if (tp->enc == TERMENC_ASCII &&
556 sp->opts->opts & TBL_OPT_DBOX) {
557 tbl_hrule(tp, sp, sp, NULL, TBL_OPT_DBOX);
558 tp->skipvsp = 2;
559 }
560 assert(tp->tbl.cols);
561 free(tp->tbl.cols);
562 tp->tbl.cols = NULL;
563 } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
564 (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
565 sp->next->next != NULL))
566 tbl_hrule(tp, sp, sp, sp->next, TBL_OPT_ALLBOX);
567
568 tp->tcol->offset = save_offset;
569 tp->flags &= ~TERMP_NONOSPACE;
570 }
571
572 static void
573 tbl_hrule(struct termp *tp, const struct tbl_span *spp,
574 const struct tbl_span *sp, const struct tbl_span *spn, int flags)
575 {
576 const struct tbl_cell *cpp; /* Layout cell above this line. */
577 const struct tbl_cell *cp; /* Layout cell in this line. */
578 const struct tbl_cell *cpn; /* Layout cell below this line. */
579 const struct tbl_dat *dpn; /* Data cell below this line. */
580 const struct roffcol *col; /* Contains width and spacing. */
581 int opts; /* For the table as a whole. */
582 int bw; /* Box line width. */
583 int hw; /* Horizontal line width. */
584 int lw, rw; /* Left and right line widths. */
585 int uw, dw; /* Vertical line widths. */
586
587 cpp = spp == NULL ? NULL : spp->layout->first;
588 cp = sp == NULL ? NULL : sp->layout->first;
589 cpn = spn == NULL ? NULL : spn->layout->first;
590 dpn = NULL;
591 if (spn != NULL) {
592 if (spn->pos == TBL_SPAN_DATA)
593 dpn = spn->first;
594 else if (spn->next != NULL)
595 dpn = spn->next->first;
596 }
597 opts = sp->opts->opts;
598 bw = opts & TBL_OPT_DBOX ? (tp->enc == TERMENC_UTF8 ? 2 : 1) :
599 opts & (TBL_OPT_BOX | TBL_OPT_ALLBOX) ? 1 : 0;
600 hw = flags == TBL_OPT_DBOX || flags == TBL_OPT_BOX ? bw :
601 sp->pos == TBL_SPAN_DHORIZ ? 2 : 1;
602
603 /* Print the left end of the line. */
604
605 if (tp->viscol == 0) {
606 (*tp->advance)(tp, tp->tcols->offset);
607 tp->viscol = tp->tcols->offset;
608 }
609 if (flags != 0)
610 tbl_direct_border(tp,
611 (spp == NULL ? 0 : BUP * bw) +
612 (spn == NULL ? 0 : BDOWN * bw) +
613 (spp == NULL || cpn == NULL ||
614 cpn->pos != TBL_CELL_DOWN ? BRIGHT * hw : 0), 1);
615
616 col = tp->tbl.cols;
617 for (;;) {
618 if (cp == NULL)
619 col++;
620 else
621 col = tp->tbl.cols + cp->col;
622
623 /* Print the horizontal line inside this column. */
624
625 lw = cpp == NULL || cpn == NULL ||
626 (cpn->pos != TBL_CELL_DOWN &&
627 (dpn == NULL || dpn->string == NULL ||
628 strcmp(dpn->string, "\\^") != 0))
629 ? hw : 0;
630 tbl_direct_border(tp, BHORIZ * lw,
631 col->width + col->spacing / 2);
632
633 /*
634 * Figure out whether a vertical line is crossing
635 * at the end of this column,
636 * and advance to the next column.
637 */
638
639 uw = dw = 0;
640 if (cpp != NULL) {
641 if (flags != TBL_OPT_DBOX) {
642 uw = cpp->vert;
643 if (uw == 0 && opts & TBL_OPT_ALLBOX)
644 uw = 1;
645 }
646 cpp = cpp->next;
647 } else if (spp != NULL && opts & TBL_OPT_ALLBOX)
648 uw = 1;
649 if (cp != NULL)
650 cp = cp->next;
651 if (cpn != NULL) {
652 if (flags != TBL_OPT_DBOX) {
653 dw = cpn->vert;
654 if (dw == 0 && opts & TBL_OPT_ALLBOX)
655 dw = 1;
656 }
657 cpn = cpn->next;
658 while (dpn != NULL && dpn->layout != cpn)
659 dpn = dpn->next;
660 } else if (spn != NULL && opts & TBL_OPT_ALLBOX)
661 dw = 1;
662 if (col + 1 == tp->tbl.cols + sp->opts->cols)
663 break;
664
665 /* Vertical lines do not cross spanned cells. */
666
667 if (cpp != NULL && cpp->pos == TBL_CELL_SPAN)
668 uw = 0;
669 if (cpn != NULL && cpn->pos == TBL_CELL_SPAN)
670 dw = 0;
671
672 /* The horizontal line inside the next column. */
673
674 rw = cpp == NULL || cpn == NULL ||
675 (cpn->pos != TBL_CELL_DOWN &&
676 (dpn == NULL || dpn->string == NULL ||
677 strcmp(dpn->string, "\\^") != 0))
678 ? hw : 0;
679
680 /* The line crossing at the end of this column. */
681
682 if (col->spacing)
683 tbl_direct_border(tp, BLEFT * lw +
684 BRIGHT * rw + BUP * uw + BDOWN * dw, 1);
685
686 /*
687 * In ASCII output, a crossing may print two characters.
688 */
689
690 if (tp->enc != TERMENC_ASCII || (uw < 2 && dw < 2))
691 uw = dw = 0;
692 if (col->spacing > 2)
693 tbl_direct_border(tp,
694 BHORIZ * rw + BUP * uw + BDOWN * dw, 1);
695
696 /* Padding before the start of the next column. */
697
698 if (col->spacing > 4)
699 tbl_direct_border(tp,
700 BHORIZ * rw, (col->spacing - 3) / 2);
701 }
702
703 /* Print the right end of the line. */
704
705 if (flags != 0) {
706 tbl_direct_border(tp,
707 (spp == NULL ? 0 : BUP * bw) +
708 (spn == NULL ? 0 : BDOWN * bw) +
709 (spp == NULL || spn == NULL ||
710 spn->layout->last->pos != TBL_CELL_DOWN ?
711 BLEFT * hw : 0), 1);
712 (*tp->endline)(tp);
713 tp->viscol = 0;
714 }
715 }
716
717 static void
718 tbl_data(struct termp *tp, const struct tbl_opts *opts,
719 const struct tbl_cell *cp, const struct tbl_dat *dp,
720 const struct roffcol *col)
721 {
722 switch (cp->pos) {
723 case TBL_CELL_HORIZ:
724 tbl_fill_border(tp, BHORIZ, col->width);
725 return;
726 case TBL_CELL_DHORIZ:
727 tbl_fill_border(tp, BHORIZ * 2, col->width);
728 return;
729 default:
730 break;
731 }
732
733 if (dp == NULL)
734 return;
735
736 switch (dp->pos) {
737 case TBL_DATA_NONE:
738 return;
739 case TBL_DATA_HORIZ:
740 case TBL_DATA_NHORIZ:
741 tbl_fill_border(tp, BHORIZ, col->width);
742 return;
743 case TBL_DATA_NDHORIZ:
744 case TBL_DATA_DHORIZ:
745 tbl_fill_border(tp, BHORIZ * 2, col->width);
746 return;
747 default:
748 break;
749 }
750
751 switch (cp->pos) {
752 case TBL_CELL_LONG:
753 case TBL_CELL_CENTRE:
754 case TBL_CELL_LEFT:
755 case TBL_CELL_RIGHT:
756 tbl_literal(tp, dp, col);
757 break;
758 case TBL_CELL_NUMBER:
759 tbl_number(tp, opts, dp, col);
760 break;
761 case TBL_CELL_DOWN:
762 case TBL_CELL_SPAN:
763 break;
764 default:
765 abort();
766 }
767 }
768
769 static void
770 tbl_fill_string(struct termp *tp, const char *cp, size_t len)
771 {
772 size_t i, sz;
773
774 sz = term_strlen(tp, cp);
775 for (i = 0; i < len; i += sz)
776 term_word(tp, cp);
777 }
778
779 static void
780 tbl_fill_char(struct termp *tp, char c, size_t len)
781 {
782 char cp[2];
783
784 cp[0] = c;
785 cp[1] = '\0';
786 tbl_fill_string(tp, cp, len);
787 }
788
789 static void
790 tbl_fill_border(struct termp *tp, int c, size_t len)
791 {
792 char buf[12];
793
794 if ((c = borders_locale[c]) > 127) {
795 (void)snprintf(buf, sizeof(buf), "\\[u%04x]", c);
796 tbl_fill_string(tp, buf, len);
797 } else
798 tbl_fill_char(tp, c, len);
799 }
800
801 static void
802 tbl_direct_border(struct termp *tp, int c, size_t len)
803 {
804 size_t i, sz;
805
806 c = borders_locale[c];
807 sz = (*tp->width)(tp, c);
808 for (i = 0; i < len; i += sz) {
809 (*tp->letter)(tp, c);
810 tp->viscol += sz;
811 }
812 }
813
814 static void
815 tbl_literal(struct termp *tp, const struct tbl_dat *dp,
816 const struct roffcol *col)
817 {
818 size_t len, padl, padr, width;
819 int ic, hspans;
820
821 assert(dp->string);
822 len = term_strlen(tp, dp->string);
823 width = col->width;
824 ic = dp->layout->col;
825 hspans = dp->hspans;
826 while (hspans--) {
827 width += tp->tbl.cols[ic].spacing;
828 ic++;
829 width += tp->tbl.cols[ic].width;
830 }
831
832 padr = width > len ? width - len : 0;
833 padl = 0;
834
835 switch (dp->layout->pos) {
836 case TBL_CELL_LONG:
837 padl = term_len(tp, 1);
838 padr = padr > padl ? padr - padl : 0;
839 break;
840 case TBL_CELL_CENTRE:
841 if (2 > padr)
842 break;
843 padl = padr / 2;
844 padr -= padl;
845 break;
846 case TBL_CELL_RIGHT:
847 padl = padr;
848 padr = 0;
849 break;
850 default:
851 break;
852 }
853
854 tbl_fill_char(tp, ASCII_NBRSP, padl);
855 tbl_word(tp, dp);
856 tbl_fill_char(tp, ASCII_NBRSP, padr);
857 }
858
859 static void
860 tbl_number(struct termp *tp, const struct tbl_opts *opts,
861 const struct tbl_dat *dp,
862 const struct roffcol *col)
863 {
864 const char *cp, *lastdigit, *lastpoint;
865 size_t intsz, padl, totsz;
866 char buf[2];
867
868 /*
869 * Almost the same code as in tblcalc_number():
870 * First find the position of the decimal point.
871 */
872
873 assert(dp->string);
874 lastdigit = lastpoint = NULL;
875 for (cp = dp->string; cp[0] != '\0'; cp++) {
876 if (cp[0] == '\\' && cp[1] == '&') {
877 lastdigit = lastpoint = cp;
878 break;
879 } else if (cp[0] == opts->decimal &&
880 (isdigit((unsigned char)cp[1]) ||
881 (cp > dp->string && isdigit((unsigned char)cp[-1]))))
882 lastpoint = cp;
883 else if (isdigit((unsigned char)cp[0]))
884 lastdigit = cp;
885 }
886
887 /* Then measure both widths. */
888
889 padl = 0;
890 totsz = term_strlen(tp, dp->string);
891 if (lastdigit != NULL) {
892 if (lastpoint == NULL)
893 lastpoint = lastdigit + 1;
894 intsz = 0;
895 buf[1] = '\0';
896 for (cp = dp->string; cp < lastpoint; cp++) {
897 buf[0] = cp[0];
898 intsz += term_strlen(tp, buf);
899 }
900
901 /*
902 * Pad left to match the decimal position,
903 * but avoid exceeding the total column width.
904 */
905
906 if (col->decimal > intsz && col->width > totsz) {
907 padl = col->decimal - intsz;
908 if (padl + totsz > col->width)
909 padl = col->width - totsz;
910 }
911
912 /* If it is not a number, simply center the string. */
913
914 } else if (col->width > totsz)
915 padl = (col->width - totsz) / 2;
916
917 tbl_fill_char(tp, ASCII_NBRSP, padl);
918 tbl_word(tp, dp);
919
920 /* Pad right to fill the column. */
921
922 if (col->width > padl + totsz)
923 tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz);
924 }
925
926 static void
927 tbl_word(struct termp *tp, const struct tbl_dat *dp)
928 {
929 int prev_font;
930
931 prev_font = tp->fonti;
932 switch (dp->layout->font) {
933 case ESCAPE_FONTBI:
934 term_fontpush(tp, TERMFONT_BI);
935 break;
936 case ESCAPE_FONTBOLD:
937 case ESCAPE_FONTCB:
938 term_fontpush(tp, TERMFONT_BOLD);
939 break;
940 case ESCAPE_FONTITALIC:
941 case ESCAPE_FONTCI:
942 term_fontpush(tp, TERMFONT_UNDER);
943 break;
944 case ESCAPE_FONTROMAN:
945 case ESCAPE_FONTCR:
946 break;
947 default:
948 abort();
949 }
950
951 term_word(tp, dp->string);
952
953 term_fontpopq(tp, prev_font);
954 }