1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-06 10:20:54 +00:00
plantuml/src/net/sourceforge/plantuml/creole/atom/AtomImg.java

216 lines
7.5 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
2013-12-10 19:36:50 +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
2022-08-17 17:34:24 +00:00
*
2013-12-10 19:36:50 +00:00
*
*/
2020-03-18 10:50:02 +00:00
package net.sourceforge.plantuml.creole.atom;
2013-12-10 19:36:50 +00:00
import static java.nio.charset.StandardCharsets.UTF_8;
2018-09-23 12:15:14 +00:00
import java.awt.Color;
2013-12-10 19:36:50 +00:00
import java.awt.image.BufferedImage;
import java.io.IOException;
import net.sourceforge.plantuml.FileSystem;
2017-12-11 21:02:10 +00:00
import net.sourceforge.plantuml.FileUtils;
2019-06-26 19:24:49 +00:00
import net.sourceforge.plantuml.Url;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.code.Base64Coder;
2020-11-21 17:33:24 +00:00
import net.sourceforge.plantuml.creole.legacy.AtomTextUtils;
2018-08-26 12:09:50 +00:00
import net.sourceforge.plantuml.flashcode.FlashCodeFactory;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.ImgValign;
import net.sourceforge.plantuml.graphic.StringBounder;
2016-08-25 20:45:37 +00:00
import net.sourceforge.plantuml.graphic.TileImageSvg;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.security.SImageIO;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SURL;
import net.sourceforge.plantuml.security.SecurityProfile;
import net.sourceforge.plantuml.security.SecurityUtils;
2020-06-07 10:03:18 +00:00
import net.sourceforge.plantuml.ugraphic.AffineTransformType;
import net.sourceforge.plantuml.ugraphic.PixelImage;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UFont;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UImage;
2019-03-29 22:14:07 +00:00
public class AtomImg extends AbstractAtom implements Atom {
2013-12-10 19:36:50 +00:00
2022-11-17 17:56:16 +00:00
public static final String DATA_IMAGE_PNG_BASE64 = "data:image/png;base64,";
2020-12-14 18:31:06 +00:00
private static final String DATA_IMAGE_SVG_BASE64 = "data:image/svg+xml;base64,";
2013-12-10 19:36:50 +00:00
private final BufferedImage image;
2016-04-04 19:05:10 +00:00
private final double scale;
2019-06-26 19:24:49 +00:00
private final Url url;
2020-02-18 21:24:31 +00:00
private final String rawFileName;
private AtomImg(BufferedImage image, double scale, Url url, String rawFileName) {
2013-12-10 19:36:50 +00:00
this.image = image;
2016-04-04 19:05:10 +00:00
this.scale = scale;
2019-06-26 19:24:49 +00:00
this.url = url;
2020-02-18 21:24:31 +00:00
this.rawFileName = rawFileName;
2013-12-10 19:36:50 +00:00
}
2018-08-26 12:09:50 +00:00
public static Atom createQrcode(String flash, double scale) {
BufferedImage im = null;
// :: comment when WASM
im = FlashCodeFactory.getFlashCodeUtils().exportFlashcode(flash, Color.BLACK, Color.WHITE);
2022-12-13 17:30:41 +00:00
if (im == null)
// ::done
2018-08-26 12:09:50 +00:00
im = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
2022-12-13 17:30:41 +00:00
2020-06-14 20:35:42 +00:00
return new AtomImg(
new UImage(new PixelImage(im, AffineTransformType.TYPE_NEAREST_NEIGHBOR)).scale(scale).getImage(1), 1,
null, null);
2018-08-26 12:09:50 +00:00
}
2019-06-26 19:24:49 +00:00
public static Atom create(String src, ImgValign valign, int vspace, double scale, Url url) {
2017-04-05 17:37:42 +00:00
final UFont font = UFont.monospaced(14);
2015-09-28 20:42:17 +00:00
final FontConfiguration fc = FontConfiguration.blackBlueTrue(font);
if (src.startsWith(DATA_IMAGE_PNG_BASE64)) {
final String data = src.substring(DATA_IMAGE_PNG_BASE64.length(), src.length());
try {
final byte bytes[] = Base64Coder.decode(data);
2020-04-19 16:04:39 +00:00
return buildRasterFromData(src, fc, bytes, scale, url);
2015-09-28 20:42:17 +00:00
} catch (Exception e) {
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("ERROR " + e.toString(), fc);
2015-09-28 20:42:17 +00:00
}
2020-12-14 18:31:06 +00:00
}
2015-09-28 20:42:17 +00:00
2020-12-14 18:31:06 +00:00
if (src.startsWith(DATA_IMAGE_SVG_BASE64)) {
final String data = src.substring(DATA_IMAGE_SVG_BASE64.length(), src.length());
try {
final byte bytes[] = Base64Coder.decode(data);
final String tmp = new String(bytes);
2022-06-06 12:36:58 +00:00
return new AtomImgSvg(new TileImageSvg(tmp, scale));
2020-12-14 18:31:06 +00:00
} catch (Exception e) {
return AtomTextUtils.createLegacy("ERROR " + e.toString(), fc);
}
2015-09-28 20:42:17 +00:00
}
2020-12-14 18:31:06 +00:00
2013-12-10 19:36:50 +00:00
try {
2019-04-21 20:40:01 +00:00
// Check if valid URL
if (src.startsWith("http:") || src.startsWith("https:")) {
2022-12-13 17:30:41 +00:00
if (src.endsWith(".svg"))
2020-05-30 15:20:23 +00:00
return buildSvgFromUrl(src, fc, SURL.create(src), scale, url);
2022-12-13 17:30:41 +00:00
2020-05-30 15:20:23 +00:00
return buildRasterFromUrl(src, fc, SURL.create(src), scale, url);
2019-04-21 20:40:01 +00:00
}
2020-05-30 15:20:23 +00:00
final SFile f = FileSystem.getInstance().getFile(src);
2013-12-10 19:36:50 +00:00
if (f.exists() == false) {
2022-12-13 17:30:41 +00:00
if (SecurityUtils.getSecurityProfile() == SecurityProfile.UNSECURE)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(File not found: " + f.getPrintablePath() + ")", fc);
2022-12-13 17:30:41 +00:00
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode)", fc);
2013-12-10 19:36:50 +00:00
}
if (f.getName().endsWith(".svg")) {
2020-05-30 15:20:23 +00:00
final String tmp = FileUtils.readSvg(f);
2022-12-13 17:30:41 +00:00
if (tmp == null)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode)", fc);
2022-12-13 17:30:41 +00:00
2022-06-06 12:36:58 +00:00
return new AtomImgSvg(new TileImageSvg(tmp, scale));
2013-12-10 19:36:50 +00:00
}
2020-05-30 15:20:23 +00:00
final BufferedImage read = f.readRasterImageFromFile();
2013-12-10 19:36:50 +00:00
if (read == null) {
2022-12-13 17:30:41 +00:00
if (SecurityUtils.getSecurityProfile() == SecurityProfile.UNSECURE)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode: " + f.getPrintablePath() + ")", fc);
2022-12-13 17:30:41 +00:00
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode)", fc);
2013-12-10 19:36:50 +00:00
}
2020-05-30 15:20:23 +00:00
return new AtomImg(f.readRasterImageFromFile(), scale, url, src);
2013-12-10 19:36:50 +00:00
} catch (IOException e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2022-12-13 17:30:41 +00:00
if (SecurityUtils.getSecurityProfile() == SecurityProfile.UNSECURE)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("ERROR " + e.toString(), fc);
2022-12-13 17:30:41 +00:00
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("ERROR", fc);
2013-12-10 19:36:50 +00:00
}
}
2020-04-19 16:04:39 +00:00
private static Atom buildRasterFromData(String source, final FontConfiguration fc, final byte[] data, double scale,
Url url) throws IOException {
final BufferedImage read = SImageIO.read(data);
2022-12-13 17:30:41 +00:00
if (read == null)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode: " + source + ")", fc);
2022-12-13 17:30:41 +00:00
2020-02-18 21:24:31 +00:00
return new AtomImg(read, scale, url, null);
2015-09-28 20:42:17 +00:00
}
2020-05-30 15:20:23 +00:00
private static Atom buildRasterFromUrl(String text, final FontConfiguration fc, SURL source, double scale, Url url)
2019-06-26 19:24:49 +00:00
throws IOException {
2022-12-13 17:30:41 +00:00
if (source == null)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode: " + text + ")", fc);
2022-12-13 17:30:41 +00:00
2020-05-30 15:20:23 +00:00
final BufferedImage read = source.readRasterImageFromURL();
2022-12-13 17:30:41 +00:00
if (read == null)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode: " + text + ")", fc);
2022-12-13 17:30:41 +00:00
2020-05-30 15:20:23 +00:00
return new AtomImg(read, scale, url, "http");
2017-12-11 21:02:10 +00:00
}
2020-05-30 15:20:23 +00:00
private static Atom buildSvgFromUrl(String text, final FontConfiguration fc, SURL source, double scale, Url url)
2020-04-19 16:04:39 +00:00
throws IOException {
2022-12-13 17:30:41 +00:00
if (source == null)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode SVG: " + text + ")", fc);
2022-12-13 17:30:41 +00:00
2020-06-14 20:35:42 +00:00
final byte[] read = source.getBytes();
2022-12-13 17:30:41 +00:00
if (read == null)
2020-11-21 17:33:24 +00:00
return AtomTextUtils.createLegacy("(Cannot decode SVG: " + text + ")", fc);
2022-12-13 17:30:41 +00:00
2022-06-06 12:36:58 +00:00
return new AtomImgSvg(new TileImageSvg(new String(read, UTF_8), scale));
2020-04-19 16:04:39 +00:00
}
2013-12-10 19:36:50 +00:00
// End
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
return new XDimension2D(image.getWidth() * scale, image.getHeight() * scale);
2013-12-10 19:36:50 +00:00
}
public double getStartingAltitude(StringBounder stringBounder) {
return 0;
}
public void drawU(UGraphic ug) {
2022-12-13 17:30:41 +00:00
if (url != null)
2019-06-26 19:24:49 +00:00
ug.startUrl(url);
2022-12-13 17:30:41 +00:00
2020-06-14 20:35:42 +00:00
ug.draw(new UImage(new PixelImage(image, AffineTransformType.TYPE_BILINEAR)).withRawFileName(rawFileName)
.scale(scale));
2022-12-13 17:30:41 +00:00
if (url != null)
2020-05-17 21:15:50 +00:00
ug.closeUrl();
2022-12-13 17:30:41 +00:00
2013-12-10 19:36:50 +00:00
}
}