[BACK]Return to term_ps.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/term_ps.c, Revision 1.44

1.44    ! mmcc        1: /*     $OpenBSD: term_ps.c,v 1.43 2015/10/13 22:57:49 schwarze Exp $ */
1.1       schwarze    2: /*
1.17      schwarze    3:  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.37      schwarze    4:  * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.38      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       schwarze   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.38      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       schwarze   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.11      deraadt    18: #include <sys/types.h>
1.1       schwarze   19:
                     20: #include <assert.h>
1.41      schwarze   21: #include <err.h>
1.1       schwarze   22: #include <stdarg.h>
1.5       schwarze   23: #include <stdint.h>
1.1       schwarze   24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
1.6       schwarze   27: #include <unistd.h>
1.1       schwarze   28:
1.21      schwarze   29: #include "mandoc_aux.h"
1.1       schwarze   30: #include "out.h"
1.35      schwarze   31: #include "term.h"
1.38      schwarze   32: #include "manconf.h"
1.1       schwarze   33: #include "main.h"
                     34:
1.17      schwarze   35: /* These work the buffer used by the header and footer. */
                     36: #define        PS_BUFSLOP        128
                     37:
1.6       schwarze   38: /* Convert PostScript point "x" to an AFM unit. */
1.24      schwarze   39: #define        PNT2AFM(p, x) \
1.17      schwarze   40:        (size_t)((double)(x) * (1000.0 / (double)(p)->ps->scale))
1.6       schwarze   41:
                     42: /* Convert an AFM unit "x" to a PostScript points */
1.24      schwarze   43: #define        AFM2PNT(p, x) \
1.17      schwarze   44:        ((double)(x) / (1000.0 / (double)(p)->ps->scale))
1.6       schwarze   45:
1.4       schwarze   46: struct glyph {
1.7       schwarze   47:        unsigned short    wx; /* WX in AFM */
1.4       schwarze   48: };
                     49:
                     50: struct font {
                     51:        const char       *name; /* FontName in AFM */
                     52: #define        MAXCHAR           95 /* total characters we can handle */
                     53:        struct glyph      gly[MAXCHAR]; /* glyph metrics */
                     54: };
1.1       schwarze   55:
1.17      schwarze   56: struct termp_ps {
                     57:        int               flags;
                     58: #define        PS_INLINE        (1 << 0)       /* we're in a word */
                     59: #define        PS_MARGINS       (1 << 1)       /* we're in the margins */
                     60: #define        PS_NEWPAGE       (1 << 2)       /* new page, no words yet */
1.32      schwarze   61: #define        PS_BACKSP        (1 << 3)       /* last character was backspace */
1.17      schwarze   62:        size_t            pscol;        /* visible column (AFM units) */
1.37      schwarze   63:        size_t            pscolnext;    /* used for overstrike */
1.17      schwarze   64:        size_t            psrow;        /* visible row (AFM units) */
                     65:        char             *psmarg;       /* margin buf */
                     66:        size_t            psmargsz;     /* margin buf size */
                     67:        size_t            psmargcur;    /* cur index in margin buf */
1.32      schwarze   68:        char              last;         /* last non-backspace seen */
1.17      schwarze   69:        enum termfont     lastf;        /* last set font */
1.29      schwarze   70:        enum termfont     nextf;        /* building next font here */
1.17      schwarze   71:        size_t            scale;        /* font scaling factor */
                     72:        size_t            pages;        /* number of pages shown */
                     73:        size_t            lineheight;   /* line height (AFM units) */
                     74:        size_t            top;          /* body top (AFM units) */
                     75:        size_t            bottom;       /* body bottom (AFM units) */
                     76:        size_t            height;       /* page height (AFM units */
                     77:        size_t            width;        /* page width (AFM units) */
1.22      schwarze   78:        size_t            lastwidth;    /* page width before last ll */
1.17      schwarze   79:        size_t            left;         /* body left (AFM units) */
                     80:        size_t            header;       /* header pos (AFM units) */
                     81:        size_t            footer;       /* footer pos (AFM units) */
1.24      schwarze   82:        size_t            pdfbytes;     /* current output byte */
1.17      schwarze   83:        size_t            pdflastpg;    /* byte of last page mark */
                     84:        size_t            pdfbody;      /* start of body object */
                     85:        size_t           *pdfobjs;      /* table of object offsets */
                     86:        size_t            pdfobjsz;     /* size of pdfobjs */
                     87: };
                     88:
1.39      schwarze   89: static int               ps_hspan(const struct termp *,
1.17      schwarze   90:                                const struct roffsu *);
                     91: static size_t            ps_width(const struct termp *, int);
                     92: static void              ps_advance(struct termp *, size_t);
                     93: static void              ps_begin(struct termp *);
                     94: static void              ps_closepage(struct termp *);
                     95: static void              ps_end(struct termp *);
                     96: static void              ps_endline(struct termp *);
                     97: static void              ps_fclose(struct termp *);
                     98: static void              ps_growbuf(struct termp *, size_t);
                     99: static void              ps_letter(struct termp *, int);
                    100: static void              ps_pclose(struct termp *);
                    101: static void              ps_pletter(struct termp *, int);
                    102: static void              ps_printf(struct termp *, const char *, ...);
                    103: static void              ps_putchar(struct termp *, char);
                    104: static void              ps_setfont(struct termp *, enum termfont);
1.39      schwarze  105: static void              ps_setwidth(struct termp *, int, int);
1.43      schwarze  106: static struct termp     *pspdf_alloc(const struct manoutput *);
1.17      schwarze  107: static void              pdf_obj(struct termp *, size_t);
                    108:
1.4       schwarze  109: /*
                    110:  * We define, for the time being, three fonts: bold, oblique/italic, and
                    111:  * normal (roman).  The following table hard-codes the font metrics for
                    112:  * ASCII, i.e., 32--127.
                    113:  */
                    114:
                    115: static const struct font fonts[TERMFONT__MAX] = {
1.6       schwarze  116:        { "Times-Roman", {
                    117:                { 250 },
                    118:                { 333 },
                    119:                { 408 },
                    120:                { 500 },
                    121:                { 500 },
                    122:                { 833 },
                    123:                { 778 },
                    124:                { 333 },
                    125:                { 333 },
                    126:                { 333 },
                    127:                { 500 },
                    128:                { 564 },
                    129:                { 250 },
                    130:                { 333 },
                    131:                { 250 },
                    132:                { 278 },
                    133:                { 500 },
                    134:                { 500 },
                    135:                { 500 },
                    136:                { 500 },
                    137:                { 500 },
                    138:                { 500 },
                    139:                { 500 },
                    140:                { 500 },
                    141:                { 500 },
                    142:                { 500 },
                    143:                { 278 },
                    144:                { 278 },
                    145:                { 564 },
                    146:                { 564 },
                    147:                { 564 },
                    148:                { 444 },
                    149:                { 921 },
                    150:                { 722 },
                    151:                { 667 },
                    152:                { 667 },
                    153:                { 722 },
                    154:                { 611 },
                    155:                { 556 },
                    156:                { 722 },
                    157:                { 722 },
                    158:                { 333 },
                    159:                { 389 },
                    160:                { 722 },
                    161:                { 611 },
                    162:                { 889 },
                    163:                { 722 },
                    164:                { 722 },
                    165:                { 556 },
                    166:                { 722 },
                    167:                { 667 },
                    168:                { 556 },
                    169:                { 611 },
                    170:                { 722 },
                    171:                { 722 },
                    172:                { 944 },
                    173:                { 722 },
                    174:                { 722 },
                    175:                { 611 },
                    176:                { 333 },
                    177:                { 278 },
                    178:                { 333 },
                    179:                { 469 },
                    180:                { 500 },
                    181:                { 333 },
                    182:                { 444 },
                    183:                { 500 },
                    184:                { 444 },
                    185:                {  500},
                    186:                {  444},
                    187:                {  333},
                    188:                {  500},
                    189:                {  500},
                    190:                {  278},
                    191:                {  278},
                    192:                {  500},
                    193:                {  278},
                    194:                {  778},
                    195:                {  500},
                    196:                {  500},
                    197:                {  500},
                    198:                {  500},
                    199:                {  333},
                    200:                {  389},
                    201:                {  278},
                    202:                {  500},
                    203:                {  500},
                    204:                {  722},
                    205:                {  500},
                    206:                {  500},
                    207:                {  444},
                    208:                {  480},
                    209:                {  200},
                    210:                {  480},
                    211:                {  541},
1.4       schwarze  212:        } },
1.6       schwarze  213:        { "Times-Bold", {
                    214:                { 250  },
                    215:                { 333  },
                    216:                { 555  },
                    217:                { 500  },
                    218:                { 500  },
                    219:                { 1000 },
                    220:                { 833  },
                    221:                { 333  },
                    222:                { 333  },
                    223:                { 333  },
                    224:                { 500  },
                    225:                { 570  },
                    226:                { 250  },
                    227:                { 333  },
                    228:                { 250  },
                    229:                { 278  },
                    230:                { 500  },
                    231:                { 500  },
                    232:                { 500  },
                    233:                { 500  },
                    234:                { 500  },
                    235:                { 500  },
                    236:                { 500  },
                    237:                { 500  },
                    238:                { 500  },
                    239:                { 500  },
                    240:                { 333  },
                    241:                { 333  },
                    242:                { 570  },
                    243:                { 570  },
                    244:                { 570  },
                    245:                { 500  },
                    246:                { 930  },
                    247:                { 722  },
                    248:                { 667  },
                    249:                { 722  },
                    250:                { 722  },
                    251:                { 667  },
                    252:                { 611  },
                    253:                { 778  },
                    254:                { 778  },
                    255:                { 389  },
                    256:                { 500  },
                    257:                { 778  },
                    258:                { 667  },
                    259:                { 944  },
                    260:                { 722  },
                    261:                { 778  },
                    262:                { 611  },
                    263:                { 778  },
                    264:                { 722  },
                    265:                { 556  },
                    266:                { 667  },
                    267:                { 722  },
                    268:                { 722  },
                    269:                { 1000 },
                    270:                { 722  },
                    271:                { 722  },
                    272:                { 667  },
                    273:                { 333  },
                    274:                { 278  },
                    275:                { 333  },
                    276:                { 581  },
                    277:                { 500  },
                    278:                { 333  },
                    279:                { 500  },
                    280:                { 556  },
                    281:                { 444  },
                    282:                {  556 },
                    283:                {  444 },
                    284:                {  333 },
                    285:                {  500 },
                    286:                {  556 },
                    287:                {  278 },
                    288:                {  333 },
                    289:                {  556 },
                    290:                {  278 },
                    291:                {  833 },
                    292:                {  556 },
                    293:                {  500 },
                    294:                {  556 },
                    295:                {  556 },
                    296:                {  444 },
                    297:                {  389 },
                    298:                {  333 },
                    299:                {  556 },
                    300:                {  500 },
                    301:                {  722 },
                    302:                {  500 },
                    303:                {  500 },
                    304:                {  444 },
                    305:                {  394 },
                    306:                {  220 },
                    307:                {  394 },
                    308:                {  520 },
1.4       schwarze  309:        } },
1.6       schwarze  310:        { "Times-Italic", {
                    311:                { 250  },
                    312:                { 333  },
                    313:                { 420  },
                    314:                { 500  },
                    315:                { 500  },
                    316:                { 833  },
                    317:                { 778  },
                    318:                { 333  },
                    319:                { 333  },
                    320:                { 333  },
                    321:                { 500  },
                    322:                { 675  },
                    323:                { 250  },
                    324:                { 333  },
                    325:                { 250  },
                    326:                { 278  },
                    327:                { 500  },
                    328:                { 500  },
                    329:                { 500  },
                    330:                { 500  },
                    331:                { 500  },
                    332:                { 500  },
                    333:                { 500  },
                    334:                { 500  },
                    335:                { 500  },
                    336:                { 500  },
                    337:                { 333  },
                    338:                { 333  },
                    339:                { 675  },
                    340:                { 675  },
                    341:                { 675  },
                    342:                { 500  },
                    343:                { 920  },
                    344:                { 611  },
                    345:                { 611  },
                    346:                { 667  },
                    347:                { 722  },
                    348:                { 611  },
                    349:                { 611  },
                    350:                { 722  },
                    351:                { 722  },
                    352:                { 333  },
                    353:                { 444  },
                    354:                { 667  },
                    355:                { 556  },
                    356:                { 833  },
                    357:                { 667  },
                    358:                { 722  },
                    359:                { 611  },
                    360:                { 722  },
                    361:                { 611  },
                    362:                { 500  },
                    363:                { 556  },
                    364:                { 722  },
                    365:                { 611  },
                    366:                { 833  },
                    367:                { 611  },
                    368:                { 556  },
                    369:                { 556  },
                    370:                { 389  },
                    371:                { 278  },
                    372:                { 389  },
                    373:                { 422  },
                    374:                { 500  },
                    375:                { 333  },
                    376:                { 500  },
                    377:                { 500  },
                    378:                { 444  },
                    379:                {  500 },
                    380:                {  444 },
                    381:                {  278 },
                    382:                {  500 },
                    383:                {  500 },
                    384:                {  278 },
                    385:                {  278 },
                    386:                {  444 },
                    387:                {  278 },
                    388:                {  722 },
                    389:                {  500 },
                    390:                {  500 },
                    391:                {  500 },
                    392:                {  500 },
                    393:                {  389 },
                    394:                {  389 },
                    395:                {  278 },
                    396:                {  500 },
                    397:                {  444 },
                    398:                {  667 },
                    399:                {  444 },
                    400:                {  444 },
                    401:                {  389 },
                    402:                {  400 },
                    403:                {  275 },
                    404:                {  400 },
                    405:                {  541 },
1.4       schwarze  406:        } },
1.29      schwarze  407:        { "Times-BoldItalic", {
                    408:                {  250 },
                    409:                {  389 },
                    410:                {  555 },
                    411:                {  500 },
                    412:                {  500 },
                    413:                {  833 },
                    414:                {  778 },
                    415:                {  333 },
                    416:                {  333 },
                    417:                {  333 },
                    418:                {  500 },
                    419:                {  570 },
                    420:                {  250 },
                    421:                {  333 },
                    422:                {  250 },
                    423:                {  278 },
                    424:                {  500 },
                    425:                {  500 },
                    426:                {  500 },
                    427:                {  500 },
                    428:                {  500 },
                    429:                {  500 },
                    430:                {  500 },
                    431:                {  500 },
                    432:                {  500 },
                    433:                {  500 },
                    434:                {  333 },
                    435:                {  333 },
                    436:                {  570 },
                    437:                {  570 },
                    438:                {  570 },
                    439:                {  500 },
                    440:                {  832 },
                    441:                {  667 },
                    442:                {  667 },
                    443:                {  667 },
                    444:                {  722 },
                    445:                {  667 },
                    446:                {  667 },
                    447:                {  722 },
                    448:                {  778 },
                    449:                {  389 },
                    450:                {  500 },
                    451:                {  667 },
                    452:                {  611 },
                    453:                {  889 },
                    454:                {  722 },
                    455:                {  722 },
                    456:                {  611 },
                    457:                {  722 },
                    458:                {  667 },
                    459:                {  556 },
                    460:                {  611 },
                    461:                {  722 },
                    462:                {  667 },
                    463:                {  889 },
                    464:                {  667 },
                    465:                {  611 },
                    466:                {  611 },
                    467:                {  333 },
                    468:                {  278 },
                    469:                {  333 },
                    470:                {  570 },
                    471:                {  500 },
                    472:                {  333 },
                    473:                {  500 },
                    474:                {  500 },
                    475:                {  444 },
                    476:                {  500 },
                    477:                {  444 },
                    478:                {  333 },
                    479:                {  500 },
                    480:                {  556 },
                    481:                {  278 },
                    482:                {  278 },
                    483:                {  500 },
                    484:                {  278 },
                    485:                {  778 },
                    486:                {  556 },
                    487:                {  500 },
                    488:                {  500 },
                    489:                {  500 },
                    490:                {  389 },
                    491:                {  389 },
                    492:                {  278 },
                    493:                {  556 },
                    494:                {  444 },
                    495:                {  667 },
                    496:                {  500 },
                    497:                {  444 },
                    498:                {  389 },
                    499:                {  348 },
                    500:                {  220 },
                    501:                {  348 },
                    502:                {  570 },
                    503:        } },
1.4       schwarze  504: };
                    505:
1.7       schwarze  506: void *
1.43      schwarze  507: pdf_alloc(const struct manoutput *outopts)
1.7       schwarze  508: {
                    509:        struct termp    *p;
                    510:
1.43      schwarze  511:        if (NULL != (p = pspdf_alloc(outopts)))
1.8       schwarze  512:                p->type = TERMTYPE_PDF;
1.7       schwarze  513:
1.40      schwarze  514:        return p;
1.7       schwarze  515: }
1.1       schwarze  516:
                    517: void *
1.43      schwarze  518: ps_alloc(const struct manoutput *outopts)
1.1       schwarze  519: {
                    520:        struct termp    *p;
1.7       schwarze  521:
1.43      schwarze  522:        if (NULL != (p = pspdf_alloc(outopts)))
1.8       schwarze  523:                p->type = TERMTYPE_PS;
1.7       schwarze  524:
1.40      schwarze  525:        return p;
1.7       schwarze  526: }
                    527:
                    528: static struct termp *
1.43      schwarze  529: pspdf_alloc(const struct manoutput *outopts)
1.7       schwarze  530: {
                    531:        struct termp    *p;
1.18      schwarze  532:        unsigned int     pagex, pagey;
                    533:        size_t           marginx, marginy, lineheight;
1.6       schwarze  534:        const char      *pp;
1.1       schwarze  535:
1.17      schwarze  536:        p = mandoc_calloc(1, sizeof(struct termp));
                    537:        p->enc = TERMENC_ASCII;
1.36      schwarze  538:        p->fontq = mandoc_reallocarray(NULL,
                    539:            (p->fontsz = 8), sizeof(enum termfont));
                    540:        p->fontq[0] = p->fontl = TERMFONT_NONE;
1.17      schwarze  541:        p->ps = mandoc_calloc(1, sizeof(struct termp_ps));
1.1       schwarze  542:
1.6       schwarze  543:        p->advance = ps_advance;
1.1       schwarze  544:        p->begin = ps_begin;
                    545:        p->end = ps_end;
                    546:        p->endline = ps_endline;
1.6       schwarze  547:        p->hspan = ps_hspan;
                    548:        p->letter = ps_letter;
1.22      schwarze  549:        p->setwidth = ps_setwidth;
1.3       schwarze  550:        p->width = ps_width;
1.24      schwarze  551:
1.6       schwarze  552:        /* Default to US letter (millimetres). */
                    553:
                    554:        pagex = 216;
                    555:        pagey = 279;
                    556:
                    557:        /*
                    558:         * The ISO-269 paper sizes can be calculated automatically, but
                    559:         * it would require bringing in -lm for pow() and I'd rather not
                    560:         * do that.  So just do it the easy way for now.  Since this
                    561:         * only happens once, I'm not terribly concerned.
                    562:         */
                    563:
1.38      schwarze  564:        pp = outopts->paper;
1.6       schwarze  565:        if (pp && strcasecmp(pp, "letter")) {
                    566:                if (0 == strcasecmp(pp, "a3")) {
                    567:                        pagex = 297;
                    568:                        pagey = 420;
                    569:                } else if (0 == strcasecmp(pp, "a4")) {
                    570:                        pagex = 210;
                    571:                        pagey = 297;
                    572:                } else if (0 == strcasecmp(pp, "a5")) {
                    573:                        pagex = 148;
                    574:                        pagey = 210;
                    575:                } else if (0 == strcasecmp(pp, "legal")) {
                    576:                        pagex = 216;
                    577:                        pagey = 356;
1.18      schwarze  578:                } else if (2 != sscanf(pp, "%ux%u", &pagex, &pagey))
1.41      schwarze  579:                        warnx("%s: Unknown paper", pp);
1.19      schwarze  580:        }
1.6       schwarze  581:
1.24      schwarze  582:        /*
1.6       schwarze  583:         * This MUST be defined before any PNT2AFM or AFM2PNT
                    584:         * calculations occur.
                    585:         */
                    586:
1.17      schwarze  587:        p->ps->scale = 11;
1.6       schwarze  588:
                    589:        /* Remember millimetres -> AFM units. */
                    590:
                    591:        pagex = PNT2AFM(p, ((double)pagex * 2.834));
                    592:        pagey = PNT2AFM(p, ((double)pagey * 2.834));
                    593:
                    594:        /* Margins are 1/9 the page x and y. */
                    595:
1.24      schwarze  596:        marginx = (size_t)((double)pagex / 9.0);
                    597:        marginy = (size_t)((double)pagey / 9.0);
1.6       schwarze  598:
                    599:        /* Line-height is 1.4em. */
                    600:
1.17      schwarze  601:        lineheight = PNT2AFM(p, ((double)p->ps->scale * 1.4));
1.4       schwarze  602:
1.22      schwarze  603:        p->ps->width = p->ps->lastwidth = (size_t)pagex;
1.18      schwarze  604:        p->ps->height = (size_t)pagey;
1.17      schwarze  605:        p->ps->header = pagey - (marginy / 2) - (lineheight / 2);
                    606:        p->ps->top = pagey - marginy;
                    607:        p->ps->footer = (marginy / 2) - (lineheight / 2);
                    608:        p->ps->bottom = marginy;
                    609:        p->ps->left = marginx;
                    610:        p->ps->lineheight = lineheight;
1.4       schwarze  611:
1.6       schwarze  612:        p->defrmargin = pagex - (marginx * 2);
1.40      schwarze  613:        return p;
1.22      schwarze  614: }
                    615:
                    616: static void
