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

Annotation of mandoc/term_ps.c, Revision 1.21

1.21    ! kristaps    1: /*     $Id: term_ps.c,v 1.20 2010/06/30 12:30:36 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.13      kristaps   60:        { "Courier", {
                     61:                { 600 },
                     62:                { 600 },
                     63:                { 600 },
                     64:                { 600 },
                     65:                { 600 },
                     66:                { 600 },
                     67:                { 600 },
                     68:                { 600 },
                     69:                { 600 },
                     70:                { 600 },
                     71:                { 600 },
                     72:                { 600 },
                     73:                { 600 },
                     74:                { 600 },
                     75:                { 600 },
                     76:                { 600 },
                     77:                { 600 },
                     78:                { 600 },
                     79:                { 600 },
                     80:                { 600 },
                     81:                { 600 },
                     82:                { 600 },
                     83:                { 600 },
                     84:                { 600 },
                     85:                { 600 },
                     86:                { 600 },
                     87:                { 600 },
                     88:                { 600 },
                     89:                { 600 },
                     90:                { 600 },
                     91:                { 600 },
                     92:                { 600 },
                     93:                { 600 },
                     94:                { 600 },
                     95:                { 600 },
                     96:                { 600 },
                     97:                { 600 },
                     98:                { 600 },
                     99:                { 600 },
                    100:                { 600 },
                    101:                { 600 },
                    102:                { 600 },
                    103:                { 600 },
                    104:                { 600 },
                    105:                { 600 },
                    106:                { 600 },
                    107:                { 600 },
                    108:                { 600 },
                    109:                { 600 },
                    110:                { 600 },
                    111:                { 600 },
                    112:                { 600 },
                    113:                { 600 },
                    114:                { 600 },
                    115:                { 600 },
                    116:                { 600 },
                    117:                { 600 },
                    118:                { 600 },
                    119:                { 600 },
                    120:                { 600 },
                    121:                { 600 },
                    122:                { 600 },
                    123:                { 600 },
                    124:                { 600 },
                    125:                { 600 },
                    126:                { 600 },
                    127:                { 600 },
                    128:                { 600 },
                    129:                { 600 },
                    130:                { 600 },
                    131:                { 600 },
                    132:                { 600 },
                    133:                { 600 },
                    134:                { 600 },
                    135:                { 600 },
                    136:                { 600 },
                    137:                { 600 },
                    138:                { 600 },
                    139:                { 600 },
                    140:                { 600 },
                    141:                { 600 },
                    142:                { 600 },
                    143:                { 600 },
                    144:                { 600 },
                    145:                { 600 },
                    146:                { 600 },
                    147:                { 600 },
                    148:                { 600 },
                    149:                { 600 },
                    150:                { 600 },
                    151:                { 600 },
                    152:                { 600 },
                    153:                { 600 },
                    154:                { 600 },
                    155:                { 600 },
                    156:        } },
                    157:        { "Courier-Bold", {
                    158:                { 600 },
                    159:                { 600 },
                    160:                { 600 },
                    161:                { 600 },
                    162:                { 600 },
                    163:                { 600 },
                    164:                { 600 },
                    165:                { 600 },
                    166:                { 600 },
                    167:                { 600 },
                    168:                { 600 },
                    169:                { 600 },
                    170:                { 600 },
                    171:                { 600 },
                    172:                { 600 },
                    173:                { 600 },
                    174:                { 600 },
                    175:                { 600 },
                    176:                { 600 },
                    177:                { 600 },
                    178:                { 600 },
                    179:                { 600 },
                    180:                { 600 },
                    181:                { 600 },
                    182:                { 600 },
                    183:                { 600 },
                    184:                { 600 },
                    185:                { 600 },
                    186:                { 600 },
                    187:                { 600 },
                    188:                { 600 },
                    189:                { 600 },
                    190:                { 600 },
                    191:                { 600 },
                    192:                { 600 },
                    193:                { 600 },
                    194:                { 600 },
                    195:                { 600 },
                    196:                { 600 },
                    197:                { 600 },
                    198:                { 600 },
                    199:                { 600 },
                    200:                { 600 },
                    201:                { 600 },
                    202:                { 600 },
                    203:                { 600 },
                    204:                { 600 },
                    205:                { 600 },
                    206:                { 600 },
                    207:                { 600 },
                    208:                { 600 },
                    209:                { 600 },
                    210:                { 600 },
                    211:                { 600 },
                    212:                { 600 },
                    213:                { 600 },
                    214:                { 600 },
                    215:                { 600 },
                    216:                { 600 },
                    217:                { 600 },
                    218:                { 600 },
                    219:                { 600 },
                    220:                { 600 },
                    221:                { 600 },
                    222:                { 600 },
                    223:                { 600 },
                    224:                { 600 },
                    225:                { 600 },
                    226:                { 600 },
                    227:                { 600 },
                    228:                { 600 },
                    229:                { 600 },
                    230:                { 600 },
                    231:                { 600 },
                    232:                { 600 },
                    233:                { 600 },
                    234:                { 600 },
                    235:                { 600 },
                    236:                { 600 },
                    237:                { 600 },
                    238:                { 600 },
                    239:                { 600 },
                    240:                { 600 },
                    241:                { 600 },
                    242:                { 600 },
                    243:                { 600 },
                    244:                { 600 },
                    245:                { 600 },
                    246:                { 600 },
                    247:                { 600 },
                    248:                { 600 },
                    249:                { 600 },
                    250:                { 600 },
                    251:                { 600 },
                    252:                { 600 },
                    253:        } },
                    254:        { "Courier-Oblique", {
                    255:                { 600 },
                    256:                { 600 },
                    257:                { 600 },
                    258:                { 600 },
                    259:                { 600 },
                    260:                { 600 },
                    261:                { 600 },
                    262:                { 600 },
                    263:                { 600 },
                    264:                { 600 },
                    265:                { 600 },
                    266:                { 600 },
                    267:                { 600 },
                    268:                { 600 },
                    269:                { 600 },
                    270:                { 600 },
                    271:                { 600 },
                    272:                { 600 },
                    273:                { 600 },
                    274:                { 600 },
                    275:                { 600 },
                    276:                { 600 },
                    277:                { 600 },
                    278:                { 600 },
                    279:                { 600 },
                    280:                { 600 },
                    281:                { 600 },
                    282:                { 600 },
                    283:                { 600 },
                    284:                { 600 },
                    285:                { 600 },
                    286:                { 600 },
                    287:                { 600 },
                    288:                { 600 },
                    289:                { 600 },
                    290:                { 600 },
                    291:                { 600 },
                    292:                { 600 },
                    293:                { 600 },
                    294:                { 600 },
                    295:                { 600 },
                    296:                { 600 },
                    297:                { 600 },
                    298:                { 600 },
                    299:                { 600 },
                    300:                { 600 },
                    301:                { 600 },
                    302:                { 600 },
                    303:                { 600 },
                    304:                { 600 },
                    305:                { 600 },
                    306:                { 600 },
                    307:                { 600 },
                    308:                { 600 },
                    309:                { 600 },
                    310:                { 600 },
                    311:                { 600 },
                    312:                { 600 },
                    313:                { 600 },
                    314:                { 600 },
                    315:                { 600 },
                    316:                { 600 },
                    317:                { 600 },
                    318:                { 600 },
                    319:                { 600 },
                    320:                { 600 },
                    321:                { 600 },
                    322:                { 600 },
                    323:                { 600 },
                    324:                { 600 },
                    325:                { 600 },
                    326:                { 600 },
                    327:                { 600 },
                    328:                { 600 },
                    329:                { 600 },
                    330:                { 600 },
                    331:                { 600 },
                    332:                { 600 },
                    333:                { 600 },
                    334:                { 600 },
                    335:                { 600 },
                    336:                { 600 },
                    337:                { 600 },
                    338:                { 600 },
                    339:                { 600 },
                    340:                { 600 },
                    341:                { 600 },
                    342:                { 600 },
                    343:                { 600 },
                    344:                { 600 },
                    345:                { 600 },
                    346:                { 600 },
                    347:                { 600 },
                    348:                { 600 },
                    349:                { 600 },
                    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");
                    597:        printf("%%%%Orientation: Portrait\n");
1.16      kristaps  598:        printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
                    599:                        p->engine.ps.width,
                    600:                        p->engine.ps.height);
                    601:        printf("%%%%DocumentNeededResources: font");
                    602:        for (i = 0; i < (int)TERMFONT__MAX; i++)
                    603:                printf(" %s", fonts[i].name);
                    604:        printf("\n%%%%EndComments\n");
                    605:
                    606:        printf("%%%%Page: %zu %zu\n",
                    607:                        p->engine.ps.pages + 1,
                    608:                        p->engine.ps.pages + 1);
1.13      kristaps  609:
1.9       kristaps  610:        ps_setfont(p, TERMFONT_NONE);
1.16      kristaps  611:        p->engine.ps.pscol = p->engine.ps.left;
                    612:        p->engine.ps.psrow = p->engine.ps.top;
1.2       kristaps  613: }
                    614:
                    615:
                    616: static void
1.13      kristaps  617: ps_pletter(struct termp *p, int c)
1.2       kristaps  618: {
1.14      kristaps  619:        int              f;
1.2       kristaps  620:
1.9       kristaps  621:        /*
                    622:         * If we're not in a PostScript "word" context, then open one
                    623:         * now at the current cursor.
                    624:         */
                    625:
