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