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

Annotation of mandoc/term_ps.c, Revision 1.47

1.47    ! schwarze    1: /*     $Id: term_ps.c,v 1.46 2011/01/25 17:32:04 kristaps 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.43      kristaps   21: #include <sys/types.h>
1.4       kristaps   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:
1.42      schwarze   32: #include "mandoc.h"
1.1       kristaps   33: #include "out.h"
                     34: #include "main.h"
                     35: #include "term.h"
                     36:
1.21      kristaps   37: /* Convert PostScript point "x" to an AFM unit. */
1.25      kristaps   38: #define        PNT2AFM(p, x) /* LINTED */ \
1.21      kristaps   39:        (size_t)((double)(x) * (1000.0 / (double)(p)->engine.ps.scale))
                     40:
                     41: /* Convert an AFM unit "x" to a PostScript points */
1.25      kristaps   42: #define        AFM2PNT(p, x) /* LINTED */ \
1.35      kristaps   43:        ((double)(x) / (1000.0 / (double)(p)->engine.ps.scale))
1.21      kristaps   44:
1.13      kristaps   45: struct glyph {
1.36      kristaps   46:        unsigned short    wx; /* WX in AFM */
1.13      kristaps   47: };
                     48:
                     49: struct font {
                     50:        const char       *name; /* FontName in AFM */
1.15      kristaps   51: #define        MAXCHAR           95 /* total characters we can handle */
1.13      kristaps   52:        struct glyph      gly[MAXCHAR]; /* glyph metrics */
                     53: };
                     54:
                     55: /*
                     56:  * We define, for the time being, three fonts: bold, oblique/italic, and
                     57:  * normal (roman).  The following table hard-codes the font metrics for
                     58:  * ASCII, i.e., 32--127.
                     59:  */
                     60:
1.16      kristaps   61: static const struct font fonts[TERMFONT__MAX] = {
1.24      kristaps   62:        { "Times-Roman", {
                     63:                { 250 },
                     64:                { 333 },
                     65:                { 408 },
                     66:                { 500 },
                     67:                { 500 },
                     68:                { 833 },
                     69:                { 778 },
                     70:                { 333 },
                     71:                { 333 },
                     72:                { 333 },
                     73:                { 500 },
                     74:                { 564 },
                     75:                { 250 },
                     76:                { 333 },
                     77:                { 250 },
                     78:                { 278 },
                     79:                { 500 },
                     80:                { 500 },
                     81:                { 500 },
                     82:                { 500 },
                     83:                { 500 },
                     84:                { 500 },
                     85:                { 500 },
                     86:                { 500 },
                     87:                { 500 },
                     88:                { 500 },
                     89:                { 278 },
                     90:                { 278 },
                     91:                { 564 },
                     92:                { 564 },
                     93:                { 564 },
                     94:                { 444 },
                     95:                { 921 },
                     96:                { 722 },
                     97:                { 667 },
                     98:                { 667 },
                     99:                { 722 },
                    100:                { 611 },
                    101:                { 556 },
                    102:                { 722 },
                    103:                { 722 },
                    104:                { 333 },
                    105:                { 389 },
                    106:                { 722 },
                    107:                { 611 },
                    108:                { 889 },
                    109:                { 722 },
                    110:                { 722 },
                    111:                { 556 },
                    112:                { 722 },
                    113:                { 667 },
                    114:                { 556 },
                    115:                { 611 },
                    116:                { 722 },
                    117:                { 722 },
                    118:                { 944 },
                    119:                { 722 },
                    120:                { 722 },
                    121:                { 611 },
                    122:                { 333 },
                    123:                { 278 },
                    124:                { 333 },
                    125:                { 469 },
                    126:                { 500 },
                    127:                { 333 },
                    128:                { 444 },
                    129:                { 500 },
                    130:                { 444 },
                    131:                {  500},
                    132:                {  444},
                    133:                {  333},
                    134:                {  500},
                    135:                {  500},
                    136:                {  278},
                    137:                {  278},
                    138:                {  500},
                    139:                {  278},
                    140:                {  778},
                    141:                {  500},
                    142:                {  500},
                    143:                {  500},
                    144:                {  500},
                    145:                {  333},
                    146:                {  389},
                    147:                {  278},
                    148:                {  500},
                    149:                {  500},
                    150:                {  722},
                    151:                {  500},
                    152:                {  500},
                    153:                {  444},
                    154:                {  480},
                    155:                {  200},
                    156:                {  480},
                    157:                {  541},
                    158:        } },
                    159:        { "Times-Bold", {
                    160:                { 250  },
                    161:                { 333  },
                    162:                { 555  },
                    163:                { 500  },
                    164:                { 500  },
                    165:                { 1000 },
                    166:                { 833  },
                    167:                { 333  },
                    168:                { 333  },
                    169:                { 333  },
                    170:                { 500  },
                    171:                { 570  },
                    172:                { 250  },
                    173:                { 333  },
                    174:                { 250  },
                    175:                { 278  },
                    176:                { 500  },
                    177:                { 500  },
                    178:                { 500  },
                    179:                { 500  },
                    180:                { 500  },
                    181:                { 500  },
                    182:                { 500  },
                    183:                { 500  },
                    184:                { 500  },
                    185:                { 500  },
                    186:                { 333  },
                    187:                { 333  },
                    188:                { 570  },
                    189:                { 570  },
                    190:                { 570  },
                    191:                { 500  },
                    192:                { 930  },
                    193:                { 722  },
                    194:                { 667  },
                    195:                { 722  },
                    196:                { 722  },
                    197:                { 667  },
                    198:                { 611  },
                    199:                { 778  },
                    200:                { 778  },
                    201:                { 389  },
                    202:                { 500  },
                    203:                { 778  },
                    204:                { 667  },
                    205:                { 944  },
                    206:                { 722  },
                    207:                { 778  },
                    208:                { 611  },
                    209:                { 778  },
                    210:                { 722  },
                    211:                { 556  },
                    212:                { 667  },
                    213:                { 722  },
                    214:                { 722  },
                    215:                { 1000 },
                    216:                { 722  },
                    217:                { 722  },
                    218:                { 667  },
                    219:                { 333  },
                    220:                { 278  },
                    221:                { 333  },
                    222:                { 581  },
                    223:                { 500  },
                    224:                { 333  },
                    225:                { 500  },
                    226:                { 556  },
                    227:                { 444  },
1.22      kristaps  228:                {  556 },
1.24      kristaps  229:                {  444 },
                    230:                {  333 },
1.22      kristaps  231:                {  500 },
1.24      kristaps  232:                {  556 },
1.22      kristaps  233:                {  278 },
1.24      kristaps  234:                {  333 },
                    235:                {  556 },
1.22      kristaps  236:                {  278 },
1.24      kristaps  237:                {  833 },
1.22      kristaps  238:                {  556 },
1.24      kristaps  239:                {  500 },
1.22      kristaps  240:                {  556 },
                    241:                {  556 },
                    242:                {  444 },
1.24      kristaps  243:                {  389 },
                    244:                {  333 },
1.22      kristaps  245:                {  556 },
                    246:                {  500 },
1.24      kristaps  247:                {  722 },
                    248:                {  500 },
1.22      kristaps  249:                {  500 },
1.24      kristaps  250:                {  444 },
                    251:                {  394 },
                    252:                {  220 },
                    253:                {  394 },
                    254:                {  520 },
1.13      kristaps  255:        } },
1.24      kristaps  256:        { "Times-Italic", {
                    257:                { 250  },
                    258:                { 333  },
                    259:                { 420  },
                    260:                { 500  },
                    261:                { 500  },
                    262:                { 833  },
                    263:                { 778  },
                    264:                { 333  },
                    265:                { 333  },
                    266:                { 333  },
                    267:                { 500  },
                    268:                { 675  },
                    269:                { 250  },
                    270:                { 333  },
                    271:                { 250  },
                    272:                { 278  },
                    273:                { 500  },
                    274:                { 500  },
                    275:                { 500  },
                    276:                { 500  },
                    277:                { 500  },
                    278:                { 500  },
                    279:                { 500  },
                    280:                { 500  },
                    281:                { 500  },
                    282:                { 500  },
                    283:                { 333  },
                    284:                { 333  },
                    285:                { 675  },
                    286:                { 675  },
                    287:                { 675  },
                    288:                { 500  },
                    289:                { 920  },
                    290:                { 611  },
                    291:                { 611  },
                    292:                { 667  },
                    293:                { 722  },
                    294:                { 611  },
                    295:                { 611  },
                    296:                { 722  },
                    297:                { 722  },
                    298:                { 333  },
                    299:                { 444  },
                    300:                { 667  },
                    301:                { 556  },
                    302:                { 833  },
                    303:                { 667  },
                    304:                { 722  },
                    305:                { 611  },
                    306:                { 722  },
                    307:                { 611  },
                    308:                { 500  },
                    309:                { 556  },
                    310:                { 722  },
                    311:                { 611  },
                    312:                { 833  },
                    313:                { 611  },
                    314:                { 556  },
                    315:                { 556  },
                    316:                { 389  },
                    317:                { 278  },
                    318:                { 389  },
                    319:                { 422  },
                    320:                { 500  },
                    321:                { 333  },
                    322:                { 500  },
                    323:                { 500  },
                    324:                { 444  },
1.22      kristaps  325:                {  500 },
1.24      kristaps  326:                {  444 },
                    327:                {  278 },
1.22      kristaps  328:                {  500 },
                    329:                {  500 },
                    330:                {  278 },
1.24      kristaps  331:                {  278 },
                    332:                {  444 },
                    333:                {  278 },
                    334:                {  722 },
                    335:                {  500 },
                    336:                {  500 },
                    337:                {  500 },
1.22      kristaps  338:                {  500 },
1.24      kristaps  339:                {  389 },
                    340:                {  389 },
1.22      kristaps  341:                {  278 },
                    342:                {  500 },
1.24      kristaps  343:                {  444 },
                    344:                {  667 },
                    345:                {  444 },
                    346:                {  444 },
                    347:                {  389 },
                    348:                {  400 },
1.22      kristaps  349:                {  275 },
1.24      kristaps  350:                {  400 },
                    351:                {  541 },
1.13      kristaps  352:        } },
                    353: };
                    354:
1.15      kristaps  355: /* These work the buffer used by the header and footer. */
1.4       kristaps  356: #define        PS_BUFSLOP        128
                    357:
1.40      joerg     358: static void
                    359: ps_growbuf(struct termp *p, size_t sz)
                    360: {
                    361:        if (p->engine.ps.psmargcur + sz <= p->engine.ps.psmargsz)
                    362:                return;
                    363:
                    364:        if (sz < PS_BUFSLOP)
                    365:                sz = PS_BUFSLOP;
1.41      kristaps  366:
1.40      joerg     367:        p->engine.ps.psmargsz += sz;
                    368:
1.41      kristaps  369:        p->engine.ps.psmarg = realloc
                    370:                (p->engine.ps.psmarg,
                    371:                 p->engine.ps.psmargsz);
                    372:
                    373:        if (NULL == p->engine.ps.psmarg) {
1.40      joerg     374:                perror(NULL);
1.44      kristaps  375:                exit((int)MANDOCLEVEL_SYSERR);
1.41      kristaps  376:        }
1.40      joerg     377: }
1.9       kristaps  378:
1.20      kristaps  379: static double            ps_hspan(const struct termp *,
1.19      kristaps  380:                                const struct roffsu *);
                    381: static size_t            ps_width(const struct termp *, char);
                    382: static void              ps_advance(struct termp *, size_t);
1.2       kristaps  383: static void              ps_begin(struct termp *);
1.37      kristaps  384: static void              ps_closepage(struct termp *);
1.2       kristaps  385: static void              ps_end(struct termp *);
                    386: static void              ps_endline(struct termp *);
1.9       kristaps  387: static void              ps_fclose(struct termp *);
1.19      kristaps  388: static void              ps_letter(struct termp *, char);
1.9       kristaps  389: static void              ps_pclose(struct termp *);
1.13      kristaps  390: static void              ps_pletter(struct termp *, int);
1.4       kristaps  391: static void              ps_printf(struct termp *, const char *, ...);
                    392: static void              ps_putchar(struct termp *, char);
1.9       kristaps  393: static void              ps_setfont(struct termp *, enum termfont);
1.37      kristaps  394: static struct termp     *pspdf_alloc(char *);
1.39      kristaps  395: static void              pdf_obj(struct termp *, size_t);
1.37      kristaps  396:
                    397:
                    398: void *
                    399: pdf_alloc(char *outopts)
                    400: {
                    401:        struct termp    *p;
                    402:
1.38      kristaps  403:        if (NULL != (p = pspdf_alloc(outopts)))
                    404:                p->type = TERMTYPE_PDF;
1.37      kristaps  405:
                    406:        return(p);
                    407: }
1.2       kristaps  408:
                    409:
1.1       kristaps  410: void *
1.17      kristaps  411: ps_alloc(char *outopts)
1.1       kristaps  412: {
                    413:        struct termp    *p;
1.37      kristaps  414:
1.38      kristaps  415:        if (NULL != (p = pspdf_alloc(outopts)))
                    416:                p->type = TERMTYPE_PS;
1.37      kristaps  417:
                    418:        return(p);
                    419: }
                    420:
                    421:
                    422: static struct termp *
                    423: pspdf_alloc(char *outopts)
                    424: {
                    425:        struct termp    *p;
1.28      kristaps  426:        size_t           pagex, pagey, marginx, marginy, lineheight;
1.17      kristaps  427:        const char      *toks[2];
1.25      kristaps  428:        const char      *pp;
1.17      kristaps  429:        char            *v;
1.1       kristaps  430:
1.46      kristaps  431:        p = term_alloc(TERMENC_ASCII);
1.1       kristaps  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.44      kristaps  585:                /* LINTED */
1.4       kristaps  586:                putchar(c);
1.37      kristaps  587:                p->engine.ps.pdfbytes++;
1.4       kristaps  588:                return;
                    589:        }
                    590:
1.40      joerg     591:        ps_growbuf(p, 2);
1.4       kristaps  592:
                    593:        pos = (int)p->engine.ps.psmargcur++;
1.5       kristaps  594:        p->engine.ps.psmarg[pos++] = c;
1.4       kristaps  595:        p->engine.ps.psmarg[pos] = '\0';
1.2       kristaps  596: }
                    597:
                    598:
1.37      kristaps  599: static void
1.39      kristaps  600: pdf_obj(struct termp *p, size_t obj)
                    601: {
                    602:
                    603:        assert(obj > 0);
                    604:
                    605:        if ((obj - 1) >= p->engine.ps.pdfobjsz) {
                    606:                p->engine.ps.pdfobjsz = obj + 128;
                    607:                p->engine.ps.pdfobjs = realloc
                    608:                        (p->engine.ps.pdfobjs,
                    609:                         p->engine.ps.pdfobjsz * sizeof(size_t));
                    610:                if (NULL == p->engine.ps.pdfobjs) {
                    611:                        perror(NULL);
1.44      kristaps  612:                        exit((int)MANDOCLEVEL_SYSERR);
1.39      kristaps  613:                }
                    614:        }
                    615:
                    616:        p->engine.ps.pdfobjs[(int)obj - 1] = p->engine.ps.pdfbytes;
                    617:        ps_printf(p, "%zu 0 obj\n", obj);
                    618: }
                    619:
                    620:
                    621: static void
1.37      kristaps  622: ps_closepage(struct termp *p)
                    623: {
                    624:        int              i;
1.38      kristaps  625:        size_t           len, base;
                    626:
                    627:        /*
                    628:         * Close out a page that we've already flushed to output.  In
                    629:         * PostScript, we simply note that the page must be showed.  In
                    630:         * PDF, we must now create the Length, Resource, and Page node
                    631:         * for the page contents.
                    632:         */
1.37      kristaps  633:
                    634:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    635:        ps_printf(p, "%s", p->engine.ps.psmarg);
                    636:
1.38      kristaps  637:        if (TERMTYPE_PS != p->type) {
1.37      kristaps  638:                ps_printf(p, "ET\n");
1.38      kristaps  639:
1.37      kristaps  640:                len = p->engine.ps.pdfbytes - p->engine.ps.pdflastpg;
1.38      kristaps  641:                base = p->engine.ps.pages * 4 + p->engine.ps.pdfbody;
                    642:
                    643:                ps_printf(p, "endstream\nendobj\n");
                    644:
                    645:                /* Length of content. */
1.39      kristaps  646:                pdf_obj(p, base + 1);
1.38      kristaps  647:                ps_printf(p, "%zu\nendobj\n", len);
                    648:
                    649:                /* Resource for content. */
1.39      kristaps  650:                pdf_obj(p, base + 2);
1.38      kristaps  651:                ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
1.37      kristaps  652:                ps_printf(p, "/Font <<\n");
1.39      kristaps  653:                for (i = 0; i < (int)TERMFONT__MAX; i++)
1.37      kristaps  654:                        ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
1.38      kristaps  655:                ps_printf(p, ">>\n>>\n");
                    656:
                    657:                /* Page node. */
1.39      kristaps  658:                pdf_obj(p, base + 3);
                    659:                ps_printf(p, "<<\n");
1.37      kristaps  660:                ps_printf(p, "/Type /Page\n");
                    661:                ps_printf(p, "/Parent 2 0 R\n");
1.38      kristaps  662:                ps_printf(p, "/Resources %zu 0 R\n", base + 2);
                    663:                ps_printf(p, "/Contents %zu 0 R\n", base);
                    664:                ps_printf(p, ">>\nendobj\n");
                    665:        } else
                    666:                ps_printf(p, "showpage\n");
1.37      kristaps  667:
                    668:        p->engine.ps.pages++;
                    669:        p->engine.ps.psrow = p->engine.ps.top;
                    670:        assert( ! (PS_NEWPAGE & p->engine.ps.flags));
                    671:        p->engine.ps.flags |= PS_NEWPAGE;
                    672: }
                    673:
                    674:
