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

Annotation of mandoc/term_ps.c, Revision 1.23

1.23    ! kristaps    1: /*     $Id: term_ps.c,v 1.22 2010/06/30 13:16:25 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");
1.16      kristaps  597:        printf("%%%%DocumentMedia: Default %zu %zu 0 () ()\n",
1.23    ! kristaps  598:                        AFM2PNT(p, p->engine.ps.width),
        !           599:                        AFM2PNT(p, p->engine.ps.height));
1.16      kristaps  600:        printf("%%%%DocumentNeededResources: font");
                    601:        for (i = 0; i < (int)TERMFONT__MAX; i++)
                    602:                printf(" %s", fonts[i].name);
                    603:        printf("\n%%%%EndComments\n");
                    604:
                    605:        printf("%%%%Page: %zu %zu\n",
                    606:                        p->engine.ps.pages + 1,
                    607:                        p->engine.ps.pages + 1);
1.13      kristaps  608:
1.9       kristaps  609:        ps_setfont(p, TERMFONT_NONE);
1.16      kristaps  610:        p->engine.ps.pscol = p->engine.ps.left;
                    611:        p->engine.ps.psrow = p->engine.ps.top;
1.2       kristaps  612: }
                    613:
                    614:
                    615: static void
1.13      kristaps  616: ps_pletter(struct termp *p, int c)
1.2       kristaps  617: {
1.14      kristaps  618:        int              f;
1.2       kristaps  619:
1.9       kristaps  620:        /*
                    621:         * If we're not in a PostScript "word" context, then open one
                    622:         * now at the current cursor.
                    623:         */
                    624:
1.2       kristaps  625:        if ( ! (PS_INLINE & p->engine.ps.psstate)) {
1.4       kristaps  626:                ps_printf(p, "%zu %zu moveto\n(",
1.21      kristaps  627:                                AFM2PNT(p, p->engine.ps.pscol),
                    628:                                AFM2PNT(p, p->engine.ps.psrow));
1.2       kristaps  629:                p->engine.ps.psstate |= PS_INLINE;
                    630:        }
                    631:
                    632:        /*
                    633:         * We need to escape these characters as per the PostScript
                    634:         * specification.  We would also escape non-graphable characters
                    635:         * (like tabs), but none of them would get to this point and
                    636:         * it's superfluous to abort() on them.
                    637:         */
                    638:
                    639:        switch (c) {
                    640:        case ('('):
                    641:                /* FALLTHROUGH */
                    642:        case (')'):
                    643:                /* FALLTHROUGH */
                    644:        case ('\\'):
1.4       kristaps  645:                ps_putchar(p, '\\');
1.2       kristaps  646:                break;
                    647:        default:
                    648:                break;
                    649:        }
                    650:
                    651:        /* Write the character and adjust where we are on the page. */
1.9       kristaps  652:
1.14      kristaps  653:        f = (int)p->engine.ps.lastf;
1.13      kristaps  654:
1.14      kristaps  655:        if (c <= 32 || (c - 32 > MAXCHAR)) {
1.13      kristaps  656:                ps_putchar(p, ' ');
1.18      kristaps  657:                p->engine.ps.pscol += fonts[f].gly[0].wx;
1.13      kristaps  658:                return;
                    659:        }
                    660:
1.4       kristaps  661:        ps_putchar(p, c);
1.14      kristaps  662:        c -= 32;
1.18      kristaps  663:        p->engine.ps.pscol += fonts[f].gly[c].wx;
1.2       kristaps  664: }
                    665:
                    666:
                    667: static void
