Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b138e287 authored by Daniel U's avatar Daniel U
Browse files

Enhance support to text size spans in toHtml()

Convert AbsoluteSizeSpan to <span> with CSS font-size property with CSS
px as the unit of measurement. Sizes measured in Android px are converted
to dip, since there is no CSS unit which conveys the idea of a device
pixel.

Add support to RelativeSizeSpan, which converts to <span> with CSS
font-size property with em as the unit of measurement.

Change-Id: I9104cc5f9b00ab1a31929565dc49e0026466cc75
parent 5c02d737
Loading
Loading
Loading
Loading
+19 −4
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.ActivityThread;
import android.app.Application;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
@@ -418,9 +420,19 @@ public class Html {
                    i = next;
                }
                if (style[j] instanceof AbsoluteSizeSpan) {
                    out.append("<font size =\"");
                    out.append(((AbsoluteSizeSpan) style[j]).getSize() / 6);
                    out.append("\">");
                    AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                    float sizeDip = s.getSize();
                    if (!s.getDip()) {
                        Application application = ActivityThread.currentApplication();
                        sizeDip /= application.getResources().getDisplayMetrics().density;
                    }

                    // px in CSS is the equivalance of dip in Android
                    out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
                }
                if (style[j] instanceof RelativeSizeSpan) {
                    float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                    out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
                }
                if (style[j] instanceof ForegroundColorSpan) {
                    int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
@@ -442,8 +454,11 @@ public class Html {
                if (style[j] instanceof ForegroundColorSpan) {
                    out.append("</span>");
                }
                if (style[j] instanceof RelativeSizeSpan) {
                    out.append("</span>");
                }
                if (style[j] instanceof AbsoluteSizeSpan) {
                    out.append("</font>");
                    out.append("</span>");
                }
                if (style[j] instanceof URLSpan) {
                    out.append("</a>");