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

Annotation of mandoc/term_ps.c, Revision 1.19

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

CVSweb