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