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

Annotation of mandoc/term_ps.c, Revision 1.41

1.41    ! kristaps    1: /*     $Id: term_ps.c,v 1.40 2010/08/01 15:46:18 joerg Exp $ */
1.1       kristaps    2: /*
1.34      schwarze    3:  * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.4       kristaps   21: #include <sys/param.h>
                     22:
1.2       kristaps   23: #include <assert.h>
1.4       kristaps   24: #include <stdarg.h>
1.17      kristaps   25: #include <stdint.h>
1.2       kristaps   26: #include <stdio.h>
1.1       kristaps   27: #include <stdlib.h>
1.4       kristaps   28: #include <string.h>
1.13      kristaps   29: #include <time.h>
1.26      kristaps   30: #include <unistd.h>
1.1       kristaps   31:
                     32: #include "out.h"
                     33: #include "main.h"
                     34: #include "term.h"
                     35:
1.21      kristaps   36: /* Convert PostScript point "x" to an AFM unit. */
1.25      kristaps   37: #define        PNT2AFM(p, x) /* LINTED */ \
1.21      kristaps   38:        (size_t)((double)(x) * (1000.0 / (double)(p)->engine.ps.scale))
                     39:
                     40: /* Convert an AFM unit "x" to a PostScript points */
1.25      kristaps   41: #define        AFM2PNT(p, x) /* LINTED */ \
1.35      kristaps   42:        ((double)(x) / (1000.0 / (double)(p)->engine.ps.scale))
1.21      kristaps   43:
1.13      kristaps   44: struct glyph {
1.36      kristaps   45:        unsigned short    wx; /* WX in AFM */
1.13      kristaps   46: };
                     47:
                     48: struct font {
                     49:        const char       *name; /* FontName in AFM */
1.15      kristaps   50: #define        MAXCHAR           95 /* total characters we can handle */
1.13      kristaps   51:        struct glyph      gly[MAXCHAR]; /* glyph metrics */
                     52: };
                     53:
                     54: /*
                     55:  * We define, for the time being, three fonts: bold, oblique/italic, and
                     56:  * normal (roman).  The following table hard-codes the font metrics for
                     57:  * ASCII, i.e., 32--127.
                     58:  */
                     59:
1.16      kristaps   60: static const struct font fonts[TERMFONT__MAX] = {
1.24      kristaps   61:        { "Times-Roman", {
                     62:                { 250 },
                     63:                { 333 },
                     64:                { 408 },
                     65:                { 500 },
                     66:                { 500 },
                     67:                { 833 },
                     68:                { 778 },
                     69:                { 333 },
                     70:                { 333 },
                     71:                { 333 },
                     72:                { 500 },
                     73:                { 564 },
                     74:                { 250 },
                     75:                { 333 },
                     76:                { 250 },
                     77:                { 278 },
                     78:                { 500 },
                     79:                { 500 },
                     80:                { 500 },
                     81:                { 500 },
                     82:                { 500 },
                     83:                { 500 },
                     84:                { 500 },
                     85:                { 500 },
                     86:                { 500 },
                     87:                { 500 },
                     88:                { 278 },
                     89:                { 278 },
                     90:                { 564 },
                     91:                { 564 },
                     92:                { 564 },
                     93:                { 444 },
                     94:                { 921 },
                     95:                { 722 },
                     96:                { 667 },
                     97:                { 667 },
                     98:                { 722 },
                     99:                { 611 },
                    100:                { 556 },
                    101:                { 722 },
                    102:                { 722 },
                    103:                { 333 },
                    104:                { 389 },
                    105:                { 722 },
                    106:                { 611 },
                    107:                { 889 },
                    108:                { 722 },
                    109:                { 722 },
                    110:                { 556 },
                    111:                { 722 },
                    112:                { 667 },
                    113:                { 556 },
                    114:                { 611 },
                    115:                { 722 },
                    116:                { 722 },
                    117:                { 944 },
                    118:                { 722 },
                    119:                { 722 },
                    120:                { 611 },
                    121:                { 333 },
                    122:                { 278 },
                    123:                { 333 },
                    124:                { 469 },
                    125:                { 500 },
                    126:                { 333 },
                    127:                { 444 },
                    128:                { 500 },
                    129:                { 444 },
                    130:                {  500},
                    131:                {  444},
                    132:                {  333},
                    133:                {  500},
                    134:                {  500},
                    135:                {  278},
                    136:                {  278},
                    137:                {  500},
                    138:                {  278},
                    139:                {  778},
                    140:                {  500},
                    141:                {  500},
                    142:                {  500},
                    143:                {  500},
                    144:                {  333},
                    145:                {  389},
                    146:                {  278},
                    147:                {  500},
                    148:                {  500},
                    149:                {  722},
                    150:                {  500},
                    151:                {  500},
                    152:                {  444},
                    153:                {  480},
                    154:                {  200},
                    155:                {  480},
                    156:                {  541},
                    157:        } },
                    158:        { "Times-Bold", {
                    159:                { 250  },
                    160:                { 333  },
                    161:                { 555  },
                    162:                { 500  },
                    163:                { 500  },
                    164:                { 1000 },
                    165:                { 833  },
                    166:                { 333  },
                    167:                { 333  },
                    168:                { 333  },
                    169:                { 500  },
                    170:                { 570  },
                    171:                { 250  },
                    172:                { 333  },
                    173:                { 250  },
                    174:                { 278  },
                    175:                { 500  },
                    176:                { 500  },
                    177:                { 500  },
                    178:                { 500  },
                    179:                { 500  },
                    180:                { 500  },
                    181:                { 500  },
                    182:                { 500  },
                    183:                { 500  },
                    184:                { 500  },
                    185:                { 333  },
                    186:                { 333  },
                    187:                { 570  },
                    188:                { 570  },
                    189:                { 570  },
                    190:                { 500  },
                    191:                { 930  },
                    192:                { 722  },
                    193:                { 667  },
                    194:                { 722  },
                    195:                { 722  },
                    196:                { 667  },
                    197:                { 611  },
                    198:                { 778  },
                    199:                { 778  },
                    200:                { 389  },
                    201:                { 500  },
                    202:                { 778  },
                    203:                { 667  },
                    204:                { 944  },
                    205:                { 722  },
                    206:                { 778  },
                    207:                { 611  },
                    208:                { 778  },
                    209:                { 722  },
                    210:                { 556  },
                    211:                { 667  },
                    212:                { 722  },
                    213:                { 722  },
                    214:                { 1000 },
                    215:                { 722  },
                    216:                { 722  },
                    217:                { 667  },
                    218:                { 333  },
                    219:                { 278  },
                    220:                { 333  },
                    221:                { 581  },
                    222:                { 500  },
                    223:                { 333  },
                    224:                { 500  },
                    225:                { 556  },
                    226:                { 444  },
1.22      kristaps  227:                {  556 },
1.24      kristaps  228:                {  444 },
                    229:                {  333 },
1.22      kristaps  230:                {  500 },
1.24      kristaps  231:                {  556 },
1.22      kristaps  232:                {  278 },
1.24      kristaps  233:                {  333 },
                    234:                {  556 },
1.22      kristaps  235:                {  278 },
1.24      kristaps  236:                {  833 },
1.22      kristaps  237:                {  556 },
1.24      kristaps  238:                {  500 },
1.22      kristaps  239:                {  556 },
                    240:                {  556 },
                    241:                {  444 },
1.24      kristaps  242:                {  389 },
                    243:                {  333 },
1.22      kristaps  244:                {  556 },
                    245:                {  500 },
1.24      kristaps  246:                {  722 },
                    247:                {  500 },
1.22      kristaps  248:                {  500 },
1.24      kristaps  249:                {  444 },
                    250:                {  394 },
                    251:                {  220 },
                    252:                {  394 },
                    253:                {  520 },
1.13      kristaps  254:        } },
1.24      kristaps  255:        { "Times-Italic", {
                    256:                { 250  },
                    257:                { 333  },
                    258:                { 420  },
                    259:                { 500  },
                    260:                { 500  },
                    261:                { 833  },
                    262:                { 778  },
                    263:                { 333  },
                    264:                { 333  },
                    265:                { 333  },
                    266:                { 500  },
                    267:                { 675  },
                    268:                { 250  },
                    269:                { 333  },
                    270:                { 250  },
                    271:                { 278  },
                    272:                { 500  },
                    273:                { 500  },
                    274:                { 500  },
                    275:                { 500  },
                    276:                { 500  },
                    277:                { 500  },
                    278:                { 500  },
                    279:                { 500  },
                    280:                { 500  },
                    281:                { 500  },
                    282:                { 333  },
                    283:                { 333  },
                    284:                { 675  },
                    285:                { 675  },
                    286:                { 675  },
                    287:                { 500  },
                    288:                { 920  },
                    289:                { 611  },
                    290:                { 611  },
                    291:                { 667  },
                    292:                { 722  },
                    293:                { 611  },
                    294:                { 611  },
                    295:                { 722  },
                    296:                { 722  },
                    297:                { 333  },
                    298:                { 444  },
                    299:                { 667  },
                    300:                { 556  },
                    301:                { 833  },
                    302:                { 667  },
                    303:                { 722  },
                    304:                { 611  },
                    305:                { 722  },
                    306:                { 611  },
                    307:                { 500  },
                    308:                { 556  },
                    309:                { 722  },
                    310:                { 611  },
                    311:                { 833  },
                    312:                { 611  },
                    313:                { 556  },
                    314:                { 556  },
                    315:                { 389  },
                    316:                { 278  },
                    317:                { 389  },
                    318:                { 422  },
                    319:                { 500  },
                    320:                { 333  },
                    321:                { 500  },
                    322:                { 500  },
                    323:                { 444  },
1.22      kristaps  324:                {  500 },
1.24      kristaps  325:                {  444 },
                    326:                {  278 },
1.22      kristaps  327:                {  500 },
                    328:                {  500 },
                    329:                {  278 },
1.24      kristaps  330:                {  278 },
                    331:                {  444 },
                    332:                {  278 },
                    333:                {  722 },
                    334:                {  500 },
                    335:                {  500 },
                    336:                {  500 },
1.22      kristaps  337:                {  500 },
1.24      kristaps  338:                {  389 },
                    339:                {  389 },
1.22      kristaps  340:                {  278 },
                    341:                {  500 },
1.24      kristaps  342:                {  444 },
                    343:                {  667 },
                    344:                {  444 },
                    345:                {  444 },
                    346:                {  389 },
                    347:                {  400 },
1.22      kristaps  348:                {  275 },
1.24      kristaps  349:                {  400 },
                    350:                {  541 },
1.13      kristaps  351:        } },
                    352: };
                    353:
1.15      kristaps  354: /* These work the buffer used by the header and footer. */
1.4       kristaps  355: #define        PS_BUFSLOP        128
                    356:
1.40      joerg     357: static void
                    358: ps_growbuf(struct termp *p, size_t sz)
                    359: {
                    360:        if (p->engine.ps.psmargcur + sz <= p->engine.ps.psmargsz)
                    361:                return;
                    362:
                    363:        if (sz < PS_BUFSLOP)
                    364:                sz = PS_BUFSLOP;
1.41    ! kristaps  365:
1.40      joerg     366:        p->engine.ps.psmargsz += sz;
                    367:
1.41    ! kristaps  368:        p->engine.ps.psmarg = realloc
        !           369:                (p->engine.ps.psmarg,
        !           370:                 p->engine.ps.psmargsz);
        !           371:
        !           372:        if (NULL == p->engine.ps.psmarg) {
1.40      joerg     373:                perror(NULL);
                    374:                exit(EXIT_FAILURE);
1.41    ! kristaps  375:        }
1.40      joerg     376: }
1.9       kristaps  377:
1.20      kristaps  378: static double            ps_hspan(const struct termp *,
1.19      kristaps  379:                                const struct roffsu *);
                    380: static size_t            ps_width(const struct termp *, char);
                    381: static void              ps_advance(struct termp *, size_t);
