Annotation of researchv10no/cmd/postscript/dpost/pictures.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  *
                      3:  * PostScript picture inclusion routines. Support for managing in-line pictures
                      4:  * has been added, and works in combination with the simple picpack pre-processor
                      5:  * that's supplied with this package. An in-line picture begins with a special
                      6:  * device control command that looks like,
                      7:  *
                      8:  *             x X InlinPicture name size
                      9:  *
                     10:  * where name is the pathname of the original picture file and size is the number
                     11:  * of bytes in the picture, which begins immediately on the next line. When dpost
                     12:  * encounters the InlinePicture device control command inlinepic() is called and
                     13:  * that routine appends the string name and the integer size to a temporary file
                     14:  * (fp_pic) and then adds the next size bytes read from the current input file to
                     15:  * file fp_pic. All in-line pictures are saved in fp_pic and located later using
                     16:  * the name string and picture file size that separate pictures saved in fp_pic.
                     17:  *
                     18:  * When a picture request (ie. an "x X PI" command) is encountered picopen() is
                     19:  * called and it first looks for the picture file in fp_pic. If it's found there
                     20:  * the entire picture (ie. size bytes) is copied from fp_pic to a new temp file
                     21:  * and that temp file is used as the picture file. If there's nothing in fp_pic
                     22:  * or if the lookup failed the original route is taken.
                     23:  *
                     24:  * Support for in-line pictures is an attempt to address requirements, expressed
                     25:  * by several organizations, of being able to store a document as a single file
                     26:  * (usually troff input) that can then be sent through dpost and ultimately to
                     27:  * a PostScript printer. The mechanism may help some users, but the are obvious
                     28:  * disadvantages to this approach, and the original mechanism is the recommended
                     29:  * approach! Perhaps the most important problem is that troff output, with in-line
                     30:  * pictures included, doesn't fit the device independent language accepted by
                     31:  * important post-processors (like proff) and that means you won't be able to
                     32:  * reliably preview a packed file on your 5620 (or whatever).
                     33:  *
                     34:  */
                     35: 
                     36: #include <stdio.h>
                     37: 
                     38: #include "comments.h"                  /* PostScript file structuring comments */
                     39: #include "gen.h"                       /* general purpose definitions */
                     40: #include "path.h"                      /* just for TEMPDIR definition */
                     41: #include "ext.h"                       /* external variable declarations */
                     42: 
                     43: FILE   *fp_pic = NULL;                 /* in-line pictures go here */
                     44: FILE   *picopen();
                     45: 
                     46: extern int     res, hpos, vpos;
                     47: extern int     picflag;
                     48: extern FILE    *tf;
                     49: 
                     50: /*****************************************************************************/
                     51: 
                     52: picture(buf)
                     53: 
                     54:     char       *buf;           /* stuff following 'x X PI' command */
                     55: 
                     56: {
                     57: 
                     58:     int                poffset;        /* page offset */
                     59:     int                indent;         /* indent */
                     60:     int                length;         /* line length  */
                     61:     int                totrap;         /* distance to next trap */
                     62:     char       name[100];      /* picture file and page string */
                     63:     char       hwo[40], *p;    /* height, width and offset strings */
                     64:     char       flags[20];      /* miscellaneous stuff */
                     65:     int                page = 1;       /* page number pulled from name[] */
                     66:     double     frame[4];       /* height, width, y, and x offsets from hwo[] */
                     67:     char       units;          /* scale indicator for frame dimensions */
                     68:     int                whiteout = 0;   /* white out the box? */
                     69:     int                outline = 0;    /* draw a box around the picture? */
                     70:     int                scaleboth = 0;  /* scale both dimensions? */
                     71:     double     adjx = 0.5;     /* left-right adjustment */
                     72:     double     adjy = 0.5;     /* top-bottom adjustment */
                     73:     double     rot = 0;        /* rotation in clockwise degrees */
                     74:     FILE       *fp_in;         /* for *name */
                     75:     int                i;              /* loop index */
                     76: 
                     77:     char       *strchr();
                     78: 
                     79: /*
                     80:  *
                     81:  * Called from devcntrl() after an 'x X PI' command is found. The syntax of that
                     82:  * command is:
                     83:  *
                     84:  *     x X PI:args
                     85:  *
                     86:  * with args separated by colons and given by:
                     87:  *
                     88:  *     poffset
                     89:  *     indent
                     90:  *     length
                     91:  *     totrap
                     92:  *     file[(page)]
                     93:  *     height[,width[,yoffset[,xoffset]]]
                     94:  *     [flags]
                     95:  *
                     96:  * poffset, indent, length, and totrap are given in machine units. height, width,
                     97:  * and offset refer to the picture frame in inches, unless they're followed by
                     98:  * the u scale indicator. flags is a string that provides a little bit of control
                     99:  * over the placement of the picture in the frame. Rotation of the picture, in
                    100:  * clockwise degrees, is set by the a flag. If it's not followed by an angle
                    101:  * the current rotation angle is incremented by 90 degrees, otherwise the angle
                    102:  * is set by the number that immediately follows the a.
                    103:  *
                    104:  */
                    105: 
                    106:     if ( picflag == OFF )              /* skip it */
                    107:        return;
                    108: 
                    109:     flushtext();
                    110: 
                    111:     flags[0] = '\0';                   /* just to be safe */
                    112:     if ( sscanf(buf, "%d:%d:%d:%d:%[^:]:%[^:]:%[^:]", &poffset, &indent,
                    113:                &length, &totrap, name, hwo, flags) < 6 ) {
                    114:            error(NON_FATAL, "too few arguments to specify picture");
                    115:            return;
                    116:     }  /* End if */
                    117: 
                    118:     if ( sscanf(name, "%*[^(](%d", &page) == 1 )       /* grab the page number */
                    119:        strtok(name, "(");                      /* and separate it from the name */
                    120: 
                    121:     if ( (fp_in = picopen(name)) == NULL ) {
                    122:        error(NON_FATAL, "can't open picture file %s", name);
                    123:        return;
                    124:     }  /* End if */
                    125: 
                    126:     frame[0] = frame[1] = -1;          /* default frame height, width */
                    127:     frame[2] = frame[3] = 0;           /* and y and x offsets */
                    128: 
                    129:     for ( i = 0, p = hwo-1; i < 4 && p != NULL; i++, p = strchr(p, ',') )
                    130:        if ( sscanf(++p, "%lf%c", &frame[i], &units) == 2 )
                    131:            if ( units == 'i' || units == ',' || units == '\0' )
                    132:                frame[i] *= res;
                    133: 
                    134:     if ( frame[0] <= 0 )               /* check what we got for height */
                    135:        frame[0] = totrap;
                    136: 
                    137:     if ( frame[1] <= 0 )               /* and width - check too big?? */
                    138:        frame[1] = length - indent;
                    139: 
                    140:     frame[3] += poffset + indent;      /* real x offset */
                    141: 
                    142:     for ( i = 0; flags[i]; i++ )
                    143:        switch ( flags[i] ) {
                    144:            case 'c': adjx = adjy = 0.5; break; /* move to the center */
                    145:            case 'l': adjx = 0; break;          /* left */
                    146:            case 'r': adjx = 1; break;          /* right */
                    147:            case 't': adjy = 1; break;          /* top */
                    148:            case 'b': adjy = 0; break;          /* or bottom justify */
                    149:            case 'o': outline = 1; break;       /* outline the picture */
                    150:            case 'w': whiteout = 1; break;      /* white out the box */
                    151:            case 's': scaleboth = 1; break;     /* scale both dimensions */
                    152:            case 'a': if ( sscanf(&flags[i+1], "%lf", &rot) != 1 )
                    153:                          rot += 90;
                    154:        }   /* End switch */
                    155: 
                    156:     restore();
                    157:     ps_include(fp_in, tf, page, whiteout, outline, scaleboth,
                    158:                frame[3]+frame[1]/2, -vpos-frame[2]-frame[0]/2, frame[1], frame[0], adjx, adjy, -rot);
                    159:     save();
                    160:     fclose(fp_in);
                    161: 
                    162: }   /* End of picture */
                    163: 
                    164: /*****************************************************************************/
                    165: 
                    166: FILE *picopen(path)
                    167: 
                    168:     char       *path;                  /* picture file pathname */
                    169: 
                    170: {
                    171: 
                    172:     char       name[100];              /* pathnames */
                    173:     long       pos;                    /* current position */
                    174:     long       total;                  /* and sizes - from *fp_pic */
                    175:     char       *tname;                 /* pathname */
                    176:     FILE       *fp;                    /* and pointer for the new temp file */
                    177: 
                    178: /*
                    179:  *
                    180:  * Responsible for finding and opening the next picture file. If we've accumulated
                    181:  * any in-line pictures fp_pic won't be NULL and we'll look there first. If *path
                    182:  * is found in *fp_pic we create another temp file, open it for update, unlink it,
                    183:  * copy in the picture, seek back to the start of the new temp file, and return
                    184:  * the file pointer to the caller. If fp_pic is NULL or the lookup fails we just
                    185:  * open file *path and return the resulting file pointer to the caller.
                    186:  *
                    187:  */
                    188: 
                    189:     if ( fp_pic != NULL ) {
                    190:        fseek(fp_pic, 0L, 0);
                    191:        while ( fscanf(fp_pic, "%s %ld\n", name, &total) != EOF ) {
                    192:            pos = ftell(fp_pic);
                    193:            if ( strcmp(path, name) == 0 ) {
                    194:                if ( (tname = tempnam(TEMPDIR, "dpost")) == NULL )
                    195:                    error(FATAL, "can't generate temp file name");
                    196:                if ( (fp = fopen(tname, "w+r")) == NULL )
                    197:                    error(FATAL, "can't open %s", tname);
                    198:                unlink(tname);
                    199:                free(tname);
                    200:                piccopy(fp_pic, fp, total);
                    201:                fseek(fp, 0L, 0);
                    202:                return(fp);
                    203:            }   /* End if */
                    204:            fseek(fp_pic, total+pos, 0);
                    205:        }   /* End while */
                    206:     }  /* End if */
                    207: 
                    208:     return(fopen(path, "r"));
                    209: 
                    210: }   /* End of picopen */
                    211: 
                    212: /*****************************************************************************/
                    213: 
                    214: inlinepic(fp, buf)
                    215: 
                    216:     FILE       *fp;                    /* current input file */
                    217:     char       *buf;                   /* whatever followed "x X InlinePicture" */
                    218: 
                    219: {
                    220: 
                    221:     char       *tname;                 /* temp file pathname - for *fp_pic */
                    222:     char       name[100];              /* picture file pathname */
                    223:     long       total;                  /* and size - both from *buf */
                    224: 
                    225: /*
                    226:  *
                    227:  * Adds an in-line picture file to the end of temporary file *fp_pic. All pictures
                    228:  * grabbed from the input file are saved in the same temp file. Each is preceeded
                    229:  * by a one line header that includes the original picture file pathname and the
                    230:  * size of the picture in bytes. The in-line picture file is opened for update,
                    231:  * left open, and unlinked so it disappears when we do.
                    232:  *
                    233:  */
                    234: 
                    235:     if ( fp_pic == NULL ) {
                    236:        if ( (tname = tempnam(TEMPDIR, "dpost")) == NULL )
                    237:            error(FATAL, "can't generate in-line picture file name");
                    238:        if ( (fp_pic = fopen(tname, "w+r")) == NULL )
                    239:            error(FATAL, "can't open in-line picture file %s", tname);
                    240:        unlink(tname);
                    241:     }  /* End if */
                    242: 
                    243:     if ( sscanf(buf, "%s %ld", name, &total) != 2 )
                    244:        error(FATAL, "in-line picture error");
                    245: 
                    246:     fseek(fp_pic, 0L, 2);
                    247:     fprintf(fp_pic, "%s %ld\n", name, total);
                    248:     getc(fp);
                    249:     fflush(fp_pic);
                    250:     piccopy(fp, fp_pic, total);
                    251:     ungetc('\n', fp);
                    252: 
                    253: }   /* End of inlinepic */
                    254: 
                    255: /*****************************************************************************/
                    256: 
                    257: piccopy(fp_in, fp_out, total)
                    258: 
                    259:     FILE       *fp_in;                 /* input */
                    260:     FILE       *fp_out;                /* and output file pointers */
                    261:     long       total;                  /* number of bytes to be copied */
                    262: 
                    263: {
                    264: 
                    265:     long       i;                      /* loop index */
                    266: 
                    267: /*
                    268:  *
                    269:  * Copies total bytes from file fp_in to fp_out. Used to append picture files to
                    270:  * *fp_pic and then copy them to yet another temporary file immediately before
                    271:  * they're used (in picture()).
                    272:  *
                    273:  */
                    274: 
                    275:     for ( i = 0; i < total; i++ )
                    276:        if ( putc(getc(fp_in), fp_out) == EOF )
                    277:            error(FATAL, "error copying in-line picture file");
                    278:     fflush(fp_out);
                    279: 
                    280: }   /* End of piccopy */
                    281: 
                    282: /*****************************************************************************/
                    283: 

unix.superglobalmegacorp.com

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