BigBadaboom / androidsvg

SVG rendering library for Android
http://bigbadaboom.github.io/androidsvg/
Apache License 2.0
1.19k stars 226 forks source link

Find the length and width of the svg #261

Closed Arashvscode closed 1 year ago

Arashvscode commented 1 year ago

Hello, I use this code to display the length and width of the svg, but it does not work and produces 0 for me, can you help me?

    public static void GetSvgImageFile(String input, ImageView img, TextView tv1) {

        try {

            File startDir = new File(input);
            FileInputStream fileInputStream = new FileInputStream(startDir);

            SVG svg = SVG.getFromInputStream(fileInputStream);
            Drawable drawable = new PictureDrawable(svg.renderToPicture());
            img.setImageDrawable(drawable);
        //  float b = svg.getDocumentHeight();
        //  float c = svg.getDocumentWidth();
            tv1.setText(String.valueOf((float)svg.getDocumentWidth()).concat(" x ").concat(String.valueOf((float)svg.getDocumentHeight())));

        } catch (IOException d) {
            d.printStackTrace();

        } catch (SVGParseException s) {
            s.printStackTrace();

        } catch (Exception exception) {

            exception.printStackTrace();
        }
    }
BigBadaboom commented 1 year ago

It will depend on your SVG.

getDocumentWidth() and getDocumentHeight() return the values of the width and height attributes, respectively. Eg.

<svg width="200" height="150" ,,,>

If your SVG doesn't have those attributes, -1 will be returned.

Arashvscode commented 1 year ago

Actually, all my svgs have this option, but it doesn't return anything for me. Is the code I wrote correct?

BigBadaboom commented 1 year ago

Can you attach one of your SVGs that doesn't work?

Arashvscode commented 1 year ago

Yes, I connected it, but the result is nothing, it returns -1. Can you send me a correct code?

BigBadaboom commented 1 year ago

I meant please attach the SVG to this GitHub issue.

Is the code I wrote correct?

It looks correct. But results will depend on your SVG.

Actually, all my svgs have this option

I forgot to mention that the width and height need to be pixel values. For example width="100" or width="100px". If the width or height use relative units like % or em, you will get -1. For example, getDocumentWidth() will return -1 for width="100%". That is because 100% is not a real measurement. It means "100% of the parent container width". But there is no parent container here to measure against.

Arashvscode commented 1 year ago

Actually most of my svgs are 100% length and width, is this a bug in your library?

BigBadaboom commented 1 year ago

No. It is not a bug. It is working as intended.

Those methods were intended to return the dimensions of the SVG - if they were specified. But "100%" is not a true dimension. It means "100% of the viewport or parent container". But, without knowing those values, "100%" means nothing.

It probably would not be useful to display to your users "100% x 100%".

Is that is what you actually do want to display? If so, then unfortunately, AndroidSVG doesn't yet have a method to get the exactly value of the width and height attributes. For now you would need to fork the code and add those methods yourself.

Arashvscode commented 1 year ago

I'd rather you add a converter feature to convert svg to png or vice versa

BigBadaboom commented 1 year ago

I don't have any plans to add that feature.