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

Annotation of mandoc/term_ps.c, Revision 1.25

1.25    ! kristaps    1: /*     $Id: term_ps.c,v 1.24 2010/06/30 20:57:41 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. */
1.25    ! kristaps   36: #define        PNT2AFM(p, x) /* LINTED */ \
1.21      kristaps   37:        (size_t)((double)(x) * (1000.0 / (double)(p)->engine.ps.scale))
                     38:
                     39: /* Convert an AFM unit "x" to a PostScript points */
1.25    ! kristaps   40: #define        AFM2PNT(p, x) /* LINTED */ \
1.21      kristaps   41:        (size_t)((double)(x) / (1000.0 / (double)(p)->engine.ps.scale))
                     42:
1.13      kristaps   43: struct glyph {
1.25    ! kristaps   44:        size_t            wx; /* WX in AFM */
1.13      kristaps   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.25    ! kristaps  392:        const char      *pp;
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:
1.25    ! kristaps  407:        p->engine.ps.scale = 11;
1.17      kristaps  408:
                    409:        toks[0] = "paper";
                    410:        toks[1] = NULL;
                    411:
1.25    ! kristaps  412:        pp = NULL;
1.19      kristaps  413:
1.17      kristaps  414:        while (outopts && *outopts)
                    415:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    416:                case (0):
1.25    ! kristaps  417:                        pp = 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.25    ! kristaps  426:        /* Default to US letter (millimetres). */
        !           427:
        !           428:        pagex = 216;
        !           429:        pagey = 279;
        !           430:
        !           431:        /*
        !           432:         * The ISO-269 paper sizes can be calculated automatically, but
        !           433:         * it would require bringing in -lm for pow() and I'd rather not
        !           434:         * do that.  So just do it the easy way for now.  Since this
        !           435:         * only happens once, I'm not terribly concerned.
        !           436:         */
        !           437:
        !           438:        if (pp && strcasecmp(pp, "letter")) {
        !           439:                if (0 == strcasecmp(pp, "a3")) {
        !           440:                        pagex = 297;
        !           441:                        pagey = 420;
        !           442:                } else if (0 == strcasecmp(pp, "a4")) {
        !           443:                        pagex = 210;
        !           444:                        pagey = 297;
        !           445:                } else if (0 == strcasecmp(pp, "a5")) {
        !           446:                        pagex = 148;
        !           447:                        pagey = 210;
        !           448:                } else if (0 == strcasecmp(pp, "legal")) {
        !           449:                        pagex = 216;
        !           450:                        pagey = 356;
        !           451:                } else if (2 != sscanf(pp, "%zux%zu", &pagex, &pagey))
        !           452:                        fprintf(stderr, "%s: Unknown paper\n", pp);
1.18      kristaps  453:        }
                    454:
1.25    ! kristaps  455:        /* Remember millimetres -> AFM units. */
        !           456:
        !           457:        pagex = PNT2AFM(p, ((double)pagex * 2.834));
        !           458:        pagey = PNT2AFM(p, ((double)pagey * 2.834));
        !           459:
1.16      kristaps  460:        assert(margin * 2 < pagex);
                    461:        assert(margin * 2 < pagey);
                    462:
                    463:        p->engine.ps.width = pagex;
                    464:        p->engine.ps.height = pagey;
                    465:        p->engine.ps.header = pagey - (margin / 2);
                    466:        p->engine.ps.top = pagey - margin;
                    467:        p->engine.ps.footer = (margin / 2);
                    468:        p->engine.ps.bottom = margin;
                    469:        p->engine.ps.left = margin;
1.18      kristaps  470:        p->engine.ps.lineheight = lineheight;
1.16      kristaps  471:
                    472:        p->defrmargin = pagex - (margin * 2);
1.1       kristaps  473:        return(p);
                    474: }
                    475:
                    476:
                    477: void
                    478: ps_free(void *arg)
                    479: {
1.4       kristaps  480:        struct termp    *p;
                    481:
                    482:        p = (struct termp *)arg;
                    483:
                    484:        if (p->engine.ps.psmarg)
                    485:                free(p->engine.ps.psmarg);
                    486:
                    487:        term_free(p);
                    488: }
                    489:
                    490:
                    491: static void
                    492: ps_printf(struct termp *p, const char *fmt, ...)
                    493: {
                    494:        va_list          ap;
                    495:        int              pos;
                    496:
                    497:        va_start(ap, fmt);
                    498:
                    499:        /*
                    500:         * If we're running in regular mode, then pipe directly into
                    501:         * vprintf().  If we're processing margins, then push the data
                    502:         * into our growable margin buffer.
                    503:         */
1.1       kristaps  504:
1.4       kristaps  505:        if ( ! (PS_MARGINS & p->engine.ps.psstate)) {
                    506:                vprintf(fmt, ap);
                    507:                va_end(ap);
                    508:                return;
                    509:        }
                    510:
                    511:        /*
                    512:         * XXX: I assume that the in-margin print won't exceed
                    513:         * PS_BUFSLOP (128 bytes), which is reasonable but still an
                    514:         * assumption that will cause pukeage if it's not the case.
                    515:         */
                    516:
                    517:        PS_GROWBUF(p, PS_BUFSLOP);
                    518:
                    519:        pos = (int)p->engine.ps.psmargcur;
                    520:        vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
                    521:        p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
1.5       kristaps  522:
                    523:        va_end(ap);
1.4       kristaps  524: }
                    525:
                    526:
                    527: static void
                    528: ps_putchar(struct termp *p, char c)
                    529: {
                    530:        int              pos;
                    531:
                    532:        /* See ps_printf(). */
                    533:
                    534:        if ( ! (PS_MARGINS & p->engine.ps.psstate)) {
                    535:                putchar(c);
                    536:                return;
                    537:        }
                    538:
                    539:        PS_GROWBUF(p, 2);
                    540:
                    541:        pos = (int)p->engine.ps.psmargcur++;
1.5       kristaps  542:        p->engine.ps.psmarg[pos++] = c;
1.4       kristaps  543:        p->engine.ps.psmarg[pos] = '\0';
1.2       kristaps  544: }
                    545:
                    546:
