added a label to custom shapes, closes #221

This commit is contained in:
hneemann 2018-12-03 18:01:25 +01:00
parent 0c36b048a7
commit bb1f76d3bc
5 changed files with 60 additions and 7 deletions

View File

@ -191,7 +191,7 @@ public final class ShapeFactory {
case CUSTOM: case CUSTOM:
final CustomShapeDescription customShapeDescription = customDescr.getAttributes().get(Keys.CUSTOM_SHAPE); final CustomShapeDescription customShapeDescription = customDescr.getAttributes().get(Keys.CUSTOM_SHAPE);
if (customShapeDescription != CustomShapeDescription.EMPTY) if (customShapeDescription != CustomShapeDescription.EMPTY)
return new CustomShape(customShapeDescription, return new CustomShape(customShapeDescription, elementAttributes.getCleanLabel(),
pt.getInputDescription(elementAttributes), pt.getInputDescription(elementAttributes),
pt.getOutputDescriptions(elementAttributes)); pt.getOutputDescriptions(elementAttributes));
default: default:

View File

@ -23,6 +23,7 @@ import de.neemann.digital.draw.shapes.Shape;
* Represents a custom shape. * Represents a custom shape.
*/ */
public class CustomShape implements Shape { public class CustomShape implements Shape {
private final String label;
private final CustomShapeDescription shapeDescription; private final CustomShapeDescription shapeDescription;
private final PinDescriptions inputs; private final PinDescriptions inputs;
private final PinDescriptions outputs; private final PinDescriptions outputs;
@ -32,11 +33,13 @@ public class CustomShape implements Shape {
* Creates a new instance * Creates a new instance
* *
* @param shapeDescription the description of the shape * @param shapeDescription the description of the shape
* @param label the label
* @param inputs the inputs of the component * @param inputs the inputs of the component
* @param outputs the inputs of the component * @param outputs the inputs of the component
* @throws PinException thrown if a pin is not found * @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.shapeDescription = shapeDescription;
this.inputs = inputs; this.inputs = inputs;
this.outputs = outputs; this.outputs = outputs;
@ -67,6 +70,10 @@ public class CustomShape implements Shape {
for (Drawable d : shapeDescription) for (Drawable d : shapeDescription)
d.drawTo(graphic, highLight); d.drawTo(graphic, highLight);
CustomShapeDescription.TextHolder l = shapeDescription.getLabel();
if (l != null && label != null && !label.isEmpty())
l.drawText(graphic, label);
for (Pin p : getPins()) { for (Pin p : getPins()) {
try { try {
CustomShapeDescription.Pin cp = shapeDescription.getPin(p.getName()); CustomShapeDescription.Pin cp = shapeDescription.getPin(p.getName());

View File

@ -30,6 +30,7 @@ public class CustomShapeDescription implements Iterable<Drawable> {
private HashMap<String, Pin> pins; private HashMap<String, Pin> pins;
private ArrayList<Drawable> drawables; private ArrayList<Drawable> drawables;
private TextHolder label;
/** /**
* Creates a new instance * Creates a new instance
@ -154,6 +155,26 @@ public class CustomShapeDescription implements Iterable<Drawable> {
return pins.size(); 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. * Stores a line.
*/ */
@ -280,6 +301,16 @@ public class CustomShapeDescription implements Iterable<Drawable> {
@Override @Override
public void drawTo(Graphic graphic, Style highLight) { 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, graphic.drawText(p1, p2, text, orientation,
Style.NORMAL Style.NORMAL
.deriveFontStyle(size, true) .deriveFontStyle(size, true)

View File

@ -274,6 +274,9 @@ public class SvgImporter {
VectorInterface pos0 = p.transform(c.getTransform()); VectorInterface pos0 = p.transform(c.getTransform());
VectorInterface pos1 = p.add(new VectorFloat(1, 0)).transform(c.getTransform()); VectorInterface pos1 = p.add(new VectorFloat(1, 0)).transform(c.getTransform());
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); drawTextElement(csd, c, element, pos0, pos1);
} }

View File

@ -12,6 +12,7 @@ import de.neemann.digital.draw.elements.Circuit;
import de.neemann.digital.draw.graphics.Style; import de.neemann.digital.draw.graphics.Style;
import de.neemann.digital.draw.graphics.Vector; import de.neemann.digital.draw.graphics.Vector;
import java.awt.*;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -80,9 +81,16 @@ public class SvgTemplate implements Closeable {
* @throws Exception Exception * @throws Exception Exception
*/ */
public void create() throws Exception { public void create() throws Exception {
w.write(" <rect fill=\"none\" stroke=\"black\" stroke-width=\"3\" x=\"0\" y=\"-10\" width=\"" + width + "\" height=\"" + height + "\"/>\n"); w.write(" <rect fill=\"none\" stroke=\"black\" stroke-width=\"3\""
+ " fill=\"" + getColor(Keys.BACKGROUND_COLOR.getDefault()) + "\""
+ " fill-opacity=\"" + (Keys.BACKGROUND_COLOR.getDefault().getAlpha() / 255f) + "\""
+ " x=\"0\" y=\"-10\" width=\"" + width + "\" height=\"" + height + "\"/>\n");
final Style style = Style.SHAPE_PIN; Style style = Style.NORMAL;
w.write(" <text id=\"label\" fill=\"" + getColor(style) + "\" font-size=\""
+ (style.getFontSize() - 1) + "\" text-anchor=\"middle\" x=\"" + width / 2 + "\" y=\"" + (-SIZE) + "\">Label</text>\n");
style = Style.SHAPE_PIN;
final int yOffs = style.getFontSize() / 3; final int yOffs = style.getFontSize() / 3;
int y = 0; int y = 0;
@ -106,7 +114,11 @@ public class SvgTemplate implements Closeable {
} }
private String getColor(Style style) { 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 @Override