[BACK]Return to term_ps.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/term_ps.c, Revision 1.6

1.6     ! schwarze    1: /*     $Id: term_ps.c,v 1.5 2010/06/29 15:49:52 schwarze Exp $ */
1.1       schwarze    2: /*
1.6     ! schwarze    3:  * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       schwarze    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:
                     21: #include <sys/param.h>
                     22:
                     23: #include <assert.h>
                     24: #include <stdarg.h>
1.5       schwarze   25: #include <stdint.h>
1.1       schwarze   26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
1.4       schwarze   29: #include <time.h>
1.6     ! schwarze   30: #include <unistd.h>
1.1       schwarze   31:
                     32: #include "out.h"
                     33: #include "main.h"
                     34: #include "term.h"
                     35:
1.6     ! schwarze   36: /* Convert PostScript point "x" to an AFM unit. */
        !            37: #define        PNT2AFM(p, x) /* LINTED */ \
        !            38:        (size_t)((double)(x) * (1000.0 / (double)(p)->engine.ps.scale))
        !            39:
        !            40: /* Convert an AFM unit "x" to a PostScript points */
        !            41: #define        AFM2PNT(p, x) /* LINTED */ \
        !            42:        (size_t)((double)(x) / (1000.0 / (double)(p)->engine.ps.scale))
        !            43:
1.4       schwarze   44: struct glyph {
1.6     ! schwarze   45:        size_t            wx; /* WX in AFM */
1.4       schwarze   46: };
                     47:
                     48: struct font {
                     49:        const char       *name; /* FontName in AFM */
                     50: #define        MAXCHAR           95 /* total characters we can handle */
                     51:        struct glyph      gly[MAXCHAR]; /* glyph metrics */
                     52: };