1.2       kristaps  382: static void              ps_begin(struct termp *);
1.37      kristaps  383: static void              ps_closepage(struct termp *);
1.2       kristaps  384: static void              ps_end(struct termp *);
                    385: static void              ps_endline(struct termp *);
1.9       kristaps  386: static void              ps_fclose(struct termp *);
1.19      kristaps  387: static void              ps_letter(struct termp *, char);
1.9       kristaps  388: static void              ps_pclose(struct termp *);
1.13      kristaps  389: static void              ps_pletter(struct termp *, int);
1.4       kristaps  390: static void              ps_printf(struct termp *, const char *, ...);
                    391: static void              ps_putchar(struct termp *, char);
1.9       kristaps  392: static void              ps_setfont(struct termp *, enum termfont);
1.37      kristaps  393: static struct termp     *pspdf_alloc(char *);
1.39      kristaps  394: static void              pdf_obj(struct termp *, size_t);
1.37      kristaps  395:
                    396:
                    397: void *
                    398: pdf_alloc(char *outopts)
                    399: {
                    400:        struct termp    *p;
                    401:
1.38      kristaps  402:        if (NULL != (p = pspdf_alloc(outopts)))
                    403:                p->type = TERMTYPE_PDF;
1.37      kristaps  404:
                    405:        return(p);
                    406: }
1.2       kristaps  407:
                    408:
1.1       kristaps  409: void *
1.17      kristaps  410: ps_alloc(char *outopts)
1.1       kristaps  411: {
                    412:        struct termp    *p;
1.37      kristaps  413:
1.38      kristaps  414:        if (NULL != (p = pspdf_alloc(outopts)))
                    415:                p->type = TERMTYPE_PS;
1.37      kristaps  416:
                    417:        return(p);
                    418: }
                    419:
                    420:
                    421: static struct termp *
                    422: pspdf_alloc(char *outopts)
                    423: {
                    424:        struct termp    *p;
1.28      kristaps  425:        size_t           pagex, pagey, marginx, marginy, lineheight;
1.17      kristaps  426:        const char      *toks[2];
1.25      kristaps  427:        const char      *pp;
1.17      kristaps  428:        char            *v;
1.1       kristaps  429:
                    430:        if (NULL == (p = term_alloc(TERMENC_ASCII)))
                    431:                return(NULL);
                    432:
1.19      kristaps  433:        p->advance = ps_advance;
1.2       kristaps  434:        p->begin = ps_begin;
                    435:        p->end = ps_end;
                    436:        p->endline = ps_endline;
1.19      kristaps  437:        p->hspan = ps_hspan;
                    438:        p->letter = ps_letter;
1.11      kristaps  439:        p->width = ps_width;
1.21      kristaps  440:
1.17      kristaps  441:        toks[0] = "paper";
                    442:        toks[1] = NULL;
                    443:
1.25      kristaps  444:        pp = NULL;
1.19      kristaps  445:
1.17      kristaps  446:        while (outopts && *outopts)
                    447:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    448:                case (0):
1.25      kristaps  449:                        pp = v;
1.17      kristaps  450:                        break;
                    451:                default:
                    452:                        break;
                    453:                }
1.16      kristaps  454:
1.25      kristaps  455:        /* Default to US letter (millimetres). */
                    456:
1.31      kristaps  457:        pagex = 216;
                    458:        pagey = 279;
1.25      kristaps  459:
                    460:        /*
                    461:         * The ISO-269 paper sizes can be calculated automatically, but
                    462:         * it would require bringing in -lm for pow() and I'd rather not
                    463:         * do that.  So just do it the easy way for now.  Since this
                    464:         * only happens once, I'm not terribly concerned.
                    465:         */
                    466:
                    467:        if (pp && strcasecmp(pp, "letter")) {
                    468:                if (0 == strcasecmp(pp, "a3")) {
                    469:                        pagex = 297;
                    470:                        pagey = 420;
                    471:                } else if (0 == strcasecmp(pp, "a4")) {
                    472:                        pagex = 210;
                    473:                        pagey = 297;
                    474:                } else if (0 == strcasecmp(pp, "a5")) {
                    475:                        pagex = 148;
                    476:                        pagey = 210;
                    477:                } else if (0 == strcasecmp(pp, "legal")) {
                    478:                        pagex = 216;
                    479:                        pagey = 356;
                    480:                } else if (2 != sscanf(pp, "%zux%zu", &pagex, &pagey))
                    481:                        fprintf(stderr, "%s: Unknown paper\n", pp);
1.27      kristaps  482:        } else if (NULL == pp)
                    483:                pp = "letter";
                    484:
                    485:        /*
                    486:         * This MUST be defined before any PNT2AFM or AFM2PNT
                    487:         * calculations occur.
                    488:         */
                    489:
                    490:        p->engine.ps.scale = 11;
                    491:
1.25      kristaps  492:        /* Remember millimetres -> AFM units. */
                    493:
                    494:        pagex = PNT2AFM(p, ((double)pagex * 2.834));
                    495:        pagey = PNT2AFM(p, ((double)pagey * 2.834));
                    496:
1.28      kristaps  497:        /* Margins are 1/9 the page x and y. */
1.27      kristaps  498:
1.29      kristaps  499:        marginx = /* LINTED */
                    500:                (size_t)((double)pagex / 9.0);
                    501:        marginy = /* LINTED */
                    502:                (size_t)((double)pagey / 9.0);
1.27      kristaps  503:
1.31      kristaps  504:        /* Line-height is 1.4em. */
                    505:
                    506:        lineheight = PNT2AFM(p, ((double)p->engine.ps.scale * 1.4));
1.16      kristaps  507:
                    508:        p->engine.ps.width = pagex;
                    509:        p->engine.ps.height = pagey;
