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