1.3       kristaps  675: /* ARGSUSED */
1.2       kristaps  676: static void
                    677: ps_end(struct termp *p)
                    678: {
1.38      kristaps  679:        size_t           i, xref, base;
1.2       kristaps  680:
1.4       kristaps  681:        /*
                    682:         * At the end of the file, do one last showpage.  This is the
                    683:         * same behaviour as groff(1) and works for multiple pages as
                    684:         * well as just one.
                    685:         */
                    686:
1.30      kristaps  687:        if ( ! (PS_NEWPAGE & p->engine.ps.flags)) {
                    688:                assert(0 == p->engine.ps.flags);
                    689:                assert('\0' == p->engine.ps.last);
1.37      kristaps  690:                ps_closepage(p);
1.30      kristaps  691:        }
1.16      kristaps  692:
1.37      kristaps  693:        if (TERMTYPE_PS == p->type) {
                    694:                ps_printf(p, "%%%%Trailer\n");
                    695:                ps_printf(p, "%%%%Pages: %zu\n", p->engine.ps.pages);
                    696:                ps_printf(p, "%%%%EOF\n");
                    697:                return;
                    698:        }
                    699:
1.39      kristaps  700:        pdf_obj(p, 2);
                    701:        ps_printf(p, "<<\n/Type /Pages\n");
1.37      kristaps  702:        ps_printf(p, "/MediaBox [0 0 %zu %zu]\n",
                    703:                        (size_t)AFM2PNT(p, p->engine.ps.width),
                    704:                        (size_t)AFM2PNT(p, p->engine.ps.height));
                    705:
                    706:        ps_printf(p, "/Count %zu\n", p->engine.ps.pages);
                    707:        ps_printf(p, "/Kids [");
                    708:
                    709:        for (i = 0; i < p->engine.ps.pages; i++)
1.38      kristaps  710:                ps_printf(p, " %zu 0 R", i * 4 +
                    711:                                p->engine.ps.pdfbody + 3);
                    712:
                    713:        base = (p->engine.ps.pages - 1) * 4 +
                    714:                p->engine.ps.pdfbody + 4;
                    715:
                    716:        ps_printf(p, "]\n>>\nendobj\n");
1.39      kristaps  717:        pdf_obj(p, base);
1.37      kristaps  718:        ps_printf(p, "<<\n");
                    719:        ps_printf(p, "/Type /Catalog\n");
                    720:        ps_printf(p, "/Pages 2 0 R\n");
                    721:        ps_printf(p, ">>\n");
                    722:        xref = p->engine.ps.pdfbytes;
                    723:        ps_printf(p, "xref\n");
1.38      kristaps  724:        ps_printf(p, "0 %zu\n", base + 1);
1.39      kristaps  725:        ps_printf(p, "0000000000 65535 f \n");
                    726:
                    727:        for (i = 0; i < base; i++)
                    728:                ps_printf(p, "%.10zu 00000 n \n",
                    729:                                p->engine.ps.pdfobjs[(int)i]);
                    730:
1.37      kristaps  731:        ps_printf(p, "trailer\n");
                    732:        ps_printf(p, "<<\n");
1.38      kristaps  733:        ps_printf(p, "/Size %zu\n", base + 1);
                    734:        ps_printf(p, "/Root %zu 0 R\n", base);
1.37      kristaps  735:        ps_printf(p, "/Info 1 0 R\n");
                    736:        ps_printf(p, ">>\n");
                    737:        ps_printf(p, "startxref\n");
                    738:        ps_printf(p, "%zu\n", xref);
                    739:        ps_printf(p, "%%%%EOF\n");
1.2       kristaps  740: }
                    741:
                    742:
                    743: static void
                    744: ps_begin(struct termp *p)
                    745: {
1.13      kristaps  746:        time_t           t;
1.16      kristaps  747:        int              i;
1.2       kristaps  748:
1.9       kristaps  749:        /*
                    750:         * Print margins into margin buffer.  Nothing gets output to the
                    751:         * screen yet, so we don't need to initialise the primary state.
1.2       kristaps  752:         */
                    753:
1.4       kristaps  754:        if (p->engine.ps.psmarg) {
                    755:                assert(p->engine.ps.psmargsz);
                    756:                p->engine.ps.psmarg[0] = '\0';
                    757:        }
                    758:
1.39      kristaps  759:        /*p->engine.ps.pdfbytes = 0;*/
1.4       kristaps  760:        p->engine.ps.psmargcur = 0;
1.29      kristaps  761:        p->engine.ps.flags = PS_MARGINS;
1.16      kristaps  762:        p->engine.ps.pscol = p->engine.ps.left;
                    763:        p->engine.ps.psrow = p->engine.ps.header;
1.4       kristaps  764:
1.9       kristaps  765:        ps_setfont(p, TERMFONT_NONE);
1.4       kristaps  766:
                    767:        (*p->headf)(p, p->argf);
                    768:        (*p->endline)(p);
                    769:
1.16      kristaps  770:        p->engine.ps.pscol = p->engine.ps.left;
                    771:        p->engine.ps.psrow = p->engine.ps.footer;
1.4       kristaps  772:
                    773:        (*p->footf)(p, p->argf);
                    774:        (*p->endline)(p);
                    775:
1.29      kristaps  776:        p->engine.ps.flags &= ~PS_MARGINS;
1.9       kristaps  777:
1.29      kristaps  778:        assert(0 == p->engine.ps.flags);
1.9       kristaps  779:        assert(p->engine.ps.psmarg);
                    780:        assert('\0' != p->engine.ps.psmarg[0]);
                    781:
                    782:        /*
                    783:         * Print header and initialise page state.  Following this,
                    784:         * stuff gets printed to the screen, so make sure we're sane.
                    785:         */
1.4       kristaps  786:
1.13      kristaps  787:        t = time(NULL);
                    788:
1.37      kristaps  789:        if (TERMTYPE_PS == p->type) {
                    790:                ps_printf(p, "%%!PS-Adobe-3.0\n");
                    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, ">>\n");
                    811:                ps_printf(p, "endobj\n");
                    812:
                    813:                for (i = 0; i < (int)TERMFONT__MAX; i++) {
1.39      kristaps  814:                        pdf_obj(p, (size_t)i + 3);
1.37      kristaps  815:                        ps_printf(p, "<<\n");
                    816:                        ps_printf(p, "/Type /Font\n");
                    817:                        ps_printf(p, "/Subtype /Type1\n");
                    818:                        ps_printf(p, "/Name /F%zu\n", i);
                    819:                        ps_printf(p, "/BaseFont /%s\n", fonts[i].name);
                    820:                        ps_printf(p, ">>\n");
                    821:                }
                    822:        }
