Annotation of src/usr.bin/mandoc/tbl_html.c, Revision 1.26
1.26 ! schwarze 1: /* $OpenBSD: tbl_html.c,v 1.25 2018/12/12 21:54:30 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.11 schwarze 81: if (h->tbl.cols == NULL) {
1.4 schwarze 82: h->tbl.len = html_tbl_len;
83: h->tbl.slen = html_tbl_strlen;
1.16 schwarze 84: h->tbl.sulen = html_tbl_sulen;
1.17 schwarze 85: tblcalc(&h->tbl, sp, 0, 0);
1.4 schwarze 86: }
87: assert(NULL == h->tblt);
1.24 schwarze 88: h->tblt = print_otag(h, TAG_TABLE, "c?ss", "tbl",
89: "border",
90: sp->opts->opts & TBL_OPT_ALLBOX ? "1" : NULL,
91: "border-style",
92: sp->opts->opts & TBL_OPT_DBOX ? "double" :
93: sp->opts->opts & TBL_OPT_BOX ? "solid" : NULL,
94: "border-top-style",
95: sp->pos == TBL_SPAN_DHORIZ ? "double" :
96: sp->pos == TBL_SPAN_HORIZ ? "solid" : NULL);
1.4 schwarze 97: }
98:
99: void
100: print_tblclose(struct html *h)
101: {
102:
103: assert(h->tblt);
104: print_tagq(h, h->tblt);
105: h->tblt = NULL;
106: }
107:
1.1 schwarze 108: void
109: print_tbl(struct html *h, const struct tbl_span *sp)
110: {
1.21 schwarze 111: const struct tbl_dat *dp;
1.24 schwarze 112: const struct tbl_cell *cp;
113: const struct tbl_span *psp;
1.21 schwarze 114: struct tag *tt;
115: const char *hspans, *vspans, *halign, *valign;
1.24 schwarze 116: const char *bborder, *lborder, *rborder;
1.21 schwarze 117: char hbuf[4], vbuf[4];
1.24 schwarze 118: int i;
1.2 schwarze 119:
1.9 schwarze 120: if (h->tblt == NULL)
1.4 schwarze 121: html_tblopen(h, sp);
122:
1.24 schwarze 123: /*
124: * Horizontal lines spanning the whole table
125: * are handled by previous or following table rows.
126: */
127:
128: if (sp->pos != TBL_SPAN_DATA)
129: return;
130:
131: /* Inhibit printing of spaces: we do padding ourselves. */
1.4 schwarze 132:
1.2 schwarze 133: h->flags |= HTML_NONOSPACE;
134: h->flags |= HTML_NOSPACE;
135:
1.24 schwarze 136: /* Draw a vertical line left of this row? */
1.1 schwarze 137:
1.24 schwarze 138: switch (sp->layout->vert) {
139: case 2:
140: lborder = "double";
141: break;
142: case 1:
143: lborder = "solid";
1.2 schwarze 144: break;
1.1 schwarze 145: default:
1.24 schwarze 146: lborder = NULL;
147: break;
148: }
1.22 schwarze 149:
1.24 schwarze 150: /* Draw a horizontal line below this row? */
151:
152: bborder = NULL;
153: if ((psp = sp->next) != NULL) {
154: switch (psp->pos) {
155: case TBL_SPAN_DHORIZ:
156: bborder = "double";
157: break;
158: case TBL_SPAN_HORIZ:
159: bborder = "solid";
160: break;
161: default:
162: break;
163: }
164: }
165:
166: tt = print_otag(h, TAG_TR, "ss",
167: "border-left-style", lborder,
168: "border-bottom-style", bborder);
169:
170: for (dp = sp->first; dp != NULL; dp = dp->next) {
171: print_stagq(h, tt);
172:
173: /*
174: * Do not generate <td> elements for continuations
175: * of spanned cells. Larger <td> elements covering
176: * this space were already generated earlier.
177: */
178:
179: cp = dp->layout;
180: if (cp->pos == TBL_CELL_SPAN || cp->pos == TBL_CELL_DOWN ||
181: (dp->string != NULL && strcmp(dp->string, "\\^") == 0))
182: continue;
183:
184: /* Determine the attribute values. */
185:
186: if (dp->hspans > 0) {
187: (void)snprintf(hbuf, sizeof(hbuf),
188: "%d", dp->hspans + 1);
189: hspans = hbuf;
190: } else
191: hspans = NULL;
192: if (dp->vspans > 0) {
193: (void)snprintf(vbuf, sizeof(vbuf),
194: "%d", dp->vspans + 1);
195: vspans = vbuf;
196: } else
197: vspans = NULL;
198:
199: switch (cp->pos) {
200: case TBL_CELL_CENTRE:
201: halign = "center";
202: break;
203: case TBL_CELL_RIGHT:
204: case TBL_CELL_NUMBER:
205: halign = "right";
206: break;
207: default:
208: halign = NULL;
209: break;
1.2 schwarze 210: }
1.24 schwarze 211: if (cp->flags & TBL_CELL_TALIGN)
212: valign = "top";
213: else if (cp->flags & TBL_CELL_BALIGN)
214: valign = "bottom";
215: else
216: valign = NULL;
217:
218: for (i = dp->hspans; i > 0; i--)
219: cp = cp->next;
220: switch (cp->vert) {
221: case 2:
222: rborder = "double";
223: break;
224: case 1:
225: rborder = "solid";
226: break;
227: default:
228: rborder = NULL;
229: break;
230: }
231:
232: /* Print the element and the attributes. */
233:
234: print_otag(h, TAG_TD, "??sss",
235: "colspan", hspans, "rowspan", vspans,
236: "vertical-align", valign,
237: "text-align", halign,
238: "border-right-style", rborder);
239: if (dp->string != NULL)
240: print_text(h, dp->string);
1.1 schwarze 241: }
242:
1.4 schwarze 243: print_tagq(h, tt);
244:
1.2 schwarze 245: h->flags &= ~HTML_NONOSPACE;
1.1 schwarze 246:
1.11 schwarze 247: if (sp->next == NULL) {
1.2 schwarze 248: assert(h->tbl.cols);
249: free(h->tbl.cols);
250: h->tbl.cols = NULL;
1.4 schwarze 251: print_tblclose(h);
1.1 schwarze 252: }
253: }