1.1       schwarze   53:
1.4       schwarze   54: /*
                     55:  * We define, for the time being, three fonts: bold, oblique/italic, and
                     56:  * normal (roman).  The following table hard-codes the font metrics for
                     57:  * ASCII, i.e., 32--127.
                     58:  */
                     59:
                     60: static const struct font fonts[TERMFONT__MAX] = {
1.6     ! schwarze   61:        { "Times-Roman", {
        !            62:                { 250 },
        !            63:                { 333 },
        !            64:                { 408 },
        !            65:                { 500 },
        !            66:                { 500 },
        !            67:                { 833 },
        !            68:                { 778 },
        !            69:                { 333 },
        !            70:                { 333 },
        !            71:                { 333 },
        !            72:                { 500 },
        !            73:                { 564 },
        !            74:                { 250 },
        !            75:                { 333 },
        !            76:                { 250 },
        !            77:                { 278 },
        !            78:                { 500 },
        !            79:                { 500 },
        !            80:                { 500 },
        !            81:                { 500 },
        !            82:                { 500 },
        !            83:                { 500 },
        !            84:                { 500 },
        !            85:                { 500 },
        !            86:                { 500 },
        !            87:                { 500 },
        !            88:                { 278 },
        !            89:                { 278 },
        !            90:                { 564 },
        !            91:                { 564 },
        !            92:                { 564 },
        !            93:                { 444 },
        !            94:                { 921 },
        !            95:                { 722 },
        !            96:                { 667 },
        !            97:                { 667 },
        !            98:                { 722 },
        !            99:                { 611 },
        !           100:                { 556 },
        !           101:                { 722 },
        !           102:                { 722 },
        !           103:                { 333 },
        !           104:                { 389 },
        !           105:                { 722 },
        !           106:                { 611 },
        !           107:                { 889 },
        !           108:                { 722 },
        !           109:                { 722 },
        !           110:                { 556 },
        !           111:                { 722 },
        !           112:                { 667 },
        !           113:                { 556 },
        !           114:                { 611 },
        !           115:                { 722 },
        !           116:                { 722 },
        !           117:                { 944 },
        !           118:                { 722 },
        !           119:                { 722 },
        !           120:                { 611 },
        !           121:                { 333 },
        !           122:                { 278 },
        !           123:                { 333 },
        !           124:                { 469 },
        !           125:                { 500 },
        !           126:                { 333 },
        !           127:                { 444 },
        !           128:                { 500 },
        !           129:                { 444 },
        !           130:                {  500},
        !           131:                {  444},
        !           132:                {  333},
        !           133:                {  500},
        !           134:                {  500},
        !           135:                {  278},
        !           136:                {  278},
        !           137:                {  500},
        !           138:                {  278},
        !           139:                {  778},
        !           140:                {  500},
        !           141:                {  500},
        !           142:                {  500},
        !           143:                {  500},
        !           144:                {  333},
        !           145:                {  389},
        !           146:                {  278},
        !           147:                {  500},
        !           148:                {  500},
        !           149:                {  722},
        !           150:                {  500},
        !           151:                {  500},
        !           152:                {  444},
        !           153:                {  480},
        !           154:                {  200},
        !           155:                {  480},
        !           156:                {  541},
1.4       schwarze  157:        } },
1.6     ! schwarze  158:        { "Times-Bold", {
        !           159:                { 250  },
        !           160:                { 333  },
        !           161:                { 555  },
        !           162:                { 500  },
        !           163:                { 500  },
        !           164:                { 1000 },
        !           165:                { 833  },
        !           166:                { 333  },
        !           167:                { 333  },
        !           168:                { 333  },
        !           169:                { 500  },
        !           170:                { 570  },
        !           171:                { 250  },
        !           172:                { 333  },
        !           173:                { 250  },
        !           174:                { 278  },
        !           175:                { 500  },
        !           176:                { 500  },
        !           177:                { 500  },
        !           178:                { 500  },
        !           179:                { 500  },
        !           180:                { 500  },
        !           181:                { 500  },
        !           182:                { 500  },
        !           183:                { 500  },
        !           184:                { 500  },
        !           185:                { 333  },
        !           186:                { 333  },
        !           187:                { 570  },
        !           188:                { 570  },
        !           189:                { 570  },
        !           190:                { 500  },
        !           191:                { 930  },
        !           192:                { 722  },
        !           193:                { 667  },
        !           194:                { 722  },
        !           195:                { 722  },
        !           196:                { 667  },
        !           197:                { 611  },
        !           198:                { 778  },
        !           199:                { 778  },
        !           200:                { 389  },
        !           201:                { 500  },
        !           202:                { 778  },
        !           203:                { 667  },
        !           204:                { 944  },
        !           205:                { 722  },
        !           206:                { 778  },
        !           207:                { 611  },
        !           208:                { 778  },
        !           209:                { 722  },
        !           210:                { 556  },
        !           211:                { 667  },
        !           212:                { 722  },
        !           213:                { 722  },
        !           214:                { 1000 },
        !           215:                { 722  },
        !           216:                { 722  },
        !           217:                { 667  },
        !           218:                { 333  },
        !           219:                { 278  },
        !           220:                { 333  },
        !           221:                { 581  },
        !           222:                { 500  },
        !           223:                { 333  },
        !           224:                { 500  },
        !           225:                { 556  },
        !           226:                { 444  },
        !           227:                {  556 },
        !           228:                {  444 },
        !           229:                {  333 },
        !           230:                {  500 },
        !           231:                {  556 },
        !           232:                {  278 },
        !           233:                {  333 },
        !           234:                {  556 },
        !           235:                {  278 },
        !           236:                {  833 },
        !           237:                {  556 },
        !           238:                {  500 },
        !           239:                {  556 },
        !           240:                {  556 },
        !           241:                {  444 },
        !           242:                {  389 },
        !           243:                {  333 },
        !           244:                {  556 },
        !           245:                {  500 },
        !           246:                {  722 },
        !           247:                {  500 },
        !           248:                {  500 },
        !           249:                {  444 },
        !           250:                {  394 },
        !           251:                {  220 },
        !           252:                {  394 },
        !           253:                {  520 },
1.4       schwarze  254:        } },
1.6     ! schwarze  255:        { "Times-Italic", {
        !           256:                { 250  },
        !           257:                { 333  },
        !           258:                { 420  },
        !           259:                { 500  },
        !           260:                { 500  },
        !           261:                { 833  },
        !           262:                { 778  },
        !           263:                { 333  },
        !           264:                { 333  },
        !           265:                { 333  },
        !           266:                { 500  },
        !           267:                { 675  },
        !           268:                { 250  },
        !           269:                { 333  },
        !           270:                { 250  },
        !           271:                { 278  },
        !           272:                { 500  },
        !           273:                { 500  },
        !           274:                { 500  },
        !           275:                { 500  },
        !           276:                { 500  },
        !           277:                { 500  },
        !           278:                { 500  },
        !           279:                { 500  },
        !           280:                { 500  },
        !           281:                { 500  },
        !           282:                { 333  },
        !           283:                { 333  },
        !           284:                { 675  },
        !           285:                { 675  },
        !           286:                { 675  },
        !           287:                { 500  },
        !           288:                { 920  },
        !           289:                { 611  },
        !           290:                { 611  },
        !           291:                { 667  },
        !           292:                { 722  },
        !           293:                { 611  },
        !           294:                { 611  },
        !           295:                { 722  },
        !           296:                { 722  },
        !           297:                { 333  },
        !           298:                { 444  },
        !           299:                { 667  },
        !           300:                { 556  },
        !           301:                { 833  },
        !           302:                { 667  },
        !           303:                { 722  },
        !           304:                { 611  },
        !           305:                { 722  },
        !           306:                { 611  },
        !           307:                { 500  },
        !           308:                { 556  },
        !           309:                { 722  },
        !           310:                { 611  },
        !           311:                { 833  },
        !           312:                { 611  },
        !           313:                { 556  },
        !           314:                { 556  },
        !           315:                { 389  },
        !           316:                { 278  },
        !           317:                { 389  },
        !           318:                { 422  },
        !           319:                { 500  },
        !           320:                { 333  },
        !           321:                { 500  },
        !           322:                { 500  },
        !           323:                { 444  },
        !           324:                {  500 },
        !           325:                {  444 },
        !           326:                {  278 },
        !           327:                {  500 },
        !           328:                {  500 },
        !           329:                {  278 },
        !           330:                {  278 },
        !           331:                {  444 },
        !           332:                {  278 },
        !           333:                {  722 },
        !           334:                {  500 },
        !           335:                {  500 },
        !           336:                {  500 },
        !           337:                {  500 },
        !           338:                {  389 },
        !           339:                {  389 },
        !           340:                {  278 },
        !           341:                {  500 },
        !           342:                {  444 },
        !           343:                {  667 },
        !           344:                {  444 },
        !           345:                {  444 },
        !           346:                {  389 },
        !           347:                {  400 },
        !           348:                {  275 },
        !           349:                {  400 },
        !           350:                {  541 },
1.4       schwarze  351:        } },
                    352: };
                    353:
                    354: /* These work the buffer used by the header and footer. */
