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

Annotation of mandoc/term_ps.c, Revision 1.26

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

CVSweb