5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * CDDL HEADER START
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * The contents of this file are subject to the terms of the
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Common Development and Distribution License, Version 1.0 only
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * (the "License"). You may not use this file except in compliance
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * with the License.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * or http://www.opensolaris.org/os/licensing.
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * See the License for the specific language governing permissions
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * and limitations under the License.
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk *
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * When distributing Covered Code, include this CDDL HEADER in each
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * If applicable, add the following below this CDDL HEADER, with the
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * fields enclosed by brackets "[]" replaced with your own identifying
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * information: Portions Copyright [yyyy] [name of copyright owner]
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * CDDL HEADER END
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Use is subject to license terms.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/* All Rights Reserved */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#pragma ident "%Z%%M% %I% %E% SMI"
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * PostScript picture inclusion routines. Support for managing in-line pictures
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * has been added, and works in combination with the simple picpack pre-processor
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * that's supplied with this package. An in-line picture begins with a special
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * device control command that looks like,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * x X InlinPicture name size
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * where name is the pathname of the original picture file and size is the number
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * of bytes in the picture, which begins immediately on the next line. When dpost
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * encounters the InlinePicture device control command inlinepic() is called and
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * that routine appends the string name and the integer size to a temporary file
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * (fp_pic) and then adds the next size bytes read from the current input file to
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * file fp_pic. All in-line pictures are saved in fp_pic and located later using
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * the name string and picture file size that separate pictures saved in fp_pic.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * When a picture request (ie. an "x X PI" command) is encountered picopen() is
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * called and it first looks for the picture file in fp_pic. If it's found there
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * the entire picture (ie. size bytes) is copied from fp_pic to a new temp file
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * and that temp file is used as the picture file. If there's nothing in fp_pic
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * or if the lookup failed the original route is taken.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Support for in-line pictures is an attempt to address requirements, expressed
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * by several orginazations, of being able to store a document as a single file
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * (usually troff input) that can then be sent through dpost and ultimately to
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * a PostScript printer. The mechanism may help some users, but the are obvious
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * disadvantages to this approach, and the original mechanism is the recommended
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * approach! Perhaps the most important problem is that troff output, with in-line
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * pictures included, doesn't fit the device independent language accepted by
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * important post-processors (like proff) and that means you won't be able to
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * reliably preview a packed file on your 5620 (or whatever).
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#include <stdio.h>
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#include "comments.h" /* PostScript file structuring comments */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#include "gen.h" /* general purpose definitions */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#include "path.h" /* just for TEMPDIR definition */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkFILE *fp_pic = NULL; /* in-line pictures go here */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkFILE *picopen();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkextern int res, hpos, vpos;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkextern int picflag;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkextern FILE *tf;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkstatic void piccopy(FILE *, FILE *, long);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*****************************************************************************/
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkvoid
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkpicture(char *buf)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /* stuff following 'x X PI' command */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk{
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int poffset; /* page offset */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int indent; /* indent */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int length; /* line length */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int totrap; /* distance to next trap */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk char name[100]; /* picture file and page string */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk char hwo[40], *p; /* height, width and offset strings */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk char flags[20]; /* miscellaneous stuff */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int page = 1; /* page number pulled from name[] */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk double frame[4]; /* height, width, y, and x offsets from hwo[] */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk char units; /* scale indicator for frame dimensions */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int whiteout = 0; /* white out the box? */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int outline = 0; /* draw a box around the picture? */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int scaleboth = 0; /* scale both dimensions? */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk double adjx = 0.5; /* left-right adjustment */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk double adjy = 0.5; /* top-bottom adjustment */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk double rot = 0; /* rotation in clockwise degrees */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk FILE *fp_in; /* for *name */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int i; /* loop index */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk char *strchr();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Called from devcntrl() after an 'x X PI' command is found. The syntax of that
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * command is:
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * x X PI:args
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * with args separated by colons and given by:
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * poffset
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * indent
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * length
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * totrap
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * file[(page)]
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * height[,width[,yoffset[,xoffset]]]
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * [flags]
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * poffset, indent, length, and totrap are given in machine units. height, width,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * and offset refer to the picture frame in inches, unless they're followed by
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * the u scale indicator. flags is a string that provides a little bit of control
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * over the placement of the picture in the frame. Rotation of the picture, in
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * clockwise degrees, is set by the a flag. If it's not followed by an angle
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * the current rotation angle is incremented by 90 degrees, otherwise the angle
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * is set by the number that immediately follows the a.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ( picflag == OFF ) /* skip it */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk endtext();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk flags[0] = '\0'; /* just to be safe */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ( sscanf(buf, "%d:%d:%d:%d:%[^:]:%[^:]:%[^:]", &poffset, &indent,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk &length, &totrap, name, hwo, flags) < 6 ) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk error(NON_FATAL, "too few arguments to specify picture");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk } /* End if */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ( sscanf(name, "%*[^(](%d", &page) == 1 ) /* grab the page number */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk strtok(name, "("); /* and separate it from the name */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ( (fp_in = picopen(name)) == NULL ) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk error(NON_FATAL, "can't open picture file %s", name);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk } /* End if */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk frame[0] = frame[1] = -1; /* default frame height, width */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk frame[2] = frame[3] = 0; /* and y and x offsets */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for ( i = 0, p = hwo-1; i < 4 && p != NULL; i++, p = strchr(p, ',') )
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ( sscanf(++p, "%lf%c", &frame[i], &units) == 2 )
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ( units == 'i' || units == ',' || units == '\0' )
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk frame[i] *= res;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ( frame[0] <= 0 ) /* check what we got for height */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk frame[0] = totrap;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if ( frame[1] <= 0 ) /* and width - check too big?? */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk frame[1] = length - indent;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk frame[3] += poffset + indent; /* real x offset */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for ( i = 0; flags[i]; i++ )
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk switch ( flags[i] ) {
case 'c': adjx = adjy = 0.5; break; /* move to the center */
case 'l': adjx = 0; break; /* left */
case 'r': adjx = 1; break; /* right */
case 't': adjy = 1; break; /* top */
case 'b': adjy = 0; break; /* or bottom justify */
case 'o': outline = 1; break; /* outline the picture */
case 'w': whiteout = 1; break; /* white out the box */
case 's': scaleboth = 1; break; /* scale both dimensions */
case 'a': if ( sscanf(&flags[i+1], "%lf", &rot) != 1 )
rot += 90;
} /* End switch */
fprintf(tf, "cleartomark restore\n");
ps_include(fp_in, tf, page, whiteout, outline, scaleboth,
frame[3]+frame[1]/2, -vpos-frame[2]-frame[0]/2, frame[1], frame[0], adjx, adjy, -rot);
fprintf(tf, "save mark\n");
xymove(hpos, vpos);
t_sf();
fclose(fp_in);
} /* End of picture */
/*****************************************************************************/
FILE *
picopen(char *path)
/* picture file pathname */
{
char name[100]; /* pathnames */
long total; /* and sizes - from *fp_pic */
char *tname; /* pathname */
FILE *fp; /* and pointer for the new temp file */
/*
*
* Responsible for finding and opening the next picture file. If we've accumulated
* any in-line pictures fp_pic won't be NULL and we'll look there first. If *path
* is found in *fp_pic we create another temp file, open it for update, unlink it,
* copy in the picture, seek back to the start of the new temp file, and return
* the file pointer to the caller. If fp_pic is NULL or the lookup fails we just
* open file *path and return the resulting file pointer to the caller.
*
*/
if ( fp_pic != NULL ) {
fseek(fp_pic, 0L, 0);
while ( fscanf(fp_pic, "%s %ld\n", name, &total) != EOF ) {
if ( strcmp(path, name) == 0 ) {
if ( (tname = tempnam(TEMPDIR, "dpost")) == NULL )
error(FATAL, "can't generate temp file name");
if ( (fp = fopen(tname, "w+")) == NULL )
error(FATAL, "can't open %s", tname);
unlink(tname);
free(tname);
piccopy(fp_pic, fp, total);
fseek(fp, 0L, 0);
return(fp);
} /* End if */
fseek(fp_pic, total, 1);
} /* End while */
} /* End if */
return(fopen(path, "r"));
} /* End of picopen */
/*****************************************************************************/
void
inlinepic(FILE *fp, char *buf)
/* fp - current input file */
/* buf - whatever followed "x X InlinePicture" */
{
char *tname; /* temp file pathname - for *fp_pic */
char name[100]; /* picture file pathname */
long total; /* and size - both from *buf */
/*
*
* Adds an in-line picture file to the end of temporary file *fp_pic. All pictures
* grabbed from the input file are saved in the same temp file. Each is preceeded
* by a one line header that includes the original picture file pathname and the
* size of the picture in bytes. The in-line picture file is opened for update,
* left open, and unlinked so it disappears when we do.
*
*/
if ( fp_pic == NULL ) {
if ( (tname = tempnam(TEMPDIR, "dpost")) == NULL )
error(FATAL, "can't generate in-line picture file name");
if ( (fp_pic = fopen(tname, "w+")) == NULL )
error(FATAL, "can't open in-line picture file %s", tname);
unlink(tname);
} /* End if */
if ( sscanf(buf, "%s %ld", name, &total) != 2 )
error(FATAL, "in-line picture error");
fseek(fp_pic, 0L, 2);
fprintf(fp_pic, "%s %ld\n", name, total);
getc(fp);
fflush(fp_pic);
piccopy(fp, fp_pic, total);
ungetc('\n', fp);
} /* End of inlinepic */
/*****************************************************************************/
static void
piccopy(FILE *fp_in, FILE *fp_out, long total)
/* fp_in - input */
/* fp_out - and output file pointers */
/* total - number of bytes to be copied */
{
long i; /* loop index */
/*
*
* Copies total bytes from file fp_in to fp_out. Used to append picture files to
* *fp_pic and then copy them to yet another temporary file immediately before
* they're used (in picture()).
*
*/
for ( i = 0; i < total; i++ )
if ( putc(getc(fp_in), fp_out) == EOF )
error(FATAL, "error copying in-line picture file");
fflush(fp_out);
} /* End of piccopy */