1544c191cSchristos /* Id: tbl_html.c,v 1.32 2019/01/06 04:55:09 schwarze Exp */
2c0d9444aSjoerg /*
3c5f73b34Sjoerg * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4544c191cSchristos * Copyright (c) 2014, 2015, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
5c0d9444aSjoerg *
6c0d9444aSjoerg * Permission to use, copy, modify, and distribute this software for any
7c0d9444aSjoerg * purpose with or without fee is hereby granted, provided that the above
8c0d9444aSjoerg * copyright notice and this permission notice appear in all copies.
9c0d9444aSjoerg *
10c0d9444aSjoerg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11c0d9444aSjoerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12c0d9444aSjoerg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13c0d9444aSjoerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14c0d9444aSjoerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15c0d9444aSjoerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16c0d9444aSjoerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17c0d9444aSjoerg */
18c0d9444aSjoerg #include "config.h"
19fec65c98Schristos
20fec65c98Schristos #include <sys/types.h>
21c0d9444aSjoerg
22c0d9444aSjoerg #include <assert.h>
23c0d9444aSjoerg #include <stdio.h>
24c0d9444aSjoerg #include <stdlib.h>
25c0d9444aSjoerg #include <string.h>
26c0d9444aSjoerg
27c0d9444aSjoerg #include "mandoc.h"
28544c191cSchristos #include "tbl.h"
29c0d9444aSjoerg #include "out.h"
30c0d9444aSjoerg #include "html.h"
31c0d9444aSjoerg
3248741257Sjoerg static void html_tblopen(struct html *, const struct tbl_span *);
33c0d9444aSjoerg static size_t html_tbl_len(size_t, void *);
34c0d9444aSjoerg static size_t html_tbl_strlen(const char *, void *);
35c9bcef03Schristos static size_t html_tbl_sulen(const struct roffsu *, void *);
36c0d9444aSjoerg
37fec65c98Schristos
38c0d9444aSjoerg static size_t
html_tbl_len(size_t sz,void * arg)39c0d9444aSjoerg html_tbl_len(size_t sz, void *arg)
40c0d9444aSjoerg {
419ff1f2acSchristos return sz;
42c0d9444aSjoerg }
43c0d9444aSjoerg
44c0d9444aSjoerg static size_t
html_tbl_strlen(const char * p,void * arg)45c0d9444aSjoerg html_tbl_strlen(const char *p, void *arg)
46c0d9444aSjoerg {
479ff1f2acSchristos return strlen(p);
48c0d9444aSjoerg }
49c0d9444aSjoerg
50c9bcef03Schristos static size_t
html_tbl_sulen(const struct roffsu * su,void * arg)51c9bcef03Schristos html_tbl_sulen(const struct roffsu *su, void *arg)
52c9bcef03Schristos {
53c9bcef03Schristos if (su->scale < 0.0)
54c9bcef03Schristos return 0;
55c9bcef03Schristos
56c9bcef03Schristos switch (su->unit) {
57c9bcef03Schristos case SCALE_FS: /* 2^16 basic units */
58c9bcef03Schristos return su->scale * 65536.0 / 24.0;
59c9bcef03Schristos case SCALE_IN: /* 10 characters per inch */
60c9bcef03Schristos return su->scale * 10.0;
61c9bcef03Schristos case SCALE_CM: /* 2.54 cm per inch */
62c9bcef03Schristos return su->scale * 10.0 / 2.54;
63c9bcef03Schristos case SCALE_PC: /* 6 pica per inch */
64c9bcef03Schristos case SCALE_VS:
65c9bcef03Schristos return su->scale * 10.0 / 6.0;
66c9bcef03Schristos case SCALE_EN:
67c9bcef03Schristos case SCALE_EM:
68c9bcef03Schristos return su->scale;
69c9bcef03Schristos case SCALE_PT: /* 12 points per pica */
70c9bcef03Schristos return su->scale * 10.0 / 6.0 / 12.0;
71c9bcef03Schristos case SCALE_BU: /* 24 basic units per character */
72c9bcef03Schristos return su->scale / 24.0;
73c9bcef03Schristos case SCALE_MM: /* 1/1000 inch */
74c9bcef03Schristos return su->scale / 100.0;
75c9bcef03Schristos default:
76c9bcef03Schristos abort();
77c9bcef03Schristos }
78c9bcef03Schristos }
79c9bcef03Schristos
8048741257Sjoerg static void
html_tblopen(struct html * h,const struct tbl_span * sp)8148741257Sjoerg html_tblopen(struct html *h, const struct tbl_span *sp)
82c0d9444aSjoerg {
83544c191cSchristos html_close_paragraph(h);
84fec65c98Schristos if (h->tbl.cols == NULL) {
85c0d9444aSjoerg h->tbl.len = html_tbl_len;
86c0d9444aSjoerg h->tbl.slen = html_tbl_strlen;
87c9bcef03Schristos h->tbl.sulen = html_tbl_sulen;
88c9bcef03Schristos tblcalc(&h->tbl, sp, 0, 0);
89c0d9444aSjoerg }
9048741257Sjoerg assert(NULL == h->tblt);
91544c191cSchristos h->tblt = print_otag(h, TAG_TABLE, "c?ss", "tbl",
92544c191cSchristos "border",
93544c191cSchristos sp->opts->opts & TBL_OPT_ALLBOX ? "1" : NULL,
94544c191cSchristos "border-style",
95544c191cSchristos sp->opts->opts & TBL_OPT_DBOX ? "double" :
96544c191cSchristos sp->opts->opts & TBL_OPT_BOX ? "solid" : NULL,
97544c191cSchristos "border-top-style",
98544c191cSchristos sp->pos == TBL_SPAN_DHORIZ ? "double" :
99544c191cSchristos sp->pos == TBL_SPAN_HORIZ ? "solid" : NULL);
10048741257Sjoerg }
10148741257Sjoerg
10248741257Sjoerg void
print_tblclose(struct html * h)10348741257Sjoerg print_tblclose(struct html *h)
10448741257Sjoerg {
10548741257Sjoerg
10648741257Sjoerg assert(h->tblt);
10748741257Sjoerg print_tagq(h, h->tblt);
10848741257Sjoerg h->tblt = NULL;
10948741257Sjoerg }
11048741257Sjoerg
11148741257Sjoerg void
print_tbl(struct html * h,const struct tbl_span * sp)11248741257Sjoerg print_tbl(struct html *h, const struct tbl_span *sp)
11348741257Sjoerg {
11448741257Sjoerg const struct tbl_dat *dp;
115544c191cSchristos const struct tbl_cell *cp;
116544c191cSchristos const struct tbl_span *psp;
11748741257Sjoerg struct tag *tt;
118544c191cSchristos const char *hspans, *vspans, *halign, *valign;
119544c191cSchristos const char *bborder, *lborder, *rborder;
120*6167eca2Schristos char hbuf[32], vbuf[32];
121544c191cSchristos int i;
12248741257Sjoerg
123fec65c98Schristos if (h->tblt == NULL)
12448741257Sjoerg html_tblopen(h, sp);
12548741257Sjoerg
126544c191cSchristos /*
127544c191cSchristos * Horizontal lines spanning the whole table
128544c191cSchristos * are handled by previous or following table rows.
129544c191cSchristos */
130544c191cSchristos
131544c191cSchristos if (sp->pos != TBL_SPAN_DATA)
132544c191cSchristos return;
133544c191cSchristos
134544c191cSchristos /* Inhibit printing of spaces: we do padding ourselves. */
13548741257Sjoerg
13648741257Sjoerg h->flags |= HTML_NONOSPACE;
13748741257Sjoerg h->flags |= HTML_NOSPACE;
13848741257Sjoerg
139544c191cSchristos /* Draw a vertical line left of this row? */
14048741257Sjoerg
141544c191cSchristos switch (sp->layout->vert) {
142544c191cSchristos case 2:
143544c191cSchristos lborder = "double";
144544c191cSchristos break;
145544c191cSchristos case 1:
146544c191cSchristos lborder = "solid";
147c0d9444aSjoerg break;
148c0d9444aSjoerg default:
149544c191cSchristos lborder = NULL;
150544c191cSchristos break;
151544c191cSchristos }
15248741257Sjoerg
153544c191cSchristos /* Draw a horizontal line below this row? */
154544c191cSchristos
155544c191cSchristos bborder = NULL;
156544c191cSchristos if ((psp = sp->next) != NULL) {
157544c191cSchristos switch (psp->pos) {
158544c191cSchristos case TBL_SPAN_DHORIZ:
159544c191cSchristos bborder = "double";
160544c191cSchristos break;
161544c191cSchristos case TBL_SPAN_HORIZ:
162544c191cSchristos bborder = "solid";
163544c191cSchristos break;
164544c191cSchristos default:
165544c191cSchristos break;
166544c191cSchristos }
167544c191cSchristos }
168544c191cSchristos
169544c191cSchristos tt = print_otag(h, TAG_TR, "ss",
170544c191cSchristos "border-left-style", lborder,
171544c191cSchristos "border-bottom-style", bborder);
172544c191cSchristos
173544c191cSchristos for (dp = sp->first; dp != NULL; dp = dp->next) {
174544c191cSchristos print_stagq(h, tt);
175544c191cSchristos
176544c191cSchristos /*
177544c191cSchristos * Do not generate <td> elements for continuations
178544c191cSchristos * of spanned cells. Larger <td> elements covering
179544c191cSchristos * this space were already generated earlier.
180544c191cSchristos */
181544c191cSchristos
182544c191cSchristos cp = dp->layout;
183544c191cSchristos if (cp->pos == TBL_CELL_SPAN || cp->pos == TBL_CELL_DOWN ||
184544c191cSchristos (dp->string != NULL && strcmp(dp->string, "\\^") == 0))
185fec65c98Schristos continue;
186544c191cSchristos
187544c191cSchristos /* Determine the attribute values. */
188544c191cSchristos
189544c191cSchristos if (dp->hspans > 0) {
190544c191cSchristos (void)snprintf(hbuf, sizeof(hbuf),
191544c191cSchristos "%d", dp->hspans + 1);
192544c191cSchristos hspans = hbuf;
193544c191cSchristos } else
194544c191cSchristos hspans = NULL;
195544c191cSchristos if (dp->vspans > 0) {
196544c191cSchristos (void)snprintf(vbuf, sizeof(vbuf),
197544c191cSchristos "%d", dp->vspans + 1);
198544c191cSchristos vspans = vbuf;
199544c191cSchristos } else
200544c191cSchristos vspans = NULL;
201544c191cSchristos
202544c191cSchristos switch (cp->pos) {
203544c191cSchristos case TBL_CELL_CENTRE:
204544c191cSchristos halign = "center";
205544c191cSchristos break;
206544c191cSchristos case TBL_CELL_RIGHT:
207544c191cSchristos case TBL_CELL_NUMBER:
208544c191cSchristos halign = "right";
209544c191cSchristos break;
210544c191cSchristos default:
211544c191cSchristos halign = NULL;
212544c191cSchristos break;
213544c191cSchristos }
214544c191cSchristos if (cp->flags & TBL_CELL_TALIGN)
215544c191cSchristos valign = "top";
216544c191cSchristos else if (cp->flags & TBL_CELL_BALIGN)
217544c191cSchristos valign = "bottom";
218544c191cSchristos else
219544c191cSchristos valign = NULL;
220544c191cSchristos
221544c191cSchristos for (i = dp->hspans; i > 0; i--)
222544c191cSchristos cp = cp->next;
223544c191cSchristos switch (cp->vert) {
224544c191cSchristos case 2:
225544c191cSchristos rborder = "double";
226544c191cSchristos break;
227544c191cSchristos case 1:
228544c191cSchristos rborder = "solid";
229544c191cSchristos break;
230544c191cSchristos default:
231544c191cSchristos rborder = NULL;
232544c191cSchristos break;
233544c191cSchristos }
234544c191cSchristos
235544c191cSchristos /* Print the element and the attributes. */
236544c191cSchristos
237544c191cSchristos print_otag(h, TAG_TD, "??sss",
238544c191cSchristos "colspan", hspans, "rowspan", vspans,
239544c191cSchristos "vertical-align", valign,
240544c191cSchristos "text-align", halign,
241544c191cSchristos "border-right-style", rborder);
242fec65c98Schristos if (dp->string != NULL)
24348741257Sjoerg print_text(h, dp->string);
24448741257Sjoerg }
245c0d9444aSjoerg
246c0d9444aSjoerg print_tagq(h, tt);
247c0d9444aSjoerg
248c0d9444aSjoerg h->flags &= ~HTML_NONOSPACE;
249c0d9444aSjoerg
250fec65c98Schristos if (sp->next == NULL) {
251c0d9444aSjoerg assert(h->tbl.cols);
252c0d9444aSjoerg free(h->tbl.cols);
253c0d9444aSjoerg h->tbl.cols = NULL;
25448741257Sjoerg print_tblclose(h);
255c0d9444aSjoerg }
256c0d9444aSjoerg }
257