Skip to content

Commit

Permalink
Improve rendering of set/reset functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danilovesky committed Nov 7, 2024
1 parent b155378 commit b6748f7
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public int getGradientY() {
}

private static final double size = 0.3;
public static final Font FANOUT_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 1);
public static final Font FANOUT_FONT = new Font(Font.SANS_SERIF, Font.ITALIC, 1);
private static final double X_FANOUT_OFFSET = -0.25 * size;
private static final double Y_FANOUT_OFFSET = -0.5 * size;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.awt.event.KeyEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.*;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;

Expand All @@ -32,24 +31,17 @@ public class VisualFunctionContact extends VisualContact implements StateObserve

private enum ArrowType { UP, DOWN }

private static final double size = 0.3;
private static final double X_FUNC_OFFSET = 1.25 * size;
private static final double Y_FUNC_OFFSET = 0.5 * size;
private static final FontRenderContext context = new FontRenderContext(AffineTransform.getScaleInstance(1000.0, 1000.0), true, true);
private static Font functionFont;
private static final double X_FUNC_OFFSET_SCALE = 1.0;
private static final double ARROW_WIDTH_SCALE = 0.6;

private static final Font functionFont = new Font(Font.SANS_SERIF, Font.PLAIN, 1);
private static final FontRenderContext context = new FontRenderContext(
AffineTransform.getScaleInstance(1000.0, 1000.0), true, true);

private FormulaRenderingResult renderedSetFunction = null;
private FormulaRenderingResult renderedResetFunction = null;
private static double functionFontSize = CircuitSettings.getContactFontSize();

