1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-12-22 02:49:06 +00:00

Allow Transparent Background for Ditaa Diagrams

This commit is contained in:
Christoph Rüthing 2022-02-28 13:04:55 +01:00
parent 8066a3f730
commit 473a8fce42
4 changed files with 46 additions and 11 deletions

View File

@ -34,6 +34,7 @@
*/
package net.sourceforge.plantuml.ditaa;
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.IOException;
@ -62,12 +63,13 @@ public class PSystemDitaa extends AbstractPSystem {
private final boolean dropShadows;
private final String data;
private final float scale;
private final boolean transparentBackground;
private final Font font;
private final boolean performSeparationOfCommonEdges;
private final boolean allCornersAreRound;
public PSystemDitaa(UmlSource source, String data, boolean performSeparationOfCommonEdges, boolean dropShadows,
boolean allCornersAreRound, float scale, Font font) {
boolean allCornersAreRound, boolean transparentBackground, float scale, Font font) {
super(source);
this.data = data;
this.dropShadows = dropShadows;
@ -85,13 +87,14 @@ public class PSystemDitaa extends AbstractPSystem {
e.printStackTrace();
this.processingOptions = null;
}
this.transparentBackground = transparentBackground;
this.scale = scale;
this.font = font;
}
PSystemDitaa add(String line) {
return new PSystemDitaa(getSource(), data + line + BackSlash.NEWLINE, performSeparationOfCommonEdges,
dropShadows, allCornersAreRound, scale, font);
dropShadows, allCornersAreRound, transparentBackground, scale, font);
}
public DiagramDescription getDescription() {
@ -116,6 +119,10 @@ public class PSystemDitaa extends AbstractPSystem {
final Field f_renderingOptions = options.getClass().getField("renderingOptions");
final Object renderingOptions = f_renderingOptions.get(options);
// renderingOptions.setBackgroundColor(font);
final Method setBackgroundColor = renderingOptions.getClass().getMethod("setBackgroundColor", Color.class);
setBackgroundColor.invoke(renderingOptions, transparentBackground ? new Color(0, 0, 0, 0) : Color.WHITE);
// renderingOptions.setFont(font);
final Method setFont = renderingOptions.getClass().getMethod("setFont", Font.class);
setFont.invoke(renderingOptions, font);

View File

@ -70,12 +70,16 @@ public class PSystemDitaaFactory extends PSystemBasicFactory<PSystemDitaa> {
if (startLine != null && (startLine.contains("-r") || startLine.contains("--round-corners")))
allCornersAreRound = true;
boolean transparentBackground = false;
if (startLine != null && (startLine.contains("-T") || startLine.contains("--transparent")))
transparentBackground = true;
final float scale = extractScale(startLine);
final Font font = extractFont(startLine);
if (getDiagramType() == DiagramType.UML)
return null;
else if (getDiagramType() == DiagramType.DITAA)
return new PSystemDitaa(source, "", performSeparationOfCommonEdges, dropShadows, allCornersAreRound, scale, font);
return new PSystemDitaa(source, "", performSeparationOfCommonEdges, dropShadows, allCornersAreRound, transparentBackground, scale, font);
else
throw new IllegalStateException(getDiagramType().name());
@ -96,9 +100,13 @@ public class PSystemDitaaFactory extends PSystemBasicFactory<PSystemDitaa> {
if (line.contains("-r") || line.contains("--round-corners"))
allCornersAreRound = true;
boolean transparentBackground = false;
if (line.contains("-T") || line.contains("--transparent"))
transparentBackground = true;
final float scale = extractScale(line);
final Font font = extractFont(line);
return new PSystemDitaa(source, "", performSeparationOfCommonEdges, dropShadows, allCornersAreRound, scale, font);
return new PSystemDitaa(source, "", performSeparationOfCommonEdges, dropShadows, allCornersAreRound, transparentBackground, scale, font);
}
if (system == null)
return null;

View File

@ -42,6 +42,8 @@ public class RenderingOptions {
private float scale = 1;
private Color backgroundColor = Color.white;
private Font font = new Font("Dialog", Font.BOLD, 12);
/**
@ -116,6 +118,18 @@ public class RenderingOptions {
antialias = b;
}
public Color getBackgroundColor() {
return backgroundColor;
}
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}
public boolean needsTransparency() {
return backgroundColor.getAlpha() < 255;
}
public Font getFont()
{
return font;

View File

@ -54,17 +54,23 @@ public class BitmapRenderer {
Stroke normalStroke;
Stroke dashStroke;
public RenderedImage renderToImage(Diagram diagram, RenderingOptions options){
BufferedImage image = new BufferedImage(
public RenderedImage renderToImage(Diagram diagram, RenderingOptions options){
BufferedImage image;
if(options.needsTransparency()) {
image = new BufferedImage(
diagram.getWidth(),
diagram.getHeight(),
BufferedImage.TYPE_INT_ARGB);
} else {
image = new BufferedImage(
diagram.getWidth(),
diagram.getHeight(),
BufferedImage.TYPE_INT_RGB);
}
return render(diagram, image, options);
}
public RenderedImage render(Diagram diagram, BufferedImage image, RenderingOptions options){
RenderedImage renderedImage = image;
Graphics2D g2 = image.createGraphics();
@ -75,7 +81,7 @@ public class BitmapRenderer {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting);
g2.setColor(Color.white);
g2.setColor(options.getBackgroundColor());
//TODO: find out why the next line does not work
g2.fillRect(0, 0, image.getWidth()+10, image.getHeight()+10);
/*for(int y = 0; y < diagram.getHeight(); y ++)