-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpostscript.c
1020 lines (923 loc) · 30.8 KB
/
postscript.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <time.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
#include "db.h"
#include "xwin.h"
#include "postscript.h"
#include "ev.h"
#define MAXBUF 256
int layer=0;
int linetype=0;
int newtype=1;
int pennum=0;
int newpen=1;
int filltype=0;
int newfill=1;
int in_line=0; /* set to 1 when working on a line */
int in_poly=0;
int colorflag=1; // 0=black; 1=color; 2=greyscale
double linewidth=1.0;
int this_pen=0; // we need to save state at start of line
int this_line=0; // because lines are implicitly terminated
int this_fill=0; // by starting a new line which overwrites
int this_isfilled=0; // pen, line and fill
int debug=0;
FILE *fp=NULL; // file descriptor for output file
OMODE outputtype=POSTSCRIPT; // 0=postscript, 1=gerber
double bbllx, bblly, bburx, bbury; // bounding box in screen coords
const char *get_svg_fill(int fill);
void ps_set_color(int color) {
if (debug) printf("ps_set_color: %d\n",color);
colorflag=color;
}
void ps_set_linewidth(double width) {
if (debug) printf("ps_set_linewidth: %f\n", width);
linewidth=width;
}
void ps_set_outputtype(OMODE mode) {
if (debug) printf("ps_set_outputtype: %d\n",mode);
outputtype=mode;
}
void ps_set_file(FILE *fout)
{
if (debug) printf("ps_set_file:\n");
fp=fout;
}
void ps_set_layer(int layernumber) {
if (debug) printf("setting layer :%d\n", layernumber);
layer=layernumber;
}
void ps_set_fill(int fill)
{
if (debug) printf("setting fill:%d\n", fill);
if (fill != filltype) {
newfill=1;
}
filltype=fill;
}
void ps_set_line(int line)
{
if (debug) printf("setting line:%d\n", line);
if (line != linetype) {
newtype=1;
}
linetype=line;
}
int hpgl_penmap[] = {
1,2,3,5,
7,6,4,1,
2,3,5,7,
6,4,1,2
};
int dxf_penmap[] = {
7,1,3,5,
4,6,2,7,
9,8,7,1,
3,5,4,6
};
void ps_set_pen(int pen)
{
if (debug) printf("setting pen:%d\n", pen);
if (colorflag==0) { // bw
pen = 0;
}
if (pennum != pen) {
newpen=1;
}
if (outputtype == DXF) {
pennum=dxf_penmap[pen%16];
} else if (outputtype == HPGL) {
pennum=hpgl_penmap[pen%16];
} else {
pennum=pen;
}
if (colorflag != 1) {
pennum=0; // force black and white
}
}
char *pen_to_svg_color(int pen) {
switch (pen) {
default:
case 0:
return("black");
break;
case 1:
return("red");
break;
case 2:
return("green");
break;
case 3:
return("blue");
break;
case 4:
// return("cyan");
return("slateblue");
break;
case 5:
return("magenta");
break;
case 6:
// return("yellow");
return("gold");
break;
case 7:
return("black");
break;
case 8:
return("slategrey");
break;
case 9:
return("darkgrey");
break;
}
}
/*
Letter 0 0 8.5i 11i
Legal 0 0 8.5i 14i
Tabloid 0 0 11i 17i
ANSI-C 0 0 17i 22i
ANSI-D 0 0 22i 34i
ANSI-E 0 0 34i 44i
*/
/* the ps interpreter evidently pays attention to pagesize labels... */
/* can't say Letter and give Ansi-A page sizes... */
void ps_preamble(
char *dev,
char *prog,
double pdx, double pdy, /* page size in inches */
double llx, double lly, /* drawing bounding box in screen coords */
double urx, double ury
) {
time_t timep;
char buf[MAXBUF];
double xmid, ymid;
double xdel, ydel;
double s1, s2, scale;
double xmax, ymax;
int landscape=0;
double tmp;
int debug=1;
if (debug) printf("ps_preamble:\n");
/* some preliminaries... */
/* create transform commands based on bb and paper size */
bbllx=llx; bblly=lly; bburx=urx; bbury=ury;
if (debug) printf("llx:%f lly:%f urx:%f ury:%f\n", llx, lly, urx, ury);
/* FIXME, check that bb is in canonic order */
if ((llx > urx) || (lly > ury)) {
printf("bounding box error in postscript.c\n");
}
xmid = (llx+urx)/2.0;
ymid = (lly+ury)/2.0;
xdel = 1.05*fabs(urx-llx);
ydel = 1.05*fabs(ury-lly);
if ( outputtype == HPGL) {
// 0.0025mm per step = 1016 ticks/in
xmax=pdx*1016.0;
ymax=pdy*1016.0;
} else if ( outputtype == POSTSCRIPT) {
// 72 pts/in
xmax=pdx*72.0;
ymax=pdy*72.0;
printf("aspect is xdel: %f, ydel: %f\n", xdel, ydel);
if (xdel > ydel) { /* flip aspect */
landscape=1;
printf("setting landscape\n");
tmp=xmax; xmax=ymax; ymax=tmp;
} else {
printf("setting portrait\n");
landscape=0;
}
} else {
xmax=pdx*1000.0;
ymax=pdx*1000.0;
}
s1 = xmax/xdel;
s2 = ymax/ydel;
scale = s2;
if (s1 < s2) {
scale = s1;
}
if ( outputtype == AUTOPLOT) {
fprintf(fp,"back\n");
fprintf(fp,"isotropic\n");
return;
} else if ( outputtype == SVG || outputtype == WEB) {
lly = bblly-(lly-bbury);
ury = bblly-(ury-bbury);
if (outputtype == WEB) {
fprintf(fp,"<html>\n");
fprintf(fp,"<body>\n");
}
fprintf(fp,"<?xml version=\"1.0\" standalone=\"no\"?>\n");
fprintf(fp,"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n");
fprintf(fp,"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
if (outputtype == WEB) {
fprintf(fp,"<title> %s </title>\n", dev);
}
fprintf(fp,"<svg xmlns=\"http://www.w3.org/2000/svg\"\n");
fprintf(fp,"xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
V_to_R(&llx,&lly);
V_to_R(&urx,&ury);
// bounding box for output geometries
// printf("<!-- llx:%g lly:%g urx:%g ury:%g -->\n", llx, lly, urx, ury );
// set default pen width to be a fraction diagonal
double diagonal;
diagonal = sqrt(pow(fabs(urx-llx),2.0)+pow(fabs(ury-lly),2.0));
ps_set_linewidth(diagonal/700.0);
// document size
if (outputtype == SVG) {
fprintf(fp,"width=\"100%%\" height=\"100%%\" viewBox=\"%g %g %g %g\" preserveAspectRatio=\"xMinYMin meet\">\n",
llx, lly, urx-llx, ury-lly);
}
// fill patterns
if (outputtype == SVG) {
// normalize stipples to drawing size
fprintf(fp,"<defs>\n");
fprintf(fp," <!-- color filters for crosshatch fills -->\n");
fprintf(fp," <filter id=\"c0\"> <!-- white (black) -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(0));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c1\"> <!-- red -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(1));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c2\"> <!-- green -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(2));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c3\"> <!-- blue -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(3));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c4\"> <!-- cyan -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(4));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c5\"> <!-- magenta -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(5));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c6\"> <!-- yellow -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(6));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c7\"> <!-- black -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(7));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c8\"> <!-- grey1 -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(8));
fprintf(fp," </filter>\n");
fprintf(fp," <filter id=\"c9\"> <!-- grey2 -->\n");
fprintf(fp," <feColorMatrix type=\"matrix\"\n");
fprintf(fp," values=\"%s\"/>\n", get_svg_fill(9));
fprintf(fp," </filter>\n");
fprintf(fp, " <pattern id=\"f0\" width=\".2\" height=\".2\" stroke=\"white\"\n");
fprintf(fp, " stroke-linecap=\"square\" stroke-width=\"%.1g\"\n",linewidth);
fprintf(fp, " patternTransform=\"rotate(45) scale(%.1g)\"\n", diagonal/70.0);
fprintf(fp, " patternUnits=\"userSpaceOnUse\">\n");
fprintf(fp, " </pattern>\n");
fprintf(fp, " <pattern id=\"f1\" width=\".2\" height=\".2\" stroke=\"white\"\n");
fprintf(fp, " stroke-linecap=\"square\" stroke-width=\"%.1g\"\n",linewidth);
fprintf(fp, " patternTransform=\"rotate(45) scale(%.1g)\"\n", diagonal/70.0);
fprintf(fp, " patternUnits=\"userSpaceOnUse\">\n");
fprintf(fp, " <polyline points=\" -.1, -.1 -.1, 1 1, 1 1, -1 -.1, -.1\" fill=\"white\"/>\n");
fprintf(fp, " </pattern>\n");
fprintf(fp, " <pattern id=\"f2\" width=\".2\" height=\".2\" stroke=\"white\"\n");
fprintf(fp, " stroke-linecap=\"square\" stroke-width=\"%.1g\"\n",linewidth);
fprintf(fp, " patternTransform=\"rotate(45) scale(%.1g)\"\n", diagonal/70.0);
fprintf(fp, " patternUnits=\"userSpaceOnUse\">\n");
fprintf(fp, " <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"2\"></line>\n");
fprintf(fp, " </pattern>\n");
fprintf(fp, " <pattern id=\"f3\" width=\".2\" height=\".2\" stroke=\"white\"\n");
fprintf(fp, " stroke-linecap=\"square\" stroke-width=\"%.1g\"\n",linewidth);
fprintf(fp, " patternTransform=\"rotate(135) scale(%.1g) translate(0.0)\"\n", diagonal/70.0);
fprintf(fp, " patternUnits=\"userSpaceOnUse\">\n");
fprintf(fp, " <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"2\"></line>\n");
fprintf(fp, " </pattern>\n");
fprintf(fp, " <pattern id=\"f4\" width=\".2\" height=\".2\" stroke=\"white\"\n");
fprintf(fp, " stroke-linecap=\"square\" stroke-width=\"%.1g\"\n",linewidth);
fprintf(fp, " patternTransform=\"rotate(0) scale(%.1g) translate(0.0)\"\n", diagonal/70.0);
fprintf(fp, " patternUnits=\"userSpaceOnUse\">\n");
fprintf(fp, " <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"2\"></line>\n");
fprintf(fp, " </pattern>\n");
fprintf(fp, " <pattern id=\"f5\" width=\".2\" height=\".2\" stroke=\"white\"\n");
fprintf(fp, " stroke-linecap=\"square\" stroke-width=\"%.1g\"\n",linewidth);
fprintf(fp, " patternTransform=\"rotate(90) scale(%.1g) translate(0.0)\"\n", diagonal/70.0);
fprintf(fp, " patternUnits=\"userSpaceOnUse\">\n");
fprintf(fp, " <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"2\"></line>\n");
fprintf(fp, " </pattern>\n");
fprintf(fp, " <pattern id=\"f6\" width=\".2\" height=\".2\" stroke=\"white\"\n");
fprintf(fp, " stroke-linecap=\"square\" stroke-width=\"%.1g\"\n",linewidth);
fprintf(fp, " patternTransform=\"rotate(0) scale(%.1g) translate(0.0)\"\n", diagonal/70.0);
fprintf(fp, " patternUnits=\"userSpaceOnUse\">\n");
fprintf(fp, " <line x1=\"0\" y1=\"0\" x2=\".2\" y2=\".2\"></line>\n");
fprintf(fp, " <line x1=\"0\" y1=\".2\" x2=\".2\" y2=\"0\"></line>\n");
fprintf(fp, " </pattern>\n");
fprintf(fp, " <pattern id=\"f7\" width=\".2\" height=\".2\" stroke=\"white\"\n");
fprintf(fp, " stroke-linecap=\"square\" stroke-width=\"%.1g\"\n",linewidth);
fprintf(fp, " patternTransform=\"rotate(45) scale(%.1g) translate(0.0)\"\n", diagonal/70.0);
fprintf(fp, " patternUnits=\"userSpaceOnUse\">\n");
fprintf(fp, " <line x1=\"0\" y1=\"0\" x2=\".2\" y2=\".2\"></line>\n");
fprintf(fp, " <line x1=\"0\" y1=\".2\" x2=\".2\" y2=\"0\"></line>\n");
fprintf(fp, " </pattern>\n");
fprintf(fp,"</defs>\n");
}
if (outputtype == WEB) {
fprintf(fp,"width=\"100%%\" viewBox=\"%g %g %g %g\" preserveAspectRatio=\"xMinYMin meet\">\n",
llx, lly, urx-llx, ury-lly);
fprintf(fp,"<style>");
fprintf(fp," rect:hover {");
fprintf(fp," opacity: 0.5");
fprintf(fp," }\n");
fprintf(fp,"</style>\n");
}
// fprintf(fp,"<h1> %s </h1>\n", dev);
// fprintf(fp,"<!-- program %s -->\n", prog);
} else if (outputtype == GERBER) {
fprintf(fp,"G04 Title: %s*\n", dev);
fprintf(fp,"G04 Creator: %s*\n", prog);
timep=time(&timep);
ctime_r(&timep, buf);
buf[strlen(buf)-1]='\0';
fprintf(fp,"G04 CreationDate: %s*\n", buf);
gethostname(buf,MAXBUF);
fprintf(fp,"G04 For: %s@%s (%s)*\n",
getpwuid(getuid())->pw_name, buf, getpwuid(getuid())->pw_gecos );
fprintf(fp,"G01*\n"); // linear interpolation
// fprintf(fp,"G70*\n"); // inches
fprintf(fp,"G90*\n"); // absolute
fprintf(fp,"%%MOIN*%%\n"); // absolute
fprintf(fp,"G04 Gerber Fmt 3.4, Leading zero omitted, Abs format*\n");
fprintf(fp,"%%FSLAX34Y34*%%\n");
fprintf(fp,"G04 Define D10 to be a 5mil circle*\n");
fprintf(fp,"%%ADD10C,.005*%%\n");
fprintf(fp,"G04 Make D10 the default aperture*\n");
fprintf(fp,"G54D10*\n");
return;
} else if (outputtype == DXF) {
fprintf(fp, " 0\n");
fprintf(fp, "SECTION\n");
fprintf(fp, " 2\n");
fprintf(fp, "ENTITIES\n");
return;
} else if (outputtype == HPGL) {
fprintf(fp, "IN;PU;\n");
fprintf(fp, "SP%d;\n", pennum);
// HPGL plots in units of 0.025 mm = 1016 ticks per inch...
// fprintf(fp, "page size is %f %f\n", pdx*1016.0, pdy*1016.0);
// fprintf(fp, "bb is llx:%f lly:%f urx:%f ury%f\n", llx, lly, urx, ury);
fprintf(fp, "IP %d,%d,%d,%d;\n", 0, 0, (int) ((urx-llx)*scale), (int) ((ury-lly)*scale) );
// flip y axis (Xwin puts 0,0 at top, increments going down)
fprintf(fp, "SC %.4f,%.4f,%.4f,%.4f;\n", llx, urx, ury, lly);
fprintf(fp, "PA0,0;\n");
//int i,j; char *ps;
//fprintf(fp,"CO \"define stipple patterns\";\n");
//for (i=0;(ps=get_stipple_bits(i))!=NULL;i++) {
// fprintf(fp,"RF%d,8,8",i);
// for (j=0; j<=7; j++) {
// for(k=0; k<=7; k++) {
// fprintf(fp, ",%c", ((ps[j]>>k)&1)?'1':'0');
// }
// }
// fprintf(fp,";\n");
//}
return;
} else if (outputtype == POSTSCRIPT) {
fprintf(fp,"%%!PS-Adobe-2.0\n");
fprintf(fp,"%%%%Title: %s\n", dev);
fprintf(fp,"%%%%Creator: %s\n", prog);
// timep=time(&timep);
fprintf(fp,"%%%%CreationDate: %s", ctime(&timep));
gethostname(buf,MAXBUF);
fprintf(fp,"%%%%For: %s@%s (%s)\n",
getpwuid(getuid())->pw_name,
buf,
getpwuid(getuid())->pw_gecos );
if (landscape) {
fprintf(fp,"%%%%Orientation: Landscape\n");
} else {
fprintf(fp,"%%%%Orientation: Portrait\n");
}
fprintf(fp,"%%%%Pages: (atend)\n");
fprintf(fp,"%%%%BoundingBox: 0 0 %d %d\n",
(int) (pdx*72.0), (int) (pdy*72.0));
fprintf(fp,"%%%%DocumentPaperSizes: custom\n");
fprintf(fp,"%%%%Magnification: 1.0000\n");
fprintf(fp,"%%%%EndComments\n");
fprintf(fp,"%%%%BeginSetup\n");
fprintf(fp,"mark {\n");
fprintf(fp,"%%BeginFeature: *PageRegion custom\n");
fprintf(fp,"<</PageSize [ %d %d ] /ImagingBBox null >> setpagedevice\n",
(int)( pdx*72.0), (int) (pdy*72.0));
fprintf(fp,"%%EndFeature\n");
fprintf(fp,"} stopped cleartomark\n");
fprintf(fp,"%%%%EndSetup\n");
fprintf(fp,"%%%%BeginProlog\n");
fprintf(fp,"/$Pig2psDict 200 dict def\n");
fprintf(fp,"$Pig2psDict begin\n");
fprintf(fp,"$Pig2psDict /mtrx matrix put\n");
if (colorflag==1) { // color
fprintf(fp,"/c-1 {0 setgray} bind def\n");
fprintf(fp,"/c0 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c1 {1.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c2 {0.000 1.000 0.000 srgb} bind def\n");
fprintf(fp,"/c3 {0.000 0.000 1.000 srgb} bind def\n");
fprintf(fp,"/c4 {0.000 1.000 1.000 srgb} bind def\n");
fprintf(fp,"/c5 {1.000 0.000 1.000 srgb} bind def\n");
fprintf(fp,"/c6 {1.000 0.700 0.000 srgb} bind def\n");
fprintf(fp,"/c7 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c8 {0.300 0.300 0.300 srgb} bind def\n");
fprintf(fp,"/c9 {0.600 0.600 0.600 srgb} bind def\n");
} else if (colorflag==0) { // black
fprintf(fp,"/c-1 {0 setgray} bind def\n");
fprintf(fp,"/c0 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c1 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c2 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c3 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c4 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c5 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c6 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c7 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c8 {0.300 0.300 0.300 srgb} bind def\n");
fprintf(fp,"/c9 {0.600 0.600 0.600 srgb} bind def\n");
} else { // gray
fprintf(fp,"/c-1 {0 setgray} bind def\n");
fprintf(fp,"/c0 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c1 {0.200 0.200 0.200 srgb} bind def\n");
fprintf(fp,"/c2 {0.300 0.300 0.300 srgb} bind def\n");
fprintf(fp,"/c3 {0.400 0.400 0.400 srgb} bind def\n");
fprintf(fp,"/c4 {0.500 0.500 0.500 srgb} bind def\n");
fprintf(fp,"/c5 {0.600 0.600 0.600 srgb} bind def\n");
fprintf(fp,"/c6 {0.700 0.700 0.700 srgb} bind def\n");
fprintf(fp,"/c7 {0.000 0.000 0.000 srgb} bind def\n");
fprintf(fp,"/c8 {0.300 0.300 0.300 srgb} bind def\n");
fprintf(fp,"/c9 {0.600 0.600 0.600 srgb} bind def\n");
}
fprintf(fp,"/cp {closepath} bind def\n");
fprintf(fp,"/ef {eofill} bind def\n");
fprintf(fp,"/gr {grestore} bind def\n");
fprintf(fp,"/gs {gsave} bind def\n");
fprintf(fp,"/sa {save} bind def\n");
fprintf(fp,"/rs {restore} bind def\n");
fprintf(fp,"/kc { currentrgbcolor [/Pattern /DeviceRGB] setcolorspace } bind def\n");
fprintf(fp,"/l {lineto} bind def\n");
fprintf(fp,"/m {moveto} bind def\n");
fprintf(fp,"/rm {rmoveto} bind def\n");
fprintf(fp,"/n {newpath} bind def\n");
fprintf(fp,"/s {stroke} bind def\n");
fprintf(fp,"/sh {show} bind def\n");
fprintf(fp,"/slc {setlinecap} bind def\n");
fprintf(fp,"/slj {setlinejoin} bind def\n");
fprintf(fp,"/slw {setlinewidth} bind def\n");
fprintf(fp,"/srgb {setrgbcolor} bind def\n");
fprintf(fp,"/rot {rotate} bind def\n");
fprintf(fp,"/sc {scale} bind def\n");
fprintf(fp,"/sd {setdash} bind def\n");
fprintf(fp,"/ff {findfont} bind def\n");
fprintf(fp,"/sf {setfont} bind def\n");
fprintf(fp,"/scf {scalefont} bind def\n");
fprintf(fp,"/sw {stringwidth} bind def\n");
fprintf(fp,"/tr {translate} bind def\n");
fprintf(fp,"%% start stipple patterns\n");
int i,j; unsigned char *ps;
for (i=0;(ps=get_stipple_bits(i))!=NULL;i++) {
fprintf(fp,"/p%d {\n",i);
fprintf(fp,".5 .5 scale 8 8 true matrix {<");
for (j=0; j<=7; j++) {
fprintf(fp,"%02x",ps[j]&255);
}
fprintf(fp,">} imagemask\n");
fprintf(fp,"} bind def\n");
fprintf(fp,"<< /PatternType 1\n");
fprintf(fp," /PaintType 2\n");
fprintf(fp," /TilingType 1\n");
fprintf(fp," /XStep 4 /YStep 4\n");
fprintf(fp," /BBox [0 0 8 8]\n");
fprintf(fp," /PaintProc { p%d }\n",i);
fprintf(fp,">>\n");
fprintf(fp,"matrix makepattern /f%d exch def\n",i);
}
fprintf(fp,"%% end stipple patterns\n");
fprintf(fp,"end\n");
fprintf(fp,"save\n");
// fprintf(fp,"newpath\n");
// fprintf(fp,"0 %d moveto\n", (int) (pdy*72.0));
// fprintf(fp,"0 0 lineto\n");
// fprintf(fp,"%d 0 lineto\n", (int) (pdx*72.0));
// fprintf(fp,"%d %d lineto closepath clip newpath\n",
// (int) (pdx*72.0), (int) (pdy*72.0));
fprintf(fp,"/$Pig2psBegin\n");
fprintf(fp,"{$Pig2psDict begin /$Pig2psEnteredState save def}def\n");
fprintf(fp,"/$Pig2psEnd {$Pig2psEnteredState restore end} def\n");
fprintf(fp,"%%BeginPageSetup\n");
fprintf(fp,"%%BB is %f,%f %f,%f\n", llx, lly, urx, ury);
if (landscape) {
fprintf(fp,"%f %f scale\n", scale, scale);
fprintf(fp,"%f %f translate\n",
(ymax/(2.0*scale))+ymid, (xmax/(2.0*scale))-xmid);
fprintf(fp,"90 rotate\n");
} else {
fprintf(fp,"%f %f scale\n", scale, scale);
fprintf(fp,"%f %f translate\n",
(xmax/(2.0*scale))-xmid, (ymax/(2.0*scale))-ymid);
}
fprintf(fp,"%%EndPageSetup\n");
fprintf(fp,"%%%%EndProlog\n");
fprintf(fp,"$Pig2psBegin\n");
fprintf(fp,"10 setmiterlimit\n");
fprintf(fp,"1 slj 1 slc\n");
fprintf(fp,"%.1f slw\n",linewidth);
fprintf(fp,"%% here starts figure;\n");
return;
} else {
printf("unrecognized plot type in postscript.c\n");
return;
}
}
void ps_comment(char *comment)
{
if ((fp != NULL)) {
if (outputtype == GERBER) { // GERBER
;
} else if (outputtype == HPGL) { // HPGL
;
} else if (outputtype == DXF) { // DXF
;
} else if (outputtype == POSTSCRIPT) { // PS
;
} else if (outputtype == SVG) { // Scalable vector graphics
// fprintf(fp, "<!--%s-->\n",comment);
} else if (outputtype == WEB) { // WEB = html
// fprintf(fp, "<!--%s-->\n",comment);
} else if (outputtype == AUTOPLOT) { // AUTOPLOT
fprintf(fp, "#%s\n",comment);
}
}
}
const char *get_svg_fill_opacity(int fill)
{
switch (fill%8) {
case 0:
return("0.0");
break;
case 1:
return("1.0");
break;
case 2:
return("0.9");
break;
case 3:
return("0.8");
break;
case 4:
return("0.6");
break;
case 5:
return("0.5");
break;
case 6:
return("0.4");
break;
case 7:
return("0.3");
break;
default:
case 8:
return("0.2");
break;
}
}
const char *get_svg_fill(int fill)
{
if (colorflag == 1) { // color mode
switch (fill % 10) {
case 0:
return ("0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"); // black
break;
case 1:
return ("1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"); // red
break;
case 2:
return ("0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0"); // green
break;
case 3:
return ("0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0"); // blue
break;
case 4:
return ("0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0"); // cyan
break;
case 5:
return ("1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0"); // magenta
break;
case 6:
return ("1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0"); // yellow
break;
case 7:
return ("0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"); // black
break;
case 8:
return ("0.25 0 0 0 0 0 0.25 0 0 0 0 0 0.25 0 0 0 0 0 1 0"); // grey1
break;
case 9:
return ("0.50 0 0 0 0 0 0.50 0 0 0 0 0 0.50 0 0 0 0 0 1 0"); // grey2
break;
}
} else if (colorflag == 0) { // black
return ("0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0");
} else { // greyscale
switch (fill % 10) {
case 0:
return ("0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0");
break;
case 1:
return ("0.1 0 0 0 0 0 0.1 0 0 0 0 0 0.1 0 0 0 0 0 1 0");
break;
case 2:
return ("0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0");
break;
case 3:
return ("0.3 0 0 0 0 0 0.3 0 0 0 0 0 0.3 0 0 0 0 0 1 0");
break;
case 4:
return ("0.4 0 0 0 0 0 0.4 0 0 0 0 0 0.4 0 0 0 0 0 1 0");
break;
case 5:
return ("0.5 0 0 0 0 0 0.5 0 0 0 0 0 0.5 0 0 0 0 0 1 0");
break;
case 6:
return ("0.6 0 0 0 0 0 0.6 0 0 0 0 0 0.6 0 0 0 0 0 1 0");
break;
case 7:
return ("0.7 0 0 0 0 0 0.7 0 0 0 0 0 0.7 0 0 0 0 0 1 0");
break;
case 8:
return ("0.8 0 0 0 0 0 0.8 0 0 0 0 0 0.8 0 0 0 0 0 1 0");
break;
case 9:
return ("0.9 0 0 0 0 0 0.9 0 0 0 0 0 0.9 0 0 0 0 0 1 0");
break;
}
}
return ("0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0");
}
void ps_end_line()
{
extern int this_pen;
extern int this_line;
extern int in_line;
extern int this_isfilled;
int debug = 0;
if (debug) printf("ps_end_line:\n");
if (outputtype == GERBER) { // GERBER
if (in_poly) {
fprintf(fp, "G37*\n");
}
} else if (outputtype == SVG || outputtype == WEB) { // SVG
fprintf(fp,"\"\n");
fprintf(fp, "stroke=\"%s\"\n", pen_to_svg_color(this_pen));
fprintf(fp, "stroke-width=\"%g\"\n", linewidth);
if (in_poly) {
// fprintf(fp, "fill=\"%s\"\n",get_svg_fill(this_fill,this_pen));
fprintf(fp, "filter=\"url(#c%d)\"\n", this_pen);
fprintf(fp, "fill=\"url(#f%d)\"\n",this_fill);
fprintf(fp, "fill-opacity=\"%s\"\n",
get_svg_fill_opacity(this_fill));
} else {
fprintf(fp, "fill=\"none\"\n");
}
fprintf(fp, "stroke-linejoin=\"round\" %s/>\n", xwin_svg_dashes(this_line));
} else if (outputtype == HPGL) { // HPGL
if (in_poly) {
fprintf(fp, "PM2;\n");
if (this_isfilled) {
fprintf(fp, "%s\n", get_hpgl_fill(this_fill));
fprintf(fp, "LT; FP;\n");
}
if (this_line == 0) {
fprintf(fp, "LT;EP;\n");
} else {
fprintf(fp, "LT%d;EP;\n", this_line);
}
}
} else if (outputtype == POSTSCRIPT) {
fprintf(fp, "c%d ", this_pen);
if (in_poly) {
fprintf(fp, "gs kc f%d setpattern fill gr\n",
get_stipple_index(this_fill,this_pen));
}
fprintf(fp, "%s ", xwin_ps_dashes(this_line));
fprintf(fp, "s\n");
}
in_poly=0;
in_line=0;
}
void ps_flush() {
if (in_line) {
ps_end_line();
}
in_line=0;
}
double xold, yold;
int in_progress=0;
void ps_start_line(double x1, double y1, int filled)
{
extern int pennum;
extern int linetype;
extern int in_line;
extern int this_pen;
extern int this_line;
extern int this_fill;
extern int this_isfilled;
if (debug) printf("ps_start_line:\n");
if (in_line) {
ps_end_line();
}
in_line++;
if (filled) {
if (outputtype == GERBER) { // GERBER
fprintf(fp,"G04 LAYER %d *\n", layer);
fprintf(fp, "G36*\n");
}
in_poly++;
}
this_pen=pennum; /* save characteristics at start of line */
this_line=linetype;
this_fill=filltype;
this_isfilled=filled;
if (outputtype == SVG || outputtype == WEB) { // SVG
fprintf(fp, "<polyline points=\"\n");
y1 = bblly-(y1-bbury);
V_to_R(&x1,&y1);
fprintf(fp, " %g,%g\n", x1, y1);
} else if (outputtype == POSTSCRIPT) { // Postscript
fprintf(fp, "n\n");
// flip y coordinate from Xwin coords
y1 = bblly-(y1-bbury);
fprintf(fp, "%.10g %.10g m\n",x1, y1);
} else if (outputtype == AUTOPLOT) { // AUTOPLOT
fprintf(fp, "jump\n");
fprintf(fp, "pen %d\n", pennum);
V_to_R(&x1, &y1);
fprintf(fp, "%.10g %.10g\n",x1, y1);
} else if (outputtype == GERBER) { // GERBER
V_to_R(&x1, &y1);
if (!in_poly) {
fprintf(fp,"G04 LAYER %d *\n", layer);
}
fprintf(fp, "X%04dY%04dD02*\n",(int)((x1*10000.0)+0.5), (int)((y1*10000.0)+0.5));
} else if (outputtype == DXF) { // DXF
V_to_R(&x1, &y1);
// fprintf(fp, " 0\n"); // start of entity
// fprintf(fp, "LINE\n"); // line type entity
// fprintf(fp, " 8\n"); // layer name to follow
// fprintf(fp, "%d\n",layer); // layer name
// fprintf(fp, " 62\n"); // color flag
// fprintf(fp, "%d\n", pennum); // color
// fprintf(fp, " 10\n%.10g\n", x1); // initial x value
// fprintf(fp, " 20\n%.10g\n", y1); // initial y value
xold=x1;
yold=y1;
in_progress=0;
} else if (outputtype == HPGL) {
if (!filled) {
if (this_line == 0) {
fprintf(fp, "LT;\n");
} else {
fprintf(fp, "LT%d;\n", this_line);
}
}
fprintf(fp, "SP%d;\n", this_pen);
fprintf(fp, "PU %.4f,%.4f;\n",x1,y1);
if (filled) {
fprintf(fp,"PM0;\n");
}
}
}
void ps_continue_line(double x1, double y1)
{
if (debug) printf("ps_continue_line:\n");
if (outputtype == SVG || outputtype == WEB) { // SVG
y1 = bblly-(y1-bbury);
V_to_R(&x1, &y1);
fprintf(fp, " %g,%g\n", x1, y1);
} else if (outputtype == POSTSCRIPT) {
// flip y coordinate from Xwin coords
y1 = bblly-(y1-bbury);
fprintf(fp, "%.10g %.10g l\n",x1, y1);
} else if (outputtype == AUTOPLOT) { // AUTOPLOT
V_to_R(&x1, &y1);
fprintf(fp, "%.10g %.10g\n",x1, y1);
} else if (outputtype == GERBER) { // GERBER
V_to_R(&x1, &y1);
fprintf(fp, "X%04dY%04dD01*\n",(int)((x1*10000.0)+0.5), (int)((y1*10000.0)+0.5));
} else if (outputtype == DXF) {
V_to_R(&x1, &y1);
if (!in_progress) {
fprintf(fp, " 0\n"); // start of entity
fprintf(fp, "LINE\n"); // line type entity
fprintf(fp, " 8\n"); // layer name to follow
fprintf(fp, "%d\n",layer); // layer name
fprintf(fp, " 62\n"); // color flag
fprintf(fp, "%d\n", pennum); // color
fprintf(fp, " 10\n%.10g\n", xold); // initial x value
fprintf(fp, " 20\n%.10g\n", yold); // initial y value
fprintf(fp, " 11\n%.10f\n", x1); // initial x value
fprintf(fp, " 21\n%.10f\n", y1); // initial y value
in_progress++;
} else {
fprintf(fp, " 0\n"); // start of entity
fprintf(fp, "LINE\n"); // line type entity
fprintf(fp, " 8\n"); // layer name to follow
fprintf(fp, "%d\n",layer); // layer name
fprintf(fp, " 62\n"); // color flag
fprintf(fp, "%d\n", pennum); // color
fprintf(fp, " 10\n%.10f\n", xold); // initial x value
fprintf(fp, " 20\n%.10f\n", yold); // initial y value
fprintf(fp, " 11\n%.10f\n", x1); // initial x value
fprintf(fp, " 21\n%.10f\n", y1); // initial y value
}
xold = x1;
yold = y1;
} else if (outputtype == HPGL) { // HPGL
fprintf(fp, "PD %.4f,%.4f;\n",x1,y1);
}
in_line++;
}
void ps_postamble()
{
// time_t timep;
// char buf[MAXBUF];
if (debug) printf("ps_postamble:\n");
if (in_line) {
ps_end_line();
in_line=0;
}
if (outputtype == POSTSCRIPT) {
fprintf(fp,"%% here ends figure;\n");
fprintf(fp,"$Pig2psEnd\n");
fprintf(fp,"showpage\n");
fprintf(fp,"%%%%EOF\n");
} else if (outputtype == GERBER) {
fprintf(fp,"G04 LAYER 999 *\n");
fprintf(fp,"M02*\n");
} else if (outputtype == DXF) {
fprintf(fp," 0\nENDSEC\n");
fprintf(fp," 0\nEOF\n");
} else if (outputtype == HPGL) {
fprintf(fp,"PU;\n");
fprintf(fp,"SP;\n");
fprintf(fp,"IN;\n");
} else if (outputtype == SVG || outputtype == WEB) {
fprintf(fp,"</svg>\n");
if (outputtype == WEB) {
fprintf(fp,"</body>\n");
fprintf(fp,"</html>\n");
}
//fprintf(fp,"<hr>\n");
//timep=time(&timep);
//ctime_r(&timep, buf);
//buf[strlen(buf)-1]='\0';
////fprintf(fp,"<p> creation date %s<br>\n", buf);
//gethostname(buf,MAXBUF);
//fprintf(fp,"<p> For: %s@%s (%s) <br>\n",
// getpwuid(getuid())->pw_name,
// buf,
// getpwuid(getuid())->pw_gecos );
}
fclose(fp); fp=NULL;
}
void ps_link(int nest, char *name, double xmin, double ymin, double xmax, double ymax) {
// FIXME: protect against null pointer...
char *p; // link to prefix
char *s; // link to suffix
char *e;
char buf[MAXBUF];
strncpy(buf, name, MAXBUF);
if ((fp != NULL) && outputtype == WEB && nest==1) {
if ((e=strstr(buf, "_sym"))!=NULL) { // check if sym