added a method to calculate the text width

This commit is contained in:
hneemann 2016-05-03 22:10:39 +02:00
parent 22bc5e7e92
commit 0afb1e17cf
2 changed files with 24 additions and 13 deletions

View File

@ -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
*/

View File

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