Latex, default text color if not specified explicitly
This commit is contained in:
		
							parent
							
								
									86d34cef6f
								
							
						
					
					
						commit
						c90675d67b
					
				| @ -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]) | ||||
| <br>Thanks to [@drakeet] | ||||
| 
 | ||||
|  | ||||
| @ -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', | ||||
|  | ||||
| @ -48,5 +48,14 @@ final Markwon markwon = Markwon.builder(context) | ||||
| 
 | ||||
| 
 | ||||
| :::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) | ||||
| ::: | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
| @ -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; | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
|  | ||||
| @ -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 "" + | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Dimitry Ivanov
						Dimitry Ivanov