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

Annotation of mandoc/tbl_html.c, Revision 1.26

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

CVSweb