]> git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
Partial support for the \n[an-margin] number register.
[mandoc.git] / tbl_term.c
1 /* $Id: tbl_term.c,v 1.51 2017/06/13 14:39:13 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011,2012,2014,2015,2017 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "mandoc.h"
28 #include "out.h"
29 #include "term.h"
30
31 static size_t term_tbl_len(size_t, void *);
32 static size_t term_tbl_strlen(const char *, void *);
33 static size_t term_tbl_sulen(const struct roffsu *, void *);
34 static void tbl_char(struct termp *, char, size_t);
35 static void tbl_data(struct termp *, const struct tbl_opts *,
36 const struct tbl_dat *,
37 const struct roffcol *);
38 static void tbl_literal(struct termp *, const struct tbl_dat *,
39 const struct roffcol *);
40 static void tbl_number(struct termp *, const struct tbl_opts *,
41 const struct tbl_dat *,
42 const struct roffcol *);
43 static void tbl_hrule(struct termp *, const struct tbl_span *, int);
44 static void tbl_word(struct termp *, const struct tbl_dat *);
45
46
47 static size_t
48 term_tbl_sulen(const struct roffsu *su, void *arg)
49 {
50 return term_hspan((const struct termp *)arg, su) / 24;
51 }
52
53 static size_t
54 term_tbl_strlen(const char *p, void *arg)
55 {
56 return term_strlen((const struct termp *)arg, p);
57 }
58
59 static size_t
60 term_tbl_len(size_t sz, void *arg)
61 {
62 return term_len((const struct termp *)arg, sz);
63 }
64
65 void
66 term_tbl(struct termp *tp, const struct tbl_span *sp)
67 {
68 const struct tbl_cell *cp;
69 const struct tbl_dat *dp;
70 static size_t offset;
71 size_t coloff, tsz;
72 int ic, horiz, spans, vert, more;
73 char fc;
74
75 /* Inhibit printing of spaces: we do padding ourselves. */
76
77 tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE;
78
79 /*
80 * The first time we're invoked for a given table block,
81 * calculate the table widths and decimal positions.
82 */
83
84 if (tp->tbl.cols == NULL) {
85 tp->tbl.len = term_tbl_len;
86 tp->tbl.slen = term_tbl_strlen;
87 tp->tbl.sulen = term_tbl_sulen;
88 tp->tbl.arg = tp;
89
90 tblcalc(&tp->tbl, sp, tp->tcol->offset, tp->tcol->rmargin);
91
92 /* Center the table as a whole. */
93
94 offset = tp->tcol->offset;
95 if (sp->opts->opts & TBL_OPT_CENTRE) {
96 tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
97 ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
98 for (ic = 0; ic < sp->opts->cols; ic++)
99 tsz += tp->tbl.cols[ic].width + 3;
100 tsz -= 3;
101 if (offset + tsz > tp->tcol->rmargin)
102 tsz -= 1;
103 tp->tcol->offset = offset + tp->tcol->rmargin > tsz ?
104 (offset + tp->tcol->rmargin - tsz) / 2 : 0;
105 }
106
107 /* Horizontal frame at the start of boxed tables. */
108
109 if (sp->opts->opts & TBL_OPT_DBOX)
110 tbl_hrule(tp, sp, 2);
111 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
112 tbl_hrule(tp, sp, 1);
113 }
114
115 /* Set up the columns. */
116
117 tp->flags |= TERMP_MULTICOL;
118 horiz = 0;
119 switch (sp->pos) {
120 case TBL_SPAN_HORIZ:
121 case TBL_SPAN_DHORIZ:
122 horiz = 1;
123 term_setcol(tp, 1);
124 break;
125 case TBL_SPAN_DATA:
126 term_setcol(tp, sp->opts->cols + 2);
127 coloff = tp->tcol->offset;
128
129 /* Set up a column for a left vertical frame. */
130
131 if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
132 sp->opts->lvert)
133 coloff++;
134 tp->tcol->rmargin = coloff;
135
136 /* Set up the data columns. */
137
138 dp = sp->first;
139 spans = 0;
140 for (ic = 0; ic < sp->opts->cols; ic++) {
141 if (spans == 0) {
142 tp->tcol++;
143 tp->tcol->offset = coloff;
144 }
145 coloff += tp->tbl.cols[ic].width;
146 tp->tcol->rmargin = coloff;
147 coloff++;
148 if (ic + 1 < sp->opts->cols)
149 coloff += 2;
150 if (spans) {
151 spans--;
152 continue;
153 }
154 if (dp == NULL)
155 continue;
156 spans = dp->spans;
157 dp = dp->next;
158 }
159
160 /* Set up a column for a right vertical frame. */
161
162 tp->tcol++;
163 tp->tcol->offset = coloff;
164 if (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ||
165 sp->opts->rvert)
166 coloff++;
167 tp->tcol->rmargin = coloff;
168
169 /* Spans may have reduced the number of columns. */
170
171 tp->lasttcol = tp->tcol - tp->tcols;
172
173 /* Fill the buffers for all data columns. */
174
175 tp->tcol = tp->tcols;
176 dp = sp->first;
177 spans = 0;
178 for (ic = 0; ic < sp->opts->cols; ic++) {
179 if (spans) {
180 spans--;
181 continue;
182 }
183 tp->tcol++;
184 tp->col = 0;
185 tbl_data(tp, sp->opts, dp, tp->tbl.cols + ic);
186 if (dp == NULL)
187 continue;
188 spans = dp->spans;
189 dp = dp->next;
190 }
191 break;
192 }
193
194 do {
195 /* Print the vertical frame at the start of each row. */
196
197 tp->tcol = tp->tcols;
198 fc = '\0';
199 if (sp->layout->vert ||
200 (sp->prev != NULL && sp->prev->layout->vert) ||
201 sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
202 fc = horiz ? '+' : '|';
203 else if (horiz && sp->opts->lvert)
204 fc = '-';
205 if (fc != '\0') {
206 (*tp->advance)(tp, tp->tcols->offset);
207 (*tp->letter)(tp, fc);
208 tp->viscol = tp->tcol->offset + 1;
209 }
210
211 /* Print the data cells. */
212
213 more = 0;
214 if (horiz) {
215 tbl_hrule(tp, sp, 0);
216 term_flushln(tp);
217 } else {
218 cp = sp->layout->first;
219 dp = sp->first;
220 spans = 0;
221 for (ic = 0; ic < sp->opts->cols; ic++) {
222
223 /* Advance to next layout cell. */
224
225 if (cp != NULL) {
226 vert = cp->vert;
227 cp = cp->next;
228 } else
229 vert = 0;
230
231 /* Skip later cells in a span. */
232
233 if (spans) {
234 spans--;
235 continue;
236 }
237
238 /* Advance to next data cell. */
239
240 if (dp != NULL) {
241 spans = dp->spans;
242 dp = dp->next;
243 }
244
245 /* Print one line of text in the cell. */
246
247 tp->tcol++;
248 if (tp->tcol->col < tp->tcol->lastcol)
249 term_flushln(tp);
250 if (tp->tcol->col < tp->tcol->lastcol)
251 more = 1;
252
253 /*
254 * Vertical frames between data cells,
255 * but not after the last column.
256 */
257
258 if (tp->tcol + 1 == tp->tcols + tp->lasttcol)
259 continue;
260 if (vert == 0 &&
261 sp->opts->opts & TBL_OPT_ALLBOX)
262 vert = 1;
263 if (vert == 0)
264 continue;
265
266 if (tp->tcol->rmargin + 1 > tp->viscol) {
267 (*tp->advance)(tp, tp->tcol->rmargin
268 + 1 - tp->viscol);
269 tp->viscol = tp->tcol->rmargin + 1;
270 }
271 while (vert--) {
272 (*tp->letter)(tp, '|');
273 tp->viscol++;
274 }
275 }
276 }
277
278 /* Print the vertical frame at the end of each row. */
279
280 fc = '\0';
281 if (sp->layout->last->vert ||
282 (sp->prev != NULL && sp->prev->layout->last->vert) ||
283 (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
284 fc = horiz ? '+' : '|';
285 else if (horiz && sp->opts->rvert)
286 fc = '-';
287 if (fc != '\0') {
288 if (horiz == 0) {
289 tp->tcol++;
290 (*tp->advance)(tp,
291 tp->tcol->offset > tp->viscol ?
292 tp->tcol->offset - tp->viscol : 1);
293 }
294 (*tp->letter)(tp, fc);
295 }
296 (*tp->endline)(tp);
297 tp->viscol = 0;
298 } while (more);
299
300 /*
301 * If we're the last row, clean up after ourselves: clear the
302 * existing table configuration and set it to NULL.
303 */
304
305 term_setcol(tp, 1);
306 tp->flags &= ~TERMP_MULTICOL;
307 tp->tcol->rmargin = tp->maxrmargin;
308 if (sp->next == NULL) {
309 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
310 tbl_hrule(tp, sp, 1);
311 tp->skipvsp = 1;
312 }
313 if (sp->opts->opts & TBL_OPT_DBOX) {
314 tbl_hrule(tp, sp, 2);
315 tp->skipvsp = 2;
316 }
317 assert(tp->tbl.cols);
318 free(tp->tbl.cols);
319 tp->tbl.cols = NULL;
320 tp->tcol->offset = offset;
321 } else if (horiz == 0 && sp->opts->opts & TBL_OPT_ALLBOX &&
322 (sp->next == NULL || sp->next->pos == TBL_SPAN_DATA ||
323 sp->next->next != NULL))
324 tbl_hrule(tp, sp, 1);
325
326 tp->flags &= ~TERMP_NONOSPACE;
327 }
328
329 /*
330 * Kinds of horizontal rulers:
331 * 0: inside the table (single or double line with crossings)
332 * 1: inner frame (single line with crossings and ends)
333 * 2: outer frame (single line without crossings with ends)
334 */
335 static void
336 tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
337 {
338 const struct tbl_cell *c1, *c2;
339 int vert;
340 char line, cross;
341
342 line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
343 cross = (kind < 2) ? '+' : '-';
344
345 if (kind)
346 term_word(tp, "+");
347 c1 = sp->layout->first;
348 c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
349 if (c2 == c1)
350 c2 = NULL;
351 for (;;) {
352 tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
353 vert = c1->vert;
354 if ((c1 = c1->next) == NULL)
355 break;
356 if (c2 != NULL) {
357 if (vert < c2->vert)
358 vert = c2->vert;
359 c2 = c2->next;
360 }
361 if (sp->opts->opts & TBL_OPT_ALLBOX && !vert)
362 vert = 1;
363 if (vert)
364 tbl_char(tp, cross, vert);
365 if (vert < 2)
366 tbl_char(tp, line, 2 - vert);
367 }
368 if (kind) {
369 term_word(tp, "+");
370 term_flushln(tp);
371 }
372 }
373
374 static void
375 tbl_data(struct termp *tp, const struct tbl_opts *opts,
376 const struct tbl_dat *dp,
377 const struct roffcol *col)
378 {
379
380 if (dp == NULL) {
381 tbl_char(tp, ASCII_NBRSP, col->width);
382 return;
383 }
384
385 switch (dp->pos) {
386 case TBL_DATA_NONE:
387 tbl_char(tp, ASCII_NBRSP, col->width);
388 return;
389 case TBL_DATA_HORIZ:
390 case TBL_DATA_NHORIZ:
391 tbl_char(tp, '-', col->width);
392 return;
393 case TBL_DATA_NDHORIZ:
394 case TBL_DATA_DHORIZ:
395 tbl_char(tp, '=', col->width);
396 return;
397 default:
398 break;
399 }
400
401 switch (dp->layout->pos) {
402 case TBL_CELL_HORIZ:
403 tbl_char(tp, '-', col->width);
404 break;
405 case TBL_CELL_DHORIZ:
406 tbl_char(tp, '=', col->width);
407 break;
408 case TBL_CELL_LONG:
409 case TBL_CELL_CENTRE:
410 case TBL_CELL_LEFT:
411 case TBL_CELL_RIGHT:
412 tbl_literal(tp, dp, col);
413 break;
414 case TBL_CELL_NUMBER:
415 tbl_number(tp, opts, dp, col);
416 break;
417 case TBL_CELL_DOWN:
418 tbl_char(tp, ASCII_NBRSP, col->width);
419 break;
420 default:
421 abort();
422 }
423 }
424
425 static void
426 tbl_char(struct termp *tp, char c, size_t len)
427 {
428 size_t i, sz;
429 char cp[2];
430
431 cp[0] = c;
432 cp[1] = '\0';
433
434 sz = term_strlen(tp, cp);
435
436 for (i = 0; i < len; i += sz)
437 term_word(tp, cp);
438 }
439
440 static void
441 tbl_literal(struct termp *tp, const struct tbl_dat *dp,
442 const struct roffcol *col)
443 {
444 size_t len, padl, padr, width;
445 int ic, spans;
446
447 assert(dp->string);
448 len = term_strlen(tp, dp->string);
449 width = col->width;
450 ic = dp->layout->col;
451 spans = dp->spans;
452 while (spans--)
453 width += tp->tbl.cols[++ic].width + 3;
454
455 padr = width > len ? width - len : 0;
456 padl = 0;
457
458 switch (dp->layout->pos) {
459 case TBL_CELL_LONG:
460 padl = term_len(tp, 1);
461 padr = padr > padl ? padr - padl : 0;
462 break;
463 case TBL_CELL_CENTRE:
464 if (2 > padr)
465 break;
466 padl = padr / 2;
467 padr -= padl;
468 break;
469 case TBL_CELL_RIGHT:
470 padl = padr;
471 padr = 0;
472 break;
473 default:
474 break;
475 }
476
477 tbl_char(tp, ASCII_NBRSP, padl);
478 tbl_word(tp, dp);
479 tbl_char(tp, ASCII_NBRSP, padr);
480 }
481
482 static void
483 tbl_number(struct termp *tp, const struct tbl_opts *opts,
484 const struct tbl_dat *dp,
485 const struct roffcol *col)
486 {
487 char *cp;
488 char buf[2];
489 size_t sz, psz, ssz, d, padl;
490 int i;
491
492 /*
493 * See calc_data_number(). Left-pad by taking the offset of our
494 * and the maximum decimal; right-pad by the remaining amount.
495 */
496
497 assert(dp->string);
498
499 sz = term_strlen(tp, dp->string);
500
501 buf[0] = opts->decimal;
502 buf[1] = '\0';
503
504 psz = term_strlen(tp, buf);
505
506 if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
507 for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
508 buf[0] = dp->string[i];
509 ssz += term_strlen(tp, buf);
510 }
511 d = ssz + psz;
512 } else
513 d = sz + psz;
514
515 if (col->decimal > d && col->width > sz) {
516 padl = col->decimal - d;
517 if (padl + sz > col->width)
518 padl = col->width - sz;
519 tbl_char(tp, ASCII_NBRSP, padl);
520 } else
521 padl = 0;
522 tbl_word(tp, dp);
523 if (col->width > sz + padl)
524 tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
525 }
526
527 static void
528 tbl_word(struct termp *tp, const struct tbl_dat *dp)
529 {
530 int prev_font;
531
532 prev_font = tp->fonti;
533 if (dp->layout->flags & TBL_CELL_BOLD)
534 term_fontpush(tp, TERMFONT_BOLD);
535 else if (dp->layout->flags & TBL_CELL_ITALIC)
536 term_fontpush(tp, TERMFONT_UNDER);
537
538 term_word(tp, dp->string);
539
540 term_fontpopq(tp, prev_font);
541 }