1.39      schwarze  617: ps_setwidth(struct termp *p, int iop, int width)
1.22      schwarze  618: {
                    619:        size_t   lastwidth;
                    620:
                    621:        lastwidth = p->ps->width;
1.34      schwarze  622:        if (iop > 0)
1.23      schwarze  623:                p->ps->width += width;
1.34      schwarze  624:        else if (iop == 0)
1.39      schwarze  625:                p->ps->width = width ? (size_t)width : p->ps->lastwidth;
                    626:        else if (p->ps->width > (size_t)width)
1.23      schwarze  627:                p->ps->width -= width;
                    628:        else
1.34      schwarze  629:                p->ps->width = 0;
1.22      schwarze  630:        p->ps->lastwidth = lastwidth;
1.1       schwarze  631: }
                    632:
                    633: void
1.7       schwarze  634: pspdf_free(void *arg)
1.1       schwarze  635: {
                    636:        struct termp    *p;
                    637:
                    638:        p = (struct termp *)arg;
                    639:
1.44    ! mmcc      640:        free(p->ps->psmarg);
        !           641:        free(p->ps->pdfobjs);
1.1       schwarze  642:
1.17      schwarze  643:        free(p->ps);
1.1       schwarze  644:        term_free(p);
                    645: }
                    646:
                    647: static void
                    648: ps_printf(struct termp *p, const char *fmt, ...)
                    649: {
                    650:        va_list          ap;
1.7       schwarze  651:        int              pos, len;
1.1       schwarze  652:
                    653:        va_start(ap, fmt);
                    654:
                    655:        /*
                    656:         * If we're running in regular mode, then pipe directly into
                    657:         * vprintf().  If we're processing margins, then push the data
                    658:         * into our growable margin buffer.
                    659:         */
                    660:
1.17      schwarze  661:        if ( ! (PS_MARGINS & p->ps->flags)) {
1.7       schwarze  662:                len = vprintf(fmt, ap);
1.1       schwarze  663:                va_end(ap);
1.24      schwarze  664:                p->ps->pdfbytes += len < 0 ? 0 : (size_t)len;
1.1       schwarze  665:                return;
                    666:        }
                    667:
1.24      schwarze  668:        /*
1.1       schwarze  669:         * XXX: I assume that the in-margin print won't exceed
                    670:         * PS_BUFSLOP (128 bytes), which is reasonable but still an
                    671:         * assumption that will cause pukeage if it's not the case.
                    672:         */
                    673:
1.9       schwarze  674:        ps_growbuf(p, PS_BUFSLOP);
1.1       schwarze  675:
1.17      schwarze  676:        pos = (int)p->ps->psmargcur;
1.19      schwarze  677:        vsnprintf(&p->ps->psmarg[pos], PS_BUFSLOP, fmt, ap);
1.1       schwarze  678:
                    679:        va_end(ap);
1.7       schwarze  680:
1.17      schwarze  681:        p->ps->psmargcur = strlen(p->ps->psmarg);
1.1       schwarze  682: }
                    683:
                    684: static void
                    685: ps_putchar(struct termp *p, char c)
                    686: {
                    687:        int              pos;
                    688:
                    689:        /* See ps_printf(). */
                    690:
1.17      schwarze  691:        if ( ! (PS_MARGINS & p->ps->flags)) {
1.1       schwarze  692:                putchar(c);
1.17      schwarze  693:                p->ps->pdfbytes++;
1.1       schwarze  694:                return;
                    695:        }
                    696:
1.9       schwarze  697:        ps_growbuf(p, 2);
1.1       schwarze  698:
1.17      schwarze  699:        pos = (int)p->ps->psmargcur++;
                    700:        p->ps->psmarg[pos++] = c;
                    701:        p->ps->psmarg[pos] = '\0';
1.1       schwarze  702: }
                    703:
1.7       schwarze  704: static void
1.8       schwarze  705: pdf_obj(struct termp *p, size_t obj)
                    706: {
                    707:
                    708:        assert(obj > 0);
                    709:
1.17      schwarze  710:        if ((obj - 1) >= p->ps->pdfobjsz) {
                    711:                p->ps->pdfobjsz = obj + 128;
1.25      schwarze  712:                p->ps->pdfobjs = mandoc_reallocarray(p->ps->pdfobjs,
                    713:                    p->ps->pdfobjsz, sizeof(size_t));
1.8       schwarze  714:        }
                    715:
1.17      schwarze  716:        p->ps->pdfobjs[(int)obj - 1] = p->ps->pdfbytes;
1.8       schwarze  717:        ps_printf(p, "%zu 0 obj\n", obj);
                    718: }
                    719:
                    720: static void
1.7       schwarze  721: ps_closepage(struct termp *p)
                    722: {
                    723:        int              i;
1.8       schwarze  724:        size_t           len, base;
                    725:
                    726:        /*
                    727:         * Close out a page that we've already flushed to output.  In
                    728:         * PostScript, we simply note that the page must be showed.  In
                    729:         * PDF, we must now create the Length, Resource, and Page node
                    730:         * for the page contents.
                    731:         */
1.7       schwarze  732:
1.17      schwarze  733:        assert(p->ps->psmarg && p->ps->psmarg[0]);
                    734:        ps_printf(p, "%s", p->ps->psmarg);
1.7       schwarze  735:
1.8       schwarze  736:        if (TERMTYPE_PS != p->type) {
1.7       schwarze  737:                ps_printf(p, "ET\n");
1.8       schwarze  738:
1.17      schwarze  739:                len = p->ps->pdfbytes - p->ps->pdflastpg;
                    740:                base = p->ps->pages * 4 + p->ps->pdfbody;
1.8       schwarze  741:
                    742:                ps_printf(p, "endstream\nendobj\n");
                    743:
                    744:                /* Length of content. */
                    745:                pdf_obj(p, base + 1);
                    746:                ps_printf(p, "%zu\nendobj\n", len);
                    747:
                    748:                /* Resource for content. */
                    749:                pdf_obj(p, base + 2);
                    750:                ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
1.7       schwarze  751:                ps_printf(p, "/Font <<\n");
1.24      schwarze  752:                for (i = 0; i < (int)TERMFONT__MAX; i++)
1.7       schwarze  753:                        ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
1.8       schwarze  754:                ps_printf(p, ">>\n>>\n");
                    755:
                    756:                /* Page node. */
                    757:                pdf_obj(p, base + 3);
1.7       schwarze  758:                ps_printf(p, "<<\n");
                    759:                ps_printf(p, "/Type /Page\n");
                    760:                ps_printf(p, "/Parent 2 0 R\n");
1.8       schwarze  761:                ps_printf(p, "/Resources %zu 0 R\n", base + 2);
                    762:                ps_printf(p, "/Contents %zu 0 R\n", base);
                    763:                ps_printf(p, ">>\nendobj\n");
                    764:        } else
                    765:                ps_printf(p, "showpage\n");
1.7       schwarze  766:
1.17      schwarze  767:        p->ps->pages++;
                    768:        p->ps->psrow = p->ps->top;
                    769:        assert( ! (PS_NEWPAGE & p->ps->flags));
                    770:        p->ps->flags |= PS_NEWPAGE;
1.7       schwarze  771: }
                    772:
