1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-11 04:32:26 +00:00
plantuml/src/net/sourceforge/plantuml/emoji/Emoji.java

372 lines
11 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
2022-01-07 18:04:36 +00:00
import java.awt.Color;
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
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2021-12-07 18:15:43 +00:00
import net.sourceforge.plantuml.emoji.data.Dummy;
2021-12-02 17:39:20 +00:00
import net.sourceforge.plantuml.openiconic.SvgPath;
import net.sourceforge.plantuml.ugraphic.UEllipse;
import net.sourceforge.plantuml.ugraphic.UGraphic;
2021-12-06 18:58:08 +00:00
import net.sourceforge.plantuml.ugraphic.UStroke;
2021-12-02 17:39:20 +00:00
import net.sourceforge.plantuml.ugraphic.UTranslate;
2022-01-07 18:04:36 +00:00
import net.sourceforge.plantuml.ugraphic.color.ColorChangerMonochrome;
2021-12-02 17:39:20 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2021-12-06 18:58:08 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColorNone;
2021-12-02 17:39:20 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColorSet;
2021-12-06 18:58:08 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColorSimple;
2021-12-02 17:39:20 +00:00
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 {
2021-12-02 17:39:20 +00:00
2021-12-07 18:15:43 +00:00
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) {
e.printStackTrace();
}
}
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));
}
2021-12-02 17:39:20 +00:00
private final List<String> data = new ArrayList<>();
2022-01-07 18:04:36 +00:00
private int minGray = 999;
private int maxGray = -1;
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 String extractData(String name, String s) {
final Pattern p = Pattern.compile(name + "=\"([^\"]+)\"");
final Matcher m = p.matcher(s);
if (m.find())
return m.group(1);
return null;
}
private synchronized void loadIfNeed() throws IOException {
if (data.size() > 0)
return;
2021-12-07 18:15:43 +00:00
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();
final Pattern p = Pattern.compile("\\<[^<>]+\\>");
final Matcher m = p.matcher(singleLine);
while (m.find()) {
final String s = m.group(0);
if (s.contains("<path") || s.contains("<g ") || s.contains("<g>") || s.contains("</g>")
|| s.contains("<circle ") || s.contains("<ellipse "))
data.add(s);
else
System.err.println("???=" + s);
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) {
e.printStackTrace();
}
2021-12-06 18:58:08 +00:00
UGraphicWithScale ugs = new UGraphicWithScale(ug, scale);
2022-01-07 18:04:36 +00:00
synchronized (this) {
if (colorForMonochrome != null && maxGray == -1)
computeMinMaxGray();
}
2021-12-06 18:58:08 +00:00
final List<UGraphicWithScale> stack = new ArrayList<>();
2021-12-02 17:39:20 +00:00
for (String s : data) {
2021-12-06 18:58:08 +00:00
if (s.contains("<path ")) {
drawPath(ugs, s, colorForMonochrome);
} else if (s.contains("</g>")) {
ugs = stack.remove(0);
} else if (s.contains("<g>")) {
stack.add(0, ugs);
2021-12-02 17:39:20 +00:00
} else if (s.contains("<g ")) {
2021-12-06 18:58:08 +00:00
stack.add(0, ugs);
ugs = applyFill(ugs, s, colorForMonochrome);
ugs = applyTransform(ugs, s);
2021-12-02 17:39:20 +00:00
2021-12-06 18:58:08 +00:00
} else if (s.contains("<circle ")) {
drawCircle(ugs, s, colorForMonochrome);
} else if (s.contains("<ellipse ")) {
drawEllipse(ugs, s, colorForMonochrome);
2021-12-02 17:39:20 +00:00
} else {
2021-12-07 18:15:43 +00:00
System.err.println("**?=" + s);
2021-12-02 17:39:20 +00:00
}
}
2021-12-06 18:58:08 +00:00
}
2022-01-07 18:04:36 +00:00
private void computeMinMaxGray() {
for (String s : data) {
2022-01-07 18:12:07 +00:00
if (s.contains("<path ") || s.contains("<g ") || s.contains("<circle ") || s.contains("<ellipse ")) {
final HColor color = justExtractColor(s);
if (color != null) {
final int gray = getGray(color);
minGray = Math.min(minGray, gray);
maxGray = Math.max(maxGray, gray);
}
2022-01-07 18:04:36 +00:00
} else {
// Nothing
}
}
}
private int getGray(HColor col) {
final Color tmp = new ColorChangerMonochrome().getChangedColor(col);
return tmp.getGreen();
}
2021-12-06 18:58:08 +00:00
private UGraphicWithScale applyFill(UGraphicWithScale ugs, String s, HColor colorForMonochrome) {
final String fillString = extractData("fill", s);
if (fillString == null)
return ugs;
if (fillString.equals("none")) {
final String strokeString = extractData("stroke", s);
if (strokeString == null)
2021-12-07 18:15:43 +00:00
return ugs;
2021-12-06 18:58:08 +00:00
ugs = ugs.apply(new HColorNone().bg());
final HColor stroke = getTrueColor(strokeString, colorForMonochrome);
ugs = ugs.apply(stroke);
final String strokeWidth = extractData("stroke-width", s);
if (strokeWidth != null) {
ugs = ugs.apply(new UStroke(Double.parseDouble(strokeWidth)));
}
} else {
final HColor fill = getTrueColor(fillString, colorForMonochrome);
ugs = ugs.apply(fill).apply(fill.bg());
}
return ugs;
}
2022-01-07 18:04:36 +00:00
private HColor justExtractColor(String s) {
final String fillString = extractData("fill", s);
if (fillString == null)
return null;
if (fillString.equals("none")) {
final String strokeString = extractData("stroke", s);
if (strokeString == null)
return null;
final HColor stroke = getTrueColor(strokeString, null);
return stroke;
} else {
final HColor fill = getTrueColor(fillString, null);
return fill;
}
}
2021-12-06 18:58:08 +00:00
private HColor getTrueColor(String code, HColor colorForMonochrome) {
final HColorSimple result = (HColorSimple) HColorSet.instance().getColorOrWhite(code);
if (colorForMonochrome == null)
return result;
2022-01-19 21:35:53 +00:00
final HColorSimple color = (HColorSimple) colorForMonochrome;
if (color.isGray())
return result.asMonochrome();
return result.asMonochrome(color, this.minGray, this.maxGray);
2021-12-06 18:58:08 +00:00
}
2021-12-07 18:15:43 +00:00
private void drawCircle(UGraphicWithScale ugs, String s, HColor colorForMonochrome) {
2021-12-06 18:58:08 +00:00
ugs = applyFill(ugs, s, colorForMonochrome);
ugs = applyTransform(ugs, s);
2021-12-07 18:15:43 +00:00
final double scalex = ugs.getAffineTransform().getScaleX();
final double scaley = ugs.getAffineTransform().getScaleY();
2021-12-06 18:58:08 +00:00
2021-12-07 18:15:43 +00:00
final double deltax = ugs.getAffineTransform().getTranslateX();
final double deltay = ugs.getAffineTransform().getTranslateY();
2021-12-06 18:58:08 +00:00
final double cx = Double.parseDouble(extractData("cx", s)) * scalex;
final double cy = Double.parseDouble(extractData("cy", s)) * scaley;
final double rx = Double.parseDouble(extractData("r", s)) * scalex;
final double ry = Double.parseDouble(extractData("r", s)) * scaley;
2021-12-07 18:15:43 +00:00
final UTranslate translate = new UTranslate(deltax + cx - rx, deltay + cy - ry);
ugs.apply(translate).draw(new UEllipse(rx * 2, ry * 2));
2021-12-06 18:58:08 +00:00
}
2021-12-07 18:15:43 +00:00
private void drawEllipse(UGraphicWithScale ugs, String s, HColor colorForMonochrome) {
2021-12-06 18:58:08 +00:00
ugs = applyFill(ugs, s, colorForMonochrome);
2021-12-07 18:15:43 +00:00
ugs = applyTransform(ugs, s);
2021-12-06 18:58:08 +00:00
2021-12-07 18:15:43 +00:00
final double scalex = ugs.getAffineTransform().getScaleX();
final double scaley = ugs.getAffineTransform().getScaleY();
final double deltax = ugs.getAffineTransform().getTranslateX();
final double deltay = ugs.getAffineTransform().getTranslateY();
2021-12-06 18:58:08 +00:00
final double cx = Double.parseDouble(extractData("cx", s)) * scalex;
final double cy = Double.parseDouble(extractData("cy", s)) * scaley;
final double rx = Double.parseDouble(extractData("rx", s)) * scalex;
final double ry = Double.parseDouble(extractData("ry", s)) * scaley;
2021-12-07 18:15:43 +00:00
final UTranslate translate = new UTranslate(deltax + cx - rx, deltay + cy - ry);
ugs.apply(translate).draw(new UEllipse(rx * 2, ry * 2));
2021-12-06 18:58:08 +00:00
}
private void drawPath(UGraphicWithScale ugs, String s, HColor colorForMonochrome) {
s = s.replace("id=\"", "ID=\"");
ugs = applyFill(ugs, s, colorForMonochrome);
ugs = applyTransform(ugs, s);
final int x1 = s.indexOf("d=\"");
final int x2 = s.indexOf('"', x1 + 3);
final String tmp = s.substring(x1 + 3, x2);
final SvgPath svgPath = new SvgPath(tmp);
svgPath.drawMe(ugs.getUg(), ugs.getAffineTransform());
}
private UGraphicWithScale applyTransform(UGraphicWithScale ugs, String s) {
final String transform = extractData("transform", s);
if (transform == null)
return ugs;
if (transform.contains("rotate("))
return applyRotate(ugs, transform);
if (transform.contains("matrix("))
return applyMatrix(ugs, transform);
final double[] scale = getScale(transform);
final UTranslate translate = getTranslate(transform);
ugs = ugs.applyTranslate(translate.getDx(), translate.getDy());
return ugs.applyScale(scale[0], scale[1]);
}
private UGraphicWithScale applyMatrix(UGraphicWithScale ugs, final String transform) {
2021-12-07 18:15:43 +00:00
final Pattern p3 = Pattern.compile(
"matrix\\(([-.0-9]+)[ ,]+([-.0-9]+)[ ,]+([-.0-9]+)[ ,]+([-.0-9]+)[ ,]+([-.0-9]+)[ ,]+([-.0-9]+)\\)");
2021-12-06 18:58:08 +00:00
final Matcher m3 = p3.matcher(transform);
if (m3.find()) {
final double v1 = Double.parseDouble(m3.group(1));
final double v2 = Double.parseDouble(m3.group(2));
final double v3 = Double.parseDouble(m3.group(3));
final double v4 = Double.parseDouble(m3.group(4));
final double v5 = Double.parseDouble(m3.group(5));
final double v6 = Double.parseDouble(m3.group(6));
ugs = ugs.applyMatrix(v1, v2, v3, v4, v5, v6);
} else
System.err.println("WARNING: " + transform);
return ugs;
}
private UGraphicWithScale applyRotate(UGraphicWithScale ugs, final String transform) {
final Pattern p3 = Pattern.compile("rotate\\(([-.0-9]+)[ ,]+([-.0-9]+)[ ,]+([-.0-9]+)\\)");
final Matcher m3 = p3.matcher(transform);
if (m3.find()) {
final double angle = Double.parseDouble(m3.group(1));
final double x = Double.parseDouble(m3.group(2));
final double y = Double.parseDouble(m3.group(3));
ugs = ugs.applyRotate(angle, x, y);
} else
System.err.println("WARNING: " + transform);
return ugs;
}
private UTranslate getTranslate(String transform) {
2021-12-07 18:15:43 +00:00
double x = 0;
double y = 0;
2021-12-06 18:58:08 +00:00
final Pattern p3 = Pattern.compile("translate\\(([-.0-9]+)[ ,]+([-.0-9]+)\\)");
final Matcher m3 = p3.matcher(transform);
if (m3.find()) {
x = Double.parseDouble(m3.group(1));
y = Double.parseDouble(m3.group(2));
} else {
final Pattern p4 = Pattern.compile("translate\\(([-.0-9]+)\\)");
final Matcher m4 = p4.matcher(transform);
if (m4.find()) {
x = Double.parseDouble(m4.group(1));
y = Double.parseDouble(m4.group(1));
2021-12-07 18:15:43 +00:00
}
2021-12-06 18:58:08 +00:00
}
return new UTranslate(x, y);
}
private double[] getScale(String transform) {
final double scale[] = new double[] { 1, 1 };
2021-12-07 18:15:43 +00:00
final Pattern p1 = Pattern.compile("scale\\(([-.0-9]+)\\)");
2021-12-06 18:58:08 +00:00
final Matcher m1 = p1.matcher(transform);
if (m1.find()) {
scale[0] = Double.parseDouble(m1.group(1));
scale[1] = scale[0];
} else {
2021-12-07 18:15:43 +00:00
final Pattern p2 = Pattern.compile("scale\\(([-.0-9]+)[ ,]+([-.0-9]+)\\)");
2021-12-06 18:58:08 +00:00
final Matcher m2 = p2.matcher(transform);
if (m2.find()) {
scale[0] = Double.parseDouble(m2.group(1));
scale[1] = Double.parseDouble(m2.group(2));
}
}
return scale;
}
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
}
}