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

Annotation of mandoc/term_ps.c, Revision 1.22

1.22    ! kristaps    1: /*     $Id: term_ps.c,v 1.21 2010/06/30 13:00:00 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.22    ! kristaps   60:        { "CharterBT-Roman", {
        !            61:                {  278 },
        !            62:                {  338 },
        !            63:                {  331 },
        !            64:                {  745 },
        !            65:                {  556 },
        !            66:                {  852 },
        !            67:                {  704 },
        !            68:                {  201 },
        !            69:                {  417 },
        !            70:                {  417 },
        !            71:                {  500 },
        !            72:                {  833 },
        !            73:                {  278 },
        !            74:                {  319 },
        !            75:                {  278 },
        !            76:                {  481 },
        !            77:                {  556 },
        !            78:                {  556 },
        !            79:                {  556 },
        !            80:                {  556 },
        !            81:                {  556 },
        !            82:                {  556 },
        !            83:                {  556 },
        !            84:                {  556 },
        !            85:                {  556 },
        !            86:                {  556 },
        !            87:                {  319 },
        !            88:                {  319 },
        !            89:                {  833 },
        !            90:                {  833 },
        !            91:                {  833 },
        !            92:                {  486 },
        !            93:                {  942 },
        !            94:                {  639 },
        !            95:                {  604 },
        !            96:                {  632 },
        !            97:                {  693 },
        !            98:                {  576 },
        !            99:                {  537 },
        !           100:                {  694 },
        !           101:                {  738 },
        !           102:                {  324 },
        !           103:                {  444 },
        !           104:                {  611 },
        !           105:                {  520 },
        !           106:                {  866 },
        !           107:                {  713 },
        !           108:                {  731 },
        !           109:                {  558 },
        !           110:                {  731 },
        !           111:                {  646 },
        !           112:                {  556 },
        !           113:                {  597 },
        !           114:                {  694 },
        !           115:                {  618 },
        !           116:                {  928 },
        !           117:                {  600 },
        !           118:                {  586 },
        !           119:                {  586 },
        !           120:                {  421 },
        !           121:                {  481 },
        !           122:                {  421 },
        !           123:                { 1000 },
        !           124:                {  500 },
        !           125:                {  201 },
        !           126:                {  507 },
        !           127:                {  539 },
        !           128:                {  446 },
        !           129:                {  565 },
        !           130:                {  491 },
        !           131:                {  321 },
        !           132:                {  523 },
        !           133:                {  564 },
        !           134:                {  280 },
        !           135:                {  266 },
        !           136:                {  517 },
        !           137:                {  282 },
        !           138:                {  843 },
        !           139:                {  568 },
        !           140:                {  539 },
        !           141:                {  551 },
        !           142:                {  531 },
        !           143:                {  382 },
        !           144:                {  400 },
        !           145:                {  334 },
        !           146:                {  569 },
        !           147:                {  494 },
        !           148:                {  771 },
        !           149:                {  503 },
        !           150:                {  495 },
        !           151:                {  468 },
        !           152:                {  486 },
        !           153:                {  500 },
        !           154:                {  486 },
        !           155:                {  833 },
1.13      kristaps  156:        } },
1.22    ! kristaps  157:        { "CharterBT-Bold", {
        !           158:                {  291 },
        !           159:                {  340 },
        !           160:                {  339 },
        !           161:                {  736 },
        !           162:                {  581 },
        !           163:                {  888 },
        !           164:                {  741 },
        !           165:                {  255 },
        !           166:                {  428 },
        !           167:                {  428 },
        !           168:                {  500 },
        !           169:                {  833 },
        !           170:                {  289 },
        !           171:                {  326 },
        !           172:                {  289 },
        !           173:                {  491 },
        !           174:                {  581 },
        !           175:                {  581 },
        !           176:                {  581 },
        !           177:                {  581 },
        !           178:                {  581 },
        !           179:                {  581 },
        !           180:                {  581 },
        !           181:                {  581 },
        !           182:                {  581 },
        !           183:                {  581 },
        !           184:                {  340 },
        !           185:                {  340 },
        !           186:                {  833 },
        !           187:                {  833 },
        !           188:                {  833 },
        !           189:                {  487 },
        !           190:                {  917 },
        !           191:                {  651 },
        !           192:                {  628 },
        !           193:                {  638 },
        !           194:                {  716 },
        !           195:                {  596 },
        !           196:                {  552 },
        !           197:                {  710 },
        !           198:                {  760 },
        !           199:                {  354 },
        !           200:                {  465 },
        !           201:                {  650 },
        !           202:                {  543 },
        !           203:                {  883 },
        !           204:                {  727 },
        !           205:                {  752 },
        !           206:                {  587 },
        !           207:                {  752 },
        !           208:                {  671 },
        !           209:                {  568 },
        !           210:                {  603 },
        !           211:                {  705 },
        !           212:                {  635 },
        !           213:                {  946 },
        !           214:                {  637 },
        !           215:                {  610 },
        !           216:                {  592 },
        !           217:                {  443 },
        !           218:                {  491 },
        !           219:                {  443 },
        !           220:                { 1000 },
        !           221:                {  500 },
        !           222:                {  255 },
        !           223:                {  544 },
        !           224:                {  577 },
        !           225:                {  476 },
        !           226:                {  596 },
        !           227:                {  524 },
        !           228:                {  341 },
        !           229:                {  551 },
        !           230:                {  597 },
        !           231:                {  305 },
        !           232:                {  297 },
        !           233:                {  553 },
        !           234:                {  304 },
        !           235:                {  892 },
        !           236:                {  605 },
        !           237:                {  577 },
        !           238:                {  591 },
        !           239:                {  575 },
        !           240:                {  421 },
        !           241:                {  447 },
        !           242:                {  358 },
        !           243:                {  600 },
        !           244:                {  513 },
        !           245:                {  799 },
        !           246:                {  531 },
        !           247:                {  515 },
        !           248:                {  495 },
        !           249:                {  493 },
        !           250:                {  500 },
        !           251:                {  493 },
        !           252:                {  833 },
1.13      kristaps  253:        } },
1.22    ! kristaps  254:        { "CharterBT-Italic", {
        !           255:                {  278 },
        !           256:                {  338 },
        !           257:                {  331 },
        !           258:                {  745 },
        !           259:                {  556 },
        !           260:                {  852 },
        !           261:                {  704 },
        !           262:                {  201 },
        !           263:                {  419 },
        !           264:                {  419 },
        !           265:                {  500 },
        !           266:                {  833 },
        !           267:                {  278 },
        !           268:                {  319 },
        !           269:                {  278 },
        !           270:                {  481 },
        !           271:                {  556 },
        !           272:                {  556 },
        !           273:                {  556 },
        !           274:                {  556 },
        !           275:                {  556 },
        !           276:                {  556 },
        !           277:                {  556 },
        !           278:                {  556 },
        !           279:                {  556 },
        !           280:                {  556 },
        !           281:                {  319 },
        !           282:                {  319 },
        !           283:                {  833 },
        !           284:                {  833 },
        !           285:                {  833 },
        !           286:                {  486 },
        !           287:                {  942 },
        !           288:                {  606 },
        !           289:                {  588 },
        !           290:                {  604 },
        !           291:                {  671 },
        !           292:                {  546 },
        !           293:                {  509 },
        !           294:                {  664 },
        !           295:                {  712 },
        !           296:                {  312 },
        !           297:                {  447 },
        !           298:                {  625 },
        !           299:                {  498 },
        !           300:                {  839 },
        !           301:                {  683 },
        !           302:                {  708 },
        !           303:                {  542 },
        !           304:                {  708 },
        !           305:                {  602 },
        !           306:                {  537 },
        !           307:                {  565 },
        !           308:                {  664 },
        !           309:                {  590 },
        !           310:                {  898 },
        !           311:                {  569 },
        !           312:                {  562 },
        !           313:                {  556 },
        !           314:                {  421 },
        !           315:                {  481 },
        !           316:                {  421 },
        !           317:                { 1000 },
        !           318:                {  500 },
        !           319:                {  201 },
        !           320:                {  525 },
        !           321:                {  507 },
        !           322:                {  394 },
        !           323:                {  523 },
        !           324:                {  424 },
        !           325:                {  292 },
        !           326:                {  481 },
        !           327:                {  551 },
        !           328:                {  287 },
        !           329:                {  269 },
        !           330:                {  514 },
        !           331:                {  275 },
        !           332:                {  815 },
        !           333:                {  556 },
        !           334:                {  502 },
        !           335:                {  516 },
        !           336:                {  512 },
        !           337:                {  398 },
        !           338:                {  370 },
        !           339:                {  333 },
        !           340:                {  553 },
        !           341:                {  454 },
        !           342:                {  713 },
        !           343:                {  477 },
        !           344:                {  475 },
        !           345:                {  440 },
        !           346:                {  486 },
        !           347:                {  500 },
        !           348:                {  486 },
        !           349:                {  833 },
1.13      kristaps  350:        } },
                    351: };
                    352:
1.15      kristaps  353: /* These work the buffer used by the header and footer. */
1.4       kristaps  354: #define        PS_BUFSLOP        128
                    355: #define        PS_GROWBUF(p, sz) \
                    356:        do if ((p)->engine.ps.psmargcur + (sz) > \
                    357:                        (p)->engine.ps.psmargsz) { \
                    358:                (p)->engine.ps.psmargsz += /* CONSTCOND */ \
                    359:                        MAX(PS_BUFSLOP, (sz)); \
                    360:                (p)->engine.ps.psmarg = realloc \
                    361:                        ((p)->engine.ps.psmarg,  \
                    362:                         (p)->engine.ps.psmargsz); \
                    363:                if (NULL == (p)->engine.ps.psmarg) { \
                    364:                        perror(NULL); \
                    365:                        exit(EXIT_FAILURE); \
                    366:                } \
                    367:        } while (/* CONSTCOND */ 0)
                    368:
1.9       kristaps  369:
1.20      kristaps  370: static double            ps_hspan(const struct termp *,
1.19      kristaps  371:                                const struct roffsu *);
                    372: static size_t            ps_width(const struct termp *, char);
                    373: static void              ps_advance(struct termp *, size_t);
1.2       kristaps  374: static void              ps_begin(struct termp *);
                    375: static void              ps_end(struct termp *);
                    376: static void              ps_endline(struct termp *);
1.9       kristaps  377: static void              ps_fclose(struct termp *);
1.19      kristaps  378: static void              ps_letter(struct termp *, char);
1.9       kristaps  379: static void              ps_pclose(struct termp *);
1.13      kristaps  380: static void              ps_pletter(struct termp *, int);
1.4       kristaps  381: static void              ps_printf(struct termp *, const char *, ...);
                    382: static void              ps_putchar(struct termp *, char);
1.9       kristaps  383: static void              ps_setfont(struct termp *, enum termfont);
1.2       kristaps  384:
                    385:
1.1       kristaps  386: void *
1.17      kristaps  387: ps_alloc(char *outopts)
1.1       kristaps  388: {
                    389:        struct termp    *p;
1.18      kristaps  390:        size_t           pagex, pagey, margin, lineheight;
1.17      kristaps  391:        const char      *toks[2];
1.18      kristaps  392:        const char      *paper;
1.17      kristaps  393:        char            *v;
1.1       kristaps  394:
                    395:        if (NULL == (p = term_alloc(TERMENC_ASCII)))
                    396:                return(NULL);
                    397:
1.19      kristaps  398:        p->advance = ps_advance;
1.2       kristaps  399:        p->begin = ps_begin;
                    400:        p->end = ps_end;
                    401:        p->endline = ps_endline;
1.19      kristaps  402:        p->hspan = ps_hspan;
                    403:        p->letter = ps_letter;
                    404:        p->type = TERMTYPE_PS;
1.11      kristaps  405:        p->width = ps_width;
1.21      kristaps  406:
                    407:        p->engine.ps.scale = 10;
1.17      kristaps  408:
                    409:        toks[0] = "paper";
                    410:        toks[1] = NULL;
                    411:
1.19      kristaps  412:        paper = "letter";
                    413:
1.17      kristaps  414:        while (outopts && *outopts)
                    415:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    416:                case (0):
1.18      kristaps  417:                        paper = v;
1.17      kristaps  418:                        break;
                    419:                default:
                    420:                        break;
                    421:                }
