File: genpstex.c

package info (click to toggle)
transfig 1%3A3.2.5.e-4%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 9,300 kB
  • sloc: ansic: 32,666; makefile: 3,255; sh: 549; csh: 25; perl: 13
file content (516 lines) | stat: -rwxr-xr-x 12,153 bytes parent folder | download | duplicates (3)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
 * TransFig: Facility for Translating Fig code
 * Copyright (c) 1991 by Micah Beck
 * Parts Copyright (c) 1985-1988 by Supoj Sutanthavibul
 * Parts Copyright (c) 1989-2002 by Brian V. Smith
 *
 * Any party obtaining a copy of these files 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 and/or distribute 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.
 *
 */

/* 
 *	genpstex.c : psTeX and psTeX_t drivers for fig2dev
 *
 *	Author: Jose Alberto Fernandez R /Maryland CP 9/90
 * 	It uses the LaTeX and PostScript drivers to generate 
 *      LaTeX processed text for a Postscript figure.
 *
 * The pstex_t driver is like a latex driver that only translates 
 * text defined in the default font.
 *
 * The pstex driver is like a PostScript driver that translates 
 * everything except for text in the default font.
 *
 * The pdftex_t and pdftex are drivers for combined PDF/LaTeX.
 *
 * The option '-p file' added to the pstex_t translator specifies
 * the name of the PostScript file to be called in the psfig macro.
 * If not set or its value is null then no PS file will be inserted.
 *
 * Jose Alberto.
 */

#include <sys/stat.h>
#include "fig2dev.h"
#include "genps.h"
#include "genpdf.h"
#include "object.h"
#include "texfonts.h"

extern double rad2deg;

#ifdef hpux
#define rint(a) floor((a)+0.5)     /* close enough? */
#endif

#ifdef gould
#define rint(a) floor((a)+0.5)     /* close enough? */
#endif

extern void
	genlatex_start (),
	gendev_null (),
     	geneps_option (),
	genps_start (),
	genps_arc (),
	genps_ellipse (),
	genps_line (),
	genps_spline (),
        genlatex_option (),
        genlatex_text (),
        genps_text ();
extern int
	genlatex_end (),
	genps_end ();

static char pstex_file[1000] = "";
static int    iObjectsRead = 0;
static int    iTextRead = 0;
static int    iObjectsFileNumber = 0;
static int    iStartDepth;
static int    iLastDepth;
static FILE * ptCreateFile;
static char   szFileName[1000];
static int    iLength;
static int    iPdfOutputs = 0;

#ifdef never
static double		unitlength;
static double		dash_mag = 1.0;

static int		(*translate_coordinates)() = NULL;
static int		(*translate_coordinates_d)() = NULL;
#else
extern double		unitlength;
extern double		dash_mag;

extern int		(*translate_coordinates)();
extern int		(*translate_coordinates_d)();
#endif

#define TOP		840
#define	SWAP(x,y)	{tmp=x; x=y; y=tmp;}
#define TRANS(x,y)		(*translate_coordinates)(&x,&y)
#define TRANS2(x1,y1,x2,y2)	(*translate_coordinates)(&x1,&y1); \
				(*translate_coordinates)(&x2,&y2)
#define TRANSD(x,y)		(*translate_coordinates_d)(&x,&y)

static void genpstex_p_finalize_objects(int depth);

/*************************************************************************
 *************************************************************************/
static
translate2(xp, yp)
  int	*xp, *yp;
{
	*xp = *xp + 1;
	*yp = (double)(TOP - *yp -1);
	}

static
translate1_d(xp, yp)
  double	*xp, *yp;
{
	*xp = *xp + 1.0;
	*yp = *yp + 1.0;
	}

static
translate2_d(xp, yp)
  double	*xp, *yp;
{
	*xp = *xp + 1.0;
	*yp = (double)TOP - *yp -1.0;
	}

/*************************************************************************
 *************************************************************************/
void genpstex_p_option(opt, optarg)
char opt, *optarg;
{
       if (opt == 'p')
       {
	   strcpy(pstex_file, optarg);
       }
       else
	   genlatex_option(opt, optarg);
}


/*************************************************************************
 *************************************************************************/
