1package eu.siacs.conversations.utils;
2
3import android.util.Log;
4
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.List;
8
9import eu.siacs.conversations.Config;
10
11public class Emoticons {
12
13 private static final UnicodeRange MISC_SYMBOLS_AND_PICTOGRAPHS = new UnicodeRange(0x1F300,0x1F5FF);
14 private static final UnicodeRange SUPPLEMENTAL_SYMBOLS = new UnicodeRange(0x1F900,0x1F9FF);
15 private static final UnicodeRange EMOTICONS = new UnicodeRange(0x1F600,0x1F64F);
16 private static final UnicodeRange TRANSPORT_SYMBOLS = new UnicodeRange(0x1F680,0x1F6FF);
17 private static final UnicodeRange MISC_SYMBOLS = new UnicodeRange(0x2600,0x26FF);
18 private static final UnicodeRange DINGBATS = new UnicodeRange(0x2700,0x27BF);
19 private static final UnicodeRange ENCLOSED_ALPHANUMERIC_SUPPLEMENT = new UnicodeRange(0x1F100,0x1F1FF);
20 private static final UnicodeRange ENCLOSED_IDEOGRAPHIC_SUPPLEMENT = new UnicodeRange(0x1F200,0x1F2FF);
21 private static final UnicodeRange REGIONAL_INDICATORS = new UnicodeRange(0x1F1E6,0x1F1FF);
22 private static final UnicodeRange GEOMETRIC_SHAPES = new UnicodeRange(0x25A0,0x25FF);
23 private static final UnicodeRange LATIN_SUPPLEMENT = new UnicodeRange(0x80,0xFF);
24 private static final UnicodeRange MISC_TECHNICAL = new UnicodeRange(0x2300,0x23FF);
25 private static final UnicodeList CYK_SYMBOLS_AND_PUNCTUATION = new UnicodeList(0x3030,0x303D);
26 private static final UnicodeList LETTERLIKE_SYMBOLS = new UnicodeList(0x2122,0x2139);
27
28 private static final UnicodeBlocks KEYCAP_COMBINEABLE = new UnicodeBlocks(new UnicodeList(0x23),new UnicodeList(0x2A),new UnicodeRange(0x30,0x39));
29
30 private static final UnicodeBlocks SYMBOLIZE = new UnicodeBlocks(
31 GEOMETRIC_SHAPES,
32 LATIN_SUPPLEMENT,
33 CYK_SYMBOLS_AND_PUNCTUATION,
34 LETTERLIKE_SYMBOLS,
35 KEYCAP_COMBINEABLE);
36 private static final UnicodeBlocks EMOJIS = new UnicodeBlocks(
37 MISC_SYMBOLS_AND_PICTOGRAPHS,
38 SUPPLEMENTAL_SYMBOLS,
39 EMOTICONS,
40 TRANSPORT_SYMBOLS,
41 MISC_SYMBOLS,
42 DINGBATS,
43 ENCLOSED_ALPHANUMERIC_SUPPLEMENT,
44 ENCLOSED_IDEOGRAPHIC_SUPPLEMENT,
45 MISC_TECHNICAL);
46 private static final int ZWJ = 0x200D;
47 private static final int VARIATION_16 = 0xFE0F;
48 private static final int COMBINING_ENCLOSING_KEYCAP = 0x20E3;
49 private static final UnicodeRange FITZPATRICK = new UnicodeRange(0x1F3FB,0x1F3FF);
50
51 private static List<Symbol> parse(String input) {
52 List<Symbol> symbols = new ArrayList<>();
53 Builder builder = new Builder();
54 boolean needsFinalBuild = false;
55 for (int cp, i = 0; i < input.length(); i += Character.charCount(cp)) {
56 cp = input.codePointAt(i);
57 if (builder.offer(cp)) {
58 needsFinalBuild = true;
59 } else {
60 symbols.add(builder.build());
61 builder = new Builder();
62 if (builder.offer(cp)) {
63 needsFinalBuild = true;
64 }
65 }
66 }
67 if (needsFinalBuild) {
68 symbols.add(builder.build());
69 }
70 return symbols;
71 }
72
73 public static boolean isEmoji(String input) {
74 List<Symbol> symbols = parse(input);
75 return symbols.size() == 1 && symbols.get(0) == Symbol.EMOJI;
76 }
77
78 public static boolean isOnlyEmoji(String input) {
79 List<Symbol> symbols = parse(input);
80 for(Symbol symbol : symbols) {
81 if (symbol == Symbol.NON_EMOJI) {
82 return false;
83 }
84 }
85 return symbols.size() > 0;
86 }
87
88 private enum Symbol {
89 EMOJI, NON_EMOJI
90 }
91
92
93 private static class Builder {
94 private final List<Integer> codepoints = new ArrayList<>();
95
96
97 public boolean offer(int codepoint) {
98 boolean add = false;
99 if (this.codepoints.size() == 0) {
100 if (SYMBOLIZE.contains(codepoint)) {
101 add = true;
102 } else if (REGIONAL_INDICATORS.contains(codepoint)) {
103 add = true;
104 } else if (EMOJIS.contains(codepoint) && !FITZPATRICK.contains(codepoint) && codepoint != ZWJ) {
105 add = true;
106 }
107 } else {
108 int previous = codepoints.get(codepoints.size() -1);
109 if (COMBINING_ENCLOSING_KEYCAP == codepoint) {
110 add = KEYCAP_COMBINEABLE.contains(previous) || previous == VARIATION_16;
111 } else if (SYMBOLIZE.contains(previous)) {
112 add = codepoint == VARIATION_16;
113 } else if (REGIONAL_INDICATORS.contains(previous) && REGIONAL_INDICATORS.contains(codepoint)) {
114 add = codepoints.size() == 1;
115 } else if (previous == VARIATION_16) {
116 add = isMerger(codepoint);
117 } else if (FITZPATRICK.contains(previous)) {
118 add = codepoint == ZWJ;
119 } else if (ZWJ == previous) {
120 add = EMOJIS.contains(codepoint);
121 } else if (isMerger(codepoint)) {
122 add = true;
123 } else if (codepoint == VARIATION_16 && EMOJIS.contains(previous)) {
124 add = true;
125 }
126 }
127 Log.d(Config.LOGTAG,"code point "+String.format("%H",codepoint)+" added="+add);
128 if (add) {
129 codepoints.add(codepoint);
130 return true;
131 } else {
132 return false;
133 }
134 }
135
136 private static boolean isMerger(int codepoint) {
137 return codepoint == ZWJ || FITZPATRICK.contains(codepoint);
138 }
139
140 public Symbol build() {
141 if (codepoints.size() > 0 && SYMBOLIZE.contains(codepoints.get(codepoints.size() - 1))) {
142 return Symbol.NON_EMOJI;
143 } else if (codepoints.size() > 1 && KEYCAP_COMBINEABLE.contains(codepoints.get(0)) && codepoints.get(codepoints.size() - 1) != COMBINING_ENCLOSING_KEYCAP) {
144 return Symbol.NON_EMOJI;
145 }
146 return codepoints.size() == 0 ? Symbol.NON_EMOJI : Symbol.EMOJI;
147 }
148 }
149
150 public static class UnicodeBlocks implements UnicodeSet {
151 final UnicodeSet[] unicodeSets;
152
153 public UnicodeBlocks(UnicodeSet... sets) {
154 this.unicodeSets = sets;
155 }
156
157 @Override
158 public boolean contains(int codepoint) {
159 for(UnicodeSet unicodeSet : unicodeSets) {
160 if (unicodeSet.contains(codepoint)) {
161 return true;
162 }
163 }
164 return false;
165 }
166 }
167
168 public interface UnicodeSet {
169 boolean contains(int codepoint);
170 }
171
172 public static class UnicodeList implements UnicodeSet {
173
174 private final List<Integer> list;
175
176 public UnicodeList(Integer... codes) {
177 this.list = Arrays.asList(codes);
178 }
179
180 @Override
181 public boolean contains(int codepoint) {
182 return this.list.contains(codepoint);
183 }
184 }
185
186
187 public static class UnicodeRange implements UnicodeSet {
188
189 private final int lower;
190 private final int upper;
191
192 UnicodeRange(int lower, int upper) {
193 this.lower = lower;
194 this.upper = upper;
195 }
196
197 public boolean contains(int codePoint) {
198 return codePoint >= lower && codePoint <= upper;
199 }
200 }
201}