1.28      kristaps  510:        p->engine.ps.header = pagey - (marginy / 2) - (lineheight / 2);
                    511:        p->engine.ps.top = pagey - marginy;
                    512:        p->engine.ps.footer = (marginy / 2) - (lineheight / 2);
                    513:        p->engine.ps.bottom = marginy;
                    514:        p->engine.ps.left = marginx;
1.18      kristaps  515:        p->engine.ps.lineheight = lineheight;
1.16      kristaps  516:
1.28      kristaps  517:        p->defrmargin = pagex - (marginx * 2);
1.1       kristaps  518:        return(p);
                    519: }
                    520:
                    521:
                    522: void
1.37      kristaps  523: pspdf_free(void *arg)
1.1       kristaps  524: {
1.4       kristaps  525:        struct termp    *p;
                    526:
                    527:        p = (struct termp *)arg;
                    528:
                    529:        if (p->engine.ps.psmarg)
                    530:                free(p->engine.ps.psmarg);
1.39      kristaps  531:        if (p->engine.ps.pdfobjs)
                    532:                free(p->engine.ps.pdfobjs);
1.4       kristaps  533:
                    534:        term_free(p);
                    535: }
                    536:
                    537:
                    538: static void
                    539: ps_printf(struct termp *p, const char *fmt, ...)
                    540: {
                    541:        va_list          ap;
1.37      kristaps  542:        int              pos, len;
1.4       kristaps  543:
                    544:        va_start(ap, fmt);
                    545:
                    546:        /*
                    547:         * If we're running in regular mode, then pipe directly into
                    548:         * vprintf().  If we're processing margins, then push the data
                    549:         * into our growable margin buffer.
                    550:         */
1.1       kristaps  551:
1.29      kristaps  552:        if ( ! (PS_MARGINS & p->engine.ps.flags)) {
1.37      kristaps  553:                len = vprintf(fmt, ap);
1.4       kristaps  554:                va_end(ap);
1.37      kristaps  555:                p->engine.ps.pdfbytes += /* LINTED */
                    556:                        len < 0 ? 0 : (size_t)len;
1.4       kristaps  557:                return;
                    558:        }
                    559:
                    560:        /*
                    561:         * XXX: I assume that the in-margin print won't exceed
                    562:         * PS_BUFSLOP (128 bytes), which is reasonable but still an
                    563:         * assumption that will cause pukeage if it's not the case.
                    564:         */
                    565:
1.40      joerg     566:        ps_growbuf(p, PS_BUFSLOP);
1.4       kristaps  567:
                    568:        pos = (int)p->engine.ps.psmargcur;
1.37      kristaps  569:        len = vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
1.5       kristaps  570:
                    571:        va_end(ap);
1.37      kristaps  572:
                    573:        p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
1.4       kristaps  574: }
                    575:
                    576:
                    577: static void
                    578: ps_putchar(struct termp *p, char c)
                    579: {
                    580:        int              pos;
                    581:
                    582:        /* See ps_printf(). */
                    583:
1.29      kristaps  584:        if ( ! (PS_MARGINS & p->engine.ps.flags)) {
1.4       kristaps  585:                putchar(c);
1.37      kristaps  586:                p->engine.ps.pdfbytes++;
1.4       kristaps  587:                return;
                    588:        }
                    589:
1.40      joerg     590:        ps_growbuf(p, 2);
1.4       kristaps  591:
                    592:        pos = (int)p->engine.ps.psmargcur++;
1.5       kristaps  593:        p->engine.ps.psmarg[pos++] = c;
1.4       kristaps  594:        p->engine.ps.psmarg[pos] = '\0';
1.2       kristaps  595: }
                    596:
                    597:
1.37      kristaps  598: static void
1.39      kristaps  599: pdf_obj(struct termp *p, size_t obj)
                    600: {
                    601:
                    602:        assert(obj > 0);
                    603:
                    604:        if ((obj - 1) >= p->engine.ps.pdfobjsz) {
                    605:                p->engine.ps.pdfobjsz = obj + 128;
                    606:                p->engine.ps.pdfobjs = realloc
                    607:                        (p->engine.ps.pdfobjs,
                    608:                         p->engine.ps.pdfobjsz * sizeof(size_t));
                    609:                if (NULL == p->engine.ps.pdfobjs) {
                    610:                        perror(NULL);
                    611:                        exit(EXIT_FAILURE);
                    612:                }
                    613:        }
                    614:
                    615:        p->engine.ps.pdfobjs[(int)obj - 1] = p->engine.ps.pdfbytes;
                    616:        ps_printf(p, "%zu 0 obj\n", obj);
                    617: }
                    618:
                    619:
                    620: static void
1.37      kristaps  621: ps_closepage(struct termp *p)
                    622: {
                    623:        int              i;
1.38      kristaps  624:        size_t           len, base;
                    625:
                    626:        /*
                    627:         * Close out a page that we've already flushed to output.  In
                    628:         * PostScript, we simply note that the page must be showed.  In
                    629:         * PDF, we must now create the Length, Resource, and Page node
                    630:         * for the page contents.
                    631:         */
1.37      kristaps  632:
                    633:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    634:        ps_printf(p, "%s", p->engine.ps.psmarg);
                    635:
1.38      kristaps  636:        if (TERMTYPE_PS != p->type) {
1.37      kristaps  637:                ps_printf(p, "ET\n");
1.38      kristaps  638:
1.37      kristaps  639:                len = p->engine.ps.pdfbytes - p->engine.ps.pdflastpg;
1.38      kristaps  640:                base = p->engine.ps.pages * 4 + p->engine.ps.pdfbody;
                    641:
                    642:                ps_printf(p, "endstream\nendobj\n");
                    643:
                    644:                /* Length of content. */
1.39      kristaps  645:                pdf_obj(p, base + 1);
1.38      kristaps  646:                ps_printf(p, "%zu\nendobj\n", len);
                    647:
                    648:                /* Resource for content. */
1.39      kristaps  649:                pdf_obj(p, base + 2);
1.38      kristaps  650:                ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
1.37      kristaps  651:                ps_printf(p, "/Font <<\n");
1.39      kristaps  652:                for (i = 0; i < (int)TERMFONT__MAX; i++)
1.37      kristaps  653:                        ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
1.38      kristaps  654:                ps_printf(p, ">>\n>>\n");
                    655:
                    656:                /* Page node. */
1.39      kristaps  657:                pdf_obj(p, base + 3);
                    658:                ps_printf(p, "<<\n");
1.37      kristaps  659:                ps_printf(p, "/Type /Page\n");
                    660:                ps_printf(p, "/Parent 2 0 R\n");
1.38      kristaps  661:                ps_printf(p, "/Resources %zu 0 R\n", base + 2);
                    662:                ps_printf(p, "/Contents %zu 0 R\n", base);
                    663:                ps_printf(p, ">>\nendobj\n");
                    664:        } else
                    665:                ps_printf(p, "showpage\n");
1.37      kristaps  666:
                    667:        p->engine.ps.pages++;
                    668:        p->engine.ps.psrow = p->engine.ps.top;
                    669:        assert( ! (PS_NEWPAGE & p->engine.ps.flags));
                    670:        p->engine.ps.flags |= PS_NEWPAGE;
                    671: }
                    672:
                    673:
