From 0afb1e17cf1559a21920d17ecb2f6d4cd79dd35a Mon Sep 17 00:00:00 2001 From: hneemann Date: Tue, 3 May 2016 22:10:39 +0200 Subject: [PATCH] added a method to calculate the text width --- .../digital/draw/graphics/GraphicMinMax.java | 17 +++++++++++++--- .../digital/draw/shapes/TunnelShape.java | 20 +++++++++---------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/neemann/digital/draw/graphics/GraphicMinMax.java b/src/main/java/de/neemann/digital/draw/graphics/GraphicMinMax.java index 6c6cbac44..58fe9998a 100644 --- a/src/main/java/de/neemann/digital/draw/graphics/GraphicMinMax.java +++ b/src/main/java/de/neemann/digital/draw/graphics/GraphicMinMax.java @@ -80,12 +80,10 @@ public class GraphicMinMax implements Graphic { */ public static void approxTextSize(Graphic gr, Vector p1, Vector p2, String text, Orientation orientation, Style style) { if (text != null && text.length() > 0) { - text = cleanLabel(text); Vector delta = p2.sub(p1).norm128(); Vector height = new Vector(delta.y, -delta.x).mul(style.getFontSize()).div(128); - Rectangle2D sb = style.getFont().getStringBounds(text, new FontRenderContext(null, true, false)); - int textWidth = (int) sb.getWidth(); + int textWidth = getTextWidth(text, style); Vector width = delta.mul(textWidth).div(128); if (p1.y == p2.y) { // 0 and 180 deg @@ -117,6 +115,19 @@ public class GraphicMinMax implements Graphic { } } + /** + * Returns a approximation of the width of the given text in the given style + * + * @param text the text + * @param style the style + * @return the approximated text width + */ + public static int getTextWidth(String text, Style style) { + text = cleanLabel(text); + Rectangle2D sb = style.getFont().getStringBounds(text, new FontRenderContext(null, true, false)); + return (int) sb.getWidth(); + } + /** * @return the upper left corner of the circuit */ diff --git a/src/main/java/de/neemann/digital/draw/shapes/TunnelShape.java b/src/main/java/de/neemann/digital/draw/shapes/TunnelShape.java index cf0325902..049eeeccb 100644 --- a/src/main/java/de/neemann/digital/draw/shapes/TunnelShape.java +++ b/src/main/java/de/neemann/digital/draw/shapes/TunnelShape.java @@ -1,6 +1,5 @@ package de.neemann.digital.draw.shapes; -import de.neemann.digital.core.ObservableValue; import de.neemann.digital.core.Observer; import de.neemann.digital.core.element.ElementAttributes; import de.neemann.digital.core.element.Keys; @@ -22,7 +21,7 @@ public class TunnelShape implements Shape { private final PinDescription input; private final String label; - private ObservableValue inValue; + //private ObservableValue inValue; /** * Creates a new instance @@ -43,19 +42,20 @@ public class TunnelShape implements Shape { @Override public InteractorInterface applyStateMonitor(IOState ioState, Observer guiObserver) { - inValue = ioState.getInput(0).addObserverToValue(guiObserver); + //inValue = ioState.getInput(0).addObserverToValue(guiObserver); return null; } @Override public void drawTo(Graphic gr, boolean highLight) { - if (inValue != null) { - Style style = Style.getWireStyle(inValue); - gr.drawPolygon(new Polygon(true) - .add(1, 0) - .add(SIZE - 1, SIZE2 - 4) - .add(SIZE - 1, -SIZE2 + 4), style); - } +// if (inValue != null) { +// Style style = Style.getWireStyle(inValue); +// gr.drawPolygon(new Polygon(true) +// .add(1, 0) +// .add(SIZE - 1, SIZE2 - 4) +// .add(SIZE - 1, -SIZE2 + 4), style); +// } + gr.drawPolygon(new Polygon(true) .add(0, 0) .add(SIZE, SIZE2 - 3)