use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.
the class SrtTextReader method initialState.
/**
* The initial state, looking for a variable to set or a command to change
* the state.
* @param scanner The scanner for more tokens.
* @param tok The first token to process.
*/
private void initialState(TokenScanner scanner, Token tok) {
String val = tok.getValue();
TokType type = tok.getType();
if (type == TokType.TEXT) {
switch(val) {
case "codepage":
int codepage = scanner.nextInt();
sort.setCodepage(codepage);
encoder = sort.getCharset().newEncoder();
break;
case "description":
sort.setDescription(scanner.nextWord());
break;
case "id1":
sort.setId1(scanner.nextInt());
break;
case "id2":
sort.setId2(scanner.nextInt());
break;
case "multi":
sort.setMulti(true);
break;
// The old name; use characters
case "code":
case "characters":
if (encoder == null)
throw new SyntaxException(scanner, "Missing codepage declaration before code");
state = IN_CHARACTER;
scanner.skipSpace();
break;
case "expand":
state = IN_EXPAND;
scanner.skipSpace();
break;
default:
throw new SyntaxException(scanner, "Unrecognised command " + val);
}
}
}
use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.
the class CommonSection method parseXpmHeader.
/**
* Parse the XPM header in a typ file.
*
* There are extensions compared to a regular XPM file.
*
* @param scanner Only for reporting syntax errors.
* @param info Information read from the string is stored here.
* @param header The string containing the xpm header and other extended data provided on the
* same line.
*/
private void parseXpmHeader(TokenScanner scanner, ColourInfo info, String header) {
TokenScanner s2 = new TokenScanner("string", new StringReader(header));
if (s2.checkToken("\""))
s2.nextToken();
try {
info.setWidth(s2.nextInt());
info.setHeight(s2.nextInt());
info.setNumberOfColours(s2.nextInt());
info.setCharsPerPixel(s2.nextInt());
} catch (NumberFormatException e) {
throw new SyntaxException(scanner, "Bad number in XPM header " + header);
}
}
use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.
the class CommonSection method readTrueImageLine.
/**
* Read a single line of pixel colours.
*
* There can be one or more colours on the line and the colours are surrounded
* by quotes. The can be trailing attribute that sets the opacity of
* the final pixel.
*/
private int readTrueImageLine(TokenScanner scanner, final int[] image, int count) {
do {
scanner.validateNext("#");
String col = scanner.nextValue();
try {
int val = (int) Long.parseLong(col, 16);
if (col.length() <= 6)
val = (val << 8) + 0xff;
image[count++] = val;
} catch (NumberFormatException e) {
throw new SyntaxException(scanner, "Not a valid colour value ");
}
} while (scanner.checkToken("#"));
scanner.validateNext("\"");
// Look for any trailing alpha=N stuff.
final int lastColourIndex = count - 1;
readExtraColourInfo(scanner, new AlphaAdder() {
/**
* Add the alpha value to the last colour that was read in.
*
* @param alpha A true alpha value ie 0 is transparent, 255 opaque.
*/
public void addAlpha(int alpha) {
image[lastColourIndex] = (image[lastColourIndex] & ~0xff) | (alpha & 0xff);
}
});
return count;
}
use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.
the class CommonSection method readXpm.
/**
* Read an XMP image from the input scanner.
*
* Note that this is sometimes used just for colours so need to deal with
* different cases.
*/
protected Xpm readXpm(TokenScanner scanner, String header, boolean simple) {
ColourInfo colourInfo = readColourInfo(scanner, header);
String msg = colourInfo.analyseColours(simple);
if (msg != null)
throw new SyntaxException(scanner, msg);
Xpm xpm = new Xpm();
xpm.setColourInfo(colourInfo);
int height = colourInfo.getHeight();
int width = colourInfo.getWidth();
if (height > 0 && width > 0) {
colourInfo.setHasBitmap(true);
Image image;
if (colourInfo.getNumberOfColours() == 0)
image = readTrueImage(scanner, colourInfo);
else
image = readImage(scanner, colourInfo);
xpm.setImage(image);
}
hasXpm = true;
return xpm;
}
use of uk.me.parabola.mkgmap.scan.SyntaxException in project mkgmap by openstreetmap.
the class CommonSection method readExtraColourInfo.
/**
* Get any keywords that are on the end of the colour line. Must not step
* over the new line boundary.
*/
private void readExtraColourInfo(TokenScanner scanner, AlphaAdder colour) {
while (!scanner.isEndOfFile()) {
Token tok = scanner.nextRawToken();
if (tok.isEol())
break;
String word = tok.getValue();
// TypWiz uses alpha, TypViewer uses "canalalpha"
if (word.endsWith("alpha")) {
scanner.validateNext("=");
String aval = scanner.nextValue();
try {
// Convert to rgba format
int alpha = Integer.decode(aval);
alpha = 255 - ((alpha << 4) + alpha);
colour.addAlpha(alpha);
} catch (NumberFormatException e) {
throw new SyntaxException(scanner, "Bad number for alpha value " + aval);
}
}
// ignore everything we don't recognise.
}
}
Aggregations