371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov/* $Id: tbl_term.c,v 1.43 2015/10/12 00:08:16 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2011, 2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * copyright notice and this permission notice appear in all copies.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic size_t term_tbl_strlen(const char *, void *);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void tbl_char(struct termp *, char, size_t);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amorestatic void tbl_data(struct termp *, const struct tbl_opts *,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const struct tbl_dat *,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const struct roffcol *);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic void tbl_literal(struct termp *, const struct tbl_dat *,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const struct roffcol *);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic void tbl_number(struct termp *, const struct tbl_opts *,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const struct tbl_dat *,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const struct roffcol *);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic void tbl_hrule(struct termp *, const struct tbl_span *, int);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic void tbl_word(struct termp *, const struct tbl_dat *);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return term_strlen((const struct termp *)arg, p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_tbl(struct termp *tp, const struct tbl_span *sp)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Inhibit printing of spaces: we do padding ourselves. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * The first time we're invoked for a given table block,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * calculate the table widths and decimal positions.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Center the table as a whole. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Horizontal frame at the start of boxed tables. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Vertical frame at the start of each row. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov horiz = sp->pos == TBL_SPAN_HORIZ || sp->pos == TBL_SPAN_DHORIZ;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Now print the actual data itself depending on the span type.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Match data cells to column numbers.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Remeber whether we need a vertical bar
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * after this cell.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Print the data and advance to the next cell.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Separate columns, except in the middle
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * of spans and after the last cell.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Vertical frame at the end of each row. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (sp->prev != NULL && sp->prev->layout->last->vert) ||
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX)))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If we're the last row, clean up after ourselves: clear the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * existing table configuration and set it to NULL.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Kinds of horizontal rulers:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * 0: inside the table (single or double line with crossings)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * 1: inner frame (single line with crossings and ends)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * 2: outer frame (single line without crossings with ends)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovtbl_hrule(struct termp *tp, const struct tbl_span *sp, int kind)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov line = (kind == 0 && TBL_SPAN_DHORIZ == sp->pos) ? '=' : '-';
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov c2 = sp->prev == NULL ? NULL : sp->prev->layout->first;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoretbl_data(struct termp *tp, const struct tbl_opts *opts,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoretbl_char(struct termp *tp, char c, size_t len)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovtbl_literal(struct termp *tp, const struct tbl_dat *dp,
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoretbl_number(struct termp *tp, const struct tbl_opts *opts,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * See calc_data_number(). Left-pad by taking the offset of our
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * and the maximum decimal; right-pad by the remaining amount.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovtbl_word(struct termp *tp, const struct tbl_dat *dp)