void genpstex_p_start(objects)
  F_compound	*objects;
{
	int	      tmp;
	struct stat   tStat;
	long          lUnitLength;

        if (*pstex_file == '\0')
        {
		put_msg("Argument -p is mandatory to pstex_p.");
		exit(1);
	}

	strncpy(szFileName, pstex_file, 900);
	iLength = strlen(szFileName);

	sprintf(szFileName + iLength, ".create");
	ptCreateFile = fopen (szFileName, "wb");

	if ( ! ptCreateFile)
        {
		put_msg("Couldn't open %s for writing", szFileName);
		exit(1);
	}
	fstat(fileno(ptCreateFile), & tStat);
	fchmod (fileno(ptCreateFile), tStat.st_mode | ((tStat.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) >> 2));
	fprintf(ptCreateFile, "#!/bin/bash\n\n", szFileName);

	fprintf(ptCreateFile, "while getopts \"r\" Option\n");
	fprintf(ptCreateFile, "do\n");
	fprintf(ptCreateFile, "  case $Option in\n");
	fprintf(ptCreateFile, "    r) iOptRemove=1;;\n");
	fprintf(ptCreateFile, "    *) echo \"illegal option -$Option\"\n");
	fprintf(ptCreateFile, "  esac\n");
	fprintf(ptCreateFile, "done\n");
	fprintf(ptCreateFile, "shift $(($OPTIND - 1))\n");

	texfontsizes[0] = texfontsizes[1] =
		TEXFONTSIZE(font_size != 0.0? font_size : DEFAULT_FONT_SIZE);

 	unitlength = mag/ppi;
	dash_mag /= unitlength*80.0;

	translate_coordinates = translate2;
	translate_coordinates_d = translate2_d;

	TRANS2(llx, lly, urx, ury);
	if (llx > urx) SWAP(llx, urx)
	if (lly > ury) SWAP(lly, ury)

	/* LaTeX start */

	/* print any whole-figure comments prefixed with "%" */
	if (objects->comments) {
	    fprintf(tfp,"%%\n");
	    print_comments("% ",objects->comments, "");
	    fprintf(tfp,"%%\n");
	}

	lUnitLength = (long) (round(4736286.72*unitlength));
	fprintf(tfp, "\\setlength{\\unitlength}{%lisp}%%\n",
				lUnitLength);
	/* define the SetFigFont macro */
	define_setfigfont(tfp);

	sprintf(szFileName + iLength, ".size");
	fprintf(ptCreateFile, "if [ \"$iOptRemove\" == \"\" ]; then\n");
	if (iPdfOutputs)
		fprintf(ptCreateFile, "  echo \"\\setlength\\pdfpagewidth{%3.2fpt}\\setlength\\pdfpageheight{%3.2fpt}\" > %s\n",
        	        (float) lUnitLength / 65536 * (urx - llx), (float) lUnitLength / 65536 * (ury - lly), szFileName);
	else
		fprintf(ptCreateFile, "echo -n\"\" > %s\n", szFileName);
	fprintf(ptCreateFile, "else\n  rm -f %s\nfi\n", szFileName);
}

/*************************************************************************
 *************************************************************************/
void genpdftex_p_start(objects)
  F_compound	*objects;
{
	iPdfOutputs = 1;
	genpstex_p_start(objects);
}


/*************************************************************************
 *************************************************************************/
int genpstex_p_end()
{
	if (iTextRead)
	{
		fprintf(tfp, "\\end{picture}%%\n");
		iTextRead = 0;
	}
	if (iObjectsRead)
	{
		genpstex_p_finalize_objects(iLastDepth);
		iObjectsRead = 0;
	}

	/* LaTeX ending */
	fprintf(tfp, "\\begin{picture}(%d,%d)\n", urx-llx, ury-lly);
	fprintf(tfp, "\\end{picture}%%\n");

	fclose(ptCreateFile);

	/* all ok */
	return 0;
}


/*************************************************************************
 *************************************************************************/
void genpstex_p_finalize_objects(depth)
int depth;
{
	char	szFileName[1000];
	int     iLength;

	strncpy(szFileName, pstex_file, 900);
	iLength = strlen(szFileName);


	sprintf(szFileName + iLength, "%03d", iObjectsFileNumber++);
	fprintf(ptCreateFile, "if [ \"$iOptRemove\" == \"\" ]; then\n");
	if (iPdfOutputs)
	{
		fprintf(ptCreateFile, "  %s -L pstex -D +%d:%d %s", prog, depth, iStartDepth, from, szFileName);
		fprintf(ptCreateFile, " | epstopdf -f > %s.pdf\n", szFileName, szFileName);
	}
	else
		fprintf(ptCreateFile, "%s -L pstex -D +%d:%d %s %s.eps\n", prog, depth, iStartDepth, from, szFileName);
	fprintf(ptCreateFile, "else\n  rm -f %s.", szFileName);
	if (iPdfOutputs)
		fprintf(ptCreateFile, "pdf");
	else
		fprintf(ptCreateFile, "eps");
	fprintf(ptCreateFile, "\nfi\n");

	fprintf(tfp, "\\begin{picture}(0,0)%%\n");
/* newer includegraphics directive suggested by Stephen Harker 1/13/99 */
#if defined(LATEX2E_GRAPHICS)
#  if defined(EPSFIG)
	fprintf(tfp, "\\epsfig{file=%s.eps}%%\n",szFileName);
#  else
	fprintf(tfp, "\\includegraphics{%s}%%\n",szFileName);
#  endif
#else
	fprintf(tfp, "\\special{psfile=%s.eps}%%\n",szFileName);
#endif
	fprintf(tfp, "\\end{picture}%%\n");
}

