From aad655d159a93e50b2e65ea52acd841fc0a399d0 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 7 May 2021 14:29:04 +0200 Subject: [PATCH] Refactoring --- .../eu/faircode/email/CustomTypefaceSpan.java | 55 +++++++++++++++ .../java/eu/faircode/email/HtmlHelper.java | 68 ------------------- .../main/java/eu/faircode/email/LineSpan.java | 62 +++++++++++++++++ 3 files changed, 117 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/CustomTypefaceSpan.java create mode 100644 app/src/main/java/eu/faircode/email/LineSpan.java diff --git a/app/src/main/java/eu/faircode/email/CustomTypefaceSpan.java b/app/src/main/java/eu/faircode/email/CustomTypefaceSpan.java new file mode 100644 index 0000000000..a5bac7a2a3 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/CustomTypefaceSpan.java @@ -0,0 +1,55 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + FairEmail is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FairEmail. If not, see . + + Copyright 2018-2021 by Marcel Bokhorst (M66B) +*/ + +import android.graphics.Paint; +import android.graphics.Typeface; +import android.text.TextPaint; +import android.text.style.TypefaceSpan; + +public class CustomTypefaceSpan extends TypefaceSpan { + private final Typeface newType; + + public CustomTypefaceSpan(String family, Typeface type) { + super(family); + newType = type; + } + + @Override + public void updateDrawState(TextPaint ds) { + applyCustomTypeFace(ds, newType); + } + + @Override + public void updateMeasureState(TextPaint paint) { + applyCustomTypeFace(paint, newType); + } + + private static void applyCustomTypeFace(Paint paint, Typeface tf) { + Typeface old = paint.getTypeface(); + int oldStyle = (old == null ? 0 : old.getStyle()); + int fake = oldStyle & ~tf.getStyle(); + if ((fake & Typeface.BOLD) != 0) + paint.setFakeBoldText(true); + if ((fake & Typeface.ITALIC) != 0) + paint.setTextSkewX(-0.25f); + paint.setTypeface(tf); + } +} diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index c3b8b2c53f..535fc0af6b 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -24,9 +24,6 @@ import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; -import android.graphics.DashPathEffect; -import android.graphics.Paint; -import android.graphics.PathEffect; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.net.Uri; @@ -37,7 +34,6 @@ import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextDirectionHeuristics; -import android.text.TextPaint; import android.text.TextUtils; import android.text.style.AlignmentSpan; import android.text.style.BulletSpan; @@ -45,7 +41,6 @@ import android.text.style.ForegroundColorSpan; import android.text.style.ImageSpan; import android.text.style.QuoteSpan; import android.text.style.RelativeSizeSpan; -import android.text.style.ReplacementSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.SubscriptSpan; @@ -2675,67 +2670,4 @@ public class HtmlHelper { spanned.getSpanFlags(spans[i])); return reverse; } - - public static class LineSpan extends ReplacementSpan { - private int lineColor; - private float strokeWidth; - private float dashLength; - - LineSpan(int lineColor, float strokeWidth, float dashLength) { - this.lineColor = lineColor; - this.strokeWidth = strokeWidth; - this.dashLength = dashLength; - } - - @Override - public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) { - return 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) { - int ypos = (top + bottom) / 2; - int c = paint.getColor(); - float s = paint.getStrokeWidth(); - PathEffect p = paint.getPathEffect(); - paint.setColor(lineColor); - paint.setStrokeWidth(strokeWidth); - if (dashLength != 0) - paint.setPathEffect(new DashPathEffect(new float[]{dashLength, dashLength}, 0)); - canvas.drawLine(0, ypos, canvas.getWidth(), ypos, paint); - paint.setColor(c); - paint.setStrokeWidth(s); - paint.setPathEffect(p); - } - } - - public static class CustomTypefaceSpan extends TypefaceSpan { - private final Typeface newType; - - public CustomTypefaceSpan(String family, Typeface type) { - super(family); - newType = type; - } - - @Override - public void updateDrawState(TextPaint ds) { - applyCustomTypeFace(ds, newType); - } - - @Override - public void updateMeasureState(TextPaint paint) { - applyCustomTypeFace(paint, newType); - } - - private static void applyCustomTypeFace(Paint paint, Typeface tf) { - Typeface old = paint.getTypeface(); - int oldStyle = (old == null ? 0 : old.getStyle()); - int fake = oldStyle & ~tf.getStyle(); - if ((fake & Typeface.BOLD) != 0) - paint.setFakeBoldText(true); - if ((fake & Typeface.ITALIC) != 0) - paint.setTextSkewX(-0.25f); - paint.setTypeface(tf); - } - } } diff --git a/app/src/main/java/eu/faircode/email/LineSpan.java b/app/src/main/java/eu/faircode/email/LineSpan.java new file mode 100644 index 0000000000..2d573ebd66 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/LineSpan.java @@ -0,0 +1,62 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + FairEmail is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FairEmail. If not, see . + + Copyright 2018-2021 by Marcel Bokhorst (M66B) +*/ + +import android.graphics.Canvas; +import android.graphics.DashPathEffect; +import android.graphics.Paint; +import android.graphics.PathEffect; +import android.text.style.ReplacementSpan; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +public class LineSpan extends ReplacementSpan { + private int lineColor; + private float strokeWidth; + private float dashLength; + + LineSpan(int lineColor, float strokeWidth, float dashLength) { + this.lineColor = lineColor; + this.strokeWidth = strokeWidth; + this.dashLength = dashLength; + } + + @Override + public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) { + return 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) { + int ypos = (top + bottom) / 2; + int c = paint.getColor(); + float s = paint.getStrokeWidth(); + PathEffect p = paint.getPathEffect(); + paint.setColor(lineColor); + paint.setStrokeWidth(strokeWidth); + if (dashLength != 0) + paint.setPathEffect(new DashPathEffect(new float[]{dashLength, dashLength}, 0)); + canvas.drawLine(0, ypos, canvas.getWidth(), ypos, paint); + paint.setColor(c); + paint.setStrokeWidth(s); + paint.setPathEffect(p); + } +}