Scale down LaTeX formula if exceed canvas width (keep ratio)

This commit is contained in:
Dimitry Ivanov 2019-07-04 17:26:03 +03:00
parent 8daa59709b
commit 879dde1382
2 changed files with 40 additions and 27 deletions
markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex
sample/src/main/java/io/noties/markwon/sample/latex

@ -361,12 +361,25 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
final Rect imageBounds = drawable.getResult().getBounds();
final int canvasWidth = drawable.getLastKnownCanvasWidth();
// todo: scale down when formula is greater than width (keep ratio and apply height)
if (fitCanvas) {
if (fitCanvas
&& imageBounds.width() < canvasWidth) {
// we increase only width (keep height as-is)
return new Rect(0, 0, canvasWidth, imageBounds.height());
// we modify bounds only if `fitCanvas` is true
final int w = imageBounds.width();
if (w < canvasWidth) {
// increase width and center formula (keep height as-is)
return new Rect(0, 0, canvasWidth, imageBounds.height());
}
// @since 4.0.2 we additionally scale down the resulting formula (keeping the ratio)
// the thing is - JLatexMathDrawable will do it anyway, but it will modify its own
// bounds (which AsyncDrawable won't catch), thus leading to an empty space after the formula
if (w > canvasWidth) {
// here we must scale it down (keeping the ratio)
final float ratio = (float) w / imageBounds.height();
final int h = (int) (canvasWidth / ratio + .5F);
return new Rect(0, 0, canvasWidth, h);
}
}
return imageBounds;

@ -66,29 +66,29 @@ public class LatexActivity extends Activity {
// }))
.usePlugin(JLatexMathPlugin.create(textView.getTextSize()))
.build();
if (true) {
// final String l = "$$\n" +
// " P(X=r)=\\frac{\\lambda^r e^{-\\lambda}}{r!}\n" +
// "$$\n" +
// "\n" +
// "$$\n" +
// " P(X<r)=P(X<r-1)\n" +
// "$$\n" +
// "\n" +
// "$$\n" +
// " P(X>r)=1-P(X<r=1)\n" +
// "$$\n" +
// "\n" +
// "$$\n" +
// " \\text{Variance} = \\lambda\n" +
//
// if (true) {
//// final String l = "$$\n" +
//// " P(X=r)=\\frac{\\lambda^r e^{-\\lambda}}{r!}\n" +
//// "$$\n" +
//// "\n" +
//// "$$\n" +
//// " P(X<r)=P(X<r-1)\n" +
//// "$$\n" +
//// "\n" +
//// "$$\n" +
//// " P(X>r)=1-P(X<r=1)\n" +
//// "$$\n" +
//// "\n" +
//// "$$\n" +
//// " \\text{Variance} = \\lambda\n" +
//// "$$";
// final String l = "$$ \n" +
// " \\sigma_T^2 = \\frac{1-p}{p^2}\n" +
// "$$";
final String l = "$$ \n" +
" \\sigma_T^2 = \\frac{1-p}{p^2}\n" +
"$$";
markwon.setMarkdown(textView, l);
return;
}
// markwon.setMarkdown(textView, l);
// return;
// }
markwon.setMarkdown(textView, markdown);
}