]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_html.c
1 /* $Id: tbl_html.c,v 1.40 2022/04/14 16:43:44 schwarze Exp $ */
3 * Copyright (c) 2014,2015,2017,2018,2021 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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>
28 #include "mandoc_dbg.h"
36 static void html_tblopen(struct html
*, const struct tbl_span
*);
37 static size_t html_tbl_len(size_t, void *);
38 static size_t html_tbl_strlen(const char *, void *);
39 static size_t html_tbl_sulen(const struct roffsu
*, void *);
43 html_tbl_len(size_t sz
, void *arg
)
49 html_tbl_strlen(const char *p
, void *arg
)
55 html_tbl_sulen(const struct roffsu
*su
, void *arg
)
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 */
69 return su
->scale
* 10.0 / 6.0;
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;
85 html_tblopen(struct html
*h
, const struct tbl_span
*sp
)
87 html_close_paragraph(h
);
88 if (h
->tbl
.cols
== NULL
) {
89 h
->tbl
.len
= html_tbl_len
;
90 h
->tbl
.slen
= html_tbl_strlen
;
91 h
->tbl
.sulen
= html_tbl_sulen
;
92 tblcalc(&h
->tbl
, sp
, 0, 0);
94 assert(NULL
== h
->tblt
);
95 h
->tblt
= print_otag(h
, TAG_TABLE
, "c?ss", "tbl",
97 sp
->opts
->opts
& TBL_OPT_ALLBOX
? "1" : NULL
,
99 sp
->opts
->opts
& TBL_OPT_DBOX
? "double" :
100 sp
->opts
->opts
& TBL_OPT_BOX
? "solid" : NULL
,
102 sp
->pos
== TBL_SPAN_DHORIZ
? "double" :
103 sp
->pos
== TBL_SPAN_HORIZ
? "solid" : NULL
);
107 print_tblclose(struct html
*h
)
111 print_tagq(h
, h
->tblt
);
116 print_tbl(struct html
*h
, const struct tbl_span
*sp
)
118 const struct tbl_dat
*dp
;
119 const struct tbl_cell
*cp
;
120 const struct tbl_span
*psp
;
121 const struct roffcol
*col
;
123 const char *hspans
, *vspans
, *halign
, *valign
;
124 const char *bborder
, *lborder
, *rborder
;
126 char hbuf
[4], vbuf
[4];
128 enum mandoc_esc save_font
;
135 * Horizontal lines spanning the whole table
136 * are handled by previous or following table rows.
139 if (sp
->pos
!= TBL_SPAN_DATA
)
142 /* Inhibit printing of spaces: we do padding ourselves. */
144 h
->flags
|= HTML_NONOSPACE
;
145 h
->flags
|= HTML_NOSPACE
;
147 /* Draw a vertical line left of this row? */
149 switch (sp
->layout
->vert
) {
161 /* Draw a horizontal line below this row? */
164 if ((psp
= sp
->next
) != NULL
) {
166 case TBL_SPAN_DHORIZ
:
177 tt
= print_otag(h
, TAG_TR
, "ss",
178 "border-left-style", lborder
,
179 "border-bottom-style", bborder
);
181 for (dp
= sp
->first
; dp
!= NULL
; dp
= dp
->next
) {
185 * Do not generate <td> elements for continuations
186 * of spanned cells. Larger <td> elements covering
187 * this space were already generated earlier.
191 if (cp
->pos
== TBL_CELL_SPAN
|| cp
->pos
== TBL_CELL_DOWN
||
192 (dp
->string
!= NULL
&& strcmp(dp
->string
, "\\^") == 0))
195 /* Determine the attribute values. */
197 if (dp
->hspans
> 0) {
198 (void)snprintf(hbuf
, sizeof(hbuf
),
199 "%d", dp
->hspans
+ 1);
203 if (dp
->vspans
> 0) {
204 (void)snprintf(vbuf
, sizeof(vbuf
),
205 "%d", dp
->vspans
+ 1);
211 case TBL_CELL_CENTRE
:
215 case TBL_CELL_NUMBER
:
222 if (cp
->flags
& TBL_CELL_TALIGN
)
224 else if (cp
->flags
& TBL_CELL_BALIGN
)
229 for (i
= dp
->hspans
; i
> 0; i
--)
243 /* Print the element and the attributes. */
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
);
250 if (dp
->layout
->pos
== TBL_CELL_HORIZ
||
251 dp
->layout
->pos
== TBL_CELL_DHORIZ
||
252 dp
->pos
== TBL_DATA_HORIZ
||
253 dp
->pos
== TBL_DATA_NHORIZ
||
254 dp
->pos
== TBL_DATA_DHORIZ
||
255 dp
->pos
== TBL_DATA_NDHORIZ
)
256 print_otag(h
, TAG_HR
, "");
257 else if (dp
->string
!= NULL
) {
258 save_font
= h
->metac
;
259 html_setfont(h
, dp
->layout
->font
);
260 if (dp
->layout
->pos
== TBL_CELL_LONG
)
261 print_text(h
, "\\[u2003]"); /* em space */
262 print_text(h
, dp
->string
);
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');
273 sz
= col
->nwidth
- col
->decimal
;
284 html_setfont(h
, save_font
);
290 h
->flags
&= ~HTML_NONOSPACE
;
292 if (sp
->next
== NULL
) {