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