1 /*
2  * TransFig: Facility for Translating Fig code
3  * Copyright (c) 1991 by Micah Beck
4  * Parts Copyright (c) 1985-1988 by Supoj Sutanthavibul
5  * Parts Copyright (c) 1989-2002 by Brian V. Smith
6  *
7  * Any party obtaining a copy of these files is granted, free of charge, a
8  * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
9  * nonexclusive right and license to deal in this software and
10  * documentation files (the "Software"), including without limitation the
11  * rights to use, copy, modify, merge, publish and/or distribute copies of
12  * the Software, and to permit persons who receive copies from any such
13  * party to do so, with the only requirement being that this copyright
14  * notice remain intact.
15  *
16  */
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include "patchlevel.h"
22 #include <math.h>
23 #include <sys/file.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 #include "pi.h"
32 
33 /* location for temporary files */
34 #define TMPDIR "/tmp"
35 
36 typedef char Boolean;
37 #define	NO	2
38 #define	False	0
39 #define	True	1
40 
41 #define DEFAULT_FONT_SIZE 11
42 
43 #ifndef MIN
44 #define MIN(A, B) ((A) < (B) ? (A) : (B))
45 #define MAX(A, B) ((A) > (B) ? (A) : (B))
46 #endif
47 
48 #ifdef USE_INLINE
49 #define INLINE inline
50 #else
51 #define INLINE
52 #endif /* USE_INLINE */
53 
54 /* include ctype.h for isascii() and isxdigit() macros */
55 #include <ctype.h>
56 
57 #ifndef X_NOT_STDC_ENV
58 #  include <string.h>
59 #else
60 #  ifdef SYSV
61 #    include <string.h>
62 #  else
63 #    include <strings.h>
64 #    ifndef strchr
65 #      define strchr index
66 #    endif
67 #    ifndef strrchr
68 #      define strrchr rindex
69 #    endif
70 #  endif  /* SYSV else */
71 #endif  /* !X_NOT_STDC_ENV else */
72 
73 #if defined(hpux) || defined(SYSV) || defined(SVR4)
74 #include <sys/types.h>
75 #define bzero(s,n) memset((s),'\0',(n))
76 #endif
77 
78 extern	double	atof();
79 
80 #define round(x)	((int) ((x) + ((x >= 0)? 0.5: -0.5)))
81 
82 #define	NUM_STD_COLS	32
83 #define	MAX_USR_COLS	512
84 
85 #define NUMSHADES	21
86 #define NUMTINTS	20
87 #define NUMPATTERNS     22
88 
89 #ifndef RGB_H
90 #define RGB_H
91 typedef struct _RGB {
92 	unsigned short red, green, blue;
93 	} RGB;
94 #endif /* RGB_H */
95 
96 /*
97  * Device driver interface structure
98  */
99 struct driver {
100  	void (*option)();	/* interpret driver-specific options */
101   	void (*start)();	/* output file header */
102   	void (*grid)();		/* draw grid */
103 	void (*arc)();		/* object generators */
104 	void (*ellipse)();
105 	void (*line)();
106 	void (*spline)();
107 	void (*text)();
108 	int (*end)();		/* output file trailer (returns status) */
109   	int text_include;	/* include text length in bounding box */
110 #define INCLUDE_TEXT 1
111 #define EXCLUDE_TEXT 0
112 };
113 
114 extern float	rgb2luminance();
115 extern void	put_msg(char *fmt, ...);
116 extern void	unpsfont();
117 extern void	print_comments();
118 extern int	lookup_X_color();
119 
120 extern char	Err_badarg[];
121 extern char	Err_mem[];
122 
123 extern char	*PSfontnames[];
124 extern int	PSisomap[];
125 
126 extern char	*prog, *from, *to;
127 extern char	*name;
128 extern double	font_size;
129 extern Boolean	correct_font_size;	/* use correct font size */
130 extern double	mag, fontmag;
131 extern FILE	*tfp;
132 
133 extern double	ppi;		/* Fig file resolution (e.g. 1200) */
134 extern int	llx, lly, urx, ury;
135 extern Boolean	landscape;
136 extern Boolean	center;
137 extern Boolean	multi_page;	/* multiple page option for PostScript */
138 extern Boolean	overlap;	/* overlap pages in multiple page output */
139 extern Boolean	orientspec;	/* true if the command-line args specified land or port */
140 extern Boolean	centerspec;	/* true if the command-line args specified -c or -e */
141 extern Boolean	magspec;	/* true if the command-line args specified -m */
142 extern Boolean	transspec;	/* set if the user specs. the GIF transparent color */
143 extern Boolean	paperspec;	/* true if the command-line args specified -z */
144 extern Boolean  boundingboxspec;/* true if the command-line args specified -B or -R */
145 extern Boolean	multispec;	/* true if the command-line args specified -M */
146 extern Boolean	metric;		/* true if the file contains Metric specifier */
147 extern char	gif_transparent[]; /* GIF transp color hex name (e.g. #ff00dd) */
148 extern char	papersize[];	/* paper size */
149 extern char     boundingbox[];  /* boundingbox */
150 extern float	THICK_SCALE;	/* convert line thickness from screen res. */
151 extern char	lang[];		/* selected output language */
152 extern char	*Fig_color_names[]; /* hex names for Fig colors */
153 extern RGB	background;	/* background (if specified by -g) */
154 extern Boolean	bgspec;		/* flag to say -g was specified */
155 extern float	grid_minor_spacing; /* grid minor spacing (if any) */
156 extern float	grid_major_spacing; /* grid major spacing (if any) */
157 extern char	gscom[];	/* to build up a command for ghostscript */
158 extern Boolean	psencode_header_done; /* if we have already emitted PSencode header */
159 extern Boolean	transp_header_done;   /* if we have already emitted transparent image header */
160 extern Boolean	grayonly;	/* convert colors to grayscale (-N option) */
161 
162 struct paperdef
163 {
164     char *name;			/* name for paper size */
165     int width;			/* paper width in points */
166     int height;			/* paper height in points */
167 };
168 
169 #define NUMPAPERSIZES 29
170 extern struct paperdef paperdef[];
171 
172 /* user-defined colors */
173 typedef		struct{
174 			int c,r,g,b;
175 			}
176 		User_color;
177 
178 extern User_color	user_colors[MAX_USR_COLS];
179 extern int		user_col_indx[MAX_USR_COLS];
180 extern int		num_usr_cols;
181 extern Boolean		pats_used, pattern_used[NUMPATTERNS];
182 
183 extern void	gendev_null();
184 extern void	gs_broken_pipe();
185 
186 /* for GIF files */
187 #define	MAXCOLORMAPSIZE 256
188 
189 struct Cmap {
190 	unsigned short red, green, blue;
191 	unsigned long pixel;
192 };
193 
194 /* define PATH_MAX if not already defined */
195 /* taken from the X11R5 server/os/osfonts.c file */
196 #ifndef X_NOT_POSIX
197 #ifdef _POSIX_SOURCE
198 #include <limits.h>
199 #else
200 #if !defined(sun) || defined(sparc)
201 #define _POSIX_SOURCE
202 #include <limits.h>
203 #undef _POSIX_SOURCE
204 #endif /* !defined(sun) || defined(sparc) */
205 #endif /* _POSIX_SOURCE */
206 #endif /* X_NOT_POSIX */
207 
208 #ifndef PATH_MAX
209 #include <sys/param.h>
210 #ifdef MAXPATHLEN
211 #define PATH_MAX MAXPATHLEN
212 #else
213 #define PATH_MAX 1024
214 #endif /* MAXPATHLEN */
215 #endif /* PATH_MAX */
216 
217 #if ( !defined(__NetBSD__) && !defined(__DARWIN__) && !defined(__FreeBSD__) )
218 extern int		sys_nerr, errno;
219 #endif
220 
221 #if ( !(defined(BSD) && (BSD >= 199306)) && !defined(__NetBSD__) && \
222 	!defined(__GNU_LIBRARY__) && !defined(__FreeBSD__) && \
223 	!defined(__GLIBC__) && !defined(__CYGWIN__) && !defined(__DARWIN__))
224 	    extern char *sys_errlist[];
225 #endif
226 
227 typedef struct _point
228 {
229     int x,y;
230 } Point;
231 
232