1.1       schwarze  355: #define        PS_BUFSLOP        128
                    356: #define        PS_GROWBUF(p, sz) \
                    357:        do if ((p)->engine.ps.psmargcur + (sz) > \
                    358:                        (p)->engine.ps.psmargsz) { \
                    359:                (p)->engine.ps.psmargsz += /* CONSTCOND */ \
                    360:                        MAX(PS_BUFSLOP, (sz)); \
                    361:                (p)->engine.ps.psmarg = realloc \
                    362:                        ((p)->engine.ps.psmarg,  \
                    363:                         (p)->engine.ps.psmargsz); \
                    364:                if (NULL == (p)->engine.ps.psmarg) { \
                    365:                        perror(NULL); \
                    366:                        exit(EXIT_FAILURE); \
                    367:                } \
                    368:        } while (/* CONSTCOND */ 0)
                    369:
1.2       schwarze  370:
1.6     ! schwarze  371: static double            ps_hspan(const struct termp *,
        !           372:                                const struct roffsu *);
        !           373: static size_t            ps_width(const struct termp *, char);
        !           374: static void              ps_advance(struct termp *, size_t);
1.1       schwarze  375: static void              ps_begin(struct termp *);
                    376: static void              ps_end(struct termp *);
                    377: static void              ps_endline(struct termp *);
1.2       schwarze  378: static void              ps_fclose(struct termp *);
1.6     ! schwarze  379: static void              ps_letter(struct termp *, char);
1.2       schwarze  380: static void              ps_pclose(struct termp *);
1.4       schwarze  381: static void              ps_pletter(struct termp *, int);
1.1       schwarze  382: static void              ps_printf(struct termp *, const char *, ...);
                    383: static void              ps_putchar(struct termp *, char);
1.2       schwarze  384: static void              ps_setfont(struct termp *, enum termfont);
1.1       schwarze  385:
                    386:
                    387: void *
1.5       schwarze  388: ps_alloc(char *outopts)
1.1       schwarze  389: {
                    390:        struct termp    *p;
1.6     ! schwarze  391:        size_t           pagex, pagey, marginx, marginy, lineheight;
1.5       schwarze  392:        const char      *toks[2];
1.6     ! schwarze  393:        const char      *pp;
1.5       schwarze  394:        char            *v;
1.1       schwarze  395:
                    396:        if (NULL == (p = term_alloc(TERMENC_ASCII)))
                    397:                return(NULL);
                    398:
1.6     ! schwarze  399:        p->advance = ps_advance;
1.1       schwarze  400:        p->begin = ps_begin;
                    401:        p->end = ps_end;
                    402:        p->endline = ps_endline;
1.6     ! schwarze  403:        p->hspan = ps_hspan;
        !           404:        p->letter = ps_letter;
        !           405:        p->type = TERMTYPE_PS;
1.3       schwarze  406:        p->width = ps_width;
1.6     ! schwarze  407:
1.5       schwarze  408:        toks[0] = "paper";
                    409:        toks[1] = NULL;
                    410:
1.6     ! schwarze  411:        pp = NULL;
        !           412:
1.5       schwarze  413:        while (outopts && *outopts)
                    414:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    415:                case (0):
1.6     ! schwarze  416:                        pp = v;
1.5       schwarze  417:                        break;
                    418:                default:
                    419:                        break;
                    420:                }
