1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-22 08:52:18 -05:00

MircColors/Smilies: Code formatting and documentation

This commit is contained in:
Sebastian Kaspari 2011-03-27 20:02:06 +02:00
parent 673598ad31
commit 591dc8f763
2 changed files with 49 additions and 16 deletions

View File

@ -34,11 +34,17 @@ import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
public class MircColors {
/**
* Class for parsing and handling mIRC colors in text messages.
*
* @author Thomas Martitz
*/
public abstract class MircColors
{
/*
* Colors from the "Classic" theme in mIRC.
*/
public static final int[] colors = {
private static final int[] colors = {
0xFFFFFF, // White
0x000000, // Black
0x00007F, // Blue (navy)
@ -64,8 +70,6 @@ public class MircColors {
private static final Pattern colorPattern = Pattern.compile("\\x03(\\d{1,2})(?:,(\\d{1,2}))?([^\\x03\\x0F]*)(\\x03|\\x0F)?");
private static final Pattern cleanupPattern = Pattern.compile("(?:\\x02|\\x1F|\\x1D|\\x0F|\\x16|\\x03(?:(?:\\d{1,2})(?:,\\d{1,2})?)?)");
private MircColors() {}
/**
* Converts a string with mIRC style and color codes to a SpannableString with
* all the style and color codes applied.
@ -73,7 +77,8 @@ public class MircColors {
* @param text A string with mIRC color codes.
* @return A SpannableString with all the styles applied.
*/
public static SpannableString toSpannable(SpannableString text) {
public static SpannableString toSpannable(SpannableString text)
{
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
replaceControlCodes(boldPattern.matcher(ssb), ssb, new StyleSpan(Typeface.BOLD));
replaceControlCodes(underlinePattern.matcher(ssb), ssb, new UnderlineSpan());
@ -91,15 +96,18 @@ public class MircColors {
}
Matcher m = colorPattern.matcher(ssb);
while (m.find()) {
int start = m.start();
int end = m.end();
Integer color = Integer.parseInt(m.group(1));
int codelength = m.group(1).length()+1;
if (color <= 15 && color >= 0) {
ssb.setSpan(new ForegroundColorSpan(colors[color] | 0xFF000000), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (m.group(2) != null) {
color = Integer.parseInt(m.group(2));
if (color <= 15 && color >= 0) {
@ -107,6 +115,7 @@ public class MircColors {
}
codelength = codelength + m.group(2).length() + 1;
}
ssb.delete(start, start+codelength);
// Reset the matcher with the modified text so that the ending color code character can be matched again.
m.reset(ssb);
@ -122,12 +131,22 @@ public class MircColors {
* @param text A string with mIRC color codes.
* @return A SpannableString with all the styles applied.
*/
public static SpannableString toSpannable(String text) {
public static SpannableString toSpannable(String text)
{
return toSpannable(new SpannableString(text));
}
private static void replaceControlCodes(Matcher m, SpannableStringBuilder ssb, CharacterStyle style) {
/**
* Replace the control codes
*
* @param m
* @param ssb
* @param style
*/
private static void replaceControlCodes(Matcher m, SpannableStringBuilder ssb, CharacterStyle style)
{
ArrayList<Integer> toremove = new ArrayList<Integer>();
while (m.find()) {
toremove.add(0, m.start());
// Remove the ending control character unless it's \x0F
@ -136,6 +155,7 @@ public class MircColors {
}
ssb.setSpan(style, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
for (Integer i : toremove) {
ssb.delete(i, i+1);
}
@ -147,7 +167,8 @@ public class MircColors {
* @param text A message with mirc colors and styles.
* @return The same message with all the colors and styles removed.
*/
public static String removeStyleAndColors(String text) {
public static String removeStyleAndColors(String text)
{
return cleanupPattern.matcher(text).replaceAll("");
}
@ -157,7 +178,8 @@ public class MircColors {
* @param text A message with mirc colors and styles.
* @return The same message with all the colors and styles removed.
*/
public static SpannableStringBuilder removeStyleAndColors(SpannableStringBuilder text) {
public static SpannableStringBuilder removeStyleAndColors(SpannableStringBuilder text)
{
ArrayList<int[]> toremove = new ArrayList<int[]>();
Matcher m = cleanupPattern.matcher(text);
while (m.find()) {
@ -168,4 +190,4 @@ public class MircColors {
}
return text;
}
}
}

View File

@ -33,9 +33,14 @@ import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.Log;
public class Smilies {
public static final HashMap<String, Integer> mappings = new HashMap<String, Integer>();
private Smilies() {}
/**
* Class for handling graphical smilies in text messages.
*
* @author Thomas Martitz
*/
public abstract class Smilies
{
private static final HashMap<String, Integer> mappings = new HashMap<String, Integer>();
/**
* Converts all smilies in a string to graphical smilies.
@ -43,7 +48,8 @@ public class Smilies {
* @param text A string with smilies.
* @return A SpannableString with graphical smilies.
*/
public static SpannableString toSpannable(SpannableString text, Context context) {
public static SpannableString toSpannable(SpannableString text, Context context)
{
mappings.put(">:o", R.drawable.smiley_yell);
mappings.put(">:-o", R.drawable.smiley_yell);
mappings.put("O:)", R.drawable.smiley_innocent);
@ -85,14 +91,17 @@ public class Smilies {
StringBuilder regex = new StringBuilder("(");
String[] smilies = mappings.keySet().toArray(new String[mappings.size()]);
for (int i = 0; i < smilies.length; i++) {
regex.append(Pattern.quote(smilies[i]));
regex.append("|");
}
regex.deleteCharAt(regex.length()-1);
regex.append(")");
Pattern smiliematcher = Pattern.compile(regex.toString());
Matcher m = smiliematcher.matcher(text);
while (m.find()) {
Log.d("Smilies", "SID: "+mappings.get(m.group(1)).intValue());
Log.d("Smilies", "OID: "+R.drawable.smiley_smile);
@ -101,6 +110,7 @@ public class Smilies {
ImageSpan span = new ImageSpan(smilie, ImageSpan.ALIGN_BOTTOM);
text.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return text;
}
@ -110,7 +120,8 @@ public class Smilies {
* @param text A string with smilies.
* @return A SpannableString with graphical smilies.
*/
public static SpannableString toSpannable(String text, Context context) {
public static SpannableString toSpannable(String text, Context context)
{
return toSpannable(new SpannableString(text), context);
}
}
}