1 | /* |
---|
2 | * TransFig: Facility for Translating Fig code |
---|
3 | * Copyright (c) 1985 Supoj Sutantavibul |
---|
4 | * Copyright (c) 1991 Micah Beck |
---|
5 | * |
---|
6 | * Permission to use, copy, modify, distribute, and sell this software and its |
---|
7 | * documentation for any purpose is hereby granted without fee, provided that |
---|
8 | * the above copyright notice appear in all copies and that both that |
---|
9 | * copyright notice and this permission notice appear in supporting |
---|
10 | * documentation. The authors make no representations about the suitability |
---|
11 | * of this software for any purpose. It is provided "as is" without express |
---|
12 | * or implied warranty. |
---|
13 | * |
---|
14 | * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
---|
15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
---|
16 | * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
---|
17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
---|
18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
---|
19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
---|
20 | * PERFORMANCE OF THIS SOFTWARE. |
---|
21 | * |
---|
22 | */ |
---|
23 | |
---|
24 | /* |
---|
25 | * Fig2dev : General Fig code translation program |
---|
26 | * |
---|
27 | */ |
---|
28 | #if defined(hpux) || defined(SYSV) |
---|
29 | #include <sys/types.h> |
---|
30 | #endif |
---|
31 | #include <sys/file.h> |
---|
32 | #include <stdio.h> |
---|
33 | #include <ctype.h> |
---|
34 | #include "patchlevel.h" |
---|
35 | #include "object.h" |
---|
36 | #include "fig2dev.h" |
---|
37 | #include "drivers.h" |
---|
38 | |
---|
39 | extern int my_getopt(); |
---|
40 | extern char *optarg; |
---|
41 | extern int optind; |
---|
42 | |
---|
43 | #define DEFAULT_FONT_SIZE 11 |
---|
44 | |
---|
45 | struct driver *dev = NULL; |
---|
46 | |
---|
47 | char Usage[] = "Usage: %s [-L language] [-f font] [-s size] [-m scale] [input [output]]\n"; |
---|
48 | char Err_badarg[] = "Argument -%c unkown to %s driver."; |
---|
49 | char Err_incomp[] = "Incomplete %s object at line %d."; |
---|
50 | char Err_mem[] = "Running out of memory."; |
---|
51 | |
---|
52 | char *prog; |
---|
53 | char *from = NULL, *to = NULL; |
---|
54 | int font_size = 0; |
---|
55 | double mag = 1.0; |
---|
56 | FILE *tfp = NULL; |
---|
57 | int llx = 0, lly = 0, urx = 0, ury = 0; |
---|
58 | |
---|
59 | struct obj_rec { |
---|
60 | void (*gendev)(); |
---|
61 | char *obj; |
---|
62 | int depth; |
---|
63 | }; |
---|
64 | |
---|
65 | put_msg(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) |
---|
66 | char *format, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6, *arg7, *arg8; |
---|
67 | { |
---|
68 | fprintf(stderr, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); |
---|
69 | fprintf(stderr, "\n"); |
---|
70 | } |
---|
71 | |
---|
72 | get_args(argc, argv) |
---|
73 | int argc; |
---|
74 | char *argv[]; |
---|
75 | { |
---|
76 | int c, i; |
---|
77 | double atof(); |
---|
78 | |
---|
79 | prog = *argv; |
---|
80 | /* add :? */ |
---|
81 | while ((c = my_getopt(argc, argv, "acd:f:l:L:m:Pp:s:S:vVwW?")) != EOF) { |
---|
82 | |
---|
83 | /* generic option handling */ |
---|
84 | switch (c) { |
---|
85 | |
---|
86 | case 'V': |
---|
87 | fprintf(stderr, "TransFig Version %s Patchlevel %s\n", |
---|
88 | VERSION, PATCHLEVEL); |
---|
89 | exit(0); |
---|
90 | break; |
---|
91 | |
---|
92 | case 'L': /* set output language */ |
---|
93 | for (i=0; *drivers[i].name; i++) |
---|
94 | if (!strcmp(optarg, drivers[i].name)) |
---|
95 | dev = drivers[i].dev; |
---|
96 | if (!dev) { |
---|
97 | fprintf(stderr, |
---|
98 | "Unknown graphics language %s\n", optarg); |
---|
99 | fprintf(stderr,"Known languages are:\n"); |
---|
100 | /* display available languages - 23/01/90 */ |
---|
101 | for (i=0; *drivers[i].name; i++) |
---|
102 | fprintf(stderr,"%s ",drivers[i].name); |
---|
103 | fprintf(stderr,"\n"); |
---|
104 | exit(1); |
---|
105 | } |
---|
106 | break; |
---|
107 | |
---|
108 | case 's': /* set default font size */ |
---|
109 | font_size = atoi(optarg); |
---|
110 | break; |
---|
111 | |
---|
112 | case 'm': /* set magnification */ |
---|
113 | mag = atof(optarg); |
---|
114 | break; |
---|
115 | |
---|
116 | case '?': /* usage */ |
---|
117 | fprintf(stderr,Usage,prog); |
---|
118 | exit(1); |
---|
119 | } |
---|
120 | |
---|
121 | /* pass options through to driver */ |
---|
122 | if (!dev) { |
---|
123 | fprintf(stderr, "No graphics language specified.\n"); |
---|
124 | exit(1); |
---|
125 | } |
---|
126 | dev->option(c, optarg); |
---|
127 | } |
---|
128 | if (!dev) { |
---|
129 | fprintf(stderr, "No graphics language specified.\n"); |
---|
130 | exit(1); |
---|
131 | } |
---|
132 | |
---|
133 | /* default font size is scaled if not specified */ |
---|
134 | if (!font_size) font_size = DEFAULT_FONT_SIZE*mag + 0.5; |
---|
135 | |
---|
136 | if (optind < argc) from = argv[optind++]; /* from file */ |
---|
137 | if (optind < argc) to = argv[optind]; /* to file */ |
---|
138 | } |
---|
139 | |
---|
140 | main(argc, argv) |
---|
141 | int argc; |
---|
142 | char *argv[]; |
---|
143 | { |
---|
144 | F_compound objects; |
---|
145 | int status; |
---|
146 | |
---|
147 | get_args(argc, argv); |
---|
148 | |
---|
149 | if (from) |
---|
150 | status = read_fig(from, &objects); |
---|
151 | else /* read from stdin */ |
---|
152 | status = readfp_fig(stdin, &objects); |
---|
153 | |
---|
154 | if (status != 0) { |
---|
155 | if (from) read_fail_message(from, status); |
---|
156 | exit(1); |
---|
157 | } |
---|
158 | |
---|
159 | if (to == NULL) |
---|
160 | tfp = stdout; |
---|
161 | else if ((tfp = fopen(to, "w")) == NULL) { |
---|
162 | fprintf(stderr, "Couldn't open %s", to); |
---|
163 | fprintf(stderr, Usage, prog); |
---|
164 | exit(1); |
---|
165 | } |
---|
166 | |
---|
167 | gendev_objects(&objects, dev); |
---|
168 | if (tfp != stdout) (void)fclose(tfp); |
---|
169 | exit(0); |
---|
170 | } |
---|
171 | |
---|
172 | /* count primitive objects & create pointer array */ |
---|
173 | static int compound_dump(com, array, count, dev) |
---|
174 | F_compound *com; |
---|
175 | struct obj_rec *array; |
---|
176 | int count; |
---|
177 | struct driver *dev; |
---|
178 | { |
---|
179 | F_arc *a; |
---|
180 | F_compound *c; |
---|
181 | F_ellipse *e; |
---|
182 | F_line *l; |
---|
183 | F_spline *s; |
---|
184 | F_text *t; |
---|
185 | |
---|
186 | for (c = com->compounds; c != NULL; c = c->next) |
---|
187 | count = compound_dump(c, array, count, dev); |
---|
188 | for (a = com->arcs; a != NULL; a = a->next) { |
---|
189 | if (array) { |
---|
190 | array[count].gendev = dev->arc; |
---|
191 | array[count].obj = (char *)a; |
---|
192 | array[count].depth = a->depth; |
---|
193 | } |
---|
194 | count += 1; |
---|
195 | } |
---|
196 | for (e = com->ellipses; e != NULL; e = e->next) { |
---|
197 | if (array) { |
---|
198 | array[count].gendev = dev->ellipse; |
---|
199 | array[count].obj = (char *)e; |
---|
200 | array[count].depth = e->depth; |
---|
201 | } |
---|
202 | count += 1; |
---|
203 | } |
---|
204 | for (l = com->lines; l != NULL; l = l->next) { |
---|
205 | if (array) { |
---|
206 | array[count].gendev = dev->line; |
---|
207 | array[count].obj = (char *)l; |
---|
208 | array[count].depth = l->depth; |
---|
209 | } |
---|
210 | count += 1; |
---|
211 | } |
---|
212 | for (s = com->splines; s != NULL; s = s->next) { |
---|
213 | if (array) { |
---|
214 | array[count].gendev = dev->spline; |
---|
215 | array[count].obj = (char *)s; |
---|
216 | array[count].depth = s->depth; |
---|
217 | } |
---|
218 | count += 1; |
---|
219 | } |
---|
220 | for (t = com->texts; t != NULL; t = t->next) { |
---|
221 | if (array) { |
---|
222 | array[count].gendev = dev->text; |
---|
223 | array[count].obj = (char *)t; |
---|
224 | array[count].depth = t->depth; |
---|
225 | } |
---|
226 | count += 1; |
---|
227 | } |
---|
228 | return count; |
---|
229 | } |
---|
230 | |
---|
231 | gendev_objects(objects, dev) |
---|
232 | F_compound *objects; |
---|
233 | struct driver *dev; |
---|
234 | { |
---|
235 | F_arc *a; |
---|
236 | F_compound *c; |
---|
237 | F_ellipse *e; |
---|
238 | F_line *l; |
---|
239 | F_spline *s; |
---|
240 | F_text *t; |
---|
241 | |
---|
242 | int obj_count, rec_comp(); |
---|
243 | struct obj_rec *rec_array, *r; |
---|
244 | |
---|
245 | if (0 == (double)objects->nwcorner.x) { |
---|
246 | fprintf(stderr, "Resolution is zero!! default to 80 ppi\n"); |
---|
247 | objects->nwcorner.x = 80; |
---|
248 | } |
---|
249 | if (objects->nwcorner.y != 1 && objects->nwcorner.y != 2) { |
---|
250 | fprintf(stderr, "Wrong coordinate system; cannot continue\n"); |
---|
251 | return; |
---|
252 | } |
---|
253 | |
---|
254 | /* Compute bounding box of objects, supressing texts if indicated */ |
---|
255 | compound_bound(objects, &llx, &lly, &urx, &ury, dev->text_include); |
---|
256 | |
---|
257 | /* dump object pointers to an array */ |
---|
258 | obj_count = compound_dump(objects, 0, 0, dev); |
---|
259 | if (!obj_count) { |
---|
260 | fprintf(stderr, "No object"); |
---|
261 | return; |
---|
262 | } |
---|
263 | rec_array = (struct obj_rec *)malloc(obj_count*sizeof(struct obj_rec)); |
---|
264 | (void)compound_dump(objects, rec_array, 0, dev); |
---|
265 | |
---|
266 | /* sort object array by depth */ |
---|
267 | qsort(rec_array, obj_count, sizeof(struct obj_rec), rec_comp); |
---|
268 | |
---|
269 | /* generate header */ |
---|
270 | (*dev->start)(objects); |
---|
271 | |
---|
272 | /* generate objects in sorted order */ |
---|
273 | for (r = rec_array; r<rec_array+obj_count; r++) |
---|
274 | (*(r->gendev))(r->obj); |
---|
275 | |
---|
276 | /* generate trailer */ |
---|
277 | (*dev->end)(); |
---|
278 | } |
---|
279 | |
---|
280 | int rec_comp(r1, r2) |
---|
281 | struct obj_rec *r1, *r2; |
---|
282 | { |
---|
283 | return (r2->depth - r1->depth); |
---|
284 | } |
---|
285 | |
---|
286 | /* null operation */ |
---|
287 | void gendev_null() {}; |
---|