1.3       kristaps  547: /* ARGSUSED */
1.2       kristaps  548: static void
                    549: ps_end(struct termp *p)
                    550: {
                    551:
1.4       kristaps  552:        /*
                    553:         * At the end of the file, do one last showpage.  This is the
                    554:         * same behaviour as groff(1) and works for multiple pages as
                    555:         * well as just one.
                    556:         */
                    557:
1.8       kristaps  558:        assert(0 == p->engine.ps.psstate);
                    559:        assert('\0' == p->engine.ps.last);
1.4       kristaps  560:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    561:        printf("%s", p->engine.ps.psmarg);
1.16      kristaps  562:        p->engine.ps.pages++;
1.4       kristaps  563:        printf("showpage\n");
1.16      kristaps  564:
                    565:        printf("%%%%Trailer\n");
                    566:        printf("%%%%Pages: %zu\n", p->engine.ps.pages);
                    567:        printf("%%%%EOF\n");
1.2       kristaps  568: }
                    569:
                    570:
                    571: static void
                    572: ps_begin(struct termp *p)
                    573: {
1.13      kristaps  574:        time_t           t;
1.16      kristaps  575:        int              i;
1.2       kristaps  576:
1.9       kristaps  577:        /*
                    578:         * Print margins into margin buffer.  Nothing gets output to the
                    579:         * screen yet, so we don't need to initialise the primary state.
1.2       kristaps  580:         */
                    581:
1.4       kristaps  582:        if (p->engine.ps.psmarg) {
                    583:                assert(p->engine.ps.psmargsz);
                    584:                p->engine.ps.psmarg[0] = '\0';
                    585:        }
                    586:
                    587:        p->engine.ps.psmargcur = 0;
1.9       kristaps  588:        p->engine.ps.psstate = PS_MARGINS;
1.16      kristaps  589:        p->engine.ps.pscol = p->engine.ps.left;
                    590:        p->engine.ps.psrow = p->engine.ps.header;
1.4       kristaps  591:
1.9       kristaps  592:        ps_setfont(p, TERMFONT_NONE);
1.4       kristaps  593:
                    594:        (*p->headf)(p, p->argf);
                    595:        (*p->endline)(p);
                    596:
1.16      kristaps  597:        p->engine.ps.pscol = p->engine.ps.left;
                    598:        p->engine.ps.psrow = p->engine.ps.footer;
1.4       kristaps  599:
                    600:        (*p->footf)(p, p->argf);
                    601:        (*p->endline)(p);
                    602:
                    603:        p->engine.ps.psstate &= ~PS_MARGINS;
1.9       kristaps  604:
1.4       kristaps  605:        assert(0 == p->engine.ps.psstate);
1.9       kristaps  606:        assert(p->engine.ps.psmarg);
                    607:        assert('\0' != p->engine.ps.psmarg[0]);
                    608:
                    609:        /*
                    610:         * Print header and initialise page state.  Following this,
                    611:         * stuff gets printed to the screen, so make sure we're sane.
                    612:         */
1.4       kristaps  613:
1.13      kristaps  614:        t = time(NULL);
                    615:
1.16      kristaps  616:        printf("%%!PS-Adobe-3.0\n");
1.13      kristaps  617:        printf("%%%%Creator: mandoc-%s\n", VERSION);
                    618:        printf("%%%%CreationDate: %s", ctime(&t));
1.16      kristaps  619:        printf("%%%%DocumentData: Clean7Bit\n");
                    620:        printf("%%%%Orientation: Portrait\n");
                    621:        printf("%%%%Pages: (atend)\n");
1.13      kristaps  622:        printf("%%%%PageOrder: Ascend\n");
1.16      kristaps  623:        printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
1.23      kristaps  624:                        AFM2PNT(p, p->engine.ps.width),
                    625:                        AFM2PNT(p, p->engine.ps.height));
