1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-17 15:42:44 +00:00
plantuml/src/net/sourceforge/plantuml/emoji/Emoji.java

101 lines
2.4 KiB
Java
Raw Normal View History

2021-12-07 18:15:43 +00:00
package net.sourceforge.plantuml.emoji;
2021-12-02 17:39:20 +00:00
import java.io.BufferedReader;
import java.io.IOException;
2021-12-06 18:58:08 +00:00
import java.io.InputStream;
2021-12-02 17:39:20 +00:00
import java.io.InputStreamReader;
import java.util.ArrayList;
2021-12-06 18:58:08 +00:00
import java.util.Collections;
2021-12-02 17:39:20 +00:00
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2021-12-06 18:58:08 +00:00
import java.util.TreeMap;
2021-12-02 17:39:20 +00:00
2021-12-07 18:15:43 +00:00
import net.sourceforge.plantuml.emoji.data.Dummy;
import net.sourceforge.plantuml.log.Logger;
2021-12-02 17:39:20 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.color.HColor;
2021-12-07 18:15:43 +00:00
// Emojji from https://twemoji.twitter.com/
2021-12-06 18:58:08 +00:00
// Shorcut from https://api.github.com/emojis
2021-12-07 18:15:43 +00:00
public class Emoji {
private final static Map<String, Emoji> ALL = new HashMap<>();
2021-12-02 17:39:20 +00:00
static {
2021-12-08 18:29:29 +00:00
final InputStream is = Dummy.class.getResourceAsStream("emoji.txt");
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
2021-12-06 18:58:08 +00:00
String s = null;
while ((s = br.readLine()) != null) {
2021-12-07 18:15:43 +00:00
new Emoji(s);
2021-12-06 18:58:08 +00:00
}
} catch (IOException e) {
Logger.error(e);
2021-12-06 18:58:08 +00:00
}
}
2021-12-07 18:15:43 +00:00
public static Map<String, Emoji> getAll() {
2021-12-06 18:58:08 +00:00
return Collections.unmodifiableMap(new TreeMap<>(ALL));
}
2022-07-27 13:51:02 +00:00
private SvgNanoParser nano;
2021-12-02 17:39:20 +00:00
private final String unicode;
2021-12-06 18:58:08 +00:00
private final String shortcut;
2021-12-02 17:39:20 +00:00
2021-12-07 18:15:43 +00:00
private Emoji(String unicode) {
2021-12-06 18:58:08 +00:00
final int x = unicode.indexOf(';');
if (x == -1) {
this.shortcut = null;
} else {
this.shortcut = unicode.substring(x + 1);
ALL.put(this.shortcut, this);
unicode = unicode.substring(0, x);
}
2021-12-02 17:39:20 +00:00
this.unicode = unicode;
ALL.put(unicode, this);
}
public static String pattern() {
2021-12-07 18:15:43 +00:00
final StringBuilder sb = new StringBuilder("\\<(#\\w+)?:(");
2021-12-02 17:39:20 +00:00
for (String s : ALL.keySet()) {
if (sb.toString().endsWith("(") == false)
sb.append("|");
sb.append(s);
}
2021-12-07 18:15:43 +00:00
sb.append("):\\>");
2021-12-02 17:39:20 +00:00
return sb.toString();
}
2021-12-07 18:15:43 +00:00
public static Emoji retrieve(String name) {
2021-12-02 17:39:20 +00:00
return ALL.get(name.toLowerCase());
}
private synchronized void loadIfNeed() throws IOException {
2022-07-27 13:51:02 +00:00
if (nano != null)
2021-12-02 17:39:20 +00:00
return;
2021-12-07 18:15:43 +00:00
2022-07-27 13:51:02 +00:00
final List<String> data = new ArrayList<String>();
2021-12-02 17:39:20 +00:00
try (BufferedReader br = new BufferedReader(
new InputStreamReader(Dummy.class.getResourceAsStream(unicode + ".svg")))) {
2021-12-07 18:15:43 +00:00
final String singleLine = br.readLine();
2022-07-27 13:51:02 +00:00
data.add(singleLine);
2021-12-02 17:39:20 +00:00
}
2022-07-27 13:51:02 +00:00
this.nano = new SvgNanoParser(data, false);
2021-12-02 17:39:20 +00:00
}
2021-12-06 18:58:08 +00:00
public void drawU(UGraphic ug, double scale, HColor colorForMonochrome) {
2021-12-02 17:39:20 +00:00
try {
loadIfNeed();
} catch (IOException e) {
Logger.error(e);
2021-12-02 17:39:20 +00:00
}
2022-07-27 13:51:02 +00:00
nano.drawU(ug, scale, colorForMonochrome);
2021-12-06 18:58:08 +00:00
}
2021-12-02 17:39:20 +00:00
2021-12-06 18:58:08 +00:00
public String getShortcut() {
return shortcut;
2021-12-02 17:39:20 +00:00
}
}