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

Annotation of mandoc/term_ps.c, Revision 1.72

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

CVSweb