1.16      kristaps  626:        printf("%%%%DocumentNeededResources: font");
                    627:        for (i = 0; i < (int)TERMFONT__MAX; i++)
                    628:                printf(" %s", fonts[i].name);
                    629:        printf("\n%%%%EndComments\n");
                    630:
                    631:        printf("%%%%Page: %zu %zu\n",
                    632:                        p->engine.ps.pages + 1,
                    633:                        p->engine.ps.pages + 1);
1.13      kristaps  634:
1.9       kristaps  635:        ps_setfont(p, TERMFONT_NONE);
1.16      kristaps  636:        p->engine.ps.pscol = p->engine.ps.left;
                    637:        p->engine.ps.psrow = p->engine.ps.top;
1.2       kristaps  638: }
                    639:
                    640:
                    641: static void
1.13      kristaps  642: ps_pletter(struct termp *p, int c)
1.2       kristaps  643: {
1.14      kristaps  644:        int              f;
1.2       kristaps  645:
1.9       kristaps  646:        /*
                    647:         * If we're not in a PostScript "word" context, then open one
                    648:         * now at the current cursor.
                    649:         */
                    650:
1.2       kristaps  651:        if ( ! (PS_INLINE & p->engine.ps.psstate)) {
1.4       kristaps  652:                ps_printf(p, "%zu %zu moveto\n(",
1.21      kristaps  653:                                AFM2PNT(p, p->engine.ps.pscol),
                    654:                                AFM2PNT(p, p->engine.ps.psrow));
1.2       kristaps  655:                p->engine.ps.psstate |= PS_INLINE;
                    656:        }
                    657:
                    658:        /*
                    659:         * We need to escape these characters as per the PostScript
                    660:         * specification.  We would also escape non-graphable characters
                    661:         * (like tabs), but none of them would get to this point and
                    662:         * it's superfluous to abort() on them.
                    663:         */
                    664:
                    665:        switch (c) {
                    666:        case ('('):
                    667:                /* FALLTHROUGH */
                    668:        case (')'):
                    669:                /* FALLTHROUGH */
                    670:        case ('\\'):
1.4       kristaps  671:                ps_putchar(p, '\\');
1.2       kristaps  672:                break;
                    673:        default:
                    674:                break;
                    675:        }
                    676:
                    677:        /* Write the character and adjust where we are on the page. */
1.9       kristaps  678:
1.14      kristaps  679:        f = (int)p->engine.ps.lastf;
1.13      kristaps  680:
1.14      kristaps  681:        if (c <= 32 || (c - 32 > MAXCHAR)) {
1.13      kristaps  682:                ps_putchar(p, ' ');
1.18      kristaps  683:                p->engine.ps.pscol += fonts[f].gly[0].wx;
1.13      kristaps  684:                return;
                    685:        }
                    686:
1.25    ! kristaps  687:        ps_putchar(p, (char)c);
1.14      kristaps  688:        c -= 32;
1.18      kristaps  689:        p->engine.ps.pscol += fonts[f].gly[c].wx;
1.2       kristaps  690: }
                    691:
                    692:
                    693: static void
