Annotation of researchv10no/cmd/post.src/dpost.utf/ps_include.c, revision 1.1

1.1     ! root        1: 
        !             2: #include <stdio.h>
        !             3: #include "ps_include.h"
        !             4: 
        !             5: #define has(word)      (strncmp(buf, word, strlen(word)) == 0)
        !             6: #define grab(n)                ((Section *)(nglobal \
        !             7:                        ? realloc((char *)global, n*sizeof(Section)) \
        !             8:                        : calloc(n, sizeof(Section))))
        !             9: 
        !            10: char   buf[512];
        !            11: typedef struct {long start, end;} Section;
        !            12: 
        !            13: extern char    *calloc(), *realloc();
        !            14: 
        !            15: 
        !            16: ps_include(fin, fout, page_no, whiteout, outline, scaleboth, cx, cy, sx, sy, ax, ay, rot)
        !            17: 
        !            18: 
        !            19:     FILE       *fin, *fout;            /* input and output files */
        !            20:     int                page_no;                /* physical page number from *fin */
        !            21:     int                whiteout;               /* erase picture area */
        !            22:     int                outline;                /* draw a box around it and */
        !            23:     int                scaleboth;              /* scale both dimensions - if not zero */
        !            24:     double     cx, cy;                 /* center of the picture and */
        !            25:     double     sx, sy;                 /* its size - in current coordinates */
        !            26:     double     ax, ay;                 /* left-right, up-down adjustment */
        !            27:     double     rot;                    /* rotation - in clockwise degrees */
        !            28: 
        !            29: 
        !            30: {
        !            31: 
        !            32: 
        !            33:     int                foundpage = 0;          /* found the page when non zero */
        !            34:     int                foundpbox = 0;          /* found the page bounding box */
        !            35:     int                nglobal = 0;            /* number of global defs so far */
        !            36:     int                maxglobal = 0;          /* and the number we've got room for */
        !            37:     Section    prolog, page, trailer;  /* prologue, page, and trailer offsets */
        !            38:     Section    *global;                /* offsets for all global definitions */
        !            39:     double     llx, lly;               /* lower left and */
        !            40:     double     urx, ury;               /* upper right corners - default coords */
        !            41:     double     w = whiteout != 0;      /* mostly for the var() macro */
        !            42:     double     o = outline != 0;
        !            43:     double     s = scaleboth != 0;
        !            44:     int                i;                      /* loop index */
        !            45: 
        !            46: 
        !            47: /*
        !            48:  *
        !            49:  * Reads a PostScript file (*fin), and uses structuring comments to locate the
        !            50:  * prologue, trailer, global definitions, and the requested page. After the whole
        !            51:  * file is scanned, the  special ps_include PostScript definitions are copied to
        !            52:  * *fout, followed by the prologue, global definitions, the requested page, and
        !            53:  * the trailer. Before returning the initial environment (saved in PS_head) is
        !            54:  * restored.
        !            55:  *
        !            56:  * By default we assume the picture is 8.5 by 11 inches, but the BoundingBox
        !            57:  * comment, if found, takes precedence.
        !            58:  *
        !            59:  */
        !            60: 
        !            61: 
        !            62:        llx = lly = 0;                  /* default BoundingBox - 8.5x11 inches */
        !            63:        urx = 72 * 8.5;
        !            64:        ury = 72 * 11.0;
        !            65: 
        !            66:        /* section boundaries and bounding box */
        !            67: 
        !            68:        prolog.start = prolog.end = 0;
        !            69:        page.start = page.end = 0;
        !            70:        trailer.start = 0;
        !            71:        fseek(fin, 0L, 0);
        !            72: 
        !            73:        while ( fgets(buf, sizeof(buf), fin) != NULL )
        !            74:                if (!has("%%"))
        !            75:                        continue;
        !            76:                else if (has("%%Page: ")) {
        !            77:                        if (!foundpage)
        !            78:                                page.start = ftell(fin);
        !            79:                        sscanf(buf, "%*s %*s %d", &i);
        !            80:                        if (i == page_no)
        !            81:                                foundpage = 1;
        !            82:                        else if (foundpage && page.end <= page.start)
        !            83:                                page.end = ftell(fin);
        !            84:                } else if (has("%%EndPage: ")) {
        !            85:                        sscanf(buf, "%*s %*s %d", &i);
        !            86:                        if (i == page_no) {
        !            87:                                foundpage = 1;
        !            88:                                page.end = ftell(fin);
        !            89:                        }
        !            90:                        if (!foundpage)
        !            91:                                page.start = ftell(fin);
        !            92:                } else if (has("%%PageBoundingBox: ")) {
        !            93:                        if (i == page_no) {
        !            94:                                foundpbox = 1;
        !            95:                                sscanf(buf, "%*s %lf %lf %lf %lf",
        !            96:                                                &llx, &lly, &urx, &ury);
        !            97:                        }
        !            98:                } else if (has("%%BoundingBox: ")) {
        !            99:                        if (!foundpbox)
        !           100:                                sscanf(buf,"%*s %lf %lf %lf %lf",
        !           101:                                                &llx, &lly, &urx, &ury);
        !           102:                } else if (has("%%EndProlog") || has("%%EndSetup") || has("%%EndDocumentSetup"))
        !           103:                        prolog.end = page.start = ftell(fin);
        !           104:                else if (has("%%Trailer"))
        !           105:                        trailer.start = ftell(fin);
        !           106:                else if (has("%%BeginGlobal")) {
        !           107:                        if (page.end <= page.start) {
        !           108:                                if (nglobal >= maxglobal) {
        !           109:                                        maxglobal += 20;
        !           110:                                        global = grab(maxglobal);
        !           111:                                }
        !           112:                                global[nglobal].start = ftell(fin);
        !           113:                        }
        !           114:                } else if (has("%%EndGlobal"))
        !           115:                        if (page.end <= page.start)
        !           116:                                global[nglobal++].end = ftell(fin);
        !           117: 
        !           118:        fseek(fin, 0L, 2);
        !           119:        if (trailer.start == 0)
        !           120:                trailer.start = ftell(fin);
        !           121:        trailer.end = ftell(fin);
        !           122: 
        !           123:        if (page.end <= page.start)
        !           124:                page.end = trailer.start;
        !           125: 
        !           126: /*
        !           127: fprintf(stderr, "prolog=(%d,%d)\n", prolog.start, prolog.end);
        !           128: fprintf(stderr, "page=(%d,%d)\n", page.start, page.end);
        !           129: for(i = 0; i < nglobal; i++)
        !           130:        fprintf(stderr, "global[%d]=(%d,%d)\n", i, global[i].start, global[i].end);
        !           131: fprintf(stderr, "trailer=(%d,%d)\n", trailer.start, trailer.end);
        !           132: */
        !           133: 
        !           134:        /* all output here */
        !           135:        print(fout, PS_head);
        !           136: /*
        !           137:  * Unix 4.0 didn't like the var macro.
        !           138:  *
        !           139:        var(llx); var(lly); var(urx); var(ury); var(w); var(o); var(s);
        !           140:        var(cx); var(cy); var(sx); var(sy); var(ax); var(ay); var(rot);
        !           141:  *
        !           142:  */
        !           143: 
        !           144:        fprintf(fout, "/llx %g def\n", llx);
        !           145:        fprintf(fout, "/lly %g def\n", lly);
        !           146:        fprintf(fout, "/urx %g def\n", urx);
        !           147:        fprintf(fout, "/ury %g def\n", ury);
        !           148:        fprintf(fout, "/w %g def\n", w);
        !           149:        fprintf(fout, "/o %g def\n", o);
        !           150:        fprintf(fout, "/s %g def\n", s);
        !           151:        fprintf(fout, "/cx %g def\n", cx);
        !           152:        fprintf(fout, "/cy %g def\n", cy);
        !           153:        fprintf(fout, "/sx %g def\n", sx);
        !           154:        fprintf(fout, "/sy %g def\n", sy);
        !           155:        fprintf(fout, "/ax %g def\n", ax);
        !           156:        fprintf(fout, "/ay %g def\n", ay);
        !           157:        fprintf(fout, "/rot %g def\n", rot);
        !           158: 
        !           159:        print(fout, PS_setup);
        !           160:        copy(fin, fout, &prolog);
        !           161:        for(i = 0; i < nglobal; i++)
        !           162:                copy(fin, fout, &global[i]);
        !           163:        copy(fin, fout, &page);
        !           164:        copy(fin, fout, &trailer);
        !           165:        print(fout, PS_tail);
        !           166: 
        !           167:        if(nglobal)
        !           168:                free(global);
        !           169: 
        !           170: }
        !           171: 
        !           172: static
        !           173: print(fout, s)
        !           174: FILE *fout;
        !           175: char **s;
        !           176: {
        !           177:        while (*s)
        !           178:                fprintf(fout, "%s\n", *s++);
        !           179: }
        !           180: 
        !           181: static
        !           182: copy(fin, fout, s)
        !           183: FILE *fin, *fout;
        !           184: Section *s;
        !           185: {
        !           186:        if (s->end <= s->start)
        !           187:                return;
        !           188:        fseek(fin, s->start, 0);
        !           189:        while (ftell(fin) < s->end && fgets(buf, sizeof(buf), fin) != NULL)
        !           190:                if (buf[0] != '%')
        !           191:                        fprintf(fout, "%s", buf);
        !           192: }
        !           193: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.