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

Annotation of mandoc/term_ps.c, Revision 1.48

1.48    ! kristaps    1: /*     $Id: term_ps.c,v 1.47 2011/03/07 01:58:24 schwarze 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.48    ! kristaps  369:        p->engine.ps.psmarg = mandoc_realloc
1.41      kristaps  370:                (p->engine.ps.psmarg,
                    371:                 p->engine.ps.psmargsz);
1.40      joerg     372: }
1.9       kristaps  373:
1.20      kristaps  374: static double            ps_hspan(const struct termp *,
1.19      kristaps  375:                                const struct roffsu *);
                    376: static size_t            ps_width(const struct termp *, char);
                    377: static void              ps_advance(struct termp *, size_t);
1.2       kristaps  378: static void              ps_begin(struct termp *);
1.37      kristaps  379: static void              ps_closepage(struct termp *);
1.2       kristaps  380: static void              ps_end(struct termp *);
                    381: static void              ps_endline(struct termp *);
1.9       kristaps  382: static void              ps_fclose(struct termp *);
1.19      kristaps  383: static void              ps_letter(struct termp *, char);
1.9       kristaps  384: static void              ps_pclose(struct termp *);
1.13      kristaps  385: static void              ps_pletter(struct termp *, int);
1.4       kristaps  386: static void              ps_printf(struct termp *, const char *, ...);
                    387: static void              ps_putchar(struct termp *, char);
1.9       kristaps  388: static void              ps_setfont(struct termp *, enum termfont);
1.37      kristaps  389: static struct termp     *pspdf_alloc(char *);
1.39      kristaps  390: static void              pdf_obj(struct termp *, size_t);
1.37      kristaps  391:
                    392:
                    393: void *
                    394: pdf_alloc(char *outopts)
                    395: {
                    396:        struct termp    *p;
                    397:
1.38      kristaps  398:        if (NULL != (p = pspdf_alloc(outopts)))
                    399:                p->type = TERMTYPE_PDF;
1.37      kristaps  400:
                    401:        return(p);
                    402: }
1.2       kristaps  403:
                    404:
1.1       kristaps  405: void *
1.17      kristaps  406: ps_alloc(char *outopts)
1.1       kristaps  407: {
                    408:        struct termp    *p;
1.37      kristaps  409:
1.38      kristaps  410:        if (NULL != (p = pspdf_alloc(outopts)))
                    411:                p->type = TERMTYPE_PS;
1.37      kristaps  412:
                    413:        return(p);
                    414: }
                    415:
                    416:
                    417: static struct termp *
                    418: pspdf_alloc(char *outopts)
                    419: {
                    420:        struct termp    *p;
1.28      kristaps  421:        size_t           pagex, pagey, marginx, marginy, lineheight;
1.17      kristaps  422:        const char      *toks[2];
1.25      kristaps  423:        const char      *pp;
1.17      kristaps  424:        char            *v;
1.1       kristaps  425:
1.46      kristaps  426:        p = term_alloc(TERMENC_ASCII);
1.1       kristaps  427:
1.19      kristaps  428:        p->advance = ps_advance;
1.2       kristaps  429:        p->begin = ps_begin;
                    430:        p->end = ps_end;
                    431:        p->endline = ps_endline;
1.19      kristaps  432:        p->hspan = ps_hspan;
                    433:        p->letter = ps_letter;
1.11      kristaps  434:        p->width = ps_width;
1.21      kristaps  435:
1.17      kristaps  436:        toks[0] = "paper";
                    437:        toks[1] = NULL;
                    438:
1.25      kristaps  439:        pp = NULL;
1.19      kristaps  440:
1.17      kristaps  441:        while (outopts && *outopts)
                    442:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    443:                case (0):
1.25      kristaps  444:                        pp = v;
1.17      kristaps  445:                        break;
                    446:                default:
                    447:                        break;
                    448:                }
1.16      kristaps  449:
1.25      kristaps  450:        /* Default to US letter (millimetres). */
                    451:
1.31      kristaps  452:        pagex = 216;
                    453:        pagey = 279;