1.4       schwarze  421:
1.6     ! schwarze  422:        /* Default to US letter (millimetres). */
        !           423:
        !           424:        pagex = 216;
        !           425:        pagey = 279;
        !           426:
        !           427:        /*
        !           428:         * The ISO-269 paper sizes can be calculated automatically, but
        !           429:         * it would require bringing in -lm for pow() and I'd rather not
        !           430:         * do that.  So just do it the easy way for now.  Since this
        !           431:         * only happens once, I'm not terribly concerned.
        !           432:         */
        !           433:
        !           434:        if (pp && strcasecmp(pp, "letter")) {
        !           435:                if (0 == strcasecmp(pp, "a3")) {
        !           436:                        pagex = 297;
        !           437:                        pagey = 420;
        !           438:                } else if (0 == strcasecmp(pp, "a4")) {
        !           439:                        pagex = 210;
        !           440:                        pagey = 297;
        !           441:                } else if (0 == strcasecmp(pp, "a5")) {
        !           442:                        pagex = 148;
        !           443:                        pagey = 210;
        !           444:                } else if (0 == strcasecmp(pp, "legal")) {
        !           445:                        pagex = 216;
        !           446:                        pagey = 356;
        !           447:                } else if (2 != sscanf(pp, "%zux%zu", &pagex, &pagey))
        !           448:                        fprintf(stderr, "%s: Unknown paper\n", pp);
        !           449:        } else if (NULL == pp)
        !           450:                pp = "letter";
        !           451:
        !           452:        /*
        !           453:         * This MUST be defined before any PNT2AFM or AFM2PNT
        !           454:         * calculations occur.
        !           455:         */
        !           456:
        !           457:        p->engine.ps.scale = 11;
        !           458:
        !           459:        /* Remember millimetres -> AFM units. */
        !           460:
        !           461:        pagex = PNT2AFM(p, ((double)pagex * 2.834));
        !           462:        pagey = PNT2AFM(p, ((double)pagey * 2.834));
        !           463:
        !           464:        /* Margins are 1/9 the page x and y. */
        !           465:
        !           466:        marginx = /* LINTED */
        !           467:                (size_t)((double)pagex / 9.0);
        !           468:        marginy = /* LINTED */
        !           469:                (size_t)((double)pagey / 9.0);
        !           470:
        !           471:        /* Line-height is 1.4em. */
        !           472:
        !           473:        lineheight = PNT2AFM(p, ((double)p->engine.ps.scale * 1.4));
1.4       schwarze  474:
                    475:        p->engine.ps.width = pagex;
                    476:        p->engine.ps.height = pagey;
1.6     ! schwarze  477:        p->engine.ps.header = pagey - (marginy / 2) - (lineheight / 2);
        !           478:        p->engine.ps.top = pagey - marginy;
        !           479:        p->engine.ps.footer = (marginy / 2) - (lineheight / 2);
        !           480:        p->engine.ps.bottom = marginy;
        !           481:        p->engine.ps.left = marginx;
        !           482:        p->engine.ps.lineheight = lineheight;
