Image with width greater than canvas scaled
This commit is contained in:
parent
323ac35bb2
commit
14447a2572
@ -24,7 +24,22 @@ public class ImageSizeResolverDef extends ImageSizeResolver {
|
||||
) {
|
||||
|
||||
if (imageSize == null) {
|
||||
return imageBounds;
|
||||
// @since 2.0.0 post process bounds to fit canvasWidth (previously was inside AsyncDrawable)
|
||||
// must be applied only if imageSize is null
|
||||
final Rect rect;
|
||||
final int w = imageBounds.width();
|
||||
if (w > canvasWidth) {
|
||||
final float reduceRatio = (float) w / canvasWidth;
|
||||
rect = new Rect(
|
||||
0,
|
||||
0,
|
||||
canvasWidth,
|
||||
(int) (imageBounds.height() / reduceRatio + .5F)
|
||||
);
|
||||
} else {
|
||||
rect = imageBounds;
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
|
||||
final Rect rect;
|
||||
|
@ -33,14 +33,6 @@ public class AsyncDrawable extends Drawable {
|
||||
private int canvasWidth;
|
||||
private float textSize;
|
||||
|
||||
/**
|
||||
* @deprecated 1.0.6 markdown images are also processed with {@link ImageSizeResolver}
|
||||
*/
|
||||
@Deprecated
|
||||
public AsyncDrawable(@NonNull String destination, @NonNull Loader loader) {
|
||||
this(destination, loader, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.0.1
|
||||
*/
|
||||
@ -178,27 +170,11 @@ public class AsyncDrawable extends Drawable {
|
||||
@NonNull
|
||||
private Rect resolveBounds() {
|
||||
|
||||
final Rect rect;
|
||||
|
||||
if (imageSizeResolver == null) {
|
||||
|
||||
// @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;
|
||||
// @since 2.0.0 previously we were checking if image is greater than canvas width here
|
||||
// but as imageSizeResolver won't be null anymore, we should transfer this logic
|
||||
// there
|
||||
return imageSizeResolver != null
|
||||
? imageSizeResolver.resolveImageSize(imageSize, result.getBounds(), canvasWidth, textSize)
|
||||
: result.getBounds();
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import org.robolectric.annotation.Config;
|
||||
|
||||
import ru.noties.markwon.renderer.ImageSize.Dimension;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static ru.noties.markwon.renderer.ImageSizeResolverDef.UNIT_EM;
|
||||
import static ru.noties.markwon.renderer.ImageSizeResolverDef.UNIT_PERCENT;
|
||||
|
||||
@ -29,7 +29,22 @@ public class ImageSizeResolverDefTest {
|
||||
public void no_image_size() {
|
||||
// no image size returns image original bounds
|
||||
final Rect rect = new Rect(0, 0, 15, 43);
|
||||
assertEquals(rect, def.resolveImageSize(null, rect, -1, Float.NaN));
|
||||
assertEquals(rect, def.resolveImageSize(null, rect, 15, Float.NaN));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void no_image_size_width_greater_than_canvas() {
|
||||
// image must be scaled (with ratio) wo fit canvas width
|
||||
final Rect rect = new Rect(0, 0, 10, 20);
|
||||
assertEquals(
|
||||
new Rect(0, 0, 8, 16),
|
||||
def.resolveImageSize(
|
||||
null,
|
||||
rect,
|
||||
8,
|
||||
Float.NaN
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -68,7 +83,7 @@ public class ImageSizeResolverDefTest {
|
||||
def.resolveImageSize(
|
||||
new ImageSize(new Dimension(7, "width"), new Dimension(9, "height")),
|
||||
rect,
|
||||
-1,
|
||||
90,
|
||||
Float.NaN
|
||||
)
|
||||
);
|
||||
@ -82,7 +97,7 @@ public class ImageSizeResolverDefTest {
|
||||
def.resolveImageSize(
|
||||
new ImageSize(new Dimension(2.f, UNIT_EM), new Dimension(4.F, UNIT_EM)),
|
||||
rect,
|
||||
-1,
|
||||
999,
|
||||
10.F
|
||||
)
|
||||
);
|
||||
@ -96,7 +111,7 @@ public class ImageSizeResolverDefTest {
|
||||
def.resolveImageSize(
|
||||
new ImageSize(new Dimension(1.F, UNIT_EM), null),
|
||||
rect,
|
||||
-1,
|
||||
42,
|
||||
10.F
|
||||
)
|
||||
);
|
||||
@ -110,7 +125,7 @@ public class ImageSizeResolverDefTest {
|
||||
def.resolveImageSize(
|
||||
new ImageSize(null, new Dimension(50, "px")),
|
||||
rect,
|
||||
-1,
|
||||
200,
|
||||
Float.NaN
|
||||
)
|
||||
);
|
||||
@ -124,7 +139,7 @@ public class ImageSizeResolverDefTest {
|
||||
def.resolveImageSize(
|
||||
new ImageSize(null, new Dimension(3.F, UNIT_EM)),
|
||||
rect,
|
||||
-1,
|
||||
40,
|
||||
10.F
|
||||
)
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user