Add SvgPictureMediaDecoder
This commit is contained in:
parent
4b2d38b92f
commit
5e3ace0c29
@ -4,6 +4,9 @@
|
||||
* `MarkwonEditor` to highlight markdown input whilst editing (new module: `markwon-editor`)
|
||||
* `Markwon#configuration` method to expose `MarkwonConfiguration` via public API
|
||||
* `HeadingSpan#getLevel` getter
|
||||
* Add `SvgPictureMediaDecoder` in `image` module to deal with SVG without dimensions ([#165])
|
||||
|
||||
[#165]: https://github.com/noties/Markwon/issues/165
|
||||
|
||||
# 4.1.2
|
||||
* Do not re-use RenderProps when creating a new visitor (fixes [#171])
|
||||
|
@ -0,0 +1,51 @@
|
||||
package io.noties.markwon.image.svg;
|
||||
|
||||
import android.graphics.Picture;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.PictureDrawable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.caverock.androidsvg.SVG;
|
||||
import com.caverock.androidsvg.SVGParseException;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import io.noties.markwon.image.MediaDecoder;
|
||||
|
||||
/**
|
||||
* @since 4.2.0-SNAPSHOT
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user