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

Annotation of mandoc/term_ps.c, Revision 1.24

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

CVSweb