1.25      kristaps  454:
                    455:        /*
                    456:         * The ISO-269 paper sizes can be calculated automatically, but
                    457:         * it would require bringing in -lm for pow() and I'd rather not
                    458:         * do that.  So just do it the easy way for now.  Since this
                    459:         * only happens once, I'm not terribly concerned.
                    460:         */
                    461:
                    462:        if (pp && strcasecmp(pp, "letter")) {
                    463:                if (0 == strcasecmp(pp, "a3")) {
                    464:                        pagex = 297;
                    465:                        pagey = 420;
                    466:                } else if (0 == strcasecmp(pp, "a4")) {
                    467:                        pagex = 210;
                    468:                        pagey = 297;
                    469:                } else if (0 == strcasecmp(pp, "a5")) {
                    470:                        pagex = 148;
                    471:                        pagey = 210;
                    472:                } else if (0 == strcasecmp(pp, "legal")) {
                    473:                        pagex = 216;
                    474:                        pagey = 356;
                    475:                } else if (2 != sscanf(pp, "%zux%zu", &pagex, &pagey))
                    476:                        fprintf(stderr, "%s: Unknown paper\n", pp);
1.27      kristaps  477:        } else if (NULL == pp)
                    478:                pp = "letter";
                    479:
                    480:        /*
                    481:         * This MUST be defined before any PNT2AFM or AFM2PNT
                    482:         * calculations occur.
                    483:         */
                    484:
                    485:        p->engine.ps.scale = 11;
                    486:
1.25      kristaps  487:        /* Remember millimetres -> AFM units. */
                    488:
                    489:        pagex = PNT2AFM(p, ((double)pagex * 2.834));
                    490:        pagey = PNT2AFM(p, ((double)pagey * 2.834));
                    491:
1.28      kristaps  492:        /* Margins are 1/9 the page x and y. */
1.27      kristaps  493:
1.29      kristaps  494:        marginx = /* LINTED */
                    495:                (size_t)((double)pagex / 9.0);
                    496:        marginy = /* LINTED */
                    497:                (size_t)((double)pagey / 9.0);
1.27      kristaps  498:
1.31      kristaps  499:        /* Line-height is 1.4em. */
                    500:
                    501:        lineheight = PNT2AFM(p, ((double)p->engine.ps.scale * 1.4));
1.16      kristaps  502:
                    503:        p->engine.ps.width = pagex;
                    504:        p->engine.ps.height = pagey;
1.28      kristaps  505:        p->engine.ps.header = pagey - (marginy / 2) - (lineheight / 2);
                    506:        p->engine.ps.top = pagey - marginy;
                    507:        p->engine.ps.footer = (marginy / 2) - (lineheight / 2);
                    508:        p->engine.ps.bottom = marginy;
                    509:        p->engine.ps.left = marginx;
1.18      kristaps  510:        p->engine.ps.lineheight = lineheight;
1.16      kristaps  511:
1.28      kristaps  512:        p->defrmargin = pagex - (marginx * 2);
1.1       kristaps  513:        return(p);
                    514: }
                    515:
                    516:
                    517: void
1.37      kristaps  518: pspdf_free(void *arg)
1.1       kristaps  519: {
1.4       kristaps  520:        struct termp    *p;
                    521:
                    522:        p = (struct termp *)arg;
                    523:
                    524:        if (p->engine.ps.psmarg)
                    525:                free(p->engine.ps.psmarg);
1.39      kristaps  526:        if (p->engine.ps.pdfobjs)
                    527:                free(p->engine.ps.pdfobjs);
1.4       kristaps  528:
                    529:        term_free(p);
                    530: }
                    531:
                    532:
                    533: static void
                    534: ps_printf(struct termp *p, const char *fmt, ...)
                    535: {
                    536:        va_list          ap;
1.37      kristaps  537:        int              pos, len;
1.4       kristaps  538:
                    539:        va_start(ap, fmt);
                    540:
                    541:        /*
                    542:         * If we're running in regular mode, then pipe directly into
                    543:         * vprintf().  If we're processing margins, then push the data
                    544:         * into our growable margin buffer.
                    545:         */
1.1       kristaps  546:
1.29      kristaps  547:        if ( ! (PS_MARGINS & p->engine.ps.flags)) {
1.37      kristaps  548:                len = vprintf(fmt, ap);
1.4       kristaps  549:                va_end(ap);
1.37      kristaps  550:                p->engine.ps.pdfbytes += /* LINTED */
                    551:                        len < 0 ? 0 : (size_t)len;
1.4       kristaps  552:                return;
                    553:        }
                    554:
                    555:        /*
                    556:         * XXX: I assume that the in-margin print won't exceed
                    557:         * PS_BUFSLOP (128 bytes), which is reasonable but still an
                    558:         * assumption that will cause pukeage if it's not the case.
                    559:         */
                    560:
1.40      joerg     561:        ps_growbuf(p, PS_BUFSLOP);
1.4       kristaps  562:
                    563:        pos = (int)p->engine.ps.psmargcur;
1.37      kristaps  564:        len = vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
1.5       kristaps  565:
                    566:        va_end(ap);
1.37      kristaps  567:
                    568:        p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
1.4       kristaps  569: }
                    570:
                    571:
                    572: static void
                    573: ps_putchar(struct termp *p, char c)
                    574: {
                    575:        int              pos;
                    576:
                    577:        /* See ps_printf(). */
                    578:
1.29      kristaps  579:        if ( ! (PS_MARGINS & p->engine.ps.flags)) {
1.44      kristaps  580:                /* LINTED */
1.4       kristaps  581:                putchar(c);
1.37      kristaps  582:                p->engine.ps.pdfbytes++;
1.4       kristaps  583:                return;
                    584:        }
                    585:
1.40      joerg     586:        ps_growbuf(p, 2);
1.4       kristaps  587:
                    588:        pos = (int)p->engine.ps.psmargcur++;
1.5       kristaps  589:        p->engine.ps.psmarg[pos++] = c;
1.4       kristaps  590:        p->engine.ps.psmarg[pos] = '\0';
1.2       kristaps  591: }
                    592:
                    593:
