version 1.34, 2015/01/28 04:19:35 |
version 1.44, 2017/06/04 22:44:15 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011, 2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2011,2012,2014,2015,2017 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
term_tbl_strlen(const char *p, void *arg) |
term_tbl_strlen(const char *p, void *arg) |
{ |
{ |
|
|
return(term_strlen((const struct termp *)arg, p)); |
return term_strlen((const struct termp *)arg, p); |
} |
} |
|
|
static size_t |
static size_t |
term_tbl_len(size_t sz, void *arg) |
term_tbl_len(size_t sz, void *arg) |
{ |
{ |
|
|
return(term_len((const struct termp *)arg, sz)); |
return term_len((const struct termp *)arg, sz); |
} |
} |
|
|
void |
void |
term_tbl(struct termp *tp, const struct tbl_span *sp) |
term_tbl(struct termp *tp, const struct tbl_span *sp) |
{ |
{ |
const struct tbl_head *hp; |
|
const struct tbl_cell *cp; |
const struct tbl_cell *cp; |
const struct tbl_dat *dp; |
const struct tbl_dat *dp; |
static size_t offset; |
static size_t offset; |
size_t rmargin, maxrmargin, tsz; |
size_t tsz; |
int horiz, spans, vert; |
int ic, horiz, spans, vert; |
|
|
rmargin = tp->rmargin; |
|
maxrmargin = tp->maxrmargin; |
|
|
|
tp->rmargin = tp->maxrmargin = TERM_MAXMARGIN; |
|
|
|
/* Inhibit printing of spaces: we do padding ourselves. */ |
/* Inhibit printing of spaces: we do padding ourselves. */ |
|
|
tp->flags |= TERMP_NONOSPACE; |
tp->flags |= TERMP_NOSPACE | TERMP_NONOSPACE | TERMP_BRNEVER; |
tp->flags |= TERMP_NOSPACE; |
|
|
|
/* |
/* |
* The first time we're invoked for a given table block, |
* The first time we're invoked for a given table block, |
* calculate the table widths and decimal positions. |
* calculate the table widths and decimal positions. |
*/ |
*/ |
|
|
if (sp->flags & TBL_SPAN_FIRST) { |
if (tp->tbl.cols == NULL) { |
term_flushln(tp); |
|
|
|
tp->tbl.len = term_tbl_len; |
tp->tbl.len = term_tbl_len; |
tp->tbl.slen = term_tbl_strlen; |
tp->tbl.slen = term_tbl_strlen; |
tp->tbl.arg = tp; |
tp->tbl.arg = tp; |
|
|
tblcalc(&tp->tbl, sp, rmargin - tp->offset); |
tblcalc(&tp->tbl, sp, tp->rmargin - tp->offset); |
|
|
/* Center the table as a whole. */ |
/* Center the table as a whole. */ |
|
|
Line 97 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
Line 88 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
if (sp->opts->opts & TBL_OPT_CENTRE) { |
if (sp->opts->opts & TBL_OPT_CENTRE) { |
tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) |
tsz = sp->opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) |
? 2 : !!sp->opts->lvert + !!sp->opts->rvert; |
? 2 : !!sp->opts->lvert + !!sp->opts->rvert; |
for (hp = sp->head; hp != NULL; hp = hp->next) |
for (ic = 0; ic < sp->opts->cols; ic++) |
tsz += tp->tbl.cols[hp->ident].width + 3; |
tsz += tp->tbl.cols[ic].width + 3; |
tsz -= 3; |
tsz -= 3; |
if (offset + tsz > rmargin) |
if (offset + tsz > tp->rmargin) |
tsz -= 1; |
tsz -= 1; |
tp->offset = (offset + rmargin > tsz) ? |
tp->offset = (offset + tp->rmargin > tsz) ? |
(offset + rmargin - tsz) / 2 : 0; |
(offset + tp->rmargin - tsz) / 2 : 0; |
} |
} |
|
|
/* Horizontal frame at the start of boxed tables. */ |
/* Horizontal frame at the start of boxed tables. */ |
Line 127 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
Line 118 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
|
|
/* |
/* |
* Now print the actual data itself depending on the span type. |
* Now print the actual data itself depending on the span type. |
* Spanner spans get a horizontal rule; data spanners have their |
* Match data cells to column numbers. |
* data printed by matching data to header. |
|
*/ |
*/ |
|
|
if (sp->pos == TBL_SPAN_DATA) { |
if (sp->pos == TBL_SPAN_DATA) { |
/* Iterate over template headers. */ |
|
cp = sp->layout->first; |
cp = sp->layout->first; |
dp = sp->first; |
dp = sp->first; |
spans = 0; |
spans = 0; |
for (hp = sp->head; hp != NULL; hp = hp->next) { |
for (ic = 0; ic < sp->opts->cols; ic++) { |
|
|
/* |
/* |
* Remeber whether we need a vertical bar |
* Remeber whether we need a vertical bar |
Line 150 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
Line 139 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
*/ |
*/ |
|
|
if (spans == 0) { |
if (spans == 0) { |
tbl_data(tp, sp->opts, dp, |
tbl_data(tp, sp->opts, dp, tp->tbl.cols + ic); |
tp->tbl.cols + hp->ident); |
|
if (dp != NULL) { |
if (dp != NULL) { |
spans = dp->spans; |
spans = dp->spans; |
dp = dp->next; |
dp = dp->next; |
Line 166 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
Line 154 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
* of spans and after the last cell. |
* of spans and after the last cell. |
*/ |
*/ |
|
|
if (hp->next == NULL || spans) |
if (ic + 1 == sp->opts->cols || spans) |
continue; |
continue; |
|
|
tbl_char(tp, ASCII_NBRSP, 1); |
tbl_char(tp, ASCII_NBRSP, 1); |
if (vert > 0) |
if (vert > 0) |
tbl_char(tp, '|', vert); |
tbl_char(tp, '|', vert); |
if (vert < 2 && hp->next != NULL) |
if (vert < 2) |
tbl_char(tp, ASCII_NBRSP, 2 - vert); |
tbl_char(tp, ASCII_NBRSP, 2 - vert); |
} |
} |
} else if (horiz) |
} else if (horiz) |
Line 193 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
Line 181 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
* existing table configuration and set it to NULL. |
* existing table configuration and set it to NULL. |
*/ |
*/ |
|
|
if (sp->flags & TBL_SPAN_LAST) { |
if (sp->next == NULL) { |
if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) { |
if (sp->opts->opts & (TBL_OPT_DBOX | TBL_OPT_BOX)) { |
tbl_hrule(tp, sp, 1); |
tbl_hrule(tp, sp, 1); |
tp->skipvsp = 1; |
tp->skipvsp = 1; |
Line 207 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
Line 195 term_tbl(struct termp *tp, const struct tbl_span *sp) |
|
tp->tbl.cols = NULL; |
tp->tbl.cols = NULL; |
tp->offset = offset; |
tp->offset = offset; |
} |
} |
|
tp->flags &= ~(TERMP_NONOSPACE | TERMP_BRNEVER); |
tp->flags &= ~TERMP_NONOSPACE; |
|
tp->rmargin = rmargin; |
|
tp->maxrmargin = maxrmargin; |
|
} |
} |
|
|
/* |
/* |
Line 236 tbl_hrule(struct termp *tp, const struct tbl_span *sp, |
|
Line 221 tbl_hrule(struct termp *tp, const struct tbl_span *sp, |
|
if (c2 == c1) |
if (c2 == c1) |
c2 = NULL; |
c2 = NULL; |
for (;;) { |
for (;;) { |
tbl_char(tp, line, tp->tbl.cols[c1->head->ident].width + 1); |
tbl_char(tp, line, tp->tbl.cols[c1->col].width + 1); |
vert = c1->vert; |
vert = c1->vert; |
if ((c1 = c1->next) == NULL) |
if ((c1 = c1->next) == NULL) |
break; |
break; |
Line 262 tbl_data(struct termp *tp, const struct tbl_opts *opts |
|
Line 247 tbl_data(struct termp *tp, const struct tbl_opts *opts |
|
const struct roffcol *col) |
const struct roffcol *col) |
{ |
{ |
|
|
if (NULL == dp) { |
if (dp == NULL) { |
tbl_char(tp, ASCII_NBRSP, col->width); |
tbl_char(tp, ASCII_NBRSP, col->width); |
return; |
return; |
} |
} |
assert(dp->layout); |
|
|
|
switch (dp->pos) { |
switch (dp->pos) { |
case TBL_DATA_NONE: |
case TBL_DATA_NONE: |
tbl_char(tp, ASCII_NBRSP, col->width); |
tbl_char(tp, ASCII_NBRSP, col->width); |
return; |
return; |
case TBL_DATA_HORIZ: |
case TBL_DATA_HORIZ: |
/* FALLTHROUGH */ |
|
case TBL_DATA_NHORIZ: |
case TBL_DATA_NHORIZ: |
tbl_char(tp, '-', col->width); |
tbl_char(tp, '-', col->width); |
return; |
return; |
case TBL_DATA_NDHORIZ: |
case TBL_DATA_NDHORIZ: |
/* FALLTHROUGH */ |
|
case TBL_DATA_DHORIZ: |
case TBL_DATA_DHORIZ: |
tbl_char(tp, '=', col->width); |
tbl_char(tp, '=', col->width); |
return; |
return; |
Line 294 tbl_data(struct termp *tp, const struct tbl_opts *opts |
|
Line 276 tbl_data(struct termp *tp, const struct tbl_opts *opts |
|
tbl_char(tp, '=', col->width); |
tbl_char(tp, '=', col->width); |
break; |
break; |
case TBL_CELL_LONG: |
case TBL_CELL_LONG: |
/* FALLTHROUGH */ |
|
case TBL_CELL_CENTRE: |
case TBL_CELL_CENTRE: |
/* FALLTHROUGH */ |
|
case TBL_CELL_LEFT: |
case TBL_CELL_LEFT: |
/* FALLTHROUGH */ |
|
case TBL_CELL_RIGHT: |
case TBL_CELL_RIGHT: |
tbl_literal(tp, dp, col); |
tbl_literal(tp, dp, col); |
break; |
break; |
Line 310 tbl_data(struct termp *tp, const struct tbl_opts *opts |
|
Line 289 tbl_data(struct termp *tp, const struct tbl_opts *opts |
|
break; |
break; |
default: |
default: |
abort(); |
abort(); |
/* NOTREACHED */ |
|
} |
} |
} |
} |
|
|
|
|
tbl_literal(struct termp *tp, const struct tbl_dat *dp, |
tbl_literal(struct termp *tp, const struct tbl_dat *dp, |
const struct roffcol *col) |
const struct roffcol *col) |
{ |
{ |
struct tbl_head *hp; |
size_t len, padl, padr, width; |
size_t width, len, padl, padr; |
int ic, spans; |
int spans; |
|
|
|
assert(dp->string); |
assert(dp->string); |
len = term_strlen(tp, dp->string); |
len = term_strlen(tp, dp->string); |
|
|
hp = dp->layout->head->next; |
|
width = col->width; |
width = col->width; |
for (spans = dp->spans; spans--; hp = hp->next) |
ic = dp->layout->col; |
width += tp->tbl.cols[hp->ident].width + 3; |
spans = dp->spans; |
|
while (spans--) |
|
width += tp->tbl.cols[++ic].width + 3; |
|
|
padr = width > len ? width - len : 0; |
padr = width > len ? width - len : 0; |
padl = 0; |
padl = 0; |
Line 396 tbl_number(struct termp *tp, const struct tbl_opts *op |
|
Line 373 tbl_number(struct termp *tp, const struct tbl_opts *op |
|
|
|
psz = term_strlen(tp, buf); |
psz = term_strlen(tp, buf); |
|
|
if (NULL != (cp = strrchr(dp->string, opts->decimal))) { |
if ((cp = strrchr(dp->string, opts->decimal)) != NULL) { |
buf[1] = '\0'; |
|
for (ssz = 0, i = 0; cp != &dp->string[i]; i++) { |
for (ssz = 0, i = 0; cp != &dp->string[i]; i++) { |
buf[0] = dp->string[i]; |
buf[0] = dp->string[i]; |
ssz += term_strlen(tp, buf); |
ssz += term_strlen(tp, buf); |
Line 421 tbl_number(struct termp *tp, const struct tbl_opts *op |
|
Line 397 tbl_number(struct termp *tp, const struct tbl_opts *op |
|
static void |
static void |
tbl_word(struct termp *tp, const struct tbl_dat *dp) |
tbl_word(struct termp *tp, const struct tbl_dat *dp) |
{ |
{ |
const void *prev_font; |
int prev_font; |
|
|
prev_font = term_fontq(tp); |
prev_font = tp->fonti; |
if (dp->layout->flags & TBL_CELL_BOLD) |
if (dp->layout->flags & TBL_CELL_BOLD) |
term_fontpush(tp, TERMFONT_BOLD); |
term_fontpush(tp, TERMFONT_BOLD); |
else if (dp->layout->flags & TBL_CELL_ITALIC) |
else if (dp->layout->flags & TBL_CELL_ITALIC) |