1.2       kristaps  626:        if ( ! (PS_INLINE & p->engine.ps.psstate)) {
1.4       kristaps  627:                ps_printf(p, "%zu %zu moveto\n(",
1.21    ! kristaps  628:                                AFM2PNT(p, p->engine.ps.pscol),
        !           629:                                AFM2PNT(p, p->engine.ps.psrow));
1.2       kristaps  630:                p->engine.ps.psstate |= PS_INLINE;
                    631:        }
                    632:
                    633:        /*
                    634:         * We need to escape these characters as per the PostScript
                    635:         * specification.  We would also escape non-graphable characters
                    636:         * (like tabs), but none of them would get to this point and
                    637:         * it's superfluous to abort() on them.
                    638:         */
                    639:
                    640:        switch (c) {
                    641:        case ('('):
                    642:                /* FALLTHROUGH */
                    643:        case (')'):
                    644:                /* FALLTHROUGH */
                    645:        case ('\\'):
1.4       kristaps  646:                ps_putchar(p, '\\');
1.2       kristaps  647:                break;
                    648:        default:
                    649:                break;
                    650:        }
                    651:
                    652:        /* Write the character and adjust where we are on the page. */
1.9       kristaps  653:
1.14      kristaps  654:        f = (int)p->engine.ps.lastf;
1.13      kristaps  655:
1.14      kristaps  656:        if (c <= 32 || (c - 32 > MAXCHAR)) {
1.13      kristaps  657:                ps_putchar(p, ' ');
1.18      kristaps  658:                p->engine.ps.pscol += fonts[f].gly[0].wx;
1.13      kristaps  659:                return;
                    660:        }
                    661:
