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