]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.78 2022/04/26 14:52:05 schwarze Exp $ */
3 * Copyright (c) 2011-2022 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
20 #include <sys/types.h>
29 #include "mandoc_dbg.h"
36 #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \
37 (cp)->pos == TBL_CELL_DHORIZ)
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
*,
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
*);
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.
69 /* Positional values of the four directions. */
73 #define BUP (3 * 3 * 3)
74 #define BHORIZ (BLEFT + BRIGHT)
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 */
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 */
138 /* Either of the above according to the selected output encoding. */
139 static const int *borders_locale
;
143 term_tbl_sulen(const struct roffsu
*su
, void *arg
)
147 i
= term_hen((const struct termp
*)arg
, su
);
148 return i
> 0 ? i
: 0;
152 term_tbl_strlen(const char *p
, void *arg
)
154 return term_strlen((const struct termp
*)arg
, p
);
158 term_tbl_len(size_t sz
, void *arg
)
160 return term_len((const struct termp
*)arg
, sz
);
165 term_tbl(struct termp
*tp
, const struct tbl_span
*sp
)
167 const struct tbl_cell
*cp
, *cpn
, *cpp
, *cps
;
168 const struct tbl_dat
*dp
;
169 static size_t offset
;
172 int hspans
, ic
, more
;
173 int dvert
, fc
, horiz
, lhori
, rhori
, uvert
;
175 /* Inhibit printing of spaces: we do padding ourselves. */
177 tp
->flags
|= TERMP_NOSPACE
| TERMP_NONOSPACE
;
178 save_offset
= tp
->tcol
->offset
;
181 * The first time we're invoked for a given table block,
182 * calculate the table widths and decimal positions.
185 if (tp
->tbl
.cols
== NULL
) {
186 borders_locale
= tp
->enc
== TERMENC_UTF8
?
187 borders_utf8
: borders_ascii
;
189 tp
->tbl
.len
= term_tbl_len
;
190 tp
->tbl
.slen
= term_tbl_strlen
;
191 tp
->tbl
.sulen
= term_tbl_sulen
;
194 tblcalc(&tp
->tbl
, sp
, tp
->tcol
->offset
, tp
->tcol
->rmargin
);
196 /* Center the table as a whole. */
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
;
206 tsz
+= tp
->tbl
.cols
[sp
->opts
->cols
- 1].width
;
207 if (offset
+ tsz
> tp
->tcol
->rmargin
)
209 offset
= offset
+ tp
->tcol
->rmargin
> tsz
?
210 (offset
+ tp
->tcol
->rmargin
- tsz
) / 2 : 0;
211 tp
->tcol
->offset
= offset
;
214 /* Horizontal frame at the start of boxed tables. */
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
);
223 /* Set up the columns. */
225 tp
->flags
|= TERMP_MULTICOL
;
226 tp
->tcol
->offset
= offset
;
230 case TBL_SPAN_DHORIZ
:
235 term_setcol(tp
, sp
->opts
->cols
+ 2);
236 coloff
= tp
->tcol
->offset
;
238 /* Set up a column for a left vertical frame. */
240 if (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
243 tp
->tcol
->rmargin
= coloff
;
245 /* Set up the data columns. */
249 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
252 tp
->tcol
->offset
= coloff
;
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
;
263 (ic
|| sp
->layout
->first
->pos
!= TBL_CELL_SPAN
)) {
269 /* Set up a column for a right vertical frame. */
272 tp
->tcol
->offset
= coloff
+ 1;
273 tp
->tcol
->rmargin
= tp
->maxrmargin
;
275 /* Spans may have reduced the number of columns. */
277 tp
->lasttcol
= tp
->tcol
- tp
->tcols
;
279 /* Fill the buffers for all data columns. */
281 tp
->tcol
= tp
->tcols
;
282 cp
= cpn
= sp
->layout
->first
;
285 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
296 tp
->flags
&= ~(TERMP_BACKAFTER
| TERMP_BACKBEFORE
);
297 tbl_data(tp
, sp
->opts
, cp
, dp
, tp
->tbl
.cols
+ ic
);
299 (ic
|| sp
->layout
->first
->pos
!= TBL_CELL_SPAN
)) {
308 /* Print the vertical frame at the start of each row. */
310 tp
->tcol
= tp
->tcols
;
311 uvert
= dvert
= sp
->opts
->opts
& TBL_OPT_DBOX
? 2 :
312 sp
->opts
->opts
& TBL_OPT_BOX
? 1 : 0;
313 if (sp
->pos
== TBL_SPAN_DATA
&& uvert
< sp
->layout
->vert
)
314 uvert
= dvert
= sp
->layout
->vert
;
315 if (sp
->next
!= NULL
&& sp
->next
->pos
== TBL_SPAN_DATA
&&
316 dvert
< sp
->next
->layout
->vert
)
317 dvert
= sp
->next
->layout
->vert
;
318 if (sp
->prev
!= NULL
&& uvert
< sp
->prev
->layout
->vert
&&
319 (horiz
|| (IS_HORIZ(sp
->layout
->first
) &&
320 !IS_HORIZ(sp
->prev
->layout
->first
))))
321 uvert
= sp
->prev
->layout
->vert
;
322 rhori
= sp
->pos
== TBL_SPAN_DHORIZ
||
323 (sp
->first
!= NULL
&& sp
->first
->pos
== TBL_DATA_DHORIZ
) ||
324 sp
->layout
->first
->pos
== TBL_CELL_DHORIZ
? 2 :
325 sp
->pos
== TBL_SPAN_HORIZ
||
326 (sp
->first
!= NULL
&& sp
->first
->pos
== TBL_DATA_HORIZ
) ||
327 sp
->layout
->first
->pos
== TBL_CELL_HORIZ
? 1 : 0;
328 fc
= BUP
* uvert
+ BDOWN
* dvert
+ BRIGHT
* rhori
;
329 if (uvert
> 0 || dvert
> 0 || (horiz
&& sp
->opts
->lvert
)) {
330 (*tp
->advance
)(tp
, tp
->tcols
->offset
);
331 tp
->viscol
= tp
->tcol
->offset
;
332 tbl_direct_border(tp
, fc
, 1);
335 /* Print the data cells. */
339 tbl_hrule(tp
, sp
->prev
, sp
, sp
->next
, 0);
341 cp
= sp
->layout
->first
;
342 cpn
= sp
->next
== NULL
? NULL
:
343 sp
->next
->layout
->first
;
344 cpp
= sp
->prev
== NULL
? NULL
:
345 sp
->prev
->layout
->first
;
348 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
351 * Figure out whether to print a
352 * vertical line after this cell
353 * and advance to next layout cell.
356 uvert
= dvert
= fc
= 0;
359 while (cps
->next
!= NULL
&&
360 cps
->next
->pos
== TBL_CELL_SPAN
)
362 if (sp
->pos
== TBL_SPAN_DATA
)
363 uvert
= dvert
= cps
->vert
;
368 case TBL_CELL_DHORIZ
:
376 if (uvert
< cpp
->vert
&&
382 IS_HORIZ(cp
->next
) &&
383 !IS_HORIZ(cpp
->next
))))
387 if (sp
->opts
->opts
& TBL_OPT_ALLBOX
) {
395 (dvert
< cpn
->vert
&&
396 tp
->enc
== TERMENC_UTF8
))
401 lhori
= (cp
!= NULL
&&
402 cp
->pos
== TBL_CELL_DHORIZ
) ||
404 dp
->pos
== TBL_DATA_DHORIZ
) ? 2 :
406 cp
->pos
== TBL_CELL_HORIZ
) ||
408 dp
->pos
== TBL_DATA_HORIZ
) ? 1 : 0;
411 * Skip later cells in a span,
412 * figure out whether to start a span,
413 * and advance to next data cell.
421 if (dp
!= NULL
&& (ic
||
422 sp
->layout
->first
->pos
!= TBL_CELL_SPAN
)) {
428 * Print one line of text in the cell
429 * and remember whether there is more.
433 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
435 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
439 * Vertical frames between data cells,
440 * but not after the last column.
444 ((uvert
== 0 && dvert
== 0 &&
445 cp
!= NULL
&& (cp
->next
== NULL
||
446 !IS_HORIZ(cp
->next
))) ||
448 tp
->tcols
+ tp
->lasttcol
)) {
454 if (tp
->viscol
< tp
->tcol
->rmargin
) {
455 (*tp
->advance
)(tp
, tp
->tcol
->rmargin
457 tp
->viscol
= tp
->tcol
->rmargin
;
459 while (tp
->viscol
< tp
->tcol
->rmargin
+
460 tp
->tbl
.cols
[ic
].spacing
/ 2)
461 tbl_direct_border(tp
,
464 if (tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
)
470 rhori
= (cp
!= NULL
&&
471 cp
->pos
== TBL_CELL_DHORIZ
) ||
473 dp
->pos
== TBL_DATA_DHORIZ
) ? 2 :
475 cp
->pos
== TBL_CELL_HORIZ
) ||
477 dp
->pos
== TBL_DATA_HORIZ
) ? 1 : 0;
479 if (tp
->tbl
.cols
[ic
].spacing
)
480 tbl_direct_border(tp
,
481 BLEFT
* lhori
+ BRIGHT
* rhori
+
482 BUP
* uvert
+ BDOWN
* dvert
, 1);
484 if (tp
->enc
== TERMENC_UTF8
)
487 if (tp
->tbl
.cols
[ic
].spacing
> 2 &&
488 (uvert
> 1 || dvert
> 1 || rhori
))
489 tbl_direct_border(tp
,
492 BDOWN
* (dvert
> 1), 1);
496 /* Print the vertical frame at the end of each row. */
498 uvert
= dvert
= sp
->opts
->opts
& TBL_OPT_DBOX
? 2 :
499 sp
->opts
->opts
& TBL_OPT_BOX
? 1 : 0;
500 if (sp
->pos
== TBL_SPAN_DATA
&&
501 uvert
< sp
->layout
->last
->vert
&&
502 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
)
503 uvert
= dvert
= sp
->layout
->last
->vert
;
504 if (sp
->next
!= NULL
&&
505 dvert
< sp
->next
->layout
->last
->vert
&&
506 sp
->next
->layout
->last
->col
+ 1 == sp
->opts
->cols
)
507 dvert
= sp
->next
->layout
->last
->vert
;
508 if (sp
->prev
!= NULL
&&
509 uvert
< sp
->prev
->layout
->last
->vert
&&
510 sp
->prev
->layout
->last
->col
+ 1 == sp
->opts
->cols
&&
511 (horiz
|| (IS_HORIZ(sp
->layout
->last
) &&
512 !IS_HORIZ(sp
->prev
->layout
->last
))))
513 uvert
= sp
->prev
->layout
->last
->vert
;
514 lhori
= sp
->pos
== TBL_SPAN_DHORIZ
||
516 sp
->last
->pos
== TBL_DATA_DHORIZ
&&
517 sp
->last
->layout
->col
+ 1 == sp
->opts
->cols
) ||
518 (sp
->layout
->last
->pos
== TBL_CELL_DHORIZ
&&
519 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ? 2 :
520 sp
->pos
== TBL_SPAN_HORIZ
||
522 sp
->last
->pos
== TBL_DATA_HORIZ
&&
523 sp
->last
->layout
->col
+ 1 == sp
->opts
->cols
) ||
524 (sp
->layout
->last
->pos
== TBL_CELL_HORIZ
&&
525 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ? 1 : 0;
526 fc
= BUP
* uvert
+ BDOWN
* dvert
+ BLEFT
* lhori
;
527 if (uvert
> 0 || dvert
> 0 || (horiz
&& sp
->opts
->rvert
)) {
528 if (horiz
== 0 && (IS_HORIZ(sp
->layout
->last
) == 0 ||
529 sp
->layout
->last
->col
+ 1 < sp
->opts
->cols
)) {
532 tbl_direct_border(tp
,
534 } while (tp
->viscol
< tp
->tcol
->offset
);
536 tbl_direct_border(tp
, fc
, 1);
543 * Clean up after this row. If it is the last line
544 * of the table, print the box line and clean up
545 * column data; otherwise, print the allbox line.
549 tp
->flags
&= ~TERMP_MULTICOL
;
550 tp
->tcol
->rmargin
= tp
->maxrmargin
;
551 if (sp
->next
== NULL
) {
552 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
553 tbl_hrule(tp
, sp
, sp
, NULL
, TBL_OPT_BOX
);
556 if (tp
->enc
== TERMENC_ASCII
&&
557 sp
->opts
->opts
& TBL_OPT_DBOX
) {
558 tbl_hrule(tp
, sp
, sp
, NULL
, TBL_OPT_DBOX
);
561 assert(tp
->tbl
.cols
);
564 } else if (horiz
== 0 && sp
->opts
->opts
& TBL_OPT_ALLBOX
&&
565 (sp
->next
== NULL
|| sp
->next
->pos
== TBL_SPAN_DATA
||
566 sp
->next
->next
!= NULL
))
567 tbl_hrule(tp
, sp
, sp
, sp
->next
, TBL_OPT_ALLBOX
);
569 tp
->tcol
->offset
= save_offset
;
570 tp
->flags
&= ~TERMP_NONOSPACE
;
574 tbl_hrule(struct termp
*tp
, const struct tbl_span
*spp
,
575 const struct tbl_span
*sp
, const struct tbl_span
*spn
, int flags
)
577 const struct tbl_cell
*cpp
; /* Layout cell above this line. */
578 const struct tbl_cell
*cp
; /* Layout cell in this line. */
579 const struct tbl_cell
*cpn
; /* Layout cell below this line. */
580 const struct tbl_dat
*dpn
; /* Data cell below this line. */
581 const struct roffcol
*col
; /* Contains width and spacing. */
582 int opts
; /* For the table as a whole. */
583 int bw
; /* Box line width. */
584 int hw
; /* Horizontal line width. */
585 int lw
, rw
; /* Left and right line widths. */
586 int uw
, dw
; /* Vertical line widths. */
588 cpp
= spp
== NULL
? NULL
: spp
->layout
->first
;
589 cp
= sp
== NULL
? NULL
: sp
->layout
->first
;
590 cpn
= spn
== NULL
? NULL
: spn
->layout
->first
;
593 if (spn
->pos
== TBL_SPAN_DATA
)
595 else if (spn
->next
!= NULL
)
596 dpn
= spn
->next
->first
;
598 opts
= sp
->opts
->opts
;
599 bw
= opts
& TBL_OPT_DBOX
? (tp
->enc
== TERMENC_UTF8
? 2 : 1) :
600 opts
& (TBL_OPT_BOX
| TBL_OPT_ALLBOX
) ? 1 : 0;
601 hw
= flags
== TBL_OPT_DBOX
|| flags
== TBL_OPT_BOX
? bw
:
602 sp
->pos
== TBL_SPAN_DHORIZ
? 2 : 1;
604 /* Print the left end of the line. */
606 if (tp
->viscol
== 0) {
607 (*tp
->advance
)(tp
, tp
->tcols
->offset
);
608 tp
->viscol
= tp
->tcols
->offset
;
611 tbl_direct_border(tp
,
612 (spp
== NULL
? 0 : BUP
* bw
) +
613 (spn
== NULL
? 0 : BDOWN
* bw
) +
614 (spp
== NULL
|| cpn
== NULL
||
615 cpn
->pos
!= TBL_CELL_DOWN
? BRIGHT
* hw
: 0), 1);
622 col
= tp
->tbl
.cols
+ cp
->col
;
624 /* Print the horizontal line inside this column. */
626 lw
= cpp
== NULL
|| cpn
== NULL
||
627 (cpn
->pos
!= TBL_CELL_DOWN
&&
628 (dpn
== NULL
|| dpn
->string
== NULL
||
629 strcmp(dpn
->string
, "\\^") != 0))
631 tbl_direct_border(tp
, BHORIZ
* lw
,
632 col
->width
+ col
->spacing
/ 2);
635 * Figure out whether a vertical line is crossing
636 * at the end of this column,
637 * and advance to the next column.
642 if (flags
!= TBL_OPT_DBOX
) {
644 if (uw
== 0 && opts
& TBL_OPT_ALLBOX
)
648 } else if (spp
!= NULL
&& opts
& TBL_OPT_ALLBOX
)
653 if (flags
!= TBL_OPT_DBOX
) {
655 if (dw
== 0 && opts
& TBL_OPT_ALLBOX
)
659 while (dpn
!= NULL
&& dpn
->layout
!= cpn
)
661 } else if (spn
!= NULL
&& opts
& TBL_OPT_ALLBOX
)
663 if (col
+ 1 == tp
->tbl
.cols
+ sp
->opts
->cols
)
666 /* Vertical lines do not cross spanned cells. */
668 if (cpp
!= NULL
&& cpp
->pos
== TBL_CELL_SPAN
)
670 if (cpn
!= NULL
&& cpn
->pos
== TBL_CELL_SPAN
)
673 /* The horizontal line inside the next column. */
675 rw
= cpp
== NULL
|| cpn
== NULL
||
676 (cpn
->pos
!= TBL_CELL_DOWN
&&
677 (dpn
== NULL
|| dpn
->string
== NULL
||
678 strcmp(dpn
->string
, "\\^") != 0))
681 /* The line crossing at the end of this column. */
684 tbl_direct_border(tp
, BLEFT
* lw
+
685 BRIGHT
* rw
+ BUP
* uw
+ BDOWN
* dw
, 1);
688 * In ASCII output, a crossing may print two characters.
691 if (tp
->enc
!= TERMENC_ASCII
|| (uw
< 2 && dw
< 2))
693 if (col
->spacing
> 2)
694 tbl_direct_border(tp
,
695 BHORIZ
* rw
+ BUP
* uw
+ BDOWN
* dw
, 1);
697 /* Padding before the start of the next column. */
699 if (col
->spacing
> 4)
700 tbl_direct_border(tp
,
701 BHORIZ
* rw
, (col
->spacing
- 3) / 2);
704 /* Print the right end of the line. */
707 tbl_direct_border(tp
,
708 (spp
== NULL
? 0 : BUP
* bw
) +
709 (spn
== NULL
? 0 : BDOWN
* bw
) +
710 (spp
== NULL
|| spn
== NULL
||
711 spn
->layout
->last
->pos
!= TBL_CELL_DOWN
?
719 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
720 const struct tbl_cell
*cp
, const struct tbl_dat
*dp
,
721 const struct roffcol
*col
)
725 tbl_fill_border(tp
, BHORIZ
, col
->width
);
727 case TBL_CELL_DHORIZ
:
728 tbl_fill_border(tp
, BHORIZ
* 2, col
->width
);
741 case TBL_DATA_NHORIZ
:
742 tbl_fill_border(tp
, BHORIZ
, col
->width
);
744 case TBL_DATA_NDHORIZ
:
745 case TBL_DATA_DHORIZ
:
746 tbl_fill_border(tp
, BHORIZ
* 2, col
->width
);
754 case TBL_CELL_CENTRE
:
757 tbl_literal(tp
, dp
, col
);
759 case TBL_CELL_NUMBER
:
760 tbl_number(tp
, opts
, dp
, col
);
771 tbl_fill_string(struct termp
*tp
, const char *cp
, size_t len
)
775 sz
= term_strlen(tp
, cp
);
776 for (i
= 0; i
< len
; i
+= sz
)
781 tbl_fill_char(struct termp
*tp
, char c
, size_t len
)
787 tbl_fill_string(tp
, cp
, len
);
791 tbl_fill_border(struct termp
*tp
, int c
, size_t len
)
795 if ((c
= borders_locale
[c
]) > 127) {
796 (void)snprintf(buf
, sizeof(buf
), "\\[u%04x]", c
);
797 tbl_fill_string(tp
, buf
, len
);
799 tbl_fill_char(tp
, c
, len
);
803 tbl_direct_border(struct termp
*tp
, int c
, size_t len
)
807 c
= borders_locale
[c
];
808 sz
= (*tp
->width
)(tp
, c
);
809 for (i
= 0; i
< len
; i
+= sz
) {
810 (*tp
->letter
)(tp
, c
);
816 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
817 const struct roffcol
*col
)
819 size_t len
, padl
, padr
, width
;
823 len
= term_strlen(tp
, dp
->string
);
825 ic
= dp
->layout
->col
;
828 width
+= tp
->tbl
.cols
[ic
].spacing
;
830 width
+= tp
->tbl
.cols
[ic
].width
;
833 padr
= width
> len
? width
- len
: 0;
836 switch (dp
->layout
->pos
) {
838 padl
= term_len(tp
, 1);
839 padr
= padr
> padl
? padr
- padl
: 0;
841 case TBL_CELL_CENTRE
:
855 tbl_fill_char(tp
, ASCII_NBRSP
, padl
);
857 tbl_fill_char(tp
, ASCII_NBRSP
, padr
);
861 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
862 const struct tbl_dat
*dp
,
863 const struct roffcol
*col
)
865 const char *cp
, *lastdigit
, *lastpoint
;
866 size_t intsz
, padl
, totsz
;
870 * Almost the same code as in tblcalc_number():
871 * First find the position of the decimal point.
875 lastdigit
= lastpoint
= NULL
;
876 for (cp
= dp
->string
; cp
[0] != '\0'; cp
++) {
877 if (cp
[0] == '\\' && cp
[1] == '&') {
878 lastdigit
= lastpoint
= cp
;
880 } else if (cp
[0] == opts
->decimal
&&
881 (isdigit((unsigned char)cp
[1]) ||
882 (cp
> dp
->string
&& isdigit((unsigned char)cp
[-1]))))
884 else if (isdigit((unsigned char)cp
[0]))
888 /* Then measure both widths. */
891 totsz
= term_strlen(tp
, dp
->string
);
892 if (lastdigit
!= NULL
) {
893 if (lastpoint
== NULL
)
894 lastpoint
= lastdigit
+ 1;
897 for (cp
= dp
->string
; cp
< lastpoint
; cp
++) {
899 intsz
+= term_strlen(tp
, buf
);
903 * Pad left to match the decimal position,
904 * but avoid exceeding the total column width.
907 if (col
->decimal
> intsz
&& col
->width
> totsz
) {
908 padl
= col
->decimal
- intsz
;
909 if (padl
+ totsz
> col
->width
)
910 padl
= col
->width
- totsz
;
913 /* If it is not a number, simply center the string. */
915 } else if (col
->width
> totsz
)
916 padl
= (col
->width
- totsz
) / 2;
918 tbl_fill_char(tp
, ASCII_NBRSP
, padl
);
921 /* Pad right to fill the column. */
923 if (col
->width
> padl
+ totsz
)
924 tbl_fill_char(tp
, ASCII_NBRSP
, col
->width
- padl
- totsz
);
928 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
932 prev_font
= tp
->fonti
;
933 switch (dp
->layout
->font
) {
935 term_fontpush(tp
, TERMFONT_BI
);
937 case ESCAPE_FONTBOLD
:
939 term_fontpush(tp
, TERMFONT_BOLD
);
941 case ESCAPE_FONTITALIC
:
943 term_fontpush(tp
, TERMFONT_UNDER
);
945 case ESCAPE_FONTROMAN
:
952 term_word(tp
, dp
->string
);
954 term_fontpopq(tp
, prev_font
);