1.9       kristaps  668: ps_pclose(struct termp *p)
                    669: {
                    670:
                    671:        /*
                    672:         * Spit out that we're exiting a word context (this is a
                    673:         * "partial close" because we don't check the last-char buffer
                    674:         * or anything).
                    675:         */
                    676:
                    677:        if ( ! (PS_INLINE & p->engine.ps.psstate))
                    678:                return;
                    679:
                    680:        ps_printf(p, ") show\n");
                    681:        p->engine.ps.psstate &= ~PS_INLINE;
                    682: }
                    683:
                    684:
                    685: static void
                    686: ps_fclose(struct termp *p)
                    687: {
                    688:
                    689:        /*
                    690:         * Strong closure: if we have a last-char, spit it out after
                    691:         * checking that we're in the right font mode.  This will of
                    692:         * course open a new scope, if applicable.
                    693:         *
                    694:         * Following this, close out any scope that's open.
                    695:         */
                    696:
                    697:        if ('\0' != p->engine.ps.last) {
                    698:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    699:                        ps_pclose(p);
                    700:                        ps_setfont(p, TERMFONT_NONE);
                    701:                }
                    702:                ps_pletter(p, p->engine.ps.last);
                    703:                p->engine.ps.last = '\0';
                    704:        }
                    705:
                    706:        if ( ! (PS_INLINE & p->engine.ps.psstate))
                    707:                return;
                    708:
                    709:        ps_pclose(p);
                    710: }
                    711:
                    712:
                    713: static void
1.8       kristaps  714: ps_letter(struct termp *p, char c)
                    715: {
                    716:        char            cc;
1.9       kristaps  717:
                    718:        /*
                    719:         * State machine dictates whether to buffer the last character
                    720:         * or not.  Basically, encoded words are detected by checking if
                    721:         * we're an "8" and switching on the buffer.  Then we put "8" in
                    722:         * our buffer, and on the next charater, flush both character
                    723:         * and buffer.  Thus, "regular" words are detected by having a
                    724:         * regular character and a regular buffer character.
                    725:         */
                    726:
1.8       kristaps  727:        if ('\0' == p->engine.ps.last) {
                    728:                assert(8 != c);
                    729:                p->engine.ps.last = c;
                    730:                return;
                    731:        } else if (8 == p->engine.ps.last) {
                    732:                assert(8 != c);
1.9       kristaps  733:                p->engine.ps.last = '\0';
1.8       kristaps  734:        } else if (8 == c) {
                    735:                assert(8 != p->engine.ps.last);
1.9       kristaps  736:                if ('_' == p->engine.ps.last) {
                    737:                        if (p->engine.ps.lastf != TERMFONT_UNDER) {
                    738:                                ps_pclose(p);
                    739:                                ps_setfont(p, TERMFONT_UNDER);
                    740:                        }
                    741:                } else if (p->engine.ps.lastf != TERMFONT_BOLD) {
                    742:                        ps_pclose(p);
                    743:                        ps_setfont(p, TERMFONT_BOLD);
                    744:                }
1.8       kristaps  745:                p->engine.ps.last = c;
                    746:                return;
                    747:        } else {
1.9       kristaps  748:                if (p->engine.ps.lastf != TERMFONT_NONE) {
                    749:                        ps_pclose(p);
                    750:                        ps_setfont(p, TERMFONT_NONE);
                    751:                }
1.8       kristaps  752:                cc = p->engine.ps.last;
                    753:                p->engine.ps.last = c;
                    754:                c = cc;
                    755:        }
                    756:
1.9       kristaps  757:        ps_pletter(p, c);
1.8       kristaps  758: }
                    759:
                    760:
                    761: static void
