Annotation of src/usr.bin/mandoc/tbl_html.c, Revision 1.16
1.16 ! schwarze 1: /* $OpenBSD: tbl_html.c,v 1.15 2017/02/05 18:13:28 schwarze Exp $ */
1.1 schwarze 2: /*
1.5 schwarze 3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.14 schwarze 4: * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 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: */
1.8 schwarze 18: #include <sys/types.h>
19:
1.1 schwarze 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:
1.4 schwarze 29: static void html_tblopen(struct html *, const struct tbl_span *);
1.2 schwarze 30: static size_t html_tbl_len(size_t, void *);
31: static size_t html_tbl_strlen(const char *, void *);
1.16 ! schwarze 32: static size_t html_tbl_sulen(const struct roffsu *, void *);
1.2 schwarze 33:
1.7 schwarze 34:
1.2 schwarze 35: static size_t
36: html_tbl_len(size_t sz, void *arg)
37: {
1.12 schwarze 38: return sz;
1.2 schwarze 39: }
40:
41: static size_t
42: html_tbl_strlen(const char *p, void *arg)
43: {
1.16 ! schwarze 44: return strlen(p);
! 45: }
1.2 schwarze 46:
1.16 ! schwarze 47: static size_t
! 48: html_tbl_sulen(const struct roffsu *su, void *arg)
! 49: {
! 50: switch (su->unit) {
! 51: case SCALE_FS: /* 2^16 basic units */
! 52: return su->scale * 65536.0 / 24.0;
! 53: case SCALE_IN: /* 10 characters per inch */
! 54: return su->scale * 10.0;
! 55: case SCALE_CM: /* 2.54 cm per inch */
! 56: return su->scale * 10.0 / 2.54;
! 57: case SCALE_PC: /* 6 pica per inch */
! 58: case SCALE_VS:
! 59: return su->scale * 10.0 / 6.0;
! 60: case SCALE_EN:
! 61: case SCALE_EM:
! 62: return su->scale;
! 63: case SCALE_PT: /* 12 points per pica */
! 64: return su->scale * 10.0 / 6.0 / 12.0;
! 65: case SCALE_BU: /* 24 basic units per character */
! 66: return su->scale / 24.0;
! 67: case SCALE_MM: /* 1/1000 inch */
! 68: return su->scale / 100.0;
! 69: default:
! 70: abort();
! 71: }
1.2 schwarze 72: }
73:
1.4 schwarze 74: static void
75: html_tblopen(struct html *h, const struct tbl_span *sp)
76: {
1.15 schwarze 77: struct tag *t;
1.10 schwarze 78: int ic;
1.4 schwarze 79:
1.11 schwarze 80: if (h->tbl.cols == NULL) {
1.4 schwarze 81: h->tbl.len = html_tbl_len;
82: h->tbl.slen = html_tbl_strlen;
1.16 ! schwarze 83: h->tbl.sulen = html_tbl_sulen;
1.8 schwarze 84: tblcalc(&h->tbl, sp, 0);
1.4 schwarze 85: }
86:
87: assert(NULL == h->tblt);
1.14 schwarze 88: h->tblt = print_otag(h, TAG_TABLE, "c", "tbl");
1.4 schwarze 89:
1.15 schwarze 90: t = print_otag(h, TAG_COLGROUP, "");
1.14 schwarze 91: for (ic = 0; ic < sp->opts->cols; ic++)
92: print_otag(h, TAG_COL, "shw", h->tbl.cols[ic].width);
1.15 schwarze 93: print_tagq(h, t);
1.4 schwarze 94: }
95:
96: void
97: print_tblclose(struct html *h)
98: {
99:
100: assert(h->tblt);
101: print_tagq(h, h->tblt);
102: h->tblt = NULL;
103: }
104:
1.1 schwarze 105: void
106: print_tbl(struct html *h, const struct tbl_span *sp)
107: {
108: const struct tbl_dat *dp;
1.2 schwarze 109: struct tag *tt;
1.10 schwarze 110: int ic;
1.2 schwarze 111:
112: /* Inhibit printing of spaces: we do padding ourselves. */
113:
1.9 schwarze 114: if (h->tblt == NULL)
1.4 schwarze 115: html_tblopen(h, sp);
116:
117: assert(h->tblt);
118:
1.2 schwarze 119: h->flags |= HTML_NONOSPACE;
120: h->flags |= HTML_NOSPACE;
121:
1.14 schwarze 122: tt = print_otag(h, TAG_TR, "");
1.1 schwarze 123:
124: switch (sp->pos) {
1.7 schwarze 125: case TBL_SPAN_HORIZ:
126: case TBL_SPAN_DHORIZ:
1.14 schwarze 127: print_otag(h, TAG_TD, "?", "colspan", "0");
1.2 schwarze 128: break;
1.1 schwarze 129: default:
1.2 schwarze 130: dp = sp->first;
1.10 schwarze 131: for (ic = 0; ic < sp->opts->cols; ic++) {
1.4 schwarze 132: print_stagq(h, tt);
1.14 schwarze 133: print_otag(h, TAG_TD, "");
1.4 schwarze 134:
1.10 schwarze 135: if (dp == NULL || dp->layout->col > ic)
136: continue;
1.9 schwarze 137: if (dp->layout->pos != TBL_CELL_DOWN)
138: if (dp->string != NULL)
1.6 schwarze 139: print_text(h, dp->string);
140: dp = dp->next;
1.2 schwarze 141: }
1.1 schwarze 142: break;
143: }
144:
1.4 schwarze 145: print_tagq(h, tt);
146:
1.2 schwarze 147: h->flags &= ~HTML_NONOSPACE;
1.1 schwarze 148:
1.11 schwarze 149: if (sp->next == NULL) {
1.2 schwarze 150: assert(h->tbl.cols);
151: free(h->tbl.cols);
152: h->tbl.cols = NULL;
1.4 schwarze 153: print_tblclose(h);
1.1 schwarze 154: }
1.4 schwarze 155:
1.1 schwarze 156: }