]> git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
Bugfix: never set termp->enc to the ambiguous value TERMENC_LOCALE,
[mandoc.git] / tbl_term.c
1 /* $Id: tbl_term.c,v 1.63 2018/11/28 13:43:54 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2018 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 "out.h"
30 #include "term.h"
31
32 #define IS_HORIZ(cp) ((cp)->pos == TBL_CELL_HORIZ || \
33 (cp)->pos == TBL_CELL_DHORIZ)
34
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. */
41
42
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 *);
61
62
63 /*
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.
68 */
69
70 /* Positional values of the four directions. */
71 #define BRIGHT 1
72 #define BDOWN 3
73 #define BLEFT (3 * 3)
74 #define BUP (3 * 3 * 3)
75 #define BHORIZ (BLEFT + BRIGHT)
76
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 */
106 };
107
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 */
137 };
138
139 /* Either of the above according to the selected output encoding. */
140 static const int *borders_locale;
141
142
143 static size_t
144 term_tbl_sulen(const struct roffsu *su, void *arg)
145 {
146 int i;
147
148 i = term_hen((const struct termp *)arg, su);
149 return i > 0 ? i : 0;
150 }
151
152 static size_t
153 term_tbl_strlen(const char *p, void *arg)
154 {
155 return term_strlen((const struct termp *)arg, p);
156 }
157
158 static size_t
159 term_tbl_len(size_t sz, void *arg)
160 {
161 return term_len((const struct termp *)arg, sz);
162 }
163
164
165 void
166 term_tbl(struct termp *tp, const struct tbl_span *sp)
167 {
168 const struct tbl_cell *cp, *cpn, *cpp, *cps;
169 const struct tbl_dat *dp;
170 static size_t offset;
171 size_t coloff, tsz;
172 int hspans, ic, more;
173 int dvert, fc, horiz, line, uvert;
174
175 /* Inhibit printing of spaces: we do padding ourselves. */
176
177 tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
178
179 /*
180 * The first time we're invoked for a given table block,
181 * calculate the table widths and decimal positions.
182 */
183
184 if (tp->tbl.cols == NULL) {
185 borders_locale = tp->enc == TERMENC_UTF8 ?
186 borders_utf8 : borders_ascii;
187
188 tp->tbl.len = term_tbl_len;
189 tp->tbl.slen = term_tbl_strlen;
190 tp->tbl.sulen = term_tbl_sulen;
191 tp->tbl.arg = tp;
192
193 tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
194
195 /* Tables leak .ta settings to subsequent text. */
196
197 term_tab_set(tp, NULL);
198 coloff = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
199 sp->opts->lvert;
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;
204 }
205
206 /* Center the table as a whole. */
207
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;
215 if (sp->opts->cols)
216 tsz += tp->tbl.cols[sp->opts->cols - 1].width;
217 if (offset + tsz > tp->tcol->rmargin)
218 tsz -= 1;
219 tp->tcol->offset = offset + tp->tcol->rmargin > tsz ?
220 (offset + tp->tcol->rmargin - tsz) / 2 : 0;
221 }
222
223 /* Horizontal frame at the start of boxed tables. */
224
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);
230 }
231
232 /* Set up the columns. */
233
234 tp->flags |= TERMP_MULTICOL;
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 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);
338 }
339
340 /* Print the data cells. */
341
342 more = 0;
343 if (horiz)
344 tbl_hrule(tp, sp, HRULE_DATA | HRULE_DOWN | HRULE_UP);
345 else {
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;
351 dp = sp->first;
352 hspans = 0;
353 for (ic = 0; ic < sp->opts->cols; ic++) {
354
355 /*
356 * Figure out whether to print a
357 * vertical line after this cell
358 * and advance to next layout cell.
359 */
360
361 uvert = dvert = fc = 0;
362 if (cp != NULL) {
363 cps = cp;
364 while (cps->next != NULL &&
365 cps->next->pos == TBL_CELL_SPAN)
366 cps = cps->next;
367 if (sp->pos == TBL_SPAN_DATA)
368 uvert = dvert = cps->vert;
369 switch (cp->pos) {
370 case TBL_CELL_HORIZ:
371 fc = BHORIZ;
372 break;
373 case TBL_CELL_DHORIZ:
374 fc = BHORIZ * 2;
375 break;
376 default:
377 break;
378 }
379 }
380 if (cpp != NULL) {
381 if (uvert < cpp->vert &&
382 cp != NULL &&
383 ((IS_HORIZ(cp) &&
384 !IS_HORIZ(cpp)) ||
385 (cp->next != NULL &&
386 cpp->next != NULL &&
387 IS_HORIZ(cp->next) &&
388 !IS_HORIZ(cpp->next))))
389 uvert = cpp->vert;
390 cpp = cpp->next;
391 }
392 if (sp->opts->opts & TBL_OPT_ALLBOX) {
393 if (uvert == 0)
394 uvert = 1;
395 if (dvert == 0)
396 dvert = 1;
397 }
398 if (cpn != NULL) {
399 if (dvert == 0 ||
400 (dvert < cpn->vert &&
401 tp->enc == TERMENC_UTF8))
402 dvert = cpn->vert;
403 cpn = cpn->next;
404 }
405
406 /*
407 * Skip later cells in a span,
408 * figure out whether to start a span,
409 * and advance to next data cell.
410 */
411
412 if (hspans) {
413 hspans--;
414 cp = cp->next;
415 continue;
416 }
417 if (dp != NULL) {
418 hspans = dp->hspans;
419 if (ic || sp->layout->first->pos
420 != TBL_CELL_SPAN)
421 dp = dp->next;
422 }
423
424 /*
425 * Print one line of text in the cell
426 * and remember whether there is more.
427 */
428
429 tp->tcol++;
430 if (tp->tcol->col < tp->tcol->lastcol)
431 term_flushln(tp);
432 if (tp->tcol->col < tp->tcol->lastcol)
433 more = 1;
434
435 /*
436 * Vertical frames between data cells,
437 * but not after the last column.
438 */
439
440 if (fc == 0 &&
441 ((uvert == 0 && dvert == 0 &&
442 cp != NULL && (cp->next == NULL ||
443 !IS_HORIZ(cp->next))) ||
444 tp->tcol + 1 ==
445 tp->tcols + tp->lasttcol)) {
446 if (cp != NULL)
447 cp = cp->next;
448 continue;
449 }
450
451 if (tp->viscol < tp->tcol->rmargin) {
452 (*tp->advance)(tp, tp->tcol->rmargin
453 - tp->viscol);
454 tp->viscol = tp->tcol->rmargin;
455 }
456 while (tp->viscol < tp->tcol->rmargin +
457 tp->tbl.cols[ic].spacing / 2)
458 tbl_direct_border(tp, fc, 1);
459
460 if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
461 continue;
462
463 if (cp != NULL) {
464 switch (cp->pos) {
465 case TBL_CELL_HORIZ:
466 fc = BLEFT;
467 break;
468 case TBL_CELL_DHORIZ:
469 fc = BLEFT * 2;
470 break;
471 default:
472 fc = 0;
473 break;
474 }
475 cp = cp->next;
476 }
477 if (cp != NULL) {
478 switch (cp->pos) {
479 case TBL_CELL_HORIZ:
480 fc += BRIGHT;
481 break;
482 case TBL_CELL_DHORIZ:
483 fc += BRIGHT * 2;
484 break;
485 default:
486 break;
487 }
488 }
489 if (tp->tbl.cols[ic].spacing)
490 tbl_direct_border(tp, fc +
491 BUP * uvert + BDOWN * dvert, 1);
492
493 if (tp->enc == TERMENC_UTF8)
494 uvert = dvert = 0;
495
496 if (fc != 0) {
497 if (cp != NULL &&
498 cp->pos == TBL_CELL_HORIZ)
499 fc = BHORIZ;
500 else if (cp != NULL &&
501 cp->pos == TBL_CELL_DHORIZ)
502 fc = BHORIZ * 2;
503 else
504 fc = 0;
505 }
506 if (tp->tbl.cols[ic].spacing > 2 &&
507 (uvert > 1 || dvert > 1 || fc != 0))
508 tbl_direct_border(tp, fc +
509 BUP * (uvert > 1) +
510 BDOWN * (dvert > 1), 1);
511 }
512 }
513
514 /* Print the vertical frame at the end of each row. */
515
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)) {
542 tp->tcol++;
543 (*tp->advance)(tp,
544 tp->tcol->offset > tp->viscol ?
545 tp->tcol->offset - tp->viscol : 1);
546 }
547 tbl_direct_border(tp, fc, 1);
548 }
549 (*tp->endline)(tp);
550 tp->viscol = 0;
551 } while (more);
552
553 /*
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.
557 */
558
559 term_setcol(tp, 1);
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);
565 tp->skipvsp = 1;
566 }
567 if (tp->enc == TERMENC_ASCII &&
568 sp->opts->opts & TBL_OPT_DBOX) {
569 tbl_hrule(tp, sp, HRULE_DBOX | HRULE_ENDS);
570 tp->skipvsp = 2;
571 }
572 assert(tp->tbl.cols);
573 free(tp->tbl.cols);
574 tp->tbl.cols = NULL;
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))
579 tbl_hrule(tp, sp,
580 HRULE_DATA | HRULE_DOWN | HRULE_UP | HRULE_ENDS);
581
582 tp->flags &= ~TERMP_NONOSPACE;
583 }
584
585 static void
586 tbl_hrule(struct termp *tp, const struct tbl_span *sp, int flags)
587 {
588 const struct tbl_cell *cp, *cpn, *cpp;
589 const struct roffcol *col;
590 int cross, dvert, line, linewidth, uvert;
591
592 cp = sp->layout->first;
593 cpn = cpp = NULL;
594 if (flags & HRULE_DATA) {
595 linewidth = sp->pos == TBL_SPAN_DHORIZ ? 2 : 1;
596 cpn = sp->next == NULL ? NULL : sp->next->layout->first;
597 if (cpn == cp)
598 cpn = NULL;
599 } else
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;
605 }
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);
610 else {
611 cpp = sp->prev == NULL ? NULL : sp->prev->layout->first;
612 if (cpp == cp)
613 cpp = NULL;
614 }
615 for (;;) {
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);
620 uvert = dvert = 0;
621 if (flags & HRULE_UP &&
622 (tp->enc == TERMENC_ASCII || sp->pos == TBL_SPAN_DATA ||
623 (sp->prev != NULL && sp->prev->layout == sp->layout)))
624 uvert = cp->vert;
625 if (flags & HRULE_DOWN)
626 dvert = cp->vert;
627 if ((cp = cp->next) == NULL)
628 break;
629 if (cpp != NULL) {
630 if (uvert < cpp->vert)
631 uvert = cpp->vert;
632 cpp = cpp->next;
633 }
634 if (cpn != NULL) {
635 if (dvert < cpn->vert)
636 dvert = cpn->vert;
637 cpn = cpn->next;
638 }
639 if (sp->opts->opts & TBL_OPT_ALLBOX) {
640 if (flags & HRULE_UP && uvert == 0)
641 uvert = 1;
642 if (flags & HRULE_DOWN && dvert == 0)
643 dvert = 1;
644 }
645 cross = BHORIZ * linewidth + BUP * uvert + BDOWN * dvert;
646 if (col->spacing)
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);
653 }
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);
658 (*tp->endline)(tp);
659 tp->viscol = 0;
660 }
661 }
662
663 static void
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)
667 {
668 switch (cp->pos) {
669 case TBL_CELL_HORIZ:
670 tbl_fill_border(tp, BHORIZ, col->width);
671 return;
672 case TBL_CELL_DHORIZ:
673 tbl_fill_border(tp, BHORIZ * 2, col->width);
674 return;
675 default:
676 break;
677 }
678
679 if (dp == NULL)
680 return;
681
682 switch (dp->pos) {
683 case TBL_DATA_NONE:
684 return;
685 case TBL_DATA_HORIZ:
686 case TBL_DATA_NHORIZ:
687 tbl_fill_border(tp, BHORIZ, col->width);
688 return;
689 case TBL_DATA_NDHORIZ:
690 case TBL_DATA_DHORIZ:
691 tbl_fill_border(tp, BHORIZ * 2, col->width);
692 return;
693 default:
694 break;
695 }
696
697 switch (cp->pos) {
698 case TBL_CELL_LONG:
699 case TBL_CELL_CENTRE:
700 case TBL_CELL_LEFT:
701 case TBL_CELL_RIGHT:
702 tbl_literal(tp, dp, col);
703 break;
704 case TBL_CELL_NUMBER:
705 tbl_number(tp, opts, dp, col);
706 break;
707 case TBL_CELL_DOWN:
708 case TBL_CELL_SPAN:
709 break;
710 default:
711 abort();
712 }
713 }
714
715 static void
716 tbl_fill_string(struct termp *tp, const char *cp, size_t len)
717 {
718 size_t i, sz;
719
720 sz = term_strlen(tp, cp);
721 for (i = 0; i < len; i += sz)
722 term_word(tp, cp);
723 }
724
725 static void
726 tbl_fill_char(struct termp *tp, char c, size_t len)
727 {
728 char cp[2];
729
730 cp[0] = c;
731 cp[1] = '\0';
732 tbl_fill_string(tp, cp, len);
733 }
734
735 static void
736 tbl_fill_border(struct termp *tp, int c, size_t len)
737 {
738 char buf[12];
739
740 if ((c = borders_locale[c]) > 127) {
741 (void)snprintf(buf, sizeof(buf), "\\[u%04x]", c);
742 tbl_fill_string(tp, buf, len);
743 } else
744 tbl_fill_char(tp, c, len);
745 }
746
747 static void
748 tbl_direct_border(struct termp *tp, int c, size_t len)
749 {
750 size_t i, sz;
751
752 c = borders_locale[c];
753 sz = (*tp->width)(tp, c);
754 for (i = 0; i < len; i += sz) {
755 (*tp->letter)(tp, c);
756 tp->viscol += sz;
757 }
758 }
759
760 static void
761 tbl_literal(struct termp *tp, const struct tbl_dat *dp,
762 const struct roffcol *col)
763 {
764 size_t len, padl, padr, width;
765 int ic, hspans;
766
767 assert(dp->string);
768 len = term_strlen(tp, dp->string);
769 width = col->width;
770 ic = dp->layout->col;
771 hspans = dp->hspans;
772 while (hspans--)
773 width += tp->tbl.cols[++ic].width + 3;
774
775 padr = width > len ? width - len : 0;
776 padl = 0;
777
778 switch (dp->layout->pos) {
779 case TBL_CELL_LONG:
780 padl = term_len(tp, 1);
781 padr = padr > padl ? padr - padl : 0;
782 break;
783 case TBL_CELL_CENTRE:
784 if (2 > padr)
785 break;
786 padl = padr / 2;
787 padr -= padl;
788 break;
789 case TBL_CELL_RIGHT:
790 padl = padr;
791 padr = 0;
792 break;
793 default:
794 break;
795 }
796
797 tbl_fill_char(tp, ASCII_NBRSP, padl);
798 tbl_word(tp, dp);
799 tbl_fill_char(tp, ASCII_NBRSP, padr);
800 }
801
802 static void
803 tbl_number(struct termp *tp, const struct tbl_opts *opts,
804 const struct tbl_dat *dp,
805 const struct roffcol *col)
806 {
807 const char *cp, *lastdigit, *lastpoint;
808 size_t intsz, padl, totsz;
809 char buf[2];
810
811 /*
812 * Almost the same code as in tblcalc_number():
813 * First find the position of the decimal point.
814 */
815
816 assert(dp->string);
817 lastdigit = lastpoint = NULL;
818 for (cp = dp->string; cp[0] != '\0'; cp++) {
819 if (cp[0] == '\\' && cp[1] == '&') {
820 lastdigit = lastpoint = cp;
821 break;
822 } else if (cp[0] == opts->decimal &&
823 (isdigit((unsigned char)cp[1]) ||
824 (cp > dp->string && isdigit((unsigned char)cp[-1]))))
825 lastpoint = cp;
826 else if (isdigit((unsigned char)cp[0]))
827 lastdigit = cp;
828 }
829
830 /* Then measure both widths. */
831
832 padl = 0;
833 totsz = term_strlen(tp, dp->string);
834 if (lastdigit != NULL) {
835 if (lastpoint == NULL)
836 lastpoint = lastdigit + 1;
837 intsz = 0;
838 buf[1] = '\0';
839 for (cp = dp->string; cp < lastpoint; cp++) {
840 buf[0] = cp[0];
841 intsz += term_strlen(tp, buf);
842 }
843
844 /*
845 * Pad left to match the decimal position,
846 * but avoid exceeding the total column width.
847 */
848
849 if (col->decimal > intsz && col->width > totsz) {
850 padl = col->decimal - intsz;
851 if (padl + totsz > col->width)
852 padl = col->width - totsz;
853 }
854
855 /* If it is not a number, simply center the string. */
856
857 } else if (col->width > totsz)
858 padl = (col->width - totsz) / 2;
859
860 tbl_fill_char(tp, ASCII_NBRSP, padl);
861 tbl_word(tp, dp);
862
863 /* Pad right to fill the column. */
864
865 if (col->width > padl + totsz)
866 tbl_fill_char(tp, ASCII_NBRSP, col->width - padl - totsz);
867 }
868
869 static void
870 tbl_word(struct termp *tp, const struct tbl_dat *dp)
871 {
872 int prev_font;
873
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);
879
880 term_word(tp, dp->string);
881
882 term_fontpopq(tp, prev_font);
883 }