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

Annotation of mandoc/term_ps.c, Revision 1.89

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

CVSweb