Latex, default text color if not specified explicitly

This commit is contained in:
Dimitry Ivanov 2020-03-09 14:54:05 +03:00
parent 86d34cef6f
commit c90675d67b
8 changed files with 120 additions and 19 deletions

View File

@ -12,13 +12,14 @@
.build(); .build();
``` ```
* `JLatexMathPlugin`: add `theme` (to customize both inlines and blocks) * `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]) * 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) * add `SoftBreakAddsNewLinePlugin` plugin (`core` module)
* `LinkResolverDef` defaults to `https` when a link does not have scheme information ([#75]) * `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 * 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]) * 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]) * `linkify` - option to use `LinkifyCompat` in `LinkifyPlugin` ([#201])
<br>Thanks to [@drakeet] <br>Thanks to [@drakeet]

View File

@ -16,6 +16,7 @@ allprojects {
} }
google() google()
jcenter() jcenter()
// maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
} }
version = VERSION_NAME version = VERSION_NAME
group = GROUP group = GROUP
@ -69,7 +70,7 @@ ext {
'commonmark-table' : "com.atlassian.commonmark:commonmark-ext-gfm-tables:$commonMarkVersion", 'commonmark-table' : "com.atlassian.commonmark:commonmark-ext-gfm-tables:$commonMarkVersion",
'android-svg' : 'com.caverock:androidsvg:1.4', 'android-svg' : 'com.caverock:androidsvg:1.4',
'android-gif' : 'pl.droidsonroids.gif:android-gif-drawable:1.2.15', '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', 'okhttp' : 'com.squareup.okhttp3:okhttp:3.9.0',
'prism4j' : 'io.noties:prism4j:2.0.0', 'prism4j' : 'io.noties:prism4j:2.0.0',
'debug' : 'io.noties:debug:5.0.0@jar', 'debug' : 'io.noties:debug:5.0.0@jar',

View File

@ -48,5 +48,14 @@ final Markwon markwon = Markwon.builder(context)
:::tip :::tip
Since <Badge text="4.0.0" /> `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
<!-- your mardown -->
![markdown-reference] of a solution...
<!-- then reference prerendered and converted to base64 SVG/PNG/GIF/etc -->
[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)
::: :::

View File

@ -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;
}
}

View File

@ -3,23 +3,23 @@ package io.noties.markwon.ext.latex;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.Rect;
import androidx.annotation.ColorInt;
import androidx.annotation.IntRange; import androidx.annotation.IntRange;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.core.MarkwonTheme;
import io.noties.markwon.image.AsyncDrawable; import io.noties.markwon.image.AsyncDrawable;
import io.noties.markwon.image.AsyncDrawableSpan;
/** /**
* @since 4.3.0-SNAPSHOT * @since 4.3.0-SNAPSHOT
*/ */
class JLatexInlineAsyncDrawableSpan extends AsyncDrawableSpan { class JLatexInlineAsyncDrawableSpan extends JLatexAsyncDrawableSpan {
private final AsyncDrawable drawable; private final AsyncDrawable drawable;
JLatexInlineAsyncDrawableSpan(@NonNull MarkwonTheme theme, @NonNull AsyncDrawable drawable, int alignment, boolean replacementTextIsLink) { JLatexInlineAsyncDrawableSpan(@NonNull MarkwonTheme theme, @NonNull JLatextAsyncDrawable drawable, @ColorInt int color) {
super(theme, drawable, alignment, replacementTextIsLink); super(theme, drawable, color);
this.drawable = drawable; this.drawable = drawable;
} }

View File

@ -228,7 +228,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
final MarkwonConfiguration configuration = visitor.configuration(); final MarkwonConfiguration configuration = visitor.configuration();
final AsyncDrawableSpan span = new AsyncDrawableSpan( final AsyncDrawableSpan span = new JLatexAsyncDrawableSpan(
configuration.theme(), configuration.theme(),
new JLatextAsyncDrawable( new JLatextAsyncDrawable(
latex, latex,
@ -236,8 +236,8 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
jLatexBlockImageSizeResolver, jLatexBlockImageSizeResolver,
null, null,
true), true),
AsyncDrawableSpan.ALIGN_CENTER, config.theme.blockTextColor()
false); );
visitor.setSpans(length, span); visitor.setSpans(length, span);
@ -273,8 +273,8 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
inlineImageSizeResolver, inlineImageSizeResolver,
null, null,
false), false),
AsyncDrawableSpan.ALIGN_CENTER, config.theme.inlineTextColor()
false); );
visitor.setSpans(length, span); visitor.setSpans(length, span);
} }
@ -415,9 +415,9 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
final JLatextAsyncDrawable jLatextAsyncDrawable = (JLatextAsyncDrawable) drawable; final JLatextAsyncDrawable jLatextAsyncDrawable = (JLatextAsyncDrawable) drawable;
if (jLatextAsyncDrawable.isBlock()) { if (jLatextAsyncDrawable.isBlock()) {
jLatexMathDrawable = createBlockDrawable(jLatextAsyncDrawable.getDestination()); jLatexMathDrawable = createBlockDrawable(jLatextAsyncDrawable);
} else { } else {
jLatexMathDrawable = createInlineDrawable(jLatextAsyncDrawable.getDestination()); jLatexMathDrawable = createInlineDrawable(jLatextAsyncDrawable);
} }
setResult(drawable, jLatexMathDrawable); setResult(drawable, jLatexMathDrawable);
@ -448,7 +448,9 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
// @since 4.3.0-SNAPSHOT // @since 4.3.0-SNAPSHOT
@NonNull @NonNull
private JLatexMathDrawable createBlockDrawable(@NonNull String latex) { private JLatexMathDrawable createBlockDrawable(@NonNull JLatextAsyncDrawable drawable) {
final String latex = drawable.getDestination();
final JLatexMathTheme theme = config.theme; final JLatexMathTheme theme = config.theme;
@ -478,7 +480,9 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
// @since 4.3.0-SNAPSHOT // @since 4.3.0-SNAPSHOT
@NonNull @NonNull
private JLatexMathDrawable createInlineDrawable(@NonNull String latex) { private JLatexMathDrawable createInlineDrawable(@NonNull JLatextAsyncDrawable drawable) {
final String latex = drawable.getDestination();
final JLatexMathTheme theme = config.theme; final JLatexMathTheme theme = config.theme;

View File

@ -47,6 +47,7 @@ public abstract class JLatexMathTheme {
/** /**
* Special immutable class to hold padding information * Special immutable class to hold padding information
*/ */
@SuppressWarnings("WeakerAccess")
public static class Padding { public static class Padding {
public final int left; public final int left;
public final int top; public final int top;
@ -60,6 +61,7 @@ public abstract class JLatexMathTheme {
this.bottom = bottom; this.bottom = bottom;
} }
@NonNull
@Override @Override
public String toString() { public String toString() {
return "Padding{" + return "Padding{" +
@ -125,6 +127,7 @@ public abstract class JLatexMathTheme {
@ColorInt @ColorInt
public abstract int blockTextColor(); public abstract int blockTextColor();
@SuppressWarnings({"unused", "UnusedReturnValue"})
public static class Builder { public static class Builder {
private final float textSize; private final float textSize;
private final float inlineTextSize; private final float inlineTextSize;
@ -142,7 +145,7 @@ public abstract class JLatexMathTheme {
private Padding inlinePadding; private Padding inlinePadding;
private Padding blockPadding; private Padding blockPadding;
private int textColor = 0xFF000000; private int textColor;
private int inlineTextColor; private int inlineTextColor;
private int blockTextColor; private int blockTextColor;

View File

@ -63,7 +63,16 @@ public class LatexActivity extends ActivityWithMenuOptions {
.add("insideBlockQuote", this::insideBlockQuote) .add("insideBlockQuote", this::insideBlockQuote)
.add("error", this::error) .add("error", this::error)
.add("legacy", this::legacy) .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 @Override
@ -151,6 +160,18 @@ public class LatexActivity extends ActivityWithMenuOptions {
markwon.setMarkdown(textView, md); 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 @NonNull
private static String wrapLatexInSampleMarkdown(@NonNull String latex) { private static String wrapLatexInSampleMarkdown(@NonNull String latex) {
return "" + return "" +