Skip to content

Commit

Permalink
Fix #2639, #2640 and #2641
Browse files Browse the repository at this point in the history
  • Loading branch information
mbastian committed Sep 25, 2022
1 parent 65e48fe commit eed37ab
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
Expand Up @@ -216,10 +216,7 @@ public Helper(
final float size = properties.getFloatValue(PreviewProperty.ARROW_SIZE)
* weight.floatValue();
float radius = -(properties.getFloatValue(PreviewProperty.EDGE_RADIUS)
+ (Float) targetItem.getData(NodeItem.SIZE) / 2f
+ Math.max(0, properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH)) /
2f //We have to divide by 2 because the border stroke is not only an outline but also draws the other half of the curve inside the node
);
+ (Float) targetItem.getData(NodeItem.SIZE) / 2f);

//Avoid arrow from passing the node's center:
if (radius > 0) {
Expand Down
Expand Up @@ -260,16 +260,13 @@ public void preProcess(PreviewModel previewModel) {

final float targetRadius = -(edgeRadius
+ (Float) targetItem.getData(NodeItem.SIZE) / 2f
+ properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH) / 2f
//We have to divide by 2 because the border stroke is not only an outline but also draws the other half of the curve inside the node
+ arrowRadiusSize);
item.setData(TARGET_RADIUS, targetRadius);

//Source
final Item sourceItem = item.getData(SOURCE);
final float sourceRadius = -(edgeRadius
+ (Float) sourceItem.getData(NodeItem.SIZE) / 2f
+ properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH) / 2f);
+ (Float) sourceItem.getData(NodeItem.SIZE) / 2f);
item.setData(SOURCE_RADIUS, sourceRadius);
}
}
Expand Down
Expand Up @@ -104,8 +104,7 @@ public CanvasSize getCanvasSize(
final PreviewProperties properties) {
final float x = item.getData(NodeItem.X);
final float y = item.getData(NodeItem.Y);
final float s = (Float) item.getData(NodeItem.SIZE)
+ properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH);
final float s = item.getData(NodeItem.SIZE);
final float r = s / 2F;
final int intS = Math.round(s);
return new CanvasSize(
Expand Down Expand Up @@ -136,17 +135,34 @@ public void renderG2D(Item item, G2DTarget target, PreviewProperties properties)
//Graphics
Graphics2D graphics = target.getGraphics();

//Border can't be larger than size
borderSize = Math.min(borderSize, size / 2f);

// Set size and pos
size = size - borderSize;
x = x - (size / 2f);
y = y - (size / 2f);
Ellipse2D.Float ellipse = new Ellipse2D.Float(x, y, size, size);

//Draw fill
Ellipse2D.Float ellipse;
if (alpha == 255) {
// Allow the border and the fill to overlap a bit to avoid rendering artifacts
ellipse = new Ellipse2D.Float(x + borderSize / 4f, y + borderSize / 4f, size - borderSize / 2f,
size - borderSize / 2f);
} else {
// Special case making sure the border and the fill are not overlapping
ellipse =
new Ellipse2D.Float(x + borderSize / 2f, y + borderSize / 2f, size - borderSize, size - borderSize);
}
graphics.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha));
graphics.fill(ellipse);

if (borderSize > 0) {
Ellipse2D.Float borderEllipse = new Ellipse2D.Float(x, y, size, size);
graphics.setColor(new Color(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue(), alpha));
graphics.setStroke(new BasicStroke(borderSize));
graphics.draw(ellipse);
graphics.draw(borderEllipse);
}

graphics.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha));
graphics.fill(ellipse);
}

public void renderSVG(Item item, SVGTarget target, PreviewProperties properties) {
Expand All @@ -155,7 +171,6 @@ public void renderSVG(Item item, SVGTarget target, PreviewProperties properties)
Float x = item.getData(NodeItem.X);
Float y = item.getData(NodeItem.Y);
Float size = item.getData(NodeItem.SIZE);
size /= 2f;
Color color = item.getData(NodeItem.COLOR);
Color borderColor = ((DependantColor) properties.getValue(PreviewProperty.NODE_BORDER_COLOR)).getColor(color);
float borderSize = properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH);
Expand All @@ -166,35 +181,57 @@ public void renderSVG(Item item, SVGTarget target, PreviewProperties properties)
alpha = 1;
}

