+ tbl_char(tp, ASCII_NBRSP, tblp->width - sz - padl);
+}
+
+static void
+tbl_calc(struct termp *tp, const struct tbl_span *sp)
+{
+ const struct tbl_dat *dp;
+ const struct tbl_head *hp;
+ struct termp_tbl *p;
+
+ /* Calculate width as the max of column cells' widths. */
+
+ hp = sp->head;
+
+ for ( ; sp; sp = sp->next) {
+ switch (sp->pos) {
+ case (TBL_DATA_HORIZ):
+ /* FALLTHROUGH */
+ case (TBL_DATA_DHORIZ):
+ continue;
+ default:
+ break;
+ }
+ for (dp = sp->first; dp; dp = dp->next) {
+ if (NULL == dp->layout)
+ continue;
+ p = &tp->tbl[dp->layout->head->ident];
+ tbl_calc_data(tp, sp->tbl, dp, p);
+ }
+ }
+
+ /* Calculate width as the simple spanner value. */
+
+ for ( ; hp; hp = hp->next)
+ switch (hp->pos) {
+ case (TBL_HEAD_VERT):
+ tp->tbl[hp->ident].width = term_len(tp, 1);
+ break;
+ case (TBL_HEAD_DVERT):
+ tp->tbl[hp->ident].width = term_len(tp, 2);
+ break;
+ default:
+ break;
+ }
+}
+
+static void
+tbl_calc_data(struct termp *tp, const struct tbl *tbl,
+ const struct tbl_dat *dp, struct termp_tbl *tblp)
+{
+
+ /* Branch down into data sub-types. */
+
+ switch (dp->layout->pos) {
+ case (TBL_CELL_HORIZ):
+ /* FALLTHROUGH */
+ case (TBL_CELL_DHORIZ):
+ tblp->width = 1;
+ break;
+ case (TBL_CELL_LONG):
+ /* FALLTHROUGH */
+ case (TBL_CELL_CENTRE):
+ /* FALLTHROUGH */
+ case (TBL_CELL_LEFT):
+ /* FALLTHROUGH */
+ case (TBL_CELL_RIGHT):
+ tbl_calc_data_literal(tp, dp, tblp);
+ break;
+ case (TBL_CELL_NUMBER):
+ tbl_calc_data_number(tp, tbl, dp, tblp);
+ break;
+ default:
+ abort();
+ /* NOTREACHED */
+ }
+}
+
+static void
+tbl_calc_data_number(struct termp *tp, const struct tbl *tbl,
+ const struct tbl_dat *dp, struct termp_tbl *tblp)
+{
+ int sz, d, psz, i, ssz;
+ char *cp, buf[2];
+
+ /*
+ * First calculate number width and decimal place (last + 1 for
+ * no-decimal numbers). If the stored decimal is subsequent
+ * ours, make our size longer by that difference
+ * (right-"shifting"); similarly, if ours is subsequent the
+ * stored, then extend the stored size by the difference.
+ * Finally, re-assign the stored values.
+ */
+
+ /* TODO: use spacing modifier. */
+
+ assert(dp->string);
+ sz = term_strlen(tp, dp->string);
+ psz = term_strlen(tp, ".");
+
+ if (NULL != (cp = strchr(dp->string, tbl->decimal))) {
+ buf[1] = '\0';
+ for (ssz = i = 0; cp != &dp->string[i]; i++) {
+ buf[0] = dp->string[i];
+ ssz += term_strlen(tp, buf);
+ }
+ d = ssz + psz;
+ } else
+ d = sz + psz;
+
+ sz += term_len(tp, 2);
+
+ if (tblp->decimal > d) {
+ sz += tblp->decimal - d;
+ d = tblp->decimal;
+ } else
+ tblp->width += d - tblp->decimal;
+
+ if (sz > tblp->width)
+ tblp->width = sz;
+ if (d > tblp->decimal)
+ tblp->decimal = d;
+}
+
+static void
+tbl_calc_data_literal(struct termp *tp,
+ const struct tbl_dat *dp,
+ struct termp_tbl *tblp)
+{
+ int sz, bufsz;
+
+ /*
+ * Calculate our width and use the spacing, with a minimum
+ * spacing dictated by position (centre, e.g,. gets a space on
+ * either side, while right/left get a single adjacent space).
+ */
+
+ assert(dp->string);
+ sz = term_strlen(tp, dp->string);
+
+ switch (dp->layout->pos) {
+ case (TBL_CELL_LONG):
+ /* FALLTHROUGH */
+ case (TBL_CELL_CENTRE):
+ bufsz = 2;
+ break;
+ default:
+ bufsz = 1;
+ break;
+ }
+
+ if (dp->layout->spacing)
+ bufsz = bufsz > dp->layout->spacing ?
+ bufsz : dp->layout->spacing;
+
+ sz += bufsz;
+ if (tblp->width < sz)
+ tblp->width = sz;