1 /*
2    Copyright (c) 1991 - 1994 Heinz W. Werntges.  All rights reserved.
3    Parts Copyright (c) 1998-2002,2003 Martin Kroeker  All rights reserved.
4 
5    Distributed by Free Software Foundation, Inc.
6 
7 This file is part of HP2xx.
8 
9 HP2xx is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
11 to anyone for the consequences of using it or for whether it serves any
12 particular purpose or works at all, unless he says so in writing.  Refer
13 to the GNU General Public License, Version 2 or later, for full details.
14 
15 Everyone is granted permission to copy, modify and redistribute
16 HP2xx, but only under the conditions described in the GNU General Public
17 License.  A copy of this license is supposed to have been
18 given to you along with HP2xx so you can know your rights and
19 responsibilities.  It should be in a file named COPYING.  Among other
20 things, the copyright notice and this notice must be preserved on all
21 copies.
22 
23 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
24 */
25 
26 /** hp2xx : A converter of HPGL files into popular raster and vector formats
27  **
28  **	xx =  mf (MetaFont), eps (PostScript), pcl (HP-PCL Level3),
29  **	      pre(view), pcx (PaintBrush-Format), img (GEM),
30  **	      pic (ATARI bitmap), pbm (Portable Bitmap), ...
31  **
32  ** (c) 1992, 1993  Heinz W. Werntges and the HP2xx team
33  **
34  ** Author:
35  **
36  **	HWW	Heinz W. Werntges  (werntges@convex.rz.uni-duesseldorf.de)
37  **		Fruchtstr. 2,  40223 Duesseldorf, Germany
38  **
39  ** New maintainer since version 3.3 and (c) 1999:
40  **     MK      Dr. Martin Kroeker (mk@daveg.com,
41  **                                 martin@ruby.chemie.uni-freiburg.de)
42  **             Zum Markwald 6, D-63165 Muehlheim, Germany
43  **
44  ** Amiga stuff & PBM & ILBM by:
45  **
46  **	CHL	Claus H. Langhans (Claus_Langhans@wildcat.fido.de)
47  **		Unter den Nussbaeumen 19, 65779 Kelkheim (Ts.), Germany
48  **
49  **
50  ** Atari stuff & IMG by:
51  **
52  **	NM	Norbert Meyer (sorry - no email)
53  **		Reinhold-Schneider-Str. 83, 40595 Duesseldorf
54  **
55  **	AS	Andreas Schwab (schwab@ls5.informatik.uni-dortmund.de)
56  **
57  **	JE	Joern Eggers (II040EG@DTHAC11.bitnet)
58  **
59  ** OS/2 Full-Screen previewer by:
60  **
61  **	HSz	Host Szillat (szillat@iaas-berlin.d400.de)
62  **
63  ** X11 previewer by:
64  **
65  **	MSch	Michael Schoene (michael@nero.uni-bonn.de)
66  **		Karolinger Str. 81,  40223 Duesseldorf, Germany
67  **
68  **
69  ** Many thanks to all the other fine people who supported HP2xx by testing,
70  ** correcting, porting, suggesting improvements, and otherwise spending
71  ** their time on HP2xx, especially:
72  **
73  ** -- Michael Schmitz
74  **  & Gerhard Steger 	 for their invaluable help with VAX/VMS,
75  ** -- Juergen Gross     for access to a HP 9000 machine
76  ** -- Roland Emmerich   for proofreading, beta-testing, and his HGC support
77  ** -- Jon Gillian       for valuable suggestions
78  ** -- Lawrence Lowe     for various improvements and contributions
79  **-------------------------------------------------------------------------
80  **/
81 
82 /** hp2xx.c:
83  **
84  ** 91/01/12  V 1.00  HWW  PC version
85  ** 91/01/17  V 1.01  HWW  Disk swapping added
86  ** 91/01/19  V 1.02  HWW  Reorganized: mf mode revived, pcx mode added
87  ** 91/01/29  V 1.03  HWW  SUN portation; HP-specials added
88  ** 91/01/31  V 1.04  HWW  (-F) FF added
89  ** 91/02/01  V 1.05  HWW  Flag -S added (Deskjet specials)
90  ** 91/02/15  V 1.06  HWW  stdlib.h & VAX_C supported
91  ** 91/02/19  V 2.01  HWW  file unchanged, but new package release!
92  ** 91/06/09  V 2.10  HWW  (beta) More options; options x & y changed to o & O
93  ** 91/06/15  V 2.11  HWW  (beta) VGA option added; PCX bug (on SUN) removed
94  ** 91/06/20  V 2.12  HWW  Rotate (-r) option & Copyright added
95  ** 91/06/20  V 2.12b HWW  Minor reformatting
96  ** 91/06/29  V 2.13a HWW  PostScript support
97  ** 91/08/28  V 2.14  HWW  ATARI 32K format added (prelim.)
98  ** 91/10/15  V 2.14c HWW  atari & stad formats; hwtools.h eliminated
99  ** 91/10/20  V 2.14d HWW  ATARI-->pic (packing removed)
100  ** 91/10/24  V 2.15  HWW  ATARI-->pic stable now; "LT;" & "LT0;" supported
101  ** 91/11/22  V 2.16b HWW  "SPn;" support
102  ** 92/01/12  V 2.17  HWW  HPGL scanner improved, to_ps() debugged, ps --> eps,
103  **			  More portabel code (ATARI acknowledged)
104  ** 92/02/06  V 2.18e HWW  Better parser, IMG & various previews supported,
105  **			  preview control via -h, -w; -W removed!
106  ** 92/02/21  V 2.19d HWW  LB etc. supported, PG; (option -P) added
107  ** 92/03/03  V 2.20c HWW  Modes added: -m epic,em,pac(ATARI) / LB bugs fixed
108  ** 92/04/15  V 2.20d HWW  Width & height treated symmetrically now
109  ** 92/04/28  V 2.20e HWW  -m pre: -d, -D effective again, default 75; PBM added
110  ** 92/05/02  V 2.21a HWW  -m pre is default now. New opt: -l logfile
111  ** 92/05/19  V 3.00c HWW  Color support (VGA preview), multiple input files
112  ** 92/05/25  V 3.00f HWW  Color support (more modes)
113  ** 92/06/09  V 3.01d HWW  XT,YT,TL,SM supported; ATARI update acknowledged;
114  **			   Mode list added for easier expansion; debugged
115  ** 92/10/20  V 3.02a HWW  LT now fully supported
116  ** 92/11/08  V 3.02b HWW  Page range allowed
117  ** 92/12/10  V 3.02c HWW  Mode "cad" added; DJ version: bug fix extended;
118  ** 92/12/12  V 3.02d HWW  2 bug fixes for option -f
119  ** 92/12/16  V 3.03a HWW  Option -t (+truesize) added
120  ** 92/12/27  V 3.03b HWW  -S int (for Deskjet color support)
121  ** 93/01/02  V 3.10a HWW  Source brushed up; ready for distribution
122  ** 93/02/08  V 3.12a HWW  Support of Joern Eggers' cs mode added (ATARI only)
123  **			   HAS_X11, HAS_SUNVIEW: added/modified
124  ** 93/04/13  V 3.12b HWW  UC added (A. Treindl), small changes; CMYK added,
125  **			   New previewer selection scheme // NEW RELEASE 3.1.2
126  ** 93/04/25  V 3.12c HWW  Minor bug fixes (to_pcl.c, hpgl.c / Eric)
127  ** 93/05/20  V 3.12d HWW  LT1 adjustment; '--version' option added (GNU std)
128  ** 93/07/20  V 3.12e HWW  SilentWait() modified (UNIX) according to A. Bagge,
129  **			   new OS/2 previewer by HSz, TIFF compression for PCL mode
130  ** 93/09/03  V 3.13a HWW  Wait() renamed to NormalWait() (name collision on VAX),
131  **			   Option -P : Option parser modified; -m hpgl added
132  ** 93/09/22  V 3.13b HWW  Fix in autoset_outfile_name()
133  ** 94/01/01  V 3.14a HWW  Additions by L. Lowe
134  ** 94/02/14  V 3.20b HWW  Re-structured to facilitate multiple user interfaces
135  ** Separate additions to HWW's V 3.20, assembled in this release  by MK:
136  ** 94/03/23          EB   Mode "gpt", Gnuplot, added by Emmanuel Bigler
137  ** 94/08/08          IMP  Mode "xfig" added by Ian MacPhedran
138  ** 97/11/29          MS   Mode "png" added by Michael Schmitz
139  ** 99/09/01  V 3.30a MK   CA,CS,IW,SA,SS,NP,PC,PW,PE and charsets1-8 added
140  ** 99/09/12  V3.30a2 MK   RO,PS,EW added, bugfixes for IW,PC,PW,EA
141  ** 99/10/01  V 3.30  MK   RO/PS changes
142  ** 99/12/01  V 3.31  MK   fixes for RO,PS,LB,DI,DR, completed PE
143  ** 00/02/06          MK   fixes for scaling and PE; allow overriding of PC/PW
144  ** 00/02/12  V 3.32  MK   Mode "tiff" added by M.Liberi (changed to use ZIP
145  **                        instead of patented LZW compression; needs libtiff)
146  ** 00/02/26          MK   Mode "escp" (Epson Esc/P2 printer language)
147  ** 02/..	      MK   Modes "dxf" and "svg"
148  ** 02/12/20		   New mode "nc", G-code for CNC milling with
149  **			   constant, user-defined Z depths (Michael Rooke)
150  ** 03/02/26	      GV   Add -S option for DXF mode (pen attribute mapping)
151  **/
152 
153 
154 #include <stdio.h>
155 #include <stdlib.h>
156 #include <string.h>
157 #include <ctype.h>
158 #include "bresnham.h"
159 #include "pendef.h"
160 #include "hp2xx.h"
161 #include "hpgl.h"
162 
163 /* the version string is now declared in hp2xx.h, so that output modules
164    may easily include the version of hp2xx that generated the file */
165 
166 /**
167  ** When adding your special mode, add a line here.
168  ** Please note the alphabetical order (and keep it).
169  ** Also see the definition of "hp2xx_mode" in hp2xx.h.
170  **/
171 
172 mode_list ModeList[] = {
173 	{XX_CAD, "cad"},	/* LaTeX: TeXcad compatible output      */
174 #ifdef	ATARI
175 	{XX_CS, "cs"},		/* LaTeX using \special{...} for C. Strunk's TeX        */
176 #endif
177 	{XX_DXF, "dxf"},	/* AutoCAD DXF format                   */
178 	{XX_EM, "em"},		/* LaTeX using \special{em:...}         */
179 #ifdef EMF
180 	{XX_EMF, "emf"},	/* Microsoft Enhanced Metafile          */
181 #ifndef UNIX
182 	{XX_EMP, "emp"},	/* Microsoft EMF printing               */
183 #endif
184 #endif
185 	{XX_EPIC, "epic"},	/* LaTeX using epic.sty macros          */
186 	{XX_EPS, "eps"},	/* Encapulated PostScript               */
187 #ifdef EPSON
188 	{XX_ESC2, "esc2"},	/* Epson Esc/P2 printer language       */
189 #endif
190 	{XX_FIG, "fig"},	/* FIG 3.1 Drawing Files                */
191 	{XX_GPT, "gpt"},	/* gnuplot vector ascii format          */
192 	{XX_HPGL, "hpgl"},	/* Simplified HP-GL                     */
193 #ifdef	AMIGA
194 	{XX_ILBM, "ilbm"},	/* Special AMIGA format                 */
195 #endif
196 	{XX_IMG, "img"},	/* Digital Research IMG raster format   */
197 #ifdef	JPG
198 	{XX_JPG, "jpg"},	/* Joint Photography Expert Group JPEG  */
199 #endif
200 	{XX_MF, "mf"},		/* Metafont source output               */
201 	{XX_NC, "nc"},		/* G code for CNC milling               */
202 	{XX_PBM, "pbm"},	/* Portable Bitmap                      */
203 	{XX_PCL, "pcl"},	/* HP-PCL Level 5 printer code          */
204 	{XX_PCX, "pcx"},	/* Paintbrush's PCX raster format       */
205 #ifdef	PIC_PAC
206 	{XX_PAC, "pac"},	/* for ATARI, e.g. used by StaD         */
207 	{XX_PIC, "pic"},	/* for ATARI. Try to replace by IMG     */
208 #endif
209 #ifdef USEPDF
210 	{XX_PDF, "pdf"},	/* Portable Document Format       */
211 #endif
212 #ifdef PNG
213 	{XX_PNG, "png"},	/* Portable Network Graphics            */
214 #endif
215 	{XX_PRE, "pre"},	/* DEFAULT: Preview on screen           */
216 	{XX_RGIP, "rgip"},	/* Uniplex RGIP vector format           */
217 	{XX_SVG, "svg"},	/* W3C Scalable vector graphics         */
218 #ifdef TIF
219 	{XX_TIFF, "tiff"},	/* Tagged image file format            */
220 #endif
221 	{XX_TERM, ""}		/* Dummy: List terminator               */
222 };
223 
224 
225 
print_supported_modes(void)226 void print_supported_modes(void)
227 {
228 	int i;
229 
230 	Eprintf("%s", ModeList[0].modestr);
231 	for (i = 1; ModeList[i].mode != XX_TERM; i++)
232 		Eprintf(",%s", ModeList[i].modestr);
233 	Eprintf("\n");
234 }
235 
236 
237 
238 
Send_version(void)239 void Send_version(void)
240 {
241 	Eprintf("\n%s\n%s\n%s\n%s\n%s\n",
242 		"hp2xx is free software and you are welcome to distribute copies of it",
243 		"  under certain conditions. There is absolutely no warranty for hp2xx!",
244 		"For full details, read file COPYING (shipped along with this package),",
245 		"  or write to:\t\tFree Software Foundation, Inc.",
246 		"\t\t\t675 Mass Ave, Cambridge, MA 02139, USA");
247 
248 	Eprintf("\n%s\tV %s  (%s)   %s\n%s",
249 		"HP2xx:\tA HPGL converter (xx = mf, eps, pcl, pcx, img ...)\n",
250 		VERS_NO, VERS_DATE, VERS_COPYRIGHT, VERS_ADDITIONS);
251 }
252 
253 
254 
usage_msg(const GEN_PAR * pg,const IN_PAR * pi,const OUT_PAR * po)255 void usage_msg(const GEN_PAR * pg, const IN_PAR * pi, const OUT_PAR * po)
256 {
257 	if (pg->quiet)
258 		return;
259 
260 	Send_version();
261 
262 #ifdef ATARI
263 	Eprintf
264 	    ("Usage:\tdouble-click on HP2XX.TTP, type parameters into\n");
265 	Eprintf("\tcommand-line with following syntax:\n");
266 	Eprintf("\t[options] [hpglfile]\n");
267 	Eprintf
268 	    ("\n\t(if command-line offers not enough space for all parameters\n");
269 	Eprintf("\tuse a command-line interpreter for starting HP2xx)\n");
270 #else
271 	Eprintf("Usage:\thp2xx [options] [file1 [file2 ...]]\n");
272 #endif				/* ATARI */
273 
274 	Eprintf
275 	    ("\tUnix: Filter usage (.. | hp2xx -q -f- [options] | ..) ok\n");
276 	NormalWait();
277 
278 	Eprintf("\nOpt fmt   defaults\tComment:\n");
279 	Eprintf
280 	    ("---------------------------------------------------------------------------\n");
281 	Eprintf
282 	    ("-m strg   %s\t\tMode. Valid mode strings for this particular binary are:\n\t\t\t",
283 	     pg->mode);
284 	print_supported_modes();
285 
286 	Eprintf
287 	    ("-n        %s\t\tno filling of polygons (ignore FP commands)\n",
288 	     FLAGSTATE(pg->nofill));
289 	Eprintf
290 	    ("-N        %s\t\tno honoring of papersize (PS) requests\n\t\t\t (plotsize calculated from data instead)\n",
291 	     FLAGSTATE(pg->no_ps));
292 	Eprintf
293 	    ("-M int	\t\tremap all drawing commands using pen 0 to this pen\n");
294 	Eprintf
295 	    ("-f strg   (auto gen.)\tName of output file ('-' = to stdout)\n");
296 	Eprintf("-l strg   (stderr)\tName of log file\n");
297 	Eprintf
298 	    ("-p strg   %1d%1d%1d%1d%1d%1d%1d%1d\tPensize(s) of pen 1 to 8 (in 1/10 mm).\n",
299 	     (unsigned) (10 * pt.width[1]), (unsigned) (10 * pt.width[2]),
300 	     (unsigned) (10 * pt.width[3]), (unsigned) (10 * pt.width[4]),
301 	     (unsigned) (10 * pt.width[5]), (unsigned) (10 * pt.width[6]),
302 	     (unsigned) (10 * pt.width[7]), (unsigned) (10 * pt.width[8]));
303 	Eprintf("\t\t\t\"strg\" must consist of 1 to 8 digits '0'-'9'\n");
304 	Eprintf("\t\t\tfirst digit = pen1 ... last digit = pen 8\n");
305 	Eprintf("\t\t\t(default settings shown)\n");
306 	Eprintf
307 	    ("-c strg   %1d%1d%1d%1d%1d%1d%1d%1d\tPen color(s) (default settings shown).\n",
308 	     pt.color[1], pt.color[2], pt.color[3], pt.color[4],
309 	     pt.color[5], pt.color[6], pt.color[7], pt.color[8]);
310 	Eprintf
311 	    ("\t\t\t0 = off, 1 = black, 2 = red, 3 = green, 4 = blue,\n");
312 	Eprintf("\t\t\t5 = cyan, 6 = magenta, 7 = yellow\n");
313 	Eprintf("-P n:n    %d:%d\t\tPage range (0:0 = all pages).\n",
314 		pi->first_page, pi->last_page);
315 	Eprintf("-q        %s\t\tQuiet mode (no diagnostics)\n",
316 		FLAGSTATE(pg->quiet));
317 	Eprintf
318 	    ("-r float%5.1f\t\tRotation angle [deg]. -r90 = landscape\n",
319 	     pi->rotation);
320 	Eprintf("-s strg   %s\tName of swap file\n", pg->swapfile);
321 	NormalWait();
322 	Eprintf("\nBitmap controls:\n");
323 	Eprintf("-d int    %d\t\tDPI value for x or x&y, if -D unused.\n",
324 		po->dpi_x);
325 	Eprintf("-D int    %d\t\tDPI value for y ONLY\n", po->dpi_x);
326 	/* x, not y! */
327 	Eprintf("-e int    %d\t\tExtra clipping space\n", pg->extraclip);
328 
329 	Eprintf("\nPCL-exclusive options:\n");
330 	Eprintf("-i         %s\tPre-initialize printer\n",
331 		FLAGSTATE(po->init_p));
332 	Eprintf("-I         %s\tInitialize using PCL3GUI commands\n",
333 		FLAGSTATE(po->init_p3gui));
334 	Eprintf("-F         %s\tSend a FormFeed at end\n",
335 		FLAGSTATE(po->formfeed));
336 	Eprintf
337 	    ("-S int     %d\tUse Deskjet special commands (0=off, 1=B/W, 3=CMY, 4=CMYK)\n",
338 	     po->specials);
339 	Eprintf
340 	    ("NOTE:    \tOnly valid for -d: 300/150/100/75; -D invalid!\n");
341 
342 	Eprintf("\nPCL / PostScript / Preview options:\n");
343 	Eprintf("-o float %5.1f\tX offset [mm] of picture\n", pi->xoff);
344 	Eprintf("-O float %5.1f\tY offset [mm] of picture\n", pi->yoff);
345 	Eprintf
346 	    ("-C           \tFit picture into center of (-a/-h/-w) rectangle\n");
347 
348 	Eprintf("\nTiff-exclusive options:\n");
349 	Eprintf
350 	    ("-S int     %d\tUse Tiff Compression Format (0/1=None, 2=RLE, 3=G3Fax, 4=G4Fax, 5=LZW, 6=OJpeg, 7=Jpeg, 8=Deflate)\n",
351 	     po->specials);
352 
353 	NormalWait();
354 
355 	Eprintf("\nDXF-exclusive options:\n");
356 	Eprintf
357 	    ("-S int     %d\tMap pens to DXF colors (0=No mapping, 1=use pen no., 2=use width*10 , 3=map widths <0.2 to 1, <0.3 to 2,0.4 to 3 , above 0.4 to 4 )\n",
358 	     po->specials);
359 
360 	Eprintf("\nSize controls:\n");
361 
362 	Eprintf
363 	    ("-a float %5.1f\tAspect factor (x/y correction). Valid: > 0.0\n",
364 	     pi->aspectfactor);
365 	Eprintf("-h float %5.1f\tHeight [mm] of picture\n", pi->height);
366 	Eprintf("-w float %5.1f\tWidth  [mm] of picture\n", pi->width);
367 	Eprintf
368 	    ("-t         %s\tShow true HPGL size. Disables -a -h -w !\n",
369 	     FLAGSTATE(pi->truesize));
370 	Eprintf("-H float %5.1f\tHardware X range [1/40 mm] of device\n",
371 		pi->hwlimit.x);
372 	Eprintf("-W float %5.1f\tHardware Y range [1/40 mm] of device\n",
373 		pi->hwlimit.y);
374 	Eprintf("-x float   -\tManual HPGL-coord range presetting: x0\n");
375 	Eprintf("-X float   -\tManual HPGL-coord range presetting: x1\n");
376 	Eprintf("-y float   -\tManual HPGL-coord range presetting: y0\n");
377 	Eprintf("-Y float   -\tManual HPGL-coord range presetting: y1\n");
378 	Eprintf("-z float %5.1f\t(3d nc output only) Z engage depth\n",
379 		po->zengage);
380 	Eprintf("-Z float %5.1f\t(3d nc output only) Z retract depth\n",
381 		po->zretract);
382 
383 #ifdef DOS
384 	Eprintf
385 	    ("\n-V int   %d\tVGA mode byte (decimal). Change at own risk!\n",
386 	     po->vga_mode);
387 #endif
388 
389 	NormalWait();
390 
391 	Eprintf("Corresponding long options:\n\n");
392 	Eprintf
393 	    ("hp2xx   [--mode] [--colors] [--pensizes] [--pages] [--quiet]\n");
394 	Eprintf
395 	    ("\t[--nofill] [--no_ps] [--mapzero]\n");
396 	Eprintf
397 	    ("\t[--width] [--height] [--aspectfactor] [--truesize]\n");
398 	Eprintf("\t[--x0] [--x1] [--y0] [--y1]\n");
399 	Eprintf("\t[--xoffset] [--yoffset] [--center]\n");
400 	Eprintf("\t[--DPI] [--DPI_x] [--DPI_y] [--extraclip]\n");
401 	Eprintf("\t[--outfile] [--logfile] [--swapfile]\n");
402 	Eprintf
403 	    ("\t[--PCL_formfeed] [--PCL_init] [--PCL_Deskjet] [--PCL_PCL3GUI]\n");
404 #ifdef DOS
405 	Eprintf("\t[--VGAmodebyte]");
406 #endif
407 	Eprintf("\t[--help] [--version]\n");
408 
409 
410 #ifdef PURE_C
411 	Eprintf("\nPress RETURN key\n");
412 	getchar();
413 #endif
414 }
415 
416 
417 
418 
reset_par(IN_PAR * pi)419 void reset_par(IN_PAR * pi)
420 /**
421  ** Reset some parameter struct elements which may have been changed
422  ** by action() to their defaults
423  **/
424 {
425 	pi->x0 = 1e10;		/* HP7550A's range is about     */
426 	pi->x1 = -1e10;		/* [-2^24, 2^24], so we're safe */
427 	pi->y0 = 1e10;
428 	pi->y1 = -1e10;
429 }
430 
431 
432 
433 
preset_par(GEN_PAR * pg,IN_PAR * pi,OUT_PAR * po)434 void preset_par(GEN_PAR * pg, IN_PAR * pi, OUT_PAR * po)
435 /**
436  ** Pre-set constant parameter struct elements with reasonable defaults
437  **/
438 {
439 	int i;
440 
441 	pi->aspectfactor = 1.0;
442 	pi->center_mode = FALSE;
443 	pi->height = 200.0;
444 	pi->width = 200.0;
445 	pi->xoff = 0.0;
446 	pi->yoff = 0.0;
447 	pi->truesize = FALSE;
448 	pi->hwcolor = FALSE;
449 	pi->hwsize = FALSE;
450 	pi->rotation = 0.0;
451 	pi->in_file = "";
452 	pi->hd = NULL;
453 	pi->first_page = 0;
454 	pi->last_page = 0;
455 
456 	pi->hwlimit.x = 33600.;
457 	pi->hwlimit.y = 47520.;
458 
459 	po->init_p = FALSE;
460 	po->init_p3gui = FALSE;
461 	po->formfeed = FALSE;
462 	po->specials = 0;
463 	po->dpi_x = 75;
464 	po->dpi_y = 0;
465 	po->vga_mode = 18;	/* 0x12: VGA 640x480, 16 colors */
466 	po->picbuf = NULL;
467 	po->outfile = "";
468 	po->zengage = -1.;
469 	po->zretract = 1.;
470 
471 	pg->logfile = "";
472 	pg->swapfile = "hp2xx.swp";
473 	pg->mode = "pre";
474 	pg->td = NULL;
475 	pg->xx_mode = XX_PRE;
476 	pg->nofill = FALSE;
477 	pg->no_ps = FALSE;
478 	pg->quiet = FALSE;
479 	pg->extraclip = 0;
480 	pg->maxpensize = 0.1;	/* 1/10 mm              */
481 	pg->maxcolor = 1;	/* max. color index             */
482 	pg->maxpens = 8;
483 	pg->is_color = FALSE;
484 	pg->mapzero = -1;
485 
486 	pt.width[0] = 0.0;	/* 1/10 mm              */
487 	pt.color[0] = xxBackground;
488 	for (i = 1; i <= NUMPENS; i++) {
489 		pt.width[i] = 0.1;	/* 1/10 mm              */
490 		pt.color[i] = xxForeground;
491 	}
492 	pt.color[1] = xxForeground;
493 	pt.color[2] = xxRed;
494 	pt.color[3] = xxGreen;
495 	pt.color[4] = xxBlue;
496 	pt.color[5] = xxCyan;
497 	pt.color[6] = xxMagenta;
498 	pt.color[7] = xxYellow;
499 	set_color_rgb(xxBackground, 255, 255, 255);
500 	set_color_rgb(xxForeground, 0, 0, 0);
501 	set_color_rgb(xxRed, 255, 0, 0);
502 	set_color_rgb(xxGreen, 0, 255, 0);
503 	set_color_rgb(xxBlue, 0, 0, 255);
504 	set_color_rgb(xxCyan, 0, 255, 255);
505 	set_color_rgb(xxMagenta, 255, 0, 255);
506 	set_color_rgb(xxYellow, 255, 255, 0);
507 
508 	reset_par(pi);
509 }
510 
511 
512 
513 
Send_Copyright(void)514 void Send_Copyright(void)
515 /**
516  **	Remnant of older (non-GNU) releases. Leave here if you like
517  **/
518 {
519 /*
520 static	unsigned char msg[] =
521 	{0xaf,0xa8,0xcd,0xd5,0x97,0xdd,0xdd,0x9f,
522 	 0x85,0x8d,0xc6,0x8c,0x85,0xed,0x8b,0x85,
523 	 0xf2,0xc0,0xd7,0xcb,0xd1,0xc2,0xc0,0xd6,
524 	 0xaf,0xa8,0xa5};
525 unsigned char	*p;
526 
527   p = msg;
528   while (*p!=0xa5)
529 	Eprintf("%c", (*p++ ^ 0xa5));
530   exit  (COPYNOTE);
531 */
532 	exit(-1);
533 }
534 
535 
536 
537 
autoset_outfile_name(const char * mode,const char * in_name,char ** outfile)538 void autoset_outfile_name(const char *mode, const char *in_name,
539 			  char **outfile)
540 {
541 	int len, i;
542 
543 	if (**outfile == '-')	/* If output explicitly to stdout:          */
544 		return;		/*    then nothing's to do here                 */
545 
546 	if (isascii(**outfile) && strlen(*outfile) > 0) {	/* If this looks like an output file name:     */
547 		return;		/*    Just accept it! Add validity check later? */
548 	}
549 	if (*in_name == '-')	/* If input from stdin                              */
550 		len = 0;
551 	else
552 		len = (int) strlen(in_name);
553 
554 	if (len == 0) {		/* If input from stdin:                           *//*    then supply a default file name           */
555 		*outfile = "hp2xx.out";
556 		return;
557 	}
558 
559 	for (i = len - 1; i; i--)	/* Search for (last) '.' char in path               */
560 		if (in_name[i] == '.')
561 			break;
562 	i++;
563 
564 /**
565  ** We can assume a valid inp_name file name here.
566  ** If not, the following fopen() in main() will fail
567  ** and no harm will be done by an incorrect output file name.
568  **/
569 /*  if ((*outfile = malloc(len+2+strlen(mode))) == NULL)*/
570 	if ((*outfile = realloc(*outfile, len + 2 + strlen(mode))) == NULL) {
571 		Eprintf("Error: No mem for output file name!\n");
572 		PError("autoset_outfile_name");
573 		exit(ERROR);
574 	}
575 	strcpy(*outfile, in_name);
576 
577 	if (strcmp(mode, "pre") == 0)
578 		return;		/* If preview mode:                             */
579 	/*    then file name is used only for window title */
580 
581 	if (i == 1 || len - i > 3) {	/* No or non-DOS extension: Add mode string       */
582 		strcat(*outfile, ".");
583 		strcat(*outfile, mode);	/* Mode string is extension!    */
584 	} else
585 		strcpy(*outfile + i, mode);	/* Replace extension    */
586 }
587 
588 
589 
590 /**************************************************************************
591  **
592  ** cleanup_x ():
593  **
594  ** Call these functions to close & remove the temp. and input file
595  **	as well as to free the raster picture buffer.
596  ** Calling is ok even if the buffer is already freed or the files
597  **	are already closed, so just call them when in doubt.
598  **/
599 
cleanup_g(GEN_PAR * pg)600 void cleanup_g(GEN_PAR * pg)
601 {
602 	if (pg != NULL && pg->td != NULL) {
603 		fclose(pg->td);
604 		pg->td = NULL;
605 	}
606 #if defined(DOS) && defined (GNU)
607 /**
608  ** GNU libc.a (DJ's DOS port) bug fix:
609  **/
610 	unlink("hp2xx.$$$");
611 #endif
612 }
613 
614 
cleanup_i(IN_PAR * pi)615 void cleanup_i(IN_PAR * pi)
616 {
617 	if (pi != NULL && pi->hd != NULL) {
618 		if (pi->hd != stdin)
619 			fclose(pi->hd);
620 		pi->hd = NULL;
621 	}
622 }
623 
624 
cleanup_o(OUT_PAR * po)625 void cleanup_o(OUT_PAR * po)
626 {
627 	if (po != NULL && po->picbuf != NULL) {
628 		free_PicBuf(po->picbuf);
629 		po->picbuf = NULL;
630 	}
631 }
632 
633 
634 
635 
cleanup(GEN_PAR * pg,IN_PAR * pi,OUT_PAR * po)636 void cleanup(GEN_PAR * pg, IN_PAR * pi, OUT_PAR * po)
637 {
638 	cleanup_g(pg);
639 	cleanup_i(pi);
640 	cleanup_o(po);
641 }
642 
643 
644 
645 
646 /**************************************************************************
647  **
648  ** HPGL_to_TMP ():
649  **
650  ** This call opens a single HP-GL input file, scans and interprets
651  ** its commands, and writes elementary move/draw commands into
652  ** a temporary file.
653  **	The input file is closed after returning, but the temp. file
654  ** is kept open. You may re-use it multiple times. Close it finally!
655  **	Calling this function invalidates later processing stages like
656  ** the picture buffer.
657  **/
658 
HPGL_to_TMP(GEN_PAR * pg,IN_PAR * pi)659 int HPGL_to_TMP(GEN_PAR * pg, IN_PAR * pi)
660 {
661   /**
662    ** Clean up previous leftovers (if any)
663    **/
664 
665 	cleanup_g(pg);
666   /**
667    ** Open HP-GL input file. Use stdin if selected.
668    **/
669 
670 	if (*pi->in_file == '-')
671 		pi->hd = stdin;
672 	else if (pi->hd == NULL) {
673 		if ((pi->hd = fopen(pi->in_file, READ_BIN)) == NULL) {
674 			PError("hp2xx (while opening HPGL file)");
675 			return ERROR;
676 		}
677 	}
678   /**
679    ** Open temporary intermediate file.
680    **
681    ** GNU libc.a (DJ's DOS port) bug fix (part 1 of 2):
682    **	 tmpfile() does not seem to work!
683    ** See code below for part 2/2 (removing hp2xx.$$$).
684    ** NOTE:
685    **	If program terminates abnormally, delete hp2xx.$$$ manually!!
686    **/
687 
688 #if defined(DOS) && defined (GNU)
689 	if ((pg->td = fopen("hp2xx.$$$", "w+b")) == NULL)
690 #elif defined(AMIGA)
691 	if ((pg->td = fopen("t:hp2xx.tmp", "w+b")) == NULL)
692 #else
693 	if ((pg->td = tmpfile()) == NULL)
694 #endif	/** !DOS && GNU	**/
695 	{
696 		PError("hp2xx -- opening temporary file");
697 		return ERROR;
698 	}
699 
700   /**
701    ** Convert HPGL data into compact temporary binary file, and obtain
702    ** scaling data (xmin/xmax/ymin/ymax in plotter coordinates)
703    **/
704 	n_commands = 0;
705 	read_HPGL(pg, pi);
706 	if (n_commands <= 1 && n_commands >= 0) {
707 		if (pi->hd != stdin) {
708 			fclose(pi->hd);
709 			pi->hd = NULL;
710 		}
711 		return ERROR;
712 	}
713 	return 0;
714 }
715 
716 
717 
718 /**************************************************************************
719  **
720  ** TMP_to_VEC ():
721  **
722  ** Vector mode conversions.
723  **	Call any number of times as long as the temp. file is opened.
724  **
725  ** Returns:
726  **	ERROR	if error
727  **	1	if mode not found
728  **	0 	if successfully processed
729  **/
730 
TMP_to_VEC(const GEN_PAR * pg,const OUT_PAR * po)731 int TMP_to_VEC(const GEN_PAR * pg, const OUT_PAR * po)
732 {
733 	if (pg->td == NULL)
734 		return ERROR;
735 	rewind(pg->td);		/* Rewind temp file for re-reading      */
736 	if (n_commands < 0)
737 		return 0;
738 
739 	switch (pg->xx_mode) {
740 	case XX_MF:
741 		to_mftex(pg, po, 0);
742 		return 0;
743 
744 	case XX_EM:
745 		to_mftex(pg, po, 1);
746 		return 0;
747 
748 	case XX_EPIC:
749 		to_mftex(pg, po, 2);
750 		return 0;
751 
752 	case XX_CAD:
753 		to_mftex(pg, po, 3);
754 		return 0;
755 
756 #ifdef	ATARI
757 	case XX_CS:
758 		to_mftex(pg, po, 4);
759 		return 0;
760 #endif
761 
762 #ifdef EMF			/*BAF */
763 	case XX_EMF:
764 		to_emf(pg, po);
765 		return 0;
766 #ifndef UNIX
767 	case XX_EMP:
768 		to_emp(pg, po);
769 		return 0;
770 	case XX_PRE:
771 		to_emw(pg, po);
772 		return 0;
773 #endif
774 #endif				/*end BAF */
775 
776 	case XX_GPT:
777 		to_mftex(pg, po, 6);
778 		return 0;
779 
780 	case XX_HPGL:
781 		to_mftex(pg, po, 5);
782 		return 0;
783 
784 	case XX_DXF:
785 		to_mftex(pg, po, 7);
786 		return 0;
787 
788 	case XX_SVG:
789 		to_mftex(pg, po, 8);
790 		return 0;
791 
792 	case XX_EPS:
793 		to_eps(pg, po);
794 		return 0;
795 #ifdef USEPDF
796 	case XX_PDF:
797 		to_pdf(pg, po);
798 		return 0;
799 #endif
800 	case XX_RGIP:
801 		to_rgip(pg, po);
802 		return 0;
803 
804 	case XX_FIG:
805 		to_fig(pg, po);
806 		return 0;
807 
808 	case XX_NC:
809 		to_mftex(pg, po, 9);
810 		return 0;
811 
812 	default:
813 		return 1;
814 	}
815 }
816 
817 
818 
819 /**************************************************************************
820  **
821  ** TMP_to_BUF ():
822  **
823  ** Rasterization into a memory buffer
824  **	A pre-requisite for all raster formats. Call any number of times
825  **		as long as the temp. file is opened.
826  **	An internal raster picture buffer is maintained in memory
827  **		by this call.
828  **	The picture buffer is kept allocated after this call so that
829  **		the time-consuming rasterization is needed only once.
830  **		De-allocate at program end or when not needed anymore!
831  **
832  ** Returns:
833  **	ERROR	if error
834  **	0 	if successfully processed
835  **/
836 
TMP_to_BUF(const GEN_PAR * pg,OUT_PAR * po)837 int TMP_to_BUF(const GEN_PAR * pg, OUT_PAR * po)
838 {
839 	int n_rows, n_cols;
840 
841 	if (pg->td == NULL)
842 		return ERROR;
843 	if (n_commands < 0)
844 		return 0;
845 	rewind(pg->td);		/* Rewind temp file for re-reading      */
846 
847 	cleanup_o(po);
848 
849 /**
850  ** 1) Allocate virtual plotter area
851  **/
852 
853 	size_PicBuf(pg, po, &n_rows, &n_cols);
854 
855 	if ((po->picbuf = allocate_PicBuf(pg, n_rows, n_cols)) == NULL) {
856 		Eprintf
857 		    ("Fatal error: cannot allocate %d*%d picture buffer\n",
858 		     n_rows, n_cols);
859 		return ERROR;
860 	}
861 
862 /**
863  ** Read vectors from temporary file and plot them in memory buffer
864  **/
865 
866 	tmpfile_to_PicBuf(pg, po);
867 
868 	return 0;
869 }
870 
871 
872 
873 /**************************************************************************
874  **
875  ** BUF_to_RAS ():
876  **
877  ** Conversion of raster picture buffer into the final output format.
878  **
879  ** Returns:
880  **	ERROR	if error
881  **	1 	if output mode not found
882  **	0 	if successfully processed
883  **/
884 
BUF_to_RAS(const GEN_PAR * pg,OUT_PAR * po)885 int BUF_to_RAS(const GEN_PAR * pg, OUT_PAR * po)
886 {
887 	if (po->picbuf == NULL)
888 		return ERROR;
889 	if (n_commands < 0)
890 		return 0;
891 	switch (pg->xx_mode) {
892 	case XX_PCL:		/* HP PCL Level 3       */
893 		return PicBuf_to_PCL(pg, po);
894 
895 	case XX_PCX:		/* Paintbrush PCX       */
896 		return PicBuf_to_PCX(pg, po);
897 
898 #ifdef	PIC_PAC			/*   To be phased out!  */
899 	case XX_PIC:		/* ATARI 32K format     */
900 		return PicBuf_to_PIC(pg, po);
901 
902 	case XX_PAC:		/* ATARI STaD format    */
903 		return PicBuf_to_PAC(pg, po);
904 #endif
905 
906 #ifdef	AMIGA
907 	case XX_ILBM:		/* AMIGA IFF-ILBM fmt   */
908 		return PicBuf_to_ILBM(pg, po);
909 #endif
910 
911 	case XX_IMG:		/* GEM's IMG format     */
912 		return PicBuf_to_IMG(pg, po);
913 #ifdef JPG
914 	case XX_JPG:		/* JPEG image format */
915 		return PicBuf_to_JPG(pg, po);
916 #endif
917 	case XX_PBM:		/* Portable BitMap fmt  */
918 		return PicBuf_to_PBM(pg, po);
919 #ifdef PNG
920 	case XX_PNG:		/* Portable Network fmt */
921 		return PicBuf_to_PNG(pg, po);
922 #endif
923 
924 #ifdef TIF
925 	case XX_TIFF:		/* Tagged image file fmt */
926 		return PicBuf_to_TIF(pg, po);
927 #endif
928 #ifdef EPSON
929 	case XX_ESC2:		/* Epson Esc/P2 */
930 		return PicBuf_to_ESCP2(pg, po);
931 #endif
932 /**
933  ** Previewers (depending on hardware platform):
934  **/
935 	case XX_PRE:
936 #if   defined(HAS_DOS_DJGR)
937 		return PicBuf_to_DJ_GR(pg, po);
938 #elif defined(HAS_DOS_HGC)
939 		return PicBuf_to_HGC(pg, po);
940 #elif defined(HAS_DOS_VGA)
941 		return PicBuf_to_VGA(pg, po);
942 #elif defined(HAS_OS2_EMX)
943 		return PicBuf_to_OS2(pg, po);
944 #elif defined(HAS_OS2_PM)
945 		return PicBuf_to_PM(pg, po);
946 #elif defined(HAS_UNIX_X11)
947 		return PicBuf_to_X11(pg, po);
948 #elif defined(HAS_UNIX_SUNVIEW)
949 		return PicBuf_to_Sunview(pg, po);
950 #elif defined(ATARI)
951 		return PicBuf_to_ATARI(pg, po);
952 #elif defined(AMIGA)
953 		return PicBuf_to_AMIGA(pg, po);
954 #elif defined(VAX)
955 		return PicBuf_to_UIS(pg, po);
956 #else
957 		return PicBuf_to_Dummy();
958 #endif				/* defined(...) */
959 	default:
960 		return 1;
961 	}
962 }
963 
964 
965 
966 /**********************************************************************
967  **
968  **  My intention is to facilitate the implementation of various
969  **  user interfaces including e.g. a Windows front-end.
970  **
971  **  Note that e.g. in Windows programs there is a "win_main()" function
972  **  instead of "main()", and that waiting for user acknowledgement
973  **  or diagnostic output must not use stdin/out/err...
974  **
975  **  In a separate file, one of possibly several versions of
976  **  user interface dependend functions + local utility functions
977  **  is supplied. The traditional version can be found in "std_main.c"
978  **
979  **	Function	Tradiditional equivalent
980  **
981  **	EPrintf(...)	fprintf(stderr, ...)
982  **	PError		perror
983  **	SilentWait	-
984  **	NormalWait	-
985  **	...		main
986  **/
987