]> git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
Flush the line preceding a table before clearing the right margin,
[mandoc.git] / tbl_term.c
1 /* $Id: tbl_term.c,v 1.39 2015/03/06 11:03:03 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2014, 2015 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 void tbl_char(struct termp *, char, size_t);
34 static void tbl_data(struct termp *, const struct tbl_opts *,
35 const struct tbl_dat *,
36 const struct roffcol *);
37 static void tbl_literal(struct termp *, const struct tbl_dat *,
38 const struct roffcol *);
39 static void tbl_number(struct termp *, const struct tbl_opts *,
40 const struct tbl_dat *,
41 const struct roffcol *);
42 static void tbl_hrule(struct termp *, const struct tbl_span *, int);
43 static void tbl_word(struct termp *, const struct tbl_dat *);
44
45
46 static size_t
47 term_tbl_strlen(const char *p, void *arg)
48 {
49
50 return(term_strlen((const struct termp *)arg, p));
51 }
52
53 static size_t
54 term_tbl_len(size_t sz, void *arg)
55 {
56
57 return(term_len((const struct termp *)arg, sz));
58 }
59
60 void
61 term_tbl(struct termp *tp, const struct tbl_span *sp)
62 {
63 const struct tbl_cell *cp;
64 const struct tbl_dat *dp;
65 static size_t offset;
66 size_t rmargin, maxrmargin, tsz;
67 int ic, horiz, spans, vert;
68
69 if (tp->tbl.cols == NULL)
70 term_flushln(tp);
71
72 rmargin = tp->rmargin;
73 maxrmargin = tp->maxrmargin;
74
75 tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
76
77 /* Inhibit printing of spaces: we do padding ourselves. */
78
79 tp->flags |= TERMP_NONOSPACE;
80 tp->flags |= TERMP_NOSPACE;
81
82 /*
83 * The first time we're invoked for a given table block,
84 * calculate the table widths and decimal positions.
85 */
86
87 if (tp->tbl.cols == NULL) {
88 tp->tbl.len = term_tbl_len;
89 tp->tbl.slen = term_tbl_strlen;
90 tp->tbl.arg = tp;
91
92 tblcalc(&tp->tbl, sp, rmargin - tp->offset);
93
94 /* Center the table as a whole. */
95
96 offset = tp->offset;
97 if (sp->opts->opts & TBL_OPT_CENTRE) {
98 tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
99 ? 2 : !!sp->opts->lvert + !!sp->opts->rvert;
100 for (ic = 0; ic < sp->opts->cols; ic++)
101 tsz += tp->tbl.cols[ic].width + 3;
102 tsz -= 3;
103 if (offset + tsz > rmargin)
104 tsz -= 1;
105 tp->offset = (offset + rmargin > tsz) ?
106 (offset + rmargin - tsz) / 2 : 0;
107 }
108
109 /* Horizontal frame at the start of boxed tables. */
110
111 if (sp->opts->opts & TBL_OPT_DBOX)
112 tbl_hrule(tp, sp, 2);
113 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
114 tbl_hrule(tp, sp, 1);
115 }
116
117 /* Vertical frame at the start of each row. */
118
119 horiz = sp->pos == TBL_SPAN_HORIZ || sp->pos == TBL_SPAN_DHORIZ;
120
121 if (sp->layout->vert ||
122 (sp->prev != NULL && sp->prev->layout->vert) ||
123 sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX))
124 term_word(tp, horiz ? "+" : "|");
125 else if (sp->opts->lvert)
126 tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
127
128 /*
129 * Now print the actual data itself depending on the span type.
130 * Match data cells to column numbers.
131 */
132
133 if (sp->pos == TBL_SPAN_DATA) {
134 cp = sp->layout->first;
135 dp = sp->first;
136 spans = 0;
137 for (ic = 0; ic < sp->opts->cols; ic++) {
138
139 /*
140 * Remeber whether we need a vertical bar
141 * after this cell.
142 */
143
144 vert = cp == NULL ? 0 : cp->vert;
145
146 /*
147 * Print the data and advance to the next cell.
148 */
149
150 if (spans == 0) {
151 tbl_data(tp, sp->opts, dp, tp->tbl.cols + ic);
152 if (dp != NULL) {
153 spans = dp->spans;
154 dp = dp->next;
155 }
156 } else
157 spans--;
158 if (cp != NULL)
159 cp = cp->next;
160
161 /*
162 * Separate columns, except in the middle
163 * of spans and after the last cell.
164 */
165
166 if (ic + 1 == sp->opts->cols || spans)
167 continue;
168
169 tbl_char(tp, ASCII_NBRSP, 1);
170 if (vert > 0)
171 tbl_char(tp, '|', vert);
172 if (vert < 2)
173 tbl_char(tp, ASCII_NBRSP, 2 - vert);
174 }
175 } else if (horiz)
176 tbl_hrule(tp, sp, 0);
177
178 /* Vertical frame at the end of each row. */
179
180 if (sp->layout->last->vert ||
181 (sp->prev != NULL && sp->prev->layout->last->vert) ||
182 (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
183 term_word(tp, horiz ? "+" : " |");
184 else if (sp->opts->rvert)
185 tbl_char(tp, horiz ? '-' : ASCII_NBRSP, 1);
186 term_flushln(tp);
187
188 /*
189 * If we're the last row, clean up after ourselves: clear the
190 * existing table configuration and set it to NULL.
191 */
192
193 if (sp->next == NULL) {
194 if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
195 tbl_hrule(tp, sp, 1);
196 tp->skipvsp = 1;
197 }
198 if (sp->opts->opts & TBL_OPT_DBOX) {
199 tbl_hrule(tp, sp, 2);
200 tp->skipvsp = 2;
201 }
202 assert(tp->tbl.cols);
203 free(tp->tbl.cols);
204 tp->tbl.cols = NULL;
205 tp->offset = offset;
206 }
207
208 tp->flags &= ~TERMP_NONOSPACE;
209 tp->rmargin = rmargin;
210 tp->maxrmargin = maxrmargin;
211 }
212
213 /*
214 * Kinds of horizontal rulers:
215 * 0: inside the table (single or double line with crossings)
216 * 1: inner frame (single line with crossings and ends)
217 * 2: outer frame (single line without crossings with ends)
218 */
219 static void
220 tbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
221 {
222 const struct tbl_cell *c1, *c2;
223 int vert;
224 char line, cross;
225
226 line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
227 cross = (kind < 2) ? '+' : '-';
228
229 if (kind)
230 term_word(tp, "+");
231 c1 = sp->layout->first;
232 c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
233 if (c2 == c1)
234 c2 = NULL;
235 for (;;) {
236 tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
237 vert = c1->vert;
238 if ((c1 = c1->next) == NULL)
239 break;
240 if (c2 != NULL) {
241 if (vert < c2->vert)
242 vert = c2->vert;
243 c2 = c2->next;
244 }
245 if (vert)
246 tbl_char(tp, cross, vert);
247 if (vert < 2)
248 tbl_char(tp, line, 2 - vert);
249 }
250 if (kind) {
251 term_word(tp, "+");
252 term_flushln(tp);
253 }
254 }
255
256 static void
257 tbl_data(struct termp *tp, const struct tbl_opts *opts,
258 const struct tbl_dat *dp,
259 const struct roffcol *col)
260 {
261
262 if (dp == NULL) {
263 tbl_char(tp, ASCII_NBRSP, col->width);
264 return;
265 }
266
267 switch (dp->pos) {
268 case TBL_DATA_NONE:
269 tbl_char(tp, ASCII_NBRSP, col->width);
270 return;
271 case TBL_DATA_HORIZ:
272 /* FALLTHROUGH */
273 case TBL_DATA_NHORIZ:
274 tbl_char(tp, '-', col->width);
275 return;
276 case TBL_DATA_NDHORIZ:
277 /* FALLTHROUGH */
278 case TBL_DATA_DHORIZ:
279 tbl_char(tp, '=', col->width);
280 return;
281 default:
282 break;
283 }
284
285 switch (dp->layout->pos) {
286 case TBL_CELL_HORIZ:
287 tbl_char(tp, '-', col->width);
288 break;
289 case TBL_CELL_DHORIZ:
290 tbl_char(tp, '=', col->width);
291 break;
292 case TBL_CELL_LONG:
293 /* FALLTHROUGH */
294 case TBL_CELL_CENTRE:
295 /* FALLTHROUGH */
296 case TBL_CELL_LEFT:
297 /* FALLTHROUGH */
298 case TBL_CELL_RIGHT:
299 tbl_literal(tp, dp, col);
300 break;
301 case TBL_CELL_NUMBER:
302 tbl_number(tp, opts, dp, col);
303 break;
304 case TBL_CELL_DOWN:
305 tbl_char(tp, ASCII_NBRSP, col->width);
306 break;
307 default:
308 abort();
309 /* NOTREACHED */
310 }
311 }
312
313 static void
314 tbl_char(struct termp *tp, char c, size_t len)
315 {
316 size_t i, sz;
317 char cp[2];
318
319 cp[0] = c;
320 cp[1] = '\0';
321
322 sz = term_strlen(tp, cp);
323
324 for (i = 0; i < len; i += sz)
325 term_word(tp, cp);
326 }
327
328 static void
329 tbl_literal(struct termp *tp, const struct tbl_dat *dp,
330 const struct roffcol *col)
331 {
332 size_t len, padl, padr, width;
333 int ic, spans;
334
335 assert(dp->string);
336 len = term_strlen(tp, dp->string);
337 width = col->width;
338 ic = dp->layout->col;
339 spans = dp->spans;
340 while (spans--)
341 width += tp->tbl.cols[++ic].width + 3;
342
343 padr = width > len ? width - len : 0;
344 padl = 0;
345
346 switch (dp->layout->pos) {
347 case TBL_CELL_LONG:
348 padl = term_len(tp, 1);
349 padr = padr > padl ? padr - padl : 0;
350 break;
351 case TBL_CELL_CENTRE:
352 if (2 > padr)
353 break;
354 padl = padr / 2;
355 padr -= padl;
356 break;
357 case TBL_CELL_RIGHT:
358 padl = padr;
359 padr = 0;
360 break;
361 default:
362 break;
363 }
364
365 tbl_char(tp, ASCII_NBRSP, padl);
366 tbl_word(tp, dp);
367 tbl_char(tp, ASCII_NBRSP, padr);
368 }
369
370 static void
371 tbl_number(struct termp *tp, const struct tbl_opts *opts,
372 const struct tbl_dat *dp,
373 const struct roffcol *col)
374 {
375 char *cp;
376 char buf[2];
377 size_t sz, psz, ssz, d, padl;
378 int i;
379
380 /*
381 * See calc_data_number(). Left-pad by taking the offset of our
382 * and the maximum decimal; right-pad by the remaining amount.
383 */
384
385 assert(dp->string);
386
387 sz = term_strlen(tp, dp->string);
388
389 buf[0] = opts->decimal;
390 buf[1] = '\0';
391
392 psz = term_strlen(tp, buf);
393
394 if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
395 for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
396 buf[0] = dp->string[i];
397 ssz += term_strlen(tp, buf);
398 }
399 d = ssz + psz;
400 } else
401 d = sz + psz;
402
403 if (col->decimal > d && col->width > sz) {
404 padl = col->decimal - d;
405 if (padl + sz > col->width)
406 padl = col->width - sz;
407 tbl_char(tp, ASCII_NBRSP, padl);
408 } else
409 padl = 0;
410 tbl_word(tp, dp);
411 if (col->width > sz + padl)
412 tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
413 }
414
415 static void
416 tbl_word(struct termp *tp, const struct tbl_dat *dp)
417 {
418 int prev_font;
419
420 prev_font = tp->fonti;
421 if (dp->layout->flags & TBL_CELL_BOLD)
422 term_fontpush(tp, TERMFONT_BOLD);
423 else if (dp->layout->flags & TBL_CELL_ITALIC)
424 term_fontpush(tp, TERMFONT_UNDER);
425
426 term_word(tp, dp->string);
427
428 term_fontpopq(tp, prev_font);
429 }