1.1       schwarze  773: static void
                    774: ps_end(struct termp *p)
                    775: {
1.8       schwarze  776:        size_t           i, xref, base;
1.1       schwarze  777:
                    778:        /*
                    779:         * At the end of the file, do one last showpage.  This is the
                    780:         * same behaviour as groff(1) and works for multiple pages as
                    781:         * well as just one.
                    782:         */
                    783:
1.17      schwarze  784:        if ( ! (PS_NEWPAGE & p->ps->flags)) {
                    785:                assert(0 == p->ps->flags);
                    786:                assert('\0' == p->ps->last);
1.7       schwarze  787:                ps_closepage(p);
1.6       schwarze  788:        }
1.4       schwarze  789:
1.7       schwarze  790:        if (TERMTYPE_PS == p->type) {
                    791:                ps_printf(p, "%%%%Trailer\n");
1.17      schwarze  792:                ps_printf(p, "%%%%Pages: %zu\n", p->ps->pages);
1.7       schwarze  793:                ps_printf(p, "%%%%EOF\n");
                    794:                return;
1.24      schwarze  795:        }
1.7       schwarze  796:
1.8       schwarze  797:        pdf_obj(p, 2);
                    798:        ps_printf(p, "<<\n/Type /Pages\n");
1.7       schwarze  799:        ps_printf(p, "/MediaBox [0 0 %zu %zu]\n",
1.17      schwarze  800:                        (size_t)AFM2PNT(p, p->ps->width),
                    801:                        (size_t)AFM2PNT(p, p->ps->height));
1.7       schwarze  802:
1.17      schwarze  803:        ps_printf(p, "/Count %zu\n", p->ps->pages);
1.7       schwarze  804:        ps_printf(p, "/Kids [");
                    805:
1.17      schwarze  806:        for (i = 0; i < p->ps->pages; i++)
1.24      schwarze  807:                ps_printf(p, " %zu 0 R", i * 4 + p->ps->pdfbody + 3);
1.8       schwarze  808:
1.24      schwarze  809:        base = (p->ps->pages - 1) * 4 + p->ps->pdfbody + 4;
1.8       schwarze  810:
                    811:        ps_printf(p, "]\n>>\nendobj\n");
                    812:        pdf_obj(p, base);
1.7       schwarze  813:        ps_printf(p, "<<\n");
                    814:        ps_printf(p, "/Type /Catalog\n");
                    815:        ps_printf(p, "/Pages 2 0 R\n");
                    816:        ps_printf(p, ">>\n");
1.17      schwarze  817:        xref = p->ps->pdfbytes;
1.7       schwarze  818:        ps_printf(p, "xref\n");
1.8       schwarze  819:        ps_printf(p, "0 %zu\n", base + 1);
                    820:        ps_printf(p, "0000000000 65535 f \n");
                    821:
                    822:        for (i = 0; i < base; i++)
1.24      schwarze  823:                ps_printf(p, "%.10zu 00000 n \n",
                    824:                    p->ps->pdfobjs[(int)i]);
1.8       schwarze  825:
1.7       schwarze  826:        ps_printf(p, "trailer\n");
                    827:        ps_printf(p, "<<\n");
1.8       schwarze  828:        ps_printf(p, "/Size %zu\n", base + 1);
                    829:        ps_printf(p, "/Root %zu 0 R\n", base);
1.7       schwarze  830:        ps_printf(p, "/Info 1 0 R\n");
                    831:        ps_printf(p, ">>\n");
                    832:        ps_printf(p, "startxref\n");
                    833:        ps_printf(p, "%zu\n", xref);
                    834:        ps_printf(p, "%%%%EOF\n");
1.1       schwarze  835: }
                    836:
                    837: static void
                    838: ps_begin(struct termp *p)
                    839: {
1.4       schwarze  840:        int              i;
1.1       schwarze  841:
1.24      schwarze  842:        /*
1.2       schwarze  843:         * Print margins into margin buffer.  Nothing gets output to the
                    844:         * screen yet, so we don't need to initialise the primary state.
1.1       schwarze  845:         */
                    846:
1.17      schwarze  847:        if (p->ps->psmarg) {
                    848:                assert(p->ps->psmargsz);
                    849:                p->ps->psmarg[0] = '\0';
1.1       schwarze  850:        }
                    851:
1.17      schwarze  852:        /*p->ps->pdfbytes = 0;*/
                    853:        p->ps->psmargcur = 0;
                    854:        p->ps->flags = PS_MARGINS;
                    855:        p->ps->pscol = p->ps->left;
                    856:        p->ps->psrow = p->ps->header;
1.1       schwarze  857:
1.2       schwarze  858:        ps_setfont(p, TERMFONT_NONE);
1.1       schwarze  859:
                    860:        (*p->headf)(p, p->argf);
                    861:        (*p->endline)(p);
                    862:
1.17      schwarze  863:        p->ps->pscol = p->ps->left;
                    864:        p->ps->psrow = p->ps->footer;
1.1       schwarze  865:
                    866:        (*p->footf)(p, p->argf);
                    867:        (*p->endline)(p);
                    868:
1.17      schwarze  869:        p->ps->flags &= ~PS_MARGINS;
1.2       schwarze  870:
1.17      schwarze  871:        assert(0 == p->ps->flags);
                    872:        assert(p->ps->psmarg);
                    873:        assert('\0' != p->ps->psmarg[0]);
1.1       schwarze  874:
1.24      schwarze  875:        /*
1.2       schwarze  876:         * Print header and initialise page state.  Following this,
                    877:         * stuff gets printed to the screen, so make sure we're sane.
                    878:         */
                    879:
1.7       schwarze  880:        if (TERMTYPE_PS == p->type) {
                    881:                ps_printf(p, "%%!PS-Adobe-3.0\n");
                    882:                ps_printf(p, "%%%%DocumentData: Clean7Bit\n");
                    883:                ps_printf(p, "%%%%Orientation: Portrait\n");
                    884:                ps_printf(p, "%%%%Pages: (atend)\n");
                    885:                ps_printf(p, "%%%%PageOrder: Ascend\n");
                    886:                ps_printf(p, "%%%%DocumentMedia: "
1.24      schwarze  887:                    "Default %zu %zu 0 () ()\n",
                    888:                    (size_t)AFM2PNT(p, p->ps->width),
                    889:                    (size_t)AFM2PNT(p, p->ps->height));
1.7       schwarze  890:                ps_printf(p, "%%%%DocumentNeededResources: font");
1.4       schwarze  891:
1.7       schwarze  892:                for (i = 0; i < (int)TERMFONT__MAX; i++)
                    893:                        ps_printf(p, " %s", fonts[i].name);
                    894:
                    895:                ps_printf(p, "\n%%%%EndComments\n");
                    896:        } else {
                    897:                ps_printf(p, "%%PDF-1.1\n");
1.8       schwarze  898:                pdf_obj(p, 1);
1.7       schwarze  899:                ps_printf(p, "<<\n");
                    900:                ps_printf(p, ">>\n");
                    901:                ps_printf(p, "endobj\n");
                    902:
                    903:                for (i = 0; i < (int)TERMFONT__MAX; i++) {
1.8       schwarze  904:                        pdf_obj(p, (size_t)i + 3);
1.7       schwarze  905:                        ps_printf(p, "<<\n");
                    906:                        ps_printf(p, "/Type /Font\n");
                    907:                        ps_printf(p, "/Subtype /Type1\n");
1.20      schwarze  908:                        ps_printf(p, "/Name /F%d\n", i);
1.7       schwarze  909:                        ps_printf(p, "/BaseFont /%s\n", fonts[i].name);
                    910:                        ps_printf(p, ">>\n");
                    911:                }
                    912:        }
                    913:
1.17      schwarze  914:        p->ps->pdfbody = (size_t)TERMFONT__MAX + 3;
                    915:        p->ps->pscol = p->ps->left;
                    916:        p->ps->psrow = p->ps->top;
                    917:        p->ps->flags |= PS_NEWPAGE;
1.6       schwarze  918:        ps_setfont(p, TERMFONT_NONE);
1.1       schwarze  919: }
                    920:
                    921: static void
