Annotation of src/usr.bin/mandoc/tbl_html.c, Revision 1.33
1.33 ! schwarze 1: /* $OpenBSD: tbl_html.c,v 1.32 2021/09/09 14:45:18 schwarze Exp $ */
1.1 schwarze 2: /*
1.5 schwarze 3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.29 schwarze 4: * Copyright (c) 2014,2015,2017,2018,2021 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 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: */
1.8 schwarze 18: #include <sys/types.h>
19:
1.1 schwarze 20: #include <assert.h>
21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
1.26 schwarze 25: #include "mandoc.h"
1.28 schwarze 26: #include "roff.h"
1.25 schwarze 27: #include "tbl.h"
1.1 schwarze 28: #include "out.h"
29: #include "html.h"
30:
1.4 schwarze 31: static void html_tblopen(struct html *, const struct tbl_span *);
1.2 schwarze 32: static size_t html_tbl_len(size_t, void *);
33: static size_t html_tbl_strlen(const char *, void *);
1.16 schwarze 34: static size_t html_tbl_sulen(const struct roffsu *, void *);
1.2 schwarze 35:
1.7 schwarze 36:
1.2 schwarze 37: static size_t
38: html_tbl_len(size_t sz, void *arg)
39: {
1.12 schwarze 40: return sz;
1.2 schwarze 41: }
42:
43: static size_t
44: html_tbl_strlen(const char *p, void *arg)
45: {
1.16 schwarze 46: return strlen(p);
47: }
1.2 schwarze 48:
1.16 schwarze 49: static size_t
50: html_tbl_sulen(const struct roffsu *su, void *arg)
51: {
1.18 schwarze 52: if (su->scale < 0.0)
53: return 0;
54:
1.16 schwarze 55: switch (su->unit) {
56: case SCALE_FS: /* 2^16 basic units */
57: return su->scale * 65536.0 / 24.0;
58: case SCALE_IN: /* 10 characters per inch */
59: return su->scale * 10.0;
60: case SCALE_CM: /* 2.54 cm per inch */
61: return su->scale * 10.0 / 2.54;
62: case SCALE_PC: /* 6 pica per inch */
63: case SCALE_VS:
64: return su->scale * 10.0 / 6.0;
65: case SCALE_EN:
66: case SCALE_EM:
67: return su->scale;
68: case SCALE_PT: /* 12 points per pica */
69: return su->scale * 10.0 / 6.0 / 12.0;
70: case SCALE_BU: /* 24 basic units per character */
71: return su->scale / 24.0;
72: case SCALE_MM: /* 1/1000 inch */
73: return su->scale / 100.0;
74: default:
75: abort();
76: }
1.2 schwarze 77: }
78:
1.4 schwarze 79: static void
80: html_tblopen(struct html *h, const struct tbl_span *sp)
81: {
1.27 schwarze 82: html_close_paragraph(h);
1.11 schwarze 83: if (h->tbl.cols == NULL) {
1.4 schwarze 84: h->tbl.len = html_tbl_len;
85: h->tbl.slen = html_tbl_strlen;
1.16 schwarze 86: h->tbl.sulen = html_tbl_sulen;
1.17 schwarze 87: tblcalc(&h->tbl, sp, 0, 0);
1.4 schwarze 88: }
89: assert(NULL == h->tblt);
1.24 schwarze 90: h->tblt = print_otag(h, TAG_TABLE, "c?ss", "tbl",
91: "border",
92: sp->opts->opts & TBL_OPT_ALLBOX ? "1" : NULL,
93: "border-style",
94: sp->opts->opts & TBL_OPT_DBOX ? "double" :
95: sp->opts->opts & TBL_OPT_BOX ? "solid" : NULL,
96: "border-top-style",
97: sp->pos == TBL_SPAN_DHORIZ ? "double" :
98: sp->pos == TBL_SPAN_HORIZ ? "solid" : NULL);
1.4 schwarze 99: }
100:
101: void
102: print_tblclose(struct html *h)
103: {
104:
105: assert(h->tblt);
106: print_tagq(h, h->tblt);
107: h->tblt = NULL;
108: }
109:
1.1 schwarze 110: void
111: print_tbl(struct html *h, const struct tbl_span *sp)
112: {
1.21 schwarze 113: const struct tbl_dat *dp;
1.24 schwarze 114: const struct tbl_cell *cp;
115: const struct tbl_span *psp;
1.33 ! schwarze 116: const struct roffcol *col;
1.21 schwarze 117: struct tag *tt;
118: const char *hspans, *vspans, *halign, *valign;
1.24 schwarze 119: const char *bborder, *lborder, *rborder;
1.33 ! schwarze 120: const char *ccp;
1.21 schwarze 121: char hbuf[4], vbuf[4];
1.33 ! schwarze 122: size_t sz;
1.29 schwarze 123: enum mandoc_esc save_font;
1.24 schwarze 124: int i;
1.2 schwarze 125:
1.9 schwarze 126: if (h->tblt == NULL)
1.4 schwarze 127: html_tblopen(h, sp);
128:
1.24 schwarze 129: /*
130: * Horizontal lines spanning the whole table
131: * are handled by previous or following table rows.
132: */
133:
134: if (sp->pos != TBL_SPAN_DATA)
135: return;
136:
137: /* Inhibit printing of spaces: we do padding ourselves. */
1.4 schwarze 138:
1.2 schwarze 139: h->flags |= HTML_NONOSPACE;
140: h->flags |= HTML_NOSPACE;
141:
1.24 schwarze 142: /* Draw a vertical line left of this row? */
1.1 schwarze 143:
1.24 schwarze 144: switch (sp->layout->vert) {
145: case 2:
146: lborder = "double";
147: break;
148: case 1:
149: lborder = "solid";
1.2 schwarze 150: break;
1.1 schwarze 151: default:
1.24 schwarze 152: lborder = NULL;
153: break;
154: }
1.22 schwarze 155:
1.24 schwarze 156: /* Draw a horizontal line below this row? */
157:
158: bborder = NULL;
159: if ((psp = sp->next) != NULL) {
160: switch (psp->pos) {
161: case TBL_SPAN_DHORIZ:
162: bborder = "double";
163: break;
164: case TBL_SPAN_HORIZ:
165: bborder = "solid";
166: break;
167: default:
168: break;
169: }
170: }
171:
172: tt = print_otag(h, TAG_TR, "ss",
173: "border-left-style", lborder,
174: "border-bottom-style", bborder);
175:
176: for (dp = sp->first; dp != NULL; dp = dp->next) {
177: print_stagq(h, tt);
178:
179: /*
180: * Do not generate <td> elements for continuations
181: * of spanned cells. Larger <td> elements covering
182: * this space were already generated earlier.
183: */
184:
185: cp = dp->layout;
186: if (cp->pos == TBL_CELL_SPAN || cp->pos == TBL_CELL_DOWN ||
187: (dp->string != NULL && strcmp(dp->string, "\\^") == 0))
188: continue;
189:
190: /* Determine the attribute values. */
191:
192: if (dp->hspans > 0) {
193: (void)snprintf(hbuf, sizeof(hbuf),
194: "%d", dp->hspans + 1);
195: hspans = hbuf;
196: } else
197: hspans = NULL;
198: if (dp->vspans > 0) {
199: (void)snprintf(vbuf, sizeof(vbuf),
200: "%d", dp->vspans + 1);
201: vspans = vbuf;
202: } else
203: vspans = NULL;
204:
205: switch (cp->pos) {
206: case TBL_CELL_CENTRE:
207: halign = "center";
208: break;
209: case TBL_CELL_RIGHT:
210: case TBL_CELL_NUMBER:
211: halign = "right";
212: break;
213: default:
214: halign = NULL;
215: break;
1.2 schwarze 216: }
1.24 schwarze 217: if (cp->flags & TBL_CELL_TALIGN)
218: valign = "top";
219: else if (cp->flags & TBL_CELL_BALIGN)
220: valign = "bottom";
221: else
222: valign = NULL;
223:
224: for (i = dp->hspans; i > 0; i--)
225: cp = cp->next;
226: switch (cp->vert) {
227: case 2:
228: rborder = "double";
229: break;
230: case 1:
231: rborder = "solid";
232: break;
233: default:
234: rborder = NULL;
235: break;
236: }
237:
238: /* Print the element and the attributes. */
239:
240: print_otag(h, TAG_TD, "??sss",
241: "colspan", hspans, "rowspan", vspans,
242: "vertical-align", valign,
243: "text-align", halign,
244: "border-right-style", rborder);
1.32 schwarze 245: if (dp->layout->pos == TBL_CELL_HORIZ ||
246: dp->layout->pos == TBL_CELL_DHORIZ ||
247: dp->pos == TBL_DATA_HORIZ ||
248: dp->pos == TBL_DATA_DHORIZ)
249: print_otag(h, TAG_HR, "");
250: else if (dp->string != NULL) {
1.29 schwarze 251: save_font = h->metac;
1.31 schwarze 252: html_setfont(h, dp->layout->font);
1.30 schwarze 253: if (dp->layout->pos == TBL_CELL_LONG)
254: print_text(h, "\\[u2003]"); /* em space */
1.24 schwarze 255: print_text(h, dp->string);
1.33 ! schwarze 256: if (dp->layout->pos == TBL_CELL_NUMBER) {
! 257: col = h->tbl.cols + dp->layout->col;
! 258: if (col->decimal < col->nwidth) {
! 259: if ((ccp = strrchr(dp->string,
! 260: sp->opts->decimal)) == NULL) {
! 261: /* Punctuation space. */
! 262: print_text(h, "\\[u2008]");
! 263: ccp = strchr(dp->string, '\0');
! 264: } else
! 265: ccp++;
! 266: sz = col->nwidth - col->decimal;
! 267: while (--sz > 0) {
! 268: if (*ccp == '\0')
! 269: /* Figure space. */
! 270: print_text(h,
! 271: "\\[u2007]");
! 272: else
! 273: ccp++;
! 274: }
! 275: }
! 276: }
1.29 schwarze 277: html_setfont(h, save_font);
278: }
1.1 schwarze 279: }
280:
1.4 schwarze 281: print_tagq(h, tt);
282:
1.2 schwarze 283: h->flags &= ~HTML_NONOSPACE;
1.1 schwarze 284:
1.11 schwarze 285: if (sp->next == NULL) {
1.2 schwarze 286: assert(h->tbl.cols);
287: free(h->tbl.cols);
288: h->tbl.cols = NULL;
1.4 schwarze 289: print_tblclose(h);
1.1 schwarze 290: }
291: }