File: genmf.c

package info (click to toggle)
transfig 1%3A3.2.1-8
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,588 kB
  • ctags: 2,063
  • sloc: ansic: 15,652; makefile: 1,166; sh: 38; csh: 10
file content (285 lines) | stat: -rw-r--r-- 7,143 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * TransFig: Facility for Translating Fig code
 *
 *  Copyright (c) 1993 Anthony Starks (ajs@merck.com)
 *
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * The X Consortium, and any party obtaining a copy of these files from
 * the X Consortium, directly or indirectly, is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and
 * documentation files (the "Software"), including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons who receive
 * copies from any such party to do so, with the only requirement being
 * that this copyright notice remain intact.  This license includes without
 * limitation a license to do the foregoing actions under any patents of
 * the party supplying this software to the X Consortium.
 */

/*
 *  fig2MF -- convert fig to mfpic METAFONT code
 *
 *  Copyright (c) 1993 Anthony Starks (ajs@merck.com)
 *
 *  Version 0.00 -- 	Incorporate Tobin's small suggestions
 *  Version 0.01 -- 	Change scaling to inches,
 *			default [x-y]scale is 1/8 inch
 *			slight format changes in cinit()
 *  Version 0.02 --	Fixed pen switching bug
 *  Version 0.03 --	Support new arcthree mode
 *  Version 0.04 --	Token support for text
 *  Version 0.05 --	Integrated into fig2dev - B.V.Smith 11/28/94
 *  Version 0.06 --	Fixed a number of bugs.
 			Converted to mfpic 0.2.10.8 alfa
				/M.Bengtsson July 1, 1998
 */

/* TODO: 
 *  - Dashed and dotted lines
 *  - Arrow heads
 *  - Filled polylines and open splines
 *  - Difference between interpolated and non-interpolated splines
 *  - ...
 */

#include <stdio.h>
#include "fig2dev.h"
#include "object.h"

#define dofill(obj)	1.2-((double)obj->fill_style/(double)BLACK_FILL)
#define VERSION		0.06
#define DEF_PEN		0.5
#define PEN_INCR	0.10
#define INFTY		1e5
typedef struct {
	char *keyword;
	double *value;
} Options;

static double ppi;
static double code = 32.0;
static int oldpen = 0;
static double penscale = 1;
static double xscale = 0.125;
static double yscale = 0.125;
static double xl = 0.0;
static double xu = 8.0;
static double yl = 0.0;
static double yu = 8.0;
static double maxy = INFTY;

void
setpen(thickness)
int thickness;
{
  if (thickness != oldpen) {
    fprintf(tfp, "penwd := %.2lfpt;\n", 
	    thickness/THICK_SCALE*penscale*yscale*mag);
    fprintf(tfp, "drawpen := pencircle scaled penwd yscaled aspect_ratio;\n");
    oldpen = thickness;
  }
}

void
genmf_start(objects)
F_compound	*objects;
{
	int	curchar;

	if (maxy == INFTY) 
		maxy = yu;
	
	ppi = objects->nwcorner.x;
	curchar = (int)code;

	fprintf(tfp,"%%\n%% fig2dev -L mf version %.2lf --- Preamble\n%%\n",
		VERSION);
	fprintf(tfp,"mag:=1; input grafbase.mf; code:=%g;\n",
		code);
	fprintf(tfp,"interim hdwdr:=1; interim hdten:=1;\n");

	fprintf(tfp,"%%\n%% %s (char %d)\n%%\n",
		(name? name: ((from) ? from : "stdin")), ++curchar);
	fprintf(tfp,"xscale:=%.3lf; yscale:=%.3lf;\n", xscale*mag, yscale*mag);
	fprintf(tfp,"bounds(%.3lf,%.3lf,%.3lf,%.3lf);\n", xl, xu, yl, yu);
	fprintf(tfp,"unitlen:=1.0in#;\n");
	fprintf(tfp,"beginmfpic(incr code);\n");
	setpen(1);
}


void
genmf_end()
{
	fprintf(tfp,"endmfpic;\nend.\n");
}

void
genmf_option(opt, optarg)
char opt;
char *optarg;
{
    switch (opt) {
	case 'C':
	    code = atof(optarg);
	    break;
	case 'n':
	    name = optarg;
	    break;
	case 'p':
	    penscale = atof(optarg);
	    break;
	case 't':
	    maxy = atof(optarg);
	    break;
	case 'x':
	    xl = atof(optarg);
	    break;
	case 'y':
	    yl = atof(optarg);
	    break;
	case 'X':
	    xu = atof(optarg);
	    break;
	case 'Y':
	    yu = atof(optarg);
	    break;

    }
}

