FairEmail/app/src/main/java/eu/faircode/email/NumberSpan.java

142 lines
4.5 KiB
Java
Raw Normal View History

2020-08-12 08:25:08 +00:00
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 <http://www.gnu.org/licenses/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2020-08-12 08:25:08 +00:00
*/
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.Spanned;
import android.text.TextPaint;
2021-10-14 17:02:50 +00:00
import android.text.TextUtils;
2020-08-12 08:25:08 +00:00
import android.text.style.BulletSpan;
2021-10-14 17:02:50 +00:00
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
2020-08-12 08:25:08 +00:00
public class NumberSpan extends BulletSpan {
2021-05-06 08:35:56 +00:00
private int indentWidth;
2021-05-06 06:22:04 +00:00
private int level;
2020-08-12 10:03:33 +00:00
private int index;
2020-08-12 08:25:08 +00:00
private TextPaint tp;
private String number;
2021-05-06 08:35:56 +00:00
private int numberWidth;
2020-08-12 08:25:08 +00:00
private int margin;
2021-10-14 17:02:50 +00:00
private static final List<String> SUPPORTED_TYPES = Collections.unmodifiableList(Arrays.asList(
"lower-alpha", "lower-latin",
2021-10-14 17:53:53 +00:00
"upper-alpha", "upper-latin",
"lower-roman", "upper-roman"
2021-10-14 17:02:50 +00:00
));
// https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type
static boolean isSupportedType(String type) {
if (TextUtils.isEmpty(type))
return false;
if (type.startsWith("decimal"))
return true;
if (SUPPORTED_TYPES.contains(type))
return true;
return false;
}
2021-05-06 06:22:04 +00:00
public NumberSpan(int indentWidth, int gapWidth, int color, float textSize, int level, int index) {
2021-10-14 17:02:50 +00:00
this(indentWidth, gapWidth, color, textSize, level, index, null);
}
public NumberSpan(int indentWidth, int gapWidth, int color, float textSize, int level, int index, String type) {
2020-08-12 08:25:08 +00:00
tp = new TextPaint();
tp.setStyle(Paint.Style.FILL);
tp.setColor(color);
tp.setTypeface(Typeface.MONOSPACE);
tp.setTextSize(textSize);
2021-05-06 06:22:04 +00:00
this.indentWidth = indentWidth;
this.level = level;
2020-08-12 10:03:33 +00:00
this.index = index;
2021-10-14 17:02:50 +00:00
if (TextUtils.isEmpty(type))
number = index + ".";
2021-10-14 17:53:53 +00:00
else {
2021-10-14 17:02:50 +00:00
switch (type) {
case "lower-alpha":
case "lower-latin":
2021-10-14 17:53:53 +00:00
number = Character.toString((char) ((int) 'a' + index));
2021-10-14 17:02:50 +00:00
break;
case "upper-alpha":
case "upper-latin":
2021-10-14 17:53:53 +00:00
number = Character.toString((char) ((int) 'A' + index));
break;
case "lower-roman":
number = Helper.toRoman(index).toLowerCase(Locale.ROOT);
break;
case "upper-roman":
number = Helper.toRoman(index);
2021-10-14 17:02:50 +00:00
break;
default:
2021-10-14 17:53:53 +00:00
number = Integer.toString(index);
2021-10-14 17:02:50 +00:00
}
2021-10-14 17:53:53 +00:00
number += '.';
}
2021-10-14 17:02:50 +00:00
2021-05-06 08:35:56 +00:00
numberWidth = Math.round(tp.measureText(number));
margin = numberWidth + gapWidth;
2020-08-12 08:25:08 +00:00
}
2020-08-12 10:03:33 +00:00
float getTextSize() {
return tp.getTextSize();
}
2021-05-06 06:22:04 +00:00
int getLevel() {
return this.level;
}
void setLevel(int level) {
this.level = level;
}
2020-08-12 10:03:33 +00:00
int getIndex() {
return index;
}
2020-08-12 08:25:08 +00:00
@Override
public int getLeadingMargin(boolean first) {
// https://issuetracker.google.com/issues/36956124
// This is called before drawLeadingMargin to justify the text
2021-05-06 09:01:27 +00:00
return indentWidth * (level + 1) + margin;
2020-08-12 08:25:08 +00:00
}
@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 (text instanceof Spanned &&
((Spanned) text).getSpanStart(this) == start) {
float textSize = tp.getTextSize();
if (textSize > p.getTextSize())
tp.setTextSize(p.getTextSize());
2021-05-06 08:35:56 +00:00
int offset = (dir < 0 ? numberWidth : 0);
2021-05-06 09:01:27 +00:00
c.drawText(number, x + indentWidth * (level + 1) * dir - offset, baseline, tp);
2020-08-12 08:25:08 +00:00
tp.setTextSize(textSize);
}
}
}