1.4       kristaps  662:        ps_putchar(p, c);
1.14      kristaps  663:        c -= 32;
1.18      kristaps  664:        p->engine.ps.pscol += fonts[f].gly[c].wx;
1.2       kristaps  665: }
                    666:
                    667:
                    668: static void
1.9       kristaps  669: ps_pclose(struct termp *p)
                    670: {
                    671:
                    672:        /*
                    673:         * Spit out that we're exiting a word context (this is a
                    674:         * "partial close" because we don't check the last-char buffer
                    675:         * or anything).
                    676:         */
                    677:
                    678:        if ( ! (PS_INLINE & p->engine.ps.psstate))
                    679:                return;
                    680:
                    681:        ps_printf(p, ") show\n");
                    682:        p->engine.ps.psstate &= ~PS_INLINE;
                    683: }
                    684:
                    685:
                    686: static void
                    687: ps_fclose(struct termp *p)
                    688: {
                    689:
                    690:        /*
                    691:         * Strong closure: if we have a last-char, spit it out after
                    692:         * checking that we're in the right font mode.  This will of
                    693:         * course open a new scope, if applicable.
                    694:         *
                    695:         * Following this, close out any scope that's open.
                    696:         */
                    697:
                    698:        if ('\0' != p->engine.ps.last) {
                    699:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    700:                        ps_pclose(p);
                    701:                        ps_setfont(p, TERMFONT_NONE);
                    702:                }
                    703:                ps_pletter(p, p->engine.ps.last);
                    704:                p->engine.ps.last = '\0';
                    705:        }
                    706:
                    707:        if ( ! (PS_INLINE & p->engine.ps.psstate))
                    708:                return;
                    709:
                    710:        ps_pclose(p);
                    711: }
                    712:
                    713:
                    714: static void
