Added sub/superscript to composer

This commit is contained in:
M66B
2022-10-15 10:41:07 +02:00
parent 8b47f1a1d0
commit 3ad1d40bb5
8 changed files with 178 additions and 16 deletions

View File

@@ -53,8 +53,6 @@ import android.text.style.QuoteSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
@@ -1206,13 +1204,6 @@ public class HtmlHelper {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/abbr
document.select("abbr").tagName("u");
// Subscript/Superscript
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sub
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup
if (!view)
for (Element subp : document.select("sub,sup"))
subp.tagName("small");
// Tables
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table
for (Element table : document.select("table")) {
@@ -3645,12 +3636,10 @@ public class HtmlHelper {
// Do nothing
break;
case "sub":
setSpan(ssb, new SubscriptSpan(), start, ssb.length());
setSpan(ssb, new RelativeSizeSpan(FONT_SMALL), start, ssb.length());
setSpan(ssb, new SubscriptSpanEx(), start, ssb.length());
break;
case "sup":
setSpan(ssb, new SuperscriptSpan(), start, ssb.length());
setSpan(ssb, new RelativeSizeSpan(FONT_SMALL), start, ssb.length());
setSpan(ssb, new SuperscriptSpanEx(), start, ssb.length());
break;
case "table":
case "thead":

View File

@@ -80,6 +80,7 @@ public class StyleHelper {
AlignmentSpan.class,
BulletSpanEx.class, NumberSpan.class,
QuoteSpan.class, IndentSpan.class,
SubscriptSpanEx.class, SuperscriptSpanEx.class,
StrikethroughSpan.class,
URLSpan.class,
TypefaceSpan.class, CustomTypefaceSpan.class,
@@ -246,6 +247,10 @@ public class StyleHelper {
return setBlockQuote(etBody, start, end, true);
} else if (groupId == R.id.group_style_mark) {
return setMark(item);
} else if (groupId == R.id.group_style_subscript) {
return setSubscript(item);
} else if (groupId == R.id.group_style_superscript) {
return setSuperscript(item);
} else if (groupId == R.id.group_style_strikethrough) {
return setStrikeThrough(item);
} else if (groupId == R.id.group_style_code) {
@@ -465,6 +470,56 @@ public class StyleHelper {
return true;
}
private boolean setSubscript(MenuItem item) {
Log.breadcrumb("style", "action", "subscript");
boolean has = false;
SubscriptSpanEx[] spans = edit.getSpans(start, end, SubscriptSpanEx.class);
for (SubscriptSpanEx span : spans) {
int s = edit.getSpanStart(span);
int e = edit.getSpanEnd(span);
int f = edit.getSpanFlags(span);
edit.removeSpan(span);
if (splitSpan(edit, start, end, s, e, f, true,
new SubscriptSpanEx(), new SubscriptSpanEx()))
has = true;
}
if (!has)
edit.setSpan(new SubscriptSpanEx(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(edit);
etBody.setSelection(start, end);
return true;
}
private boolean setSuperscript(MenuItem item) {
Log.breadcrumb("style", "action", "superscript");
boolean has = false;
SuperscriptSpanEx[] spans = edit.getSpans(start, end, SuperscriptSpanEx.class);
for (SuperscriptSpanEx span : spans) {
int s = edit.getSpanStart(span);
int e = edit.getSpanEnd(span);
int f = edit.getSpanFlags(span);
edit.removeSpan(span);
if (splitSpan(edit, start, end, s, e, f, true,
new SuperscriptSpanEx(), new SuperscriptSpanEx()))
has = true;
}
if (!has) {
edit.setSpan(new SuperscriptSpanEx(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
edit.setSpan(new RelativeSizeSpan(HtmlHelper.FONT_SMALL), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
etBody.setText(edit);
etBody.setSelection(start, end);
return true;
}
private boolean setStrikeThrough(MenuItem item) {
Log.breadcrumb("style", "action", "strike");

View File

@@ -0,0 +1,39 @@
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/>.
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import android.text.TextPaint;
import android.text.style.SubscriptSpan;
import androidx.annotation.NonNull;
public class SubscriptSpanEx extends SubscriptSpan {
@Override
public void updateDrawState(@NonNull TextPaint textPaint) {
super.updateDrawState(textPaint);
textPaint.setTextSize(textPaint.getTextSize() * HtmlHelper.FONT_SMALL);
}
@Override
public void updateMeasureState(@NonNull TextPaint textPaint) {
super.updateMeasureState(textPaint);
textPaint.setTextSize(textPaint.getTextSize() * HtmlHelper.FONT_SMALL);
}
}

View File

@@ -0,0 +1,39 @@
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/>.
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import android.text.TextPaint;
import android.text.style.SuperscriptSpan;
import androidx.annotation.NonNull;
public class SuperscriptSpanEx extends SuperscriptSpan {
@Override
public void updateDrawState(@NonNull TextPaint textPaint) {
super.updateDrawState(textPaint);
textPaint.setTextSize(textPaint.getTextSize() * HtmlHelper.FONT_SMALL);
}
@Override
public void updateMeasureState(@NonNull TextPaint textPaint) {
super.updateMeasureState(textPaint);
textPaint.setTextSize(textPaint.getTextSize() * HtmlHelper.FONT_SMALL);
}
}