Skip to content

Commit

Permalink
Render colors correctly in topics and other foreground colored places.
Browse files Browse the repository at this point in the history
  • Loading branch information
liato authored and pocmo committed Mar 27, 2011
1 parent 59c70a7 commit 7c4abe0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
14 changes: 12 additions & 2 deletions application/src/org/yaaic/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ public SpannableString render(Context context)
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 <font> tag so we'll surround the html with <pre> tags.
String entext = "<pre>"+TextUtils.htmlEncode(text).replaceAll(" ", "&nbsp;")+"</pre>";
String htmltext = Colors.mircColorParser(entext);
Spanned colortext = Html2.fromHtml(htmltext);

canvas = new SpannableString(prefix + timestamp + nick);
Expand All @@ -257,7 +259,15 @@ public SpannableString render(Context context)
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);
}
}

Expand Down
3 changes: 3 additions & 0 deletions application/src/org/yaaic/utils/Colors.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;


public class Colors {
/*
Expand Down Expand Up @@ -80,6 +82,7 @@ public static String mircColorParser(String text) {
}

// Remove left over codes
Log.d("html", removeStyleAndColors(sb.toString()));
return removeStyleAndColors(sb.toString());
}

Expand Down
5 changes: 4 additions & 1 deletion application/src/org/yaaic/utils/Html2.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
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.
Expand Down Expand Up @@ -90,7 +91,7 @@ public static interface ImageGetter {
*/
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,
Expand Down Expand Up @@ -467,6 +468,7 @@ public Spanned convert() {
}

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 </br> for each <br>
// so we can safely emite the linebreaks when we handle the close tag.
Expand Down Expand Up @@ -518,6 +520,7 @@ private void handleStartTag(String tag, Attributes attributes) {
}

private void handleEndTag(String tag) {
Log.d("colors", "handleEndTag: "+tag);
if (tag.equalsIgnoreCase("br")) {
handleBr(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("p")) {
Expand Down

0 comments on commit 7c4abe0

Please sign in to comment.