1.3       kristaps  674: /* ARGSUSED */
1.2       kristaps  675: static void
                    676: ps_end(struct termp *p)
                    677: {
1.38      kristaps  678:        size_t           i, xref, base;
1.2       kristaps  679:
1.4       kristaps  680:        /*
                    681:         * At the end of the file, do one last showpage.  This is the
                    682:         * same behaviour as groff(1) and works for multiple pages as
                    683:         * well as just one.
                    684:         */
                    685:
1.30      kristaps  686:        if ( ! (PS_NEWPAGE & p->engine.ps.flags)) {
                    687:                assert(0 == p->engine.ps.flags);
                    688:                assert('\0' == p->engine.ps.last);
1.37      kristaps  689:                ps_closepage(p);
1.30      kristaps  690:        }
1.16      kristaps  691:
1.37      kristaps  692:        if (TERMTYPE_PS == p->type) {
                    693:                ps_printf(p, "%%%%Trailer\n");
                    694:                ps_printf(p, "%%%%Pages: %zu\n", p->engine.ps.pages);
                    695:                ps_printf(p, "%%%%EOF\n");
                    696:                return;
                    697:        }
                    698:
1.39      kristaps  699:        pdf_obj(p, 2);
                    700:        ps_printf(p, "<<\n/Type /Pages\n");
1.37      kristaps  701:        ps_printf(p, "/MediaBox [0 0 %zu %zu]\n",
                    702:                        (size_t)AFM2PNT(p, p->engine.ps.width),
                    703:                        (size_t)AFM2PNT(p, p->engine.ps.height));
                    704:
                    705:        ps_printf(p, "/Count %zu\n", p->engine.ps.pages);
                    706:        ps_printf(p, "/Kids [");
                    707:
                    708:        for (i = 0; i < p->engine.ps.pages; i++)
1.38      kristaps  709:                ps_printf(p, " %zu 0 R", i * 4 +
                    710:                                p->engine.ps.pdfbody + 3);
                    711:
                    712:        base = (p->engine.ps.pages - 1) * 4 +
                    713:                p->engine.ps.pdfbody + 4;
                    714:
                    715:        ps_printf(p, "]\n>>\nendobj\n");
1.39      kristaps  716:        pdf_obj(p, base);
1.37      kristaps  717:        ps_printf(p, "<<\n");
                    718:        ps_printf(p, "/Type /Catalog\n");
                    719:        ps_printf(p, "/Pages 2 0 R\n");
                    720:        ps_printf(p, ">>\n");
                    721:        xref = p->engine.ps.pdfbytes;
                    722:        ps_printf(p, "xref\n");
1.38      kristaps  723:        ps_printf(p, "0 %zu\n", base + 1);
1.39      kristaps  724:        ps_printf(p, "0000000000 65535 f \n");
                    725:
                    726:        for (i = 0; i < base; i++)
                    727:                ps_printf(p, "%.10zu 00000 n \n",
                    728:                                p->engine.ps.pdfobjs[(int)i]);
                    729:
1.37      kristaps  730:        ps_printf(p, "trailer\n");
                    731:        ps_printf(p, "<<\n");
1.38      kristaps  732:        ps_printf(p, "/Size %zu\n", base + 1);
                    733:        ps_printf(p, "/Root %zu 0 R\n", base);
1.37      kristaps  734:        ps_printf(p, "/Info 1 0 R\n");
                    735:        ps_printf(p, ">>\n");
                    736:        ps_printf(p, "startxref\n");
                    737:        ps_printf(p, "%zu\n", xref);
                    738:        ps_printf(p, "%%%%EOF\n");
1.2       kristaps  739: }
                    740:
                    741:
                    742: static void
                    743: ps_begin(struct termp *p)
                    744: {
1.13      kristaps  745:        time_t           t;
1.16      kristaps  746:        int              i;
1.2       kristaps  747:
1.9       kristaps  748:        /*
                    749:         * Print margins into margin buffer.  Nothing gets output to the
                    750:         * screen yet, so we don't need to initialise the primary state.
1.2       kristaps  751:         */
                    752:
1.4       kristaps  753:        if (p->engine.ps.psmarg) {
                    754:                assert(p->engine.ps.psmargsz);
                    755:                p->engine.ps.psmarg[0] = '\0';
                    756:        }
                    757:
1.39      kristaps  758:        /*p->engine.ps.pdfbytes = 0;*/
1.4       kristaps  759:        p->engine.ps.psmargcur = 0;
1.29      kristaps  760:        p->engine.ps.flags = PS_MARGINS;
1.16      kristaps  761:        p->engine.ps.pscol = p->engine.ps.left;
                    762:        p->engine.ps.psrow = p->engine.ps.header;
1.4       kristaps  763:
1.9       kristaps  764:        ps_setfont(p, TERMFONT_NONE);
1.4       kristaps  765:
                    766:        (*p->headf)(p, p->argf);
                    767:        (*p->endline)(p);
                    768:
1.16      kristaps  769:        p->engine.ps.pscol = p->engine.ps.left;
                    770:        p->engine.ps.psrow = p->engine.ps.footer;
1.4       kristaps  771:
                    772:        (*p->footf)(p, p->argf);
                    773:        (*p->endline)(p);
                    774:
1.29      kristaps  775:        p->engine.ps.flags &= ~PS_MARGINS;
1.9       kristaps  776:
1.29      kristaps  777:        assert(0 == p->engine.ps.flags);
1.9       kristaps  778:        assert(p->engine.ps.psmarg);
                    779:        assert('\0' != p->engine.ps.psmarg[0]);
                    780:
                    781:        /*
                    782:         * Print header and initialise page state.  Following this,
                    783:         * stuff gets printed to the screen, so make sure we're sane.
                    784:         */
1.4       kristaps  785:
1.13      kristaps  786:        t = time(NULL);
                    787:
1.37      kristaps  788:        if (TERMTYPE_PS == p->type) {
                    789:                ps_printf(p, "%%!PS-Adobe-3.0\n");
                    790:                ps_printf(p, "%%%%Creator: mandoc-%s\n", VERSION);
                    791:                ps_printf(p, "%%%%CreationDate: %s", ctime(&t));
                    792:                ps_printf(p, "%%%%DocumentData: Clean7Bit\n");
                    793:                ps_printf(p, "%%%%Orientation: Portrait\n");
                    794:                ps_printf(p, "%%%%Pages: (atend)\n");
                    795:                ps_printf(p, "%%%%PageOrder: Ascend\n");
                    796:                ps_printf(p, "%%%%DocumentMedia: "
                    797:                                "Default %zu %zu 0 () ()\n",
                    798:                                (size_t)AFM2PNT(p, p->engine.ps.width),
                    799:                                (size_t)AFM2PNT(p, p->engine.ps.height));
                    800:                ps_printf(p, "%%%%DocumentNeededResources: font");
                    801:
                    802:                for (i = 0; i < (int)TERMFONT__MAX; i++)
                    803:                        ps_printf(p, " %s", fonts[i].name);
                    804:
                    805:                ps_printf(p, "\n%%%%EndComments\n");
                    806:        } else {
                    807:                ps_printf(p, "%%PDF-1.1\n");
1.39      kristaps  808:                pdf_obj(p, 1);
1.37      kristaps  809:                ps_printf(p, "<<\n");
                    810:                ps_printf(p, "/Creator mandoc-%s\n", VERSION);
                    811:                ps_printf(p, ">>\n");
                    812:                ps_printf(p, "endobj\n");
                    813:
                    814:                for (i = 0; i < (int)TERMFONT__MAX; i++) {
1.39      kristaps  815:                        pdf_obj(p, (size_t)i + 3);
1.37      kristaps  816:                        ps_printf(p, "<<\n");
                    817:                        ps_printf(p, "/Type /Font\n");
                    818:                        ps_printf(p, "/Subtype /Type1\n");
                    819:                        ps_printf(p, "/Name /F%zu\n", i);
                    820:                        ps_printf(p, "/BaseFont /%s\n", fonts[i].name);
                    821:                        ps_printf(p, ">>\n");
                    822:                }
                    823:        }
1.16      kristaps  824:
1.37      kristaps  825:        p->engine.ps.pdfbody = (size_t)TERMFONT__MAX + 3;
1.16      kristaps  826:        p->engine.ps.pscol = p->engine.ps.left;
                    827:        p->engine.ps.psrow = p->engine.ps.top;
1.29      kristaps  828:        p->engine.ps.flags |= PS_NEWPAGE;
1.33      kristaps  829:        ps_setfont(p, TERMFONT_NONE);
1.2       kristaps  830: }
                    831:
                    832:
                    833: static void
