[BACK]Return to tbl_html.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/tbl_html.c, Revision 1.22

1.22    ! schwarze    1: /*     $OpenBSD: tbl_html.c,v 1.21 2018/11/25 19:23:59 schwarze Exp $ */
1.1       schwarze    2: /*
1.5       schwarze    3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.20      schwarze    4:  * Copyright (c) 2014, 2015, 2017, 2018 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: {
1.18      schwarze   50:        if (su->scale < 0.0)
                     51:                return 0;
                     52:
1.16      schwarze   53:        switch (su->unit) {
                     54:        case SCALE_FS:  /* 2^16 basic units */
                     55:                return su->scale * 65536.0 / 24.0;
                     56:        case SCALE_IN:  /* 10 characters per inch */
                     57:                return su->scale * 10.0;
                     58:        case SCALE_CM:  /* 2.54 cm per inch */
                     59:                return su->scale * 10.0 / 2.54;
                     60:        case SCALE_PC:  /* 6 pica per inch */
                     61:        case SCALE_VS:
                     62:                return su->scale * 10.0 / 6.0;
                     63:        case SCALE_EN:
                     64:        case SCALE_EM:
                     65:                return su->scale;
                     66:        case SCALE_PT:  /* 12 points per pica */
                     67:                return su->scale * 10.0 / 6.0 / 12.0;
                     68:        case SCALE_BU:  /* 24 basic units per character */
                     69:                return su->scale / 24.0;
                     70:        case SCALE_MM:  /* 1/1000 inch */
                     71:                return su->scale / 100.0;
                     72:        default:
                     73:                abort();
                     74:        }
1.2       schwarze   75: }
                     76:
1.4       schwarze   77: static void
                     78: html_tblopen(struct html *h, const struct tbl_span *sp)
                     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.17      schwarze   84:                tblcalc(&h->tbl, sp, 0, 0);
1.4       schwarze   85:        }
                     86:        assert(NULL == h->tblt);
1.14      schwarze   87:        h->tblt = print_otag(h, TAG_TABLE, "c", "tbl");
1.4       schwarze   88: }
                     89:
                     90: void
                     91: print_tblclose(struct html *h)
                     92: {
                     93:
                     94:        assert(h->tblt);
                     95:        print_tagq(h, h->tblt);
                     96:        h->tblt = NULL;
                     97: }
                     98:
1.1       schwarze   99: void
                    100: print_tbl(struct html *h, const struct tbl_span *sp)
                    101: {
1.21      schwarze  102:        const struct tbl_dat    *dp;
                    103:        struct tag              *tt;
                    104:        const char              *hspans, *vspans, *halign, *valign;
                    105:        char                     hbuf[4], vbuf[4];
1.2       schwarze  106:
                    107:        /* Inhibit printing of spaces: we do padding ourselves. */
                    108:
1.9       schwarze  109:        if (h->tblt == NULL)
1.4       schwarze  110:                html_tblopen(h, sp);
                    111:
                    112:        assert(h->tblt);
                    113:
1.2       schwarze  114:        h->flags |= HTML_NONOSPACE;
                    115:        h->flags |= HTML_NOSPACE;
                    116:
1.14      schwarze  117:        tt = print_otag(h, TAG_TR, "");
1.1       schwarze  118:
                    119:        switch (sp->pos) {
1.7       schwarze  120:        case TBL_SPAN_HORIZ:
                    121:        case TBL_SPAN_DHORIZ:
1.14      schwarze  122:                print_otag(h, TAG_TD, "?", "colspan", "0");
1.2       schwarze  123:                break;
1.1       schwarze  124:        default:
1.21      schwarze  125:                for (dp = sp->first; dp != NULL; dp = dp->next) {
1.4       schwarze  126:                        print_stagq(h, tt);
1.22    ! schwarze  127:
        !           128:                        /*
        !           129:                         * Do not generate <td> elements for continuations
        !           130:                         * of spanned cells.  Larger <td> elements covering
        !           131:                         * this space were already generated earlier.
        !           132:                         */
        !           133:
        !           134:                        if (dp->layout->pos == TBL_CELL_SPAN ||
        !           135:                            dp->layout->pos == TBL_CELL_DOWN ||
        !           136:                            (dp->string != NULL &&
        !           137:                             strcmp(dp->string, "\\^") == 0))
1.10      schwarze  138:                                continue;
1.21      schwarze  139:
                    140:                        /* Determine the attribute values. */
                    141:
                    142:                        if (dp->hspans > 0) {
                    143:                                (void)snprintf(hbuf, sizeof(hbuf),
                    144:                                    "%d", dp->hspans + 1);
                    145:                                hspans = hbuf;
                    146:                        } else
                    147:                                hspans = NULL;
                    148:                        if (dp->vspans > 0) {
                    149:                                (void)snprintf(vbuf, sizeof(vbuf),
                    150:                                    "%d", dp->vspans + 1);
                    151:                                vspans = vbuf;
                    152:                        } else
                    153:                                vspans = NULL;
                    154:
1.20      schwarze  155:                        switch (dp->layout->pos) {
                    156:                        case TBL_CELL_CENTRE:
                    157:                                halign = "center";
                    158:                                break;
                    159:                        case TBL_CELL_RIGHT:
                    160:                        case TBL_CELL_NUMBER:
                    161:                                halign = "right";
                    162:                                break;
                    163:                        default:
                    164:                                halign = NULL;
                    165:                                break;
                    166:                        }
                    167:                        if (dp->layout->flags & TBL_CELL_TALIGN)
                    168:                                valign = "top";
                    169:                        else if (dp->layout->flags & TBL_CELL_BALIGN)
                    170:                                valign = "bottom";
                    171:                        else
                    172:                                valign = NULL;
1.21      schwarze  173:
                    174:                        /* Print the element and the attributes. */
                    175:
1.20      schwarze  176:                        if (halign == NULL && valign == NULL)
1.21      schwarze  177:                                print_otag(h, TAG_TD, "??",
                    178:                                    "colspan", hspans, "rowspan", vspans);
1.20      schwarze  179:                        else if (halign == NULL)
1.21      schwarze  180:                                print_otag(h, TAG_TD, "??s",
                    181:                                    "colspan", hspans, "rowspan", vspans,
1.20      schwarze  182:                                    "vertical-align", valign);
                    183:                        else if (valign == NULL)
1.21      schwarze  184:                                print_otag(h, TAG_TD, "??s",
                    185:                                    "colspan", hspans, "rowspan", vspans,
1.20      schwarze  186:                                    "text-align", halign);
                    187:                        else
1.21      schwarze  188:                                print_otag(h, TAG_TD, "??ss",
                    189:                                    "colspan", hspans, "rowspan", vspans,
1.20      schwarze  190:                                    "vertical-align", valign,
                    191:                                    "text-align", halign);
1.21      schwarze  192:                        if (dp->string != NULL)
                    193:                                print_text(h, dp->string);
1.2       schwarze  194:                }
1.1       schwarze  195:                break;
                    196:        }
                    197:
1.4       schwarze  198:        print_tagq(h, tt);
                    199:
1.2       schwarze  200:        h->flags &= ~HTML_NONOSPACE;
1.1       schwarze  201:
1.11      schwarze  202:        if (sp->next == NULL) {
1.2       schwarze  203:                assert(h->tbl.cols);
                    204:                free(h->tbl.cols);
                    205:                h->tbl.cols = NULL;
1.4       schwarze  206:                print_tblclose(h);
1.1       schwarze  207:        }
                    208: }