Fix j-latex-plugin background config

This commit is contained in:
Dimitry Ivanov 2019-06-20 16:06:57 +03:00
parent 5c78f1d515
commit 6ed641fa47
2 changed files with 51 additions and 7 deletions

View File

@ -34,6 +34,14 @@ import ru.noties.jlatexmath.JLatexMathDrawable;
*/ */
public class JLatexMathPlugin extends AbstractMarkwonPlugin { public class JLatexMathPlugin extends AbstractMarkwonPlugin {
/**
* @since 4.0.0-SNAPSHOT
*/
public interface BackgroundProvider {
@NonNull
Drawable provide();
}
public interface BuilderConfigure { public interface BuilderConfigure {
void configureBuilder(@NonNull Builder builder); void configureBuilder(@NonNull Builder builder);
} }
@ -64,7 +72,8 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
private final float textSize; private final float textSize;
private final Drawable background; // @since 4.0.0-SNAPSHOT
private final BackgroundProvider backgroundProvider;
@JLatexMathDrawable.Align @JLatexMathDrawable.Align
private final int align; private final int align;
@ -78,7 +87,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
Config(@NonNull Builder builder) { Config(@NonNull Builder builder) {
this.textSize = builder.textSize; this.textSize = builder.textSize;
this.background = builder.background; this.backgroundProvider = builder.backgroundProvider;
this.align = builder.align; this.align = builder.align;
this.fitCanvas = builder.fitCanvas; this.fitCanvas = builder.fitCanvas;
this.padding = builder.padding; this.padding = builder.padding;
@ -149,7 +158,8 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
private final float textSize; private final float textSize;
private Drawable background; // @since 4.0.0-SNAPSHOT
private BackgroundProvider backgroundProvider;
@JLatexMathDrawable.Align @JLatexMathDrawable.Align
private int align = JLatexMathDrawable.ALIGN_CENTER; private int align = JLatexMathDrawable.ALIGN_CENTER;
@ -166,8 +176,8 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
} }
@NonNull @NonNull
public Builder background(@NonNull Drawable background) { public Builder backgroundProvider(@NonNull BackgroundProvider backgroundProvider) {
this.background = background; this.backgroundProvider = backgroundProvider;
return this; return this;
} }
@ -236,7 +246,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
final JLatexMathDrawable jLatexMathDrawable = final JLatexMathDrawable jLatexMathDrawable =
JLatexMathDrawable.builder(drawable.getDestination()) JLatexMathDrawable.builder(drawable.getDestination())
.textSize(config.textSize) .textSize(config.textSize)
.background(config.background) .background(config.backgroundProvider.provide())
.align(config.align) .align(config.align)
.fitCanvas(config.fitCanvas) .fitCanvas(config.fitCanvas)
.padding(config.padding) .padding(config.padding)

View File

@ -1,9 +1,12 @@
package io.noties.markwon.sample.latex; package io.noties.markwon.sample.latex;
import android.app.Activity; import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import io.noties.markwon.Markwon; import io.noties.markwon.Markwon;
@ -44,9 +47,40 @@ public class LatexActivity extends Activity {
final Markwon markwon = Markwon.builder(this) final Markwon markwon = Markwon.builder(this)
// .usePlugin(ImagesPlugin.create(this)) // .usePlugin(ImagesPlugin.create(this))
.usePlugin(JLatexMathPlugin.create(textView.getTextSize())) .usePlugin(JLatexMathPlugin.create(textView.getTextSize(), new JLatexMathPlugin.BuilderConfigure() {
@Override
public void configureBuilder(@NonNull JLatexMathPlugin.Builder builder) {
builder.backgroundProvider(new JLatexMathPlugin.BackgroundProvider() {
@NonNull
@Override
public Drawable provide() {
return new ColorDrawable(0x40ff0000);
}
});
}
}))
.build(); .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" +
"$$";
markwon.setMarkdown(textView, l);
return;
}
markwon.setMarkdown(textView, markdown); markwon.setMarkdown(textView, markdown);
} }
} }