forked from erichVK5/translate2geda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plotter.java
2200 lines (1833 loc) · 70.4 KB
/
Plotter.java
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
// v1.0
// Copyright 2000 Philipp Knirsch
// Written by Philipp Knirsch
//
// v1.1
// modifications by Erich Heinzle 2015, to
// support PCB design
// fixed metric/inches flag detection when parsing
// added a long nanometre Polygon class to improve pad detection
//
// v1.2
// modifications by Erich Heinzle 2016, to
// support headless operation
//
// Latest modifications to allow use in translate2geda.
// Hacky but it works.
// TODO
// - Graphic export is AWT 1.1 vintage and a bit broken currently.
// - Eliminate use of doubles and use long nanometres for all dimensions
//
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//
// RS-274X virtual plotter
//
// This class now is a virtual plotter. It can take an input in RS-274X
// format and can either render it into a Graphics context previously
// set using the setGraphics() method and scaling the image by the
// factors for the X and Y axis.
//
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Rectangle; // use for bounds of polygons
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;
import java.util.StringTokenizer;
// EH addition
// something for writing out PCB primitives
import java.io.PrintWriter;
import java.io.*;
// and some stuff for png graphics
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
//
//
// This is our virtual plotter. It is mainly a state machine simulating a
// real plotter with some additional interface methods. Some of them are
// again corresponding to the real world plotter, like the plotInput()
// method, others are more for interaction with the main program, like
// setGraphics() or setScale().
//
class Plotter {
static final boolean DEBUG = false; //true; //false;
Graphics g; // Graphics context into which the plotter renders
BufferedImage bi; // try this here
Graphics2D bg;
int xsize; // X size of Graphics in pixels
int ysize; // Y size of Graphics in pixels
boolean getDim; // Flag if we are currently calculating the dimension
boolean firstCoordsRead = false;
// if getDim is to work with images way off
// centre then we need this - ESH
double xmin; // Minimum value for X-axis
double ymin; // Minimum value for Y-axis
double xmax; // Maximum value for X-axis
double ymax; // Maximum value for Y-axis
double sx; // Scaling parameter of X-axis
double sy; // Scaling parameter of Y-axis
// added by eh for PCB / kicad layout output
// int stupidPADSoffset = -2900; // for 10x
// PADS files have a strange Y offset
// -1500 for 5x
//
// first, an ability to centre the displaced gerber data would be nice
boolean imageCentred;
boolean mirroredYaxis = false;
boolean headless = true;
double xOffset = 0.0; //-1.0; //0.0; // -1.0;
double yOffset = 0.0; //-28.0; //0.0; // -28.0;
// double nxOffset = -1.0; // not needed
//double nyOffset = -28.0; // not needed
double tempx;
double tempy;
double tempnx;
double tempny;
// now, a flag for generating PCB layout drawing primitive
String PCBelements = "";
boolean generatePCBelements = true;
String outputFileName = "PCBlayoutData.pcb";
long rectangularPolygonPadCount = 0;
// EH addition
// if (generatePCBelements) {
File outputFile = new File(outputFileName);
// here endeth EH additions
// we will make use of int pos below as a pad/track label
// added for centring and PCN element export
Vector cmdVector;// Vector containing all single commands and tokens
int pos; // Global position in cmdVector for parser
boolean ext; // Flag wether we are inside an extended command
// State variables. We have quite a few of them. First the current
// position. Then the selected apterture, followed by the drawing mode.
// We have also various interpolations states including a scaling factor
// for linear interpolations and several flags for area filling,
// multiquadrant interpolation and absolut or incremental positioning.
// At last we also have a vector with the points of the area fill...
double x; // Current X position
double y; // Current Y position
double i; // X center of arc
double j; // Y center of arc
int d; // Current draft
// Aperture types
static final int CIRCLE = 1;
static final int RECTANGLE = 2;
static final int OVAL = 3;
static final int POLYGON = 4;
double[][] ad; // Aperture definitions array
// Drawing modes
static final int ON = 1;
static final int OFF = 2;
static final int FLASH = 3;
int draw; // Current drawing mode
int drawColor;// Current drawing color (black==0 or white==1)
// Interpolation modes
static final int LINEAR = 0;
static final int CLOCK = 1;
static final int CCLOCK = 2;
int interpol;// Current interpolation
double scale; // Scale for linear interpolation (10, 1, .1, .01)
boolean metric; // Flag if the data is in inches or millimeters.
boolean areafill;// Flag for area fill
Polygon points; // Polygon of the area to be filled
LongPolygon longPoints; // Polygon to hold nanometre dimensions
boolean multi; // Flag for single or multiquadrant interpolation
boolean absolute;// Flag for absolute or incremental positioning
boolean stop; // Stop program flag
// Variables of the state machine for the extended commands
boolean omitLZeros;// Omit leading or trailing zeros.
static final String[] trailZeros = { "", "0", "00", "000", "0000", "00000", "000000", "0000000", "00000000", "000000000", "0000000000", "00000000000", "000000000000"};
int xlen; // Complete number of digits a X-axis number can have
int xdigits; // Number of digits of X-axis after decimal.
int ylen; // Complete number of digits a Y-axis number can have
int ydigits; // Number of digits of Y-axis after decimal.
// Helper array for parseDouble() method
static final double[] div = {1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0};
private class LongPolygon {
int initialVertices = 20;
int vertexCount = 0;
int pointCount = 0;
long[] xcoords = new long[initialVertices];
long[] ycoords = new long[initialVertices];
long yPosition = 0;
long xPosition = 0;
public LongPolygon() {
pointCount = 0;
vertexCount = 0;
xPosition = 0;
yPosition = 0;
xcoords[pointCount] = 0;
ycoords[pointCount] = 0;
}
public LongPolygon(long x, long y) { // x,y are position of polygon
pointCount = 0;
vertexCount = 0;
xcoords[pointCount] = x;
ycoords[pointCount] = y; // to allow centroid to return a meaningful value for null polygon
xPosition = x;
yPosition = y;
}
public Polygon polygon() {
Polygon traditionalPolygon = new Polygon();
return traditionalPolygon;
}
public int npoints() {
return pointCount;
}
public void addPoint(long x, long y) {
xcoords[pointCount] = x;
ycoords[pointCount] = y;
if ((xcoords[0] == x) && (ycoords[0] == y)) {
vertexCount = pointCount;
}
if (DEBUG) {
System.out.println("Added longPoly points: x : " + x + ", y: " + y);
}
pointCount++;
if (pointCount == xcoords.length) {
long[] newXcoords = new long[xcoords.length * 2];
long[] newYcoords = new long[xcoords.length * 2];
for (int i = 0; i < pointCount; i++) {
newXcoords[i] = xcoords[i];
newYcoords[i] = ycoords[i];
}
xcoords = newXcoords;
ycoords = newYcoords;
}
}
public int vertexCount() {
return vertexCount;
}
public long centroidX() {
long Xcentroid = 0;
long Xmax = xcoords[0];
long Xmin = xcoords[0];
for (int i = 1; i < pointCount; i ++) {
if (Xmax < xcoords[i]) {
Xmax = xcoords[i];
}
if (Xmin > xcoords[i]) {
Xmin = xcoords[i];
}
}
Xcentroid = (Xmax - Xmin)/2 + Xmin;
return Xcentroid;
}
public long centroidY() {
long Ycentroid = 0;
long Ymax = ycoords[0];
long Ymin = ycoords[0];
for (int i = 1; i < pointCount; i ++) {
if (Ymax < ycoords[i]) {
Ymax = ycoords[i];
}
if (Ymin > ycoords[i]) {
Ymin = ycoords[i];
}
}
Ycentroid = (Ymax - Ymin)/2 + Ymin;
return Ycentroid;
}
public long xSize(){
long xSize = 0;
long Xmax = xcoords[0];
long Xmin = xcoords[0];
for (int i = 1; i < pointCount; i ++) {
if (Xmax < xcoords[i]) {
Xmax = xcoords[i];
}
if (Xmin > xcoords[i]) {
Xmin = xcoords[i];
}
}
xSize = (Xmax - Xmin);
if (xSize < 0) {
xSize = -xSize;
}
return xSize;
}
public long ySize() {
long ySize = 0;
long Ymax = ycoords[0];
long Ymin = ycoords[0];
for (int i = 1; i < pointCount; i ++) {
if (Ymax < ycoords[i]) {
Ymax = ycoords[i];
}
if (Ymin > ycoords[i]) {
Ymin = ycoords[i];
}
}
ySize = (Ymax - Ymin);
if (ySize < 0) {
ySize = -ySize;
}
return ySize;
}
public void clearPolygon() {
pointCount = 0;
vertexCount = 0;
xcoords[pointCount] = 0;
ycoords[pointCount] = 0;
}
}
//
// We don't have to do a lot in the public constructor, only initialize a
// few variables.
//
public Plotter() {
g = null; // No default Graphics context
// bg = null; //try here
getDim = false; // We usually don't calc the dim
sx = 1.0; // Default is 1:1 scaling on X-axis
sy = 1.0; // Default is 1:1 scaling on Y-axis
cmdVector = new Vector(); // New command vector
points = new Polygon(); // Polygon for area fill
longPoints = new LongPolygon(); // for storing nanometre dimensions and returning centroid
imageCentred = false;
mirroredYaxis = false;
}
public boolean centred() {
return imageCentred;
}
public void setMirrorYaxis() {
mirroredYaxis = !mirroredYaxis;
}
//
// Sets the Graphics context into which the virtual plotter will do it's
// rendering. This allows us to render into any any Java Graphics device,
// from a on screen Canvas over an off screen image up to a printer.
//
public void setGraphics(Graphics ig) {
g = ig;
}
//
// Sets the size in pixles for our Graphics context. Unfortunately we
// can't extract that from the Graphics object itself, so we have to set
// it prior to drawing... One more thing missing in the Java 1.1 stuff...
//
public void setSize(int x, int y) {
xsize = x;
ysize = y;
// try this
if (!headless) {
bi = new BufferedImage(x ,y, BufferedImage.TYPE_INT_RGB);
bg = bi.createGraphics();
bg.setColor(Color.WHITE);
bg.fillRect(0, 0, x, y);
bg.setColor(Color.BLACK);
}
}
//
// Sets the scaling factor on x and y axis. We might want this to either
// zoom the image or similar stuff.
//
public void setScale(double x, double y) {
sx = x;
sy = y;
}
//
// Tries to parse and plot the given input. A Graphic context into which
// the plot will be drawn has to be set prior to a call to this method.
// THe Graphic context itself may be any Java Graphic, thereby enabling
// either rendering on screen, in an off-screen image or even on a
// printer.
//
// 4/25/00: Small new feature: We do the 'drawing' twice. In the first
// run we keep a record of the dimensions of the drawing area, which we
// the use in the second run to scale the plot to match the display
// as good as possible.
//
public boolean plotInput(String in) throws IOException {
if ((g == null) && !headless) {
System.out.println("Error: No Graphics context for plotting set.");
return false;
}
if ((in == null) || in.equals("")) {
System.out.println("Error: Input is null or empty.");
return false;
}
// PrintWriter PCBelementFile = new PrintWriter(outputFile);
// firstCoordsRead = false; // to allow centring to work
// xOffset = 0.0;
// yOffset = 0.0;
// getDim = true;
getDim = false; // new
initStateMachine();
parseInput(in, 0);
// processInput(999999999);
// we now sort out the necessary offsets to centre at 0,0
// xOffset = ((xmin-xmax)/2.0);
// yOffset = ((ymin-ymax)/2.0);
// getDim = false;
// initStateMachine();
// parseInput(in, 0);
return processInput(999999999);
}
// a modified plotImage routine to centre and plot the image
public boolean centreImage(String in) throws IOException {
if ((g == null) && !headless) {
System.out.println("Error: No Graphics context for plotting set.");
return false;
}
if (in == null || in.equals("")) {
System.out.println("Error: Input is null or empty.");
return false;
}
// PrintWriter PCBelementFile = new PrintWriter(outputFile);
// if (!imageCentred) {
firstCoordsRead = false; // to allow centring to work
xOffset = 0.0;
yOffset = 0.0;
getDim = true;
initStateMachine();
parseInput(in, 0);
processInput(999999999);
//PCBelementFile.println(PCBelements);
//PCBelementFile.close();
// we now sort out the necessary offsets to centre at 0,0
xOffset = ((xmin-xmax)/2.0);
yOffset = ((ymin-ymax)/2.0);
imageCentred = true;
// }
getDim = false;
initStateMachine();
parseInput(in, 0);
return processInput(999999999);
}
// 27/7/2015
// Modified plotInput method tries to parse and convert the given
// input into a gEDA PCB footprint file.
// The method should ideally be passed the gerber filename
//
// 4/25/00: Small new feature: We do the 'drawing' twice. In the first
// run we keep a record of the dimensions of the drawing area, which we
// the use in the second run to scale the plot to match the display
// as good as possible.
//
public boolean generatePCBFile(String in, String filename) throws IOException {
if ((in == null) || in.equals("")) {
System.out.println("Error: Input is null or empty.");
return false;
}
firstCoordsRead = false; // to allow centring to work
// xOffset = 0.0;
//yOffset = 0.0;
PCBelements = "";
// done to avoid doubling up data with successive
// conversions of the same or different gerber files
try {
File footprintFile = new File(filename + ".fp");
PrintWriter PCBelementFile = new PrintWriter(footprintFile);
// getDim = true; // actually, we don't care with footprints
// if the gerber is not well centred
getDim = false;
initStateMachine();
parseInput(in, 0);
processInput(999999999);
PCBelements = "Element[\"\" \""
+ "convertedGerber"
+ "\" \"\" \"\" 0 0 0 -3000 0 100 \"\"]\n(\n"
+ PCBelements
+ ")";
PCBelementFile.println(PCBelements);
PCBelementFile.close();
}
catch (Exception e) {
System.out.println("Error: Couldn't write footprint file.");
return false;
}
PCBelements = ""; // clear it out in case we convert more.
// getDim = false;
// initStateMachine();
//parseInput(in, 0);
return true; // processInput(999999999);
}
//
// Simple method to (re)initialize the state machine. We do this only
// once at the beginning of the plotInput().
//
public void initStateMachine() {
// Reset the state variables of the plotter
x = 0.0;
y = 0.0;
d = 10;
ad = new double[1000][6];
ad[10] = new double[]{1, 0.025, 0, 0, 0, 0};
draw = OFF;
drawColor = 0;
interpol = LINEAR;
scale = 1.0;
metric = true;
areafill = false;
multi = false;
absolute = true;
stop = false;
omitLZeros = true;
xlen = 5;
xdigits = 3;
ylen = 5;
ydigits = 3;
// New Polygon for area fill
points = new Polygon();
// Clean up command vector
cmdVector.removeAllElements();
pos = 0; // Reset initial parser position
ext = false; // We start of with normal commands
}
//
// Here we split up the whole input into pieces to make processing it
// much easier. Actually, we do something really nasty here. For the IF
// extended command, we read in the file and insert the just read file
// directly into out cmdVector. As we hopefully won't encounter this too
// often this is a nice hack to do it. Other wise we would have to do
// recursive calls which might drain our stack space.
//
public void parseInput(String in, int pos) {
if (DEBUG)
System.out.println("Debug: Start parsing input.");
// Parse through the whole input and store every command and token
// in the cmdVector for later processing
StringTokenizer st = new StringTokenizer(in,"%*", true);
// Store all commands and tokens in cmdVector
while (st.hasMoreTokens()) {
cmdVector.insertElementAt(st.nextToken(), pos++);
}
if (DEBUG)
System.out.println("Debug: End parsing input.");
}
//
// And finally we do the drawing. Here the real deal is being done, namely
// the rendering of the image.
//
// 4/23/00: As of today we can instruct the process input to process a
// certain number of commands instead of all of them.
//
public boolean processInput(int numCmd) throws IOException {
String cmd; // Current command
if (DEBUG) {
System.out.println("Debug: Start processing input.");
}
// EH addition
if (DEBUG && generatePCBelements) {
System.out.println("Debug: Using " + outputFileName
+ " for layout primitives");
}
// EH addition end
// As long as there are more commands avaliable continue
// If a command is found and executed, the position will be incremented
// in those methods, so we don't need to do it here.
while (pos < cmdVector.size()) {
// Get the current command from the cmdVector
cmd = (String) cmdVector.elementAt(pos);
if (cmd == null) {
cmd = "";
}
if (DEBUG)
System.out.println("Debug: Processing command "+cmd);
// If we entered or exited an eXtended command, switch the flag
if (cmd.equals("%")) {
ext = !ext;
if (ext) {
if (DEBUG)
System.out.println("Debug: Start of extended command");
}
else {
if (DEBUG)
System.out.println("Debug: End of extended command");
}
pos++;
//continue;
}
// Always skip the dang "*" tokens as they don't interest
if (cmd.equals("*")) {
pos++;
//continue;
}
// Handle eXtended RS-274X command
if (ext) {
if (handleExtendedCmd() == false) {
System.out.println("Error: Failed executing command.");
if (DEBUG)
System.out.println("Debug: End processing input.");
return false;
}
}
// Handle RS-274D command
else {
if (handleNormalCmd() == false) {
System.out.println("Error: Failed executing command.");
if (DEBUG)
System.out.println("Debug: End processing input.");
return false;
}
}
// Ha! We encountered a stop command, so lets do it!
if (stop) {
if (DEBUG)
System.out.println("Debug: End processing input.");
return true;
}
numCmd--;
if (numCmd == 0)
return true;
}
// We reached the input without a M00 or M02... Ah well, maybe they
// simply forgot to put it in... Not really critical, is it?! ;)
// EH addition - moved to calling routine
// have we been writing PCB drawing primitives
// to a file, if so, close it
// if (generatePCBelements) {
// File outputFile = new outputfile(outputFileName);
// PCBelementFile.println(PCBelements);
// PCBelementFile.close();
//}
//
if (DEBUG)
System.out.println("Debug: End processing input.");
return true;
}
//
// Handles an eXtended RS-274X command. They always start with a 2
// character specifier, followed by various parameters, sometimes even
// more commands.
//
private boolean handleExtendedCmd() {
String cmd;
// As long as there are more command available
while (pos < cmdVector.size()) {
// Get the current command from the cmdVector
cmd = (String) cmdVector.elementAt(pos);
// see if this helps gcj
if (cmd == null) {
cmd = "";
} else {
cmd = cmd.trim();
} // AND IT DOES!!!!!!! gcj does not trim CR properly
// We came to the end of that eXtended command without problems,
// so return true.
if (cmd.equals("%")) {
return true;
}
if (DEBUG)
System.out.println("Debug: Executing extended command "+cmd);
// Handle a single command. Might consist of several 'subcommands'
// like G01X100Y200D02. We simply collect them one after the other
// and execute the effect after we have processed all subcommands.
while (cmd.length() > 0) {
// EH addition
// metric flag on by default, look for inches
if (cmd.startsWith("MOIN")) {
metric = false;
cmd = "";
if (DEBUG) {
System.out.println("Debug: Inch units detected");
}
}
// Aperture definition. This is a pretty complex thing, i hope
// i'm doing it right. :)
if (cmd.startsWith("ADD")) {
int nl = numberLength(cmd, 3);
int pd = parseInteger(cmd.substring(3, nl));
cmd = cmd.substring(nl);
if (cmd == null) {
cmd = "";
}
// New circle aperture
if (cmd.startsWith("C,")) {
ad[pd][0] = CIRCLE;
cmd = cmd.substring(2);
}
if (cmd == null) {
cmd = "";
}
// New circle aperture
if (cmd.startsWith("R,")) {
ad[pd][0] = RECTANGLE;
cmd = cmd.substring(2);
}
if (cmd == null) {
cmd = "";
}
// New circle aperture
if (cmd.startsWith("O,")) {
ad[pd][0] = OVAL;
cmd = cmd.substring(2);
}
if (cmd == null) {
cmd = "";
}
// New circle aperture
if (cmd.startsWith("P,")) {
ad[pd][0] = POLYGON;
cmd = cmd.substring(2);
}
if (cmd == null) {
cmd = "";
}
nl = 1;
StringTokenizer st = new StringTokenizer(cmd, "X");
while (st.hasMoreTokens()) {
ad[pd][nl++] = parseDouble(st.nextToken());
}
}
// Axis selection. We simply skip it, as portrait or land-
// scape really doesn't matter for pictures here.
if (cmd.startsWith("ASAXBY") || cmd.startsWith("ASAYBX")) {
cmd = cmd.substring(6);
if (cmd == null) {
cmd = "";
}
}
// Parse the format statement, one of the most important
// extended commands.
if (cmd.startsWith("FS")) {
cmd = cmd.substring(2);
if (cmd == null) {
cmd = "";
}
// Decided if we omit leading or trailing zeros
if (cmd.startsWith("L")) {
omitLZeros = true;
}
else {
omitLZeros = false;
}
cmd = cmd.substring(1);
if (cmd == null) {
cmd = "";
}
// Are we using absolute or relative coordinates
if (cmd.startsWith("A")) {
absolute = true;
}
else {
absolute = false;
}
cmd = cmd.substring(1);
if (cmd == null) {
cmd = "";
}
// Our parser is clever enough to parse any number after N
if (cmd.startsWith("N")) {
cmd = cmd.substring(2);
}
if (cmd == null) {
cmd = "";
}
// Our parser is clever enough to parse any number after G
if (cmd.startsWith("G")) {
cmd = cmd.substring(2);
if (cmd == null) {
cmd = "";
}
}
if (cmd.startsWith("X")) {
xdigits = parseInteger(cmd.substring(2,3));
xlen = xdigits + parseInteger(cmd.substring(1,2));
cmd = cmd.substring(3);
if (cmd == null) {
cmd = "";
}
}
if (cmd.startsWith("Y")) {
ydigits = parseInteger(cmd.substring(2,3));
ylen = ydigits + parseInteger(cmd.substring(1,2));
cmd = cmd.substring(3);
if (cmd == null) {
cmd = "";
}
}
// Our parser is clever enough to parse any number after D
if (cmd.startsWith("D")) {
cmd = cmd.substring(2);
}
if (cmd == null) {
cmd = "";
}
// Our parser is clever enough to parse any number after M
if (cmd.startsWith("M")) {
cmd = cmd.substring(2);
if (cmd == null) {
cmd = "";
}
}
}
// Include file. Really nasty trick how we do it, but it works
// just fine :)
if (cmd.startsWith("IF")) {
includeFile(cmd.substring(2));
cmd = "";
if (cmd == null) {
cmd = "";
}
}
// Set layer to clear
if (cmd.startsWith("LPC")) {
drawColor = 1;
if (!headless) {
g.setColor(Color.white);
bg.setColor(Color.white); // try
}
cmd = cmd.substring(3);
if (cmd == null) {
cmd = "";
}
}
// Set layer to dark
if (cmd.startsWith("LPD")) {
drawColor = 0;
if (!headless) {
g.setColor(Color.black);
bg.setColor(Color.black); // try
}
cmd = cmd.substring(3);
if (cmd == null) {
cmd = "";
}
}
// Darn, didn't know what to do with that command, so lets
// just forget about it at the moment... :)
cmd = "";
}
// gcj:
if (cmd == null) {
cmd = "";
}
// Advance to next command
pos++;
}
return true;
}
//
// Handles a old RS command. They always start with only 1 character and
// are much less complex and nested as the eXtended commands.
//
private boolean handleNormalCmd() throws IOException {
String cmd;