noties / Markwon

Android markdown library (no WebView)
https://noties.io/Markwon/
Apache License 2.0
2.68k stars 298 forks source link

Fontawesome svg not loaded #448

Open visyone opened 12 months ago

visyone commented 12 months ago

Hi, I'm trying to use markown to show fontawesome icons (SVGs) as an image in a HTML tag, but I can figure it out.

I have configured the markown instance like this:

Markown markown = Markwon.builder(ctx).usePlugins(List.of(
           HtmlPlugin.create(),
           ImagesPlugin.create(plugin -> plugin.addSchemeHandler(OkHttpNetworkSchemeHandler.create(new OkHttpClient()))),
           ImagesPlugin.create(plugin -> plugin.addSchemeHandler(FileSchemeHandler.createWithAssets(ctx)))
           ))
           .build();

I have put the font awesome files in the assets folder of the app (the path to an icon is assets/fontawesome/svgs/solid/plus.svg)

and I used the com.caverock:androidsvg:1.4 dependency for render the SVGs.

With this configuration I'm try to display the following markdown:

<img alt=\"font-awesome-plus\" src=\"file:///android_asset/fontawesome/svgs/solid/plus.svg\" width=\"150px\" height=\"150px\">

The SVG is not loaded (the alt text of the image is shown) and the error seems referees to the decode function of the SVGMediaDecoder class

    @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 float w = svg.getDocumentWidth();
        final float h = svg.getDocumentHeight();
        final float density = resources.getDisplayMetrics().density;

        final int width = (int) (w * density + .5F);
        final int height = (int) (h * density + .5F);

        final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
        final Canvas canvas = new Canvas(bitmap);
        canvas.scale(density, density);
        svg.renderToCanvas(canvas);

        return new BitmapDrawable(resources, bitmap);
    }

when the decode function runs the width and height are -1 causing the error in Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

Which configuration do I miss?