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

Annotation of mandoc/term_ps.c, Revision 1.30

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

CVSweb