1*60395358Sschwarze /* $OpenBSD: out.c,v 1.58 2025/01/05 18:03:51 schwarze Exp $ */ 24175bdabSschwarze /* 3ec04407bSschwarze * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4e968d739Sschwarze * Copyright (c) 2011, 2014, 2015, 2017, 2018, 2019, 2021 5e968d739Sschwarze * Ingo Schwarze <schwarze@openbsd.org> 64175bdabSschwarze * 74175bdabSschwarze * Permission to use, copy, modify, and distribute this software for any 84175bdabSschwarze * purpose with or without fee is hereby granted, provided that the above 94175bdabSschwarze * copyright notice and this permission notice appear in all copies. 104175bdabSschwarze * 114175bdabSschwarze * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 124175bdabSschwarze * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 134175bdabSschwarze * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 144175bdabSschwarze * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 154175bdabSschwarze * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 164175bdabSschwarze * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 174175bdabSschwarze * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 184175bdabSschwarze */ 194175bdabSschwarze #include <sys/types.h> 204175bdabSschwarze 21b822ca0dSschwarze #include <assert.h> 22a7d5d23dSschwarze #include <ctype.h> 23e8a65004Sschwarze #include <stdint.h> 247d063611Sschwarze #include <stdio.h> 254175bdabSschwarze #include <stdlib.h> 26b822ca0dSschwarze #include <string.h> 27b822ca0dSschwarze #include <time.h> 284175bdabSschwarze 294f4f7972Sschwarze #include "mandoc_aux.h" 307d063611Sschwarze #include "mandoc.h" 31fae2491eSschwarze #include "tbl.h" 324175bdabSschwarze #include "out.h" 334175bdabSschwarze 349f0607b7Sschwarze struct tbl_colgroup { 359f0607b7Sschwarze struct tbl_colgroup *next; 369f0607b7Sschwarze size_t wanted; 379f0607b7Sschwarze int startcol; 389f0607b7Sschwarze int endcol; 399f0607b7Sschwarze }; 409f0607b7Sschwarze 419f0607b7Sschwarze static size_t tblcalc_data(struct rofftbl *, struct roffcol *, 422c3e66c4Sschwarze const struct tbl_opts *, const struct tbl_dat *, 432c3e66c4Sschwarze size_t); 449f0607b7Sschwarze static size_t tblcalc_literal(struct rofftbl *, struct roffcol *, 452c3e66c4Sschwarze const struct tbl_dat *, size_t); 469f0607b7Sschwarze static size_t tblcalc_number(struct rofftbl *, struct roffcol *, 47c7497e73Sschwarze const struct tbl_opts *, const struct tbl_dat *); 48ec04407bSschwarze 4949aff9f8Sschwarze 504175bdabSschwarze /* 51a09d548bSschwarze * Parse the *src string and store a scaling unit into *dst. 52a09d548bSschwarze * If the string doesn't specify the unit, use the default. 53a09d548bSschwarze * If no default is specified, fail. 54ecd22486Sschwarze * Return a pointer to the byte after the last byte used, 55ecd22486Sschwarze * or NULL on total failure. 564175bdabSschwarze */ 57ecd22486Sschwarze const char * 584175bdabSschwarze a2roffsu(const char *src, struct roffsu *dst, enum roffscale def) 594175bdabSschwarze { 60a09d548bSschwarze char *endptr; 614175bdabSschwarze 6284f230baSschwarze dst->unit = def == SCALE_MAX ? SCALE_BU : def; 6384f230baSschwarze dst->scale = strtod(src, &endptr); 6484f230baSschwarze if (endptr == src) 65ecd22486Sschwarze return NULL; 664175bdabSschwarze 6784f230baSschwarze switch (*endptr++) { 6849aff9f8Sschwarze case 'c': 6984f230baSschwarze dst->unit = SCALE_CM; 704175bdabSschwarze break; 7149aff9f8Sschwarze case 'i': 7284f230baSschwarze dst->unit = SCALE_IN; 734175bdabSschwarze break; 7449aff9f8Sschwarze case 'f': 7584f230baSschwarze dst->unit = SCALE_FS; 764175bdabSschwarze break; 7749aff9f8Sschwarze case 'M': 7884f230baSschwarze dst->unit = SCALE_MM; 7984f230baSschwarze break; 8084f230baSschwarze case 'm': 8184f230baSschwarze dst->unit = SCALE_EM; 824175bdabSschwarze break; 8349aff9f8Sschwarze case 'n': 8484f230baSschwarze dst->unit = SCALE_EN; 854175bdabSschwarze break; 8684f230baSschwarze case 'P': 8784f230baSschwarze dst->unit = SCALE_PC; 8884f230baSschwarze break; 8984f230baSschwarze case 'p': 9084f230baSschwarze dst->unit = SCALE_PT; 9184f230baSschwarze break; 9284f230baSschwarze case 'u': 9384f230baSschwarze dst->unit = SCALE_BU; 9484f230baSschwarze break; 9584f230baSschwarze case 'v': 9684f230baSschwarze dst->unit = SCALE_VS; 9784f230baSschwarze break; 984175bdabSschwarze default: 9923b9ae24Sschwarze endptr--; 10084f230baSschwarze if (SCALE_MAX == def) 101ecd22486Sschwarze return NULL; 10284f230baSschwarze dst->unit = def; 10384f230baSschwarze break; 1044175bdabSschwarze } 105ecd22486Sschwarze return endptr; 1064175bdabSschwarze } 107b822ca0dSschwarze 108ec04407bSschwarze /* 109ec04407bSschwarze * Calculate the abstract widths and decimal positions of columns in a 110ec04407bSschwarze * table. This routine allocates the columns structures then runs over 111ec04407bSschwarze * all rows and cells in the table. The function pointers in "tbl" are 112ec04407bSschwarze * used for the actual width calculations. 113ec04407bSschwarze */ 114ec04407bSschwarze void 1159f0607b7Sschwarze tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first, 116d1c3ec49Sschwarze size_t offset, size_t rmargin) 117ec04407bSschwarze { 11899d91ba7Sschwarze const struct tbl_opts *opts; 1199f0607b7Sschwarze const struct tbl_span *sp; 120ec04407bSschwarze const struct tbl_dat *dp; 121ec04407bSschwarze struct roffcol *col; 1229f0607b7Sschwarze struct tbl_colgroup *first_group, **gp, *g; 1237d11b6e3Sschwarze size_t *colwidth; 1249f0607b7Sschwarze size_t ewidth, min1, min2, wanted, width, xwidth; 1259f0607b7Sschwarze int done, icol, maxcol, necol, nxcol, quirkcol; 126ec04407bSschwarze 127ec04407bSschwarze /* 128ec04407bSschwarze * Allocate the master column specifiers. These will hold the 129ec04407bSschwarze * widths and decimal positions for all cells in the column. It 130ec04407bSschwarze * must be freed and nullified by the caller. 131ec04407bSschwarze */ 132ec04407bSschwarze 1339f0607b7Sschwarze assert(tbl->cols == NULL); 1349f0607b7Sschwarze tbl->cols = mandoc_calloc((size_t)sp_first->opts->cols, 13549aff9f8Sschwarze sizeof(struct roffcol)); 1369f0607b7Sschwarze opts = sp_first->opts; 137ec04407bSschwarze 1389f0607b7Sschwarze maxcol = -1; 1399f0607b7Sschwarze first_group = NULL; 1409f0607b7Sschwarze for (sp = sp_first; sp != NULL; sp = sp->next) { 1419f0607b7Sschwarze if (sp->pos != TBL_SPAN_DATA) 142ec04407bSschwarze continue; 1439f0607b7Sschwarze 144ec04407bSschwarze /* 145ec04407bSschwarze * Account for the data cells in the layout, matching it 146ec04407bSschwarze * to data cells in the data section. 147ec04407bSschwarze */ 1489f0607b7Sschwarze 1499f0607b7Sschwarze for (dp = sp->first; dp != NULL; dp = dp->next) { 1505f6d1ba3Sschwarze icol = dp->layout->col; 151f6d3ad1cSschwarze while (maxcol < icol + dp->hspans) 152e8a65004Sschwarze tbl->cols[++maxcol].spacing = SIZE_MAX; 15346fa2066Sschwarze col = tbl->cols + icol; 15446fa2066Sschwarze col->flags |= dp->layout->flags; 15546fa2066Sschwarze if (dp->layout->flags & TBL_CELL_WIGN) 15646fa2066Sschwarze continue; 1579f0607b7Sschwarze 1589f0607b7Sschwarze /* Handle explicit width specifications. */ 1592c3e66c4Sschwarze if (col->width < dp->layout->width) 1602c3e66c4Sschwarze col->width = dp->layout->width; 161e8a65004Sschwarze if (dp->layout->spacing != SIZE_MAX && 162e8a65004Sschwarze (col->spacing == SIZE_MAX || 163e8a65004Sschwarze col->spacing < dp->layout->spacing)) 164e8a65004Sschwarze col->spacing = dp->layout->spacing; 1659f0607b7Sschwarze 1669f0607b7Sschwarze /* 1679f0607b7Sschwarze * Calculate an automatic width. 1689f0607b7Sschwarze * Except for spanning cells, apply it. 1699f0607b7Sschwarze */ 1709f0607b7Sschwarze 1719f0607b7Sschwarze width = tblcalc_data(tbl, 1729f0607b7Sschwarze dp->hspans == 0 ? col : NULL, 1739f0607b7Sschwarze opts, dp, 174238123d0Sschwarze dp->block == 0 ? 0 : 175238123d0Sschwarze dp->layout->width ? dp->layout->width : 176e6d4e954Sschwarze rmargin ? (rmargin + sp->opts->cols / 2) 177e6d4e954Sschwarze / (sp->opts->cols + 1) : 0); 1789f0607b7Sschwarze if (dp->hspans == 0) 1799f0607b7Sschwarze continue; 1809f0607b7Sschwarze 1819f0607b7Sschwarze /* 1821d61b63dSschwarze * Build a singly linked list 1839f0607b7Sschwarze * of all groups of columns joined by spans, 1849f0607b7Sschwarze * recording the minimum width for each group. 1859f0607b7Sschwarze */ 1869f0607b7Sschwarze 1871d61b63dSschwarze gp = &first_group; 1881d61b63dSschwarze while (*gp != NULL && ((*gp)->startcol != icol || 1891d61b63dSschwarze (*gp)->endcol != icol + dp->hspans)) 1909f0607b7Sschwarze gp = &(*gp)->next; 1911d61b63dSschwarze if (*gp == NULL) { 1929f0607b7Sschwarze g = mandoc_malloc(sizeof(*g)); 1939f0607b7Sschwarze g->next = *gp; 1949f0607b7Sschwarze g->wanted = width; 1959f0607b7Sschwarze g->startcol = icol; 1969f0607b7Sschwarze g->endcol = icol + dp->hspans; 1979f0607b7Sschwarze *gp = g; 1989f0607b7Sschwarze } else if ((*gp)->wanted < width) 1999f0607b7Sschwarze (*gp)->wanted = width; 200ec04407bSschwarze } 201ec04407bSschwarze } 20246fa2066Sschwarze 20346fa2066Sschwarze /* 204ea3fff40Sschwarze * The minimum width of columns explicitly specified 205ea3fff40Sschwarze * in the layout is 1n. 2069f0607b7Sschwarze */ 2079f0607b7Sschwarze 208ea3fff40Sschwarze if (maxcol < sp_first->opts->cols - 1) 209ea3fff40Sschwarze maxcol = sp_first->opts->cols - 1; 210ea3fff40Sschwarze for (icol = 0; icol <= maxcol; icol++) { 211ea3fff40Sschwarze col = tbl->cols + icol; 212ea3fff40Sschwarze if (col->width < 1) 213ea3fff40Sschwarze col->width = 1; 214ea3fff40Sschwarze 215ea3fff40Sschwarze /* 216ea3fff40Sschwarze * Column spacings are needed for span width 217ea3fff40Sschwarze * calculations, so set the default values now. 218ea3fff40Sschwarze */ 219ea3fff40Sschwarze 220ea3fff40Sschwarze if (col->spacing == SIZE_MAX || icol == maxcol) 221ea3fff40Sschwarze col->spacing = 3; 222ea3fff40Sschwarze } 2239f0607b7Sschwarze 2249f0607b7Sschwarze /* 2259f0607b7Sschwarze * Replace the minimum widths with the missing widths, 2269f0607b7Sschwarze * and dismiss groups that are already wide enough. 2279f0607b7Sschwarze */ 2289f0607b7Sschwarze 2299f0607b7Sschwarze gp = &first_group; 2309f0607b7Sschwarze while ((g = *gp) != NULL) { 2319f0607b7Sschwarze done = 0; 2329f0607b7Sschwarze for (icol = g->startcol; icol <= g->endcol; icol++) { 2339f0607b7Sschwarze width = tbl->cols[icol].width; 2349f0607b7Sschwarze if (icol < g->endcol) 2359f0607b7Sschwarze width += tbl->cols[icol].spacing; 2369f0607b7Sschwarze if (g->wanted <= width) { 2379f0607b7Sschwarze done = 1; 2389f0607b7Sschwarze break; 2399f0607b7Sschwarze } else 2401fddd665Sschwarze g->wanted -= width; 2419f0607b7Sschwarze } 2429f0607b7Sschwarze if (done) { 2439f0607b7Sschwarze *gp = g->next; 2449f0607b7Sschwarze free(g); 2459f0607b7Sschwarze } else 2461fddd665Sschwarze gp = &g->next; 2479f0607b7Sschwarze } 2489f0607b7Sschwarze 2497d11b6e3Sschwarze colwidth = mandoc_reallocarray(NULL, maxcol + 1, sizeof(*colwidth)); 2509f0607b7Sschwarze while (first_group != NULL) { 2519f0607b7Sschwarze 2529f0607b7Sschwarze /* 2537d11b6e3Sschwarze * Rebuild the array of the widths of all columns 2547d11b6e3Sschwarze * participating in spans that require expansion. 2557d11b6e3Sschwarze */ 2567d11b6e3Sschwarze 2577d11b6e3Sschwarze for (icol = 0; icol <= maxcol; icol++) 2587d11b6e3Sschwarze colwidth[icol] = SIZE_MAX; 2597d11b6e3Sschwarze for (g = first_group; g != NULL; g = g->next) 2607d11b6e3Sschwarze for (icol = g->startcol; icol <= g->endcol; icol++) 2617d11b6e3Sschwarze colwidth[icol] = tbl->cols[icol].width; 2627d11b6e3Sschwarze 2637d11b6e3Sschwarze /* 2649f0607b7Sschwarze * Find the smallest and second smallest column width 2659f0607b7Sschwarze * among the columns which may need expamsion. 2669f0607b7Sschwarze */ 2679f0607b7Sschwarze 2689f0607b7Sschwarze min1 = min2 = SIZE_MAX; 2699f0607b7Sschwarze for (icol = 0; icol <= maxcol; icol++) { 2707d11b6e3Sschwarze width = colwidth[icol]; 27156155c97Sschwarze if (min1 > width) { 2729f0607b7Sschwarze min2 = min1; 27356155c97Sschwarze min1 = width; 27456155c97Sschwarze } else if (min1 < width && min2 > width) 27556155c97Sschwarze min2 = width; 2769f0607b7Sschwarze } 2779f0607b7Sschwarze 2789f0607b7Sschwarze /* 2799f0607b7Sschwarze * Find the minimum wanted width 2809f0607b7Sschwarze * for any one of the narrowest columns, 2819f0607b7Sschwarze * and mark the columns wanting that width. 2829f0607b7Sschwarze */ 2839f0607b7Sschwarze 2849f0607b7Sschwarze wanted = min2; 2859f0607b7Sschwarze for (g = first_group; g != NULL; g = g->next) { 2869f0607b7Sschwarze necol = 0; 2879f0607b7Sschwarze for (icol = g->startcol; icol <= g->endcol; icol++) 2887d11b6e3Sschwarze if (colwidth[icol] == min1) 2899f0607b7Sschwarze necol++; 2909f0607b7Sschwarze if (necol == 0) 2919f0607b7Sschwarze continue; 2929f0607b7Sschwarze width = min1 + (g->wanted - 1) / necol + 1; 2939f0607b7Sschwarze if (width > min2) 2949f0607b7Sschwarze width = min2; 2959f0607b7Sschwarze if (wanted > width) 2969f0607b7Sschwarze wanted = width; 2979f0607b7Sschwarze } 2989f0607b7Sschwarze 29956155c97Sschwarze /* Record the effect of the widening. */ 3009f0607b7Sschwarze 3019f0607b7Sschwarze gp = &first_group; 3029f0607b7Sschwarze while ((g = *gp) != NULL) { 3039f0607b7Sschwarze done = 0; 3049f0607b7Sschwarze for (icol = g->startcol; icol <= g->endcol; icol++) { 3057d11b6e3Sschwarze if (colwidth[icol] != min1) 3069f0607b7Sschwarze continue; 3079f0607b7Sschwarze if (g->wanted <= wanted - min1) { 30856155c97Sschwarze tbl->cols[icol].width += g->wanted; 3099f0607b7Sschwarze done = 1; 3109f0607b7Sschwarze break; 3119f0607b7Sschwarze } 31256155c97Sschwarze tbl->cols[icol].width = wanted; 3139f0607b7Sschwarze g->wanted -= wanted - min1; 3149f0607b7Sschwarze } 3159f0607b7Sschwarze if (done) { 3169f0607b7Sschwarze *gp = g->next; 3179f0607b7Sschwarze free(g); 3189f0607b7Sschwarze } else 3191fddd665Sschwarze gp = &g->next; 3209f0607b7Sschwarze } 3219f0607b7Sschwarze } 3227d11b6e3Sschwarze free(colwidth); 3239f0607b7Sschwarze 3249f0607b7Sschwarze /* 3253a412390Sschwarze * Align numbers with text. 32646fa2066Sschwarze * Count columns to equalize and columns to maximize. 32746fa2066Sschwarze * Find maximum width of the columns to equalize. 32846fa2066Sschwarze * Find total width of the columns *not* to maximize. 32946fa2066Sschwarze */ 33046fa2066Sschwarze 33146fa2066Sschwarze necol = nxcol = 0; 33246fa2066Sschwarze ewidth = xwidth = 0; 33346fa2066Sschwarze for (icol = 0; icol <= maxcol; icol++) { 33446fa2066Sschwarze col = tbl->cols + icol; 3353a412390Sschwarze if (col->width > col->nwidth) 3363a412390Sschwarze col->decimal += (col->width - col->nwidth) / 2; 33746fa2066Sschwarze if (col->flags & TBL_CELL_EQUAL) { 33846fa2066Sschwarze necol++; 33946fa2066Sschwarze if (ewidth < col->width) 34046fa2066Sschwarze ewidth = col->width; 34146fa2066Sschwarze } 34246fa2066Sschwarze if (col->flags & TBL_CELL_WMAX) 34346fa2066Sschwarze nxcol++; 34446fa2066Sschwarze else 34546fa2066Sschwarze xwidth += col->width; 34646fa2066Sschwarze } 34746fa2066Sschwarze 34846fa2066Sschwarze /* 34946fa2066Sschwarze * Equalize columns, if requested for any of them. 35046fa2066Sschwarze * Update total width of the columns not to maximize. 35146fa2066Sschwarze */ 35246fa2066Sschwarze 35346fa2066Sschwarze if (necol) { 35446fa2066Sschwarze for (icol = 0; icol <= maxcol; icol++) { 35546fa2066Sschwarze col = tbl->cols + icol; 35646fa2066Sschwarze if ( ! (col->flags & TBL_CELL_EQUAL)) 35746fa2066Sschwarze continue; 35846fa2066Sschwarze if (col->width == ewidth) 35946fa2066Sschwarze continue; 360d1c3ec49Sschwarze if (nxcol && rmargin) 36146fa2066Sschwarze xwidth += ewidth - col->width; 36246fa2066Sschwarze col->width = ewidth; 36346fa2066Sschwarze } 36446fa2066Sschwarze } 36546fa2066Sschwarze 36646fa2066Sschwarze /* 36746fa2066Sschwarze * If there are any columns to maximize, find the total 36846fa2066Sschwarze * available width, deducting 3n margins between columns. 36946fa2066Sschwarze * Distribute the available width evenly. 37046fa2066Sschwarze */ 37146fa2066Sschwarze 372d1c3ec49Sschwarze if (nxcol && rmargin) { 373eb274914Sschwarze xwidth += 3*maxcol + 37499d91ba7Sschwarze (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ? 37599d91ba7Sschwarze 2 : !!opts->lvert + !!opts->rvert); 376d1c3ec49Sschwarze if (rmargin <= offset + xwidth) 377eb274914Sschwarze return; 378d1c3ec49Sschwarze xwidth = rmargin - offset - xwidth; 37999d91ba7Sschwarze 38099d91ba7Sschwarze /* 38199d91ba7Sschwarze * Emulate a bug in GNU tbl width calculation that 38299d91ba7Sschwarze * manifests itself for large numbers of x-columns. 38399d91ba7Sschwarze * Emulating it for 5 x-columns gives identical 38499d91ba7Sschwarze * behaviour for up to 6 x-columns. 38599d91ba7Sschwarze */ 38699d91ba7Sschwarze 38799d91ba7Sschwarze if (nxcol == 5) { 38899d91ba7Sschwarze quirkcol = xwidth % nxcol + 2; 38999d91ba7Sschwarze if (quirkcol != 3 && quirkcol != 4) 39099d91ba7Sschwarze quirkcol = -1; 39199d91ba7Sschwarze } else 39299d91ba7Sschwarze quirkcol = -1; 39399d91ba7Sschwarze 39499d91ba7Sschwarze necol = 0; 39599d91ba7Sschwarze ewidth = 0; 39646fa2066Sschwarze for (icol = 0; icol <= maxcol; icol++) { 39746fa2066Sschwarze col = tbl->cols + icol; 39846fa2066Sschwarze if ( ! (col->flags & TBL_CELL_WMAX)) 39946fa2066Sschwarze continue; 40099d91ba7Sschwarze col->width = (double)xwidth * ++necol / nxcol 40199d91ba7Sschwarze - ewidth + 0.4995; 40299d91ba7Sschwarze if (necol == quirkcol) 40399d91ba7Sschwarze col->width--; 40499d91ba7Sschwarze ewidth += col->width; 40546fa2066Sschwarze } 40646fa2066Sschwarze } 407ec04407bSschwarze } 408ec04407bSschwarze 4099f0607b7Sschwarze static size_t 410ec04407bSschwarze tblcalc_data(struct rofftbl *tbl, struct roffcol *col, 4112c3e66c4Sschwarze const struct tbl_opts *opts, const struct tbl_dat *dp, size_t mw) 412ec04407bSschwarze { 413ec04407bSschwarze size_t sz; 414ec04407bSschwarze 415ec04407bSschwarze /* Branch down into data sub-types. */ 416ec04407bSschwarze 417ec04407bSschwarze switch (dp->layout->pos) { 41849aff9f8Sschwarze case TBL_CELL_HORIZ: 41949aff9f8Sschwarze case TBL_CELL_DHORIZ: 420ec04407bSschwarze sz = (*tbl->len)(1, tbl->arg); 4219f0607b7Sschwarze if (col != NULL && col->width < sz) 422ec04407bSschwarze col->width = sz; 4239f0607b7Sschwarze return sz; 42449aff9f8Sschwarze case TBL_CELL_LONG: 42549aff9f8Sschwarze case TBL_CELL_CENTRE: 42649aff9f8Sschwarze case TBL_CELL_LEFT: 42749aff9f8Sschwarze case TBL_CELL_RIGHT: 4289f0607b7Sschwarze return tblcalc_literal(tbl, col, dp, mw); 42949aff9f8Sschwarze case TBL_CELL_NUMBER: 4309f0607b7Sschwarze return tblcalc_number(tbl, col, opts, dp); 43149aff9f8Sschwarze case TBL_CELL_DOWN: 4329f0607b7Sschwarze return 0; 433ec04407bSschwarze default: 434ec04407bSschwarze abort(); 435ec04407bSschwarze } 436ec04407bSschwarze } 437ec04407bSschwarze 4389f0607b7Sschwarze static size_t 439ec04407bSschwarze tblcalc_literal(struct rofftbl *tbl, struct roffcol *col, 4402c3e66c4Sschwarze const struct tbl_dat *dp, size_t mw) 441ec04407bSschwarze { 4422c3e66c4Sschwarze const char *str; /* Beginning of the first line. */ 4432c3e66c4Sschwarze const char *beg; /* Beginning of the current line. */ 4442c3e66c4Sschwarze char *end; /* End of the current line. */ 445d1c3ec49Sschwarze size_t lsz; /* Length of the current line. */ 446d1c3ec49Sschwarze size_t wsz; /* Length of the current word. */ 4479f0607b7Sschwarze size_t msz; /* Length of the longest line. */ 448ec04407bSschwarze 4492c3e66c4Sschwarze if (dp->string == NULL || *dp->string == '\0') 4509f0607b7Sschwarze return 0; 4512c3e66c4Sschwarze str = mw ? mandoc_strdup(dp->string) : dp->string; 4529f0607b7Sschwarze msz = lsz = 0; 4532c3e66c4Sschwarze for (beg = str; beg != NULL && *beg != '\0'; beg = end) { 4542c3e66c4Sschwarze end = mw ? strchr(beg, ' ') : NULL; 4552c3e66c4Sschwarze if (end != NULL) { 4562c3e66c4Sschwarze *end++ = '\0'; 4572c3e66c4Sschwarze while (*end == ' ') 4582c3e66c4Sschwarze end++; 4592c3e66c4Sschwarze } 460d1c3ec49Sschwarze wsz = (*tbl->slen)(beg, tbl->arg); 461d1c3ec49Sschwarze if (mw && lsz && lsz + 1 + wsz <= mw) 462d1c3ec49Sschwarze lsz += 1 + wsz; 463d1c3ec49Sschwarze else 464d1c3ec49Sschwarze lsz = wsz; 4659f0607b7Sschwarze if (msz < lsz) 4669f0607b7Sschwarze msz = lsz; 467ec04407bSschwarze } 4682c3e66c4Sschwarze if (mw) 4692c3e66c4Sschwarze free((void *)str); 4709f0607b7Sschwarze if (col != NULL && col->width < msz) 4719f0607b7Sschwarze col->width = msz; 4729f0607b7Sschwarze return msz; 4732c3e66c4Sschwarze } 474ec04407bSschwarze 4759f0607b7Sschwarze static size_t 476ec04407bSschwarze tblcalc_number(struct rofftbl *tbl, struct roffcol *col, 477c7497e73Sschwarze const struct tbl_opts *opts, const struct tbl_dat *dp) 478ec04407bSschwarze { 479a7d5d23dSschwarze const char *cp, *lastdigit, *lastpoint; 480a7d5d23dSschwarze size_t intsz, totsz; 481ec04407bSschwarze char buf[2]; 482ec04407bSschwarze 483a7d5d23dSschwarze if (dp->string == NULL || *dp->string == '\0') 4849f0607b7Sschwarze return 0; 4859f0607b7Sschwarze 4869f0607b7Sschwarze totsz = (*tbl->slen)(dp->string, tbl->arg); 4879f0607b7Sschwarze if (col == NULL) 4889f0607b7Sschwarze return totsz; 489a7d5d23dSschwarze 490ec04407bSschwarze /* 491a7d5d23dSschwarze * Find the last digit and 492a7d5d23dSschwarze * the last decimal point that is adjacent to a digit. 493a7d5d23dSschwarze * The alignment indicator "\&" overrides everything. 494ec04407bSschwarze */ 495ec04407bSschwarze 496a7d5d23dSschwarze lastdigit = lastpoint = NULL; 497a7d5d23dSschwarze for (cp = dp->string; cp[0] != '\0'; cp++) { 498a7d5d23dSschwarze if (cp[0] == '\\' && cp[1] == '&') { 499a7d5d23dSschwarze lastdigit = lastpoint = cp; 500a7d5d23dSschwarze break; 501a7d5d23dSschwarze } else if (cp[0] == opts->decimal && 502a7d5d23dSschwarze (isdigit((unsigned char)cp[1]) || 503a7d5d23dSschwarze (cp > dp->string && isdigit((unsigned char)cp[-1])))) 504a7d5d23dSschwarze lastpoint = cp; 505a7d5d23dSschwarze else if (isdigit((unsigned char)cp[0])) 506a7d5d23dSschwarze lastdigit = cp; 507ec04407bSschwarze } 508a7d5d23dSschwarze 509a7d5d23dSschwarze /* Not a number, treat as a literal string. */ 510a7d5d23dSschwarze 511a7d5d23dSschwarze if (lastdigit == NULL) { 5129f0607b7Sschwarze if (col != NULL && col->width < totsz) 513a7d5d23dSschwarze col->width = totsz; 5149f0607b7Sschwarze return totsz; 515a7d5d23dSschwarze } 516a7d5d23dSschwarze 517a7d5d23dSschwarze /* Measure the width of the integer part. */ 518a7d5d23dSschwarze 519a7d5d23dSschwarze if (lastpoint == NULL) 520a7d5d23dSschwarze lastpoint = lastdigit + 1; 521a7d5d23dSschwarze intsz = 0; 522a7d5d23dSschwarze buf[1] = '\0'; 523a7d5d23dSschwarze for (cp = dp->string; cp < lastpoint; cp++) { 524a7d5d23dSschwarze buf[0] = cp[0]; 525a7d5d23dSschwarze intsz += (*tbl->slen)(buf, tbl->arg); 526a7d5d23dSschwarze } 527a7d5d23dSschwarze 528a7d5d23dSschwarze /* 529a7d5d23dSschwarze * If this number has more integer digits than all numbers 530a7d5d23dSschwarze * seen on earlier lines, shift them all to the right. 531a7d5d23dSschwarze * If it has fewer, shift this number to the right. 532a7d5d23dSschwarze */ 533a7d5d23dSschwarze 534a7d5d23dSschwarze if (intsz > col->decimal) { 535a7d5d23dSschwarze col->nwidth += intsz - col->decimal; 536a7d5d23dSschwarze col->decimal = intsz; 537ec04407bSschwarze } else 538a7d5d23dSschwarze totsz += col->decimal - intsz; 539ec04407bSschwarze 540a7d5d23dSschwarze /* Update the maximum total width seen so far. */ 541ec04407bSschwarze 542a7d5d23dSschwarze if (totsz > col->nwidth) 543a7d5d23dSschwarze col->nwidth = totsz; 544e968d739Sschwarze if (col->nwidth > col->width) 545e968d739Sschwarze col->width = col->nwidth; 5469f0607b7Sschwarze return totsz; 547ec04407bSschwarze } 548