Fixed text justification numbered lists

pull/178/head
M66B 5 years ago
parent e5e2126613
commit 63ee1b1c99

@ -34,6 +34,7 @@ import android.text.Layout;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.BulletSpan; import android.text.style.BulletSpan;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
@ -1768,6 +1769,7 @@ public class HtmlHelper {
static Spanned fromDocument(Context context, @NonNull Document document, @Nullable Html.ImageGetter imageGetter, @Nullable Html.TagHandler tagHandler) { static Spanned fromDocument(Context context, @NonNull Document document, @Nullable Html.ImageGetter imageGetter, @Nullable Html.TagHandler tagHandler) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean debug = prefs.getBoolean("debug", false); boolean debug = prefs.getBoolean("debug", false);
int colorAccent = Helper.resolveColor(context, R.attr.colorAccent); int colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
int dp3 = Helper.dp2pixels(context, 3); int dp3 = Helper.dp2pixels(context, 3);
int dp6 = Helper.dp2pixels(context, 6); int dp6 = Helper.dp2pixels(context, 6);
@ -1997,7 +1999,9 @@ public class HtmlHelper {
break; break;
} }
} }
ssb.setSpan(new NumberSpan(dp6, colorAccent, index), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
float textSize = Helper.getTextSize(context, 0);
ssb.setSpan(new NumberSpan(dp6, colorAccent, textSize, index), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
break; break;
case "ol": case "ol":
@ -2125,41 +2129,37 @@ public class HtmlHelper {
} }
private static class NumberSpan implements LeadingMarginSpan { private static class NumberSpan implements LeadingMarginSpan {
private final int gapWidth; private TextPaint tp;
private int color; private String number;
private final int index; private int margin;
private float textWidth;
public NumberSpan(int gapWidth, int color, float textSize, int index) {
public NumberSpan(int gapWidth, int color, int index) { tp = new TextPaint();
this.gapWidth = gapWidth; tp.setStyle(Paint.Style.FILL);
this.color = color; tp.setColor(color);
this.index = index; tp.setTypeface(Typeface.MONOSPACE);
tp.setTextSize(textSize);
number = index + ".";
margin = Math.round(tp.measureText(number) + gapWidth);
} }
@Override @Override
public int getLeadingMargin(boolean first) { public int getLeadingMargin(boolean first) {
return Math.round(textWidth + gapWidth); // https://issuetracker.google.com/issues/36956124
// This is called before drawLeadingMargin to justify the text
return margin;
} }
@Override @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) { 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 (((Spanned) text).getSpanStart(this) == start) { if (text instanceof Spanned &&
Paint.Style ostyle = p.getStyle(); ((Spanned) text).getSpanStart(this) == start) {
int ocolor = p.getColor(); float textSize = tp.getTextSize();
Typeface oface = p.getTypeface(); if (textSize > p.getTextSize())
tp.setTextSize(p.getTextSize());
p.setStyle(Paint.Style.FILL); c.drawText(number, x + dir, baseline, tp);
p.setColor(color); tp.setTextSize(textSize);
p.setTypeface(Typeface.MONOSPACE);
String number = index + ".";
textWidth = p.measureText(number);
c.drawText(number, x, baseline, p);
p.setStyle(ostyle);
p.setColor(ocolor);
p.setTypeface(oface);
} }
} }
} }

Loading…
Cancel
Save