/*************************************************************************
 *************************************************************************/
void genpstex_p_object(depth)
int depth;
{
	if (iTextRead)
	{
		fprintf(tfp, "\\end{picture}%%\n");
		iTextRead = 0;
	}
	if (iObjectsRead == 0)
	{
		iStartDepth = depth;
		iObjectsRead = 1;
	}
	iLastDepth = depth;

}

void genpstex_p_arc(obj)
F_arc	*obj;
{
	genpstex_p_object(obj->depth);
}


void genpstex_p_ellipse(obj)
F_ellipse	*obj;
{
	genpstex_p_object(obj->depth);
}

void genpstex_p_line(obj)
F_line	*obj;
{
	genpstex_p_object(obj->depth);
}

void genpstex_p_spline(obj)
F_spline	*obj;
{
	genpstex_p_object(obj->depth);
}



/*************************************************************************
 *************************************************************************/
void genpstex_p_text(t)
F_text	*t;
{

	if (!special_text(t))
		genpstex_p_object(t->depth);
	else
	{
		if (iObjectsRead)
		{
			genpstex_p_finalize_objects(iLastDepth);
			iObjectsRead = 0;
		}
		if (iTextRead == 0)
		{
			fprintf(tfp, "\\begin{picture}(0,0)(%d,%d)\n", llx, lly);
			iTextRead = 1;
		}
		genlatex_text(t);
	}

}

/*************************************************************************
 *************************************************************************/
void genpstex_t_option(opt, optarg)
char opt, *optarg;
{
       if (opt == 'p') 
	   strcpy(pstex_file, optarg);
       else
	   genlatex_option(opt, optarg);
}


void genpstex_t_start(objects)
F_compound	*objects;
{
	/* Put PostScript Image if any*/
        if (pstex_file[0] != '\0') {
		fprintf(tfp, "\\begin{picture}(0,0)%%\n");
/* newer includegraphics directive suggested by Stephen Harker 1/13/99 */
#if defined(LATEX2E_GRAPHICS)
#  if defined(EPSFIG)
		fprintf(tfp, "\\epsfig{file=%s}%%\n",pstex_file); 
#  else
		fprintf(tfp, "\\includegraphics{%s}%%\n",pstex_file);
#  endif
#else
		fprintf(tfp, "\\special{psfile=%s}%%\n",pstex_file);
#endif
		fprintf(tfp, "\\end{picture}%%\n");
	}
        genlatex_start(objects);

}

void genpstex_t_text(t)
F_text	*t;
{

	if (!special_text(t))
	  gendev_null();
	else genlatex_text(t);
}

/*************************************************************************
 *************************************************************************/
void genpstex_text(t)
F_text	*t;
{

	if (!special_text(t))
	  genps_text(t);
	else gendev_null();
}

void genpstex_option(opt, optarg)
char opt, *optarg;
{
       if (opt != 'p')
	   genlatex_option(opt, optarg);
}

struct driver dev_pstex_p = {
  	genpstex_p_option,
	genpstex_p_start,
	gendev_null,
	genpstex_p_arc,
	genpstex_p_ellipse,
	genpstex_p_line,
	genpstex_p_spline,
	genpstex_p_text,
	genpstex_p_end,
	INCLUDE_TEXT
};

struct driver dev_pdftex_p = {
  	genpstex_p_option,
	genpdftex_p_start,
	gendev_null,
	genpstex_p_arc,
	genpstex_p_ellipse,
	genpstex_p_line,
	genpstex_p_spline,
	genpstex_p_text,
	genpstex_p_end,
	INCLUDE_TEXT
};

struct driver dev_pstex_t = {
  	genpstex_t_option,
	genpstex_t_start,
	gendev_null,
	gendev_null,
	gendev_null,
	gendev_null,
	gendev_null,
	genpstex_t_text,
	genlatex_end,
	INCLUDE_TEXT
};

struct driver dev_pdftex_t = {
  	genpstex_t_option,
	genpstex_t_start,
	gendev_null,
	gendev_null,
	gendev_null,
	gendev_null,
	gendev_null,
	genpstex_t_text,
	genlatex_end,
	INCLUDE_TEXT
};

struct driver dev_pstex = {
  	geneps_option, 	/* use eps so always exported in Portrait mode */
	genps_start,
	genps_grid,
	genps_arc,
	genps_ellipse,
	genps_line,
	genps_spline,
	genpstex_text,
	genps_end,
	INCLUDE_TEXT
};

extern void     genpdf_option();
extern void     genpdf_start();
extern int      genpdf_end();

struct driver dev_pdftex = {
  	genpdf_option,
	genpdf_start,
	genps_grid,
	genps_arc,
	genps_ellipse,
	genps_line,
	genps_spline,
	genpstex_text,
	genpdf_end,
	INCLUDE_TEXT
};