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