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

Annotation of mandoc/term_ps.c, Revision 1.90

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

CVSweb