void
genmf_line(l)
F_line *l;
{
	F_point	*p;
	setpen(l->thickness);
	fprintf(tfp, "  store (curpath)\n");
	if ((l->type==1) && (l->fill_style<0)) /* Open polyline */
		fprintf(tfp,"  drawn polyline(false)\n");
	else {	
  /* Closed and/or filled polygon, cover underlying figures 
   * first with white if this polygon is shaded */
		if (l->fill_style == BLACK_FILL)
			fprintf(tfp,"  filled ");
		else if (l->fill_style < BLACK_FILL && l->fill_style > 0)
			fprintf(tfp,"  shade(%lfpt) unfilled", dofill(l));
		else if (l->fill_style == 0)
			fprintf(tfp,"  drawn unfilled ");
		else 
			fprintf(tfp,"  drawn ");
		fprintf(tfp,"polyline(true)\n");
	}
	p = l->points;
	fprintf(tfp,"      ((%lf, %lf)", p->x/ppi, maxy-(p->y/ppi));
	p = p->next;
	for ( ; p != NULL; p=p->next) {
	    fprintf(tfp,",\n       (%lf, %lf)", p->x/ppi, maxy-(p->y/ppi));
	}
	fprintf(tfp, ");\n");
	return;
}


void
genmf_spline(s)
F_spline *s;
{
	F_point	*p;
	setpen(s->thickness);
	fprintf(tfp, "  store (curpath)\n");
	if ((s->type == 0) || (s->type == 2) && (s->fill_style < 0)) /* Open spline */
		fprintf(tfp,"  drawn curve(false)\n");
	else { /* Closed and/or filled spline, see comment above */
		if (s->fill_style == BLACK_FILL)
			fprintf(tfp,"  filled ");
		else if (s->fill_style < BLACK_FILL && s->fill_style > 0)
			fprintf(tfp,"  shade(%lfpt) unfilled ", dofill(s));
		else if (s->fill_style == 0)
			fprintf(tfp,"  drawn unfilled ");
		else
			fprintf(tfp,"  drawn ");
		fprintf(tfp,"curve(true)\n");
	}
	p = s->points;
	fprintf(tfp,"      ((%lf, %lf)", p->x/ppi, maxy-(p->y/ppi));
	p = p->next;
	for ( ; p != NULL; p=p->next) {
	    fprintf(tfp,",\n       (%lf, %lf)", p->x/ppi, maxy-(p->y/ppi));
	}
	fprintf(tfp, ");\n");
	return;
}


void
genmf_ellipse(e)
F_ellipse *e;
{
        setpen(e->thickness);
	fprintf(tfp, "  store (curpath)\n");
	if (e->fill_style == BLACK_FILL)
		fprintf(tfp,"  filled ");
	else if (e->fill_style < BLACK_FILL && e->fill_style > 0)
		fprintf(tfp,"  shade(%lfpt) ", dofill(e));
	else
		fprintf(tfp,"  drawn ");
	if (e->type == 3 || e->type == 4)
	{
		fprintf(tfp,"circle((%lf,%lf),%lf);\n",
			e->center.x/ppi, maxy-(e->center.y/ppi), e->radiuses.x/ppi);
	}
	else if (e->type == 1 || e->type == 2)
	{
		fprintf(tfp,"ellipse((%lf,%lf),%lf,%lf,0);\n",
			e->center.x/ppi, maxy-(e->center.y/ppi), 
			e->radiuses.x/ppi, e->radiuses.y/ppi);
	}

}

void
genmf_arc(a)
F_arc *a;
{

	setpen(a->thickness);
	fprintf(tfp, "  store (curpath)\n");
	fprintf(tfp,"  drawn arcppp((%lf,%lf), (%lf,%lf), (%lf,%lf));\n",
		a->point[0].x/ppi, maxy-(a->point[0].y/ppi),
		a->point[1].x/ppi, maxy-(a->point[1].y/ppi),
		a->point[2].x/ppi, maxy-(a->point[2].y/ppi));

	return;

}

void
genmf_text(t)
F_text *t;
{
	fprintf(stderr, "warning: text ignored in MF output: `%s'\n",
		t->cstring);
	return;
}


struct driver dev_mf = {
     	genmf_option,
	genmf_start,
	genmf_arc,
	genmf_ellipse,
	genmf_line,
	genmf_spline,
	genmf_text,
	genmf_end,
	INCLUDE_TEXT
};