1.4       schwarze  483:
1.6     ! schwarze  484:        p->defrmargin = pagex - (marginx * 2);
1.1       schwarze  485:        return(p);
                    486: }
                    487:
                    488:
                    489: void
                    490: ps_free(void *arg)
                    491: {
                    492:        struct termp    *p;
                    493:
                    494:        p = (struct termp *)arg;
                    495:
                    496:        if (p->engine.ps.psmarg)
                    497:                free(p->engine.ps.psmarg);
                    498:
                    499:        term_free(p);
                    500: }
                    501:
                    502:
                    503: static void
                    504: ps_printf(struct termp *p, const char *fmt, ...)
                    505: {
                    506:        va_list          ap;
                    507:        int              pos;
                    508:
                    509:        va_start(ap, fmt);
                    510:
                    511:        /*
                    512:         * If we're running in regular mode, then pipe directly into
                    513:         * vprintf().  If we're processing margins, then push the data
                    514:         * into our growable margin buffer.
                    515:         */
                    516:
1.6     ! schwarze  517:        if ( ! (PS_MARGINS & p->engine.ps.flags)) {
1.1       schwarze  518:                vprintf(fmt, ap);
                    519:                va_end(ap);
                    520:                return;
                    521:        }
                    522:
                    523:        /*
                    524:         * XXX: I assume that the in-margin print won't exceed
                    525:         * PS_BUFSLOP (128 bytes), which is reasonable but still an
                    526:         * assumption that will cause pukeage if it's not the case.
                    527:         */
                    528:
                    529:        PS_GROWBUF(p, PS_BUFSLOP);
                    530:
                    531:        pos = (int)p->engine.ps.psmargcur;
                    532:        vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
                    533:        p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
                    534:
                    535:        va_end(ap);
                    536: }
                    537:
                    538:
                    539: static void
                    540: ps_putchar(struct termp *p, char c)
                    541: {
                    542:        int              pos;
                    543:
                    544:        /* See ps_printf(). */
                    545:
1.6     ! schwarze  546:        if ( ! (PS_MARGINS & p->engine.ps.flags)) {
1.1       schwarze  547:                putchar(c);
                    548:                return;
                    549:        }
                    550:
                    551:        PS_GROWBUF(p, 2);
                    552:
                    553:        pos = (int)p->engine.ps.psmargcur++;
                    554:        p->engine.ps.psmarg[pos++] = c;
                    555:        p->engine.ps.psmarg[pos] = '\0';
                    556: }
                    557:
                    558:
                    559: /* ARGSUSED */
                    560: static void
                    561: ps_end(struct termp *p)
                    562: {
                    563:
                    564:        /*
                    565:         * At the end of the file, do one last showpage.  This is the
                    566:         * same behaviour as groff(1) and works for multiple pages as
                    567:         * well as just one.
                    568:         */
                    569:
1.6     ! schwarze  570:        if ( ! (PS_NEWPAGE & p->engine.ps.flags)) {
        !           571:                assert(0 == p->engine.ps.flags);
        !           572:                assert('\0' == p->engine.ps.last);
        !           573:                assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
        !           574:                printf("%s", p->engine.ps.psmarg);
        !           575:                p->engine.ps.pages++;
        !           576:                printf("showpage\n");
        !           577:        }
1.4       schwarze  578:
                    579:        printf("%%%%Trailer\n");
                    580:        printf("%%%%Pages: %zu\n", p->engine.ps.pages);
                    581:        printf("%%%%EOF\n");
1.1       schwarze  582: }
                    583:
                    584:
                    585: static void
                    586: ps_begin(struct termp *p)
                    587: {
1.4       schwarze  588:        time_t           t;
                    589:        int              i;
1.1       schwarze  590:
1.2       schwarze  591:        /*
                    592:         * Print margins into margin buffer.  Nothing gets output to the
                    593:         * screen yet, so we don't need to initialise the primary state.
1.1       schwarze  594:         */
                    595:
                    596:        if (p->engine.ps.psmarg) {
                    597:                assert(p->engine.ps.psmargsz);
                    598:                p->engine.ps.psmarg[0] = '\0';
                    599:        }
                    600:
                    601:        p->engine.ps.psmargcur = 0;
1.6     ! schwarze  602:        p->engine.ps.flags = PS_MARGINS;
1.4       schwarze  603:        p->engine.ps.pscol = p->engine.ps.left;
                    604:        p->engine.ps.psrow = p->engine.ps.header;
1.1       schwarze  605:
1.2       schwarze  606:        ps_setfont(p, TERMFONT_NONE);
1.1       schwarze  607:
                    608:        (*p->headf)(p, p->argf);
                    609:        (*p->endline)(p);
                    610:
1.4       schwarze  611:        p->engine.ps.pscol = p->engine.ps.left;
                    612:        p->engine.ps.psrow = p->engine.ps.footer;
1.1       schwarze  613:
                    614:        (*p->footf)(p, p->argf);
                    615:        (*p->endline)(p);
                    616:
1.6     ! schwarze  617:        p->engine.ps.flags &= ~PS_MARGINS;
1.2       schwarze  618:
1.6     ! schwarze  619:        assert(0 == p->engine.ps.flags);
1.2       schwarze  620:        assert(p->engine.ps.psmarg);
                    621:        assert('\0' != p->engine.ps.psmarg[0]);
1.1       schwarze  622:
1.2       schwarze  623:        /*
                    624:         * Print header and initialise page state.  Following this,
                    625:         * stuff gets printed to the screen, so make sure we're sane.
                    626:         */
                    627:
1.4       schwarze  628:        t = time(NULL);
                    629:
                    630:        printf("%%!PS-Adobe-3.0\n");
                    631:        printf("%%%%Creator: mandoc-%s\n", VERSION);
                    632:        printf("%%%%CreationDate: %s", ctime(&t));
                    633:        printf("%%%%DocumentData: Clean7Bit\n");
                    634:        printf("%%%%Orientation: Portrait\n");
                    635:        printf("%%%%Pages: (atend)\n");
                    636:        printf("%%%%PageOrder: Ascend\n");
                    637:        printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
1.6     ! schwarze  638:                        AFM2PNT(p, p->engine.ps.width),
        !           639:                        AFM2PNT(p, p->engine.ps.height));
