xref: /openbsd-src/usr.bin/mandoc/tbl_html.c (revision d1df930ffab53da22f3324c32bed7ac5709915e6)
1 /*	$OpenBSD: tbl_html.c,v 1.19 2018/06/25 13:46:01 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2014, 2015, 2017 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", "tbl");
88 }
89 
90 void
91 print_tblclose(struct html *h)
92 {
93 
94 	assert(h->tblt);
95 	print_tagq(h, h->tblt);
96 	h->tblt = NULL;
97 }
98 
99 void
100 print_tbl(struct html *h, const struct tbl_span *sp)
101 {
102 	const struct tbl_dat *dp;
103 	struct tag	*tt;
104 	int		 ic;
105 
106 	/* Inhibit printing of spaces: we do padding ourselves. */
107 
108 	if (h->tblt == NULL)
109 		html_tblopen(h, sp);
110 
111 	assert(h->tblt);
112 
113 	h->flags |= HTML_NONOSPACE;
114 	h->flags |= HTML_NOSPACE;
115 
116 	tt = print_otag(h, TAG_TR, "");
117 
118 	switch (sp->pos) {
119 	case TBL_SPAN_HORIZ:
120 	case TBL_SPAN_DHORIZ:
121 		print_otag(h, TAG_TD, "?", "colspan", "0");
122 		break;
123 	default:
124 		dp = sp->first;
125 		for (ic = 0; ic < sp->opts->cols; ic++) {
126 			print_stagq(h, tt);
127 			print_otag(h, TAG_TD, "");
128 
129 			if (dp == NULL || dp->layout->col > ic)
130 				continue;
131 			if (dp->layout->pos != TBL_CELL_DOWN)
132 				if (dp->string != NULL)
133 					print_text(h, dp->string);
134 			dp = dp->next;
135 		}
136 		break;
137 	}
138 
139 	print_tagq(h, tt);
140 
141 	h->flags &= ~HTML_NONOSPACE;
142 
143 	if (sp->next == NULL) {
144 		assert(h->tbl.cols);
145 		free(h->tbl.cols);
146 		h->tbl.cols = NULL;
147 		print_tblclose(h);
148 	}
149 
150 }
151