[BACK]Return to tbl_html.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/tbl_html.c, Revision 1.39

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

CVSweb