1.4       schwarze  922: ps_pletter(struct termp *p, int c)
1.1       schwarze  923: {
1.4       schwarze  924:        int              f;
1.6       schwarze  925:
                    926:        /*
                    927:         * If we haven't opened a page context, then output that we're
                    928:         * in a new page and make sure the font is correctly set.
                    929:         */
                    930:
1.17      schwarze  931:        if (PS_NEWPAGE & p->ps->flags) {
1.7       schwarze  932:                if (TERMTYPE_PS == p->type) {
1.24      schwarze  933:                        ps_printf(p, "%%%%Page: %zu %zu\n",
                    934:                            p->ps->pages + 1, p->ps->pages + 1);
                    935:                        ps_printf(p, "/%s %zu selectfont\n",
                    936:                            fonts[(int)p->ps->lastf].name,
                    937:                            p->ps->scale);
1.7       schwarze  938:                } else {
1.24      schwarze  939:                        pdf_obj(p, p->ps->pdfbody +
                    940:                            p->ps->pages * 4);
1.7       schwarze  941:                        ps_printf(p, "<<\n");
1.24      schwarze  942:                        ps_printf(p, "/Length %zu 0 R\n",
                    943:                            p->ps->pdfbody + 1 + p->ps->pages * 4);
1.7       schwarze  944:                        ps_printf(p, ">>\nstream\n");
                    945:                }
1.17      schwarze  946:                p->ps->pdflastpg = p->ps->pdfbytes;
                    947:                p->ps->flags &= ~PS_NEWPAGE;
1.6       schwarze  948:        }
1.24      schwarze  949:
1.2       schwarze  950:        /*
                    951:         * If we're not in a PostScript "word" context, then open one
                    952:         * now at the current cursor.
                    953:         */
                    954:
1.17      schwarze  955:        if ( ! (PS_INLINE & p->ps->flags)) {
1.7       schwarze  956:                if (TERMTYPE_PS != p->type) {
1.24      schwarze  957:                        ps_printf(p, "BT\n/F%d %zu Tf\n",
                    958:                            (int)p->ps->lastf, p->ps->scale);
1.7       schwarze  959:                        ps_printf(p, "%.3f %.3f Td\n(",
1.24      schwarze  960:                            AFM2PNT(p, p->ps->pscol),
                    961:                            AFM2PNT(p, p->ps->psrow));
1.7       schwarze  962:                } else
1.24      schwarze  963:                        ps_printf(p, "%.3f %.3f moveto\n(",
                    964:                            AFM2PNT(p, p->ps->pscol),
                    965:                            AFM2PNT(p, p->ps->psrow));
1.17      schwarze  966:                p->ps->flags |= PS_INLINE;
1.1       schwarze  967:        }
                    968:
1.17      schwarze  969:        assert( ! (PS_NEWPAGE & p->ps->flags));
1.6       schwarze  970:
1.1       schwarze  971:        /*
                    972:         * We need to escape these characters as per the PostScript
                    973:         * specification.  We would also escape non-graphable characters
                    974:         * (like tabs), but none of them would get to this point and
                    975:         * it's superfluous to abort() on them.
                    976:         */
                    977:
                    978:        switch (c) {
1.24      schwarze  979:        case '(':
                    980:        case ')':
                    981:        case '\\':
1.1       schwarze  982:                ps_putchar(p, '\\');
                    983:                break;
                    984:        default:
                    985:                break;
                    986:        }
                    987:
                    988:        /* Write the character and adjust where we are on the page. */
1.2       schwarze  989:
1.17      schwarze  990:        f = (int)p->ps->lastf;
1.4       schwarze  991:
1.26      schwarze  992:        if (c <= 32 || c - 32 >= MAXCHAR)
                    993:                c = 32;
1.4       schwarze  994:
1.6       schwarze  995:        ps_putchar(p, (char)c);
1.4       schwarze  996:        c -= 32;
1.17      schwarze  997:        p->ps->pscol += (size_t)fonts[f].gly[c].wx;
1.1       schwarze  998: }
                    999:
                   1000: static void
