Support more font size attributes

pull/178/head
M66B 5 years ago
parent babeb01863
commit 1f036f5216

@ -79,6 +79,8 @@ public class HtmlHelper {
private static final int PREVIEW_SIZE = 500; // characters private static final int PREVIEW_SIZE = 500; // characters
private static final int DEFAULT_FONT_SIZE = 16; // pixels private static final int DEFAULT_FONT_SIZE = 16; // pixels
private static final float FONT_SMALL = 0.8f;
private static final float FONT_LARGE = 1.25f;
private static final float MIN_LUMINANCE = 0.5f; private static final float MIN_LUMINANCE = 0.5f;
private static final int TAB_SIZE = 2; private static final int TAB_SIZE = 2;
private static final int MAX_AUTO_LINK = 250; private static final int MAX_AUTO_LINK = 250;
@ -357,6 +359,7 @@ public class HtmlHelper {
Whitelist whitelist = Whitelist.relaxed() Whitelist whitelist = Whitelist.relaxed()
.addTags("hr", "abbr", "big", "font", "dfn", "del", "s", "tt") .addTags("hr", "abbr", "big", "font", "dfn", "del", "s", "tt")
.addAttributes(":all", "style") .addAttributes(":all", "style")
.addAttributes("font", "size")
.removeTags("col", "colgroup", "thead", "tbody") .removeTags("col", "colgroup", "thead", "tbody")
.removeAttributes("table", "width") .removeAttributes("table", "width")
.removeAttributes("td", "colspan", "rowspan", "width") .removeAttributes("td", "colspan", "rowspan", "width")
@ -374,11 +377,36 @@ public class HtmlHelper {
// Font // Font
for (Element font : document.select("font")) { for (Element font : document.select("font")) {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font
String color = font.attr("color"); String color = font.attr("color");
String size = font.attr("size");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
font.removeAttr("color"); font.removeAttr("color");
font.removeAttr("face"); font.removeAttr("size");
font.attr("style", "color:" + color + ";");
StringBuilder sb = new StringBuilder();
if (!TextUtils.isEmpty(color))
sb.append("color:").append(color).append(";");
if (!TextUtils.isEmpty(size)) {
try {
int s = Integer.parseInt(size);
if (s < 3)
size = "small";
else if (s > 3)
size = "large";
else
size = "medium";
sb.append("font-size:").append(size).append(";");
} catch (NumberFormatException ex) {
Log.w(ex);
}
}
font.attr("style", sb.toString());
font.tagName("span"); font.tagName("span");
} }
@ -447,7 +475,7 @@ public class HtmlHelper {
Float fsize = getFontSize(value, current); Float fsize = getFontSize(value, current);
if (fsize != null && fsize != 0 && if (fsize != null && fsize != 0 &&
((!small && fsize <= 0.8f) || (!big && fsize >= 1.25))) { ((!small && fsize <= FONT_SMALL) || (!big && fsize >= FONT_LARGE))) {
Element e = new Element(fsize < 1 ? "small" : "big"); Element e = new Element(fsize < 1 ? "small" : "big");
int px = Math.round(DEFAULT_FONT_SIZE * fsize); int px = Math.round(DEFAULT_FONT_SIZE * fsize);
e.attr("x-font-size", Integer.toString(px)); e.attr("x-font-size", Integer.toString(px));
@ -847,18 +875,45 @@ public class HtmlHelper {
if (TextUtils.isEmpty(value)) if (TextUtils.isEmpty(value))
return null; return null;
float _current = (current == null ? 1.0f : current / (float) DEFAULT_FONT_SIZE);
// Absolute
switch (value) {
case "xx-small":
case "x-small":
case "small":
return FONT_SMALL;
case "medium":
return 1.0f;
case "large":
case "x-large":
case "xx-large":
case "xxx-large":
return FONT_LARGE;
}
// Relative
switch (value) {
case "smaller":
return FONT_SMALL * _current;
case "larger":
return FONT_LARGE * _current;
}
if (current == null) if (current == null)
current = DEFAULT_FONT_SIZE; current = DEFAULT_FONT_SIZE;
try { try {
if (value.endsWith("%"))
return Integer.parseInt(value.substring(0, value.length() - 1).trim()) / 100f * _current;
if (value.endsWith("em")) if (value.endsWith("em"))
return Float.parseFloat(value.substring(0, value.length() - 2).trim()) * current / (float) DEFAULT_FONT_SIZE; return Float.parseFloat(value.substring(0, value.length() - 2).trim()) * _current;
if (value.endsWith("rem")) if (value.endsWith("rem"))
return Float.parseFloat(value.substring(0, value.length() - 3).trim()); return Float.parseFloat(value.substring(0, value.length() - 3).trim());
if (value.endsWith("px")) if (value.endsWith("px"))
return Integer.parseInt(value.substring(0, value.length() - 2).trim()) / (float) DEFAULT_FONT_SIZE; return Integer.parseInt(value.substring(0, value.length() - 2).trim()) / (float) DEFAULT_FONT_SIZE;
return Integer.parseInt(value.trim()) / (float) DEFAULT_FONT_SIZE; return Integer.parseInt(value.trim()) / (float) DEFAULT_FONT_SIZE;
} catch (NumberFormatException ignored) { } catch (NumberFormatException ex) {
Log.w(ex);
} }
return null; return null;

Loading…
Cancel
Save