1.16      kristaps  823:
1.37      kristaps  824:        p->engine.ps.pdfbody = (size_t)TERMFONT__MAX + 3;
1.16      kristaps  825:        p->engine.ps.pscol = p->engine.ps.left;
                    826:        p->engine.ps.psrow = p->engine.ps.top;
1.29      kristaps  827:        p->engine.ps.flags |= PS_NEWPAGE;
1.33      kristaps  828:        ps_setfont(p, TERMFONT_NONE);
1.2       kristaps  829: }
                    830:
                    831:
                    832: static void
1.13      kristaps  833: ps_pletter(struct termp *p, int c)
1.2       kristaps  834: {
1.14      kristaps  835:        int              f;
1.32      kristaps  836:
1.9       kristaps  837:        /*
1.32      kristaps  838:         * If we haven't opened a page context, then output that we're
                    839:         * in a new page and make sure the font is correctly set.
1.9       kristaps  840:         */
                    841:
1.32      kristaps  842:        if (PS_NEWPAGE & p->engine.ps.flags) {
1.37      kristaps  843:                if (TERMTYPE_PS == p->type) {
                    844:                        ps_printf(p, "%%%%Page: %zu %zu\n",
                    845:                                        p->engine.ps.pages + 1,
                    846:                                        p->engine.ps.pages + 1);
                    847:                        ps_printf(p, "/%s %zu selectfont\n",
                    848:                                        fonts[(int)p->engine.ps.lastf].name,
                    849:                                        p->engine.ps.scale);
                    850:                } else {
1.39      kristaps  851:                        pdf_obj(p, p->engine.ps.pdfbody +
1.38      kristaps  852:                                        p->engine.ps.pages * 4);
1.37      kristaps  853:                        ps_printf(p, "<<\n");
                    854:                        ps_printf(p, "/Length %zu 0 R\n",
1.38      kristaps  855:                                        p->engine.ps.pdfbody + 1 +
                    856:                                        p->engine.ps.pages * 4);
1.37      kristaps  857:                        ps_printf(p, ">>\nstream\n");
                    858:                }
                    859:                p->engine.ps.pdflastpg = p->engine.ps.pdfbytes;
1.32      kristaps  860:                p->engine.ps.flags &= ~PS_NEWPAGE;
                    861:        }
                    862:
                    863:        /*
                    864:         * If we're not in a PostScript "word" context, then open one
                    865:         * now at the current cursor.
                    866:         */
1.30      kristaps  867:
1.29      kristaps  868:        if ( ! (PS_INLINE & p->engine.ps.flags)) {
1.37      kristaps  869:                if (TERMTYPE_PS != p->type) {
                    870:                        ps_printf(p, "BT\n/F%d %zu Tf\n",
                    871:                                        (int)p->engine.ps.lastf,
                    872:                                        p->engine.ps.scale);
                    873:                        ps_printf(p, "%.3f %.3f Td\n(",
                    874:                                        AFM2PNT(p, p->engine.ps.pscol),
                    875:                                        AFM2PNT(p, p->engine.ps.psrow));
                    876:                } else
                    877:                        ps_printf(p, "%.3f %.3f moveto\n(",
                    878:                                        AFM2PNT(p, p->engine.ps.pscol),
                    879:                                        AFM2PNT(p, p->engine.ps.psrow));
1.29      kristaps  880:                p->engine.ps.flags |= PS_INLINE;
1.2       kristaps  881:        }
                    882:
1.29      kristaps  883:        assert( ! (PS_NEWPAGE & p->engine.ps.flags));
                    884:
1.2       kristaps  885:        /*
                    886:         * We need to escape these characters as per the PostScript
                    887:         * specification.  We would also escape non-graphable characters
                    888:         * (like tabs), but none of them would get to this point and
                    889:         * it's superfluous to abort() on them.
                    890:         */
                    891:
                    892:        switch (c) {
                    893:        case ('('):
                    894:                /* FALLTHROUGH */
                    895:        case (')'):
                    896:                /* FALLTHROUGH */
                    897:        case ('\\'):
1.4       kristaps  898:                ps_putchar(p, '\\');
1.2       kristaps  899:                break;
                    900:        default:
                    901:                break;
                    902:        }
                    903:
                    904:        /* Write the character and adjust where we are on the page. */
1.9       kristaps  905:
1.14      kristaps  906:        f = (int)p->engine.ps.lastf;
1.13      kristaps  907:
1.45      schwarze  908:        if (c <= 32 || (c - 32 >= MAXCHAR)) {
1.13      kristaps  909:                ps_putchar(p, ' ');
1.36      kristaps  910:                p->engine.ps.pscol += (size_t)fonts[f].gly[0].wx;
1.13      kristaps  911:                return;
                    912:        }
                    913:
1.25      kristaps  914:        ps_putchar(p, (char)c);
1.14      kristaps  915:        c -= 32;
1.36      kristaps  916:        p->engine.ps.pscol += (size_t)fonts[f].gly[c].wx;
1.2       kristaps  917: }
                    918:
                    919:
                    920: static void