1.9       kristaps  694: ps_pclose(struct termp *p)
                    695: {
                    696:
                    697:        /*
                    698:         * Spit out that we're exiting a word context (this is a
                    699:         * "partial close" because we don't check the last-char buffer
                    700:         * or anything).
                    701:         */
                    702:
                    703:        if ( ! (PS_INLINE & p->engine.ps.psstate))
                    704:                return;
                    705:
                    706:        ps_printf(p, ") show\n");
                    707:        p->engine.ps.psstate &= ~PS_INLINE;
                    708: }
                    709:
                    710:
                    711: static void
                    712: ps_fclose(struct termp *p)
                    713: {
                    714:
                    715:        /*
                    716:         * Strong closure: if we have a last-char, spit it out after
                    717:         * checking that we're in the right font mode.  This will of
                    718:         * course open a new scope, if applicable.
                    719:         *
                    720:         * Following this, close out any scope that's open.
                    721:         */
                    722:
                    723:        if ('\0' != p->engine.ps.last) {
                    724:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    725:                        ps_pclose(p);
                    726:                        ps_setfont(p, TERMFONT_NONE);
                    727:                }
                    728:                ps_pletter(p, p->engine.ps.last);
                    729:                p->engine.ps.last = '\0';
                    730:        }
                    731:
                    732:        if ( ! (PS_INLINE & p->engine.ps.psstate))
                    733:                return;
                    734:
                    735:        ps_pclose(p);
                    736: }
                    737:
                    738:
                    739: static void
1.8       kristaps  740: ps_letter(struct termp *p, char c)
                    741: {
                    742:        char            cc;
1.9       kristaps  743:
                    744:        /*
                    745:         * State machine dictates whether to buffer the last character
                    746:         * or not.  Basically, encoded words are detected by checking if
                    747:         * we're an "8" and switching on the buffer.  Then we put "8" in
                    748:         * our buffer, and on the next charater, flush both character
                    749:         * and buffer.  Thus, "regular" words are detected by having a
                    750:         * regular character and a regular buffer character.
                    751:         */
                    752:
1.8       kristaps  753:        if ('\0' == p->engine.ps.last) {
                    754:                assert(8 != c);
                    755:                p->engine.ps.last = c;
                    756:                return;
                    757:        } else if (8 == p->engine.ps.last) {
                    758:                assert(8 != c);
1.9       kristaps  759:                p->engine.ps.last = '\0';
1.8       kristaps  760:        } else if (8 == c) {
                    761:                assert(8 != p->engine.ps.last);
1.9       kristaps  762:                if ('_' == p->engine.ps.last) {
                    763:                        if (p->engine.ps.lastf != TERMFONT_UNDER) {
                    764:                                ps_pclose(p);
                    765:                                ps_setfont(p, TERMFONT_UNDER);
                    766:                        }
                    767:                } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
                    768:                        ps_pclose(p);
                    769:                        ps_setfont(p, TERMFONT_BOLD);
                    770:                }
1.8       kristaps  771:                p->engine.ps.last = c;
                    772:                return;
                    773:        } else {
1.9       kristaps  774:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    775:                        ps_pclose(p);
                    776:                        ps_setfont(p, TERMFONT_NONE);
                    777:                }
1.8       kristaps  778:                cc = p->engine.ps.last;
                    779:                p->engine.ps.last = c;
                    780:                c = cc;
                    781:        }
                    782:
1.9       kristaps  783:        ps_pletter(p, c);
1.8       kristaps  784: }
                    785:
                    786:
                    787: static void
