17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
23f928ce6ceastha * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate
31f928ce6ceastha#pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate/*
347c478bdstevel@tonic-gate *
357c478bdstevel@tonic-gate * PostScript picture inclusion routines. Support for managing in-line pictures
367c478bdstevel@tonic-gate * has been added, and works in combination with the simple picpack pre-processor
377c478bdstevel@tonic-gate * that's supplied with this package. An in-line picture begins with a special
387c478bdstevel@tonic-gate * device control command that looks like,
397c478bdstevel@tonic-gate *
407c478bdstevel@tonic-gate *		x X InlinPicture name size
417c478bdstevel@tonic-gate *
427c478bdstevel@tonic-gate * where name is the pathname of the original picture file and size is the number
437c478bdstevel@tonic-gate * of bytes in the picture, which begins immediately on the next line. When dpost
447c478bdstevel@tonic-gate * encounters the InlinePicture device control command inlinepic() is called and
457c478bdstevel@tonic-gate * that routine appends the string name and the integer size to a temporary file
467c478bdstevel@tonic-gate * (fp_pic) and then adds the next size bytes read from the current input file to
477c478bdstevel@tonic-gate * file fp_pic. All in-line pictures are saved in fp_pic and located later using
487c478bdstevel@tonic-gate * the name string and picture file size that separate pictures saved in fp_pic.
497c478bdstevel@tonic-gate *
507c478bdstevel@tonic-gate * When a picture request (ie. an "x X PI" command) is encountered picopen() is
517c478bdstevel@tonic-gate * called and it first looks for the picture file in fp_pic. If it's found there
527c478bdstevel@tonic-gate * the entire picture (ie. size bytes) is copied from fp_pic to a new temp file
537c478bdstevel@tonic-gate * and that temp file is used as the picture file. If there's nothing in fp_pic
547c478bdstevel@tonic-gate * or if the lookup failed the original route is taken.
557c478bdstevel@tonic-gate *
567c478bdstevel@tonic-gate * Support for in-line pictures is an attempt to address requirements, expressed
577c478bdstevel@tonic-gate * by several orginazations, of being able to store a document as a single file
587c478bdstevel@tonic-gate * (usually troff input) that can then be sent through dpost and ultimately to
597c478bdstevel@tonic-gate * a PostScript printer. The mechanism may help some users, but the are obvious
607c478bdstevel@tonic-gate * disadvantages to this approach, and the original mechanism is the recommended
617c478bdstevel@tonic-gate * approach! Perhaps the most important problem is that troff output, with in-line
627c478bdstevel@tonic-gate * pictures included, doesn't fit the device independent language accepted by
637c478bdstevel@tonic-gate * important post-processors (like proff) and that means you won't be able to
647c478bdstevel@tonic-gate * reliably preview a packed file on your 5620 (or whatever).
657c478bdstevel@tonic-gate *
667c478bdstevel@tonic-gate */
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate#include <stdio.h>
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate#include "comments.h"			/* PostScript file structuring comments */
727c478bdstevel@tonic-gate#include "gen.h"			/* general purpose definitions */
737c478bdstevel@tonic-gate#include "path.h"			/* just for TEMPDIR definition */
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gateFILE	*fp_pic = NULL;			/* in-line pictures go here */
777c478bdstevel@tonic-gateFILE	*picopen();
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gateextern int	res, hpos, vpos;
807c478bdstevel@tonic-gateextern int	picflag;
817c478bdstevel@tonic-gateextern FILE	*tf;
827c478bdstevel@tonic-gate
83f928ce6ceasthastatic void piccopy(FILE *, FILE *, long);
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate/*****************************************************************************/
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate
88f928ce6ceasthavoid
89f928ce6ceasthapicture(char *buf)
90f928ce6ceastha    /* stuff following 'x X PI' command */
917c478bdstevel@tonic-gate{
927c478bdstevel@tonic-gate    int		poffset;	/* page offset */
937c478bdstevel@tonic-gate    int		indent;		/* indent */
947c478bdstevel@tonic-gate    int		length;		/* line length  */
957c478bdstevel@tonic-gate    int		totrap;		/* distance to next trap */
967c478bdstevel@tonic-gate    char	name[100];	/* picture file and page string */
977c478bdstevel@tonic-gate    char	hwo[40], *p;	/* height, width and offset strings */
987c478bdstevel@tonic-gate    char	flags[20];	/* miscellaneous stuff */
997c478bdstevel@tonic-gate    int		page = 1;	/* page number pulled from name[] */
1007c478bdstevel@tonic-gate    double	frame[4];	/* height, width, y, and x offsets from hwo[] */
1017c478bdstevel@tonic-gate    char	units;		/* scale indicator for frame dimensions */
1027c478bdstevel@tonic-gate    int		whiteout = 0;	/* white out the box? */
1037c478bdstevel@tonic-gate    int		outline = 0;	/* draw a box around the picture? */
1047c478bdstevel@tonic-gate    int		scaleboth = 0;	/* scale both dimensions? */
1057c478bdstevel@tonic-gate    double	adjx = 0.5;	/* left-right adjustment */
1067c478bdstevel@tonic-gate    double	adjy = 0.5;	/* top-bottom adjustment */
1077c478bdstevel@tonic-gate    double	rot = 0;	/* rotation in clockwise degrees */
1087c478bdstevel@tonic-gate    FILE	*fp_in;		/* for *name */
1097c478bdstevel@tonic-gate    int		i;		/* loop index */
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gate    char	*strchr();
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate/*
1157c478bdstevel@tonic-gate *
1167c478bdstevel@tonic-gate * Called from devcntrl() after an 'x X PI' command is found. The syntax of that
1177c478bdstevel@tonic-gate * command is:
1187c478bdstevel@tonic-gate *
1197c478bdstevel@tonic-gate *	x X PI:args
1207c478bdstevel@tonic-gate *
1217c478bdstevel@tonic-gate * with args separated by colons and given by:
1227c478bdstevel@tonic-gate *
1237c478bdstevel@tonic-gate *	poffset
1247c478bdstevel@tonic-gate *	indent
1257c478bdstevel@tonic-gate *	length
1267c478bdstevel@tonic-gate *	totrap
1277c478bdstevel@tonic-gate *	file[(page)]
1287c478bdstevel@tonic-gate *	height[,width[,yoffset[,xoffset]]]
1297c478bdstevel@tonic-gate *	[flags]
1307c478bdstevel@tonic-gate *
1317c478bdstevel@tonic-gate * poffset, indent, length, and totrap are given in machine units. height, width,
1327c478bdstevel@tonic-gate * and offset refer to the picture frame in inches, unless they're followed by
1337c478bdstevel@tonic-gate * the u scale indicator. flags is a string that provides a little bit of control
1347c478bdstevel@tonic-gate * over the placement of the picture in the frame. Rotation of the picture, in
1357c478bdstevel@tonic-gate * clockwise degrees, is set by the a flag. If it's not followed by an angle
1367c478bdstevel@tonic-gate * the current rotation angle is incremented by 90 degrees, otherwise the angle
1377c478bdstevel@tonic-gate * is set by the number that immediately follows the a.
1387c478bdstevel@tonic-gate *
1397c478bdstevel@tonic-gate */
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate    if ( picflag == OFF )		/* skip it */
1437c478bdstevel@tonic-gate	return;
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate    endtext();
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate    flags[0] = '\0';			/* just to be safe */
1487c478bdstevel@tonic-gate    if ( sscanf(buf, "%d:%d:%d:%d:%[^:]:%[^:]:%[^:]", &poffset, &indent,
1497c478bdstevel@tonic-gate		&length, &totrap, name, hwo, flags) < 6 )  {
1507c478bdstevel@tonic-gate	    error(NON_FATAL, "too few arguments to specify picture");
1517c478bdstevel@tonic-gate	    return;
1527c478bdstevel@tonic-gate    }	/* End if */
1537c478bdstevel@tonic-gate
1547c478bdstevel@tonic-gate    if ( sscanf(name, "%*[^(](%d", &page) == 1 )	/* grab the page number */
1557c478bdstevel@tonic-gate	strtok(name, "(");			/* and separate it from the name */
1567c478bdstevel@tonic-gate
1577c478bdstevel@tonic-gate    if ( (fp_in = picopen(name)) == NULL ) {
1587c478bdstevel@tonic-gate	error(NON_FATAL, "can't open picture file %s", name);
1597c478bdstevel@tonic-gate	return;
1607c478bdstevel@tonic-gate    }	/* End if */
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gate    frame[0] = frame[1] = -1;		/* default frame height, width */
1637c478bdstevel@tonic-gate    frame[2] = frame[3] = 0;		/* and y and x offsets */
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate    for ( i = 0, p = hwo-1; i < 4 && p != NULL; i++, p = strchr(p, ',') )
1667c478bdstevel@tonic-gate	if ( sscanf(++p, "%lf%c", &frame[i], &units) == 2 )
1677c478bdstevel@tonic-gate	    if ( units == 'i' || units == ',' || units == '\0' )
1687c478bdstevel@tonic-gate		frame[i] *= res;
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gate    if ( frame[0] <= 0 )		/* check what we got for height */
1717c478bdstevel@tonic-gate	frame[0] = totrap;
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate    if ( frame[1] <= 0 )		/* and width - check too big?? */
1747c478bdstevel@tonic-gate	frame[1] = length - indent;
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gate    frame[3] += poffset + indent;	/* real x offset */
1777c478bdstevel@tonic-gate
1787c478bdstevel@tonic-gate    for ( i = 0; flags[i]; i++ )
1797c478bdstevel@tonic-gate	switch ( flags[i] )  {
1807c478bdstevel@tonic-gate	    case 'c': adjx = adjy = 0.5; break;	/* move to the center */
1817c478bdstevel@tonic-gate	    case 'l': adjx = 0; break;		/* left */
1827c478bdstevel@tonic-gate	    case 'r': adjx = 1; break;		/* right */
1837c478bdstevel@tonic-gate	    case 't': adjy = 1; break;		/* top */
1847c478bdstevel@tonic-gate	    case 'b': adjy = 0; break;		/* or bottom justify */
1857c478bdstevel@tonic-gate	    case 'o': outline = 1; break;	/* outline the picture */
1867c478bdstevel@tonic-gate	    case 'w': whiteout = 1; break;	/* white out the box */
1877c478bdstevel@tonic-gate	    case 's': scaleboth = 1; break;	/* scale both dimensions */
1887c478bdstevel@tonic-gate	    case 'a': if ( sscanf(&flags[i+1], "%lf", &rot) != 1 )
1897c478bdstevel@tonic-gate			  rot += 90;
1907c478bdstevel@tonic-gate	}   /* End switch */
1917c478bdstevel@tonic-gate
1927c478bdstevel@tonic-gate    fprintf(tf, "cleartomark restore\n");
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate    ps_include(fp_in, tf, page, whiteout, outline, scaleboth,
1957c478bdstevel@tonic-gate		frame[3]+frame[1]/2, -vpos-frame[2]-frame[0]/2, frame[1], frame[0], adjx, adjy, -rot);
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate    fprintf(tf, "save mark\n");
1987c478bdstevel@tonic-gate    xymove(hpos, vpos);
1997c478bdstevel@tonic-gate    t_sf();
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate    fclose(fp_in);
2027c478bdstevel@tonic-gate
2037c478bdstevel@tonic-gate}   /* End of picture */
2047c478bdstevel@tonic-gate
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate/*****************************************************************************/
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate
209f928ce6ceasthaFILE *
210f928ce6ceasthapicopen(char *path)
211f928ce6ceastha    /* picture file pathname */
2127c478bdstevel@tonic-gate{
2137c478bdstevel@tonic-gate    char	name[100];		/* pathnames */
2147c478bdstevel@tonic-gate    long	total;			/* and sizes - from *fp_pic */
2157c478bdstevel@tonic-gate    char	*tname;			/* pathname */
2167c478bdstevel@tonic-gate    FILE	*fp;			/* and pointer for the new temp file */
2177c478bdstevel@tonic-gate
2187c478bdstevel@tonic-gate
2197c478bdstevel@tonic-gate/*
2207c478bdstevel@tonic-gate *
2217c478bdstevel@tonic-gate * Responsible for finding and opening the next picture file. If we've accumulated
2227c478bdstevel@tonic-gate * any in-line pictures fp_pic won't be NULL and we'll look there first. If *path
2237c478bdstevel@tonic-gate * is found in *fp_pic we create another temp file, open it for update, unlink it,
2247c478bdstevel@tonic-gate * copy in the picture, seek back to the start of the new temp file, and return
2257c478bdstevel@tonic-gate * the file pointer to the caller. If fp_pic is NULL or the lookup fails we just
2267c478bdstevel@tonic-gate * open file *path and return the resulting file pointer to the caller.
2277c478bdstevel@tonic-gate *
2287c478bdstevel@tonic-gate */
2297c478bdstevel@tonic-gate
2307c478bdstevel@tonic-gate
2317c478bdstevel@tonic-gate    if ( fp_pic != NULL )  {
2327c478bdstevel@tonic-gate	fseek(fp_pic, 0L, 0);
2337c478bdstevel@tonic-gate	while ( fscanf(fp_pic, "%s %ld\n", name, &total) != EOF )  {
2347c478bdstevel@tonic-gate	    if ( strcmp(path, name) == 0 )  {
2357c478bdstevel@tonic-gate		if ( (tname = tempnam(TEMPDIR, "dpost")) == NULL )
2367c478bdstevel@tonic-gate		    error(FATAL, "can't generate temp file name");
2377c478bdstevel@tonic-gate		if ( (fp = fopen(tname, "w+")) == NULL )
2387c478bdstevel@tonic-gate		    error(FATAL, "can't open %s", tname);
2397c478bdstevel@tonic-gate		unlink(tname);
2407c478bdstevel@tonic-gate		free(tname);
2417c478bdstevel@tonic-gate		piccopy(fp_pic, fp, total);
2427c478bdstevel@tonic-gate		fseek(fp, 0L, 0);
2437c478bdstevel@tonic-gate		return(fp);
2447c478bdstevel@tonic-gate	    }   /* End if */
2457c478bdstevel@tonic-gate	    fseek(fp_pic, total, 1);
2467c478bdstevel@tonic-gate	}   /* End while */
2477c478bdstevel@tonic-gate    }	/* End if */
2487c478bdstevel@tonic-gate
2497c478bdstevel@tonic-gate    return(fopen(path, "r"));
2507c478bdstevel@tonic-gate
2517c478bdstevel@tonic-gate}   /* End of picopen */
2527c478bdstevel@tonic-gate
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate/*****************************************************************************/
2557c478bdstevel@tonic-gate
2567c478bdstevel@tonic-gate
257f928ce6ceasthavoid
258f928ce6ceasthainlinepic(FILE *fp, char *buf)
259f928ce6ceastha    /* fp - current input file */
260f928ce6ceastha    /* buf - whatever followed "x X InlinePicture" */
2617c478bdstevel@tonic-gate{
2627c478bdstevel@tonic-gate    char	*tname;			/* temp file pathname - for *fp_pic */
2637c478bdstevel@tonic-gate    char	name[100];		/* picture file pathname */
2647c478bdstevel@tonic-gate    long	total;			/* and size - both from *buf */
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate
2677c478bdstevel@tonic-gate/*
2687c478bdstevel@tonic-gate *
2697c478bdstevel@tonic-gate * Adds an in-line picture file to the end of temporary file *fp_pic. All pictures
2707c478bdstevel@tonic-gate * grabbed from the input file are saved in the same temp file. Each is preceeded
2717c478bdstevel@tonic-gate * by a one line header that includes the original picture file pathname and the
2727c478bdstevel@tonic-gate * size of the picture in bytes. The in-line picture file is opened for update,
2737c478bdstevel@tonic-gate * left open, and unlinked so it disappears when we do.
2747c478bdstevel@tonic-gate *
2757c478bdstevel@tonic-gate */
2767c478bdstevel@tonic-gate
2777c478bdstevel@tonic-gate
2787c478bdstevel@tonic-gate    if ( fp_pic == NULL )  {
2797c478bdstevel@tonic-gate	if ( (tname = tempnam(TEMPDIR, "dpost")) == NULL )
2807c478bdstevel@tonic-gate	    error(FATAL, "can't generate in-line picture file name");
2817c478bdstevel@tonic-gate	if ( (fp_pic = fopen(tname, "w+")) == NULL )
2827c478bdstevel@tonic-gate	    error(FATAL, "can't open in-line picture file %s", tname);
2837c478bdstevel@tonic-gate	unlink(tname);
2847c478bdstevel@tonic-gate    }	/* End if */
2857c478bdstevel@tonic-gate
2867c478bdstevel@tonic-gate    if ( sscanf(buf, "%s %ld", name, &total) != 2 )
2877c478bdstevel@tonic-gate	error(FATAL, "in-line picture error");
2887c478bdstevel@tonic-gate
2897c478bdstevel@tonic-gate    fseek(fp_pic, 0L, 2);
2907c478bdstevel@tonic-gate    fprintf(fp_pic, "%s %ld\n", name, total);
2917c478bdstevel@tonic-gate    getc(fp);
2927c478bdstevel@tonic-gate    fflush(fp_pic);
2937c478bdstevel@tonic-gate    piccopy(fp, fp_pic, total);
2947c478bdstevel@tonic-gate    ungetc('\n', fp);
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate}   /* End of inlinepic */
2977c478bdstevel@tonic-gate
2987c478bdstevel@tonic-gate
2997c478bdstevel@tonic-gate/*****************************************************************************/
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate
302f928ce6ceasthastatic void
303f928ce6ceasthapiccopy(FILE *fp_in, FILE *fp_out, long total)
304f928ce6ceastha    /* fp_in - input */
305f928ce6ceastha    /* fp_out - and output file pointers */
306f928ce6ceastha    /* total - number of bytes to be copied */
3077c478bdstevel@tonic-gate{
3087c478bdstevel@tonic-gate    long	i;			/* loop index */
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate/*
3127c478bdstevel@tonic-gate *
3137c478bdstevel@tonic-gate * Copies total bytes from file fp_in to fp_out. Used to append picture files to
3147c478bdstevel@tonic-gate * *fp_pic and then copy them to yet another temporary file immediately before
3157c478bdstevel@tonic-gate * they're used (in picture()).
3167c478bdstevel@tonic-gate *
3177c478bdstevel@tonic-gate */
3187c478bdstevel@tonic-gate
3197c478bdstevel@tonic-gate
3207c478bdstevel@tonic-gate    for ( i = 0; i < total; i++ )
3217c478bdstevel@tonic-gate	if ( putc(getc(fp_in), fp_out) == EOF )
3227c478bdstevel@tonic-gate	    error(FATAL, "error copying in-line picture file");
3237c478bdstevel@tonic-gate    fflush(fp_out);
3247c478bdstevel@tonic-gate
3257c478bdstevel@tonic-gate}   /* End of piccopy */
326