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);
}
}

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M22,18h-2v1h3v1h-4v-2c0,-0.55 0.45,-1 1,-1h2v-1h-3v-1h3c0.55,0 1,0.45 1,1v1C23,17.55 22.55,18 22,18zM5.88,18h2.66l3.4,-5.42h0.12l3.4,5.42h2.66l-4.65,-7.27L17.81,4h-2.68l-3.07,4.99h-0.12L8.85,4H6.19l4.32,6.73L5.88,18z"/>
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M22,7h-2v1h3v1h-4V7c0,-0.55 0.45,-1 1,-1h2V5h-3V4h3c0.55,0 1,0.45 1,1v1C23,6.55 22.55,7 22,7zM5.88,20h2.66l3.4,-5.42h0.12l3.4,5.42h2.66l-4.65,-7.27L17.81,6h-2.68l-3.07,4.99h-0.12L8.85,6H6.19l4.32,6.73L5.88,20z"/>
</vector>

View File

@ -151,8 +151,26 @@
</group>
<group
android:id="@+id/group_style_strikethrough"
android:id="@+id/group_style_subscript"
android:orderInCategory="10">
<item
android:id="@+id/menu_style_subscript"
android:icon="@drawable/twotone_subscript_24"
android:title="@string/title_style_subscript" />
</group>
<group
android:id="@+id/group_style_superscript"
android:orderInCategory="11">
<item
android:id="@+id/menu_style_superscript"
android:icon="@drawable/twotone_superscript_24"
android:title="@string/title_style_superscript" />
</group>
<group
android:id="@+id/group_style_strikethrough"
android:orderInCategory="12">
<item
android:id="@+id/menu_style_strikethrough"
android:icon="@drawable/twotone_format_strikethrough_24"
@ -161,7 +179,7 @@
<group
android:id="@+id/group_style_code"
android:orderInCategory="11">
android:orderInCategory="13">
<item
android:id="@+id/menu_style_code"
android:icon="@drawable/twotone_code_24"
@ -170,7 +188,7 @@
<group
android:id="@+id/group_style_clear"
android:orderInCategory="12">
android:orderInCategory="14">
<item
android:id="@+id/menu_style_clear"
android:icon="@drawable/twotone_format_clear_24"

View File

@ -1444,6 +1444,8 @@
<string name="title_style_blockquote">Block quote</string>
<string name="title_style_indentation">Indentation</string>
<string name="title_style_mark">Highlight</string>
<string name="title_style_subscript">Subscript</string>
<string name="title_style_superscript">Superscript</string>
<string name="title_style_strikethrough">Strikethrough</string>
<string name="title_style_code" translatable="false">Code</string>
<string name="title_style_clear">Clear formatting</string>