plantuml/src/net/sourceforge/plantuml/ugraphic/svg/UGraphicSvg.java

218 lines
7.1 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2010-11-15 20:35:36 +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
*
2010-11-15 20:35:36 +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
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-15 20:35:36 +00:00
* 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
*
*/
package net.sourceforge.plantuml.ugraphic.svg;
2017-06-05 11:27:21 +00:00
import java.awt.geom.Dimension2D;
2010-11-15 20:35:36 +00:00
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
2010-11-15 20:35:36 +00:00
import javax.xml.transform.TransformerException;
import net.sourceforge.plantuml.Pragma;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Url;
2021-10-02 17:54:37 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.posimo.DotPath;
2022-02-14 17:44:01 +00:00
import net.sourceforge.plantuml.svg.DarkStrategy;
2021-01-10 20:52:19 +00:00
import net.sourceforge.plantuml.svg.LengthAdjust;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.svg.SvgGraphics;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.AbstractCommonUGraphic;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.ugraphic.AbstractUGraphic;
import net.sourceforge.plantuml.ugraphic.ClipContainer;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UCenteredCharacter;
2016-07-25 19:25:28 +00:00
import net.sourceforge.plantuml.ugraphic.UComment;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.ugraphic.UEllipse;
2021-03-07 12:23:24 +00:00
import net.sourceforge.plantuml.ugraphic.UGroupType;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.ugraphic.UImage;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UImageSvg;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.ugraphic.ULine;
import net.sourceforge.plantuml.ugraphic.UPath;
2021-03-12 18:16:16 +00:00
import net.sourceforge.plantuml.ugraphic.UPixel;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.ugraphic.UPolygon;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.UText;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
2021-05-06 21:23:05 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColorGradient;
2010-11-15 20:35:36 +00:00
public class UGraphicSvg extends AbstractUGraphic<SvgGraphics> implements ClipContainer {
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
private final boolean textAsPath2;
2015-05-31 18:56:03 +00:00
private final String target;
private final Pragma pragma;
2013-12-10 19:36:50 +00:00
2018-06-12 20:50:45 +00:00
public double dpiFactor() {
return 1;
}
2013-12-10 19:36:50 +00:00
@Override
protected AbstractCommonUGraphic copyUGraphic() {
return new UGraphicSvg(this);
}
private UGraphicSvg(UGraphicSvg other) {
super(other);
this.textAsPath2 = other.textAsPath2;
2015-05-31 18:56:03 +00:00
this.target = other.target;
this.pragma = other.pragma;
2013-12-10 19:36:50 +00:00
register();
}
2021-05-06 21:23:05 +00:00
public UGraphicSvg(HColor defaultBackground, boolean svgDimensionStyle, Dimension2D minDim, ColorMapper colorMapper,
2020-12-06 21:43:09 +00:00
boolean textAsPath, double scale, String linkTarget, String hover, long seed, String preserveAspectRatio,
StringBounder stringBounder, LengthAdjust lengthAdjust, Pragma pragma) {
2022-02-14 17:44:01 +00:00
this(defaultBackground, minDim, colorMapper,
new SvgGraphics(colorMapper.toSvg(defaultBackground), svgDimensionStyle, minDim, scale, hover, seed,
preserveAspectRatio, lengthAdjust, DarkStrategy.IGNORE_DARK_COLOR, pragma),
textAsPath, linkTarget, stringBounder, pragma);
2021-05-06 21:23:05 +00:00
if (defaultBackground instanceof HColorGradient) {
final SvgGraphics svg = getGraphicObject();
svg.paintBackcolorGradient(colorMapper, (HColorGradient) defaultBackground);
}
2013-12-10 19:36:50 +00:00
}
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
@Override
protected boolean manageHiddenAutomatically() {
return false;
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
@Override
protected void beforeDraw() {
getGraphicObject().setHidden(getParam().isHidden());
}
@Override
protected void afterDraw() {
getGraphicObject().setHidden(false);
2010-11-15 20:35:36 +00:00
}
2021-05-06 21:23:05 +00:00
private UGraphicSvg(HColor defaultBackground, Dimension2D minDim, ColorMapper colorMapper, SvgGraphics svg,
boolean textAsPath, String linkTarget, StringBounder stringBounder, Pragma pragma) {
2021-10-02 17:54:37 +00:00
super(defaultBackground, colorMapper, stringBounder, svg);
2013-12-10 19:36:50 +00:00
this.textAsPath2 = textAsPath;
2015-05-31 18:56:03 +00:00
this.target = linkTarget;
this.pragma = pragma;
2013-12-10 19:36:50 +00:00
register();
}
private void register() {
2010-11-15 20:35:36 +00:00
registerDriver(URectangle.class, new DriverRectangleSvg(this));
2022-02-01 20:21:45 +00:00
if (textAsPath2)
registerDriver(UText.class, new DriverTextAsPathSvg(this));
2022-02-01 20:21:45 +00:00
else
2010-11-15 20:35:36 +00:00
registerDriver(UText.class, new DriverTextSvg(getStringBounder(), this));
2022-02-01 20:21:45 +00:00
2010-11-15 20:35:36 +00:00
registerDriver(ULine.class, new DriverLineSvg(this));
2021-03-12 18:16:16 +00:00
registerDriver(UPixel.class, new DriverPixelSvg());
2010-11-15 20:35:36 +00:00
registerDriver(UPolygon.class, new DriverPolygonSvg(this));
2015-04-07 18:18:37 +00:00
registerDriver(UEllipse.class, new DriverEllipseSvg(this));
registerDriver(UImage.class, new DriverImagePng(this));
2013-12-10 19:36:50 +00:00
registerDriver(UImageSvg.class, new DriverImageSvgSvg());
2010-11-15 20:35:36 +00:00
registerDriver(UPath.class, new DriverPathSvg(this));
2011-08-08 17:48:29 +00:00
registerDriver(DotPath.class, new DriverDotPathSvg());
2013-12-10 19:36:50 +00:00
registerDriver(UCenteredCharacter.class, new DriverCenteredCharacterSvg());
2010-11-15 20:35:36 +00:00
}
public SvgGraphics getSvgGraphics() {
return this.getGraphicObject();
}
@Override
public void writeToStream(OutputStream os, String metadata, int dpi) throws IOException {
2010-11-15 20:35:36 +00:00
try {
2022-02-01 20:21:45 +00:00
if (metadata != null)
2019-09-14 18:12:04 +00:00
getGraphicObject().addComment(metadata);
2022-02-01 20:21:45 +00:00
if (pragma.isDefine("svginteractive") && Boolean.valueOf(pragma.getValue("svginteractive"))) {
// For performance reasons and also because we want the entire graph DOM to be create so we can register
// the event handlers on them we will append to the end of the document
getGraphicObject().addStyle("onmouseinteractivefooter.css");
getGraphicObject().addScriptTag("https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@3.0/dist/svg.min.js");
getGraphicObject().addScript("onmouseinteractivefooter.js");
}
2010-11-15 20:35:36 +00:00
getGraphicObject().createXml(os);
} catch (TransformerException e) {
throw new IOException(e.toString());
}
}
2020-05-17 21:15:50 +00:00
@Override
public void startGroup(Map<UGroupType, String> typeIdents) {
getGraphicObject().startGroup(typeIdents);
}
2020-05-17 21:15:50 +00:00
@Override
public void closeGroup() {
getGraphicObject().closeGroup();
}
@Override
2013-12-10 19:36:50 +00:00
public void startUrl(Url url) {
2015-05-31 18:56:03 +00:00
getGraphicObject().openLink(url.getUrl(), url.getTooltip(), target);
2010-11-15 20:35:36 +00:00
}
2020-05-17 21:15:50 +00:00
@Override
public void closeUrl() {
2013-12-10 19:36:50 +00:00
getGraphicObject().closeLink();
2010-11-15 20:35:36 +00:00
}
2011-04-19 16:50:40 +00:00
2016-07-25 19:25:28 +00:00
@Override
protected void drawComment(UComment comment) {
getGraphicObject().addComment(comment.getComment());
}
2016-12-01 20:29:25 +00:00
@Override
public boolean matchesProperty(String propertyName) {
2022-02-01 20:21:45 +00:00
if (propertyName.equalsIgnoreCase("SVG"))
2016-12-01 20:29:25 +00:00
return true;
2022-02-01 20:21:45 +00:00
2016-12-01 20:29:25 +00:00
return super.matchesProperty(propertyName);
}
2013-12-10 19:36:50 +00:00
// @Override
// public String startHiddenGroup() {
// getGraphicObject().startHiddenGroup();
// return null;
// }
//
// @Override
// public String closeHiddenGroup() {
// getGraphicObject().closeHiddenGroup();
// return null;
// }
2010-11-15 20:35:36 +00:00
}