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
diff --git a/core/java/android/text/Html.java b/core/java/android/text/Html.java
index 6622a71..b491710 100644
--- a/core/java/android/text/Html.java
+++ b/core/java/android/text/Html.java
@@ -26,6 +26,8 @@
 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 @@
                     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 @@
                 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>");