1.2       kristaps  788: ps_advance(struct termp *p, size_t len)
                    789: {
                    790:
1.9       kristaps  791:        /*
                    792:         * Advance some spaces.  This can probably be made smarter,
                    793:         * i.e., to have multiple space-separated words in the same
                    794:         * scope, but this is easier:  just close out the current scope
                    795:         * and readjust our column settings.
                    796:         */
1.8       kristaps  797:
1.9       kristaps  798:        ps_fclose(p);
1.14      kristaps  799:        p->engine.ps.pscol += len;
1.2       kristaps  800: }
                    801:
                    802:
                    803: static void
                    804: ps_endline(struct termp *p)
                    805: {
1.8       kristaps  806:
1.9       kristaps  807:        /* Close out any scopes we have open: we're at eoln. */
                    808:
                    809:        ps_fclose(p);
1.2       kristaps  810:
1.9       kristaps  811:        /*
                    812:         * If we're in the margin, don't try to recalculate our current
                    813:         * row.  XXX: if the column tries to be fancy with multiple
                    814:         * lines, we'll do nasty stuff.
                    815:         */
1.2       kristaps  816:
                    817:        if (PS_MARGINS & p->engine.ps.psstate)
                    818:                return;
                    819:
1.9       kristaps  820:        /*
                    821:         * Put us down a line.  If we're at the page bottom, spit out a
                    822:         * showpage and restart our row.
                    823:         */
                    824:
1.16      kristaps  825:        p->engine.ps.pscol = p->engine.ps.left;
                    826:        if (p->engine.ps.psrow >= p->engine.ps.lineheight +
                    827:                        p->engine.ps.bottom) {
                    828:                p->engine.ps.psrow -= p->engine.ps.lineheight;
1.2       kristaps  829:                return;
                    830:        }
                    831:
1.4       kristaps  832:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    833:        printf("%s", p->engine.ps.psmarg);
1.23      kristaps  834:        printf("showpage\n");
                    835:        p->engine.ps.pages++;
1.16      kristaps  836:        printf("%%%%Page: %zu %zu\n",
                    837:                        p->engine.ps.pages + 1,
                    838:                        p->engine.ps.pages + 1);
                    839:        p->engine.ps.psrow = p->engine.ps.top;
1.1       kristaps  840: }
1.9       kristaps  841:
                    842:
                    843: static void
                    844: ps_setfont(struct termp *p, enum termfont f)
                    845: {
                    846:
1.16      kristaps  847:        assert(f < TERMFONT__MAX);
1.21      kristaps  848:        ps_printf(p, "/%s %zu selectfont\n",
                    849:                        fonts[(int)f].name, p->engine.ps.scale);
1.9       kristaps  850:        p->engine.ps.lastf = f;
                    851: }
                    852:
1.11      kristaps  853:
1.12      kristaps  854: /* ARGSUSED */
1.11      kristaps  855: static size_t
                    856: ps_width(const struct termp *p, char c)
                    857: {
                    858:
1.14      kristaps  859:        if (c <= 32 || c - 32 >= MAXCHAR)
1.18      kristaps  860:                return(fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14      kristaps  861:
                    862:        c -= 32;
1.18      kristaps  863:        return(fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1.11      kristaps  864: }
1.19      kristaps  865:
                    866:
1.20      kristaps  867: static double
1.19      kristaps  868: ps_hspan(const struct termp *p, const struct roffsu *su)
                    869: {
                    870:        double           r;
                    871:
                    872:        /*
                    873:         * All of these measurements are derived by converting from the
1.21      kristaps  874:         * native measurement to AFM units.
1.19      kristaps  875:         */
                    876:
                    877:        switch (su->unit) {
                    878:        case (SCALE_CM):
1.21      kristaps  879:                r = PNT2AFM(p, su->scale * 28.34);
1.19      kristaps  880:                break;
                    881:        case (SCALE_IN):
1.21      kristaps  882:                r = PNT2AFM(p, su->scale * 72);
1.19      kristaps  883:                break;
                    884:        case (SCALE_PC):
1.21      kristaps  885:                r = PNT2AFM(p, su->scale * 12);
1.19      kristaps  886:                break;
                    887:        case (SCALE_PT):
1.21      kristaps  888:                r = PNT2AFM(p, su->scale * 100);
1.19      kristaps  889:                break;
                    890:        case (SCALE_EM):
                    891:                r = su->scale *
                    892:                        fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
                    893:                break;
                    894:        case (SCALE_MM):
1.21      kristaps  895:                r = PNT2AFM(p, su->scale * 2.834);
1.19      kristaps  896:                break;
                    897:        case (SCALE_EN):
                    898:                r = su->scale *
                    899:                        fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
                    900:                break;
                    901:        case (SCALE_VS):
                    902:                r = su->scale * p->engine.ps.lineheight;
                    903:                break;
                    904:        default:
                    905:                r = su->scale;
                    906:                break;
                    907:        }
                    908:
1.20      kristaps  909:        return(r);
1.19      kristaps  910: }
                    911:

CVSweb