Search in sources :

Example 16 with SyntaxException

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);
        }
    }
}
Also used : SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) TokType(uk.me.parabola.mkgmap.scan.TokType)

Example 17 with SyntaxException

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);
    }
}
Also used : TokenScanner(uk.me.parabola.mkgmap.scan.TokenScanner) SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) StringReader(java.io.StringReader)

Example 18 with SyntaxException

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;
}
Also used : SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) AlphaAdder(uk.me.parabola.imgfmt.app.typ.AlphaAdder)

Example 19 with SyntaxException

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;
}
Also used : Xpm(uk.me.parabola.imgfmt.app.typ.Xpm) SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) BitmapImage(uk.me.parabola.imgfmt.app.typ.BitmapImage) TrueImage(uk.me.parabola.imgfmt.app.typ.TrueImage) Image(uk.me.parabola.imgfmt.app.typ.Image) ColourInfo(uk.me.parabola.imgfmt.app.typ.ColourInfo)

Example 20 with SyntaxException

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.
    }
}
Also used : SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) Token(uk.me.parabola.mkgmap.scan.Token)

Aggregations

SyntaxException (uk.me.parabola.mkgmap.scan.SyntaxException)42 Token (uk.me.parabola.mkgmap.scan.Token)8 FileNotFoundException (java.io.FileNotFoundException)5 TokenScanner (uk.me.parabola.mkgmap.scan.TokenScanner)5 EqualsOp (uk.me.parabola.mkgmap.osmstyle.eval.EqualsOp)4 Op (uk.me.parabola.mkgmap.osmstyle.eval.Op)4 ValueOp (uk.me.parabola.mkgmap.osmstyle.eval.ValueOp)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 NotOp (uk.me.parabola.mkgmap.osmstyle.eval.NotOp)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 Matcher (java.util.regex.Matcher)2 ExitException (uk.me.parabola.imgfmt.ExitException)2 MapFailedException (uk.me.parabola.imgfmt.MapFailedException)2 BitmapImage (uk.me.parabola.imgfmt.app.typ.BitmapImage)2 ColourInfo (uk.me.parabola.imgfmt.app.typ.ColourInfo)2 TYPFile (uk.me.parabola.imgfmt.app.typ.TYPFile)2 TypData (uk.me.parabola.imgfmt.app.typ.TypData)2 Xpm (uk.me.parabola.imgfmt.app.typ.Xpm)2