1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-02 00:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/ugraphic/svg/UGraphicSvg.java

209 lines
6.9 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
2010-11-15 20:35:36 +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:
*
* 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 javax.xml.transform.TransformerException;
2016-08-25 20:45:37 +00:00
import net.sourceforge.plantuml.FileFormat;
2017-10-07 09:46:53 +00:00
import net.sourceforge.plantuml.TikzFontDistortion;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Url;
import net.sourceforge.plantuml.graphic.HtmlColorGradient;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.TextBlockUtils;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.posimo.DotPath;
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;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.ugraphic.ColorMapper;
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;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic2;
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;
import net.sourceforge.plantuml.ugraphic.UPolygon;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.UText;
2015-04-07 18:18:37 +00:00
public class UGraphicSvg extends AbstractUGraphic<SvgGraphics> implements ClipContainer, UGraphic2 {
2010-11-15 20:35:36 +00:00
private final StringBounder stringBounder;
2013-12-10 19:36:50 +00:00
private final boolean textAsPath2;
2015-05-31 18:56:03 +00:00
private final String target;
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.stringBounder = other.stringBounder;
this.textAsPath2 = other.textAsPath2;
2015-05-31 18:56:03 +00:00
this.target = other.target;
2013-12-10 19:36:50 +00:00
register();
}
2018-03-09 21:37:34 +00:00
public UGraphicSvg(boolean svgDimensionStyle, Dimension2D minDim, ColorMapper colorMapper, String backcolor,
2019-11-03 17:40:03 +00:00
boolean textAsPath, double scale, String linkTarget, String hover, long seed, String preserveAspectRatio) {
this(minDim, colorMapper, new SvgGraphics(svgDimensionStyle, minDim, backcolor, scale, hover, seed,
preserveAspectRatio), textAsPath, linkTarget);
2013-12-10 19:36:50 +00:00
}
2018-03-09 21:37:34 +00:00
public UGraphicSvg(boolean svgDimensionStyle, Dimension2D minDim, ColorMapper colorMapper, boolean textAsPath,
2019-11-03 17:40:03 +00:00
double scale, String linkTarget, String hover, long seed, String preserveAspectRatio) {
this(minDim, colorMapper, new SvgGraphics(svgDimensionStyle, minDim, scale, hover, seed, preserveAspectRatio),
textAsPath, linkTarget);
2013-12-10 19:36:50 +00:00
}
2018-03-09 21:37:34 +00:00
public UGraphicSvg(boolean svgDimensionStyle, Dimension2D minDim, ColorMapper mapper, HtmlColorGradient gr,
2019-11-03 17:40:03 +00:00
boolean textAsPath, double scale, String linkTarget, String hover, long seed, String preserveAspectRatio) {
this(minDim, mapper, new SvgGraphics(svgDimensionStyle, minDim, scale, hover, seed, preserveAspectRatio),
textAsPath, linkTarget);
2013-12-10 19:36:50 +00:00
final SvgGraphics svg = getGraphicObject();
svg.paintBackcolorGradient(mapper, gr);
}
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
}
2017-06-05 11:27:21 +00:00
private UGraphicSvg(Dimension2D minDim, ColorMapper colorMapper, SvgGraphics svg, boolean textAsPath,
String linkTarget) {
2011-08-08 17:48:29 +00:00
super(colorMapper, svg);
2017-10-07 09:46:53 +00:00
this.stringBounder = FileFormat.PNG.getDefaultStringBounder(TikzFontDistortion.getDefault());
2013-12-10 19:36:50 +00:00
this.textAsPath2 = textAsPath;
2015-05-31 18:56:03 +00:00
this.target = linkTarget;
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));
2013-12-10 19:36:50 +00:00
if (textAsPath2) {
registerDriver(UText.class, new DriverTextAsPathSvg(TextBlockUtils.getFontRenderContext(), this));
2010-11-15 20:35:36 +00:00
} else {
registerDriver(UText.class, new DriverTextSvg(getStringBounder(), this));
}
registerDriver(ULine.class, new DriverLineSvg(this));
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();
}
public StringBounder getStringBounder() {
return stringBounder;
}
2017-04-05 17:37:42 +00:00
public void createXml(OutputStream os, String metadata) throws IOException {
2010-11-15 20:35:36 +00:00
try {
2017-04-05 17:37:42 +00:00
if (metadata != null) {
2019-09-14 18:12:04 +00:00
getGraphicObject().addComment(metadata);
2017-04-05 17:37:42 +00:00
}
2010-11-15 20:35:36 +00:00
getGraphicObject().createXml(os);
} catch (TransformerException e) {
throw new IOException(e.toString());
}
}
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
}
2013-12-10 19:36:50 +00:00
public void closeAction() {
getGraphicObject().closeLink();
2010-11-15 20:35:36 +00:00
}
2011-04-19 16:50:40 +00:00
2015-04-07 18:18:37 +00:00
public void writeImageTOBEMOVED(OutputStream os, String metadata, int dpi) throws IOException {
2017-04-05 17:37:42 +00:00
createXml(os, metadata);
2011-01-23 19:36:52 +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) {
if (propertyName.equalsIgnoreCase("SVG")) {
return true;
}
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
}