1.37      kristaps  594: static void
1.39      kristaps  595: pdf_obj(struct termp *p, size_t obj)
                    596: {
                    597:
                    598:        assert(obj > 0);
                    599:
                    600:        if ((obj - 1) >= p->engine.ps.pdfobjsz) {
                    601:                p->engine.ps.pdfobjsz = obj + 128;
                    602:                p->engine.ps.pdfobjs = realloc
                    603:                        (p->engine.ps.pdfobjs,
                    604:                         p->engine.ps.pdfobjsz * sizeof(size_t));
                    605:                if (NULL == p->engine.ps.pdfobjs) {
                    606:                        perror(NULL);
1.44      kristaps  607:                        exit((int)MANDOCLEVEL_SYSERR);
1.39      kristaps  608:                }
                    609:        }
                    610:
                    611:        p->engine.ps.pdfobjs[(int)obj - 1] = p->engine.ps.pdfbytes;
                    612:        ps_printf(p, "%zu 0 obj\n", obj);
                    613: }
                    614:
                    615:
                    616: static void
1.37      kristaps  617: ps_closepage(struct termp *p)
                    618: {
                    619:        int              i;
1.38      kristaps  620:        size_t           len, base;
                    621:
                    622:        /*
                    623:         * Close out a page that we've already flushed to output.  In
                    624:         * PostScript, we simply note that the page must be showed.  In
                    625:         * PDF, we must now create the Length, Resource, and Page node
                    626:         * for the page contents.
                    627:         */
1.37      kristaps  628:
                    629:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    630:        ps_printf(p, "%s", p->engine.ps.psmarg);
                    631:
1.38      kristaps  632:        if (TERMTYPE_PS != p->type) {
1.37      kristaps  633:                ps_printf(p, "ET\n");
1.38      kristaps  634:
1.37      kristaps  635:                len = p->engine.ps.pdfbytes - p->engine.ps.pdflastpg;
1.38      kristaps  636:                base = p->engine.ps.pages * 4 + p->engine.ps.pdfbody;
                    637:
                    638:                ps_printf(p, "endstream\nendobj\n");
                    639:
                    640:                /* Length of content. */
1.39      kristaps  641:                pdf_obj(p, base + 1);
1.38      kristaps  642:                ps_printf(p, "%zu\nendobj\n", len);
                    643:
                    644:                /* Resource for content. */
1.39      kristaps  645:                pdf_obj(p, base + 2);
1.38      kristaps  646:                ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
1.37      kristaps  647:                ps_printf(p, "/Font <<\n");
1.39      kristaps  648:                for (i = 0; i < (int)TERMFONT__MAX; i++)
1.37      kristaps  649:                        ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
1.38      kristaps  650:                ps_printf(p, ">>\n>>\n");
                    651:
                    652:                /* Page node. */
1.39      kristaps  653:                pdf_obj(p, base + 3);
                    654:                ps_printf(p, "<<\n");
1.37      kristaps  655:                ps_printf(p, "/Type /Page\n");
                    656:                ps_printf(p, "/Parent 2 0 R\n");
1.38      kristaps  657:                ps_printf(p, "/Resources %zu 0 R\n", base + 2);
                    658:                ps_printf(p, "/Contents %zu 0 R\n", base);
                    659:                ps_printf(p, ">>\nendobj\n");
                    660:        } else
                    661:                ps_printf(p, "showpage\n");
1.37      kristaps  662:
                    663:        p->engine.ps.pages++;
                    664:        p->engine.ps.psrow = p->engine.ps.top;
                    665:        assert( ! (PS_NEWPAGE & p->engine.ps.flags));
                    666:        p->engine.ps.flags |= PS_NEWPAGE;
                    667: }
                    668:
                    669:
