]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
1 /* $Id: tbl_term.c,v 1.63 2018/11/28 13:43:54 schwarze Exp $ */
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2018 Ingo Schwarze <schwarze@openbsd.org>
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>
32 #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \
33 (cp)->pos == TBL_CELL_DHORIZ)
35 /* Flags for tbl_hrule(). */
36 #define HRULE_DBOX (1 << 0) /* Top and bottom, ASCII mode only. */
37 #define HRULE_DATA (1 << 1) /* In the middle of the table. */
38 #define HRULE_DOWN (1 << 2) /* Allow downward branches. */
39 #define HRULE_UP (1 << 3) /* Allow upward branches. */
40 #define HRULE_ENDS (1 << 4) /* Also generate left and right ends. */
43 static size_t term_tbl_len(size_t, void *);
44 static size_t term_tbl_strlen(const char *, void *);
45 static size_t term_tbl_sulen(const struct roffsu
*, void *);
46 static void tbl_data(struct termp
*, const struct tbl_opts
*,
47 const struct tbl_cell
*,
48 const struct tbl_dat
*,
49 const struct roffcol
*);
50 static void tbl_direct_border(struct termp
*, int, size_t);
51 static void tbl_fill_border(struct termp
*, int, size_t);
52 static void tbl_fill_char(struct termp
*, char, size_t);
53 static void tbl_fill_string(struct termp
*, const char *, size_t);
54 static void tbl_hrule(struct termp
*, const struct tbl_span
*, int);
55 static void tbl_literal(struct termp
*, const struct tbl_dat
*,
56 const struct roffcol
*);
57 static void tbl_number(struct termp
*, const struct tbl_opts
*,
58 const struct tbl_dat
*,
59 const struct roffcol
*);
60 static void tbl_word(struct termp
*, const struct tbl_dat
*);
64 * The following border-character tables are indexed
65 * by ternary (3-based) numbers, as opposed to binary or decimal.
66 * Each ternary digit describes the line width in one direction:
67 * 0 means no line, 1 single or light line, 2 double or heavy line.
70 /* Positional values of the four directions. */
74 #define BUP (3 * 3 * 3)
75 #define BHORIZ (BLEFT + BRIGHT)
77 /* Code points to use for each combination of widths. */
78 static const int borders_utf8
[81] = {
79 0x0020, 0x2576, 0x257a, /* 000 right */
80 0x2577, 0x250c, 0x250d, /* 001 down */
81 0x257b, 0x250e, 0x250f, /* 002 */
82 0x2574, 0x2500, 0x257c, /* 010 left */
83 0x2510, 0x252c, 0x252e, /* 011 left down */
84 0x2512, 0x2530, 0x2532, /* 012 */
85 0x2578, 0x257e, 0x2501, /* 020 left */
86 0x2511, 0x252d, 0x252f, /* 021 left down */
87 0x2513, 0x2531, 0x2533, /* 022 */
88 0x2575, 0x2514, 0x2515, /* 100 up */
89 0x2502, 0x251c, 0x251d, /* 101 up down */
90 0x257d, 0x251f, 0x2522, /* 102 */
91 0x2518, 0x2534, 0x2536, /* 110 up left */
92 0x2524, 0x253c, 0x253e, /* 111 all */
93 0x2527, 0x2541, 0x2546, /* 112 */
94 0x2519, 0x2535, 0x2537, /* 120 up left */
95 0x2525, 0x253d, 0x253f, /* 121 all */
96 0x252a, 0x2545, 0x2548, /* 122 */
97 0x2579, 0x2516, 0x2517, /* 200 up */
98 0x257f, 0x251e, 0x2521, /* 201 up down */
99 0x2503, 0x2520, 0x2523, /* 202 */
100 0x251a, 0x2538, 0x253a, /* 210 up left */
101 0x2526, 0x2540, 0x2544, /* 211 all */
102 0x2528, 0x2542, 0x254a, /* 212 */
103 0x251b, 0x2539, 0x253b, /* 220 up left */
104 0x2529, 0x2543, 0x2547, /* 221 all */
105 0x252b, 0x2549, 0x254b, /* 222 */
108 /* ASCII approximations for these code points, compatible with groff. */
109 static const int borders_ascii
[81] = {
110 ' ', '-', '=', /* 000 right */
111 '|', '+', '+', /* 001 down */
112 '|', '+', '+', /* 002 */
113 '-', '-', '=', /* 010 left */
114 '+', '+', '+', /* 011 left down */
115 '+', '+', '+', /* 012 */
116 '=', '=', '=', /* 020 left */
117 '+', '+', '+', /* 021 left down */
118 '+', '+', '+', /* 022 */
119 '|', '+', '+', /* 100 up */
120 '|', '+', '+', /* 101 up down */
121 '|', '+', '+', /* 102 */
122 '+', '+', '+', /* 110 up left */
123 '+', '+', '+', /* 111 all */
124 '+', '+', '+', /* 112 */
125 '+', '+', '+', /* 120 up left */
126 '+', '+', '+', /* 121 all */
127 '+', '+', '+', /* 122 */
128 '|', '+', '+', /* 200 up */
129 '|', '+', '+', /* 201 up down */
130 '|', '+', '+', /* 202 */
131 '+', '+', '+', /* 210 up left */
132 '+', '+', '+', /* 211 all */
133 '+', '+', '+', /* 212 */
134 '+', '+', '+', /* 220 up left */
135 '+', '+', '+', /* 221 all */
136 '+', '+', '+', /* 222 */
139 /* Either of the above according to the selected output encoding. */
140 static const int *borders_locale
;
144 term_tbl_sulen(const struct roffsu
*su
, void *arg
)
148 i
= term_hen((const struct termp
*)arg
, su
);
149 return i
> 0 ? i
: 0;
153 term_tbl_strlen(const char *p
, void *arg
)
155 return term_strlen((const struct termp
*)arg
, p
);
159 term_tbl_len(size_t sz
, void *arg
)
161 return term_len((const struct termp
*)arg
, sz
);
166 term_tbl(struct termp
*tp
, const struct tbl_span
*sp
)
168 const struct tbl_cell
*cp
, *cpn
, *cpp
, *cps
;
169 const struct tbl_dat
*dp
;
170 static size_t offset
;
172 int hspans
, ic
, more
;
173 int dvert
, fc
, horiz
, line
, uvert
;
175 /* Inhibit printing of spaces: we do padding ourselves. */
177 tp
->flags
|= TERMP_NOSPACE
| TERMP_NONOSPACE
;
180 * The first time we're invoked for a given table block,
181 * calculate the table widths and decimal positions.
184 if (tp
->tbl
.cols
== NULL
) {
185 borders_locale
= tp
->enc
== TERMENC_UTF8
?
186 borders_utf8
: borders_ascii
;
188 tp
->tbl
.len
= term_tbl_len
;
189 tp
->tbl
.slen
= term_tbl_strlen
;
190 tp
->tbl
.sulen
= term_tbl_sulen
;
193 tblcalc(&tp
->tbl
, sp
, tp
->tcol
->offset
, tp
->tcol
->rmargin
);
195 /* Tables leak .ta settings to subsequent text. */
197 term_tab_set(tp
, NULL
);
198 coloff
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
200 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
201 coloff
+= tp
->tbl
.cols
[ic
].width
;
202 term_tab_iset(coloff
);
203 coloff
+= tp
->tbl
.cols
[ic
].spacing
;
206 /* Center the table as a whole. */
208 offset
= tp
->tcol
->offset
;
209 if (sp
->opts
->opts
& TBL_OPT_CENTRE
) {
210 tsz
= sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
)
211 ? 2 : !!sp
->opts
->lvert
+ !!sp
->opts
->rvert
;
212 for (ic
= 0; ic
+ 1 < sp
->opts
->cols
; ic
++)
213 tsz
+= tp
->tbl
.cols
[ic
].width
+
214 tp
->tbl
.cols
[ic
].spacing
;
216 tsz
+= tp
->tbl
.cols
[sp
->opts
->cols
- 1].width
;
217 if (offset
+ tsz
> tp
->tcol
->rmargin
)
219 tp
->tcol
->offset
= offset
+ tp
->tcol
->rmargin
> tsz
?
220 (offset
+ tp
->tcol
->rmargin
- tsz
) / 2 : 0;
223 /* Horizontal frame at the start of boxed tables. */
225 if (tp
->enc
== TERMENC_ASCII
&&
226 sp
->opts
->opts
& TBL_OPT_DBOX
)
227 tbl_hrule(tp
, sp
, HRULE_DBOX
| HRULE_ENDS
);
228 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
))
229 tbl_hrule(tp
, sp
, HRULE_DOWN
| HRULE_ENDS
);
232 /* Set up the columns. */
234 tp
->flags
|= TERMP_MULTICOL
;
238 case TBL_SPAN_DHORIZ
:
243 term_setcol(tp
, sp
->opts
->cols
+ 2);
244 coloff
= tp
->tcol
->offset
;
246 /* Set up a column for a left vertical frame. */
248 if (sp
->opts
->opts
& (TBL_OPT_BOX
| TBL_OPT_DBOX
) ||
251 tp
->tcol
->rmargin
= coloff
;
253 /* Set up the data columns. */
257 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
260 tp
->tcol
->offset
= coloff
;
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
;
273 if (ic
|| sp
->layout
->first
->pos
!= TBL_CELL_SPAN
)
277 /* Set up a column for a right vertical frame. */
280 tp
->tcol
->offset
= coloff
+ 1;
281 tp
->tcol
->rmargin
= tp
->maxrmargin
;
283 /* Spans may have reduced the number of columns. */
285 tp
->lasttcol
= tp
->tcol
- tp
->tcols
;
287 /* Fill the buffers for all data columns. */
289 tp
->tcol
= tp
->tcols
;
290 cp
= cpn
= sp
->layout
->first
;
293 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
304 tbl_data(tp
, sp
->opts
, cp
, dp
, tp
->tbl
.cols
+ ic
);
308 if (cp
->pos
!= TBL_CELL_SPAN
)
315 /* Print the vertical frame at the start of each row. */
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 line
= sp
->pos
== TBL_SPAN_DHORIZ
||
330 sp
->layout
->first
->pos
== TBL_CELL_DHORIZ
? 2 :
331 sp
->pos
== TBL_SPAN_HORIZ
||
332 sp
->layout
->first
->pos
== TBL_CELL_HORIZ
? 1 : 0;
333 fc
= BUP
* uvert
+ BDOWN
* dvert
+ BRIGHT
* line
;
334 if (uvert
> 0 || dvert
> 0 || (horiz
&& sp
->opts
->lvert
)) {
335 (*tp
->advance
)(tp
, tp
->tcols
->offset
);
336 tp
->viscol
= tp
->tcol
->offset
;
337 tbl_direct_border(tp
, fc
, 1);
340 /* Print the data cells. */
344 tbl_hrule(tp
, sp
, HRULE_DATA
| HRULE_DOWN
| HRULE_UP
);
346 cp
= sp
->layout
->first
;
347 cpn
= sp
->next
== NULL
? NULL
:
348 sp
->next
->layout
->first
;
349 cpp
= sp
->prev
== NULL
? NULL
:
350 sp
->prev
->layout
->first
;
353 for (ic
= 0; ic
< sp
->opts
->cols
; ic
++) {
356 * Figure out whether to print a
357 * vertical line after this cell
358 * and advance to next layout cell.
361 uvert
= dvert
= fc
= 0;
364 while (cps
->next
!= NULL
&&
365 cps
->next
->pos
== TBL_CELL_SPAN
)
367 if (sp
->pos
== TBL_SPAN_DATA
)
368 uvert
= dvert
= cps
->vert
;
373 case TBL_CELL_DHORIZ
:
381 if (uvert
< cpp
->vert
&&
387 IS_HORIZ(cp
->next
) &&
388 !IS_HORIZ(cpp
->next
))))
392 if (sp
->opts
->opts
& TBL_OPT_ALLBOX
) {
400 (dvert
< cpn
->vert
&&
401 tp
->enc
== TERMENC_UTF8
))
407 * Skip later cells in a span,
408 * figure out whether to start a span,
409 * and advance to next data cell.
419 if (ic
|| sp
->layout
->first
->pos
425 * Print one line of text in the cell
426 * and remember whether there is more.
430 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
432 if (tp
->tcol
->col
< tp
->tcol
->lastcol
)
436 * Vertical frames between data cells,
437 * but not after the last column.
441 ((uvert
== 0 && dvert
== 0 &&
442 cp
!= NULL
&& (cp
->next
== NULL
||
443 !IS_HORIZ(cp
->next
))) ||
445 tp
->tcols
+ tp
->lasttcol
)) {
451 if (tp
->viscol
< tp
->tcol
->rmargin
) {
452 (*tp
->advance
)(tp
, tp
->tcol
->rmargin
454 tp
->viscol
= tp
->tcol
->rmargin
;
456 while (tp
->viscol
< tp
->tcol
->rmargin
+
457 tp
->tbl
.cols
[ic
].spacing
/ 2)
458 tbl_direct_border(tp
, fc
, 1);
460 if (tp
->tcol
+ 1 == tp
->tcols
+ tp
->lasttcol
)
468 case TBL_CELL_DHORIZ
:
482 case TBL_CELL_DHORIZ
:
489 if (tp
->tbl
.cols
[ic
].spacing
)
490 tbl_direct_border(tp
, fc
+
491 BUP
* uvert
+ BDOWN
* dvert
, 1);
493 if (tp
->enc
== TERMENC_UTF8
)
498 cp
->pos
== TBL_CELL_HORIZ
)
500 else if (cp
!= NULL
&&
501 cp
->pos
== TBL_CELL_DHORIZ
)
506 if (tp
->tbl
.cols
[ic
].spacing
> 2 &&
507 (uvert
> 1 || dvert
> 1 || fc
!= 0))
508 tbl_direct_border(tp
, fc
+
510 BDOWN
* (dvert
> 1), 1);
514 /* Print the vertical frame at the end of each row. */
516 uvert
= dvert
= sp
->opts
->opts
& TBL_OPT_DBOX
? 2 :
517 sp
->opts
->opts
& TBL_OPT_BOX
? 1 : 0;
518 if (sp
->pos
== TBL_SPAN_DATA
&&
519 uvert
< sp
->layout
->last
->vert
&&
520 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
)
521 uvert
= dvert
= sp
->layout
->last
->vert
;
522 if (sp
->next
!= NULL
&&
523 dvert
< sp
->next
->layout
->last
->vert
&&
524 sp
->next
->layout
->last
->col
+ 1 == sp
->opts
->cols
)
525 dvert
= sp
->next
->layout
->last
->vert
;
526 if (sp
->prev
!= NULL
&&
527 uvert
< sp
->prev
->layout
->last
->vert
&&
528 sp
->prev
->layout
->last
->col
+ 1 == sp
->opts
->cols
&&
529 (horiz
|| (IS_HORIZ(sp
->layout
->last
) &&
530 !IS_HORIZ(sp
->prev
->layout
->last
))))
531 uvert
= sp
->prev
->layout
->last
->vert
;
532 line
= sp
->pos
== TBL_SPAN_DHORIZ
||
533 (sp
->layout
->last
->pos
== TBL_CELL_DHORIZ
&&
534 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ? 2 :
535 sp
->pos
== TBL_SPAN_HORIZ
||
536 (sp
->layout
->last
->pos
== TBL_CELL_HORIZ
&&
537 sp
->layout
->last
->col
+ 1 == sp
->opts
->cols
) ? 1 : 0;
538 fc
= BUP
* uvert
+ BDOWN
* dvert
+ BLEFT
* line
;
539 if (uvert
> 0 || dvert
> 0 || (horiz
&& sp
->opts
->rvert
)) {
540 if (horiz
== 0 && (IS_HORIZ(sp
->layout
->last
) == 0 ||
541 sp
->layout
->last
->col
+ 1 < sp
->opts
->cols
)) {
544 tp
->tcol
->offset
> tp
->viscol
?
545 tp
->tcol
->offset
- tp
->viscol
: 1);
547 tbl_direct_border(tp
, fc
, 1);
554 * Clean up after this row. If it is the last line
555 * of the table, print the box line and clean up
556 * column data; otherwise, print the allbox line.
560 tp
->flags
&= ~TERMP_MULTICOL
;
561 tp
->tcol
->rmargin
= tp
->maxrmargin
;
562 if (sp
->next
== NULL
) {
563 if (sp
->opts
->opts
& (TBL_OPT_DBOX
| TBL_OPT_BOX
)) {
564 tbl_hrule(tp
, sp
, HRULE_UP
| HRULE_ENDS
);
567 if (tp
->enc
== TERMENC_ASCII
&&
568 sp
->opts
->opts
& TBL_OPT_DBOX
) {
569 tbl_hrule(tp
, sp
, HRULE_DBOX
| HRULE_ENDS
);
572 assert(tp
->tbl
.cols
);
575 tp
->tcol
->offset
= offset
;
576 } else if (horiz
== 0 && sp
->opts
->opts
& TBL_OPT_ALLBOX
&&
577 (sp
->next
== NULL
|| sp
->next
->pos
== TBL_SPAN_DATA
||
578 sp
->next
->next
!= NULL
))
580 HRULE_DATA
| HRULE_DOWN
| HRULE_UP
| HRULE_ENDS
);
582 tp
->flags
&= ~TERMP_NONOSPACE
;
586 tbl_hrule(struct termp
*tp
, const struct tbl_span
*sp
, int flags
)
588 const struct tbl_cell
*cp
, *cpn
, *cpp
;
589 const struct roffcol
*col
;
590 int cross
, dvert
, line
, linewidth
, uvert
;
592 cp
= sp
->layout
->first
;
594 if (flags
& HRULE_DATA
) {
595 linewidth
= sp
->pos
== TBL_SPAN_DHORIZ
? 2 : 1;
596 cpn
= sp
->next
== NULL
? NULL
: sp
->next
->layout
->first
;
600 linewidth
= tp
->enc
== TERMENC_UTF8
&&
601 sp
->opts
->opts
& TBL_OPT_DBOX
? 2 : 1;
602 if (tp
->viscol
== 0) {
603 (*tp
->advance
)(tp
, tp
->tcols
->offset
);
604 tp
->viscol
= tp
->tcols
->offset
;
606 if (flags
& HRULE_ENDS
)
607 tbl_direct_border(tp
, linewidth
* (BRIGHT
+
608 (flags
& (HRULE_UP
| HRULE_DBOX
) ? BUP
: 0) +
609 (flags
& (HRULE_DOWN
| HRULE_DBOX
) ? BDOWN
: 0)), 1);
611 cpp
= sp
->prev
== NULL
? NULL
: sp
->prev
->layout
->first
;
616 col
= tp
->tbl
.cols
+ cp
->col
;
617 line
= cpn
== NULL
|| cpn
->pos
!= TBL_CELL_DOWN
?
618 BHORIZ
* linewidth
: 0;
619 tbl_direct_border(tp
, line
, col
->width
+ col
->spacing
/ 2);
621 if (flags
& HRULE_UP
&&
622 (tp
->enc
== TERMENC_ASCII
|| sp
->pos
== TBL_SPAN_DATA
||
623 (sp
->prev
!= NULL
&& sp
->prev
->layout
== sp
->layout
)))
625 if (flags
& HRULE_DOWN
)
627 if ((cp
= cp
->next
) == NULL
)
630 if (uvert
< cpp
->vert
)
635 if (dvert
< cpn
->vert
)
639 if (sp
->opts
->opts
& TBL_OPT_ALLBOX
) {
640 if (flags
& HRULE_UP
&& uvert
== 0)
642 if (flags
& HRULE_DOWN
&& dvert
== 0)
645 cross
= BHORIZ
* linewidth
+ BUP
* uvert
+ BDOWN
* dvert
;
647 tbl_direct_border(tp
, cross
, 1);
648 if (col
->spacing
> 2)
649 tbl_direct_border(tp
, tp
->enc
== TERMENC_ASCII
&&
650 (uvert
> 1 || dvert
> 1) ? cross
: line
, 1);
651 if (col
->spacing
> 4)
652 tbl_direct_border(tp
, line
, (col
->spacing
- 3) / 2);
654 if (flags
& HRULE_ENDS
) {
655 tbl_direct_border(tp
, linewidth
* (BLEFT
+
656 (flags
& (HRULE_UP
| HRULE_DBOX
) ? BUP
: 0) +
657 (flags
& (HRULE_DOWN
| HRULE_DBOX
) ? BDOWN
: 0)), 1);
664 tbl_data(struct termp
*tp
, const struct tbl_opts
*opts
,
665 const struct tbl_cell
*cp
, const struct tbl_dat
*dp
,
666 const struct roffcol
*col
)
670 tbl_fill_border(tp
, BHORIZ
, col
->width
);
672 case TBL_CELL_DHORIZ
:
673 tbl_fill_border(tp
, BHORIZ
* 2, col
->width
);
686 case TBL_DATA_NHORIZ
:
687 tbl_fill_border(tp
, BHORIZ
, col
->width
);
689 case TBL_DATA_NDHORIZ
:
690 case TBL_DATA_DHORIZ
:
691 tbl_fill_border(tp
, BHORIZ
* 2, col
->width
);
699 case TBL_CELL_CENTRE
:
702 tbl_literal(tp
, dp
, col
);
704 case TBL_CELL_NUMBER
:
705 tbl_number(tp
, opts
, dp
, col
);
716 tbl_fill_string(struct termp
*tp
, const char *cp
, size_t len
)
720 sz
= term_strlen(tp
, cp
);
721 for (i
= 0; i
< len
; i
+= sz
)
726 tbl_fill_char(struct termp
*tp
, char c
, size_t len
)
732 tbl_fill_string(tp
, cp
, len
);
736 tbl_fill_border(struct termp
*tp
, int c
, size_t len
)
740 if ((c
= borders_locale
[c
]) > 127) {
741 (void)snprintf(buf
, sizeof(buf
), "\\[u%04x]", c
);
742 tbl_fill_string(tp
, buf
, len
);
744 tbl_fill_char(tp
, c
, len
);
748 tbl_direct_border(struct termp
*tp
, int c
, size_t len
)
752 c
= borders_locale
[c
];
753 sz
= (*tp
->width
)(tp
, c
);
754 for (i
= 0; i
< len
; i
+= sz
) {
755 (*tp
->letter
)(tp
, c
);
761 tbl_literal(struct termp
*tp
, const struct tbl_dat
*dp
,
762 const struct roffcol
*col
)
764 size_t len
, padl
, padr
, width
;
768 len
= term_strlen(tp
, dp
->string
);
770 ic
= dp
->layout
->col
;
773 width
+= tp
->tbl
.cols
[++ic
].width
+ 3;
775 padr
= width
> len
? width
- len
: 0;
778 switch (dp
->layout
->pos
) {
780 padl
= term_len(tp
, 1);
781 padr
= padr
> padl
? padr
- padl
: 0;
783 case TBL_CELL_CENTRE
:
797 tbl_fill_char(tp
, ASCII_NBRSP
, padl
);
799 tbl_fill_char(tp
, ASCII_NBRSP
, padr
);
803 tbl_number(struct termp
*tp
, const struct tbl_opts
*opts
,
804 const struct tbl_dat
*dp
,
805 const struct roffcol
*col
)
807 const char *cp
, *lastdigit
, *lastpoint
;
808 size_t intsz
, padl
, totsz
;
812 * Almost the same code as in tblcalc_number():
813 * First find the position of the decimal point.
817 lastdigit
= lastpoint
= NULL
;
818 for (cp
= dp
->string
; cp
[0] != '\0'; cp
++) {
819 if (cp
[0] == '\\' && cp
[1] == '&') {
820 lastdigit
= lastpoint
= cp
;
822 } else if (cp
[0] == opts
->decimal
&&
823 (isdigit((unsigned char)cp
[1]) ||
824 (cp
> dp
->string
&& isdigit((unsigned char)cp
[-1]))))
826 else if (isdigit((unsigned char)cp
[0]))
830 /* Then measure both widths. */
833 totsz
= term_strlen(tp
, dp
->string
);
834 if (lastdigit
!= NULL
) {
835 if (lastpoint
== NULL
)
836 lastpoint
= lastdigit
+ 1;
839 for (cp
= dp
->string
; cp
< lastpoint
; cp
++) {
841 intsz
+= term_strlen(tp
, buf
);
845 * Pad left to match the decimal position,
846 * but avoid exceeding the total column width.
849 if (col
->decimal
> intsz
&& col
->width
> totsz
) {
850 padl
= col
->decimal
- intsz
;
851 if (padl
+ totsz
> col
->width
)
852 padl
= col
->width
- totsz
;
855 /* If it is not a number, simply center the string. */
857 } else if (col
->width
> totsz
)
858 padl
= (col
->width
- totsz
) / 2;
860 tbl_fill_char(tp
, ASCII_NBRSP
, padl
);
863 /* Pad right to fill the column. */
865 if (col
->width
> padl
+ totsz
)
866 tbl_fill_char(tp
, ASCII_NBRSP
, col
->width
- padl
- totsz
);
870 tbl_word(struct termp
*tp
, const struct tbl_dat
*dp
)
874 prev_font
= tp
->fonti
;
875 if (dp
->layout
->flags
& TBL_CELL_BOLD
)
876 term_fontpush(tp
, TERMFONT_BOLD
);
877 else if (dp
->layout
->flags
& TBL_CELL_ITALIC
)
878 term_fontpush(tp
, TERMFONT_UNDER
);
880 term_word(tp
, dp
->string
);
882 term_fontpopq(tp
, prev_font
);