1.13      kristaps  834: ps_pletter(struct termp *p, int c)
1.2       kristaps  835: {
1.14      kristaps  836:        int              f;
1.32      kristaps  837:
1.9       kristaps  838:        /*
1.32      kristaps  839:         * If we haven't opened a page context, then output that we're
                    840:         * in a new page and make sure the font is correctly set.
1.9       kristaps  841:         */
                    842:
1.32      kristaps  843:        if (PS_NEWPAGE & p->engine.ps.flags) {
1.37      kristaps  844:                if (TERMTYPE_PS == p->type) {
                    845:                        ps_printf(p, "%%%%Page: %zu %zu\n",
                    846:                                        p->engine.ps.pages + 1,
                    847:                                        p->engine.ps.pages + 1);
                    848:                        ps_printf(p, "/%s %zu selectfont\n",
                    849:                                        fonts[(int)p->engine.ps.lastf].name,
                    850:                                        p->engine.ps.scale);
                    851:                } else {
1.39      kristaps  852:                        pdf_obj(p, p->engine.ps.pdfbody +
1.38      kristaps  853:                                        p->engine.ps.pages * 4);
1.37      kristaps  854:                        ps_printf(p, "<<\n");
                    855:                        ps_printf(p, "/Length %zu 0 R\n",
1.38      kristaps  856:                                        p->engine.ps.pdfbody + 1 +
                    857:                                        p->engine.ps.pages * 4);
1.37      kristaps  858:                        ps_printf(p, ">>\nstream\n");
                    859:                }
                    860:                p->engine.ps.pdflastpg = p->engine.ps.pdfbytes;
1.32      kristaps  861:                p->engine.ps.flags &= ~PS_NEWPAGE;
                    862:        }
                    863:
                    864:        /*
                    865:         * If we're not in a PostScript "word" context, then open one
                    866:         * now at the current cursor.
                    867:         */
1.30      kristaps  868:
1.29      kristaps  869:        if ( ! (PS_INLINE & p->engine.ps.flags)) {
1.37      kristaps  870:                if (TERMTYPE_PS != p->type) {
                    871:                        ps_printf(p, "BT\n/F%d %zu Tf\n",
                    872:                                        (int)p->engine.ps.lastf,
                    873:                                        p->engine.ps.scale);
                    874:                        ps_printf(p, "%.3f %.3f Td\n(",
                    875:                                        AFM2PNT(p, p->engine.ps.pscol),
                    876:                                        AFM2PNT(p, p->engine.ps.psrow));
                    877:                } else
                    878:                        ps_printf(p, "%.3f %.3f moveto\n(",
                    879:                                        AFM2PNT(p, p->engine.ps.pscol),
                    880:                                        AFM2PNT(p, p->engine.ps.psrow));
1.29      kristaps  881:                p->engine.ps.flags |= PS_INLINE;
1.2       kristaps  882:        }
                    883:
1.29      kristaps  884:        assert( ! (PS_NEWPAGE & p->engine.ps.flags));
                    885:
1.2       kristaps  886:        /*
                    887:         * We need to escape these characters as per the PostScript
                    888:         * specification.  We would also escape non-graphable characters
                    889:         * (like tabs), but none of them would get to this point and
                    890:         * it's superfluous to abort() on them.
                    891:         */
                    892:
                    893:        switch (c) {
                    894:        case ('('):
                    895:                /* FALLTHROUGH */
                    896:        case (')'):
                    897:                /* FALLTHROUGH */
                    898:        case ('\\'):
1.4       kristaps  899:                ps_putchar(p, '\\');
1.2       kristaps  900:                break;
                    901:        default:
                    902:                break;
                    903:        }
                    904:
                    905:        /* Write the character and adjust where we are on the page. */
1.9       kristaps  906:
1.14      kristaps  907:        f = (int)p->engine.ps.lastf;
1.13      kristaps  908:
1.14      kristaps  909:        if (c <= 32 || (c - 32 > MAXCHAR)) {
1.13      kristaps  910:                ps_putchar(p, ' ');
1.36      kristaps  911:                p->engine.ps.pscol += (size_t)fonts[f].gly[0].wx;
1.13      kristaps  912:                return;
                    913:        }
                    914:
1.25      kristaps  915:        ps_putchar(p, (char)c);
1.14      kristaps  916:        c -= 32;
1.36      kristaps  917:        p->engine.ps.pscol += (size_t)fonts[f].gly[c].wx;
1.2       kristaps  918: }
                    919:
                    920:
                    921: static void