1.3       kristaps  670: /* ARGSUSED */
1.2       kristaps  671: static void
                    672: ps_end(struct termp *p)
                    673: {
1.38      kristaps  674:        size_t           i, xref, base;
1.2       kristaps  675:
1.4       kristaps  676:        /*
                    677:         * At the end of the file, do one last showpage.  This is the
                    678:         * same behaviour as groff(1) and works for multiple pages as
                    679:         * well as just one.
                    680:         */
                    681:
1.30      kristaps  682:        if ( ! (PS_NEWPAGE & p->engine.ps.flags)) {
                    683:                assert(0 == p->engine.ps.flags);
                    684:                assert('\0' == p->engine.ps.last);
1.37      kristaps  685:                ps_closepage(p);
1.30      kristaps  686:        }
1.16      kristaps  687:
1.37      kristaps  688:        if (TERMTYPE_PS == p->type) {
                    689:                ps_printf(p, "%%%%Trailer\n");
                    690:                ps_printf(p, "%%%%Pages: %zu\n", p->engine.ps.pages);
                    691:                ps_printf(p, "%%%%EOF\n");
                    692:                return;
                    693:        }
                    694:
1.39      kristaps  695:        pdf_obj(p, 2);
                    696:        ps_printf(p, "<<\n/Type /Pages\n");
1.37      kristaps  697:        ps_printf(p, "/MediaBox [0 0 %zu %zu]\n",
                    698:                        (size_t)AFM2PNT(p, p->engine.ps.width),
                    699:                        (size_t)AFM2PNT(p, p->engine.ps.height));
                    700:
                    701:        ps_printf(p, "/Count %zu\n", p->engine.ps.pages);
                    702:        ps_printf(p, "/Kids [");
                    703:
                    704:        for (i = 0; i < p->engine.ps.pages; i++)
1.38      kristaps  705:                ps_printf(p, " %zu 0 R", i * 4 +
                    706:                                p->engine.ps.pdfbody + 3);
                    707:
                    708:        base = (p->engine.ps.pages - 1) * 4 +
                    709:                p->engine.ps.pdfbody + 4;
                    710:
                    711:        ps_printf(p, "]\n>>\nendobj\n");
1.39      kristaps  712:        pdf_obj(p, base);
1.37      kristaps  713:        ps_printf(p, "<<\n");
                    714:        ps_printf(p, "/Type /Catalog\n");
                    715:        ps_printf(p, "/Pages 2 0 R\n");
                    716:        ps_printf(p, ">>\n");
                    717:        xref = p->engine.ps.pdfbytes;
                    718:        ps_printf(p, "xref\n");
1.38      kristaps  719:        ps_printf(p, "0 %zu\n", base + 1);
1.39      kristaps  720:        ps_printf(p, "0000000000 65535 f \n");
                    721:
                    722:        for (i = 0; i < base; i++)
                    723:                ps_printf(p, "%.10zu 00000 n \n",
                    724:                                p->engine.ps.pdfobjs[(int)i]);
                    725:
1.37      kristaps  726:        ps_printf(p, "trailer\n");
                    727:        ps_printf(p, "<<\n");
1.38      kristaps  728:        ps_printf(p, "/Size %zu\n", base + 1);
                    729:        ps_printf(p, "/Root %zu 0 R\n", base);
1.37      kristaps  730:        ps_printf(p, "/Info 1 0 R\n");
                    731:        ps_printf(p, ">>\n");
                    732:        ps_printf(p, "startxref\n");
                    733:        ps_printf(p, "%zu\n", xref);
                    734:        ps_printf(p, "%%%%EOF\n");
1.2       kristaps  735: }
                    736:
                    737:
                    738: static void
                    739: ps_begin(struct termp *p)
                    740: {
1.13      kristaps  741:        time_t           t;
1.16      kristaps  742:        int              i;
1.2       kristaps  743:
1.9       kristaps  744:        /*
                    745:         * Print margins into margin buffer.  Nothing gets output to the
                    746:         * screen yet, so we don't need to initialise the primary state.
1.2       kristaps  747:         */
                    748:
1.4       kristaps  749:        if (p->engine.ps.psmarg) {
                    750:                assert(p->engine.ps.psmargsz);
                    751:                p->engine.ps.psmarg[0] = '\0';
                    752:        }
                    753:
1.39      kristaps  754:        /*p->engine.ps.pdfbytes = 0;*/
1.4       kristaps  755:        p->engine.ps.psmargcur = 0;
1.29      kristaps  756:        p->engine.ps.flags = PS_MARGINS;
1.16      kristaps  757:        p->engine.ps.pscol = p->engine.ps.left;
                    758:        p->engine.ps.psrow = p->engine.ps.header;
1.4       kristaps  759:
1.9       kristaps  760:        ps_setfont(p, TERMFONT_NONE);
1.4       kristaps  761:
                    762:        (*p->headf)(p, p->argf);
                    763:        (*p->endline)(p);
                    764:
1.16      kristaps  765:        p->engine.ps.pscol = p->engine.ps.left;
                    766:        p->engine.ps.psrow = p->engine.ps.footer;
1.4       kristaps  767:
                    768:        (*p->footf)(p, p->argf);
                    769:        (*p->endline)(p);
                    770:
1.29      kristaps  771:        p->engine.ps.flags &= ~PS_MARGINS;
1.9       kristaps  772:
1.29      kristaps  773:        assert(0 == p->engine.ps.flags);
1.9       kristaps  774:        assert(p->engine.ps.psmarg);
                    775:        assert('\0' != p->engine.ps.psmarg[0]);
                    776:
                    777:        /*
                    778:         * Print header and initialise page state.  Following this,
                    779:         * stuff gets printed to the screen, so make sure we're sane.
                    780:         */
1.4       kristaps  781:
1.13      kristaps  782:        t = time(NULL);
                    783:
1.37      kristaps  784:        if (TERMTYPE_PS == p->type) {
                    785:                ps_printf(p, "%%!PS-Adobe-3.0\n");
                    786:                ps_printf(p, "%%%%CreationDate: %s", ctime(&t));
                    787:                ps_printf(p, "%%%%DocumentData: Clean7Bit\n");
                    788:                ps_printf(p, "%%%%Orientation: Portrait\n");
                    789:                ps_printf(p, "%%%%Pages: (atend)\n");
                    790:                ps_printf(p, "%%%%PageOrder: Ascend\n");
                    791:                ps_printf(p, "%%%%DocumentMedia: "
                    792:                                "Default %zu %zu 0 () ()\n",
                    793:                                (size_t)AFM2PNT(p, p->engine.ps.width),
                    794:                                (size_t)AFM2PNT(p, p->engine.ps.height));
                    795:                ps_printf(p, "%%%%DocumentNeededResources: font");
                    796:
                    797:                for (i = 0; i < (int)TERMFONT__MAX; i++)
                    798:                        ps_printf(p, " %s", fonts[i].name);
                    799:
                    800:                ps_printf(p, "\n%%%%EndComments\n");
                    801:        } else {
                    802:                ps_printf(p, "%%PDF-1.1\n");
1.39      kristaps  803:                pdf_obj(p, 1);
1.37      kristaps  804:                ps_printf(p, "<<\n");
                    805:                ps_printf(p, ">>\n");
                    806:                ps_printf(p, "endobj\n");
                    807:
                    808:                for (i = 0; i < (int)TERMFONT__MAX; i++) {
1.39      kristaps  809:                        pdf_obj(p, (size_t)i + 3);
1.37      kristaps  810:                        ps_printf(p, "<<\n");
                    811:                        ps_printf(p, "/Type /Font\n");
                    812:                        ps_printf(p, "/Subtype /Type1\n");
                    813:                        ps_printf(p, "/Name /F%zu\n", i);
                    814:                        ps_printf(p, "/BaseFont /%s\n", fonts[i].name);
                    815:                        ps_printf(p, ">>\n");
                    816:                }
                    817:        }
1.16      kristaps  818:
1.37      kristaps  819:        p->engine.ps.pdfbody = (size_t)TERMFONT__MAX + 3;
1.16      kristaps  820:        p->engine.ps.pscol = p->engine.ps.left;
                    821:        p->engine.ps.psrow = p->engine.ps.top;
1.29      kristaps  822:        p->engine.ps.flags |= PS_NEWPAGE;
1.33      kristaps  823:        ps_setfont(p, TERMFONT_NONE);
1.2       kristaps  824: }
                    825:
                    826:
                    827: static void