1.4       schwarze  640:        printf("%%%%DocumentNeededResources: font");
                    641:        for (i = 0; i < (int)TERMFONT__MAX; i++)
                    642:                printf(" %s", fonts[i].name);
                    643:        printf("\n%%%%EndComments\n");
                    644:
                    645:        p->engine.ps.pscol = p->engine.ps.left;
                    646:        p->engine.ps.psrow = p->engine.ps.top;
1.6     ! schwarze  647:        p->engine.ps.flags |= PS_NEWPAGE;
        !           648:        ps_setfont(p, TERMFONT_NONE);
1.1       schwarze  649: }
                    650:
                    651:
                    652: static void
1.4       schwarze  653: ps_pletter(struct termp *p, int c)
1.1       schwarze  654: {
1.4       schwarze  655:        int              f;
1.6     ! schwarze  656:
        !           657:        /*
        !           658:         * If we haven't opened a page context, then output that we're
        !           659:         * in a new page and make sure the font is correctly set.
        !           660:         */
        !           661:
        !           662:        if (PS_NEWPAGE & p->engine.ps.flags) {
        !           663:                printf("%%%%Page: %zu %zu\n",
        !           664:                                p->engine.ps.pages + 1,
        !           665:                                p->engine.ps.pages + 1);
        !           666:                ps_printf(p, "/%s %zu selectfont\n",
        !           667:                                fonts[(int)p->engine.ps.lastf].name,
        !           668:                                p->engine.ps.scale);
        !           669:                p->engine.ps.flags &= ~PS_NEWPAGE;
        !           670:        }
1.1       schwarze  671:
1.2       schwarze  672:        /*
                    673:         * If we're not in a PostScript "word" context, then open one
                    674:         * now at the current cursor.
                    675:         */
                    676:
1.6     ! schwarze  677:        if ( ! (PS_INLINE & p->engine.ps.flags)) {
1.1       schwarze  678:                ps_printf(p, "%zu %zu moveto\n(",
1.6     ! schwarze  679:                                AFM2PNT(p, p->engine.ps.pscol),
        !           680:                                AFM2PNT(p, p->engine.ps.psrow));
        !           681:                p->engine.ps.flags |= PS_INLINE;
1.1       schwarze  682:        }
                    683:
1.6     ! schwarze  684:        assert( ! (PS_NEWPAGE & p->engine.ps.flags));
        !           685:
1.1       schwarze  686:        /*
                    687:         * We need to escape these characters as per the PostScript
                    688:         * specification.  We would also escape non-graphable characters
                    689:         * (like tabs), but none of them would get to this point and
                    690:         * it's superfluous to abort() on them.
                    691:         */
                    692:
                    693:        switch (c) {
                    694:        case ('('):
                    695:                /* FALLTHROUGH */
                    696:        case (')'):
                    697:                /* FALLTHROUGH */
                    698:        case ('\\'):
                    699:                ps_putchar(p, '\\');
                    700:                break;
                    701:        default:
                    702:                break;
                    703:        }
                    704:
                    705:        /* Write the character and adjust where we are on the page. */
1.2       schwarze  706:
1.4       schwarze  707:        f = (int)p->engine.ps.lastf;
                    708:
                    709:        if (c <= 32 || (c - 32 > MAXCHAR)) {
                    710:                ps_putchar(p, ' ');
1.6     ! schwarze  711:                p->engine.ps.pscol += fonts[f].gly[0].wx;
1.4       schwarze  712:                return;
                    713:        }
                    714:
1.6     ! schwarze  715:        ps_putchar(p, (char)c);
1.4       schwarze  716:        c -= 32;
1.6     ! schwarze  717:        p->engine.ps.pscol += fonts[f].gly[c].wx;
1.1       schwarze  718: }
                    719:
                    720:
                    721: static void