1.9       kristaps  921: ps_pclose(struct termp *p)
                    922: {
                    923:
                    924:        /*
                    925:         * Spit out that we're exiting a word context (this is a
                    926:         * "partial close" because we don't check the last-char buffer
                    927:         * or anything).
                    928:         */
                    929:
1.29      kristaps  930:        if ( ! (PS_INLINE & p->engine.ps.flags))
1.9       kristaps  931:                return;
                    932:
1.37      kristaps  933:        if (TERMTYPE_PS != p->type) {
                    934:                ps_printf(p, ") Tj\nET\n");
                    935:        } else
                    936:                ps_printf(p, ") show\n");
                    937:
1.29      kristaps  938:        p->engine.ps.flags &= ~PS_INLINE;
1.9       kristaps  939: }
                    940:
                    941:
                    942: static void
                    943: ps_fclose(struct termp *p)
                    944: {
                    945:
                    946:        /*
                    947:         * Strong closure: if we have a last-char, spit it out after
                    948:         * checking that we're in the right font mode.  This will of
                    949:         * course open a new scope, if applicable.
                    950:         *
                    951:         * Following this, close out any scope that's open.
                    952:         */
                    953:
                    954:        if ('\0' != p->engine.ps.last) {
                    955:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    956:                        ps_pclose(p);
                    957:                        ps_setfont(p, TERMFONT_NONE);
                    958:                }
                    959:                ps_pletter(p, p->engine.ps.last);
                    960:                p->engine.ps.last = '\0';
                    961:        }
                    962:
1.29      kristaps  963:        if ( ! (PS_INLINE & p->engine.ps.flags))
1.9       kristaps  964:                return;
                    965:
                    966:        ps_pclose(p);
                    967: }
                    968:
                    969:
                    970: static void
1.8       kristaps  971: ps_letter(struct termp *p, char c)
                    972: {
                    973:        char            cc;
1.9       kristaps  974:
                    975:        /*
                    976:         * State machine dictates whether to buffer the last character
                    977:         * or not.  Basically, encoded words are detected by checking if
                    978:         * we're an "8" and switching on the buffer.  Then we put "8" in
                    979:         * our buffer, and on the next charater, flush both character
                    980:         * and buffer.  Thus, "regular" words are detected by having a
                    981:         * regular character and a regular buffer character.
                    982:         */
                    983:
1.8       kristaps  984:        if ('\0' == p->engine.ps.last) {
                    985:                assert(8 != c);
                    986:                p->engine.ps.last = c;
                    987:                return;
                    988:        } else if (8 == p->engine.ps.last) {
                    989:                assert(8 != c);
1.9       kristaps  990:                p->engine.ps.last = '\0';
1.8       kristaps  991:        } else if (8 == c) {
                    992:                assert(8 != p->engine.ps.last);
1.9       kristaps  993:                if ('_' == p->engine.ps.last) {
                    994:                        if (p->engine.ps.lastf != TERMFONT_UNDER) {
                    995:                                ps_pclose(p);
                    996:                                ps_setfont(p, TERMFONT_UNDER);
                    997:                        }
                    998:                } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
                    999:                        ps_pclose(p);
                   1000:                        ps_setfont(p, TERMFONT_BOLD);
                   1001:                }
1.8       kristaps 1002:                p->engine.ps.last = c;
                   1003:                return;
                   1004:        } else {
1.9       kristaps 1005:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                   1006:                        ps_pclose(p);
                   1007:                        ps_setfont(p, TERMFONT_NONE);
                   1008:                }
1.8       kristaps 1009:                cc = p->engine.ps.last;
                   1010:                p->engine.ps.last = c;
                   1011:                c = cc;
                   1012:        }
                   1013:
1.9       kristaps 1014:        ps_pletter(p, c);
1.8       kristaps 1015: }
                   1016:
                   1017:
                   1018: static void
