forked from dgtlrift/hp2xx
/
to_eps.c
575 lines (491 loc) · 14.3 KB
/
to_eps.c
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
Copyright (c) 1991 - 1994 Heinz W. Werntges. All rights reserved.
Parts Copyright (c) 1999 Martin Kroeker All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of HP2xx.
HP2xx is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the GNU General Public License, Version 2 or later, for full details.
Everyone is granted permission to copy, modify and redistribute
HP2xx, but only under the conditions described in the GNU General Public
License. A copy of this license is supposed to have been
given to you along with HP2xx so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
/** to_eps.c: Converter to Encapsulated PostScript format;
** (derived from PLPLOT driver "postscript.c")
**
** 91/06/29 V 1.00 HWW Derived from postscript.c
** 91/10/15 V 1.01 HWW ANSI_C
** 91/11/20 V 1.02 HWW Changes due to "SPn;"
** 91/12/22 V 1.02a HWW Many small changes
** 92/05/17 V 1.02b HWW Output to stdout if outfile == '-'
** 92/05/24 V 1.10a HWW Color supported (RGB)
** 92/10/20 V 1.10b HWW Bug fix: Setting color implies a new path
** 92/12/12 V 1.10c HWW Info line now interprets outfile=='-' as "stdout"
** 93/04/12 V 1.10d HWW Explicit prototypes for time() and ctime() removed;
** VMstatus reports only if !p->quiet;
** BoundingBox calc.: roundinf included (floor, ceil)
** 93/04/25 V 1.10e HWW BoundingBox corrected for (half) max. pen width
** 93/11/15 V 1.11a HWW EPS syntax corrections (courtesy N. H. F. Beebe)
** 94/02/15 V 1.20a HWW Adapted to changes in hp2xx.h
** 01/12/04 MK Added missing colon to BeginProcSet (Bengt-Arne Fjellner)
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "bresnham.h"
#include "hp2xx.h"
#include "pendef.h"
#include "lindef.h"
/*#define A4_height 297*//* in [mm] */
static int linecount = 0;
static float xcoord2mm, ycoord2mm;
static float xmin, ymin;
/* globals for states since we have to init them for each file now */
static PEN_W lastwidth;
static int lastcap;
static int lastjoin;
static int lastlimit;
static Byte lastred, lastgreen, lastblue;
void eps_set_linewidth(PEN_W, HPGL_Pt *, FILE *);
void eps_set_linecap(LineEnds type, PEN_W pensize, HPGL_Pt * ppt,
FILE * fd);
void eps_set_linejoin(LineJoins type, LineLimit limit, PEN_W pensize,
HPGL_Pt * ppt, FILE * fd);
void eps_set_color(PEN_C pencolor, HPGL_Pt * ppt, FILE * fd);
void eps_init(const GEN_PAR *, const OUT_PAR *, FILE *, PEN_W);
void eps_end(FILE *);
void eps_stroke_and_move_to(HPGL_Pt *, FILE *);
void eps_line_to(HPGL_Pt *, char, FILE *);
void eps_draw_dot(double radius, FILE * fd);
char *eps_Getdate(void);
/**
** Close graphics file
**/
void eps_end(FILE * fd)
{
fprintf(fd, " S\neop\n");
fprintf(fd, "@end\n");
fprintf(fd, "%%%%PageTrailer\n");
fprintf(fd, "%%%%Trailer\n");
fprintf(fd, "%%%%EOF\n");
linecount = 0;
}
/**
** Flush old path and move
**/
void eps_stroke_and_move_to(HPGL_Pt * ppt, FILE * fd)
{
fprintf(fd, " S\n%6.2f %6.2f M", /* S: Start a new path */
(ppt->x - xmin) * xcoord2mm, (ppt->y - ymin) * ycoord2mm);
linecount = 0;
}
/**
** Set line width
**/
void eps_set_linewidth(PEN_W width, HPGL_Pt * ppt, FILE * fd)
{
if ((fabs(width - lastwidth) >= 0.01) && (width >= 0.05)) {
eps_stroke_and_move_to(ppt, fd); /* MUST start a new path! */
fprintf(fd, " %6.3f W\n", width);
lastwidth = width;
}
return;
}
/**
** Set line ends
**/
void eps_set_linecap(LineEnds type, PEN_W pensize, HPGL_Pt * ppt, FILE * fd)
{
int newcap;
if (pensize > 0.35) {
switch (type) {
case LAE_butt:
newcap = 0;
break;
case LAE_triangular: /* triangular not implemented in PS/PDF */
newcap = 1;
break;
case LAE_round:
newcap = 1;
break;
case LAE_square:
newcap = 2;
break;
default:
newcap = 0;
break;
}
} else {
newcap = 1;
}
if (newcap != lastcap) {
eps_stroke_and_move_to(ppt, fd); /* MUST start a new path! */
fprintf(fd, " %d setlinecap\n", newcap);
lastcap = newcap;
}
return;
}
/**
** Set line Joins
**/
void eps_set_linejoin(LineJoins type, LineLimit limit, PEN_W pensize,
HPGL_Pt * ppt, FILE * fd)
{
int newjoin;
int newlimit = lastlimit;
if (pensize > 0.35) {
switch (type) {
case LAJ_plain_miter:
newjoin = 0;
newlimit = 5; /* arbitrary value */
break;
case LAJ_bevel_miter: /* not available */
newjoin = 0;
newlimit = limit;
break;
case LAJ_triangular: /* not available */
newjoin = 1;
break;
case LAJ_round:
newjoin = 1;
break;
case LAJ_bevelled:
newjoin = 2;
break;
case LAJ_nojoin: /* not available */
newjoin = 1;
break;
default:
newjoin = 0;
newlimit = 5; /* arbitrary value */
break;
}
} else {
newjoin = 1;
}
if (newjoin != lastjoin) {
eps_stroke_and_move_to(ppt, fd); /* MUST start a new path! */
fprintf(fd, " %d setlinejoin\n", newjoin);
lastjoin = newjoin;
}
if (newlimit != lastlimit) {
eps_stroke_and_move_to(ppt, fd); /* MUST start a new path! */
fprintf(fd, " %d setmiterlimit\n", newlimit);
lastlimit = newlimit;
}
}
/**
** Set RGB color
**/
void eps_set_color(PEN_C pencolor, HPGL_Pt * ppt, FILE * fd)
{
if ((pt.clut[pencolor][0] != lastred) ||
(pt.clut[pencolor][1] != lastgreen)
|| (pt.clut[pencolor][2] != lastblue)) {
eps_stroke_and_move_to(ppt, fd); /* MUST start a new path! */
fprintf(fd, " %6.3f %6.3f %6.3f C\n",
(double) pt.clut[pencolor][0] / 255.0,
(double) pt.clut[pencolor][1] / 255.0,
(double) pt.clut[pencolor][2] / 255.0);
lastred = pt.clut[pencolor][0];
lastgreen = pt.clut[pencolor][1];
lastblue = pt.clut[pencolor][2];
} else {
return;
}
}
void eps_line_to(HPGL_Pt * ppt, char mode, FILE * fd)
{
if (linecount > 3) {
putc('\n', fd);
linecount = 0;
} else
putc(' ', fd);
fprintf(fd, "%6.2f %6.2f %c",
(ppt->x - xmin) * xcoord2mm, (ppt->y - ymin) * ycoord2mm,
mode);
linecount++;
}
void eps_draw_dot(double radius, FILE * fd)
{
fprintf(fd, " currentpoint newpath %0.2f 0 360 arc fill\n",
radius);
}
/**
** Get the date and time: This is optional, since its result only
** appeares in the PS header.
**/
char *eps_Getdate(void)
{
int len;
time_t t;
char *p;
t = time((time_t *) 0);
p = ctime(&t);
len = (int) strlen(p);
/*@-modobserver@ */ *(p + len - 1) = '\0';
/* zap the newline character */
return p;
}
/**
** PostScript definitions
**/
void eps_init(const GEN_PAR * pg, const OUT_PAR * po, FILE * fd,
PEN_W pensize)
{
long left, right, low, high;
double hmxpenw;
lastwidth = -1.0;
lastcap = lastjoin = lastlimit = -1;
lastred = lastgreen = lastblue = -1;
hmxpenw = pg->maxpensize / 2.0; /* Half max. pen width, in mm */
/**
** Header comments into PostScript file
**/
if (po->specials)
fprintf(fd, "%%!PS-Adobe-2.0\n");
else
fprintf(fd, "%%!PS-Adobe-2.0 EPSF-2.0\n");
fprintf(fd, "%%%%Title: %s\n", po->outfile);
fprintf(fd,
"%%%%Creator: hp2xx %s (c) 1991-1994 by H. Werntges, 1999-2003 by M. Kroeker\n",VERS_NO);
fprintf(fd, "%%%%CreationDate: %s\n", eps_Getdate());
fprintf(fd, "%%%%Pages: 1\n");
/**
** Bounding Box limits: Conversion factor: 2.834646 * 1/72" = 1 mm
**
** (hmxpenw & floor/ceil corrections suggested by Eric Norum)
**/
left = (long) floor(fabs(po->xoff - hmxpenw) * MM_TO_PS_POINT);
low = (long) floor(fabs(po->yoff - hmxpenw) * MM_TO_PS_POINT);
right =
(long) ceil((po->xoff + po->width + hmxpenw) * MM_TO_PS_POINT);
high =
(long) ceil((po->yoff + po->height + hmxpenw) *
MM_TO_PS_POINT);
fprintf(fd, "%%%%BoundingBox: %ld %ld %ld %ld\n", left, low, right,
high);
if (!pg->quiet)
Eprintf("Bounding Box: [%ld %ld %ld %ld]\n", left, low,
right, high);
fprintf(fd, "%%%%EndComments\n\n");
if (po->specials == 1) { /* include pagesize directives - disallowed in EPS*/
fprintf(fd, "%%%%BeginFeature: PageSize %fx%fmm\n",po->width,po->height);
fprintf(fd, "<< /PageSize [\n");
fprintf(fd, " %f 2.835 mul %f 2.835 mul\n",po->width,po->height);
fprintf(fd, " ] >> setpagedevice\n");
fprintf(fd, "%%EndFeature\n");
}
/**
** Definitions
**/
fprintf(fd, "%%%%BeginProcSet:\n");
fprintf(fd, "/PSSave save def\n"); /* save VM state */
fprintf(fd, "/PSDict 200 dict def\n"); /* define a dictionary */
fprintf(fd, "PSDict begin\n"); /* start using it */
fprintf(fd, "/@restore /restore load def\n");
fprintf(fd, "/restore\n");
fprintf(fd, " {vmstatus pop\n");
fprintf(fd, " dup @VMused lt {pop @VMused} if\n");
fprintf(fd, " exch pop exch @restore /@VMused exch def\n");
fprintf(fd, " } def\n");
fprintf(fd, "/@pri\n");
fprintf(fd, " {\n");
fprintf(fd, " ( ) print\n");
fprintf(fd,
" ( ) cvs print\n");
fprintf(fd, " } def\n");
fprintf(fd, "/@start\n"); /* - @start - -- start everything */
fprintf(fd, " {\n");
fprintf(fd, " vmstatus pop /@VMused exch def pop\n");
fprintf(fd, " } def\n");
fprintf(fd, "/@end\n"); /* - @end - -- finished */
fprintf(fd, " {");
if (!pg->quiet) {
fprintf(fd, "(VM Used: ) print @VMused @pri\n");
fprintf(fd,
" (. Unused: ) print vmstatus @VMused sub @pri pop pop\n");
fprintf(fd, " (\\n) print flush\n");
}
fprintf(fd, " end\n");
fprintf(fd, " PSSave restore\n");
fprintf(fd, " } def\n");
fprintf(fd, "/bop\n"); /* bop - -- begin a new page */
fprintf(fd, " {\n");
fprintf(fd, " /SaveImage save def\n");
fprintf(fd, " } def\n");
fprintf(fd, "/eop\n"); /* - eop - -- end a page */
fprintf(fd, " {\n");
fprintf(fd, " showpage\n");
fprintf(fd, " SaveImage restore\n");
fprintf(fd, " } def\n");
fprintf(fd, "/@line\n"); /* set line parameters */
fprintf(fd, " {\n");
fprintf(fd,
"%%%% 1 setlinejoin %%%% now set from LA command\n");
fprintf(fd,
"%%%% 1 setmiterlimit %%%% now set from LA command\n");
fprintf(fd, " } def\n");
fprintf(fd, "/@SetPlot\n");
fprintf(fd, " {\n");
fprintf(fd, " %f %f scale\n", MM_TO_PS_POINT, MM_TO_PS_POINT); /* 1/72"--> mm */
fprintf(fd, " %7.3f %7.3f translate\n", po->xoff + hmxpenw,
po->yoff + hmxpenw);
fprintf(fd, " %6.3f setlinewidth\n", pensize);
fprintf(fd, " } def\n");
fprintf(fd, "/C {setrgbcolor} def\n");
fprintf(fd, "/D {lineto} def\n");
fprintf(fd, "/M {moveto} def\n");
fprintf(fd, "/S {stroke} def\n");
fprintf(fd, "/W {setlinewidth} def\n");
fprintf(fd, "/Z {stroke newpath} def\n");
fprintf(fd, "end\n"); /* end of dictionary definition */
fprintf(fd, "%%%%EndProcSet\n\n");
/**
** Set up the plots
**/
fprintf(fd, "%%%%BeginSetup\n");
fprintf(fd, "/#copies 1 def\n");
fprintf(fd, "%%%%EndSetup\n");
fprintf(fd, "%%%%Page: 1 1\n");
fprintf(fd, "%%%%BeginPageSetup\n");
fprintf(fd, "PSDict begin\n");
fprintf(fd, "@start\n");
fprintf(fd, "@line\n");
fprintf(fd, "@SetPlot\n\n");
fprintf(fd, "bop\n");
fprintf(fd, "%%%%EndPageSetup\n");
}
/**
** Higher-level interface: Output Encapsulated PostScript format
**/
int to_eps(const GEN_PAR * pg, const OUT_PAR * po)
{
PlotCmd cmd;
HPGL_Pt pt1 = { 0, 0 };
FILE *md;
int pen_no = 0, err;
PEN_W pensize;
err = 0;
if (!pg->quiet)
Eprintf("\n\n- Writing EPS code to \"%s\"\n",
*po->outfile == '-' ? "stdout" : po->outfile);
/* Init. of PostScript file: */
if (*po->outfile != '-') {
if ((md = fopen(po->outfile, "w")) == NULL) {
PError("hp2xx (eps)");
return ERROR;
}
} else
md = stdout;
/* PS header */
pensize = pt.width[DEFAULT_PEN_NO]; /* Default pen */
eps_init(pg, po, md, pensize);
if (pensize > 0.05)
fprintf(md, " %6.3f W\n", pensize);
/* Factor for transformation of HP coordinates to mm */
xcoord2mm = po->width / (po->xmax - po->xmin);
ycoord2mm = po->height / (po->ymax - po->ymin);
xmin = po->xmin;
ymin = po->ymin;
/**
** Command loop: While temporary file not empty: process command.
**/
while ((cmd = PlotCmd_from_tmpfile()) != CMD_EOF) {
switch (cmd) {
case NOP:
break;
case SET_PEN:
if ((pen_no = fgetc(pg->td)) == EOF) {
PError("Unexpected end of temp. file: ");
err = ERROR;
goto EPS_exit;
}
pensize = pt.width[pen_no];
break;
case DEF_PW:
if (!load_pen_width_table(pg->td,0)) {
PError("Unexpected end of temp. file");
err = ERROR;
goto EPS_exit;
}
pensize = pt.width[pen_no];
break;
case DEF_PC:
err = load_pen_color_table(pg->td,0);
if (err < 0) {
PError("Unexpected end of temp. file");
err = ERROR;
goto EPS_exit;
}
break;
case DEF_LA:
if (load_line_attr(pg->td,0) < 0) {
PError("Unexpected end of temp. file");
err = ERROR;
goto EPS_exit;
}
break;
case MOVE_TO:
eps_set_linewidth(pensize, &pt1, md);
eps_set_linecap(CurrentLineAttr.End, pensize, &pt1,
md);
eps_set_linejoin(CurrentLineAttr.Join,
CurrentLineAttr.Limit, pensize,
&pt1, md);
eps_set_color(pt.color[pen_no], &pt1, md);
HPGL_Pt_from_tmpfile(&pt1);
if (pensize > 0.05)
eps_stroke_and_move_to(&pt1, md);
break;
case DRAW_TO:
eps_set_linewidth(pensize, &pt1, md);
eps_set_linecap(CurrentLineAttr.End, pensize, &pt1,
md);
eps_set_linejoin(CurrentLineAttr.Join,
CurrentLineAttr.Limit, pensize,
&pt1, md);
eps_set_color(pt.color[pen_no], &pt1, md);
HPGL_Pt_from_tmpfile(&pt1);
if (pensize > 0.05)
eps_line_to(&pt1, 'D', md);
break;
case PLOT_AT:
eps_set_linewidth(pensize, &pt1, md);
eps_set_linecap(CurrentLineAttr.End, pensize, &pt1,
md);
eps_set_linejoin(CurrentLineAttr.Join,
CurrentLineAttr.Limit, pensize,
&pt1, md);
eps_set_color(pt.color[pen_no], &pt1, md);
HPGL_Pt_from_tmpfile(&pt1);
if (pensize > 0.05) {
eps_line_to(&pt1, 'M', md);
eps_line_to(&pt1, 'D', md); /* not sure whether this is needed */
eps_draw_dot(pensize / 2, md);
}
break;
default:
Eprintf("Illegal cmd in temp. file!");
err = ERROR;
goto EPS_exit;
}
}
/* Finish up */
eps_end(md);
EPS_exit:
if (md != stdout)
fclose(md);
if (!pg->quiet)
Eprintf("\n");
return err;
}