1.2       schwarze  722: ps_pclose(struct termp *p)
                    723: {
                    724:
                    725:        /*
                    726:         * Spit out that we're exiting a word context (this is a
                    727:         * "partial close" because we don't check the last-char buffer
                    728:         * or anything).
                    729:         */
                    730:
1.6     ! schwarze  731:        if ( ! (PS_INLINE & p->engine.ps.flags))
1.2       schwarze  732:                return;
                    733:
                    734:        ps_printf(p, ") show\n");
1.6     ! schwarze  735:        p->engine.ps.flags &= ~PS_INLINE;
1.2       schwarze  736: }
                    737:
                    738:
                    739: static void
                    740: ps_fclose(struct termp *p)
                    741: {
                    742:
                    743:        /*
                    744:         * Strong closure: if we have a last-char, spit it out after
                    745:         * checking that we're in the right font mode.  This will of
                    746:         * course open a new scope, if applicable.
                    747:         *
                    748:         * Following this, close out any scope that's open.
                    749:         */
                    750:
                    751:        if ('\0' != p->engine.ps.last) {
                    752:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    753:                        ps_pclose(p);
                    754:                        ps_setfont(p, TERMFONT_NONE);
                    755:                }
                    756:                ps_pletter(p, p->engine.ps.last);
                    757:                p->engine.ps.last = '\0';
                    758:        }
                    759:
1.6     ! schwarze  760:        if ( ! (PS_INLINE & p->engine.ps.flags))
1.2       schwarze  761:                return;
                    762:
                    763:        ps_pclose(p);
                    764: }
                    765:
                    766:
                    767: static void
                    768: ps_letter(struct termp *p, char c)
                    769: {
                    770:        char            cc;
                    771:
                    772:        /*
                    773:         * State machine dictates whether to buffer the last character
                    774:         * or not.  Basically, encoded words are detected by checking if
                    775:         * we're an "8" and switching on the buffer.  Then we put "8" in
                    776:         * our buffer, and on the next charater, flush both character
                    777:         * and buffer.  Thus, "regular" words are detected by having a
                    778:         * regular character and a regular buffer character.
                    779:         */
                    780:
                    781:        if ('\0' == p->engine.ps.last) {
                    782:                assert(8 != c);
                    783:                p->engine.ps.last = c;
                    784:                return;
                    785:        } else if (8 == p->engine.ps.last) {
                    786:                assert(8 != c);
                    787:                p->engine.ps.last = '\0';
                    788:        } else if (8 == c) {
                    789:                assert(8 != p->engine.ps.last);
                    790:                if ('_' == p->engine.ps.last) {
                    791:                        if (p->engine.ps.lastf != TERMFONT_UNDER) {
                    792:                                ps_pclose(p);
                    793:                                ps_setfont(p, TERMFONT_UNDER);
                    794:                        }
                    795:                } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
                    796:                        ps_pclose(p);
                    797:                        ps_setfont(p, TERMFONT_BOLD);
                    798:                }
                    799:                p->engine.ps.last = c;
                    800:                return;
                    801:        } else {
                    802:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    803:                        ps_pclose(p);
                    804:                        ps_setfont(p, TERMFONT_NONE);
                    805:                }
                    806:                cc = p->engine.ps.last;
                    807:                p->engine.ps.last = c;
                    808:                c = cc;
                    809:        }
                    810:
                    811:        ps_pletter(p, c);
                    812: }
                    813:
                    814:
                    815: static void