1.16      kristaps  422:
1.21      kristaps  423:        margin = PNT2AFM(p, 72);
                    424:        lineheight = PNT2AFM(p, 12);
                    425:
1.18      kristaps  426:        if (0 == strcasecmp(paper, "a4")) {
1.21      kristaps  427:                pagex = PNT2AFM(p, 595);
                    428:                pagey = PNT2AFM(p, 842);
1.18      kristaps  429:        } else {
1.21      kristaps  430:                pagex = PNT2AFM(p, 612);
                    431:                pagey = PNT2AFM(p, 792);
1.18      kristaps  432:        }
                    433:
1.16      kristaps  434:        assert(margin * 2 < pagex);
                    435:        assert(margin * 2 < pagey);
                    436:
                    437:        p->engine.ps.width = pagex;
                    438:        p->engine.ps.height = pagey;
                    439:        p->engine.ps.header = pagey - (margin / 2);
                    440:        p->engine.ps.top = pagey - margin;
                    441:        p->engine.ps.footer = (margin / 2);
                    442:        p->engine.ps.bottom = margin;
                    443:        p->engine.ps.left = margin;
1.18      kristaps  444:        p->engine.ps.lineheight = lineheight;
1.16      kristaps  445:
                    446:        p->defrmargin = pagex - (margin * 2);