1.13      kristaps  828: ps_pletter(struct termp *p, int c)
1.2       kristaps  829: {
1.14      kristaps  830:        int              f;
1.32      kristaps  831:
1.9       kristaps  832:        /*
1.32      kristaps  833:         * If we haven't opened a page context, then output that we're
                    834:         * in a new page and make sure the font is correctly set.
1.9       kristaps  835:         */
                    836:
1.32      kristaps  837:        if (PS_NEWPAGE & p->engine.ps.flags) {
1.37      kristaps  838:                if (TERMTYPE_PS == p->type) {
                    839:                        ps_printf(p, "%%%%Page: %zu %zu\n",
                    840:                                        p->engine.ps.pages + 1,
                    841:                                        p->engine.ps.pages + 1);
                    842:                        ps_printf(p, "/%s %zu selectfont\n",
                    843:                                        fonts[(int)p->engine.ps.lastf].name,
                    844:                                        p->engine.ps.scale);
                    845:                } else {
1.39      kristaps  846:                        pdf_obj(p, p->engine.ps.pdfbody +
1.38      kristaps  847:                                        p->engine.ps.pages * 4);
1.37      kristaps  848:                        ps_printf(p, "<<\n");
                    849:                        ps_printf(p, "/Length %zu 0 R\n",
1.38      kristaps  850:                                        p->engine.ps.pdfbody + 1 +
                    851:                                        p->engine.ps.pages * 4);
1.37      kristaps  852:                        ps_printf(p, ">>\nstream\n");
                    853:                }
                    854:                p->engine.ps.pdflastpg = p->engine.ps.pdfbytes;
1.32      kristaps  855:                p->engine.ps.flags &= ~PS_NEWPAGE;
                    856:        }
                    857:
                    858:        /*
                    859:         * If we're not in a PostScript "word" context, then open one
                    860:         * now at the current cursor.
                    861:         */
1.30      kristaps  862:
1.29      kristaps  863:        if ( ! (PS_INLINE & p->engine.ps.flags)) {
1.37      kristaps  864:                if (TERMTYPE_PS != p->type) {
                    865:                        ps_printf(p, "BT\n/F%d %zu Tf\n",
                    866:                                        (int)p->engine.ps.lastf,
                    867:                                        p->engine.ps.scale);
                    868:                        ps_printf(p, "%.3f %.3f Td\n(",
                    869:                                        AFM2PNT(p, p->engine.ps.pscol),
                    870:                                        AFM2PNT(p, p->engine.ps.psrow));
                    871:                } else
                    872:                        ps_printf(p, "%.3f %.3f moveto\n(",
                    873:                                        AFM2PNT(p, p->engine.ps.pscol),
                    874:                                        AFM2PNT(p, p->engine.ps.psrow));
1.29      kristaps  875:                p->engine.ps.flags |= PS_INLINE;
1.2       kristaps  876:        }
                    877:
1.29      kristaps  878:        assert( ! (PS_NEWPAGE & p->engine.ps.flags));
                    879:
1.2       kristaps  880:        /*
                    881:         * We need to escape these characters as per the PostScript
                    882:         * specification.  We would also escape non-graphable characters
                    883:         * (like tabs), but none of them would get to this point and
                    884:         * it's superfluous to abort() on them.
                    885:         */
                    886:
                    887:        switch (c) {
                    888:        case ('('):
                    889:                /* FALLTHROUGH */
                    890:        case (')'):
                    891:                /* FALLTHROUGH */
                    892:        case ('\\'):
1.4       kristaps  893:                ps_putchar(p, '\\');
1.2       kristaps  894:                break;
                    895:        default:
                    896:                break;
                    897:        }
                    898:
                    899:        /* Write the character and adjust where we are on the page. */
1.9       kristaps  900:
1.14      kristaps  901:        f = (int)p->engine.ps.lastf;
1.13      kristaps  902:
1.45      schwarze  903:        if (c <= 32 || (c - 32 >= MAXCHAR)) {
1.13      kristaps  904:                ps_putchar(p, ' ');
1.36      kristaps  905:                p->engine.ps.pscol += (size_t)fonts[f].gly[0].wx;
1.13      kristaps  906:                return;
                    907:        }
                    908:
1.25      kristaps  909:        ps_putchar(p, (char)c);
1.14      kristaps  910:        c -= 32;
1.36      kristaps  911:        p->engine.ps.pscol += (size_t)fonts[f].gly[c].wx;
1.2       kristaps  912: }
                    913:
                    914:
                    915: static void