// Border can't be larger than size
borderSize = Math.min(borderSize, size / 2f);

Element nodeElem = target.createElement("circle");
Element nodeBorderElem = nodeElem;
nodeElem.setAttribute("class", SVGUtils.idAsClassAttribute(node.getId()));
nodeElem.setAttribute("cx", x.toString());
nodeElem.setAttribute("cy", y.toString());
nodeElem.setAttribute("r", size.toString());
nodeElem.setAttribute("fill", target.toHexString(color));
nodeElem.setAttribute("fill-opacity", "" + alpha);

if (borderSize > 0) {
nodeElem.setAttribute("stroke", target.toHexString(borderColor));
nodeElem.setAttribute(
if (alpha < 1) {
// Special case making sure the border and the fill are not overlapping
nodeBorderElem = target.createElement("circle");
nodeBorderElem.setAttribute("cx", x.toString());
nodeBorderElem.setAttribute("cy", y.toString());
nodeBorderElem.setAttribute("r", Float.toString((size / 2f) - borderSize / 2f));
nodeBorderElem.setAttribute("fill", "none");

nodeElem.setAttribute("r", Float.toString((size / 2f) - borderSize));
target.getTopElement(SVGTarget.TOP_NODES).appendChild(nodeElem);
} else {
nodeElem.setAttribute("r", Float.toString((size - borderSize) / 2f));
}
nodeBorderElem.setAttribute("stroke", target.toHexString(borderColor));
nodeBorderElem.setAttribute(
"stroke-width",
Float.toString(borderSize * target.getScaleRatio()));
nodeElem.setAttribute("stroke-opacity", "" + alpha);
nodeBorderElem.setAttribute("stroke-opacity", "" + alpha);
} else {
nodeElem.setAttribute("r", Float.toString((size - borderSize) / 2f));
}
target.getTopElement(SVGTarget.TOP_NODES).appendChild(nodeElem);

target.getTopElement(SVGTarget.TOP_NODES).appendChild(nodeBorderElem);
}

public void renderPDF(Item item, PDFTarget target, PreviewProperties properties) {
Float x = item.getData(NodeItem.X);
Float y = item.getData(NodeItem.Y);
Float size = item.getData(NodeItem.SIZE);
size /= 2f;
Color color = item.getData(NodeItem.COLOR);
Color borderColor = ((DependantColor) properties.getValue(PreviewProperty.NODE_BORDER_COLOR)).getColor(color);
float borderSize = properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH);
float alpha = properties.getBooleanValue(PreviewProperty.NODE_PER_NODE_OPACITY) ?
color.getAlpha() / 255f :
properties.getFloatValue(PreviewProperty.NODE_OPACITY) / 100f;

// Border can't be larger than size
borderSize = Math.min(borderSize, size / 2f);

PdfContentByte cb = target.getContentByte();
cb.setRGBColorStroke(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue());
cb.setLineWidth(borderSize);
Expand All @@ -206,9 +243,15 @@ public void renderPDF(Item item, PDFTarget target, PreviewProperties properties)
gState.setStrokeOpacity(alpha);
cb.setGState(gState);
}
cb.circle(x, -y, size);
if (borderSize > 0) {

cb.circle(x, -y, (size / 2f) - borderSize / 2f);
if (borderSize > 0 && alpha == 1f) {
cb.fillStroke();
} else if (borderSize > 0 && alpha < 1f) {
// Special case to make sure the border and the fill are not overlapping
cb.stroke();
cb.circle(x, -y, (size / 2f) - borderSize);
cb.fill();
} else {
cb.fill();
}
Expand Down

0 comments on commit eed37ab

Please sign in to comment.