Fix wrong fitCanvas config for JLatexMathPlugin

This commit is contained in:
Dimitry Ivanov 2019-06-20 16:19:43 +03:00
parent 6ed641fa47
commit d630039196
2 changed files with 47 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package io.noties.markwon.ext.latex; package io.noties.markwon.ext.latex;
import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
@ -27,6 +28,7 @@ import io.noties.markwon.image.AsyncDrawableLoader;
import io.noties.markwon.image.AsyncDrawableScheduler; import io.noties.markwon.image.AsyncDrawableScheduler;
import io.noties.markwon.image.AsyncDrawableSpan; import io.noties.markwon.image.AsyncDrawableSpan;
import io.noties.markwon.image.ImageSize; import io.noties.markwon.image.ImageSize;
import io.noties.markwon.image.ImageSizeResolver;
import ru.noties.jlatexmath.JLatexMathDrawable; import ru.noties.jlatexmath.JLatexMathDrawable;
/** /**
@ -102,10 +104,12 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
} }
private final JLatextAsyncDrawableLoader jLatextAsyncDrawableLoader; private final JLatextAsyncDrawableLoader jLatextAsyncDrawableLoader;
private final JLatexImageSizeResolver jLatexImageSizeResolver;
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
JLatexMathPlugin(@NonNull Config config) { JLatexMathPlugin(@NonNull Config config) {
this.jLatextAsyncDrawableLoader = new JLatextAsyncDrawableLoader(config); this.jLatextAsyncDrawableLoader = new JLatextAsyncDrawableLoader(config);
this.jLatexImageSizeResolver = new JLatexImageSizeResolver(config.fitCanvas);
} }
@Override @Override
@ -132,10 +136,8 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
new AsyncDrawable( new AsyncDrawable(
latex, latex,
jLatextAsyncDrawableLoader, jLatextAsyncDrawableLoader,
configuration.imageSizeResolver(), jLatexImageSizeResolver,
new ImageSize( null),
new ImageSize.Dimension(100, "%"),
null)),
AsyncDrawableSpan.ALIGN_BOTTOM, AsyncDrawableSpan.ALIGN_BOTTOM,
false); false);
@ -290,4 +292,32 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
return null; return null;
} }
} }
// we must make drawable fit canvas (if specified), but do not keep the ratio whilst scaling up
// @since 4.0.0-SNAPSHOT
private static class JLatexImageSizeResolver extends ImageSizeResolver {
private final boolean fitCanvas;
JLatexImageSizeResolver(boolean fitCanvas) {
this.fitCanvas = fitCanvas;
}
@NonNull
@Override
public Rect resolveImageSize(
@Nullable ImageSize imageSize,
@NonNull Rect imageBounds,
int canvasWidth,
float textSize) {
if (fitCanvas
&& imageBounds.width() < canvasWidth) {
// we increase only width (keep height as-is)
return new Rect(0, 0, canvasWidth, imageBounds.height());
}
return imageBounds;
}
}
} }

View File

@ -12,6 +12,7 @@ import androidx.annotation.Nullable;
import io.noties.markwon.Markwon; import io.noties.markwon.Markwon;
import io.noties.markwon.ext.latex.JLatexMathPlugin; import io.noties.markwon.ext.latex.JLatexMathPlugin;
import io.noties.markwon.sample.R; import io.noties.markwon.sample.R;
import ru.noties.jlatexmath.JLatexMathDrawable;
public class LatexActivity extends Activity { public class LatexActivity extends Activity {
@ -50,13 +51,18 @@ public class LatexActivity extends Activity {
.usePlugin(JLatexMathPlugin.create(textView.getTextSize(), new JLatexMathPlugin.BuilderConfigure() { .usePlugin(JLatexMathPlugin.create(textView.getTextSize(), new JLatexMathPlugin.BuilderConfigure() {
@Override @Override
public void configureBuilder(@NonNull JLatexMathPlugin.Builder builder) { public void configureBuilder(@NonNull JLatexMathPlugin.Builder builder) {
builder.backgroundProvider(new JLatexMathPlugin.BackgroundProvider() { builder
@NonNull .backgroundProvider(new JLatexMathPlugin.BackgroundProvider() {
@Override @NonNull
public Drawable provide() { @Override
return new ColorDrawable(0x40ff0000); public Drawable provide() {
} return new ColorDrawable(0x40ff0000);
}); }
})
.fitCanvas(true)
.align(JLatexMathDrawable.ALIGN_LEFT)
.padding(48)
;
} }
})) }))
.build(); .build();