From bb1f76d3bcc7ba2537b57dcd56787c8cf9f7f19f Mon Sep 17 00:00:00 2001 From: hneemann Date: Mon, 3 Dec 2018 18:01:25 +0100 Subject: [PATCH] added a label to custom shapes, closes #221 --- .../digital/draw/shapes/ShapeFactory.java | 2 +- .../draw/shapes/custom/CustomShape.java | 9 +++++- .../shapes/custom/CustomShapeDescription.java | 31 +++++++++++++++++++ .../draw/shapes/custom/svg/SvgImporter.java | 5 ++- .../draw/shapes/custom/svg/SvgTemplate.java | 20 +++++++++--- 5 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/neemann/digital/draw/shapes/ShapeFactory.java b/src/main/java/de/neemann/digital/draw/shapes/ShapeFactory.java index 5a4c319c5..3605fb257 100644 --- a/src/main/java/de/neemann/digital/draw/shapes/ShapeFactory.java +++ b/src/main/java/de/neemann/digital/draw/shapes/ShapeFactory.java @@ -191,7 +191,7 @@ public final class ShapeFactory { case CUSTOM: final CustomShapeDescription customShapeDescription = customDescr.getAttributes().get(Keys.CUSTOM_SHAPE); if (customShapeDescription != CustomShapeDescription.EMPTY) - return new CustomShape(customShapeDescription, + return new CustomShape(customShapeDescription, elementAttributes.getCleanLabel(), pt.getInputDescription(elementAttributes), pt.getOutputDescriptions(elementAttributes)); default: diff --git a/src/main/java/de/neemann/digital/draw/shapes/custom/CustomShape.java b/src/main/java/de/neemann/digital/draw/shapes/custom/CustomShape.java index 6cab9ee61..3c4477076 100644 --- a/src/main/java/de/neemann/digital/draw/shapes/custom/CustomShape.java +++ b/src/main/java/de/neemann/digital/draw/shapes/custom/CustomShape.java @@ -23,6 +23,7 @@ import de.neemann.digital.draw.shapes.Shape; * Represents a custom shape. */ public class CustomShape implements Shape { + private final String label; private final CustomShapeDescription shapeDescription; private final PinDescriptions inputs; private final PinDescriptions outputs; @@ -32,11 +33,13 @@ public class CustomShape implements Shape { * Creates a new instance * * @param shapeDescription the description of the shape + * @param label the label * @param inputs the inputs of the component * @param outputs the inputs of the component * @throws PinException thrown if a pin is not found */ - public CustomShape(CustomShapeDescription shapeDescription, PinDescriptions inputs, PinDescriptions outputs) throws PinException { + public CustomShape(CustomShapeDescription shapeDescription, String label, PinDescriptions inputs, PinDescriptions outputs) throws PinException { + this.label = label; this.shapeDescription = shapeDescription; this.inputs = inputs; this.outputs = outputs; @@ -67,6 +70,10 @@ public class CustomShape implements Shape { for (Drawable d : shapeDescription) d.drawTo(graphic, highLight); + CustomShapeDescription.TextHolder l = shapeDescription.getLabel(); + if (l != null && label != null && !label.isEmpty()) + l.drawText(graphic, label); + for (Pin p : getPins()) { try { CustomShapeDescription.Pin cp = shapeDescription.getPin(p.getName()); diff --git a/src/main/java/de/neemann/digital/draw/shapes/custom/CustomShapeDescription.java b/src/main/java/de/neemann/digital/draw/shapes/custom/CustomShapeDescription.java index 0448e7268..4070a907e 100644 --- a/src/main/java/de/neemann/digital/draw/shapes/custom/CustomShapeDescription.java +++ b/src/main/java/de/neemann/digital/draw/shapes/custom/CustomShapeDescription.java @@ -30,6 +30,7 @@ public class CustomShapeDescription implements Iterable { private HashMap pins; private ArrayList drawables; + private TextHolder label; /** * Creates a new instance @@ -154,6 +155,26 @@ public class CustomShapeDescription implements Iterable { return pins.size(); } + /** + * Sets the label positioning info. + * + * @param pos0 pos0 + * @param pos1 pos1 + * @param textOrientation textOrientation + * @param fontSize fontSize + * @param filled filled + */ + public void setLabel(Vector pos0, Vector pos1, Orientation textOrientation, int fontSize, Color filled) { + label = new TextHolder(pos0, pos1, "", textOrientation, fontSize, filled); + } + + /** + * @return the TextHolder used to draw the label, maybe null + */ + public TextHolder getLabel() { + return label; + } + /** * Stores a line. */ @@ -280,6 +301,16 @@ public class CustomShapeDescription implements Iterable { @Override public void drawTo(Graphic graphic, Style highLight) { + drawText(graphic, text); + } + + /** + * Draws the given text to the given graphic instance + * + * @param graphic the graphic instance to draw to + * @param text the text to draw + */ + public void drawText(Graphic graphic, String text) { graphic.drawText(p1, p2, text, orientation, Style.NORMAL .deriveFontStyle(size, true) diff --git a/src/main/java/de/neemann/digital/draw/shapes/custom/svg/SvgImporter.java b/src/main/java/de/neemann/digital/draw/shapes/custom/svg/SvgImporter.java index f3922ea8b..045636138 100644 --- a/src/main/java/de/neemann/digital/draw/shapes/custom/svg/SvgImporter.java +++ b/src/main/java/de/neemann/digital/draw/shapes/custom/svg/SvgImporter.java @@ -274,7 +274,10 @@ public class SvgImporter { VectorInterface pos0 = p.transform(c.getTransform()); VectorInterface pos1 = p.add(new VectorFloat(1, 0)).transform(c.getTransform()); - drawTextElement(csd, c, element, pos0, pos1); + if (element.getAttribute("id").equals("label")) + csd.setLabel(pos0.round(), pos1.round(), c.getTextOrientation(), (int) c.getFontSize(), c.getFilled()); + else + drawTextElement(csd, c, element, pos0, pos1); } private void drawTextElement(CustomShapeDescription csd, Context c, Element element, VectorInterface pos0, VectorInterface pos1) throws SvgException { diff --git a/src/main/java/de/neemann/digital/draw/shapes/custom/svg/SvgTemplate.java b/src/main/java/de/neemann/digital/draw/shapes/custom/svg/SvgTemplate.java index 8ce7f95bc..301ec08e7 100644 --- a/src/main/java/de/neemann/digital/draw/shapes/custom/svg/SvgTemplate.java +++ b/src/main/java/de/neemann/digital/draw/shapes/custom/svg/SvgTemplate.java @@ -12,6 +12,7 @@ import de.neemann.digital.draw.elements.Circuit; import de.neemann.digital.draw.graphics.Style; import de.neemann.digital.draw.graphics.Vector; +import java.awt.*; import java.io.*; import java.nio.charset.StandardCharsets; @@ -80,9 +81,16 @@ public class SvgTemplate implements Closeable { * @throws Exception Exception */ public void create() throws Exception { - w.write(" \n"); + w.write(" \n"); - final Style style = Style.SHAPE_PIN; + Style style = Style.NORMAL; + w.write(" Label\n"); + + style = Style.SHAPE_PIN; final int yOffs = style.getFontSize() / 3; int y = 0; @@ -98,7 +106,7 @@ public class SvgTemplate implements Closeable { for (PinDescription o : outputs) { w.write(" \n"); w.write(" \n"); - Vector labelPos = new Vector(width-4, y + yOffs); + Vector labelPos = new Vector(width - 4, y + yOffs); w.write(" " + o.getName() + "\n"); w.write(" \n"); y += 20; @@ -106,7 +114,11 @@ public class SvgTemplate implements Closeable { } private String getColor(Style style) { - return "#" + Integer.toHexString(style.getColor().getRGB()).substring(2); + return getColor(style.getColor()); + } + + private String getColor(Color color) { + return "#" + Integer.toHexString(color.getRGB()).substring(2); } @Override