+ struct roffsu su;
+ const struct tbl_opts *opts;
+ const struct tbl_span *sp;
+ const struct tbl_dat *dp;
+ struct roffcol *col;
+ struct tbl_colgroup *first_group, **gp, *g;
+ size_t *colwidth;
+ size_t ewidth, min1, min2, wanted, width, xwidth;
+ int done, icol, maxcol, necol, nxcol, quirkcol;
+
+ /*
+ * Allocate the master column specifiers. These will hold the
+ * widths and decimal positions for all cells in the column. It
+ * must be freed and nullified by the caller.
+ */
+
+ assert(tbl->cols == NULL);
+ tbl->cols = mandoc_calloc((size_t)sp_first->opts->cols,
+ sizeof(struct roffcol));
+ opts = sp_first->opts;
+
+ maxcol = -1;
+ first_group = NULL;
+ for (sp = sp_first; sp != NULL; sp = sp->next) {
+ if (sp->pos != TBL_SPAN_DATA)
+ continue;
+
+ /*
+ * Account for the data cells in the layout, matching it
+ * to data cells in the data section.
+ */
+
+ for (dp = sp->first; dp != NULL; dp = dp->next) {
+ icol = dp->layout->col;
+ while (maxcol < icol + dp->hspans)
+ tbl->cols[++maxcol].spacing = SIZE_MAX;
+ col = tbl->cols + icol;
+ col->flags |= dp->layout->flags;
+ if (dp->layout->flags & TBL_CELL_WIGN)
+ continue;
+
+ /* Handle explicit width specifications. */
+
+ if (dp->layout->wstr != NULL &&
+ dp->layout->width == 0 &&
+ a2roffsu(dp->layout->wstr, &su, SCALE_EN)
+ != NULL)
+ dp->layout->width =
+ (*tbl->sulen)(&su, tbl->arg);
+ if (col->width < dp->layout->width)
+ col->width = dp->layout->width;
+ if (dp->layout->spacing != SIZE_MAX &&
+ (col->spacing == SIZE_MAX ||
+ col->spacing < dp->layout->spacing))
+ col->spacing = dp->layout->spacing;
+
+ /*
+ * Calculate an automatic width.
+ * Except for spanning cells, apply it.
+ */
+
+ width = tblcalc_data(tbl,
+ dp->hspans == 0 ? col : NULL,
+ opts, dp,
+ dp->block == 0 ? 0 :
+ dp->layout->width ? dp->layout->width :
+ rmargin ? (rmargin + sp->opts->cols / 2)
+ / (sp->opts->cols + 1) : 0);
+ if (dp->hspans == 0)
+ continue;
+
+ /*
+ * Build a singly linked list
+ * of all groups of columns joined by spans,
+ * recording the minimum width for each group.
+ */
+
+ gp = &first_group;
+ while (*gp != NULL && ((*gp)->startcol != icol ||
+ (*gp)->endcol != icol + dp->hspans))
+ gp = &(*gp)->next;
+ if (*gp == NULL) {
+ g = mandoc_malloc(sizeof(*g));
+ g->next = *gp;
+ g->wanted = width;
+ g->startcol = icol;
+ g->endcol = icol + dp->hspans;
+ *gp = g;
+ } else if ((*gp)->wanted < width)
+ (*gp)->wanted = width;
+ }
+ }