net.sourceforge.plantuml.svek.CucaDiagramFileMakerSvek Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plantuml Show documentation
Show all versions of plantuml Show documentation
PlantUML is a component that allows to quickly write :
* sequence diagram,
* use case diagram,
* class diagram,
* activity diagram,
* component diagram,
* state diagram
* object diagram
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2013, Arnaud Roques
*
* Project Info: http://plantuml.sourceforge.net
*
* 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
* Revision $Revision: 6711 $
*
*/
package net.sourceforge.plantuml.svek;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import net.sourceforge.plantuml.CMapData;
import net.sourceforge.plantuml.Dimension2DDouble;
import net.sourceforge.plantuml.EmptyImageBuilder;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.FontParam;
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.Scale;
import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.UmlDiagramType;
import net.sourceforge.plantuml.Url;
import net.sourceforge.plantuml.activitydiagram3.ftile.EntityImageLegend;
import net.sourceforge.plantuml.api.ImageDataComplex;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.cucadiagram.CucaDiagram;
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.cucadiagram.IEntity;
import net.sourceforge.plantuml.cucadiagram.Link;
import net.sourceforge.plantuml.cucadiagram.dot.CucaDiagramSimplifierActivity;
import net.sourceforge.plantuml.cucadiagram.dot.CucaDiagramSimplifierState;
import net.sourceforge.plantuml.cucadiagram.dot.DotData;
import net.sourceforge.plantuml.eps.EpsStrategy;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.HtmlColor;
import net.sourceforge.plantuml.graphic.HtmlColorGradient;
import net.sourceforge.plantuml.graphic.HtmlColorSimple;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.StringBounderUtils;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
import net.sourceforge.plantuml.png.PngIO;
import net.sourceforge.plantuml.ugraphic.UChangeBackColor;
import net.sourceforge.plantuml.ugraphic.UFont;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.eps.UGraphicEps;
import net.sourceforge.plantuml.ugraphic.g2d.UGraphicG2d;
import net.sourceforge.plantuml.ugraphic.svg.UGraphicSvg;
import net.sourceforge.plantuml.ugraphic.visio.UGraphicVdx;
public final class CucaDiagramFileMakerSvek implements CucaDiagramFileMaker {
private final CucaDiagram diagram;
private final List flashcodes;
static private final StringBounder stringBounder;
static {
final EmptyImageBuilder builder = new EmptyImageBuilder(10, 10, Color.WHITE);
stringBounder = StringBounderUtils.asStringBounder(builder.getGraphics2D());
}
public CucaDiagramFileMakerSvek(CucaDiagram diagram, List flashcodes) throws IOException {
this.diagram = diagram;
this.flashcodes = flashcodes;
}
public ImageData createFile(OutputStream os, List dotStrings, FileFormatOption fileFormatOption)
throws IOException {
try {
return createFileInternal(os, dotStrings, fileFormatOption);
} catch (InterruptedException e) {
e.printStackTrace();
throw new IOException(e);
}
}
private ImageData createFileInternal(OutputStream os, List dotStrings, FileFormatOption fileFormatOption)
throws IOException, InterruptedException {
if (diagram.getUmlDiagramType() == UmlDiagramType.ACTIVITY) {
new CucaDiagramSimplifierActivity(diagram, dotStrings);
} else if (diagram.getUmlDiagramType() == UmlDiagramType.STATE) {
new CucaDiagramSimplifierState(diagram, dotStrings);
}
final DotData dotData = new DotData(diagram.getEntityFactory().getRootGroup(), getOrderedLinks(), diagram
.getLeafs().values(), diagram.getUmlDiagramType(), diagram.getSkinParam(), diagram.getRankdir(),
diagram, diagram, diagram.getColorMapper(), diagram.getEntityFactory(), diagram.isHideEmptyDescriptionForState());
final CucaDiagramFileMakerSvek2 svek2 = new CucaDiagramFileMakerSvek2(dotData, diagram.getEntityFactory(),
false);
TextBlockBackcolored result = svek2.createFile(diagram.getDotStringSkek());
result = addLegend(result);
result = addTitle(result);
result = addHeaderAndFooter(result);
// final Dimension2D dim =
// Dimension2DDouble.delta(result.getDimension(stringBounder), 10);
final Dimension2D dim = result.calculateDimension(stringBounder);
final FileFormat fileFormat = fileFormatOption.getFileFormat();
if (fileFormat == FileFormat.PNG) {
createPng(os, fileFormatOption, result, dim);
} else if (fileFormat == FileFormat.SVG) {
createSvg(os, fileFormatOption, result, dim);
} else if (fileFormat == FileFormat.VDX) {
createVdx(os, fileFormatOption, result, dim);
} else if (fileFormat == FileFormat.EPS) {
createEps(os, fileFormatOption, result, dim);
} else {
throw new UnsupportedOperationException(fileFormat.toString());
}
double deltaX = 0;
double deltaY = 0;
if (result instanceof DecorateEntityImage) {
deltaX += ((DecorateEntityImage) result).getDeltaX();
deltaY += ((DecorateEntityImage) result).getDeltaY();
}
final Dimension2D finalDimension = Dimension2DDouble.delta(dim, deltaX, deltaY);
CMapData cmap = null;
if (diagram.hasUrl() && fileFormatOption.getFileFormat() == FileFormat.PNG) {
cmap = cmapString(svek2);
}
final String widthwarning = diagram.getSkinParam().getValue("widthwarning");
if (widthwarning != null && widthwarning.matches("\\d+")) {
this.warningOrError = svek2.getBibliotekon().getWarningOrError(Integer.parseInt(widthwarning));
} else {
this.warningOrError = null;
}
return new ImageDataComplex(finalDimension, cmap, getWarningOrError());
}
private List getOrderedLinks() {
final List result = new ArrayList();
for (Link l : diagram.getLinks()) {
addLinkNew(result, l);
}
return result;
// return diagram.getLinks();
}
private void addLinkNew(List result, Link link) {
for (int i = 0; i < result.size(); i++) {
final Link other = result.get(i);
if (other.sameConnections(link)) {
while (i < result.size() && result.get(i).sameConnections(link)) {
i++;
}
if (i == result.size()) {
result.add(link);
} else {
result.add(i, link);
}
return;
}
}
result.add(link);
}
private String warningOrError;
private String getWarningOrError() {
return warningOrError;
}
private CMapData cmapString(CucaDiagramFileMakerSvek2 svek2) {
final CMapData cmapdata = new CMapData();
final Collection all = new ArrayList(diagram.getLeafs().values());
all.addAll(diagram.getGroups(false));
int seq = 1;
for (IEntity ent : all) {
final List rev = new ArrayList(ent.getUrls());
// For zlevel order
Collections.reverse(rev);
for (Url url : rev) {
appendUrl(cmapdata, seq, url);
seq++;
}
}
for (Link link : diagram.getLinks()) {
final Url url = link.getUrl();
if (url == null) {
continue;
}
appendUrl(cmapdata, seq, url);
seq++;
}
return cmapdata;
}
private void appendUrl(final CMapData cmapdata, int seq, Url url) {
cmapdata.appendString("");
cmapdata.appendString("\n");
}
private TextBlockBackcolored addHeaderAndFooter(TextBlockBackcolored original) {
final Display footer = diagram.getFooter();
final Display header = diagram.getHeader();
if (footer == null && header == null) {
return original;
}
final TextBlock textFooter = footer == null ? null : TextBlockUtils.create(footer, new FontConfiguration(
getFont(FontParam.FOOTER), getFontColor(FontParam.FOOTER, null)), diagram.getFooterAlignment(),
diagram.getSkinParam());
final TextBlock textHeader = header == null ? null : TextBlockUtils.create(header, new FontConfiguration(
getFont(FontParam.HEADER), getFontColor(FontParam.HEADER, null)), diagram.getHeaderAlignment(),
diagram.getSkinParam());
return new DecorateEntityImage(original, textHeader, diagram.getHeaderAlignment(), textFooter,
diagram.getFooterAlignment());
}
private TextBlockBackcolored addTitle(TextBlockBackcolored original) {
final Display title = diagram.getTitle();
if (title == null) {
return original;
}
final TextBlock text = TextBlockUtils.create(title, new FontConfiguration(getFont(FontParam.TITLE),
getFontColor(FontParam.TITLE, null)), HorizontalAlignment.CENTER, diagram.getSkinParam());
return DecorateEntityImage.addTop(original, text, HorizontalAlignment.CENTER);
}
private TextBlockBackcolored addLegend(TextBlockBackcolored original) {
final Display legend = diagram.getLegend();
if (legend == null) {
return original;
}
final TextBlock text = EntityImageLegend.create(legend, diagram.getSkinParam());
return DecorateEntityImage.addBottom(original, text, diagram.getLegendAlignment());
}
private final UFont getFont(FontParam fontParam) {
final ISkinParam skinParam = diagram.getSkinParam();
return skinParam.getFont(fontParam, null);
}
private final HtmlColor getFontColor(FontParam fontParam, String stereo) {
final ISkinParam skinParam = diagram.getSkinParam();
return skinParam.getFontHtmlColor(fontParam, stereo);
}
private void createPng(OutputStream os, FileFormatOption fileFormatOption, final TextBlockBackcolored result,
final Dimension2D dim) throws IOException {
final double dpiFactor;
final Scale scale = diagram.getScale();
if (scale == null) {
dpiFactor = diagram.getDpiFactor(fileFormatOption);
} else {
dpiFactor = scale.getScale(dim.getWidth(), dim.getHeight());
}
final UGraphicG2d ug = (UGraphicG2d) fileFormatOption.createUGraphic(diagram.getSkinParam().getColorMapper(),
dpiFactor, dim, result.getBackcolor(), diagram.isRotation());
result.drawU(ug);
PngIO.write(ug.getBufferedImage(), os, diagram.getMetadata(), diagram.getDpi(fileFormatOption));
}
private void createPngOld(OutputStream os, FileFormatOption fileFormatOption, final IEntityImage result,
final Dimension2D dim) throws IOException {
Color backColor = Color.WHITE;
if (result.getBackcolor() instanceof HtmlColorSimple) {
backColor = diagram.getSkinParam().getColorMapper().getMappedColor(result.getBackcolor());
}
final double dpiFactor;
final Scale scale = diagram.getScale();
if (scale == null) {
dpiFactor = diagram.getDpiFactor(fileFormatOption);
} else {
dpiFactor = scale.getScale(dim.getWidth(), dim.getHeight());
}
final EmptyImageBuilder builder;
final Graphics2D graphics2D;
if (diagram.isRotation()) {
builder = new EmptyImageBuilder((int) (dim.getHeight() * dpiFactor), (int) (dim.getWidth() * dpiFactor),
backColor);
graphics2D = builder.getGraphics2D();
graphics2D.rotate(-Math.PI / 2);
graphics2D.translate(-builder.getBufferedImage().getHeight(), 0);
} else {
builder = new EmptyImageBuilder((int) (dim.getWidth() * dpiFactor), (int) (dim.getHeight() * dpiFactor),
backColor);
graphics2D = builder.getGraphics2D();
}
final UGraphic ug = new UGraphicG2d(diagram.getSkinParam().getColorMapper(), graphics2D, dpiFactor);
((UGraphicG2d) ug).setBufferedImage(builder.getBufferedImage());
final BufferedImage im = ((UGraphicG2d) ug).getBufferedImage();
if (result.getBackcolor() instanceof HtmlColorGradient) {
ug.apply(new UChangeBackColor(result.getBackcolor())).draw(new URectangle(im.getWidth(), im.getHeight()));
}
result.drawU(ug);
PngIO.write(im, os, diagram.getMetadata(), diagram.getDpi(fileFormatOption));
}
private void createSvg(OutputStream os, FileFormatOption fileFormatOption, final TextBlockBackcolored result,
final Dimension2D dim) throws IOException {
Color backColor = Color.WHITE;
if (result.getBackcolor() instanceof HtmlColorSimple) {
backColor = diagram.getSkinParam().getColorMapper().getMappedColor(result.getBackcolor());
}
final UGraphicSvg ug;
if (result.getBackcolor() instanceof HtmlColorGradient) {
ug = new UGraphicSvg(diagram.getSkinParam().getColorMapper(), (HtmlColorGradient) result.getBackcolor(),
false);
} else if (backColor == null || backColor.equals(Color.WHITE)) {
ug = new UGraphicSvg(diagram.getSkinParam().getColorMapper(), false);
} else {
ug = new UGraphicSvg(diagram.getSkinParam().getColorMapper(), StringUtils.getAsHtml(backColor), false);
}
// ug.getParam().setSprites(diagram.getSprites());
result.drawU(ug);
ug.createXml(os);
}
private void createVdx(OutputStream os, FileFormatOption fileFormatOption, final TextBlockBackcolored result,
final Dimension2D dim) throws IOException {
final UGraphicVdx ug = new UGraphicVdx(diagram.getSkinParam().getColorMapper());
result.drawU(ug);
ug.createVsd(os);
}
private void createEps(OutputStream os, FileFormatOption fileFormatOption, final TextBlockBackcolored result,
final Dimension2D dim) throws IOException {
final UGraphicEps ug = new UGraphicEps(diagram.getSkinParam().getColorMapper(), EpsStrategy.getDefault2());
// ug.getParam().setSprites(diagram.getSprites());
result.drawU(ug);
os.write(ug.getEPSCode().getBytes());
}
}