1.8       kristaps  715: ps_letter(struct termp *p, char c)
                    716: {
                    717:        char            cc;
1.9       kristaps  718:
                    719:        /*
                    720:         * State machine dictates whether to buffer the last character
                    721:         * or not.  Basically, encoded words are detected by checking if
                    722:         * we're an "8" and switching on the buffer.  Then we put "8" in
                    723:         * our buffer, and on the next charater, flush both character
                    724:         * and buffer.  Thus, "regular" words are detected by having a
                    725:         * regular character and a regular buffer character.
                    726:         */
                    727:
1.8       kristaps  728:        if ('\0' == p->engine.ps.last) {
                    729:                assert(8 != c);
                    730:                p->engine.ps.last = c;
                    731:                return;
                    732:        } else if (8 == p->engine.ps.last) {
                    733:                assert(8 != c);
1.9       kristaps  734:                p->engine.ps.last = '\0';
1.8       kristaps  735:        } else if (8 == c) {
                    736:                assert(8 != p->engine.ps.last);
1.9       kristaps  737:                if ('_' == p->engine.ps.last) {
                    738:                        if (p->engine.ps.lastf != TERMFONT_UNDER) {
                    739:                                ps_pclose(p);
                    740:                                ps_setfont(p, TERMFONT_UNDER);
                    741:                        }
                    742:                } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
                    743:                        ps_pclose(p);
                    744:                        ps_setfont(p, TERMFONT_BOLD);
                    745:                }
1.8       kristaps  746:                p->engine.ps.last = c;
                    747:                return;
                    748:        } else {
1.9       kristaps  749:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    750:                        ps_pclose(p);
                    751:                        ps_setfont(p, TERMFONT_NONE);
                    752:                }
1.8       kristaps  753:                cc = p->engine.ps.last;
                    754:                p->engine.ps.last = c;
                    755:                c = cc;
                    756:        }
                    757:
1.9       kristaps  758:        ps_pletter(p, c);
1.8       kristaps  759: }
                    760:
                    761:
                    762: static void
