Giter Club home page Giter Club logo

andenginesvgtextureregionextension's People

Contributors

nicolasgramlich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

andenginesvgtextureregionextension's Issues

Lint Suggestions

the new Android Lint tool suggests the following:

Use android.util.FloatMath#ceil() instead of java.lang.Math#ceil to avoid argument float to double conversion
for lines 154 and 155 of SVGHandler.java

Use new SparseIntArray(...) instead for better performance
for line 21 of SVGDirectColorMapper.java

Pattern fill implementation

Pattern fill now isn't supported in this extension.
I implemented it in my modification of andengine. I modified it hardly, so I cannot push it, but I can describe how I did it.

Here's new methodes for *Handler:

    protected void parsePattern(Attributes attributes) {
        this.attributes = attributes;
        canvas.push(currentCanvas);

        currentCanvas = mSVGPaint.createCanvasForPatternFill(attributes);

    }

    @Override
    protected void parsePatternEnd() {
        currentCanvas = canvas.pop();
    }

New SVGPaint method

    public Canvas createCanvasForPatternFill(Attributes pAttributes) {
        final String id = SAXHelper.getStringAttribute(pAttributes, ATTRIBUTE_ID);
        if (id == null) {
            return null;
        }

        Float widthF = SAXHelper.getFloatAttribute(pAttributes, ATTRIBUTE_WIDTH, 1);
        Float heightF = SAXHelper.getFloatAttribute(pAttributes, ATTRIBUTE_HEIGHT, 1);

        int width = (int) Math.ceil(widthF);
        int height = (int) Math.ceil(heightF);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);

        String viewBox = SAXHelper.getStringAttribute(pAttributes, "viewBox");
        if (viewBox != null) {
            String[] strings = viewBox.split("\\s");
            float posX = Float.valueOf(strings[0]);
            float posY = Float.valueOf(strings[1]);
            canvas.translate(-posX, -posY);
        }
        canvas.scale(SVGPatternFill.DETALIZATION_SCALE, SVGPatternFill.DETALIZATION_SCALE);

        final SVGGradient svgGradient = new SVGPatternFill(id, pAttributes, bitmap);
        this.mSVGGradientMap.put(id, svgGradient);
        return canvas;
    }

And SVGPatternFill class

public class SVGPatternFill extends SVGGradient {
    public static final float DETALIZATION_SCALE = 2f;
    private Bitmap bitmap;

    public SVGPatternFill(String id, Attributes attributes, Bitmap bitmap) {
        super(id, Type.PATTERN, attributes);
        this.bitmap = bitmap;
    }

    @Override
    public Shader createShader() {
        if (this.mShader != null) {
            return this.mShader;
        }

        mShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

        this.mMatrix = this.getTransform();
        if (this.mMatrix == null) {
            mMatrix = new Matrix();
        }
        mMatrix.postScale(1/DETALIZATION_SCALE, 1/DETALIZATION_SCALE);
        this.mShader.setLocalMatrix(this.mMatrix);

        return this.mShader;
    }

    @Override
    protected void resolveHref(HashMap<String, SVGGradient> pSVGGradientMap) {
        super.resolveHref(pSVGGradientMap);
        bitmap = ((SVGPatternFill) mParent).bitmap;
    }

    @Override
    protected Matrix getTransform() {
        if (this.mMatrix != null) {
            return this.mMatrix;
        } else {
            final String transfromString = this.mSVGAttributes.getStringAttribute(ATTRIBUTE_PATTERN_TRANSFORM, false);
            return getTransformFromString(transfromString);
        }
    }
}

Probably not best solution, but I just get acceptable result and want to share it :)

Pattern fill implementation

Pattern fill now isn't supported in this extension.
I implemented it in my modification of andengine. I modified it hardly, so I cannot push it, but I can describe how I did it.

Here's new methodes for *Handler:

    protected void parsePattern(Attributes attributes) {
        this.attributes = attributes;
        canvas.push(currentCanvas);

        currentCanvas = mSVGPaint.createCanvasForPatternFill(attributes);

    }

    @Override
    protected void parsePatternEnd() {
        currentCanvas = canvas.pop();
    }

New SVGPaint method

    public Canvas createCanvasForPatternFill(Attributes pAttributes) {
        final String id = SAXHelper.getStringAttribute(pAttributes, ATTRIBUTE_ID);
        if (id == null) {
            return null;
        }

        Float widthF = SAXHelper.getFloatAttribute(pAttributes, ATTRIBUTE_WIDTH, 1);
        Float heightF = SAXHelper.getFloatAttribute(pAttributes, ATTRIBUTE_HEIGHT, 1);

        int width = (int) Math.ceil(widthF);
        int height = (int) Math.ceil(heightF);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);

        String viewBox = SAXHelper.getStringAttribute(pAttributes, "viewBox");
        if (viewBox != null) {
            String[] strings = viewBox.split("\\s");
            float posX = Float.valueOf(strings[0]);
            float posY = Float.valueOf(strings[1]);
            canvas.translate(-posX, -posY);
        }
        canvas.scale(SVGPatternFill.DETALIZATION_SCALE, SVGPatternFill.DETALIZATION_SCALE);

        final SVGGradient svgGradient = new SVGPatternFill(id, pAttributes, bitmap);
        this.mSVGGradientMap.put(id, svgGradient);
        return canvas;
    }

And SVGPatternFill class

public class SVGPatternFill extends SVGGradient {
    public static final float DETALIZATION_SCALE = 2f;
    private Bitmap bitmap;

    public SVGPatternFill(String id, Attributes attributes, Bitmap bitmap) {
        super(id, Type.PATTERN, attributes);
        this.bitmap = bitmap;
    }

    @Override
    public Shader createShader() {
        if (this.mShader != null) {
            return this.mShader;
        }

        mShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

        this.mMatrix = this.getTransform();
        if (this.mMatrix == null) {
            mMatrix = new Matrix();
        }
        mMatrix.postScale(1/DETALIZATION_SCALE, 1/DETALIZATION_SCALE);
        this.mShader.setLocalMatrix(this.mMatrix);

        return this.mShader;
    }

    @Override
    protected void resolveHref(HashMap<String, SVGGradient> pSVGGradientMap) {
        super.resolveHref(pSVGGradientMap);
        bitmap = ((SVGPatternFill) mParent).bitmap;
    }

    @Override
    protected Matrix getTransform() {
        if (this.mMatrix != null) {
            return this.mMatrix;
        } else {
            final String transfromString = this.mSVGAttributes.getStringAttribute(ATTRIBUTE_PATTERN_TRANSFORM, false);
            return getTransformFromString(transfromString);
        }
    }
}

Probably not best solution, but I just get acceptable result and want to share it :)

Throws exceptions during parse some SVG data

Adobe Illustrator generated for me SVG file that contains following lines:

                        <polyline fill="none" stroke="#000000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" points="
                            25.19,53.59 24.7,53.72 19.68,55.04                      "/>

To fix this error, we need just trim string before splitting it.

        final String[] parts = pString.trim().split("[\\s,]+");

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.