FileDocCategorySizeDatePackage
TransformListParser.javaAPI DocphoneME MR2 API (J2ME)11136Wed May 02 18:00:36 BST 2007com.sun.perseus.parser

TransformListParser

public class TransformListParser extends NumberParser
The TransformListParser class converts attributes conforming to the SVG transform syntax into AffineTransform objects.
version
$Id: TransformListParser.java,v 1.2 2006/04/21 06:40:42 st125089 Exp $

Fields Summary
private com.sun.perseus.j2d.Transform
transform
Captures the transform built by this parser
Constructors Summary
Methods Summary
protected final voidparseMatrix()
Parses a matrix transform. 'm' is assumed to be the current character.

        current = read();

        // Parse 'atrix wsp? ( wsp?'
        if (current != 'a") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 't") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'r") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'i") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'x") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();
        if (current != '(") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();

        float a = parseNumber();
        skipCommaSpaces();
        float b = parseNumber();
        skipCommaSpaces();
        float c = parseNumber();
        skipCommaSpaces();
        float d = parseNumber();
        skipCommaSpaces();
        float e = parseNumber();
        skipCommaSpaces();
        float f = parseNumber();
       
        skipSpaces();

        if (current != ')") {
            throw new IllegalArgumentException("Expected ')' and got >"
                                               + (char) current + "<");
        }

        transform.mMultiply(new Transform(a, b, c, d, e, f));
    
protected final voidparseRotate()
Parses a rotate transform. 'r' is assumed to be the current character.

        current = read();

        // Parse 'otate wsp? ( wsp?'
        if (current != 'o") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 't") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'a") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 't") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'e") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();

        if (current != '(") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();

        float theta = parseNumber();
        skipSpaces();
      
        switch (current) {
        case ')":
            transform.mRotate(theta);
            return;
        case ',":
            current = read();
            skipSpaces();
        default:
            // nothing.
        }
      
        float cx = parseNumber();
        skipCommaSpaces();
        float cy = parseNumber();
      
        skipSpaces();
        if (current != ')") {
            throw new IllegalArgumentException();
        }

        transform.mTranslate(cx, cy);
        transform.mRotate(theta);
        transform.mTranslate(-cx, -cy);
    
protected final voidparseScale()
Parses a scale transform. 'c' is assumed to be the current character.

        current = read();

        // Parse 'ale wsp? ( wsp?'
        if (current != 'a") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'l") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'e") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();
        if (current != '(") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();

        float sx = parseNumber();
        skipSpaces();

        switch (current) {
        case ')":
            transform.mScale(sx);
            return;
        case ',":
            current = read();
            skipSpaces();
        default:
            // nothing
        }

        float sy = parseNumber();

        skipSpaces();
        if (current != ')") {
            throw new IllegalArgumentException();
        }
      
        transform.mScale(sx, sy);
    
protected final voidparseSkew()
Parses a skew transform. 'e' is assumed to be the current character.

        current = read();

        // Parse 'ew[XY] wsp? ( wsp?'
        if (current != 'e") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'w") {
            throw new IllegalArgumentException();
        }
        current = read();

        boolean skewX = false;
        switch (current) {
        case 'X":
            skewX = true;
        case 'Y":
            break;
        default:
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();
        if (current != '(") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();

        float sk = parseNumber();

        skipSpaces();
        if (current != ')") {
            throw new IllegalArgumentException();
        }
      
        float tan = MathSupport.tan(MathSupport.toRadians(sk));

        if (skewX) {
            Transform shear = new Transform(1, 0, tan, 1, 0, 0);
            transform.mMultiply(shear);
            // transform.shear(tan, 0);
        } else {
            Transform shear = new Transform(1, tan, 0, 1, 0, 0);
            transform.mMultiply(shear);
            // transform.shear(0, tan);
        }
    
public com.sun.perseus.j2d.TransformparseTransformList(java.lang.String txfStr)

param
txfStr the string containing the set of transform commands
return
An AffineTransform object corresponding to the input transform list.

        setString(txfStr);

        transform = new Transform(1, 0, 0, 1, 0, 0);

        // Parse leading wsp*
        current = read();
        skipSpaces();

        // Now, iterate on 'transforms?'
        loop2: for (;;) {
            switch (current) {
            case 'm":
                parseMatrix();
                break;
            case 'r":
                parseRotate();
                break;
            case 't":
                parseTranslate();
                break;
            case 's":
                current = read();
                switch (current) {
                case 'c":
                    parseScale();
                    break;
                case 'k":
                    parseSkew();
                    break;
                default:
                    throw new IllegalArgumentException();
                }
                break;
            case -1:
                break loop2;
            default:
                throw new IllegalArgumentException();
            }
            current = read();
            skipCommaSpaces();
        }
      
        return transform;
    
protected final voidparseTranslate()
Parses a translate transform. 't' is assumed to be the current character.

        current = read();

        // Parse 'ranslate wsp? ( wsp?'
        if (current != 'r") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'a") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'n") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 's") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'l") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'a") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 't") {
            throw new IllegalArgumentException();
        }
        current = read();
        if (current != 'e") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();
        if (current != '(") {
            throw new IllegalArgumentException();
        }
        current = read();
        skipSpaces();

        float tx = parseNumber();
        skipSpaces();

        switch (current) {
        case ')":
            transform.mTranslate(tx, 0);
            return;
        case ',":
            current = read();
            skipSpaces();
        default:
            // nothing
        }

        float ty = parseNumber();

        skipSpaces();
        if (current != ')") {
            throw new IllegalArgumentException();
        }

        transform.mTranslate(tx, ty);