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