[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.4

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