From 31506a7acdded0ecf1304757fc04e4c4f571f9aa Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Sun, 15 Apr 2018 19:53:30 +0300 Subject: [PATCH] Fit an image without dimensions to canvas width (and keep ratio) --- .../noties/markwon/spans/AsyncDrawable.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/ru/noties/markwon/spans/AsyncDrawable.java b/library/src/main/java/ru/noties/markwon/spans/AsyncDrawable.java index 2253a5ba..705b666f 100644 --- a/library/src/main/java/ru/noties/markwon/spans/AsyncDrawable.java +++ b/library/src/main/java/ru/noties/markwon/spans/AsyncDrawable.java @@ -52,6 +52,7 @@ public class AsyncDrawable extends Drawable { this.imageSize = imageSize; } + @NonNull public String getDestination() { return destination; } @@ -172,13 +173,29 @@ public class AsyncDrawable extends Drawable { */ @NonNull private Rect resolveBounds() { + final Rect rect; + if (imageSizeResolver == null || imageSize == null) { - rect = result.getBounds(); + + // @since 1.0.5 + final Rect bounds = result.getBounds(); + if (bounds.width() > canvasWidth) { + + // let's scale image down, as we do not want to expand beyond canvas width + final float ratio = (float) bounds.width() / bounds.height(); + final int height = (int) (canvasWidth / ratio + .5F); + rect = new Rect(0, 0, canvasWidth, height); + + } else { + rect = bounds; + } + } else { rect = imageSizeResolver.resolveImageSize(imageSize, result.getBounds(), canvasWidth, textSize); } + return rect; } }