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