1.1       kristaps  447:        return(p);
                    448: }
                    449:
                    450:
                    451: void
                    452: ps_free(void *arg)
                    453: {
1.4       kristaps  454:        struct termp    *p;
                    455:
                    456:        p = (struct termp *)arg;
                    457:
                    458:        if (p->engine.ps.psmarg)
                    459:                free(p->engine.ps.psmarg);
                    460:
                    461:        term_free(p);
                    462: }
                    463:
                    464:
                    465: static void
                    466: ps_printf(struct termp *p, const char *fmt, ...)
                    467: {
                    468:        va_list          ap;
                    469:        int              pos;
                    470:
                    471:        va_start(ap, fmt);
                    472:
                    473:        /*
                    474:         * If we're running in regular mode, then pipe directly into
                    475:         * vprintf().  If we're processing margins, then push the data
                    476:         * into our growable margin buffer.
                    477:         */
1.1       kristaps  478:
1.4       kristaps  479:        if ( ! (PS_MARGINS & p->engine.ps.psstate)) {
                    480:                vprintf(fmt, ap);
                    481:                va_end(ap);
                    482:                return;
                    483:        }
                    484:
                    485:        /*
                    486:         * XXX: I assume that the in-margin print won't exceed
                    487:         * PS_BUFSLOP (128 bytes), which is reasonable but still an
                    488:         * assumption that will cause pukeage if it's not the case.
                    489:         */
                    490:
                    491:        PS_GROWBUF(p, PS_BUFSLOP);
                    492:
                    493:        pos = (int)p->engine.ps.psmargcur;
                    494:        vsnprintf(&p->engine.ps.psmarg[pos], PS_BUFSLOP, fmt, ap);
                    495:        p->engine.ps.psmargcur = strlen(p->engine.ps.psmarg);
1.5       kristaps  496:
                    497:        va_end(ap);
1.4       kristaps  498: }
                    499:
                    500:
                    501: static void
                    502: ps_putchar(struct termp *p, char c)
                    503: {
                    504:        int              pos;
                    505:
                    506:        /* See ps_printf(). */
                    507:
                    508:        if ( ! (PS_MARGINS & p->engine.ps.psstate)) {
                    509:                putchar(c);
                    510:                return;
                    511:        }
                    512:
                    513:        PS_GROWBUF(p, 2);
                    514:
                    515:        pos = (int)p->engine.ps.psmargcur++;
1.5       kristaps  516:        p->engine.ps.psmarg[pos++] = c;
1.4       kristaps  517:        p->engine.ps.psmarg[pos] = '\0';
1.2       kristaps  518: }
                    519:
                    520:
