From 3ce50f90b41dc4f908dc761b22b96566b2dfe7a4 Mon Sep 17 00:00:00 2001 From: chengjunzhang61 Date: Wed, 8 Dec 2021 10:02:22 -0500 Subject: [PATCH] Update markdown core plugin --- .../io/noties/markwon/core/CorePlugin.java | 19 ++++- .../io/noties/markwon/core/MarkwonTheme.java | 6 +- .../noties/markwon/core/spans/CodeSpan.java | 7 +- .../markwon/core/spans/HeadingSpan.java | 40 ++++----- .../markwon/image/AsyncDrawableSpan.java | 18 +++- .../markwon/image/ImageClickResolver.java | 9 ++ .../markwon/image/ImageSizeResolverDef.java | 82 +++++++++---------- 7 files changed, 111 insertions(+), 70 deletions(-) create mode 100644 markwon-core/src/main/java/io/noties/markwon/image/ImageClickResolver.java diff --git a/markwon-core/src/main/java/io/noties/markwon/core/CorePlugin.java b/markwon-core/src/main/java/io/noties/markwon/core/CorePlugin.java index 4961505a..62d51903 100644 --- a/markwon-core/src/main/java/io/noties/markwon/core/CorePlugin.java +++ b/markwon-core/src/main/java/io/noties/markwon/core/CorePlugin.java @@ -3,6 +3,8 @@ package io.noties.markwon.core; import android.text.Spannable; import android.text.Spanned; import android.text.method.LinkMovementMethod; +import android.text.style.ClickableSpan; +import android.view.View; import android.widget.TextView; import androidx.annotation.NonNull; @@ -55,6 +57,7 @@ import io.noties.markwon.core.factory.StrongEmphasisSpanFactory; import io.noties.markwon.core.factory.ThematicBreakSpanFactory; import io.noties.markwon.core.spans.OrderedListItemSpan; import io.noties.markwon.core.spans.TextViewSpan; +import io.noties.markwon.image.ImageClickResolver; import io.noties.markwon.image.ImageProps; /** @@ -114,13 +117,19 @@ public class CorePlugin extends AbstractMarkwonPlugin { // @since 4.0.0 private final List onTextAddedListeners = new ArrayList<>(0); - + private static ImageClickResolver imageClickResolver = null; // @since 4.5.0 private boolean hasExplicitMovementMethod; protected CorePlugin() { } + @NonNull + public CorePlugin addImageClickResolver(@NonNull ImageClickResolver clickResolver){ + imageClickResolver = clickResolver; + return this; + } + /** * @since 4.5.0 */ @@ -323,6 +332,7 @@ public class CorePlugin extends AbstractMarkwonPlugin { visitor.visitChildren(image); + // we must check if anything _was_ added, as we need at least one char to render if (length == visitor.length()) { visitor.builder().append('\uFFFC'); @@ -345,8 +355,13 @@ public class CorePlugin extends AbstractMarkwonPlugin { ImageProps.DESTINATION.set(props, destination); ImageProps.REPLACEMENT_TEXT_IS_LINK.set(props, link); ImageProps.IMAGE_SIZE.set(props, null); - visitor.setSpans(length, spanFactory.getSpans(configuration, props)); + visitor.setSpans(length, new ClickableSpan() { + @Override + public void onClick(@NonNull View widget) { + imageClickResolver.clickResolve(widget, destination); + } + }); } }); } diff --git a/markwon-core/src/main/java/io/noties/markwon/core/MarkwonTheme.java b/markwon-core/src/main/java/io/noties/markwon/core/MarkwonTheme.java index 53504310..0753562d 100644 --- a/markwon-core/src/main/java/io/noties/markwon/core/MarkwonTheme.java +++ b/markwon-core/src/main/java/io/noties/markwon/core/MarkwonTheme.java @@ -110,7 +110,7 @@ public class MarkwonTheme { // taken from html spec (most browsers render headings like that) // is not exposed via protected modifier in order to disallow modification private static final float[] HEADING_SIZES = { - 2.F, 1.5F, 1.17F, 1.F, .83F, .67F, + 2.F, 1.75F, 1.514F, 1.388F, 1.135F, 0.946F, }; protected static final int THEMATIC_BREAK_DEF_ALPHA = 25; @@ -393,6 +393,10 @@ public class MarkwonTheme { return color; } + public int getLinkColor() { + return linkColor; + } + /** * @since 3.0.0 */ diff --git a/markwon-core/src/main/java/io/noties/markwon/core/spans/CodeSpan.java b/markwon-core/src/main/java/io/noties/markwon/core/spans/CodeSpan.java index 455fe34b..112baff3 100644 --- a/markwon-core/src/main/java/io/noties/markwon/core/spans/CodeSpan.java +++ b/markwon-core/src/main/java/io/noties/markwon/core/spans/CodeSpan.java @@ -1,5 +1,6 @@ package io.noties.markwon.core.spans; +import android.graphics.Paint; import android.text.TextPaint; import android.text.style.MetricAffectingSpan; @@ -26,10 +27,14 @@ public class CodeSpan extends MetricAffectingSpan { @Override public void updateDrawState(TextPaint ds) { apply(ds); - ds.bgColor = theme.getCodeBackgroundColor(ds); +// ds.bgColor = theme.getCodeBackgroundColor(ds); } private void apply(TextPaint p) { theme.applyCodeTextStyle(p); } + + private int getTagWidth(CharSequence text, int start, int end, Paint paint) { + return Math.round(paint.measureText(text.subSequence(start, end).toString())); + } } diff --git a/markwon-core/src/main/java/io/noties/markwon/core/spans/HeadingSpan.java b/markwon-core/src/main/java/io/noties/markwon/core/spans/HeadingSpan.java index 28cf3f35..ef7540e4 100644 --- a/markwon-core/src/main/java/io/noties/markwon/core/spans/HeadingSpan.java +++ b/markwon-core/src/main/java/io/noties/markwon/core/spans/HeadingSpan.java @@ -49,33 +49,33 @@ public class HeadingSpan extends MetricAffectingSpan implements LeadingMarginSpa @Override public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) { - if ((level == 1 || level == 2) - && LeadingMarginUtils.selfEnd(end, text, this)) { + // if ((level == 1 || level == 2) + // && LeadingMarginUtils.selfEnd(end, text, this)) { - paint.set(p); + // paint.set(p); - theme.applyHeadingBreakStyle(paint); + // theme.applyHeadingBreakStyle(paint); - final float height = paint.getStrokeWidth(); + // final float height = paint.getStrokeWidth(); - if (height > .0F) { + // if (height > .0F) { - final int b = (int) (bottom - height + .5F); + // final int b = (int) (bottom - height + .5F); - final int left; - final int right; - if (dir > 0) { - left = x; - right = c.getWidth(); - } else { - left = x - c.getWidth(); - right = x; - } + // final int left; + // final int right; + // if (dir > 0) { + // left = x; + // right = c.getWidth(); + // } else { + // left = x - c.getWidth(); + // right = x; + // } - rect.set(left, b, right, bottom); - c.drawRect(rect, paint); - } - } + // rect.set(left, b, right, bottom); + // c.drawRect(rect, paint); + // } + // } } /** diff --git a/markwon-core/src/main/java/io/noties/markwon/image/AsyncDrawableSpan.java b/markwon-core/src/main/java/io/noties/markwon/image/AsyncDrawableSpan.java index 27e43720..0516ea90 100644 --- a/markwon-core/src/main/java/io/noties/markwon/image/AsyncDrawableSpan.java +++ b/markwon-core/src/main/java/io/noties/markwon/image/AsyncDrawableSpan.java @@ -4,6 +4,7 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.text.style.ReplacementSpan; +import android.util.Log; import androidx.annotation.IntDef; import androidx.annotation.IntRange; @@ -65,11 +66,20 @@ public class AsyncDrawableSpan extends ReplacementSpan { final Rect rect = drawable.getBounds(); if (fm != null) { - fm.ascent = -rect.bottom; - fm.descent = 0; +// fm.ascent = -rect.bottom; +// fm.descent = 0; +// +// fm.top = fm.ascent; +// fm.bottom = 0; + Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt(); + int fontHeight = fmPaint.descent - fmPaint.ascent; + int drHeight = rect.bottom - rect.top; + int centerY = fmPaint.ascent + fontHeight / 2; + fm.ascent = centerY - drHeight / 2; fm.top = fm.ascent; - fm.bottom = 0; + fm.bottom = centerY + drHeight / 2; + fm.descent = fm.bottom; } size = rect.right; @@ -124,6 +134,8 @@ public class AsyncDrawableSpan extends ReplacementSpan { } canvas.translate(x, translationY); drawable.draw(canvas); + } catch (Exception e){ + Log.e("UseRecycleBitmap", e.toString()); } finally { canvas.restoreToCount(save); } diff --git a/markwon-core/src/main/java/io/noties/markwon/image/ImageClickResolver.java b/markwon-core/src/main/java/io/noties/markwon/image/ImageClickResolver.java new file mode 100644 index 00000000..065949b2 --- /dev/null +++ b/markwon-core/src/main/java/io/noties/markwon/image/ImageClickResolver.java @@ -0,0 +1,9 @@ +package io.noties.markwon.image; + +import android.view.View; + +import androidx.annotation.NonNull; + +public interface ImageClickResolver { + void clickResolve(@NonNull View view, @NonNull String link); +} diff --git a/markwon-core/src/main/java/io/noties/markwon/image/ImageSizeResolverDef.java b/markwon-core/src/main/java/io/noties/markwon/image/ImageSizeResolverDef.java index 52d6abac..cc8ecfec 100644 --- a/markwon-core/src/main/java/io/noties/markwon/image/ImageSizeResolverDef.java +++ b/markwon-core/src/main/java/io/noties/markwon/image/ImageSizeResolverDef.java @@ -38,17 +38,13 @@ public class ImageSizeResolverDef extends ImageSizeResolver { // must be applied only if imageSize is null final Rect rect; final int w = imageBounds.width(); - if (w > canvasWidth) { - final float reduceRatio = (float) w / canvasWidth; - rect = new Rect( - 0, - 0, - canvasWidth, - (int) (imageBounds.height() / reduceRatio + .5F) - ); - } else { - rect = imageBounds; - } + final float reduceRatio = (float) canvasWidth / w; + rect = new Rect( + 0, + 0, + canvasWidth, + (int) (imageBounds.height() * reduceRatio + .5F) + ); return rect; } @@ -60,40 +56,40 @@ public class ImageSizeResolverDef extends ImageSizeResolver { final int imageWidth = imageBounds.width(); final int imageHeight = imageBounds.height(); - final float ratio = (float) imageWidth / imageHeight; + final float ratio = (float) imageHeight / imageWidth; + final int w = canvasWidth; + final int h = canvasWidth * (int)ratio; - if (width != null) { + rect = new Rect(0, 0, w, h); - final int w; - final int h; - - if (UNIT_PERCENT.equals(width.unit)) { - w = (int) (canvasWidth * (width.value / 100.F) + .5F); - } else { - w = resolveAbsolute(width, imageWidth, textSize); - } - - if (height == null - || UNIT_PERCENT.equals(height.unit)) { - h = (int) (w / ratio + .5F); - } else { - h = resolveAbsolute(height, imageHeight, textSize); - } - - rect = new Rect(0, 0, w, h); - - } else if (height != null) { - - if (!UNIT_PERCENT.equals(height.unit)) { - final int h = resolveAbsolute(height, imageHeight, textSize); - final int w = (int) (h * ratio + .5F); - rect = new Rect(0, 0, w, h); - } else { - rect = imageBounds; - } - } else { - rect = imageBounds; - } +// if (width != null) { +// +//// +//// if (UNIT_PERCENT.equals(width.unit)) { +//// w = (int) (canvasWidth * (width.value / 100.F) + .5F); +//// } else { +//// w = resolveAbsolute(width, imageWidth, textSize); +//// } +// +//// if (height == null +//// || UNIT_PERCENT.equals(height.unit)) { +//// h = (int) (w / ratio + .5F); +//// } else { +//// h = resolveAbsolute(height, imageHeight, textSize); +//// } +// +// } else if (height != null) { +// +// if (!UNIT_PERCENT.equals(height.unit)) { +// final int h = resolveAbsolute(height, imageHeight, textSize); +// final int w = (int) (h * ratio + .5F); +// rect = new Rect(0, 0, w, h); +// } else { +// rect = imageBounds; +// } +// } else { +// rect = imageBounds; +// } return rect; }