1.2       schwarze 1001: ps_pclose(struct termp *p)
                   1002: {
                   1003:
1.24      schwarze 1004:        /*
1.2       schwarze 1005:         * Spit out that we're exiting a word context (this is a
                   1006:         * "partial close" because we don't check the last-char buffer
                   1007:         * or anything).
                   1008:         */
                   1009:
1.17      schwarze 1010:        if ( ! (PS_INLINE & p->ps->flags))
1.2       schwarze 1011:                return;
1.24      schwarze 1012:
1.7       schwarze 1013:        if (TERMTYPE_PS != p->type) {
                   1014:                ps_printf(p, ") Tj\nET\n");
                   1015:        } else
                   1016:                ps_printf(p, ") show\n");
                   1017:
1.17      schwarze 1018:        p->ps->flags &= ~PS_INLINE;
1.2       schwarze 1019: }
                   1020:
                   1021: static void
                   1022: ps_fclose(struct termp *p)
                   1023: {
                   1024:
                   1025:        /*
                   1026:         * Strong closure: if we have a last-char, spit it out after
                   1027:         * checking that we're in the right font mode.  This will of
                   1028:         * course open a new scope, if applicable.
                   1029:         *
                   1030:         * Following this, close out any scope that's open.
                   1031:         */
                   1032:
1.29      schwarze 1033:        if (p->ps->last != '\0') {
1.32      schwarze 1034:                assert( ! (p->ps->flags & PS_BACKSP));
1.29      schwarze 1035:                if (p->ps->nextf != p->ps->lastf) {
1.2       schwarze 1036:                        ps_pclose(p);
1.29      schwarze 1037:                        ps_setfont(p, p->ps->nextf);
1.2       schwarze 1038:                }
1.29      schwarze 1039:                p->ps->nextf = TERMFONT_NONE;
1.17      schwarze 1040:                ps_pletter(p, p->ps->last);
                   1041:                p->ps->last = '\0';
1.2       schwarze 1042:        }
                   1043:
1.17      schwarze 1044:        if ( ! (PS_INLINE & p->ps->flags))
1.2       schwarze 1045:                return;
                   1046:
                   1047:        ps_pclose(p);
                   1048: }
                   1049:
                   1050: static void
1.17      schwarze 1051: ps_letter(struct termp *p, int arg)
1.2       schwarze 1052: {
1.37      schwarze 1053:        size_t          savecol, wx;
1.30      schwarze 1054:        char            c;
1.17      schwarze 1055:
                   1056:        c = arg >= 128 || arg <= 0 ? '?' : arg;
1.2       schwarze 1057:
                   1058:        /*
1.32      schwarze 1059:         * When receiving a backspace, merely flag it.
                   1060:         * We don't know yet whether it is
                   1061:         * a font instruction or an overstrike.
1.2       schwarze 1062:         */
                   1063:
1.32      schwarze 1064:        if (c == '\b') {
1.29      schwarze 1065:                assert(p->ps->last != '\0');
1.32      schwarze 1066:                assert( ! (p->ps->flags & PS_BACKSP));
                   1067:                p->ps->flags |= PS_BACKSP;
                   1068:                return;
                   1069:        }
                   1070:
                   1071:        /*
                   1072:         * Decode font instructions.
                   1073:         */
                   1074:
                   1075:        if (p->ps->flags & PS_BACKSP) {
                   1076:                if (p->ps->last == '_') {
1.29      schwarze 1077:                        switch (p->ps->nextf) {
                   1078:                        case TERMFONT_BI:
                   1079:                                break;
                   1080:                        case TERMFONT_BOLD:
                   1081:                                p->ps->nextf = TERMFONT_BI;
                   1082:                                break;
                   1083:                        default:
                   1084:                                p->ps->nextf = TERMFONT_UNDER;
                   1085:                        }
1.32      schwarze 1086:                        p->ps->last = c;
                   1087:                        p->ps->flags &= ~PS_BACKSP;
                   1088:                        return;
                   1089:                }
                   1090:                if (p->ps->last == c) {
1.29      schwarze 1091:                        switch (p->ps->nextf) {
                   1092:                        case TERMFONT_BI:
                   1093:                                break;
                   1094:                        case TERMFONT_UNDER:
                   1095:                                p->ps->nextf = TERMFONT_BI;
                   1096:                                break;
                   1097:                        default:
                   1098:                                p->ps->nextf = TERMFONT_BOLD;
1.2       schwarze 1099:                        }
1.32      schwarze 1100:                        p->ps->flags &= ~PS_BACKSP;
                   1101:                        return;
1.2       schwarze 1102:                }
1.32      schwarze 1103:
                   1104:                /*
                   1105:                 * This is not a font instruction, but rather
                   1106:                 * the next character.  Prepare for overstrike.
                   1107:                 */
                   1108:
                   1109:                savecol = p->ps->pscol;
                   1110:        } else
                   1111:                savecol = SIZE_MAX;
                   1112:
                   1113:        /*
                   1114:         * We found the next character, so the font instructions
                   1115:         * for the previous one are complete.
                   1116:         * Use them and print it.
                   1117:         */
                   1118:
                   1119:        if (p->ps->last != '\0') {
1.29      schwarze 1120:                if (p->ps->nextf != p->ps->lastf) {
1.2       schwarze 1121:                        ps_pclose(p);
1.29      schwarze 1122:                        ps_setfont(p, p->ps->nextf);
1.2       schwarze 1123:                }
1.29      schwarze 1124:                p->ps->nextf = TERMFONT_NONE;
1.37      schwarze 1125:
                   1126:                /*
                   1127:                 * For an overstrike, if a previous character
                   1128:                 * was wider, advance to center the new one.
                   1129:                 */
                   1130:
                   1131:                if (p->ps->pscolnext) {
                   1132:                        wx = fonts[p->ps->lastf].gly[(int)p->ps->last-32].wx;
                   1133:                        if (p->ps->pscol + wx < p->ps->pscolnext)
                   1134:                                p->ps->pscol = (p->ps->pscol +
                   1135:                                    p->ps->pscolnext - wx) / 2;
                   1136:                }
                   1137:
1.29      schwarze 1138:                ps_pletter(p, p->ps->last);
1.37      schwarze 1139:
                   1140:                /*
                   1141:                 * For an overstrike, if a previous character
                   1142:                 * was wider, advance to the end of the old one.
                   1143:                 */
                   1144:
                   1145:                if (p->ps->pscol < p->ps->pscolnext) {
                   1146:                        ps_pclose(p);
                   1147:                        p->ps->pscol = p->ps->pscolnext;
                   1148:                }
1.2       schwarze 1149:        }
1.32      schwarze 1150:
                   1151:        /*
                   1152:         * Do not print the current character yet because font
                   1153:         * instructions might follow; only remember it.
                   1154:         * For the first character, nothing else is done.
                   1155:         * The final character will get printed from ps_fclose().
                   1156:         */
                   1157:
1.29      schwarze 1158:        p->ps->last = c;
1.32      schwarze 1159:
                   1160:        /*
                   1161:         * For an overstrike, back up to the previous position.
1.37      schwarze 1162:         * If the previous character is wider than any it overstrikes,
                   1163:         * remember the current position, because it might also be
                   1164:         * wider than all that will overstrike it.
1.32      schwarze 1165:         */
                   1166:
                   1167:        if (savecol != SIZE_MAX) {
1.37      schwarze 1168:                if (p->ps->pscolnext < p->ps->pscol)
                   1169:                        p->ps->pscolnext = p->ps->pscol;
1.32      schwarze 1170:                ps_pclose(p);
                   1171:                p->ps->pscol = savecol;
                   1172:                p->ps->flags &= ~PS_BACKSP;
1.37      schwarze 1173:        } else
                   1174:                p->ps->pscolnext = 0;
1.2       schwarze 1175: }
                   1176:
                   1177: static void
