tbl_term.c revision 1.1.1.3 1 1.1.1.3 joerg /* $Vendor-Id: tbl_term.c,v 1.21 2011/09/20 23:05:49 schwarze Exp $ */
2 1.1 joerg /*
3 1.1.1.3 joerg * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
4 1.1.1.2 joerg * Copyright (c) 2011 Ingo Schwarze <schwarze (at) openbsd.org>
5 1.1 joerg *
6 1.1 joerg * Permission to use, copy, modify, and distribute this software for any
7 1.1 joerg * purpose with or without fee is hereby granted, provided that the above
8 1.1 joerg * copyright notice and this permission notice appear in all copies.
9 1.1 joerg *
10 1.1 joerg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 joerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 joerg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 joerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 joerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 joerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 joerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 joerg */
18 1.1 joerg #ifdef HAVE_CONFIG_H
19 1.1 joerg #include "config.h"
20 1.1 joerg #endif
21 1.1 joerg
22 1.1 joerg #include <assert.h>
23 1.1 joerg #include <stdio.h>
24 1.1 joerg #include <stdlib.h>
25 1.1 joerg #include <string.h>
26 1.1 joerg
27 1.1 joerg #include "mandoc.h"
28 1.1 joerg #include "out.h"
29 1.1 joerg #include "term.h"
30 1.1 joerg
31 1.1 joerg static size_t term_tbl_len(size_t, void *);
32 1.1 joerg static size_t term_tbl_strlen(const char *, void *);
33 1.1 joerg static void tbl_char(struct termp *, char, size_t);
34 1.1 joerg static void tbl_data(struct termp *, const struct tbl *,
35 1.1 joerg const struct tbl_dat *,
36 1.1 joerg const struct roffcol *);
37 1.1.1.3 joerg static size_t tbl_rulewidth(struct termp *, const struct tbl_head *);
38 1.1.1.3 joerg static void tbl_hframe(struct termp *, const struct tbl_span *, int);
39 1.1 joerg static void tbl_literal(struct termp *, const struct tbl_dat *,
40 1.1 joerg const struct roffcol *);
41 1.1 joerg static void tbl_number(struct termp *, const struct tbl *,
42 1.1 joerg const struct tbl_dat *,
43 1.1 joerg const struct roffcol *);
44 1.1 joerg static void tbl_hrule(struct termp *, const struct tbl_span *);
45 1.1 joerg static void tbl_vrule(struct termp *, const struct tbl_head *);
46 1.1 joerg
47 1.1 joerg
48 1.1 joerg static size_t
49 1.1 joerg term_tbl_strlen(const char *p, void *arg)
50 1.1 joerg {
51 1.1 joerg
52 1.1 joerg return(term_strlen((const struct termp *)arg, p));
53 1.1 joerg }
54 1.1 joerg
55 1.1 joerg static size_t
56 1.1 joerg term_tbl_len(size_t sz, void *arg)
57 1.1 joerg {
58 1.1 joerg
59 1.1 joerg return(term_len((const struct termp *)arg, sz));
60 1.1 joerg }
61 1.1 joerg
62 1.1 joerg void
63 1.1 joerg term_tbl(struct termp *tp, const struct tbl_span *sp)
64 1.1 joerg {
65 1.1 joerg const struct tbl_head *hp;
66 1.1 joerg const struct tbl_dat *dp;
67 1.1 joerg struct roffcol *col;
68 1.1.1.2 joerg int spans;
69 1.1 joerg size_t rmargin, maxrmargin;
70 1.1 joerg
71 1.1 joerg rmargin = tp->rmargin;
72 1.1 joerg maxrmargin = tp->maxrmargin;
73 1.1 joerg
74 1.1 joerg tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
75 1.1 joerg
76 1.1 joerg /* Inhibit printing of spaces: we do padding ourselves. */
77 1.1 joerg
78 1.1 joerg tp->flags |= TERMP_NONOSPACE;
79 1.1 joerg tp->flags |= TERMP_NOSPACE;
80 1.1 joerg
81 1.1 joerg /*
82 1.1 joerg * The first time we're invoked for a given table block,
83 1.1 joerg * calculate the table widths and decimal positions.
84 1.1 joerg */
85 1.1 joerg
86 1.1 joerg if (TBL_SPAN_FIRST & sp->flags) {
87 1.1 joerg term_flushln(tp);
88 1.1 joerg
89 1.1 joerg tp->tbl.len = term_tbl_len;
90 1.1 joerg tp->tbl.slen = term_tbl_strlen;
91 1.1 joerg tp->tbl.arg = tp;
92 1.1 joerg
93 1.1 joerg tblcalc(&tp->tbl, sp);
94 1.1 joerg }
95 1.1 joerg
96 1.1 joerg /* Horizontal frame at the start of boxed tables. */
97 1.1 joerg
98 1.1.1.3 joerg if (TBL_SPAN_FIRST & sp->flags) {
99 1.1.1.3 joerg if (TBL_OPT_DBOX & sp->tbl->opts)
100 1.1.1.3 joerg tbl_hframe(tp, sp, 1);
101 1.1.1.3 joerg if (TBL_OPT_DBOX & sp->tbl->opts ||
102 1.1.1.3 joerg TBL_OPT_BOX & sp->tbl->opts)
103 1.1.1.3 joerg tbl_hframe(tp, sp, 0);
104 1.1.1.3 joerg }
105 1.1 joerg
106 1.1 joerg /* Vertical frame at the start of each row. */
107 1.1 joerg
108 1.1.1.3 joerg if (TBL_OPT_BOX & sp->tbl->opts || TBL_OPT_DBOX & sp->tbl->opts)
109 1.1.1.3 joerg term_word(tp, TBL_SPAN_HORIZ == sp->pos ||
110 1.1.1.3 joerg TBL_SPAN_DHORIZ == sp->pos ? "+" : "|");
111 1.1 joerg
112 1.1 joerg /*
113 1.1 joerg * Now print the actual data itself depending on the span type.
114 1.1 joerg * Spanner spans get a horizontal rule; data spanners have their
115 1.1 joerg * data printed by matching data to header.
116 1.1 joerg */
117 1.1 joerg
118 1.1 joerg switch (sp->pos) {
119 1.1 joerg case (TBL_SPAN_HORIZ):
120 1.1 joerg /* FALLTHROUGH */
121 1.1 joerg case (TBL_SPAN_DHORIZ):
122 1.1 joerg tbl_hrule(tp, sp);
123 1.1 joerg break;
124 1.1 joerg case (TBL_SPAN_DATA):
125 1.1 joerg /* Iterate over template headers. */
126 1.1 joerg dp = sp->first;
127 1.1.1.2 joerg spans = 0;
128 1.1 joerg for (hp = sp->head; hp; hp = hp->next) {
129 1.1.1.2 joerg /*
130 1.1.1.2 joerg * If the current data header is invoked during
131 1.1.1.2 joerg * a spanner ("spans" > 0), don't emit anything
132 1.1.1.2 joerg * at all.
133 1.1.1.2 joerg */
134 1.1 joerg switch (hp->pos) {
135 1.1 joerg case (TBL_HEAD_VERT):
136 1.1 joerg /* FALLTHROUGH */
137 1.1 joerg case (TBL_HEAD_DVERT):
138 1.1.1.2 joerg if (spans <= 0)
139 1.1.1.2 joerg tbl_vrule(tp, hp);
140 1.1 joerg continue;
141 1.1 joerg case (TBL_HEAD_DATA):
142 1.1 joerg break;
143 1.1 joerg }
144 1.1 joerg
145 1.1.1.2 joerg if (--spans >= 0)
146 1.1.1.2 joerg continue;
147 1.1.1.2 joerg
148 1.1.1.3 joerg /*
149 1.1.1.3 joerg * All cells get a leading blank, except the
150 1.1.1.3 joerg * first one and those after double rulers.
151 1.1.1.3 joerg */
152 1.1.1.3 joerg
153 1.1.1.3 joerg if (hp->prev && TBL_HEAD_DVERT != hp->prev->pos)
154 1.1.1.3 joerg tbl_char(tp, ASCII_NBRSP, 1);
155 1.1.1.3 joerg
156 1.1 joerg col = &tp->tbl.cols[hp->ident];
157 1.1 joerg tbl_data(tp, sp->tbl, dp, col);
158 1.1 joerg
159 1.1.1.3 joerg /* No trailing blanks. */
160 1.1.1.3 joerg
161 1.1.1.3 joerg if (NULL == hp->next)
162 1.1.1.3 joerg break;
163 1.1.1.3 joerg
164 1.1.1.3 joerg /*
165 1.1.1.3 joerg * Add another blank between cells,
166 1.1.1.3 joerg * or two when there is no vertical ruler.
167 1.1.1.3 joerg */
168 1.1.1.3 joerg
169 1.1.1.3 joerg tbl_char(tp, ASCII_NBRSP,
170 1.1.1.3 joerg TBL_HEAD_VERT == hp->next->pos ||
171 1.1.1.3 joerg TBL_HEAD_DVERT == hp->next->pos ? 1 : 2);
172 1.1.1.3 joerg
173 1.1.1.2 joerg /*
174 1.1.1.2 joerg * Go to the next data cell and assign the
175 1.1.1.2 joerg * number of subsequent spans, if applicable.
176 1.1.1.2 joerg */
177 1.1.1.2 joerg
178 1.1.1.2 joerg if (dp) {
179 1.1.1.2 joerg spans = dp->spans;
180 1.1 joerg dp = dp->next;
181 1.1.1.2 joerg }
182 1.1 joerg }
183 1.1 joerg break;
184 1.1 joerg }
185 1.1 joerg
186 1.1.1.3 joerg /* Vertical frame at the end of each row. */
187 1.1.1.3 joerg
188 1.1.1.3 joerg if (TBL_OPT_BOX & sp->tbl->opts || TBL_OPT_DBOX & sp->tbl->opts)
189 1.1.1.3 joerg term_word(tp, TBL_SPAN_HORIZ == sp->pos ||
190 1.1.1.3 joerg TBL_SPAN_DHORIZ == sp->pos ? "+" : " |");
191 1.1 joerg term_flushln(tp);
192 1.1 joerg
193 1.1 joerg /*
194 1.1 joerg * If we're the last row, clean up after ourselves: clear the
195 1.1 joerg * existing table configuration and set it to NULL.
196 1.1 joerg */
197 1.1 joerg
198 1.1 joerg if (TBL_SPAN_LAST & sp->flags) {
199 1.1.1.3 joerg if (TBL_OPT_DBOX & sp->tbl->opts ||
200 1.1.1.3 joerg TBL_OPT_BOX & sp->tbl->opts)
201 1.1.1.3 joerg tbl_hframe(tp, sp, 0);
202 1.1.1.3 joerg if (TBL_OPT_DBOX & sp->tbl->opts)
203 1.1.1.3 joerg tbl_hframe(tp, sp, 1);
204 1.1 joerg assert(tp->tbl.cols);
205 1.1 joerg free(tp->tbl.cols);
206 1.1 joerg tp->tbl.cols = NULL;
207 1.1 joerg }
208 1.1 joerg
209 1.1 joerg tp->flags &= ~TERMP_NONOSPACE;
210 1.1 joerg tp->rmargin = rmargin;
211 1.1 joerg tp->maxrmargin = maxrmargin;
212 1.1 joerg
213 1.1 joerg }
214 1.1 joerg
215 1.1.1.3 joerg /*
216 1.1.1.3 joerg * Horizontal rules extend across the entire table.
217 1.1.1.3 joerg * Calculate the width by iterating over columns.
218 1.1.1.3 joerg */
219 1.1.1.3 joerg static size_t
220 1.1.1.3 joerg tbl_rulewidth(struct termp *tp, const struct tbl_head *hp)
221 1.1.1.3 joerg {
222 1.1.1.3 joerg size_t width;
223 1.1.1.3 joerg
224 1.1.1.3 joerg width = tp->tbl.cols[hp->ident].width;
225 1.1.1.3 joerg if (TBL_HEAD_DATA == hp->pos) {
226 1.1.1.3 joerg /* Account for leading blanks. */
227 1.1.1.3 joerg if (hp->prev && TBL_HEAD_DVERT != hp->prev->pos)
228 1.1.1.3 joerg width++;
229 1.1.1.3 joerg /* Account for trailing blanks. */
230 1.1.1.3 joerg width++;
231 1.1.1.3 joerg if (hp->next &&
232 1.1.1.3 joerg TBL_HEAD_VERT != hp->next->pos &&
233 1.1.1.3 joerg TBL_HEAD_DVERT != hp->next->pos)
234 1.1.1.3 joerg width++;
235 1.1.1.3 joerg }
236 1.1.1.3 joerg return(width);
237 1.1.1.3 joerg }
238 1.1.1.3 joerg
239 1.1.1.3 joerg /*
240 1.1.1.3 joerg * Rules inside the table can be single or double
241 1.1.1.3 joerg * and have crossings with vertical rules marked with pluses.
242 1.1.1.3 joerg */
243 1.1 joerg static void
244 1.1 joerg tbl_hrule(struct termp *tp, const struct tbl_span *sp)
245 1.1 joerg {
246 1.1 joerg const struct tbl_head *hp;
247 1.1 joerg char c;
248 1.1 joerg
249 1.1 joerg c = '-';
250 1.1 joerg if (TBL_SPAN_DHORIZ == sp->pos)
251 1.1 joerg c = '=';
252 1.1 joerg
253 1.1.1.3 joerg for (hp = sp->head; hp; hp = hp->next)
254 1.1.1.3 joerg tbl_char(tp,
255 1.1.1.3 joerg TBL_HEAD_DATA == hp->pos ? c : '+',
256 1.1.1.3 joerg tbl_rulewidth(tp, hp));
257 1.1 joerg }
258 1.1 joerg
259 1.1.1.3 joerg /*
260 1.1.1.3 joerg * Rules above and below the table are always single
261 1.1.1.3 joerg * and have an additional plus at the beginning and end.
262 1.1.1.3 joerg * For double frames, this function is called twice,
263 1.1.1.3 joerg * and the outer one does not have crossings.
264 1.1.1.3 joerg */
265 1.1 joerg static void
266 1.1.1.3 joerg tbl_hframe(struct termp *tp, const struct tbl_span *sp, int outer)
267 1.1 joerg {
268 1.1 joerg const struct tbl_head *hp;
269 1.1 joerg
270 1.1 joerg term_word(tp, "+");
271 1.1.1.3 joerg for (hp = sp->head; hp; hp = hp->next)
272 1.1.1.3 joerg tbl_char(tp,
273 1.1.1.3 joerg outer || TBL_HEAD_DATA == hp->pos ? '-' : '+',
274 1.1.1.3 joerg tbl_rulewidth(tp, hp));
275 1.1 joerg term_word(tp, "+");
276 1.1 joerg term_flushln(tp);
277 1.1 joerg }
278 1.1 joerg
279 1.1 joerg static void
280 1.1 joerg tbl_data(struct termp *tp, const struct tbl *tbl,
281 1.1 joerg const struct tbl_dat *dp,
282 1.1 joerg const struct roffcol *col)
283 1.1 joerg {
284 1.1 joerg
285 1.1 joerg if (NULL == dp) {
286 1.1 joerg tbl_char(tp, ASCII_NBRSP, col->width);
287 1.1 joerg return;
288 1.1 joerg }
289 1.1.1.2 joerg assert(dp->layout);
290 1.1 joerg
291 1.1 joerg switch (dp->pos) {
292 1.1 joerg case (TBL_DATA_NONE):
293 1.1 joerg tbl_char(tp, ASCII_NBRSP, col->width);
294 1.1 joerg return;
295 1.1 joerg case (TBL_DATA_HORIZ):
296 1.1 joerg /* FALLTHROUGH */
297 1.1 joerg case (TBL_DATA_NHORIZ):
298 1.1 joerg tbl_char(tp, '-', col->width);
299 1.1 joerg return;
300 1.1 joerg case (TBL_DATA_NDHORIZ):
301 1.1 joerg /* FALLTHROUGH */
302 1.1 joerg case (TBL_DATA_DHORIZ):
303 1.1 joerg tbl_char(tp, '=', col->width);
304 1.1 joerg return;
305 1.1 joerg default:
306 1.1 joerg break;
307 1.1 joerg }
308 1.1 joerg
309 1.1.1.2 joerg switch (dp->layout->pos) {
310 1.1 joerg case (TBL_CELL_HORIZ):
311 1.1 joerg tbl_char(tp, '-', col->width);
312 1.1 joerg break;
313 1.1 joerg case (TBL_CELL_DHORIZ):
314 1.1 joerg tbl_char(tp, '=', col->width);
315 1.1 joerg break;
316 1.1 joerg case (TBL_CELL_LONG):
317 1.1 joerg /* FALLTHROUGH */
318 1.1 joerg case (TBL_CELL_CENTRE):
319 1.1 joerg /* FALLTHROUGH */
320 1.1 joerg case (TBL_CELL_LEFT):
321 1.1 joerg /* FALLTHROUGH */
322 1.1 joerg case (TBL_CELL_RIGHT):
323 1.1 joerg tbl_literal(tp, dp, col);
324 1.1 joerg break;
325 1.1 joerg case (TBL_CELL_NUMBER):
326 1.1 joerg tbl_number(tp, tbl, dp, col);
327 1.1 joerg break;
328 1.1.1.2 joerg case (TBL_CELL_DOWN):
329 1.1.1.2 joerg tbl_char(tp, ASCII_NBRSP, col->width);
330 1.1.1.2 joerg break;
331 1.1 joerg default:
332 1.1 joerg abort();
333 1.1 joerg /* NOTREACHED */
334 1.1 joerg }
335 1.1 joerg }
336 1.1 joerg
337 1.1 joerg static void
338 1.1 joerg tbl_vrule(struct termp *tp, const struct tbl_head *hp)
339 1.1 joerg {
340 1.1 joerg
341 1.1 joerg switch (hp->pos) {
342 1.1 joerg case (TBL_HEAD_VERT):
343 1.1 joerg term_word(tp, "|");
344 1.1 joerg break;
345 1.1 joerg case (TBL_HEAD_DVERT):
346 1.1 joerg term_word(tp, "||");
347 1.1 joerg break;
348 1.1 joerg default:
349 1.1 joerg break;
350 1.1 joerg }
351 1.1 joerg }
352 1.1 joerg
353 1.1 joerg static void
354 1.1 joerg tbl_char(struct termp *tp, char c, size_t len)
355 1.1 joerg {
356 1.1 joerg size_t i, sz;
357 1.1 joerg char cp[2];
358 1.1 joerg
359 1.1 joerg cp[0] = c;
360 1.1 joerg cp[1] = '\0';
361 1.1 joerg
362 1.1 joerg sz = term_strlen(tp, cp);
363 1.1 joerg
364 1.1 joerg for (i = 0; i < len; i += sz)
365 1.1 joerg term_word(tp, cp);
366 1.1 joerg }
367 1.1 joerg
368 1.1 joerg static void
369 1.1 joerg tbl_literal(struct termp *tp, const struct tbl_dat *dp,
370 1.1 joerg const struct roffcol *col)
371 1.1 joerg {
372 1.1.1.3 joerg size_t len, padl, padr;
373 1.1 joerg
374 1.1.1.2 joerg assert(dp->string);
375 1.1.1.3 joerg len = term_strlen(tp, dp->string);
376 1.1.1.3 joerg padr = col->width > len ? col->width - len : 0;
377 1.1.1.3 joerg padl = 0;
378 1.1 joerg
379 1.1.1.2 joerg switch (dp->layout->pos) {
380 1.1 joerg case (TBL_CELL_LONG):
381 1.1.1.3 joerg padl = term_len(tp, 1);
382 1.1.1.3 joerg padr = padr > padl ? padr - padl : 0;
383 1.1 joerg break;
384 1.1 joerg case (TBL_CELL_CENTRE):
385 1.1.1.3 joerg if (2 > padr)
386 1.1.1.2 joerg break;
387 1.1.1.3 joerg padl = padr / 2;
388 1.1.1.2 joerg padr -= padl;
389 1.1 joerg break;
390 1.1 joerg case (TBL_CELL_RIGHT):
391 1.1.1.3 joerg padl = padr;
392 1.1.1.3 joerg padr = 0;
393 1.1 joerg break;
394 1.1 joerg default:
395 1.1 joerg break;
396 1.1 joerg }
397 1.1 joerg
398 1.1 joerg tbl_char(tp, ASCII_NBRSP, padl);
399 1.1.1.2 joerg term_word(tp, dp->string);
400 1.1.1.3 joerg tbl_char(tp, ASCII_NBRSP, padr);
401 1.1 joerg }
402 1.1 joerg
403 1.1 joerg static void
404 1.1 joerg tbl_number(struct termp *tp, const struct tbl *tbl,
405 1.1 joerg const struct tbl_dat *dp,
406 1.1 joerg const struct roffcol *col)
407 1.1 joerg {
408 1.1 joerg char *cp;
409 1.1 joerg char buf[2];
410 1.1 joerg size_t sz, psz, ssz, d, padl;
411 1.1 joerg int i;
412 1.1 joerg
413 1.1 joerg /*
414 1.1 joerg * See calc_data_number(). Left-pad by taking the offset of our
415 1.1 joerg * and the maximum decimal; right-pad by the remaining amount.
416 1.1 joerg */
417 1.1 joerg
418 1.1.1.2 joerg assert(dp->string);
419 1.1 joerg
420 1.1.1.2 joerg sz = term_strlen(tp, dp->string);
421 1.1 joerg
422 1.1 joerg buf[0] = tbl->decimal;
423 1.1 joerg buf[1] = '\0';
424 1.1 joerg
425 1.1 joerg psz = term_strlen(tp, buf);
426 1.1 joerg
427 1.1.1.2 joerg if (NULL != (cp = strrchr(dp->string, tbl->decimal))) {
428 1.1 joerg buf[1] = '\0';
429 1.1.1.2 joerg for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
430 1.1.1.2 joerg buf[0] = dp->string[i];
431 1.1 joerg ssz += term_strlen(tp, buf);
432 1.1 joerg }
433 1.1 joerg d = ssz + psz;
434 1.1 joerg } else
435 1.1 joerg d = sz + psz;
436 1.1 joerg
437 1.1 joerg padl = col->decimal - d;
438 1.1 joerg
439 1.1 joerg tbl_char(tp, ASCII_NBRSP, padl);
440 1.1.1.2 joerg term_word(tp, dp->string);
441 1.1.1.3 joerg if (col->width > sz + padl)
442 1.1.1.3 joerg tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
443 1.1 joerg }
444 1.1 joerg
445