1.9       kristaps  916: ps_pclose(struct termp *p)
                    917: {
                    918:
                    919:        /*
                    920:         * Spit out that we're exiting a word context (this is a
                    921:         * "partial close" because we don't check the last-char buffer
                    922:         * or anything).
                    923:         */
                    924:
1.29      kristaps  925:        if ( ! (PS_INLINE & p->engine.ps.flags))
1.9       kristaps  926:                return;
                    927:
1.37      kristaps  928:        if (TERMTYPE_PS != p->type) {
                    929:                ps_printf(p, ") Tj\nET\n");
                    930:        } else
                    931:                ps_printf(p, ") show\n");
                    932:
1.29      kristaps  933:        p->engine.ps.flags &= ~PS_INLINE;
1.9       kristaps  934: }
                    935:
                    936:
                    937: static void
                    938: ps_fclose(struct termp *p)
                    939: {
                    940:
                    941:        /*
                    942:         * Strong closure: if we have a last-char, spit it out after
                    943:         * checking that we're in the right font mode.  This will of
                    944:         * course open a new scope, if applicable.
                    945:         *
                    946:         * Following this, close out any scope that's open.
                    947:         */
                    948:
                    949:        if ('\0' != p->engine.ps.last) {
                    950:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    951:                        ps_pclose(p);
                    952:                        ps_setfont(p, TERMFONT_NONE);
                    953:                }
                    954:                ps_pletter(p, p->engine.ps.last);
                    955:                p->engine.ps.last = '\0';
                    956:        }
                    957:
1.29      kristaps  958:        if ( ! (PS_INLINE & p->engine.ps.flags))
1.9       kristaps  959:                return;
                    960:
                    961:        ps_pclose(p);
                    962: }
                    963:
                    964:
                    965: static void
1.8       kristaps  966: ps_letter(struct termp *p, char c)
                    967: {
                    968:        char            cc;
1.9       kristaps  969:
                    970:        /*
                    971:         * State machine dictates whether to buffer the last character
                    972:         * or not.  Basically, encoded words are detected by checking if
                    973:         * we're an "8" and switching on the buffer.  Then we put "8" in
                    974:         * our buffer, and on the next charater, flush both character
                    975:         * and buffer.  Thus, "regular" words are detected by having a
                    976:         * regular character and a regular buffer character.
                    977:         */
                    978:
1.8       kristaps  979:        if ('\0' == p->engine.ps.last) {
                    980:                assert(8 != c);
                    981:                p->engine.ps.last = c;
                    982:                return;
                    983:        } else if (8 == p->engine.ps.last) {
                    984:                assert(8 != c);
1.9       kristaps  985:                p->engine.ps.last = '\0';
1.8       kristaps  986:        } else if (8 == c) {
                    987:                assert(8 != p->engine.ps.last);
1.9       kristaps  988:                if ('_' == p->engine.ps.last) {
                    989:                        if (p->engine.ps.lastf != TERMFONT_UNDER) {
                    990:                                ps_pclose(p);
                    991:                                ps_setfont(p, TERMFONT_UNDER);
                    992:                        }
                    993:                } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
                    994:                        ps_pclose(p);
                    995:                        ps_setfont(p, TERMFONT_BOLD);
                    996:                }
1.8       kristaps  997:                p->engine.ps.last = c;
                    998:                return;
                    999:        } else {
1.9       kristaps 1000:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                   1001:                        ps_pclose(p);
                   1002:                        ps_setfont(p, TERMFONT_NONE);
                   1003:                }
1.8       kristaps 1004:                cc = p->engine.ps.last;
                   1005:                p->engine.ps.last = c;
                   1006:                c = cc;
                   1007:        }
                   1008:
1.9       kristaps 1009:        ps_pletter(p, c);
1.8       kristaps 1010: }
                   1011:
                   1012:
                   1013: static void
