1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-10 04:02:27 +00:00
plantuml/src/net/sourceforge/plantuml/openiconic/OpenIcon.java

152 lines
4.6 KiB
Java
Raw Normal View History

2015-04-07 18:26:58 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2015-04-07 18:26:58 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
2017-03-15 19:13:31 +00:00
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2015-04-07 18:26:58 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
2015-04-07 18:26:58 +00:00
*
*/
package net.sourceforge.plantuml.openiconic;
2022-02-17 18:27:32 +00:00
import net.sourceforge.plantuml.awt.geom.Dimension2D;
2015-04-07 18:26:58 +00:00
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.plantuml.Dimension2DDouble;
2015-06-07 10:23:10 +00:00
import net.sourceforge.plantuml.graphic.AbstractTextBlock;
2015-04-07 18:26:58 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.log.Logger;
2015-04-07 18:26:58 +00:00
import net.sourceforge.plantuml.openiconic.data.DummyIcon;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2015-04-07 18:26:58 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2021-05-23 15:35:13 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColorAutomaticLegacy;
import net.sourceforge.plantuml.ugraphic.color.HColorSimple;
2015-04-07 18:26:58 +00:00
public class OpenIcon {
private SvgPath svgPath;
2021-05-14 08:42:57 +00:00
private List<String> rawData = new ArrayList<>();
2015-04-07 18:26:58 +00:00
private final String id;
public static OpenIcon retrieve(String name) {
2017-04-05 17:37:42 +00:00
final InputStream is = getResource(name);
2015-04-07 18:26:58 +00:00
if (is == null) {
return null;
}
try {
return new OpenIcon(is, name);
} catch (IOException e) {
Logger.error(e);
2015-04-07 18:26:58 +00:00
return null;
}
}
OpenIcon(String name) throws IOException {
2017-04-05 17:37:42 +00:00
this(getResource(name), name);
2015-04-07 18:26:58 +00:00
}
2017-04-05 17:37:42 +00:00
private static InputStream getResource(String name) {
2015-04-07 18:26:58 +00:00
// System.err.println("OPENING " + name);
return DummyIcon.class.getResourceAsStream(name + ".svg");
}
private OpenIcon(InputStream is, String id) throws IOException {
this.id = id;
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String s = null;
while ((s = br.readLine()) != null) {
rawData.add(s);
if (s.contains("<path")) {
final int x1 = s.indexOf('"');
final int x2 = s.indexOf('"', x1 + 1);
svgPath = new SvgPath(s.substring(x1 + 1, x2));
}
2015-04-07 18:26:58 +00:00
}
}
if (rawData.size() != 3 && rawData.size() != 4) {
throw new IllegalStateException();
}
}
2020-05-30 15:20:23 +00:00
void saveCopy(SFile fnew) throws IOException {
try(PrintWriter pw = fnew.createPrintWriter()) {
pw.println(rawData.get(0));
pw.println(svgPath.toSvg());
pw.println(rawData.get(rawData.size() - 1));
}
2015-04-07 18:26:58 +00:00
}
private Dimension2D getDimension(double factor) {
final String width = getNumber(rawData.get(0), "width");
final String height = getNumber(rawData.get(0), "height");
return new Dimension2DDouble(Integer.parseInt(width) * factor, Integer.parseInt(height) * factor);
}
private String getNumber(String s, String arg) {
int x1 = s.indexOf(arg);
if (x1 == -1) {
throw new IllegalArgumentException();
}
x1 = s.indexOf("\"", x1);
if (x1 == -1) {
throw new IllegalArgumentException();
}
final int x2 = s.indexOf("\"", x1 + 1);
if (x2 == -1) {
throw new IllegalArgumentException();
}
return s.substring(x1 + 1, x2);
}
2020-03-18 10:50:02 +00:00
public TextBlock asTextBlock(final HColor color, final double factor) {
2015-06-07 10:23:10 +00:00
return new AbstractTextBlock() {
2015-04-07 18:26:58 +00:00
public void drawU(UGraphic ug) {
2021-05-23 15:35:13 +00:00
HColor textColor = color;
if (textColor instanceof HColorAutomaticLegacy && ug.getParam().getBackcolor() != null) {
textColor = ((HColorSimple) ug.getParam().getBackcolor()).opposite();
}
svgPath.drawMe(ug.apply(textColor), factor);
2015-04-07 18:26:58 +00:00
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
return getDimension(factor);
}
};
}
}