1.9       kristaps  922: ps_pclose(struct termp *p)
                    923: {
                    924:
                    925:        /*
                    926:         * Spit out that we're exiting a word context (this is a
                    927:         * "partial close" because we don't check the last-char buffer
                    928:         * or anything).
                    929:         */
                    930:
1.29      kristaps  931:        if ( ! (PS_INLINE & p->engine.ps.flags))
1.9       kristaps  932:                return;
                    933:
1.37      kristaps  934:        if (TERMTYPE_PS != p->type) {
                    935:                ps_printf(p, ") Tj\nET\n");
                    936:        } else
                    937:                ps_printf(p, ") show\n");
                    938:
1.29      kristaps  939:        p->engine.ps.flags &= ~PS_INLINE;
1.9       kristaps  940: }
                    941:
                    942:
                    943: static void
                    944: ps_fclose(struct termp *p)
                    945: {
                    946:
                    947:        /*
                    948:         * Strong closure: if we have a last-char, spit it out after
                    949:         * checking that we're in the right font mode.  This will of
                    950:         * course open a new scope, if applicable.
                    951:         *
                    952:         * Following this, close out any scope that's open.
                    953:         */
                    954:
                    955:        if ('\0' != p->engine.ps.last) {
                    956:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    957:                        ps_pclose(p);
                    958:                        ps_setfont(p, TERMFONT_NONE);
                    959:                }
                    960:                ps_pletter(p, p->engine.ps.last);
                    961:                p->engine.ps.last = '\0';
                    962:        }
                    963:
1.29      kristaps  964:        if ( ! (PS_INLINE & p->engine.ps.flags))
1.9       kristaps  965:                return;
                    966:
                    967:        ps_pclose(p);
                    968: }
                    969:
                    970:
                    971: static void
1.8       kristaps  972: ps_letter(struct termp *p, char c)
                    973: {
                    974:        char            cc;
1.9       kristaps  975:
                    976:        /*
                    977:         * State machine dictates whether to buffer the last character
                    978:         * or not.  Basically, encoded words are detected by checking if
                    979:         * we're an "8" and switching on the buffer.  Then we put "8" in
                    980:         * our buffer, and on the next charater, flush both character
                    981:         * and buffer.  Thus, "regular" words are detected by having a
                    982:         * regular character and a regular buffer character.
                    983:         */
                    984:
1.8       kristaps  985:        if ('\0' == p->engine.ps.last) {
                    986:                assert(8 != c);
                    987:                p->engine.ps.last = c;
                    988:                return;
                    989:        } else if (8 == p->engine.ps.last) {
                    990:                assert(8 != c);
1.9       kristaps  991:                p->engine.ps.last = '\0';
1.8       kristaps  992:        } else if (8 == c) {
                    993:                assert(8 != p->engine.ps.last);
1.9       kristaps  994:                if ('_' == p->engine.ps.last) {
                    995:                        if (p->engine.ps.lastf != TERMFONT_UNDER) {
                    996:                                ps_pclose(p);
                    997:                                ps_setfont(p, TERMFONT_UNDER);
                    998:                        }
                    999:                } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
                   1000:                        ps_pclose(p);
                   1001:                        ps_setfont(p, TERMFONT_BOLD);
                   1002:                }
1.8       kristaps 1003:                p->engine.ps.last = c;
                   1004:                return;
                   1005:        } else {
1.9       kristaps 1006:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                   1007:                        ps_pclose(p);
                   1008:                        ps_setfont(p, TERMFONT_NONE);
                   1009:                }
1.8       kristaps 1010:                cc = p->engine.ps.last;
                   1011:                p->engine.ps.last = c;
                   1012:                c = cc;
                   1013:        }
                   1014:
1.9       kristaps 1015:        ps_pletter(p, c);
1.8       kristaps 1016: }
                   1017:
                   1018:
                   1019: static void
1.2       kristaps 1020: ps_advance(struct termp *p, size_t len)
                   1021: {
                   1022:
1.9       kristaps 1023:        /*
                   1024:         * Advance some spaces.  This can probably be made smarter,
                   1025:         * i.e., to have multiple space-separated words in the same
                   1026:         * scope, but this is easier:  just close out the current scope
                   1027:         * and readjust our column settings.
                   1028:         */
1.8       kristaps 1029:
1.9       kristaps 1030:        ps_fclose(p);
1.14      kristaps 1031:        p->engine.ps.pscol += len;
1.2       kristaps 1032: }
                   1033:
                   1034:
                   1035: static void
                   1036: ps_endline(struct termp *p)
                   1037: {
1.8       kristaps 1038:
1.9       kristaps 1039:        /* Close out any scopes we have open: we're at eoln. */
                   1040:
                   1041:        ps_fclose(p);
1.2       kristaps 1042:
1.9       kristaps 1043:        /*
                   1044:         * If we're in the margin, don't try to recalculate our current
                   1045:         * row.  XXX: if the column tries to be fancy with multiple
                   1046:         * lines, we'll do nasty stuff.
                   1047:         */
1.2       kristaps 1048:
1.29      kristaps 1049:        if (PS_MARGINS & p->engine.ps.flags)
                   1050:                return;
                   1051:
                   1052:        /* Left-justify. */
                   1053:
                   1054:        p->engine.ps.pscol = p->engine.ps.left;
                   1055:
                   1056:        /* If we haven't printed anything, return. */
                   1057:
                   1058:        if (PS_NEWPAGE & p->engine.ps.flags)
1.2       kristaps 1059:                return;
                   1060:
1.9       kristaps 1061:        /*
                   1062:         * Put us down a line.  If we're at the page bottom, spit out a
                   1063:         * showpage and restart our row.
                   1064:         */
                   1065:
1.16      kristaps 1066:        if (p->engine.ps.psrow >= p->engine.ps.lineheight +
                   1067:                        p->engine.ps.bottom) {
                   1068:                p->engine.ps.psrow -= p->engine.ps.lineheight;
1.2       kristaps 1069:                return;
                   1070:        }
                   1071:
1.37      kristaps 1072:        ps_closepage(p);
1.1       kristaps 1073: }
1.9       kristaps 1074:
                   1075:
                   1076: static void
                   1077: ps_setfont(struct termp *p, enum termfont f)
                   1078: {
                   1079:
1.16      kristaps 1080:        assert(f < TERMFONT__MAX);
1.32      kristaps 1081:        p->engine.ps.lastf = f;
                   1082:
                   1083:        /*
                   1084:         * If we're still at the top of the page, let the font-setting
                   1085:         * be delayed until we actually have stuff to print.
                   1086:         */
                   1087:
                   1088:        if (PS_NEWPAGE & p->engine.ps.flags)
                   1089:                return;
                   1090:
1.37      kristaps 1091:        if (TERMTYPE_PS == p->type)
                   1092:                ps_printf(p, "/%s %zu selectfont\n",
                   1093:                                fonts[(int)f].name,
                   1094:                                p->engine.ps.scale);
                   1095:        else
                   1096:                ps_printf(p, "/F%d %zu Tf\n",
                   1097:                                (int)f,
                   1098:                                p->engine.ps.scale);
1.9       kristaps 1099: }
                   1100:
1.11      kristaps 1101:
1.12      kristaps 1102: /* ARGSUSED */
1.11      kristaps 1103: static size_t
                   1104: ps_width(const struct termp *p, char c)
                   1105: {
                   1106:
1.14      kristaps 1107:        if (c <= 32 || c - 32 >= MAXCHAR)
1.36      kristaps 1108:                return((size_t)fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14      kristaps 1109:
                   1110:        c -= 32;
1.36      kristaps 1111:        return((size_t)fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1.11      kristaps 1112: }
1.19      kristaps 1113:
                   1114:
1.20      kristaps 1115: static double
1.19      kristaps 1116: ps_hspan(const struct termp *p, const struct roffsu *su)
                   1117: {
                   1118:        double           r;
                   1119:
                   1120:        /*
                   1121:         * All of these measurements are derived by converting from the
1.21      kristaps 1122:         * native measurement to AFM units.
1.19      kristaps 1123:         */
                   1124:
                   1125:        switch (su->unit) {
                   1126:        case (SCALE_CM):
1.21      kristaps 1127:                r = PNT2AFM(p, su->scale * 28.34);
1.19      kristaps 1128:                break;
                   1129:        case (SCALE_IN):
1.21      kristaps 1130:                r = PNT2AFM(p, su->scale * 72);
1.19      kristaps 1131:                break;
                   1132:        case (SCALE_PC):
1.21      kristaps 1133:                r = PNT2AFM(p, su->scale * 12);
1.19      kristaps 1134:                break;
                   1135:        case (SCALE_PT):
1.21      kristaps 1136:                r = PNT2AFM(p, su->scale * 100);
1.19      kristaps 1137:                break;
                   1138:        case (SCALE_EM):
1.36      kristaps 1139:                r = su->scale *
1.19      kristaps 1140:                        fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
                   1141:                break;
                   1142:        case (SCALE_MM):
1.21      kristaps 1143:                r = PNT2AFM(p, su->scale * 2.834);
1.19      kristaps 1144:                break;
                   1145:        case (SCALE_EN):
1.36      kristaps 1146:                r = su->scale *
1.19      kristaps 1147:                        fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
                   1148:                break;
                   1149:        case (SCALE_VS):
                   1150:                r = su->scale * p->engine.ps.lineheight;
                   1151:                break;
                   1152:        default:
                   1153:                r = su->scale;
                   1154:                break;
                   1155:        }
                   1156:
1.20      kristaps 1157:        return(r);
1.19      kristaps 1158: }
                   1159:

CVSweb