1.1       schwarze  816: ps_advance(struct termp *p, size_t len)
                    817: {
                    818:
1.2       schwarze  819:        /*
                    820:         * Advance some spaces.  This can probably be made smarter,
                    821:         * i.e., to have multiple space-separated words in the same
                    822:         * scope, but this is easier:  just close out the current scope
                    823:         * and readjust our column settings.
                    824:         */
1.1       schwarze  825:
1.2       schwarze  826:        ps_fclose(p);
1.4       schwarze  827:        p->engine.ps.pscol += len;
1.1       schwarze  828: }
                    829:
                    830:
                    831: static void
                    832: ps_endline(struct termp *p)
                    833: {
                    834:
1.2       schwarze  835:        /* Close out any scopes we have open: we're at eoln. */
                    836:
                    837:        ps_fclose(p);
                    838:
                    839:        /*
                    840:         * If we're in the margin, don't try to recalculate our current
                    841:         * row.  XXX: if the column tries to be fancy with multiple
                    842:         * lines, we'll do nasty stuff.
                    843:         */
1.1       schwarze  844:
1.6     ! schwarze  845:        if (PS_MARGINS & p->engine.ps.flags)
        !           846:                return;
        !           847:
        !           848:        /* Left-justify. */
        !           849:
        !           850:        p->engine.ps.pscol = p->engine.ps.left;
        !           851:
        !           852:        /* If we haven't printed anything, return. */
        !           853:
        !           854:        if (PS_NEWPAGE & p->engine.ps.flags)
1.1       schwarze  855:                return;
                    856:
1.2       schwarze  857:        /*
                    858:         * Put us down a line.  If we're at the page bottom, spit out a
                    859:         * showpage and restart our row.
                    860:         */
                    861:
1.4       schwarze  862:        if (p->engine.ps.psrow >= p->engine.ps.lineheight +
                    863:                        p->engine.ps.bottom) {
                    864:                p->engine.ps.psrow -= p->engine.ps.lineheight;
1.1       schwarze  865:                return;
                    866:        }
                    867:
                    868:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    869:        printf("%s", p->engine.ps.psmarg);
                    870:        printf("showpage\n");
1.4       schwarze  871:        p->engine.ps.pages++;
                    872:        p->engine.ps.psrow = p->engine.ps.top;
1.6     ! schwarze  873:        assert( ! (PS_NEWPAGE & p->engine.ps.flags));
        !           874:        p->engine.ps.flags |= PS_NEWPAGE;
1.1       schwarze  875: }
1.2       schwarze  876:
                    877:
                    878: static void
                    879: ps_setfont(struct termp *p, enum termfont f)
                    880: {
                    881:
1.4       schwarze  882:        assert(f < TERMFONT__MAX);
1.2       schwarze  883:        p->engine.ps.lastf = f;
1.6     ! schwarze  884:
        !           885:        /*
        !           886:         * If we're still at the top of the page, let the font-setting
        !           887:         * be delayed until we actually have stuff to print.
        !           888:         */
        !           889:
        !           890:        if (PS_NEWPAGE & p->engine.ps.flags)
        !           891:                return;
        !           892:
        !           893:        ps_printf(p, "/%s %zu selectfont\n",
        !           894:                        fonts[(int)f].name, p->engine.ps.scale);
1.2       schwarze  895: }
                    896:
1.3       schwarze  897:
1.4       schwarze  898: /* ARGSUSED */
1.3       schwarze  899: static size_t
                    900: ps_width(const struct termp *p, char c)
                    901: {
                    902:
1.4       schwarze  903:        if (c <= 32 || c - 32 >= MAXCHAR)
1.6     ! schwarze  904:                return(fonts[(int)TERMFONT_NONE].gly[0].wx);
1.4       schwarze  905:
                    906:        c -= 32;
1.6     ! schwarze  907:        return(fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
        !           908: }
        !           909:
        !           910:
        !           911: static double
        !           912: ps_hspan(const struct termp *p, const struct roffsu *su)
        !           913: {
        !           914:        double           r;
        !           915:
        !           916:        /*
        !           917:         * All of these measurements are derived by converting from the
        !           918:         * native measurement to AFM units.
        !           919:         */
        !           920:
        !           921:        switch (su->unit) {
        !           922:        case (SCALE_CM):
        !           923:                r = PNT2AFM(p, su->scale * 28.34);
        !           924:                break;
        !           925:        case (SCALE_IN):
        !           926:                r = PNT2AFM(p, su->scale * 72);
        !           927:                break;
        !           928:        case (SCALE_PC):
        !           929:                r = PNT2AFM(p, su->scale * 12);
        !           930:                break;
        !           931:        case (SCALE_PT):
        !           932:                r = PNT2AFM(p, su->scale * 100);
        !           933:                break;
        !           934:        case (SCALE_EM):
        !           935:                r = su->scale *
        !           936:                        fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
        !           937:                break;
        !           938:        case (SCALE_MM):
        !           939:                r = PNT2AFM(p, su->scale * 2.834);
        !           940:                break;
        !           941:        case (SCALE_EN):
        !           942:                r = su->scale *
        !           943:                        fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
        !           944:                break;
        !           945:        case (SCALE_VS):
        !           946:                r = su->scale * p->engine.ps.lineheight;
        !           947:                break;
        !           948:        default:
        !           949:                r = su->scale;
        !           950:                break;
        !           951:        }
        !           952:
        !           953:        return(r);
1.3       schwarze  954: }
1.6     ! schwarze  955: