mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-19 09:54:49 -04:00
added a method to calculate the text width
This commit is contained in:
parent
22bc5e7e92
commit
0afb1e17cf
@ -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) {
|
public static void approxTextSize(Graphic gr, Vector p1, Vector p2, String text, Orientation orientation, Style style) {
|
||||||
if (text != null && text.length() > 0) {
|
if (text != null && text.length() > 0) {
|
||||||
text = cleanLabel(text);
|
|
||||||
Vector delta = p2.sub(p1).norm128();
|
Vector delta = p2.sub(p1).norm128();
|
||||||
Vector height = new Vector(delta.y, -delta.x).mul(style.getFontSize()).div(128);
|
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 = getTextWidth(text, style);
|
||||||
int textWidth = (int) sb.getWidth();
|
|
||||||
Vector width = delta.mul(textWidth).div(128);
|
Vector width = delta.mul(textWidth).div(128);
|
||||||
|
|
||||||
if (p1.y == p2.y) { // 0 and 180 deg
|
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
|
* @return the upper left corner of the circuit
|
||||||
*/
|
*/
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package de.neemann.digital.draw.shapes;
|
package de.neemann.digital.draw.shapes;
|
||||||
|
|
||||||
import de.neemann.digital.core.ObservableValue;
|
|
||||||
import de.neemann.digital.core.Observer;
|
import de.neemann.digital.core.Observer;
|
||||||
import de.neemann.digital.core.element.ElementAttributes;
|
import de.neemann.digital.core.element.ElementAttributes;
|
||||||
import de.neemann.digital.core.element.Keys;
|
import de.neemann.digital.core.element.Keys;
|
||||||
@ -22,7 +21,7 @@ public class TunnelShape implements Shape {
|
|||||||
|
|
||||||
private final PinDescription input;
|
private final PinDescription input;
|
||||||
private final String label;
|
private final String label;
|
||||||
private ObservableValue inValue;
|
//private ObservableValue inValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance
|
* Creates a new instance
|
||||||
@ -43,19 +42,20 @@ public class TunnelShape implements Shape {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InteractorInterface applyStateMonitor(IOState ioState, Observer guiObserver) {
|
public InteractorInterface applyStateMonitor(IOState ioState, Observer guiObserver) {
|
||||||
inValue = ioState.getInput(0).addObserverToValue(guiObserver);
|
//inValue = ioState.getInput(0).addObserverToValue(guiObserver);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawTo(Graphic gr, boolean highLight) {
|
public void drawTo(Graphic gr, boolean highLight) {
|
||||||
if (inValue != null) {
|
// if (inValue != null) {
|
||||||
Style style = Style.getWireStyle(inValue);
|
// Style style = Style.getWireStyle(inValue);
|
||||||
gr.drawPolygon(new Polygon(true)
|
// gr.drawPolygon(new Polygon(true)
|
||||||
.add(1, 0)
|
// .add(1, 0)
|
||||||
.add(SIZE - 1, SIZE2 - 4)
|
// .add(SIZE - 1, SIZE2 - 4)
|
||||||
.add(SIZE - 1, -SIZE2 + 4), style);
|
// .add(SIZE - 1, -SIZE2 + 4), style);
|
||||||
}
|
// }
|
||||||
|
|
||||||
gr.drawPolygon(new Polygon(true)
|
gr.drawPolygon(new Polygon(true)
|
||||||
.add(0, 0)
|
.add(0, 0)
|
||||||
.add(SIZE, SIZE2 - 3)
|
.add(SIZE, SIZE2 - 3)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user