1.2       kristaps 1014: ps_advance(struct termp *p, size_t len)
                   1015: {
                   1016:
1.9       kristaps 1017:        /*
                   1018:         * Advance some spaces.  This can probably be made smarter,
                   1019:         * i.e., to have multiple space-separated words in the same
                   1020:         * scope, but this is easier:  just close out the current scope
                   1021:         * and readjust our column settings.
                   1022:         */
1.8       kristaps 1023:
1.9       kristaps 1024:        ps_fclose(p);
1.14      kristaps 1025:        p->engine.ps.pscol += len;
1.2       kristaps 1026: }
                   1027:
                   1028:
                   1029: static void
                   1030: ps_endline(struct termp *p)
                   1031: {
1.8       kristaps 1032:
1.9       kristaps 1033:        /* Close out any scopes we have open: we're at eoln. */
                   1034:
                   1035:        ps_fclose(p);
1.2       kristaps 1036:
1.9       kristaps 1037:        /*
                   1038:         * If we're in the margin, don't try to recalculate our current
                   1039:         * row.  XXX: if the column tries to be fancy with multiple
                   1040:         * lines, we'll do nasty stuff.
                   1041:         */
1.2       kristaps 1042:
1.29      kristaps 1043:        if (PS_MARGINS & p->engine.ps.flags)
                   1044:                return;
                   1045:
                   1046:        /* Left-justify. */
                   1047:
                   1048:        p->engine.ps.pscol = p->engine.ps.left;
                   1049:
                   1050:        /* If we haven't printed anything, return. */
                   1051:
                   1052:        if (PS_NEWPAGE & p->engine.ps.flags)
1.2       kristaps 1053:                return;
                   1054:
1.9       kristaps 1055:        /*
                   1056:         * Put us down a line.  If we're at the page bottom, spit out a
                   1057:         * showpage and restart our row.
                   1058:         */
                   1059:
1.16      kristaps 1060:        if (p->engine.ps.psrow >= p->engine.ps.lineheight +
                   1061:                        p->engine.ps.bottom) {
                   1062:                p->engine.ps.psrow -= p->engine.ps.lineheight;
1.2       kristaps 1063:                return;
                   1064:        }
                   1065:
1.37      kristaps 1066:        ps_closepage(p);
1.1       kristaps 1067: }
1.9       kristaps 1068:
                   1069:
                   1070: static void
                   1071: ps_setfont(struct termp *p, enum termfont f)
                   1072: {
                   1073:
1.16      kristaps 1074:        assert(f < TERMFONT__MAX);
1.32      kristaps 1075:        p->engine.ps.lastf = f;
                   1076:
                   1077:        /*
                   1078:         * If we're still at the top of the page, let the font-setting
                   1079:         * be delayed until we actually have stuff to print.
                   1080:         */
                   1081:
                   1082:        if (PS_NEWPAGE & p->engine.ps.flags)
                   1083:                return;
                   1084:
1.37      kristaps 1085:        if (TERMTYPE_PS == p->type)
                   1086:                ps_printf(p, "/%s %zu selectfont\n",
                   1087:                                fonts[(int)f].name,
                   1088:                                p->engine.ps.scale);
                   1089:        else
                   1090:                ps_printf(p, "/F%d %zu Tf\n",
                   1091:                                (int)f,
                   1092:                                p->engine.ps.scale);
1.9       kristaps 1093: }
                   1094:
1.11      kristaps 1095:
1.12      kristaps 1096: /* ARGSUSED */
1.11      kristaps 1097: static size_t
                   1098: ps_width(const struct termp *p, char c)
                   1099: {
                   1100:
1.14      kristaps 1101:        if (c <= 32 || c - 32 >= MAXCHAR)
1.36      kristaps 1102:                return((size_t)fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14      kristaps 1103:
                   1104:        c -= 32;
1.36      kristaps 1105:        return((size_t)fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1.11      kristaps 1106: }
1.19      kristaps 1107:
                   1108:
1.20      kristaps 1109: static double
1.19      kristaps 1110: ps_hspan(const struct termp *p, const struct roffsu *su)
                   1111: {
                   1112:        double           r;
                   1113:
                   1114:        /*
                   1115:         * All of these measurements are derived by converting from the
1.21      kristaps 1116:         * native measurement to AFM units.
1.19      kristaps 1117:         */
                   1118:
                   1119:        switch (su->unit) {
                   1120:        case (SCALE_CM):
1.21      kristaps 1121:                r = PNT2AFM(p, su->scale * 28.34);
1.19      kristaps 1122:                break;
                   1123:        case (SCALE_IN):
1.21      kristaps 1124:                r = PNT2AFM(p, su->scale * 72);
1.19      kristaps 1125:                break;
                   1126:        case (SCALE_PC):
1.21      kristaps 1127:                r = PNT2AFM(p, su->scale * 12);
1.19      kristaps 1128:                break;
                   1129:        case (SCALE_PT):
1.21      kristaps 1130:                r = PNT2AFM(p, su->scale * 100);
1.19      kristaps 1131:                break;
                   1132:        case (SCALE_EM):
1.36      kristaps 1133:                r = su->scale *
1.19      kristaps 1134:                        fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
                   1135:                break;
                   1136:        case (SCALE_MM):
1.21      kristaps 1137:                r = PNT2AFM(p, su->scale * 2.834);
1.19      kristaps 1138:                break;
                   1139:        case (SCALE_EN):
1.36      kristaps 1140:                r = su->scale *
1.19      kristaps 1141:                        fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
                   1142:                break;
                   1143:        case (SCALE_VS):
                   1144:                r = su->scale * p->engine.ps.lineheight;
                   1145:                break;
                   1146:        default:
                   1147:                r = su->scale;
                   1148:                break;
                   1149:        }
                   1150:
1.20      kristaps 1151:        return(r);
1.19      kristaps 1152: }
                   1153:

CVSweb