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:
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:

View File

@ -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());

View File

@ -30,6 +30,7 @@ public class CustomShapeDescription implements Iterable<Drawable> {
private HashMap<String, Pin> pins;
private ArrayList<Drawable> drawables;
private TextHolder label;
/**
* Creates a new instance
@ -154,6 +155,26 @@ public class CustomShapeDescription implements Iterable<Drawable> {
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<Drawable> {
@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)

View File

@ -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 {

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.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(" <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;
int y = 0;
@ -98,7 +106,7 @@ public class SvgTemplate implements Closeable {
for (PinDescription o : outputs) {
w.write(" <g>\n");
w.write(" <circle fill=\"" + getColor(Style.WIRE_OUT) + "\" id=\"pin:" + o.getName() + "\" cx=\"" + width + "\" cy=\"" + y + "\" r=\"3\"/>\n");
Vector labelPos = new Vector(width-4, y + yOffs);
Vector labelPos = new Vector(width - 4, y + yOffs);
w.write(" <text fill=\"" + getColor(style) + "\" font-size=\"" + (style.getFontSize() - 1) + "\" text-anchor=\"end\" x=\"" + labelPos.getX() + "\" y=\"" + labelPos.getY() + "\">" + o.getName() + "</text>\n");
w.write(" </g>\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