From 7c4abe0c9cc8d0a54fc4ca7c3ca0af67d6c46c54 Mon Sep 17 00:00:00 2001 From: liato Date: Wed, 16 Mar 2011 18:42:58 +0100 Subject: [PATCH] Render colors correctly in topics and other foreground colored places. --- application/src/org/yaaic/model/Message.java | 14 ++++++++++++-- application/src/org/yaaic/utils/Colors.java | 3 +++ application/src/org/yaaic/utils/Html2.java | 5 ++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/application/src/org/yaaic/model/Message.java b/application/src/org/yaaic/model/Message.java index 5d1fea0..a39773a 100644 --- a/application/src/org/yaaic/model/Message.java +++ b/application/src/org/yaaic/model/Message.java @@ -231,7 +231,9 @@ public class Message String nick = hasSender() ? "<" + sender + "> " : ""; String timestamp = settings.showTimestamp() ? renderTimeStamp(settings.use24hFormat()) : ""; if (settings.showMircColors()) { - String htmltext = Colors.mircColorParser(TextUtils.htmlEncode(text)); + // Tagsoup doesn't like when a html string begins with a tag so we'll surround the html with
 tags.
+                String entext = "
"+TextUtils.htmlEncode(text).replaceAll(" ", " ")+"
"; + String htmltext = Colors.mircColorParser(entext); Spanned colortext = Html2.fromHtml(htmltext); canvas = new SpannableString(prefix + timestamp + nick); @@ -257,7 +259,15 @@ public class Message canvas.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (hasColor() && settings.showColors()) { - canvas.setSpan(new ForegroundColorSpan(color), 0, canvas.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + // Only apply the foreground color on areas that don't already have a foreground color. + ForegroundColorSpan[] spans = canvas.getSpans(0, canvas.length(), ForegroundColorSpan.class); + int start = 0; + for (int i = 0; i < spans.length; i++) { + canvas.getSpanStart(spans[i]); + canvas.setSpan(new ForegroundColorSpan(color), start, canvas.getSpanStart(spans[i]), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + start = canvas.getSpanEnd(spans[i]); + } + canvas.setSpan(new ForegroundColorSpan(color), start, canvas.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } diff --git a/application/src/org/yaaic/utils/Colors.java b/application/src/org/yaaic/utils/Colors.java index 9527ab3..5d553a4 100644 --- a/application/src/org/yaaic/utils/Colors.java +++ b/application/src/org/yaaic/utils/Colors.java @@ -3,6 +3,8 @@ package org.yaaic.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; +import android.util.Log; + public class Colors { /* @@ -80,6 +82,7 @@ public class Colors { } // Remove left over codes + Log.d("html", removeStyleAndColors(sb.toString())); return removeStyleAndColors(sb.toString()); } diff --git a/application/src/org/yaaic/utils/Html2.java b/application/src/org/yaaic/utils/Html2.java index 174a597..0443418 100644 --- a/application/src/org/yaaic/utils/Html2.java +++ b/application/src/org/yaaic/utils/Html2.java @@ -59,6 +59,7 @@ import android.text.style.TextAppearanceSpan; import android.text.style.TypefaceSpan; import android.text.style.URLSpan; import android.text.style.UnderlineSpan; +import android.util.Log; /** * This class processes HTML strings into displayable styled text. @@ -90,7 +91,7 @@ public class Html2 { */ public static interface TagHandler { /** - * This method will be called whenn the HTML parser encounters + * This method will be called when the HTML parser encounters * a tag that it does not know how to interpret. */ public void handleTag(boolean opening, String tag, @@ -467,6 +468,7 @@ class HtmlToSpannedConverter implements ContentHandler { } private void handleStartTag(String tag, Attributes attributes) { + Log.d("colors", "handleStartTag: "+tag); if (tag.equalsIgnoreCase("br")) { // We don't need to handle this. TagSoup will ensure that there's a
for each
// so we can safely emite the linebreaks when we handle the close tag. @@ -518,6 +520,7 @@ class HtmlToSpannedConverter implements ContentHandler { } private void handleEndTag(String tag) { + Log.d("colors", "handleEndTag: "+tag); if (tag.equalsIgnoreCase("br")) { handleBr(mSpannableStringBuilder); } else if (tag.equalsIgnoreCase("p")) {