1.2       kristaps  763: ps_advance(struct termp *p, size_t len)
                    764: {
                    765:
1.9       kristaps  766:        /*
                    767:         * Advance some spaces.  This can probably be made smarter,
                    768:         * i.e., to have multiple space-separated words in the same
                    769:         * scope, but this is easier:  just close out the current scope
                    770:         * and readjust our column settings.
                    771:         */
1.8       kristaps  772:
1.9       kristaps  773:        ps_fclose(p);
1.14      kristaps  774:        p->engine.ps.pscol += len;
1.2       kristaps  775: }
                    776:
                    777:
                    778: static void
                    779: ps_endline(struct termp *p)
                    780: {
1.8       kristaps  781:
1.9       kristaps  782:        /* Close out any scopes we have open: we're at eoln. */
                    783:
                    784:        ps_fclose(p);
1.2       kristaps  785:
1.9       kristaps  786:        /*
                    787:         * If we're in the margin, don't try to recalculate our current
                    788:         * row.  XXX: if the column tries to be fancy with multiple
                    789:         * lines, we'll do nasty stuff.
                    790:         */
1.2       kristaps  791:
                    792:        if (PS_MARGINS & p->engine.ps.psstate)
                    793:                return;
                    794:
1.9       kristaps  795:        /*
                    796:         * Put us down a line.  If we're at the page bottom, spit out a
                    797:         * showpage and restart our row.
                    798:         */
                    799:
1.16      kristaps  800:        p->engine.ps.pscol = p->engine.ps.left;
                    801:        if (p->engine.ps.psrow >= p->engine.ps.lineheight +
                    802:                        p->engine.ps.bottom) {
                    803:                p->engine.ps.psrow -= p->engine.ps.lineheight;
1.2       kristaps  804:                return;
                    805:        }
                    806:
1.4       kristaps  807:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    808:        printf("%s", p->engine.ps.psmarg);
1.16      kristaps  809:        printf("%%%%Page: %zu %zu\n",
                    810:                        p->engine.ps.pages + 1,
                    811:                        p->engine.ps.pages + 1);
1.2       kristaps  812:        printf("showpage\n");
1.16      kristaps  813:        p->engine.ps.pages++;
                    814:        p->engine.ps.psrow = p->engine.ps.top;
1.1       kristaps  815: }
1.9       kristaps  816:
                    817:
                    818: static void
                    819: ps_setfont(struct termp *p, enum termfont f)
                    820: {
                    821:
1.16      kristaps  822:        assert(f < TERMFONT__MAX);
1.21    ! kristaps  823:        ps_printf(p, "/%s %zu selectfont\n",
        !           824:                        fonts[(int)f].name, p->engine.ps.scale);
1.9       kristaps  825:        p->engine.ps.lastf = f;
                    826: }
                    827:
1.11      kristaps  828:
1.12      kristaps  829: /* ARGSUSED */
1.11      kristaps  830: static size_t
                    831: ps_width(const struct termp *p, char c)
                    832: {
                    833:
1.14      kristaps  834:        if (c <= 32 || c - 32 >= MAXCHAR)
1.18      kristaps  835:                return(fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14      kristaps  836:
                    837:        c -= 32;
1.18      kristaps  838:        return(fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1.11      kristaps  839: }
1.19      kristaps  840:
                    841:
1.20      kristaps  842: static double
1.19      kristaps  843: ps_hspan(const struct termp *p, const struct roffsu *su)
                    844: {
                    845:        double           r;
                    846:
                    847:        /*
                    848:         * All of these measurements are derived by converting from the
1.21    ! kristaps  849:         * native measurement to AFM units.
1.19      kristaps  850:         */
                    851:
                    852:        switch (su->unit) {
                    853:        case (SCALE_CM):
1.21    ! kristaps  854:                r = PNT2AFM(p, su->scale * 28.34);
1.19      kristaps  855:                break;
                    856:        case (SCALE_IN):
1.21    ! kristaps  857:                r = PNT2AFM(p, su->scale * 72);
1.19      kristaps  858:                break;
                    859:        case (SCALE_PC):
1.21    ! kristaps  860:                r = PNT2AFM(p, su->scale * 12);
1.19      kristaps  861:                break;
                    862:        case (SCALE_PT):
1.21    ! kristaps  863:                r = PNT2AFM(p, su->scale * 100);
1.19      kristaps  864:                break;
                    865:        case (SCALE_EM):
                    866:                r = su->scale *
                    867:                        fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
                    868:                break;
                    869:        case (SCALE_MM):
1.21    ! kristaps  870:                r = PNT2AFM(p, su->scale * 2.834);
1.19      kristaps  871:                break;
                    872:        case (SCALE_EN):
                    873:                r = su->scale *
                    874:                        fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
                    875:                break;
                    876:        case (SCALE_VS):
                    877:                r = su->scale * p->engine.ps.lineheight;
                    878:                break;
                    879:        default:
                    880:                r = su->scale;
                    881:                break;
                    882:        }
                    883:
1.20      kristaps  884:        return(r);
1.19      kristaps  885: }
                    886:

CVSweb