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

View File

@ -361,14 +361,27 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
final Rect imageBounds = drawable.getResult().getBounds(); final Rect imageBounds = drawable.getResult().getBounds();
final int canvasWidth = drawable.getLastKnownCanvasWidth(); final int canvasWidth = drawable.getLastKnownCanvasWidth();
// todo: scale down when formula is greater than width (keep ratio and apply height) if (fitCanvas) {
if (fitCanvas // we modify bounds only if `fitCanvas` is true
&& imageBounds.width() < canvasWidth) { final int w = imageBounds.width();
// we increase only width (keep height as-is)
if (w < canvasWidth) {
// increase width and center formula (keep height as-is)
return new Rect(0, 0, canvasWidth, imageBounds.height()); 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; return imageBounds;
} }
} }

View File

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