1.3       kristaps  521: /* ARGSUSED */
1.2       kristaps  522: static void
                    523: ps_end(struct termp *p)
                    524: {
                    525:
1.4       kristaps  526:        /*
                    527:         * At the end of the file, do one last showpage.  This is the
                    528:         * same behaviour as groff(1) and works for multiple pages as
                    529:         * well as just one.
                    530:         */
                    531:
1.8       kristaps  532:        assert(0 == p->engine.ps.psstate);
                    533:        assert('\0' == p->engine.ps.last);
1.4       kristaps  534:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    535:        printf("%s", p->engine.ps.psmarg);
1.16      kristaps  536:        p->engine.ps.pages++;
1.4       kristaps  537:        printf("showpage\n");
1.16      kristaps  538:
                    539:        printf("%%%%Trailer\n");
                    540:        printf("%%%%Pages: %zu\n", p->engine.ps.pages);
                    541:        printf("%%%%EOF\n");
1.2       kristaps  542: }
                    543:
                    544:
                    545: static void
                    546: ps_begin(struct termp *p)
                    547: {
1.13      kristaps  548:        time_t           t;
1.16      kristaps  549:        int              i;
1.2       kristaps  550:
1.9       kristaps  551:        /*
                    552:         * Print margins into margin buffer.  Nothing gets output to the
                    553:         * screen yet, so we don't need to initialise the primary state.
1.2       kristaps  554:         */
                    555:
1.4       kristaps  556:        if (p->engine.ps.psmarg) {
                    557:                assert(p->engine.ps.psmargsz);
                    558:                p->engine.ps.psmarg[0] = '\0';
                    559:        }
                    560:
                    561:        p->engine.ps.psmargcur = 0;
1.9       kristaps  562:        p->engine.ps.psstate = PS_MARGINS;
1.16      kristaps  563:        p->engine.ps.pscol = p->engine.ps.left;
                    564:        p->engine.ps.psrow = p->engine.ps.header;
1.4       kristaps  565:
1.9       kristaps  566:        ps_setfont(p, TERMFONT_NONE);
1.4       kristaps  567:
                    568:        (*p->headf)(p, p->argf);
                    569:        (*p->endline)(p);
                    570:
1.16      kristaps  571:        p->engine.ps.pscol = p->engine.ps.left;
                    572:        p->engine.ps.psrow = p->engine.ps.footer;
1.4       kristaps  573:
                    574:        (*p->footf)(p, p->argf);
                    575:        (*p->endline)(p);
                    576:
                    577:        p->engine.ps.psstate &= ~PS_MARGINS;
1.9       kristaps  578:
1.4       kristaps  579:        assert(0 == p->engine.ps.psstate);
1.9       kristaps  580:        assert(p->engine.ps.psmarg);
                    581:        assert('\0' != p->engine.ps.psmarg[0]);
                    582:
                    583:        /*
                    584:         * Print header and initialise page state.  Following this,
                    585:         * stuff gets printed to the screen, so make sure we're sane.
                    586:         */
1.4       kristaps  587:
1.13      kristaps  588:        t = time(NULL);
                    589:
1.16      kristaps  590:        printf("%%!PS-Adobe-3.0\n");
1.13      kristaps  591:        printf("%%%%Creator: mandoc-%s\n", VERSION);
                    592:        printf("%%%%CreationDate: %s", ctime(&t));
1.16      kristaps  593:        printf("%%%%DocumentData: Clean7Bit\n");
                    594:        printf("%%%%Orientation: Portrait\n");
                    595:        printf("%%%%Pages: (atend)\n");
1.13      kristaps  596:        printf("%%%%PageOrder: Ascend\n");
                    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