1.2       kristaps  762: ps_advance(struct termp *p, size_t len)
                    763: {
                    764:
1.9       kristaps  765:        /*
                    766:         * Advance some spaces.  This can probably be made smarter,
                    767:         * i.e., to have multiple space-separated words in the same
                    768:         * scope, but this is easier:  just close out the current scope
                    769:         * and readjust our column settings.
                    770:         */
1.8       kristaps  771:
1.9       kristaps  772:        ps_fclose(p);
1.14      kristaps  773:        p->engine.ps.pscol += len;
1.2       kristaps  774: }
                    775:
                    776:
                    777: static void
                    778: ps_endline(struct termp *p)
                    779: {
1.8       kristaps  780:
1.9       kristaps  781:        /* Close out any scopes we have open: we're at eoln. */
                    782:
                    783:        ps_fclose(p);
1.2       kristaps  784:
1.9       kristaps  785:        /*
                    786:         * If we're in the margin, don't try to recalculate our current
                    787:         * row.  XXX: if the column tries to be fancy with multiple
                    788:         * lines, we'll do nasty stuff.
                    789:         */
1.2       kristaps  790:
                    791:        if (PS_MARGINS & p->engine.ps.psstate)
                    792:                return;
                    793:
1.9       kristaps  794:        /*
                    795:         * Put us down a line.  If we're at the page bottom, spit out a
                    796:         * showpage and restart our row.
                    797:         */
                    798:
1.16      kristaps  799:        p->engine.ps.pscol = p->engine.ps.left;
                    800:        if (p->engine.ps.psrow >= p->engine.ps.lineheight +
                    801:                        p->engine.ps.bottom) {
                    802:                p->engine.ps.psrow -= p->engine.ps.lineheight;
1.2       kristaps  803:                return;
                    804:        }
                    805:
1.4       kristaps  806:        assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
                    807:        printf("%s", p->engine.ps.psmarg);
1.23    ! kristaps  808:        printf("showpage\n");
        !           809:        p->engine.ps.pages++;
1.16      kristaps  810:        printf("%%%%Page: %zu %zu\n",
                    811:                        p->engine.ps.pages + 1,
                    812:                        p->engine.ps.pages + 1);
                    813:        p->engine.ps.psrow = p->engine.ps.top;
1.1       kristaps  814: }
1.9       kristaps  815:
                    816:
                    817: static void
                    818: ps_setfont(struct termp *p, enum termfont f)
                    819: {
                    820:
1.16      kristaps  821:        assert(f < TERMFONT__MAX);
1.21      kristaps  822:        ps_printf(p, "/%s %zu selectfont\n",
                    823:                        fonts[(int)f].name, p->engine.ps.scale);
1.9       kristaps  824:        p->engine.ps.lastf = f;
                    825: }
                    826:
1.11      kristaps  827:
1.12      kristaps  828: /* ARGSUSED */
1.11      kristaps  829: static size_t
                    830: ps_width(const struct termp *p, char c)
                    831: {
                    832:
1.14      kristaps  833:        if (c <= 32 || c - 32 >= MAXCHAR)
1.18      kristaps  834:                return(fonts[(int)TERMFONT_NONE].gly[0].wx);
1.14      kristaps  835:
                    836:        c -= 32;
1.18      kristaps  837:        return(fonts[(int)TERMFONT_NONE].gly[(int)c].wx);
1.11      kristaps  838: }
1.19      kristaps  839:
                    840:
1.20      kristaps  841: static double
1.19      kristaps  842: ps_hspan(const struct termp *p, const struct roffsu *su)
                    843: {
                    844:        double           r;
                    845:
                    846:        /*
                    847:         * All of these measurements are derived by converting from the
1.21      kristaps  848:         * native measurement to AFM units.
1.19      kristaps  849:         */
                    850:
                    851:        switch (su->unit) {
                    852:        case (SCALE_CM):
1.21      kristaps  853:                r = PNT2AFM(p, su->scale * 28.34);
1.19      kristaps  854:                break;
                    855:        case (SCALE_IN):
1.21      kristaps  856:                r = PNT2AFM(p, su->scale * 72);
1.19      kristaps  857:                break;
                    858:        case (SCALE_PC):
1.21      kristaps  859:                r = PNT2AFM(p, su->scale * 12);
1.19      kristaps  860:                break;
                    861:        case (SCALE_PT):
1.21      kristaps  862:                r = PNT2AFM(p, su->scale * 100);
1.19      kristaps  863:                break;
                    864:        case (SCALE_EM):
                    865:                r = su->scale *
                    866:                        fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
                    867:                break;
                    868:        case (SCALE_MM):
1.21      kristaps  869:                r = PNT2AFM(p, su->scale * 2.834);
1.19      kristaps  870:                break;
                    871:        case (SCALE_EN):
                    872:                r = su->scale *
                    873:                        fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
                    874:                break;
                    875:        case (SCALE_VS):
                    876:                r = su->scale * p->engine.ps.lineheight;
                    877:                break;
                    878:        default:
                    879:                r = su->scale;
                    880:                break;
                    881:        }
                    882:
1.20      kristaps  883:        return(r);
1.19      kristaps  884: }
                    885:

CVSweb