import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
import ij.*;
import ij.io.OpenDialog;
import ij.plugin.PlugIn;
import ij.plugin.frame.Recorder;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;
public class HPGL_Reader implements PlugIn {
/** This character denotes the end of file */
private static final int YYEOF = -1;
/** initial size of the lookahead buffer */
private static final int ZZ_BUFFERSIZE = 16384;
/** lexical states */
private static final int STRING = 1;
private static final int YYINITIAL = 0;
private static final int PARAMETERS = 2;
/**
* Translates characters to character classes
*/
private static final char [] ZZ_CMAP = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 10, 0, 2, 11, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 3, 3, 0,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 12, 0, 0, 0, 0,
0, 1, 7, 1, 8, 1, 1, 1, 1, 1, 1, 1, 6, 1, 1, 1,
1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/**
* Translates DFA states to action switch labels.
*/
private static final int [] ZZ_ACTION = zzUnpackAction();
private static final String ZZ_ACTION_PACKED_0 =
"\3\0\2\1\1\2\3\1\2\3\1\1\1\4\1\5\1\6\1\7\1\10\1\7\1\11\1\12";
private static int [] zzUnpackAction() {
int [] result = new int[20];
int offset = 0;
offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAction(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
do result[j++] = value; while (--count > 0);
}
return j;
}
/**
* Translates a state to a row index in the transition table
*/
private static final int [] ZZ_ROWMAP = zzUnpackRowMap();
private static final String ZZ_ROWMAP_PACKED_0 =
"\0\0\0\15\0\32\0\47\0\64\0\47\0\101\0\116\0\133\0\47\0\150\0\165\0\202\0\47\0\47\0\47\0\47\0\217\0\47\0\47";
private static int [] zzUnpackRowMap() {
int [] result = new int[20];
int offset = 0;
offset = zzUnpackRowMap(ZZ_ROWMAP_PACKED_0, offset, result);
return result;
}
private static int zzUnpackRowMap(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int high = packed.charAt(i++) << 16;
result[j++] = high | packed.charAt(i++);
}
return j;
}
/**
* The transition table of the DFA
*/
private static final int [] ZZ_TRANS = zzUnpackTrans();
private static final String ZZ_TRANS_PACKED_0 =
"\1\4\1\5\1\6\2\4\1\6\1\7\1\10\1\11\1\5\2\6\1\4\13\12\1\13\1\12\1\4\1\14"+
"\1\4\1\15\2\16\4\14\2\4\1\17\16\0\1\20\4\0\4\20\4\0\1\20\4\0\1\20\1\21\2\20"+
"\4\0\1\20\4\0\1\21\3\20\4\0\1\20\4\0\3\20\1\22\15\0\1\12\3\0\1\23\4\0\4\23\6\0\1\15\11\0\12\24\1\0\2\24";
private static int [] zzUnpackTrans() {
int [] result = new int[156];
int offset = 0;
offset = zzUnpackTrans(ZZ_TRANS_PACKED_0, offset, result);
return result;
}
private static int zzUnpackTrans(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
value--;
do result[j++] = value; while (--count > 0);
}
return j;
}
/* error codes */
private static final int ZZ_UNKNOWN_ERROR = 0;
private static final int ZZ_NO_MATCH = 1;
private static final int ZZ_PUSHBACK_2BIG = 2;
/* error messages for the codes above */
private static final String ZZ_ERROR_MSG[] = {
"Unkown internal scanner error",
"Error: could not match input",
"Error: pushback value was too large"
};
/**
* ZZ_ATTRIBUTE[aState] contains the attributes of state aState
*/
private static final int [] ZZ_ATTRIBUTE = zzUnpackAttribute();
private static final String ZZ_ATTRIBUTE_PACKED_0 = "\3\0\1\11\1\1\1\11\3\1\1\11\3\1\4\11\1\1\2\11";
private static int [] zzUnpackAttribute() {
int [] result = new int[20];
int offset = 0;
offset = zzUnpackAttribute(ZZ_ATTRIBUTE_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAttribute(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
do result[j++] = value; while (--count > 0);
}
return j;
}
/** the input device */
private java.io.Reader zzReader;
/** the current state of the DFA */
private int zzState;
/** the current lexical state */
private int zzLexicalState = YYINITIAL;
/** this buffer contains the current text to be matched and is
the source of the yytext() string */
private char zzBuffer[] = new char[ZZ_BUFFERSIZE];
/** the textposition at the last accepting state */
private int zzMarkedPos;
/** the textposition at the last state to be included in yytext */
private int zzPushbackPos;
/** the current text position in the buffer */
private int zzCurrentPos;
/** startRead marks the beginning of the yytext() string in the buffer */
private int zzStartRead;
/** endRead marks the last character in the buffer, that has been read from input */
private int zzEndRead;
/** number of newlines encountered up to the start of the matched text */
private int yyline;
/** the number of characters up to the start of the matched text */
private int yychar;
/** the number of characters from the last newline up to the start of the matched text */
private int yycolumn;
/**
* zzAtBOL == true <=> the scanner is currently at the beginning of a line
*/
private boolean zzAtBOL = true;
/** zzAtEOF == true <=> the scanner is at the EOF */
private boolean zzAtEOF;
/* user code: */
/**
* HPGL_ Reader is an import filter for ImageJ which reads HPGL
* (Hewlett Packard Graphics Language) files.
*
* March 2006 - Version 3
*
* It was written by Ulf Dittmer,
* and is hereby released under the same terms as ImageJ.
*/
final static int DEFAULT_FONT_SIZE = 12;
final static int DEFAULT_SELECT_PEN = 1;
final static int DEFAULT_LINE_TYPE = 99;
final static int DEFAULT_PEN_WIDTH = 12;
// dimensions
final static double CmPerInch = 2.54;
final static double PixelsPerInch = 72.0;
final static double PlotterUnitsPerInch = 1016.0;
final static double CmWidthA4 = 29.7;
final static double CmHeightA4 = 21.0;
final static double CmWidthA3 = CmHeightA4 * 2.0;
final static double CmHeightA3 = CmWidthA4;
char strTerm = '\u0003';
String cmd = null, labelBuffer;
StringBuffer str = new StringBuffer();
Vector params = new Vector();
boolean useJava2D = IJ.isJava2();
boolean isA4 = true; // set this to false for A3 output
boolean plotterUnits, penWidthUnitsRelative;
char labelSeparator;
int lineType, penWidth, selectPen, xPos, yPos, xPosOld, yPosOld, fontSize;
boolean penDown, plotAbsolute;
double sx, sy, p1x, p1y, p2x, p2y, xMin, yMin, xMax, yMax;
// The cmap array is used for selecting the colors with the SP command
// The first two entries must always be white and black, any (optional)
// following ones can be chosen at will. I'm not aware of a 'standard' sequence.
Color[] cmap = new Color[] { Color.white, Color.black, Color.red, Color.green,
Color.blue, Color.magenta, Color.cyan, Color.yellow };
// The dashvalues array is used for selecting the dash type of lines with the LT command.
// Each entry denotes a sequence of black and white pixels, e.g. the first one specifies
// that the line should consist of 5 black pixels followed by 5 white pixels, repeated
// ad infinitum. An even count of numbers is required, while the number of dash types
// can be chosen at will. See http://rsb.info.nih.gov/ij/plugins/dotted-line.html for more info.
// Note that type 0 is not used - it means plot a dot at the specifying point.
float[][] dashvalues = new float[][] {
{ 1.0f, 1.0f }, { 1.0f, 9.0f }, { 5.0f, 5.0f }, { 8.0f, 2.0f }, { 1.0f, 1.0f, 1.0f, 9.0f },
{ 2.0f, 2.0f, 2.0f, 12.0f }, { 2.0f, 2.0f, 2.0f, 12.0f, 2.0f, 2.0f, 2.0f }
};
ImageProcessor img = null;
ImagePlus implus = null;
int width=-1, height=-1;
double run, rise, slant;
public HPGL_Reader() {
init();
}
protected void init() {
lineType = DEFAULT_LINE_TYPE;
penWidth = DEFAULT_PEN_WIDTH;
setLineWidth();
selectPen = DEFAULT_SELECT_PEN;
setColor();
xPos = 0;
yPos = 0;
xPosOld = 0;
yPosOld = 0;
// 72 pixels per inch for onscreen images, 1016 plotter units per inch for HPGL
sx = PixelsPerInch / PlotterUnitsPerInch;
sy = PixelsPerInch / PlotterUnitsPerInch;
fontSize = DEFAULT_FONT_SIZE;
setFontSize();
penDown = false;
plotAbsolute = true;
run = 1.0;
rise = 0.0;
slant = 0.0;
labelBuffer = "";
plotterUnits = true;
penWidthUnitsRelative = false;
setP1P2();
}
protected void setColor() {
if (img != null)
img.setColor(cmap[selectPen]);
}
protected void setLineWidth() {
if (img != null)
img.setLineWidth((int) (penWidth * sx + 0.5));
}
protected void setFontSize() {
if (img != null)
img.setFont(new Font("SansSerif", Font.PLAIN, fontSize));
}
protected void setP1P2() {
p1x = 0;
p1y = 0;
if (isA4) {
p2x = CmWidthA4 / CmPerInch * PlotterUnitsPerInch;
p2y = CmHeightA4 / CmPerInch * PlotterUnitsPerInch;
} else {
// A3
p2x = CmWidthA3 / CmPerInch * PlotterUnitsPerInch;
p2y = CmHeightA3 / CmPerInch * PlotterUnitsPerInch;
}
}
public void run (String arg) {
String directory=null, fileName=null;
String options = Macro.getOptions();
// if there are no options, then the plugin has been selected from the menu,
// and the dialog should be shown
if (options == null) {
OpenDialog od = new OpenDialog("Open HPGL", arg);
fileName = od.getFileName();
if (fileName == null)
return;
directory = od.getDirectory();
} else {
fileName = Macro.getValue(options, "file", null);
if (fileName == null)
return;
directory = Macro.getValue(options, "dir", ".");
// you can only set useJava2D to true if the JVM supports it
if (useJava2D)
useJava2D = "true".equals(Macro.getValue(options, "usejava2d", "true"));
isA4 = "true".equals(Macro.getValue(options, "a4", "true"));
}
try {
implus = WindowManager.getCurrentImage();
if (implus == null) {
ColorProcessor cp = new ColorProcessor(
(int) (0.5 + (isA4 ? CmWidthA4 : CmWidthA3) / CmPerInch * PixelsPerInch),
(int) (0.5 + (isA4 ? CmHeightA4 : CmHeightA3) / CmPerInch * PixelsPerInch));
cp.setColor(Color.white);
cp.fill();
implus = new ImagePlus("HPGL Import", cp);
implus.show();
}
if (!implus.lock())
return;
if (Recorder.record) {
Recorder.setCommand("HPGL Reader");
Recorder.recordOption("a4", ""+isA4);
Recorder.recordOption("usejava2d", ""+useJava2D);
Recorder.recordOption("file", fileName);
Recorder.recordOption("dir", directory);
Recorder.saveCommand();
}
img = implus.getProcessor();
width = img.getWidth();
height = img.getHeight();
Undo.reset();
Undo.setup(Undo.COMPOUND_FILTER, implus);
importFile(directory + File.separator + fileName);
implus.changes = true;
implus.updateAndRepaintWindow();
implus.unlock();
IJ.showStatus("HPGL file " + fileName + " imported" + (useJava2D ? "" : " w/o Java2D"));
} catch (Exception ex) {
IJ.showMessage("Open HPGL", ex.getMessage());
}
}
public static void main (String args[]) throws Exception {
if (args == null) {
System.err.println("usage: java HPGL_Reader file");
System.exit(1);
}
HPGL_Reader self = new HPGL_Reader();
self.importFile(args[0]);
}
protected void importFile (String fileName) throws Exception {
try {
zzReader = new FileReader(fileName);
while ( !zzAtEOF ) yylex();
} catch (Exception ex) {
System.out.println("Unexpected exception:");
ex.printStackTrace();
}
}
protected int x (double xIn) {
if (! plotterUnits)
xIn = ((xIn - xMin) / (xMax - xMin)) * (p2x - p1x) + p1x;
return (int) (xIn * sx + 0.5);
}
protected int y (double yIn) {
if (! plotterUnits)
yIn = ((yIn - yMin) / (yMax - yMin)) * (p2y - p1y) + p1y;
return height - (int) (yIn * sy + 0.5);
}
protected void action (String cmd, Vector params) {
//System.out.println(cmd + " " + params.size());
Enumeration paramsEnum = params.elements();
if ("IN".equals(cmd)) {
init();
} else if ("PA".equals(cmd)) {
plotAbsolute = true;
while (paramsEnum.hasMoreElements()) {
xPosOld = xPos;
yPosOld = yPos;
xPos = Integer.parseInt((String) paramsEnum.nextElement());
yPos = Integer.parseInt((String) paramsEnum.nextElement());
if (penDown)
drawLine(x(xPosOld), y(yPosOld), x(xPos), y(yPos));
}
} else if ("PD".equals(cmd)) {
penDown = true;
while (paramsEnum.hasMoreElements()) {
xPosOld = xPos;
yPosOld = yPos;
xPos = Integer.parseInt((String) paramsEnum.nextElement());
yPos = Integer.parseInt((String) paramsEnum.nextElement());
drawLine(x(xPosOld), y(yPosOld), x(xPos), y(yPos));
}
} else if ("PR".equals(cmd)) {
plotAbsolute = false;
while (paramsEnum.hasMoreElements()) {
xPosOld = xPos;
yPosOld = yPos;
xPos = xPosOld + Integer.parseInt((String) paramsEnum.nextElement());
yPos = yPosOld + Integer.parseInt((String) paramsEnum.nextElement());
if (penDown)
drawLine(x(xPosOld), y(yPosOld), x(xPos), y(yPos));
}
} else if ("PU".equals(cmd)) {
penDown = false;
while (paramsEnum.hasMoreElements()) {
xPosOld = xPos;
yPosOld = yPos;
xPos = Integer.parseInt((String) paramsEnum.nextElement());
yPos = Integer.parseInt((String) paramsEnum.nextElement());
}
} else if ("LT".equals(cmd)) {
if (paramsEnum.hasMoreElements()) {
lineType = Integer.parseInt((String) paramsEnum.nextElement());
} else {
lineType = DEFAULT_LINE_TYPE;
}
// ignore second parameter - pattern length
if (lineType < 0) lineType *= -1; // minus sign used as delimiter
if (lineType >= dashvalues.length) {
System.err.println("HPGL Import: LT " + lineType + " invalid; resetting to 1");
lineType = DEFAULT_LINE_TYPE;
}
} else if ("SP".equals(cmd)) {
if (paramsEnum.hasMoreElements()) {
selectPen = Integer.parseInt((String) paramsEnum.nextElement());
} else {
selectPen = DEFAULT_SELECT_PEN;
}
if (selectPen < 0) selectPen *= -1; // minus sign used as delimiter
if (selectPen > cmap.length-1) {
System.err.println("HPGL Import: SP " + selectPen + " invalid; resetting to 1");
selectPen = DEFAULT_SELECT_PEN;
}
setColor();
} else if ("CI".equals(cmd)) {
int radiusX = Integer.parseInt((String) paramsEnum.nextElement());
int radiusY = radiusX;
if (paramsEnum.hasMoreElements())
radiusY = Integer.parseInt((String) paramsEnum.nextElement());
// the following code is lifted from ij.gui.OvalRoi.drawPixels
int a = x(radiusX/2);
int b = x(radiusY/2);
// using x() for converting units, not absolute points
double a2 = a*a;
double b2 = b*b;
int xbase = x(xPos)+a;
int ybase = y(yPos)+b;
double yy;
if (img != null)
img.moveTo(x(xPos), y(yPos)+b);
for (int i=-a+1; i<=a; i++) {
yy = Math.sqrt(b2*(1.0-(i*i)/a2));
if (img != null)
img.lineTo(xbase+i, ybase+(int)(yy+0.5));
}
if (img != null)
img.moveTo(x(xPos), y(yPos)+b);
for (int i=-a+1; i<=a; i++) {
yy = Math.sqrt(b2*(1.0-(i*i)/a2));
if (img != null)
img.lineTo(xbase+i, ybase-(int)(yy+0.5));
}
} else if ("DI".equals(cmd) || "DR".equals(cmd)) {
if (paramsEnum.hasMoreElements()) {
run = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
rise = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
} else {
// no parameters - text is horizontal, left to right
run = 1.0;
rise = 0.0;
}
} else if ("SL".equals(cmd)) {
// slant is parsed, but ignored when drawing text
if (paramsEnum.hasMoreElements()) {
slant = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
} else {
// no parameters - text is not slanted
slant = 0.0;
}
} else if ("DT".equals(cmd)) {
// do nothing - is handled by lexer
} else if ("LB".equals(cmd)) {
String str = (String) paramsEnum.nextElement();
if (img != null)
drawString(str, x(xPos), y(yPos));
} else if ("BL".equals(cmd)) {
if (paramsEnum.hasMoreElements()) {
labelBuffer = (String) paramsEnum.nextElement();
} else {
labelBuffer = "";
}
} else if ("PB".equals(cmd)) {
if (img != null)
drawString(labelBuffer, x(xPos), y(yPos));
} else if ("WU".equals(cmd)) {
// WU is parsed, but ignored
if (paramsEnum.hasMoreElements()) {
// no parameter or parameter of zero means pen width units are absolute
penWidthUnitsRelative = (0 != Integer.parseInt((String) paramsEnum.nextElement()));
} else {
penWidthUnitsRelative = false;
}
} else if ("PW".equals(cmd)) {
if (paramsEnum.hasMoreElements()) {
penWidth = (int) Math.floor(0.5 + Float.parseFloat((String) paramsEnum.nextElement()));
} else {
penWidth = DEFAULT_PEN_WIDTH;
}
if (penWidth < 1) {
System.err.println("HPGL Import: PW " + penWidth + " invalid; resetting to 1");
penWidth = DEFAULT_PEN_WIDTH;
}
setLineWidth();
} else if ("SI".equals(cmd)) {
// parameter to SI are given in cm
if (paramsEnum.hasMoreElements()) {
// ignore first parameter - we assume square characters, i.e. width = height
paramsEnum.nextElement();
fontSize = (int) Math.floor(0.5 + PixelsPerInch / CmPerInch * Double.valueOf((String) paramsEnum.nextElement()).doubleValue());
} else {
// reset to default if no parameter
fontSize = DEFAULT_FONT_SIZE;
}
setFontSize();
} else if ("SR".equals(cmd)) {
// parameter to SR are given in %
if (paramsEnum.hasMoreElements()) {
// ignore first parameter - we assume square characters, i.e. width = height
paramsEnum.nextElement();
double value = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
fontSize = (int) Math.floor(0.5 + (p2x - p1x) * value / 100.0);
} else {
// reset to default if no parameter
fontSize = DEFAULT_FONT_SIZE;
}
setFontSize();
} else if ("SC".equals(cmd)) {
if (paramsEnum.hasMoreElements()) {
xMin = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
xMax = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
yMin = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
yMax = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
plotterUnits = false;
} else {
plotterUnits = true;
}
} else if ("IP".equals(cmd)) {
if (paramsEnum.hasMoreElements()) {
p1x = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
p1y = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
p2x = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
p2y = Double.valueOf((String) paramsEnum.nextElement()).doubleValue();
} else {
setP1P2();
}
} else {
System.out.println("ignored: " + cmd + " " + params.size());
}
}
protected void drawString(String lb, int x1, int y1) {
if (img == null) return;
if (!useJava2D || (Math.abs(rise) < 1.0e-6)) {
img.drawString(lb, x1, y1);
} else {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setFont(new Font("SansSerif", Font.PLAIN, fontSize));
g2.setColor(cmap[selectPen]);
implus.updateImage();
g2.drawImage(implus.getImage(), 0, 0, null);
g2.rotate(-Math.atan2(rise, run), x1, y1);
g2.drawString(lb, x1, y1);
implus.setImage(bi);
img = implus.getProcessor();
}
}
protected void drawLine (int x1, int y1, int x2, int y2) {
if (img == null) return;
if (!useJava2D || (lineType == DEFAULT_LINE_TYPE)) {
img.drawLine(x1, y1, x2, y2);
} else if (lineType == 0) {
img.drawLine(x1, y1, x1, y1);
} else {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
implus.updateImage();
g2.drawImage(implus.getImage(), 0, 0, null);
Stroke stroke = new BasicStroke(x(penWidth), BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, dashvalues[lineType], 0);
g2.setStroke(stroke);
g2.setColor(cmap[selectPen]);
g2.drawLine(x1, y1, x2, y2);
implus.setImage(bi);
img = implus.getProcessor();
// the following line may seem unnecessary -as it should be-,
// but it's needed on OS X at the least
setLineWidth();
}
}
/**
* Creates a new scanner
* There is also a java.io.InputStream version of this constructor.
*
* @param in the java.io.Reader to read input from.
*/
public HPGL_Reader(java.io.Reader in) {
init();
this.zzReader = in;
}
/**
* Creates a new scanner.
* There is also java.io.Reader version of this constructor.
*
* @param in the java.io.Inputstream to read input from.
*/
public HPGL_Reader(java.io.InputStream in) {
this(new java.io.InputStreamReader(in));
}
/**
* Refills the input buffer.
*
* @return false, iff there was new input.
*
* @exception java.io.IOException if any I/O-Error occurs
*/
private boolean zzRefill() throws java.io.IOException {
/* first: make room (if you can) */
if (zzStartRead > 0) {
System.arraycopy(zzBuffer, zzStartRead, zzBuffer, 0, zzEndRead-zzStartRead);
/* translate stored positions */
zzEndRead-= zzStartRead;
zzCurrentPos-= zzStartRead;
zzMarkedPos-= zzStartRead;
zzPushbackPos-= zzStartRead;
zzStartRead = 0;
}
/* is the buffer big enough? */
if (zzCurrentPos >= zzBuffer.length) {
/* if not: blow it up */
char newBuffer[] = new char[zzCurrentPos*2];
System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length);
zzBuffer = newBuffer;
}
/* finally: fill the buffer with new input */
int numRead = zzReader.read(zzBuffer, zzEndRead, zzBuffer.length-zzEndRead);
if (numRead < 0) {
return true;
}
else {
zzEndRead+= numRead;
return false;
}
}
/**
* Closes the input stream.
*/
private final void yyclose() throws java.io.IOException {
zzAtEOF = true; /* indicate end of file */
zzEndRead = zzStartRead; /* invalidate buffer */
if (zzReader != null)
zzReader.close();
}
/**
* Resets the scanner to read from a new input stream.
* Does not close the old reader.
*
* All internal variables are reset, the old input stream
* cannot be reused (internal buffer is discarded and lost).
* Lexical state is set to ZZ_INITIAL.
*
* @param reader the new input stream
*/
private final void yyreset(java.io.Reader reader) {
zzReader = reader;
zzAtBOL = true;
zzAtEOF = false;
zzEndRead = zzStartRead = 0;
zzCurrentPos = zzMarkedPos = zzPushbackPos = 0;
yyline = yychar = yycolumn = 0;
zzLexicalState = YYINITIAL;
}
/**
* Returns the current lexical state.
*/
private final int yystate() {
return zzLexicalState;
}
/**
* Enters a new lexical state
*
* @param newState the new lexical state
*/
private final void yybegin(int newState) {
zzLexicalState = newState;
}
/**
* Returns the text matched by the current regular expression.
*/
private final String yytext() {
return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead );
}
/**
* Returns the character at position pos from the matched text.
*
* It is equivalent to yytext().charAt(pos), but faster
*
* @param pos the position of the character to fetch.
* A value from 0 to yylength()-1.
*
* @return the character at position pos
*/
private final char yycharat(int pos) {
return zzBuffer[zzStartRead+pos];
}
/**
* Returns the length of the matched text region.
*/
private final int yylength() {
return zzMarkedPos-zzStartRead;
}
/**
* Reports an error that occured while scanning.
*
* In a wellformed scanner (no or only correct usage of
* yypushback(int) and a match-all fallback rule) this method
* will only be called with things that "Can't Possibly Happen".
* If this method is called, something is seriously wrong
* (e.g. a JFlex bug producing a faulty scanner etc.).
*
* Usual syntax/scanner level error handling should be done in error fallback rules.
*
* @param errorCode the code of the errormessage to display
*/
private void zzScanError(int errorCode) {
String message;
try {
message = ZZ_ERROR_MSG[errorCode];
}
catch (ArrayIndexOutOfBoundsException e) {
message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR];
}
throw new Error(message);
}
/**
* Pushes the specified amount of characters back into the input stream.
*
* They will be read again by then next call of the scanning method
*
* @param number the number of characters to be read again.
* This number must not be greater than yylength()!
*/
private void yypushback(int number) {
if ( number > yylength() )
zzScanError(ZZ_PUSHBACK_2BIG);
zzMarkedPos -= number;
}
/**
* Resumes scanning until the next regular expression is matched,
* the end of input is encountered or an I/O-Error occurs.
*
* @return the next token
* @exception java.io.IOException if any I/O-Error occurs
*/
private int yylex() throws java.io.IOException {
int zzInput;
int zzAction;
// cached fields:
int zzCurrentPosL;
int zzMarkedPosL;
int zzEndReadL = zzEndRead;
char [] zzBufferL = zzBuffer;
char [] zzCMapL = ZZ_CMAP;
int [] zzTransL = ZZ_TRANS;
int [] zzRowMapL = ZZ_ROWMAP;
int [] zzAttrL = ZZ_ATTRIBUTE;
while (true) {
zzMarkedPosL = zzMarkedPos;
zzAction = -1;
zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL;
zzState = zzLexicalState;
zzForAction: {
while (true) {
if (zzCurrentPosL < zzEndReadL)
zzInput = zzBufferL[zzCurrentPosL++];
else if (zzAtEOF) {
zzInput = YYEOF;
break zzForAction;
}
else {
// store back cached positions
zzCurrentPos = zzCurrentPosL;
zzMarkedPos = zzMarkedPosL;
boolean eof = zzRefill();
// get translated positions and possibly new buffer
zzCurrentPosL = zzCurrentPos;
zzMarkedPosL = zzMarkedPos;
zzBufferL = zzBuffer;
zzEndReadL = zzEndRead;
if (eof) {
zzInput = YYEOF;
break zzForAction;
} else {
zzInput = zzBufferL[zzCurrentPosL++];
}
}
int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ];
if (zzNext == -1) break zzForAction;
zzState = zzNext;
int zzAttributes = zzAttrL[zzState];
if ( (zzAttributes & 1) == 1 ) {
zzAction = zzState;
zzMarkedPosL = zzCurrentPosL;
if ( (zzAttributes & 8) == 8 ) break zzForAction;
}
}
}
// store back cached position
zzMarkedPos = zzMarkedPosL;
switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {
case 8:
{ str.setLength(0);
cmd = yytext();
yybegin(STRING);
}
case 11: break;
case 5:
{ /* parameter delimiter */
}
case 12: break;
case 2:
{ /* ignore */
}
case 13: break;
case 9:
{ action(cmd, params);
cmd = yytext();
params.setSize(0);
}
case 14: break;
case 3:
{ if (yytext().charAt(0) == strTerm) {
//System.out.println("end of string:"+(int)yytext().charAt(0));
params.add(str.toString());
//System.out.println("label:"+str.toString());
action(cmd, params);
params.setSize(0);
yybegin(YYINITIAL);
} else {
//System.out.println("-"+yytext()+"-");
str.append(yytext());
}
}
case 15: break;
case 6:
{ action(cmd, params);
params.setSize(0);
yybegin(YYINITIAL);
}
case 16: break;
case 4:
{ params.add(yytext());
}
case 17: break;
case 1:
{ /* ignore every character that's not recognizably part of a command */
//System.out.println("ignoring char: "+(int)yytext().charAt(0));
}
case 18: break;
case 10:
{ strTerm = yytext().charAt(2);
if (';' == strTerm) {
// resetting string termination character to Control-c
strTerm = '\u0003';
}
cmd = "DT";
params.add(""+strTerm);
//System.out.println("string terminator:"+(int)strTerm);
action(cmd, params);
params.setSize(0);
}
case 19: break;
case 7:
{ cmd = yytext();
params.setSize(0);
yybegin(PARAMETERS);
}
case 20: break;
default:
if (zzInput == YYEOF && zzStartRead == zzCurrentPos) {
zzAtEOF = true;
return YYEOF;
}
else {
zzScanError(ZZ_NO_MATCH);
}
}
}
}
}