Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SvgSupport for svg without a width and height info #165

Closed
3dmg opened this issue Sep 5, 2019 · 4 comments
Closed

SvgSupport for svg without a width and height info #165

3dmg opened this issue Sep 5, 2019 · 4 comments

Comments

@3dmg
Copy link

3dmg commented Sep 5, 2019

  • Markwon version: 4.1.1

First of all, thank you for your great work!

When I want to load a SVG without a width & height set in the file, I get this exception:

MARKWON-IMAGE: Error loading image: https://upload.wikimedia.org/wikipedia/commons/d/d7/Android_robot.svg
    java.lang.IllegalArgumentException: width and height must be > 0
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1033)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1000)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:950)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:911)
        at io.noties.markwon.image.svg.SvgMediaDecoder.decode(SvgMediaDecoder.java:70)
        at io.noties.markwon.image.AsyncDrawableLoaderImpl$1.run(AsyncDrawableLoaderImpl.java:123)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764) 

Most of the SVGs, I found online, were without these size information.

Is it possible to fallback to a default size for this case or is there another solution?

One example of an affected SVG: https://upload.wikimedia.org/wikipedia/commons/d/d7/Android_robot.svg

@noties
Copy link
Owner

noties commented Sep 5, 2019

Hello @3dmg !

It's an interesting case and I haven't seen SVG without width/height so far. It seems that absent width/height just says 100% of whatever dimension is missing... and it's fine in HTML (element can be given an explicit size), but for us it's a bit of a problem. Currently we scale received SVG up by device density, which makes SVG responsive, but without width/height it seems to be impossible.

We can try rendering such SVGs without scaling, can you try this and see if it solves your problem?

public class SvgPictureMediaDecoder extends MediaDecoder {

    public static final String CONTENT_TYPE = "image/svg+xml";

    @NonNull
    public static SvgPictureMediaDecoder create() {
        return new SvgPictureMediaDecoder();
    }

    @NonNull
    @Override
    public Drawable decode(@Nullable String contentType, @NonNull InputStream inputStream) {

        final SVG svg;
        try {
            svg = SVG.getFromInputStream(inputStream);
        } catch (SVGParseException e) {
            throw new IllegalStateException("Exception decoding SVG", e);
        }

        final Picture picture = svg.renderToPicture();
        return new PictureDrawable(picture);
    }

    @NonNull
    @Override
    public Collection<String> supportedTypes() {
        return Collections.singleton(CONTENT_TYPE);
    }
}

And usage (this will replace default SvgMediaDecoder):

final Markwon markwon = Markwon.builder(context)
        .usePlugin(ImagesPlugin.create(configure -> {
            configure.addMediaDecoder(SvgPictureMediaDecoder.create());
        }))
        .build();

@3dmg
Copy link
Author

3dmg commented Sep 5, 2019

Yes, this is working. Thank you!

When you need it, here you find a big list of different SVGs, with and without the size info: https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/

@noties
Copy link
Owner

noties commented Sep 5, 2019

Thanks, I'm going to take a look at it and see if some generic solution can be implemented 👍

@noties noties mentioned this issue Nov 15, 2019
@noties
Copy link
Owner

noties commented Nov 15, 2019

I have added SvgPictureMediaDecoder into 4.2.0 release

@noties noties closed this as completed Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants