]> git.cameronkatri.com Git - mandoc.git/blob - tbl_term.c
Explicit block closure macros clobber next-line block head scope,
[mandoc.git] / tbl_term.c
1 /* $Id: tbl_term.c,v 1.31 2014/10/14 18:18:05 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2014 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 size_t tbl_rulewidth(struct termp *, const struct tbl_head *);
38 static void tbl_hframe(struct termp *, const struct tbl_span *, int);
39 static void tbl_literal(struct termp *, const struct tbl_dat *,
40 const struct roffcol *);
41 static void tbl_number(struct termp *, const struct tbl_opts *,
42 const struct tbl_dat *,
43 const struct roffcol *);
44 static void tbl_hrule(struct termp *, const struct tbl_span *);
45 static void tbl_vrule(struct termp *, const struct tbl_head *);
46 static void tbl_word(struct termp *, const struct tbl_dat *);
47
48
49 static size_t
50 term_tbl_strlen(const char *p, void *arg)
51 {
52
53 return(term_strlen((const struct termp *)arg, p));
54 }
55
56 static size_t
57 term_tbl_len(size_t sz, void *arg)
58 {
59
60 return(term_len((const struct termp *)arg, sz));
61 }
62
63 void
64 term_tbl(struct termp *tp, const struct tbl_span *sp)
65 {
66 const struct tbl_head *hp;
67 const struct tbl_dat *dp;
68 struct roffcol *col;
69 int spans;
70 size_t rmargin, maxrmargin;
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 (TBL_SPAN_FIRST & sp->flags) {
88 term_flushln(tp);
89
90 tp->tbl.len = term_tbl_len;
91 tp->tbl.slen = term_tbl_strlen;
92 tp->tbl.arg = tp;
93
94 tblcalc(&tp->tbl, sp, rmargin - tp->offset);
95 }
96
97 /* Horizontal frame at the start of boxed tables. */
98
99 if (TBL_SPAN_FIRST & sp->flags) {
100 if (TBL_OPT_DBOX & sp->opts->opts)
101 tbl_hframe(tp, sp, 1);
102 if (TBL_OPT_DBOX & sp->opts->opts ||
103 TBL_OPT_BOX & sp->opts->opts)
104 tbl_hframe(tp, sp, 0);
105 }
106
107 /* Vertical frame at the start of each row. */
108
109 if ((TBL_OPT_BOX | TBL_OPT_DBOX) & sp->opts->opts ||
110 (sp->head != NULL && sp->head->vert))
111 term_word(tp, TBL_SPAN_HORIZ == sp->pos ||
112 TBL_SPAN_DHORIZ == sp->pos ? "+" : "|");
113
114 /*
115 * Now print the actual data itself depending on the span type.
116 * Spanner spans get a horizontal rule; data spanners have their
117 * data printed by matching data to header.
118 */
119
120 switch (sp->pos) {
121 case TBL_SPAN_HORIZ:
122 /* FALLTHROUGH */
123 case TBL_SPAN_DHORIZ:
124 tbl_hrule(tp, sp);
125 break;
126 case TBL_SPAN_DATA:
127 /* Iterate over template headers. */
128 dp = sp->first;
129 spans = 0;
130 for (hp = sp->head; hp; hp = hp->next) {
131
132 /*
133 * If the current data header is invoked during
134 * a spanner ("spans" > 0), don't emit anything
135 * at all.
136 */
137
138 if (--spans >= 0)
139 continue;
140
141 /* Separate columns. */
142
143 if (NULL != hp->prev)
144 tbl_vrule(tp, hp);
145
146 col = &tp->tbl.cols[hp->ident];
147 tbl_data(tp, sp->opts, dp, col);
148
149 /*
150 * Go to the next data cell and assign the
151 * number of subsequent spans, if applicable.
152 */
153
154 if (dp) {
155 spans = dp->spans;
156 dp = dp->next;
157 }
158 }
159 break;
160 }
161
162 /* Vertical frame at the end of each row. */
163
164 if ((TBL_OPT_BOX | TBL_OPT_DBOX) & sp->opts->opts ||
165 sp->layout->vert)
166 term_word(tp, TBL_SPAN_HORIZ == sp->pos ||
167 TBL_SPAN_DHORIZ == sp->pos ? "+" : " |");
168 term_flushln(tp);
169
170 /*
171 * If we're the last row, clean up after ourselves: clear the
172 * existing table configuration and set it to NULL.
173 */
174
175 if (TBL_SPAN_LAST & sp->flags) {
176 if (TBL_OPT_DBOX & sp->opts->opts ||
177 TBL_OPT_BOX & sp->opts->opts) {
178 tbl_hframe(tp, sp, 0);
179 tp->skipvsp = 1;
180 }
181 if (TBL_OPT_DBOX & sp->opts->opts) {
182 tbl_hframe(tp, sp, 1);
183 tp->skipvsp = 2;
184 }
185 assert(tp->tbl.cols);
186 free(tp->tbl.cols);
187 tp->tbl.cols = NULL;
188 }
189
190 tp->flags &= ~TERMP_NONOSPACE;
191 tp->rmargin = rmargin;
192 tp->maxrmargin = maxrmargin;
193
194 }
195
196 /*
197 * Horizontal rules extend across the entire table.
198 * Calculate the width by iterating over columns.
199 */
200 static size_t
201 tbl_rulewidth(struct termp *tp, const struct tbl_head *hp)
202 {
203 size_t width;
204
205 width = tp->tbl.cols[hp->ident].width;
206
207 /* Account for leading blanks. */
208 if (hp->prev)
209 width += 2 - hp->vert;
210
211 /* Account for trailing blank. */
212 width++;
213
214 return(width);
215 }
216
217 /*
218 * Rules inside the table can be single or double
219 * and have crossings with vertical rules marked with pluses.
220 */
221 static void
222 tbl_hrule(struct termp *tp, const struct tbl_span *sp)
223 {
224 const struct tbl_head *hp;
225 char c;
226
227 c = '-';
228 if (TBL_SPAN_DHORIZ == sp->pos)
229 c = '=';
230
231 for (hp = sp->head; hp; hp = hp->next) {
232 if (hp->prev && hp->vert)
233 tbl_char(tp, '+', hp->vert);
234 tbl_char(tp, c, tbl_rulewidth(tp, hp));
235 }
236 }
237
238 /*
239 * Rules above and below the table are always single
240 * and have an additional plus at the beginning and end.
241 * For double frames, this function is called twice,
242 * and the outer one does not have crossings.
243 */
244 static void
245 tbl_hframe(struct termp *tp, const struct tbl_span *sp, int outer)
246 {
247 const struct tbl_head *hp;
248
249 term_word(tp, "+");
250 for (hp = sp->head; hp; hp = hp->next) {
251 if (hp->prev && hp->vert)
252 tbl_char(tp, (outer ? '-' : '+'), hp->vert);
253 tbl_char(tp, '-', tbl_rulewidth(tp, hp));
254 }
255 term_word(tp, "+");
256 term_flushln(tp);
257 }
258
259 static void
260 tbl_data(struct termp *tp, const struct tbl_opts *opts,
261 const struct tbl_dat *dp,
262 const struct roffcol *col)
263 {
264
265 if (NULL == dp) {
266 tbl_char(tp, ASCII_NBRSP, col->width);
267 return;
268 }
269 assert(dp->layout);
270
271 switch (dp->pos) {
272 case TBL_DATA_NONE:
273 tbl_char(tp, ASCII_NBRSP, col->width);
274 return;
275 case TBL_DATA_HORIZ:
276 /* FALLTHROUGH */
277 case TBL_DATA_NHORIZ:
278 tbl_char(tp, '-', col->width);
279 return;
280 case TBL_DATA_NDHORIZ:
281 /* FALLTHROUGH */
282 case TBL_DATA_DHORIZ:
283 tbl_char(tp, '=', col->width);
284 return;
285 default:
286 break;
287 }
288
289 switch (dp->layout->pos) {
290 case TBL_CELL_HORIZ:
291 tbl_char(tp, '-', col->width);
292 break;
293 case TBL_CELL_DHORIZ:
294 tbl_char(tp, '=', col->width);
295 break;
296 case TBL_CELL_LONG:
297 /* FALLTHROUGH */
298 case TBL_CELL_CENTRE:
299 /* FALLTHROUGH */
300 case TBL_CELL_LEFT:
301 /* FALLTHROUGH */
302 case TBL_CELL_RIGHT:
303 tbl_literal(tp, dp, col);
304 break;
305 case TBL_CELL_NUMBER:
306 tbl_number(tp, opts, dp, col);
307 break;
308 case TBL_CELL_DOWN:
309 tbl_char(tp, ASCII_NBRSP, col->width);
310 break;
311 default:
312 abort();
313 /* NOTREACHED */
314 }
315 }
316
317 static void
318 tbl_vrule(struct termp *tp, const struct tbl_head *hp)
319 {
320
321 tbl_char(tp, ASCII_NBRSP, 1);
322 if (0 < hp->vert)
323 tbl_char(tp, '|', hp->vert);
324 if (2 > hp->vert)
325 tbl_char(tp, ASCII_NBRSP, 2 - hp->vert);
326 }
327
328 static void
329 tbl_char(struct termp *tp, char c, size_t len)
330 {
331 size_t i, sz;
332 char cp[2];
333
334 cp[0] = c;
335 cp[1] = '\0';
336
337 sz = term_strlen(tp, cp);
338
339 for (i = 0; i < len; i += sz)
340 term_word(tp, cp);
341 }
342
343 static void
344 tbl_literal(struct termp *tp, const struct tbl_dat *dp,
345 const struct roffcol *col)
346 {
347 struct tbl_head *hp;
348 size_t width, len, padl, padr;
349 int spans;
350
351 assert(dp->string);
352 len = term_strlen(tp, dp->string);
353
354 hp = dp->layout->head->next;
355 width = col->width;
356 for (spans = dp->spans; spans--; hp = hp->next)
357 width += tp->tbl.cols[hp->ident].width + 3;
358
359 padr = width > len ? width - len : 0;
360 padl = 0;
361
362 switch (dp->layout->pos) {
363 case TBL_CELL_LONG:
364 padl = term_len(tp, 1);
365 padr = padr > padl ? padr - padl : 0;
366 break;
367 case TBL_CELL_CENTRE:
368 if (2 > padr)
369 break;
370 padl = padr / 2;
371 padr -= padl;
372 break;
373 case TBL_CELL_RIGHT:
374 padl = padr;
375 padr = 0;
376 break;
377 default:
378 break;
379 }
380
381 tbl_char(tp, ASCII_NBRSP, padl);
382 tbl_word(tp, dp);
383 tbl_char(tp, ASCII_NBRSP, padr);
384 }
385
386 static void
387 tbl_number(struct termp *tp, const struct tbl_opts *opts,
388 const struct tbl_dat *dp,
389 const struct roffcol *col)
390 {
391 char *cp;
392 char buf[2];
393 size_t sz, psz, ssz, d, padl;
394 int i;
395
396 /*
397 * See calc_data_number(). Left-pad by taking the offset of our
398 * and the maximum decimal; right-pad by the remaining amount.
399 */
400
401 assert(dp->string);
402
403 sz = term_strlen(tp, dp->string);
404
405 buf[0] = opts->decimal;
406 buf[1] = '\0';
407
408 psz = term_strlen(tp, buf);
409
410 if (NULL != (cp = strrchr(dp->string, opts->decimal))) {
411 buf[1] = '\0';
412 for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
413 buf[0] = dp->string[i];
414 ssz += term_strlen(tp, buf);
415 }
416 d = ssz + psz;
417 } else
418 d = sz + psz;
419
420 padl = col->decimal - d;
421
422 tbl_char(tp, ASCII_NBRSP, padl);
423 tbl_word(tp, dp);
424 if (col->width > sz + padl)
425 tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
426 }
427
428 static void
429 tbl_word(struct termp *tp, const struct tbl_dat *dp)
430 {
431 const void *prev_font;
432
433 prev_font = term_fontq(tp);
434 if (dp->layout->flags & TBL_CELL_BOLD)
435 term_fontpush(tp, TERMFONT_BOLD);
436 else if (dp->layout->flags & TBL_CELL_ITALIC)
437 term_fontpush(tp, TERMFONT_UNDER);
438
439 term_word(tp, dp->string);
440
441 term_fontpopq(tp, prev_font);
442 }