1 /*
2    Copyright (c) 1991 - 1993 Heinz W. Werntges.  All rights reserved.
3    Distributed by Free Software Foundation, Inc.
4 
5 This file is part of HP2xx.
6 
7 HP2xx is distributed in the hope that it will be useful, but
8 WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
9 to anyone for the consequences of using it or for whether it serves any
10 particular purpose or works at all, unless he says so in writing.  Refer
11 to the GNU General Public License, Version 2 or later, for full details.
12 
13 Everyone is granted permission to copy, modify and redistribute
14 HP2xx, but only under the conditions described in the GNU General Public
15 License.  A copy of this license is supposed to have been
16 given to you along with HP2xx so you can know your rights and
17 responsibilities.  It should be in a file named COPYING.  Among other
18 things, the copyright notice and this notice must be preserved on all
19 copies.
20 
21 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22 */
23 
24 /** to_mf.c: 	Converter to Metafont and misc. TeX formats
25  **
26  ** 91/01/19  V 1.00  HWW  Derived from HPtoGF.c
27  ** 91/02/10  V 1.01  HWW  "zaehler" removed
28  ** 91/02/15  V 1.02  HWW  stdlib.h supported
29  ** 91/02/18  V 1.03  HWW  some int's changed to float's
30  ** 91/06/09  V 1.04  HWW  New options added; some simplifications done
31  ** 91/10/15  V 1.05  HWW  ANSI_C
32  ** 91/11/20  V 1.06  HWW  Many changes for "SPn;" support
33  ** 92/01/13  V 1.06a HWW  debugged
34  ** 92/02/27  V 1.07b HWW  TeX modes added (epic, emTeX specials)
35  ** 92/05/17  V 1.07c HWW  Output to stdout if outfile == '-'
36  ** 92/05/19  V 1.07d HWW  Warning if color mode
37  ** 92/12/10  V 1.08a HWW  CAD (TeXcad) mode added
38  ** 92/12/12  V 1.08b HWW  Info line now interprets outfile=='-' as "stdout"
39  **/
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include "bresnham.h"
44 #include "hp2xx.h"
45 
46 
47 
48 extern	float	xmin, xmax, ymin, ymax;
49 
50 
51 
to_mftex(PAR * p,FILE * td,int mode)52 void	to_mftex (PAR *p, FILE *td, int mode)
53 {
54 PlotCmd		cmd;
55 HPGL_Pt		pt1;
56 float		coord2mm;
57 FILE		*md;
58 int		pensize, chars_out = 0, max_chars_out = 210, np = 1;
59 char		*ftype, *scale_cmd, *pen_cmd,
60 		*poly_start, *poly_next, *poly_last, *poly_end,
61 		*draw_dot, *exit_cmd;
62 
63   switch (mode)
64   {
65     case 0:	/* Metafont mode	*/
66 	ftype		= "METAFONT";
67 	scale_cmd	= "mode_setup;\nbeginchar(\"Z\",%4.3fmm#,%4.3fmm#,0);\n";
68 	pen_cmd		= "pickup pencircle scaled 0.%1dmm;\n";
69 	poly_start	= "draw(%4.3fmm,%4.3fmm)";
70 	poly_next	= "--(%4.3fmm,%4.3fmm)";
71 	poly_last	= "--(%4.3fmm,%4.3fmm);\n";
72 	poly_end	= ";\n";
73 	draw_dot	= "drawdot(%4.3fmm,%4.3fmm);\n";
74 	exit_cmd	= "endchar;\nend;\n";
75 	break;
76     case 1:	/* TeX (em-Specials) mode	*/
77 	ftype		= "emTeX-specials";
78 	scale_cmd	= "\\unitlength1mm\n\\begin{picture}(%4.3f,%4.3f)\n";
79 	pen_cmd		= "\\special{em:linewidth 0.%1dmm}\n";
80 	poly_start	= "\\put(%4.3f,%4.3f){\\special{em:moveto}}\n";
81 	poly_next	= "\\put(%4.3f,%4.3f){\\special{em:lineto}}\n";
82 	poly_last	= poly_next;
83 	poly_end	= "";
84 	draw_dot	= "\\put(%4.3f,%4.3f){\\makebox(0,0)[cc]{.}}\n";
85 	exit_cmd	= "\\end{picture}\n";
86 	break;
87     case 2:	/* TeX (epic) mode	*/
88 	ftype		= "TeX (epic)";
89 	scale_cmd	= "\\unitlength1mm\n\\begin{picture}(%4.3f,%4.3f)\n";
90 	pen_cmd		= "\\linethickness{0.%1dmm}\n";
91 	poly_start	= "\\drawline(%4.3f,%4.3f)";
92 	poly_next	= "(%4.3f,%4.3f)";
93 	poly_last	= "(%4.3f,%4.3f)\n";
94 	poly_end	= "\n";
95 	draw_dot	= "\\put(%4.3f,%4.3f){\\picsquare}\n";
96 	exit_cmd	= "\\end{picture}\n";
97 	break;
98     case 3:	/* TeXcad (\emline-Macros) mode	*/
99 	ftype		= "TeXcad compatible";
100 	scale_cmd	= "\\unitlength=1mm\n\\begin{picture}(%4.3f,%4.3f)\n";
101 	pen_cmd		= "\\special{em:linewidth 0.%1dmm}\n\\linethickness{ 0.%1dmm}\n";
102 	poly_start	= "\\emline{%4.3f}{%4.3f}{%d}";
103 	poly_next	= "{%4.3f}{%4.3f}{%d}\n";
104 	poly_last	= poly_next;
105 	poly_end	= "";
106 	draw_dot	= "\\put(%4.3f,%4.3f){\\makebox(0,0)[cc]{.}}\n";
107 	exit_cmd	= "\\end{picture}\n";
108 	break;
109   }
110 
111   if (!p->quiet)
112 	fprintf(stderr,"\n\n- Writing %s code to \"%s\"\n", ftype,
113 		*p->outfile == '-' ? "stdout" : p->outfile);
114 
115   if (p->is_color)
116 	fprintf(stderr, "\nWARNING: MF/TeX modes ignore colors!\n");
117 
118   if (*p->outfile != '-')
119   {
120 	if ((md = fopen(p->outfile, "w")) == NULL)
121 	{
122 		perror("hp2xx (mf/tex)");
123 		exit(ERROR);
124 	}
125   }
126   else
127 	md = stdout;
128 
129   fprintf(md,"%% %s code in %s, created by hp2xx\n",
130 	ftype, p->outfile);
131   fprintf(md, scale_cmd, p->width, p->height);
132 
133   pensize = p->pensize[p->pen];
134   if (pensize != 0)
135 	if (mode == 3)
136 		fprintf(md, pen_cmd, pensize, pensize);
137 	else
138 		fprintf(md, pen_cmd, pensize);
139 
140   /* Factor transforming the coordinate values into millimeters: */
141   coord2mm = p->height / (ymax-ymin);
142 
143 
144   while ((cmd = PlotCmd_from_tmpfile()) != EOF)
145 	switch (cmd)
146 	{
147 	  case NOP:
148 		break;
149 
150 	  case SET_PEN:
151 		if ((p->pen = fgetc(td)) == EOF)
152 		{
153 			perror("Unexpected end of temp. file: ");
154 			exit (ERROR);
155 		}
156 		pensize = p->pensize[p->pen];
157 		if (pensize != 0)
158 		{
159 			if (chars_out)	/* Finish up old polygon */
160 			{
161 				fprintf(md, poly_end);
162 				chars_out = 0;
163 			}
164 			if (mode == 3)
165 				fprintf(md, pen_cmd, pensize, pensize);
166 			else
167 				fprintf(md, pen_cmd, pensize);
168 		}
169 		break;
170 
171 	  case MOVE_TO:
172 		HPGL_Pt_from_tmpfile (&pt1);
173 		if (pensize == 0 || mode == 3)
174 			break;
175 		if (chars_out)		/* Finish up old polygon */
176 			fprintf(md, poly_end);
177 		chars_out =  fprintf(md, poly_start,
178 			(pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
179 		break;
180 
181 	  case DRAW_TO:
182 		if (mode == 3)	/* Needs special treatment: no polygons!	*/
183 		{
184 			chars_out =  fprintf(md, poly_start,
185 			  (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm, np++);
186 			HPGL_Pt_from_tmpfile (&pt1);
187 			chars_out += fprintf(md, poly_next,
188 			  (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm, np++);
189 			break;
190 		}
191 
192 		HPGL_Pt_from_tmpfile (&pt1);
193 		if (pensize == 0)
194 			break;
195 		if (chars_out > max_chars_out)
196 					/* prevent overlong lines */
197 		{
198 			fprintf(md, poly_last,
199 			  (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
200 			chars_out =  fprintf(md, poly_start,
201 			  (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
202 		} else
203 			chars_out += fprintf(md, poly_next,
204 			  (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
205 		break;
206 
207 	  case PLOT_AT:
208 		HPGL_Pt_from_tmpfile (&pt1);
209 		if (chars_out)		/* Finish up old polygon */
210 		{
211 			fprintf(md, poly_end);
212 			chars_out = 0;
213 		}
214 		if (pensize == 0)
215 			break;
216 		fprintf(md, draw_dot,
217 			(pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
218 		break;
219 
220 	  default:
221 		fprintf(stderr,"Illegal cmd in temp. file!");
222 		exit (ERROR);
223 	}
224 
225 
226   if (chars_out)			/* Finish up old polygon */
227   {
228 	fprintf(md, poly_end);
229 	chars_out = 0;
230   }
231 
232   fprintf(md, exit_cmd);		/* Add file trailer	*/
233 
234   if (md != stdout)
235 	fclose(md);
236 
237   if (!p->quiet)
238 	fputc ('\n', stderr);
239 }
240