1.2       kristaps 1019: ps_advance(struct termp *p, size_t len)
                   1020: {
                   1021:
1.9       kristaps 1022:        /*
                   1023:         * Advance some spaces.  This can probably be made smarter,
                   1024:         * i.e., to have multiple space-separated words in the same
                   1025:         * scope, but this is easier:  just close out the current scope
                   1026:         * and readjust our column settings.
                   1027:         */
1.8       kristaps 1028:
1.9       kristaps 1029:        ps_fclose(p);
1.14      kristaps 1030:        p->engine.ps.pscol += len;
1.2       kristaps 1031: }
                   1032:
                   1033:
                   1034: static void
                   1035: ps_endline(struct termp *p)
                   1036: {
1.8       kristaps 1037:
1.9       kristaps 1038:        /* Close out any scopes we have open: we're at eoln. */
                   1039:
                   1040:        ps_fclose(p);
1.2       kristaps 1041:
1.9       kristaps 1042:        /*
                   1043:         * If we're in the margin, don't try to recalculate our current
                   1044:         * row.  XXX: if the column tries to be fancy with multiple
                   1045:         * lines, we'll do nasty stuff.
                   1046:         */
1.2       kristaps 1047:
1.29      kristaps 1048:        if (PS_MARGINS & p->engine.ps.flags)
                   1049:                return;
                   1050:
                   1051:        /* Left-justify. */
                   1052:
                   1053:        p->engine.ps.pscol = p->engine.ps.left;
                   1054:
                   1055:        /* If we haven't printed anything, return. */
                   1056:
                   1057:        if (PS_NEWPAGE & p->engine.ps.flags)
1.2       kristaps 1058:                return;
                   1059:
1.9       kristaps 1060:        /*
                   1061:         * Put us down a line.  If we're at the page bottom, spit out a
                   1062:         * showpage and restart our row.
                   1063:         */
                   1064:
1.16      kristaps 1065:        if (p->engine.ps.psrow >= p->engine.ps.lineheight +
                   1066:                        p->engine.ps.bottom) {
                   1067:                p->engine.ps.psrow -= p->engine.ps.lineheight;
1.2       kristaps 1068:                return;
                   1069:        }
                   1070:
1.37      kristaps 1071:        ps_closepage(p);
1.1       kristaps 1072: }
1.9       kristaps 1073:
                   1074:
                   1075: static void
                   1076: ps_setfont(struct termp *p, enum termfont f)
                   1077: {
                   1078:
1.16      kristaps 1079:        assert(f < TERMFONT__MAX);
1.32      kristaps 1080:        p->engine.ps.lastf = f;
                   1081:
                   1082:        /*
                   1083:         * If we're still at the top of the page, let the font-setting
                   1084:         * be delayed until we actually have stuff to print.
                   1085:         */
                   1086:
                   1087:        if (PS_NEWPAGE & p->engine.ps.flags)
                   1088:                return;
                   1089:
1.37      kristaps 1090:        if (TERMTYPE_PS == p->type)
                   1091:                ps_printf(p, "/%s %zu selectfont\n",
                   1092:                                fonts[(int)f].name,
                   1093:                                p->engine.ps.scale);
                   1094:        else
                   1095:                ps_printf(p, "/F%d %zu Tf\n",
                   1096:                                (int)f,
                   1097:                                p->engine.ps.scale);
1.9       kristaps 1098: }
                   1099:
1.11      kristaps 1100:
1.12      kristaps 1101: /* ARGSUSED */
1.11      kristaps 1102: static size_t
                   1103: ps_width(const struct termp *p, char c)
                   1104: {
                   1105:
1.14      kristaps 1106:        if (c <= 32 || c - 32 >= MAXCHAR)
1.36      kristaps 1107:                return((size_t)fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14      kristaps 1108:
                   1109:        c -= 32;
1.36      kristaps 1110:        return((size_t)fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1.11      kristaps 1111: }
1.19      kristaps 1112:
                   1113:
1.20      kristaps 1114: static double
1.19      kristaps 1115: ps_hspan(const struct termp *p, const struct roffsu *su)
                   1116: {
                   1117:        double           r;
                   1118:
                   1119:        /*
                   1120:         * All of these measurements are derived by converting from the
1.21      kristaps 1121:         * native measurement to AFM units.
1.19      kristaps 1122:         */
                   1123:
                   1124:        switch (su->unit) {
                   1125:        case (SCALE_CM):
1.21      kristaps 1126:                r = PNT2AFM(p, su->scale * 28.34);
1.19      kristaps 1127:                break;
                   1128:        case (SCALE_IN):
1.21      kristaps 1129:                r = PNT2AFM(p, su->scale * 72);
1.19      kristaps 1130:                break;
                   1131:        case (SCALE_PC):
1.21      kristaps 1132:                r = PNT2AFM(p, su->scale * 12);
1.19      kristaps 1133:                break;
                   1134:        case (SCALE_PT):
1.21      kristaps 1135:                r = PNT2AFM(p, su->scale * 100);
1.19      kristaps 1136:                break;
                   1137:        case (SCALE_EM):
1.36      kristaps 1138:                r = su->scale *
1.19      kristaps 1139:                        fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
                   1140:                break;
                   1141:        case (SCALE_MM):
1.21      kristaps 1142:                r = PNT2AFM(p, su->scale * 2.834);
1.19      kristaps 1143:                break;
                   1144:        case (SCALE_EN):
1.36      kristaps 1145:                r = su->scale *
1.19      kristaps 1146:                        fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
                   1147:                break;
                   1148:        case (SCALE_VS):
                   1149:                r = su->scale * p->engine.ps.lineheight;
                   1150:                break;
                   1151:        default:
                   1152:                r = su->scale;
                   1153:                break;
                   1154:        }
                   1155:
1.20      kristaps 1156:        return(r);
1.19      kristaps 1157: }
                   1158:

CVSweb