Emoticons.java

  1package eu.siacs.conversations.utils;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5
  6public class Emoticons {
  7
  8	private static final UnicodeRange MISC_SYMBOLS_AND_PICTOGRAPHS = new UnicodeRange(0x1F300,0x1F5FF);
  9	private static final UnicodeRange SUPPLEMENTAL_SYMBOLS = new UnicodeRange(0x1F900,0x1F9FF);
 10	private static final UnicodeRange EMOTICONS = new UnicodeRange(0x1F600,0x1F64F);
 11	private static final UnicodeRange TRANSPORT_SYMBOLS = new UnicodeRange(0x1F680,0x1F6FF);
 12	private static final UnicodeRange MISC_SYMBOLS = new UnicodeRange(0x2600,0x26FF);
 13	private static final UnicodeRange DINGBATS = new UnicodeRange(0x2700,0x27BF);
 14	private static final UnicodeRange ENCLOSED_ALPHANUMERIC_SUPPLEMENT = new UnicodeRange(0x1F100,0x1F1FF);
 15	private static final UnicodeRange REGIONAL_INDICATORS = new UnicodeRange(0x1F1E6,0x1F1FF);
 16	private static final UnicodeRange GEOMETRIC_SHAPES = new UnicodeRange(0x25A0,0x25FF);
 17	private static final UnicodeRange LATIN_SUPPLEMENT = new UnicodeRange(0x80,0xFF);
 18	private static final UnicodeRange MISC_TECHNICAL = new UnicodeRange(0x2300,0x23FF);
 19	private static final UnicodeBlocks SYMBOLIZE = new UnicodeBlocks(GEOMETRIC_SHAPES, LATIN_SUPPLEMENT);
 20	private static final UnicodeBlocks EMOJIS = new UnicodeBlocks(
 21			MISC_SYMBOLS_AND_PICTOGRAPHS,
 22			SUPPLEMENTAL_SYMBOLS,
 23			EMOTICONS,
 24			TRANSPORT_SYMBOLS,
 25			MISC_SYMBOLS,
 26			DINGBATS,
 27			ENCLOSED_ALPHANUMERIC_SUPPLEMENT,
 28			MISC_TECHNICAL);
 29	private static final int ZWJ = 0x200D;
 30	private static final int VARIATION_16 = 0xFE0F;
 31	private static final UnicodeRange FITZPATRICK = new UnicodeRange(0x1F3FB,0x1F3FF);
 32
 33	private static List<Symbol> parse(String input) {
 34		List<Symbol> symbols = new ArrayList<>();
 35		Builder builder = new Builder();
 36		boolean needsFinalBuild = false;
 37		for (int cp, i = 0; i < input.length(); i += Character.charCount(cp)) {
 38			cp = input.codePointAt(i);
 39			if (builder.offer(cp)) {
 40				needsFinalBuild = true;
 41			} else {
 42				symbols.add(builder.build());
 43				builder = new Builder();
 44				if (builder.offer(cp)) {
 45					needsFinalBuild = true;
 46				}
 47			}
 48		}
 49		if (needsFinalBuild) {
 50			symbols.add(builder.build());
 51		}
 52		return symbols;
 53	}
 54
 55	public static boolean isEmoji(String input) {
 56		List<Symbol> symbols = parse(input);
 57		return symbols.size() == 1 && symbols.get(0) == Symbol.EMOJI;
 58	}
 59
 60	public static boolean isOnlyEmoji(String input) {
 61		List<Symbol> symbols = parse(input);
 62		for(Symbol symbol : symbols) {
 63			if (symbol == Symbol.NON_EMOJI) {
 64				return false;
 65			}
 66		}
 67		return symbols.size() > 0;
 68	}
 69
 70	private enum Symbol {
 71		EMOJI, NON_EMOJI
 72	}
 73
 74
 75	private static class Builder {
 76		private final List<Integer> codepoints = new ArrayList<>();
 77
 78
 79		public boolean offer(int codepoint) {
 80			boolean add = false;
 81			if (this.codepoints.size() == 0) {
 82				if (SYMBOLIZE.contains(codepoint)) {
 83					add = true;
 84				} else if (REGIONAL_INDICATORS.contains(codepoint)) {
 85					add = true;
 86				} else if (EMOJIS.contains(codepoint) && !FITZPATRICK.contains(codepoint) && codepoint != ZWJ) {
 87					add = true;
 88				}
 89			} else {
 90				int previous = codepoints.get(codepoints.size() -1);
 91				if (SYMBOLIZE.contains(previous)) {
 92					add = codepoint == VARIATION_16;
 93				} else if (REGIONAL_INDICATORS.contains(previous) && REGIONAL_INDICATORS.contains(codepoint)) {
 94					add = codepoints.size() == 1;
 95				} else if (previous == VARIATION_16) {
 96					add = isMerger(codepoint);
 97				} else if (FITZPATRICK.contains(previous)) {
 98					add = codepoint == ZWJ || EMOJIS.contains(codepoint);
 99				} else if (ZWJ == previous) {
100					add = EMOJIS.contains(codepoint) || FITZPATRICK.contains(codepoint);
101				} else if (isMerger(codepoint)) {
102					add = true;
103				} else if (codepoint == VARIATION_16 && EMOJIS.contains(previous)) {
104					add = true;
105				}
106			}
107			if (add) {
108				codepoints.add(codepoint);
109				return true;
110			} else {
111				return false;
112			}
113		}
114
115		private static boolean isMerger(int codepoint) {
116			return codepoint == ZWJ || FITZPATRICK.contains(codepoint);
117		}
118
119		public Symbol build() {
120			if (codepoints.size() > 0 && SYMBOLIZE.contains(codepoints.get(codepoints.size() - 1))) {
121				return Symbol.NON_EMOJI;
122			}
123			return codepoints.size() == 0 ? Symbol.NON_EMOJI : Symbol.EMOJI;
124		}
125	}
126
127	public static class UnicodeBlocks {
128		final UnicodeRange[] ranges;
129
130		public UnicodeBlocks(UnicodeRange... ranges) {
131			this.ranges = ranges;
132		}
133
134		public boolean contains(int codepoint) {
135			for(UnicodeRange range : ranges) {
136				if (range.contains(codepoint)) {
137					return true;
138				}
139			}
140			return false;
141		}
142	}
143
144
145	public static class UnicodeRange {
146
147		private final int lower;
148		private final int upper;
149
150		UnicodeRange(int lower, int upper) {
151			this.lower = lower;
152			this.upper = upper;
153		}
154
155		public boolean contains(int codePoint) {
156			return codePoint >= lower && codePoint <= upper;
157		}
158	}
159}