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