From c90675d67baf923377158a05927fe96e691493bf Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Mon, 9 Mar 2020 14:54:05 +0300 Subject: [PATCH] Latex, default text color if not specified explicitly --- CHANGELOG.md | 5 +- build.gradle | 3 +- docs/docs/v4/ext-latex/README.md | 11 +++- .../ext/latex/JLatexAsyncDrawableSpan.java | 62 +++++++++++++++++++ .../latex/JLatexInlineAsyncDrawableSpan.java | 8 +-- .../markwon/ext/latex/JLatexMathPlugin.java | 22 ++++--- .../markwon/ext/latex/JLatexMathTheme.java | 5 +- .../markwon/sample/latex/LatexActivity.java | 23 ++++++- 8 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexAsyncDrawableSpan.java diff --git a/CHANGELOG.md b/CHANGELOG.md index c2711cc4..a1bf9443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,13 +12,14 @@ .build(); ``` * `JLatexMathPlugin`: add `theme` (to customize both inlines and blocks) -* `JLatexMathPlugin`: add `renderMode` to use previous (pre `4.3.0`) LaTeX rendering +* `JLatexMathPlugin`: add `renderMode` to use previous (pre `4.3.0`) LaTeX rendering (`LEGACY` & `BLOCKS_AND_INLINES`) * add `JLatexMathPlugin.ErrorHandler` to catch latex rendering errors and (optionally) display error drawable ([#204]) +* `JLatexMathPlugin` add text color customization ([#207]) +* `JLatexMathPlugin` will use text color of widget in which it is displayed **if color is not set explicitly** * add `SoftBreakAddsNewLinePlugin` plugin (`core` module) * `LinkResolverDef` defaults to `https` when a link does not have scheme information ([#75]) * add `option` abstraction for `sample` module allowing switching of multiple cases in runtime via menu * non-empty bounds for `AsyncDrawable` when no dimensions are not yet available ([#189]) -* `JLatexMathPlugin` add text color customization ([#207]) * `linkify` - option to use `LinkifyCompat` in `LinkifyPlugin` ([#201])
Thanks to [@drakeet] diff --git a/build.gradle b/build.gradle index 41a8b70b..582e26ae 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ allprojects { } google() jcenter() +// maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } version = VERSION_NAME group = GROUP @@ -69,7 +70,7 @@ ext { 'commonmark-table' : "com.atlassian.commonmark:commonmark-ext-gfm-tables:$commonMarkVersion", 'android-svg' : 'com.caverock:androidsvg:1.4', 'android-gif' : 'pl.droidsonroids.gif:android-gif-drawable:1.2.15', - 'jlatexmath-android' : 'ru.noties:jlatexmath-android:0.1.0', + 'jlatexmath-android' : 'ru.noties:jlatexmath-android:0.1.1', 'okhttp' : 'com.squareup.okhttp3:okhttp:3.9.0', 'prism4j' : 'io.noties:prism4j:2.0.0', 'debug' : 'io.noties:debug:5.0.0@jar', diff --git a/docs/docs/v4/ext-latex/README.md b/docs/docs/v4/ext-latex/README.md index 16fde085..d8f5f241 100644 --- a/docs/docs/v4/ext-latex/README.md +++ b/docs/docs/v4/ext-latex/README.md @@ -48,5 +48,14 @@ final Markwon markwon = Markwon.builder(context) :::tip -Since `JLatexMathPlugin` operates independently of `ImagesPlugin` +Sometimes it is enough to use rendered to an image LaTeX formula and +inline it directly in your markdown document. For this markdown references can be useful. For example: +```markdown + +![markdown-reference] of a solution... + + +[markdown-reference]: data:image/svg+xml;base64,base64encodeddata== +``` +For this to work an image loader that supports data uri and base64 must be used. Default `Markwon` [image-loader](../image/) supports it out of box (including SVG support) ::: \ No newline at end of file diff --git a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexAsyncDrawableSpan.java b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexAsyncDrawableSpan.java new file mode 100644 index 00000000..799aaf37 --- /dev/null +++ b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexAsyncDrawableSpan.java @@ -0,0 +1,62 @@ +package io.noties.markwon.ext.latex; + +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.drawable.Drawable; + +import androidx.annotation.ColorInt; +import androidx.annotation.NonNull; + +import org.scilab.forge.jlatexmath.TeXIcon; + +import io.noties.markwon.core.MarkwonTheme; +import io.noties.markwon.image.AsyncDrawableSpan; +import ru.noties.jlatexmath.JLatexMathDrawable; +import ru.noties.jlatexmath.awt.Color; + +/** + * @since 4.3.0-SNAPSHOT + */ +public class JLatexAsyncDrawableSpan extends AsyncDrawableSpan { + + private final JLatextAsyncDrawable drawable; + private final int color; + private boolean appliedTextColor; + + public JLatexAsyncDrawableSpan( + @NonNull MarkwonTheme theme, + @NonNull JLatextAsyncDrawable drawable, + @ColorInt int color) { + super(theme, drawable, ALIGN_CENTER, false); + this.drawable = drawable; + this.color = color; + // if color is not 0 -> then no need to apply text color + this.appliedTextColor = color != 0; + } + + @Override + public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) { + if (!appliedTextColor && drawable.hasResult()) { + // it is important to check for type (in case of an error, or custom placeholder or whatever + // this result can be of other type) + final Drawable drawableResult = drawable.getResult(); + if (drawableResult instanceof JLatexMathDrawable) { + final JLatexMathDrawable result = (JLatexMathDrawable) drawableResult; + final TeXIcon icon = result.icon(); + icon.setForeground(new Color(paint.getColor())); + appliedTextColor = true; + } + } + super.draw(canvas, text, start, end, x, top, y, bottom, paint); + } + + @NonNull + public JLatextAsyncDrawable drawable() { + return drawable; + } + + @ColorInt + public int color() { + return color; + } +} diff --git a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexInlineAsyncDrawableSpan.java b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexInlineAsyncDrawableSpan.java index 10b59837..09fdd553 100644 --- a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexInlineAsyncDrawableSpan.java +++ b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexInlineAsyncDrawableSpan.java @@ -3,23 +3,23 @@ package io.noties.markwon.ext.latex; import android.graphics.Paint; import android.graphics.Rect; +import androidx.annotation.ColorInt; import androidx.annotation.IntRange; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.image.AsyncDrawable; -import io.noties.markwon.image.AsyncDrawableSpan; /** * @since 4.3.0-SNAPSHOT */ -class JLatexInlineAsyncDrawableSpan extends AsyncDrawableSpan { +class JLatexInlineAsyncDrawableSpan extends JLatexAsyncDrawableSpan { private final AsyncDrawable drawable; - JLatexInlineAsyncDrawableSpan(@NonNull MarkwonTheme theme, @NonNull AsyncDrawable drawable, int alignment, boolean replacementTextIsLink) { - super(theme, drawable, alignment, replacementTextIsLink); + JLatexInlineAsyncDrawableSpan(@NonNull MarkwonTheme theme, @NonNull JLatextAsyncDrawable drawable, @ColorInt int color) { + super(theme, drawable, color); this.drawable = drawable; } diff --git a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathPlugin.java b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathPlugin.java index 1403edb9..eb15af1d 100644 --- a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathPlugin.java +++ b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathPlugin.java @@ -228,7 +228,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { final MarkwonConfiguration configuration = visitor.configuration(); - final AsyncDrawableSpan span = new AsyncDrawableSpan( + final AsyncDrawableSpan span = new JLatexAsyncDrawableSpan( configuration.theme(), new JLatextAsyncDrawable( latex, @@ -236,8 +236,8 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { jLatexBlockImageSizeResolver, null, true), - AsyncDrawableSpan.ALIGN_CENTER, - false); + config.theme.blockTextColor() + ); visitor.setSpans(length, span); @@ -273,8 +273,8 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { inlineImageSizeResolver, null, false), - AsyncDrawableSpan.ALIGN_CENTER, - false); + config.theme.inlineTextColor() + ); visitor.setSpans(length, span); } @@ -415,9 +415,9 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { final JLatextAsyncDrawable jLatextAsyncDrawable = (JLatextAsyncDrawable) drawable; if (jLatextAsyncDrawable.isBlock()) { - jLatexMathDrawable = createBlockDrawable(jLatextAsyncDrawable.getDestination()); + jLatexMathDrawable = createBlockDrawable(jLatextAsyncDrawable); } else { - jLatexMathDrawable = createInlineDrawable(jLatextAsyncDrawable.getDestination()); + jLatexMathDrawable = createInlineDrawable(jLatextAsyncDrawable); } setResult(drawable, jLatexMathDrawable); @@ -448,7 +448,9 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { // @since 4.3.0-SNAPSHOT @NonNull - private JLatexMathDrawable createBlockDrawable(@NonNull String latex) { + private JLatexMathDrawable createBlockDrawable(@NonNull JLatextAsyncDrawable drawable) { + + final String latex = drawable.getDestination(); final JLatexMathTheme theme = config.theme; @@ -478,7 +480,9 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { // @since 4.3.0-SNAPSHOT @NonNull - private JLatexMathDrawable createInlineDrawable(@NonNull String latex) { + private JLatexMathDrawable createInlineDrawable(@NonNull JLatextAsyncDrawable drawable) { + + final String latex = drawable.getDestination(); final JLatexMathTheme theme = config.theme; diff --git a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathTheme.java b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathTheme.java index 4f8ede7b..e729060b 100644 --- a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathTheme.java +++ b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathTheme.java @@ -47,6 +47,7 @@ public abstract class JLatexMathTheme { /** * Special immutable class to hold padding information */ + @SuppressWarnings("WeakerAccess") public static class Padding { public final int left; public final int top; @@ -60,6 +61,7 @@ public abstract class JLatexMathTheme { this.bottom = bottom; } + @NonNull @Override public String toString() { return "Padding{" + @@ -125,6 +127,7 @@ public abstract class JLatexMathTheme { @ColorInt public abstract int blockTextColor(); + @SuppressWarnings({"unused", "UnusedReturnValue"}) public static class Builder { private final float textSize; private final float inlineTextSize; @@ -142,7 +145,7 @@ public abstract class JLatexMathTheme { private Padding inlinePadding; private Padding blockPadding; - private int textColor = 0xFF000000; + private int textColor; private int inlineTextColor; private int blockTextColor; diff --git a/sample/src/main/java/io/noties/markwon/sample/latex/LatexActivity.java b/sample/src/main/java/io/noties/markwon/sample/latex/LatexActivity.java index 9816b0a1..f07ecc1d 100644 --- a/sample/src/main/java/io/noties/markwon/sample/latex/LatexActivity.java +++ b/sample/src/main/java/io/noties/markwon/sample/latex/LatexActivity.java @@ -63,7 +63,16 @@ public class LatexActivity extends ActivityWithMenuOptions { .add("insideBlockQuote", this::insideBlockQuote) .add("error", this::error) .add("legacy", this::legacy) - .add("textColor", this::textColor); + .add("textColor", this::textColor) + .add("defaultTextColor", this::defaultTextColor); + } + + @Override + protected void beforeOptionSelected(@NonNull String option) { + super.beforeOptionSelected(option); + + // reset text color + textView.setTextColor(0xFF000000); } @Override @@ -151,6 +160,18 @@ public class LatexActivity extends ActivityWithMenuOptions { markwon.setMarkdown(textView, md); } + private void defaultTextColor() { + // @since 4.3.0-SNAPSHOT text color is automatically taken from textView + textView.setTextColor(0xFFff0000); + + final String md = wrapLatexInSampleMarkdown(LATEX_LONG_DIVISION); + final Markwon markwon = Markwon.builder(this) + .usePlugin(MarkwonInlineParserPlugin.create()) + .usePlugin(JLatexMathPlugin.create(textView.getTextSize())) + .build(); + markwon.setMarkdown(textView, md); + } + @NonNull private static String wrapLatexInSampleMarkdown(@NonNull String latex) { return "" +