[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.53

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