]> git.cameronkatri.com Git - mandoc.git/blob - out.c
In the fallback code to look for manual pages without using mandoc.db(5),
[mandoc.git] / out.c
1 /* $Id: out.c,v 1.80 2021/08/10 12:55:03 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011,2014,2015,2017,2018 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "mandoc_aux.h"
31 #include "mandoc.h"
32 #include "tbl.h"
33 #include "out.h"
34
35 struct tbl_colgroup {
36 struct tbl_colgroup *next;
37 size_t wanted;
38 int startcol;
39 int endcol;
40 };
41
42 static size_t tblcalc_data(struct rofftbl *, struct roffcol *,
43 const struct tbl_opts *, const struct tbl_dat *,
44 size_t);
45 static size_t tblcalc_literal(struct rofftbl *, struct roffcol *,
46 const struct tbl_dat *, size_t);
47 static size_t tblcalc_number(struct rofftbl *, struct roffcol *,
48 const struct tbl_opts *, const struct tbl_dat *);
49
50
51 /*
52 * Parse the *src string and store a scaling unit into *dst.
53 * If the string doesn't specify the unit, use the default.
54 * If no default is specified, fail.
55 * Return a pointer to the byte after the last byte used,
56 * or NULL on total failure.
57 */
58 const char *
59 a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
60 {
61 char *endptr;
62
63 dst->unit = def == SCALE_MAX ? SCALE_BU : def;
64 dst->scale = strtod(src, &endptr);
65 if (endptr == src)
66 return NULL;
67
68 switch (*endptr++) {
69 case 'c':
70 dst->unit = SCALE_CM;
71 break;
72 case 'i':
73 dst->unit = SCALE_IN;
74 break;
75 case 'f':
76 dst->unit = SCALE_FS;
77 break;
78 case 'M':
79 dst->unit = SCALE_MM;
80 break;
81 case 'm':
82 dst->unit = SCALE_EM;
83 break;
84 case 'n':
85 dst->unit = SCALE_EN;
86 break;
87 case 'P':
88 dst->unit = SCALE_PC;
89 break;
90 case 'p':
91 dst->unit = SCALE_PT;
92 break;
93 case 'u':
94 dst->unit = SCALE_BU;
95 break;
96 case 'v':
97 dst->unit = SCALE_VS;
98 break;
99 default:
100 endptr--;
101 if (SCALE_MAX == def)
102 return NULL;
103 dst->unit = def;
104 break;
105 }
106 return endptr;
107 }
108
109 /*
110 * Calculate the abstract widths and decimal positions of columns in a
111 * table. This routine allocates the columns structures then runs over
112 * all rows and cells in the table. The function pointers in "tbl" are
113 * used for the actual width calculations.
114 */
115 void
116 tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first,
117 size_t offset, size_t rmargin)
118 {
119 struct roffsu su;
120 const struct tbl_opts *opts;
121 const struct tbl_span *sp;
122 const struct tbl_dat *dp;
123 struct roffcol *col;
124 struct tbl_colgroup *first_group, **gp, *g;
125 size_t *colwidth;
126 size_t ewidth, min1, min2, wanted, width, xwidth;
127 int done, icol, maxcol, necol, nxcol, quirkcol;
128
129 /*
130 * Allocate the master column specifiers. These will hold the
131 * widths and decimal positions for all cells in the column. It
132 * must be freed and nullified by the caller.
133 */
134
135 assert(tbl->cols == NULL);
136 tbl->cols = mandoc_calloc((size_t)sp_first->opts->cols,
137 sizeof(struct roffcol));
138 opts = sp_first->opts;
139
140 maxcol = -1;
141 first_group = NULL;
142 for (sp = sp_first; sp != NULL; sp = sp->next) {
143 if (sp->pos != TBL_SPAN_DATA)
144 continue;
145
146 /*
147 * Account for the data cells in the layout, matching it
148 * to data cells in the data section.
149 */
150
151 gp = &first_group;
152 for (dp = sp->first; dp != NULL; dp = dp->next) {
153 icol = dp->layout->col;
154 while (maxcol < icol + dp->hspans)
155 tbl->cols[++maxcol].spacing = SIZE_MAX;
156 col = tbl->cols + icol;
157 col->flags |= dp->layout->flags;
158 if (dp->layout->flags & TBL_CELL_WIGN)
159 continue;
160
161 /* Handle explicit width specifications. */
162
163 if (dp->layout->wstr != NULL &&
164 dp->layout->width == 0 &&
165 a2roffsu(dp->layout->wstr, &su, SCALE_EN)
166 != NULL)
167 dp->layout->width =
168 (*tbl->sulen)(&su, tbl->arg);
169 if (col->width < dp->layout->width)
170 col->width = dp->layout->width;
171 if (dp->layout->spacing != SIZE_MAX &&
172 (col->spacing == SIZE_MAX ||
173 col->spacing < dp->layout->spacing))
174 col->spacing = dp->layout->spacing;
175
176 /*
177 * Calculate an automatic width.
178 * Except for spanning cells, apply it.
179 */
180
181 width = tblcalc_data(tbl,
182 dp->hspans == 0 ? col : NULL,
183 opts, dp,
184 dp->block == 0 ? 0 :
185 dp->layout->width ? dp->layout->width :
186 rmargin ? (rmargin + sp->opts->cols / 2)
187 / (sp->opts->cols + 1) : 0);
188 if (dp->hspans == 0)
189 continue;
190
191 /*
192 * Build an ordered, singly linked list
193 * of all groups of columns joined by spans,
194 * recording the minimum width for each group.
195 */
196
197 while (*gp != NULL && ((*gp)->startcol < icol ||
198 (*gp)->endcol < icol + dp->hspans))
199 gp = &(*gp)->next;
200 if (*gp == NULL || (*gp)->startcol > icol ||
201 (*gp)->endcol > icol + dp->hspans) {
202 g = mandoc_malloc(sizeof(*g));
203 g->next = *gp;
204 g->wanted = width;
205 g->startcol = icol;
206 g->endcol = icol + dp->hspans;
207 *gp = g;
208 } else if ((*gp)->wanted < width)
209 (*gp)->wanted = width;
210 }
211 }
212
213 /*
214 * The minimum width of columns explicitly specified
215 * in the layout is 1n.
216 */
217
218 if (maxcol < sp_first->opts->cols - 1)
219 maxcol = sp_first->opts->cols - 1;
220 for (icol = 0; icol <= maxcol; icol++) {
221 col = tbl->cols + icol;
222 if (col->width < 1)
223 col->width = 1;
224
225 /*
226 * Column spacings are needed for span width
227 * calculations, so set the default values now.
228 */
229
230 if (col->spacing == SIZE_MAX || icol == maxcol)
231 col->spacing = 3;
232 }
233
234 /*
235 * Replace the minimum widths with the missing widths,
236 * and dismiss groups that are already wide enough.
237 */
238
239 gp = &first_group;
240 while ((g = *gp) != NULL) {
241 done = 0;
242 for (icol = g->startcol; icol <= g->endcol; icol++) {
243 width = tbl->cols[icol].width;
244 if (icol < g->endcol)
245 width += tbl->cols[icol].spacing;
246 if (g->wanted <= width) {
247 done = 1;
248 break;
249 } else
250 (*gp)->wanted -= width;
251 }
252 if (done) {
253 *gp = g->next;
254 free(g);
255 } else
256 gp = &(*gp)->next;
257 }
258
259 colwidth = mandoc_reallocarray(NULL, maxcol + 1, sizeof(*colwidth));
260 while (first_group != NULL) {
261
262 /*
263 * Rebuild the array of the widths of all columns
264 * participating in spans that require expansion.
265 */
266
267 for (icol = 0; icol <= maxcol; icol++)
268 colwidth[icol] = SIZE_MAX;
269 for (g = first_group; g != NULL; g = g->next)
270 for (icol = g->startcol; icol <= g->endcol; icol++)
271 colwidth[icol] = tbl->cols[icol].width;
272
273 /*
274 * Find the smallest and second smallest column width
275 * among the columns which may need expamsion.
276 */
277
278 min1 = min2 = SIZE_MAX;
279 for (icol = 0; icol <= maxcol; icol++) {
280 if (min1 > colwidth[icol]) {
281 min2 = min1;
282 min1 = colwidth[icol];
283 } else if (min1 < colwidth[icol] &&
284 min2 > colwidth[icol])
285 min2 = colwidth[icol];
286 }
287
288 /*
289 * Find the minimum wanted width
290 * for any one of the narrowest columns,
291 * and mark the columns wanting that width.
292 */
293
294 wanted = min2;
295 for (g = first_group; g != NULL; g = g->next) {
296 necol = 0;
297 for (icol = g->startcol; icol <= g->endcol; icol++)
298 if (tbl->cols[icol].width == min1)
299 necol++;
300 if (necol == 0)
301 continue;
302 width = min1 + (g->wanted - 1) / necol + 1;
303 if (width > min2)
304 width = min2;
305 if (wanted > width)
306 wanted = width;
307 for (icol = g->startcol; icol <= g->endcol; icol++)
308 if (colwidth[icol] == min1 ||
309 (colwidth[icol] < min2 &&
310 colwidth[icol] > width))
311 colwidth[icol] = width;
312 }
313
314 /* Record the effect of the widening on the group list. */
315
316 gp = &first_group;
317 while ((g = *gp) != NULL) {
318 done = 0;
319 for (icol = g->startcol; icol <= g->endcol; icol++) {
320 if (colwidth[icol] != wanted ||
321 tbl->cols[icol].width == wanted)
322 continue;
323 if (g->wanted <= wanted - min1) {
324 done = 1;
325 break;
326 }
327 g->wanted -= wanted - min1;
328 }
329 if (done) {
330 *gp = g->next;
331 free(g);
332 } else
333 gp = &(*gp)->next;
334 }
335
336 /* Record the effect of the widening on the columns. */
337
338 for (icol = 0; icol <= maxcol; icol++)
339 if (colwidth[icol] == wanted)
340 tbl->cols[icol].width = wanted;
341 }
342 free(colwidth);
343
344 /*
345 * Align numbers with text.
346 * Count columns to equalize and columns to maximize.
347 * Find maximum width of the columns to equalize.
348 * Find total width of the columns *not* to maximize.
349 */
350
351 necol = nxcol = 0;
352 ewidth = xwidth = 0;
353 for (icol = 0; icol <= maxcol; icol++) {
354 col = tbl->cols + icol;
355 if (col->width > col->nwidth)
356 col->decimal += (col->width - col->nwidth) / 2;
357 else
358 col->width = col->nwidth;
359 if (col->flags & TBL_CELL_EQUAL) {
360 necol++;
361 if (ewidth < col->width)
362 ewidth = col->width;
363 }
364 if (col->flags & TBL_CELL_WMAX)
365 nxcol++;
366 else
367 xwidth += col->width;
368 }
369
370 /*
371 * Equalize columns, if requested for any of them.
372 * Update total width of the columns not to maximize.
373 */
374
375 if (necol) {
376 for (icol = 0; icol <= maxcol; icol++) {
377 col = tbl->cols + icol;
378 if ( ! (col->flags & TBL_CELL_EQUAL))
379 continue;
380 if (col->width == ewidth)
381 continue;
382 if (nxcol && rmargin)
383 xwidth += ewidth - col->width;
384 col->width = ewidth;
385 }
386 }
387
388 /*
389 * If there are any columns to maximize, find the total
390 * available width, deducting 3n margins between columns.
391 * Distribute the available width evenly.
392 */
393
394 if (nxcol && rmargin) {
395 xwidth += 3*maxcol +
396 (opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ?
397 2 : !!opts->lvert + !!opts->rvert);
398 if (rmargin <= offset + xwidth)
399 return;
400 xwidth = rmargin - offset - xwidth;
401
402 /*
403 * Emulate a bug in GNU tbl width calculation that
404 * manifests itself for large numbers of x-columns.
405 * Emulating it for 5 x-columns gives identical
406 * behaviour for up to 6 x-columns.
407 */
408
409 if (nxcol == 5) {
410 quirkcol = xwidth % nxcol + 2;
411 if (quirkcol != 3 && quirkcol != 4)
412 quirkcol = -1;
413 } else
414 quirkcol = -1;
415
416 necol = 0;
417 ewidth = 0;
418 for (icol = 0; icol <= maxcol; icol++) {
419 col = tbl->cols + icol;
420 if ( ! (col->flags & TBL_CELL_WMAX))
421 continue;
422 col->width = (double)xwidth * ++necol / nxcol
423 - ewidth + 0.4995;
424 if (necol == quirkcol)
425 col->width--;
426 ewidth += col->width;
427 }
428 }
429 }
430
431 static size_t
432 tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
433 const struct tbl_opts *opts, const struct tbl_dat *dp, size_t mw)
434 {
435 size_t sz;
436
437 /* Branch down into data sub-types. */
438
439 switch (dp->layout->pos) {
440 case TBL_CELL_HORIZ:
441 case TBL_CELL_DHORIZ:
442 sz = (*tbl->len)(1, tbl->arg);
443 if (col != NULL && col->width < sz)
444 col->width = sz;
445 return sz;
446 case TBL_CELL_LONG:
447 case TBL_CELL_CENTRE:
448 case TBL_CELL_LEFT:
449 case TBL_CELL_RIGHT:
450 return tblcalc_literal(tbl, col, dp, mw);
451 case TBL_CELL_NUMBER:
452 return tblcalc_number(tbl, col, opts, dp);
453 case TBL_CELL_DOWN:
454 return 0;
455 default:
456 abort();
457 }
458 }
459
460 static size_t
461 tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
462 const struct tbl_dat *dp, size_t mw)
463 {
464 const char *str; /* Beginning of the first line. */
465 const char *beg; /* Beginning of the current line. */
466 char *end; /* End of the current line. */
467 size_t lsz; /* Length of the current line. */
468 size_t wsz; /* Length of the current word. */
469 size_t msz; /* Length of the longest line. */
470
471 if (dp->string == NULL || *dp->string == '\0')
472 return 0;
473 str = mw ? mandoc_strdup(dp->string) : dp->string;
474 msz = lsz = 0;
475 for (beg = str; beg != NULL && *beg != '\0'; beg = end) {
476 end = mw ? strchr(beg, ' ') : NULL;
477 if (end != NULL) {
478 *end++ = '\0';
479 while (*end == ' ')
480 end++;
481 }
482 wsz = (*tbl->slen)(beg, tbl->arg);
483 if (mw && lsz && lsz + 1 + wsz <= mw)
484 lsz += 1 + wsz;
485 else
486 lsz = wsz;
487 if (msz < lsz)
488 msz = lsz;
489 }
490 if (mw)
491 free((void *)str);
492 if (col != NULL && col->width < msz)
493 col->width = msz;
494 return msz;
495 }
496
497 static size_t
498 tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
499 const struct tbl_opts *opts, const struct tbl_dat *dp)
500 {
501 const char *cp, *lastdigit, *lastpoint;
502 size_t intsz, totsz;
503 char buf[2];
504
505 if (dp->string == NULL || *dp->string == '\0')
506 return 0;
507
508 totsz = (*tbl->slen)(dp->string, tbl->arg);
509 if (col == NULL)
510 return totsz;
511
512 /*
513 * Find the last digit and
514 * the last decimal point that is adjacent to a digit.
515 * The alignment indicator "\&" overrides everything.
516 */
517
518 lastdigit = lastpoint = NULL;
519 for (cp = dp->string; cp[0] != '\0'; cp++) {
520 if (cp[0] == '\\' && cp[1] == '&') {
521 lastdigit = lastpoint = cp;
522 break;
523 } else if (cp[0] == opts->decimal &&
524 (isdigit((unsigned char)cp[1]) ||
525 (cp > dp->string && isdigit((unsigned char)cp[-1]))))
526 lastpoint = cp;
527 else if (isdigit((unsigned char)cp[0]))
528 lastdigit = cp;
529 }
530
531 /* Not a number, treat as a literal string. */
532
533 if (lastdigit == NULL) {
534 if (col != NULL && col->width < totsz)
535 col->width = totsz;
536 return totsz;
537 }
538
539 /* Measure the width of the integer part. */
540
541 if (lastpoint == NULL)
542 lastpoint = lastdigit + 1;
543 intsz = 0;
544 buf[1] = '\0';
545 for (cp = dp->string; cp < lastpoint; cp++) {
546 buf[0] = cp[0];
547 intsz += (*tbl->slen)(buf, tbl->arg);
548 }
549
550 /*
551 * If this number has more integer digits than all numbers
552 * seen on earlier lines, shift them all to the right.
553 * If it has fewer, shift this number to the right.
554 */
555
556 if (intsz > col->decimal) {
557 col->nwidth += intsz - col->decimal;
558 col->decimal = intsz;
559 } else
560 totsz += col->decimal - intsz;
561
562 /* Update the maximum total width seen so far. */
563
564 if (totsz > col->nwidth)
565 col->nwidth = totsz;
566 return totsz;
567 }