1 /* $Id: tbl_html.c,v 1.38 2021/09/09 16:52:52 schwarze Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014,2015,2017,2018,2021 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
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 *);
40 html_tbl_len(size_t sz, void *arg)
46 html_tbl_strlen(const char *p, void *arg)
52 html_tbl_sulen(const struct roffsu *su, void *arg)
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 */
66 return su->scale * 10.0 / 6.0;
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;
82 html_tblopen(struct html *h, const struct tbl_span *sp)
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);
91 assert(NULL == h->tblt);
92 h->tblt = print_otag(h, TAG_TABLE, "c?ss", "tbl",
94 sp->opts->opts & TBL_OPT_ALLBOX ? "1" : NULL,
96 sp->opts->opts & TBL_OPT_DBOX ? "double" :
97 sp->opts->opts & TBL_OPT_BOX ? "solid" : NULL,
99 sp->pos == TBL_SPAN_DHORIZ ? "double" :
100 sp->pos == TBL_SPAN_HORIZ ? "solid" : NULL);
104 print_tblclose(struct html *h)
108 print_tagq(h, h->tblt);
113 print_tbl(struct html *h, const struct tbl_span *sp)
115 const struct tbl_dat *dp;
116 const struct tbl_cell *cp;
117 const struct tbl_span *psp;
118 const struct roffcol *col;
120 const char *hspans, *vspans, *halign, *valign;
121 const char *bborder, *lborder, *rborder;
123 char hbuf[4], vbuf[4];
125 enum mandoc_esc save_font;
132 * Horizontal lines spanning the whole table
133 * are handled by previous or following table rows.
136 if (sp->pos != TBL_SPAN_DATA)
139 /* Inhibit printing of spaces: we do padding ourselves. */
141 h->flags |= HTML_NONOSPACE;
142 h->flags |= HTML_NOSPACE;
144 /* Draw a vertical line left of this row? */
146 switch (sp->layout->vert) {
158 /* Draw a horizontal line below this row? */
161 if ((psp = sp->next) != NULL) {
163 case TBL_SPAN_DHORIZ:
174 tt = print_otag(h, TAG_TR, "ss",
175 "border-left-style", lborder,
176 "border-bottom-style", bborder);
178 for (dp = sp->first; dp != NULL; dp = dp->next) {
182 * Do not generate <td> elements for continuations
183 * of spanned cells. Larger <td> elements covering
184 * this space were already generated earlier.
188 if (cp->pos == TBL_CELL_SPAN || cp->pos == TBL_CELL_DOWN ||
189 (dp->string != NULL && strcmp(dp->string, "\\^") == 0))
192 /* Determine the attribute values. */
194 if (dp->hspans > 0) {
195 (void)snprintf(hbuf, sizeof(hbuf),
196 "%d", dp->hspans + 1);
200 if (dp->vspans > 0) {
201 (void)snprintf(vbuf, sizeof(vbuf),
202 "%d", dp->vspans + 1);
208 case TBL_CELL_CENTRE:
212 case TBL_CELL_NUMBER:
219 if (cp->flags & TBL_CELL_TALIGN)
221 else if (cp->flags & TBL_CELL_BALIGN)
226 for (i = dp->hspans; i > 0; i--)
240 /* Print the element and the attributes. */
242 print_otag(h, TAG_TD, "??sss",
243 "colspan", hspans, "rowspan", vspans,
244 "vertical-align", valign,
245 "text-align", halign,
246 "border-right-style", rborder);
247 if (dp->layout->pos == TBL_CELL_HORIZ ||
248 dp->layout->pos == TBL_CELL_DHORIZ ||
249 dp->pos == TBL_DATA_HORIZ ||
250 dp->pos == TBL_DATA_DHORIZ)
251 print_otag(h, TAG_HR, "");
252 else if (dp->string != NULL) {
253 save_font = h->metac;
254 html_setfont(h, dp->layout->font);
255 if (dp->layout->pos == TBL_CELL_LONG)
256 print_text(h, "\\[u2003]"); /* em space */
257 print_text(h, dp->string);
258 if (dp->layout->pos == TBL_CELL_NUMBER) {
259 col = h->tbl.cols + dp->layout->col;
260 if (col->decimal < col->nwidth) {
261 if ((ccp = strrchr(dp->string,
262 sp->opts->decimal)) == NULL) {
263 /* Punctuation space. */
264 print_text(h, "\\[u2008]");
265 ccp = strchr(dp->string, '\0');
268 sz = col->nwidth - col->decimal;
279 html_setfont(h, save_font);
285 h->flags &= ~HTML_NONOSPACE;
287 if (sp->next == NULL) {