1.1       schwarze 1178: ps_advance(struct termp *p, size_t len)
                   1179: {
                   1180:
1.2       schwarze 1181:        /*
                   1182:         * Advance some spaces.  This can probably be made smarter,
                   1183:         * i.e., to have multiple space-separated words in the same
                   1184:         * scope, but this is easier:  just close out the current scope
                   1185:         * and readjust our column settings.
                   1186:         */
1.1       schwarze 1187:
1.2       schwarze 1188:        ps_fclose(p);
1.17      schwarze 1189:        p->ps->pscol += len;
1.1       schwarze 1190: }
                   1191:
                   1192: static void
                   1193: ps_endline(struct termp *p)
                   1194: {
                   1195:
1.2       schwarze 1196:        /* Close out any scopes we have open: we're at eoln. */
                   1197:
                   1198:        ps_fclose(p);
                   1199:
                   1200:        /*
                   1201:         * If we're in the margin, don't try to recalculate our current
                   1202:         * row.  XXX: if the column tries to be fancy with multiple
1.24      schwarze 1203:         * lines, we'll do nasty stuff.
1.2       schwarze 1204:         */
1.1       schwarze 1205:
1.17      schwarze 1206:        if (PS_MARGINS & p->ps->flags)
1.6       schwarze 1207:                return;
                   1208:
                   1209:        /* Left-justify. */
                   1210:
1.17      schwarze 1211:        p->ps->pscol = p->ps->left;
1.6       schwarze 1212:
                   1213:        /* If we haven't printed anything, return. */
                   1214:
1.17      schwarze 1215:        if (PS_NEWPAGE & p->ps->flags)
1.1       schwarze 1216:                return;
                   1217:
1.2       schwarze 1218:        /*
                   1219:         * Put us down a line.  If we're at the page bottom, spit out a
                   1220:         * showpage and restart our row.
                   1221:         */
                   1222:
1.24      schwarze 1223:        if (p->ps->psrow >= p->ps->lineheight + p->ps->bottom) {
1.17      schwarze 1224:                p->ps->psrow -= p->ps->lineheight;
1.1       schwarze 1225:                return;
                   1226:        }
                   1227:
1.7       schwarze 1228:        ps_closepage(p);
1.1       schwarze 1229: }
1.2       schwarze 1230:
                   1231: static void
                   1232: ps_setfont(struct termp *p, enum termfont f)
                   1233: {
                   1234:
1.4       schwarze 1235:        assert(f < TERMFONT__MAX);
1.17      schwarze 1236:        p->ps->lastf = f;
1.24      schwarze 1237:
1.6       schwarze 1238:        /*
                   1239:         * If we're still at the top of the page, let the font-setting
                   1240:         * be delayed until we actually have stuff to print.
                   1241:         */
                   1242:
1.17      schwarze 1243:        if (PS_NEWPAGE & p->ps->flags)
1.6       schwarze 1244:                return;
                   1245:
1.7       schwarze 1246:        if (TERMTYPE_PS == p->type)
1.24      schwarze 1247:                ps_printf(p, "/%s %zu selectfont\n",
                   1248:                    fonts[(int)f].name, p->ps->scale);
1.7       schwarze 1249:        else
1.24      schwarze 1250:                ps_printf(p, "/F%d %zu Tf\n",
                   1251:                    (int)f, p->ps->scale);
1.2       schwarze 1252: }
                   1253:
1.3       schwarze 1254: static size_t
1.17      schwarze 1255: ps_width(const struct termp *p, int c)
1.3       schwarze 1256: {
                   1257:
1.4       schwarze 1258:        if (c <= 32 || c - 32 >= MAXCHAR)
1.26      schwarze 1259:                c = 0;
                   1260:        else
                   1261:                c -= 32;
1.4       schwarze 1262:
1.40      schwarze 1263:        return (size_t)fonts[(int)TERMFONT_NONE].gly[c].wx;
1.6       schwarze 1264: }
                   1265:
1.39      schwarze 1266: static int
1.6       schwarze 1267: ps_hspan(const struct termp *p, const struct roffsu *su)
                   1268: {
                   1269:        double           r;
1.24      schwarze 1270:
1.6       schwarze 1271:        /*
                   1272:         * All of these measurements are derived by converting from the
                   1273:         * native measurement to AFM units.
                   1274:         */
                   1275:        switch (su->unit) {
1.28      schwarze 1276:        case SCALE_BU:
                   1277:                /*
                   1278:                 * Traditionally, the default unit is fixed to the
                   1279:                 * output media.  So this would refer to the point.  In
                   1280:                 * mandoc(1), however, we stick to the default terminal
                   1281:                 * scaling unit so that output is the same regardless
                   1282:                 * the media.
                   1283:                 */
                   1284:                r = PNT2AFM(p, su->scale * 72.0 / 240.0);
                   1285:                break;
1.24      schwarze 1286:        case SCALE_CM:
1.28      schwarze 1287:                r = PNT2AFM(p, su->scale * 72.0 / 2.54);
                   1288:                break;
                   1289:        case SCALE_EM:
                   1290:                r = su->scale *
                   1291:                    fonts[(int)TERMFONT_NONE].gly[109 - 32].wx;
                   1292:                break;
                   1293:        case SCALE_EN:
                   1294:                r = su->scale *
                   1295:                    fonts[(int)TERMFONT_NONE].gly[110 - 32].wx;
1.6       schwarze 1296:                break;
1.24      schwarze 1297:        case SCALE_IN:
1.27      schwarze 1298:                r = PNT2AFM(p, su->scale * 72.0);
1.6       schwarze 1299:                break;
1.28      schwarze 1300:        case SCALE_MM:
                   1301:                r = su->scale *
                   1302:                    fonts[(int)TERMFONT_NONE].gly[109 - 32].wx / 100.0;
                   1303:                break;
1.24      schwarze 1304:        case SCALE_PC:
1.27      schwarze 1305:                r = PNT2AFM(p, su->scale * 12.0);
1.6       schwarze 1306:                break;
1.24      schwarze 1307:        case SCALE_PT:
1.28      schwarze 1308:                r = PNT2AFM(p, su->scale * 1.0);
1.6       schwarze 1309:                break;
1.24      schwarze 1310:        case SCALE_VS:
1.17      schwarze 1311:                r = su->scale * p->ps->lineheight;
1.6       schwarze 1312:                break;
                   1313:        default:
                   1314:                r = su->scale;
                   1315:                break;
                   1316:        }
                   1317:
1.40      schwarze 1318:        return r * 24.0;
1.17      schwarze 1319: }
                   1320:
                   1321: static void
                   1322: ps_growbuf(struct termp *p, size_t sz)
                   1323: {
                   1324:        if (p->ps->psmargcur + sz <= p->ps->psmargsz)
                   1325:                return;
                   1326:
                   1327:        if (sz < PS_BUFSLOP)
                   1328:                sz = PS_BUFSLOP;
                   1329:
                   1330:        p->ps->psmargsz += sz;
1.25      schwarze 1331:        p->ps->psmarg = mandoc_realloc(p->ps->psmarg, p->ps->psmargsz);
1.3       schwarze 1332: }