static {
try {
functionFont = Font.createFont(Font.TYPE1_FONT, ClassLoader.getSystemResourceAsStream("fonts/eurm10.pfb"));
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
}

public VisualFunctionContact(FunctionContact contact) {
super(contact);
}
Expand Down Expand Up @@ -113,16 +105,17 @@ private FormulaRenderingResult getRenderedSetFunction() {
}

private Point2D getSetFormulaOffset() {
double xOffset = X_FUNC_OFFSET;
double yOffset = -1.25 * Y_FUNC_OFFSET;
double s = CircuitSettings.getContactFontSize();
double xOffset = (X_FUNC_OFFSET_SCALE + ARROW_WIDTH_SCALE) * s;
double yOffset = -0.5 * CircuitSettings.getContactFontSize();
FormulaRenderingResult renderingResult = getRenderedSetFunction();
if (renderingResult != null) {
Direction dir = getDirection();
if (isPort()) {
dir = dir.flip();
}
if ((dir == Direction.SOUTH) || (dir == Direction.WEST)) {
xOffset = -(X_FUNC_OFFSET + renderingResult.boundingBox.getWidth());
xOffset = -X_FUNC_OFFSET_SCALE * s - renderingResult.boundingBox.getWidth();
}
}
return new Point2D.Double(xOffset, yOffset);
Expand Down Expand Up @@ -161,18 +154,19 @@ private FormulaRenderingResult getRenderedResetFunction() {
}

private Point2D getResetFormulaOffset() {
double xOffset = X_FUNC_OFFSET;
double yOffset = Y_FUNC_OFFSET;
double s = CircuitSettings.getContactFontSize();
double xOffset = (X_FUNC_OFFSET_SCALE + ARROW_WIDTH_SCALE) * s;
double yOffset = 0.5 * s;
FormulaRenderingResult renderingResult = getRenderedResetFunction();
if (renderingResult != null) {
Direction dir = getDirection();
if (!(getParent() instanceof VisualFunctionComponent)) {
dir = dir.flip();
}
if ((dir == Direction.SOUTH) || (dir == Direction.WEST)) {
xOffset = -(X_FUNC_OFFSET + renderingResult.boundingBox.getWidth());
xOffset = -X_FUNC_OFFSET_SCALE * s - renderingResult.boundingBox.getWidth();
}
yOffset = Y_FUNC_OFFSET + renderingResult.boundingBox.getHeight();
yOffset = 0.5 * s + renderingResult.boundingBox.getHeight();
}
return new Point2D.Double(xOffset, yOffset);
}
Expand All @@ -195,34 +189,45 @@ private Rectangle2D getResetBoundingBox() {
return bb;
}

private void drawArrow(Graphics2D g, ArrowType arrowType, double arrX, double arrY) {
private void drawArrow(Graphics2D g, ArrowType arrowType, Point2D offset) {
double s = CircuitSettings.getContactFontSize();
g.setStroke(new BasicStroke((float) s / 25));
double s1 = 0.75 * s;
double s2 = 0.45 * s;
double s3 = 0.30 * s;
g.setStroke(new BasicStroke(0.08f * (float) s));
double middleX = offset.getX() - ARROW_WIDTH_SCALE * s;
double middleY = offset.getY() - 0.3 * s;
double lineLongHeight = 0.25 * s;
double lineShortHeight = 0.15 * s;
double headHeight = 0.4 * s;
double headWidth = 0.15 * s;
switch (arrowType) {
case DOWN:
Line2D upLine = new Line2D.Double(arrX, arrY - s1, arrX, arrY - s3);
Path2D upPath = new Path2D.Double();
upPath.moveTo(arrX - 0.05, arrY - s3);
upPath.lineTo(arrX + 0.05, arrY - s3);
upPath.lineTo(arrX, arrY);
upPath.closePath();
g.fill(upPath);
g.draw(upLine);
break;
case UP:
Line2D downLine = new Line2D.Double(arrX, arrY, arrX, arrY - s2);
Path2D downPath = new Path2D.Double();
downPath.moveTo(arrX - 0.05, arrY - s2);
downPath.lineTo(arrX + 0.05, arrY - s2);
downPath.lineTo(arrX, arrY - s1);
downPath.closePath();
g.fill(downPath);
g.draw(downLine);
double upLineY = middleY + lineLongHeight;
double upHeadY = middleY - lineShortHeight;
Path2D upHeadPath = new Path2D.Double();
upHeadPath.moveTo(middleX - headWidth, upHeadY);
upHeadPath.lineTo(middleX + headWidth, upHeadY);
upHeadPath.lineTo(middleX, upHeadY - headHeight);
upHeadPath.closePath();
g.fill(upHeadPath);
g.draw(new Line2D.Double(middleX, upLineY, middleX, upHeadY));
break;
case DOWN:
double downLineY = middleY - lineLongHeight;
double downHeadY = middleY + lineShortHeight;
Path2D downHeadPath = new Path2D.Double();
downHeadPath.moveTo(middleX - headWidth, downHeadY);
downHeadPath.lineTo(middleX + headWidth, downHeadY);
downHeadPath.lineTo(middleX, downHeadY + headHeight);
downHeadPath.closePath();
g.fill(downHeadPath);
g.draw(new Line2D.Double(middleX, downLineY, middleX, downHeadY));
break;
}
double assignStartX = middleX + 0.25 * s;
double assignEndX = middleX + 0.55 * s;
double assignTopY = middleY - 0.08 * s;
double assignBottomY = middleY + 0.08 * s;
g.draw(new Line2D.Double(assignStartX, assignTopY, assignEndX, assignTopY));
g.draw(new Line2D.Double(assignStartX, assignBottomY, assignEndX, assignBottomY));
}

private void drawFormula(Graphics2D g, ArrowType arrowType, Point2D offset, FormulaRenderingResult renderingResult) {
Expand All @@ -239,12 +244,7 @@ private void drawFormula(Graphics2D g, ArrowType arrowType, Point2D offset, Form
g.transform(rotateTransform);
}

double dXArrow = -0.1;
if ((dir == Direction.SOUTH) || (dir == Direction.WEST)) {
dXArrow = 0.1 + renderingResult.boundingBox.getWidth();
}

drawArrow(g, arrowType, offset.getX() + dXArrow, offset.getY());
drawArrow(g, arrowType, offset);

g.translate(offset.getX(), offset.getY());
renderingResult.draw(g);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,19 @@ public void mouseClicked(GraphEditorMouseEvent e) {
@Override
public Collection<VisualNode> getNodeWithAdjacentConnections(VisualModel model, VisualNode node) {
HashSet<VisualNode> result = new HashSet<>();
if (node instanceof VisualCircuitComponent) {
VisualCircuitComponent component = (VisualCircuitComponent) node;
if ((node instanceof VisualContact contact) && contact.isDriven()) {
result.add(contact);
for (VisualConnection connection : model.getConnections(contact)) {
VisualNode firstNode = connection.getFirst();
if (firstNode instanceof VisualReplica) {
result.add(connection);
result.add(firstNode);
}
}
} else if (node instanceof VisualCircuitComponent component) {
result.add(component);
for (VisualContact inputContact : component.getVisualInputs()) {
for (VisualConnection connection : model.getConnections(inputContact)) {
VisualNode firstNode = connection.getFirst();
if (firstNode instanceof VisualReplica) {
result.add(connection);
result.add(firstNode);
}
}
result.addAll(getNodeWithAdjacentConnections(model, inputContact));
}
} else {
Queue<VisualNode> queue = new LinkedList<>(super.getNodeWithAdjacentConnections(model, node));
Expand All @@ -156,8 +158,7 @@ public Collection<VisualNode> getNodeWithAdjacentConnections(VisualModel model,
continue;
}
result.add(node);
if (node instanceof VisualConnection) {
VisualConnection connection = (VisualConnection) node;
if (node instanceof VisualConnection connection) {
VisualNode firstNode = connection.getFirst();
if (firstNode instanceof VisualJoint) {
result.add(firstNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public RenderedFormula(String text, BooleanFormula formula, Font font, Positioni
this.xOffset = offset.getX();
this.yOffset = offset.getY();
final FontRenderContext context = new FontRenderContext(AffineTransform.getScaleInstance(1000.0, 1000.0), true, true);

renderingResult = FormulaToGraphics.print(text, font, context);
if (formula != One.getInstance()) {
renderingResult.add(FormulaToGraphics.render(formula, context, font));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Point2D getLabelOffset() {

@Override
public Font getNameFont() {
return LABEL_FONT.deriveFont((float) StgSettings.getTransitionFontSize());
return NAME_FONT.deriveFont((float) StgSettings.getTransitionFontSize());
}

@Override
Expand Down
Binary file modified workcraft/WorkcraftCore/res/fonts/default.pfb
Binary file not shown.
Binary file removed workcraft/WorkcraftCore/res/fonts/eurm10.pfb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class VisualComponent extends VisualTransformableNode implements Dependen
public static final String PROPERTY_COLOR = "Color";
public static final String PROPERTY_FILL_COLOR = "Fill color";

public static final Font NAME_FONT = new Font(Font.SANS_SERIF, Font.ITALIC, 1);
public static final Font LABEL_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 1);
public static final Font NAME_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 1);
public static final Font LABEL_FONT = new Font(Font.SANS_SERIF, Font.ITALIC, 1);

private final MathNode refNode;
private Color foregroundColor = VisualCommonSettings.getBorderColor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class VisualReplica extends VisualTransformableNode implements Replica, D

public static final String PROPERTY_NAME_COLOR = "Name color";

public static final Font NAME_FONT = new Font(Font.SANS_SERIF, Font.ITALIC, 1);
public static final Font NAME_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 1);

private RenderedText nameRenderedText = new RenderedText("", getNameFont(), getNamePositioning(), getNameOffset());
private Color nameColor = VisualCommonSettings.getNameColor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public void init(FontRenderContext fontRenderContext, Font font, boolean unicode
init(imply, or, fontRenderContext, font, unicodeAllowed);
init(or, xor, fontRenderContext, font, unicodeAllowed);
init(xor, and, fontRenderContext, font, unicodeAllowed);
init(and, not, fontRenderContext, font, unicodeAllowed);
// Use default.pfb font with improved u00B7 symbol for AND operator
init(and, not, fontRenderContext, defaultFont, unicodeAllowed);
init(not, vars, fontRenderContext, font, unicodeAllowed);
init(vars, constants, fontRenderContext, font, unicodeAllowed);
init(constants, paren, fontRenderContext, font, unicodeAllowed);
Expand Down Expand Up @@ -273,35 +274,35 @@ public FormulaRenderingResult visit(Not node) {
public static class AndPrinter extends DelegatingPrinter {
@Override
public FormulaRenderingResult visit(And node) {
return visitBinary(this, unicodeAllowed ? " \u2022 " : " * ", node);
return visitBinary(this, unicodeAllowed ? "\u00B7" : "*", node);
}
}

public static class OrPrinter extends DelegatingPrinter {
@Override
public FormulaRenderingResult visit(Or node) {
return visitBinary(this, " + ", node);
return visitBinary(this, "+", node);
}
}

public static class XorPrinter extends DelegatingPrinter {
@Override
public FormulaRenderingResult visit(Xor node) {
return visitBinary(this, unicodeAllowed ? " \u2295 " : " ^ ", node);
return visitBinary(this, unicodeAllowed ? "\u2295" : "^", node);
}
}

public static class ImplyPrinter extends DelegatingPrinter {
@Override
public FormulaRenderingResult visit(Imply node) {
return visitBinary(next, unicodeAllowed ? " \u21d2 " : " => ", node);
return visitBinary(next, unicodeAllowed ? "\u21d2" : "=>", node);
}
}

public static class IffPrinter extends DelegatingPrinter {
@Override
public FormulaRenderingResult visit(Iff node) {
return visitBinary(this, " = ", node);
return visitBinary(this, "=", node);
}
}

Expand Down

0 comments on commit b6748f7

Please sign in to comment.