source: tags/initial/fig2dev/fig2dev.c

Last change on this file was 2, checked in by oldcode, 23 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
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
39extern int my_getopt();
40extern char *optarg;
41extern int optind;
42
43#define DEFAULT_FONT_SIZE 11
44
45struct driver *dev = NULL;
46
47char            Usage[] = "Usage: %s [-L language] [-f font] [-s size] [-m scale] [input [output]]\n";
48char            Err_badarg[] = "Argument -%c unkown to %s driver.";
49char            Err_incomp[] = "Incomplete %s object at line %d.";
50char            Err_mem[] = "Running out of memory.";
51
52char            *prog;
53char            *from = NULL, *to = NULL;
54int             font_size = 0;
55double          mag = 1.0;
56FILE            *tfp = NULL;
57int             llx = 0, lly = 0, urx = 0, ury = 0;
58
59struct obj_rec {
60        void (*gendev)();
61        char *obj;
62        int depth;
63};
64
65put_msg(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
66char   *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
72get_args(argc, argv)
73int      argc;
74char    *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
140main(argc, argv)
141int      argc;
142char    *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 */
173static int compound_dump(com, array, count, dev)
174F_compound *com;
175struct obj_rec *array;
176int count;
177struct 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
231gendev_objects(objects, dev)
232F_compound      *objects;
233struct 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
280int rec_comp(r1, r2)
281struct obj_rec *r1, *r2;
282{
283        return (r2->depth - r1->depth);
284}
285
286/* null operation */
287void gendev_null() {};
Note: See TracBrowser for help on using the repository browser.