Adds a size attribute to the text component. See #91

This commit is contained in:
hneemann 2017-12-20 19:54:19 +01:00
parent 893283a0cd
commit d5ddf1e0a3
14 changed files with 91 additions and 26 deletions

View File

@ -6,6 +6,7 @@ import de.neemann.digital.core.arithmetic.LeftRightFormat;
import de.neemann.digital.core.io.InValue;
import de.neemann.digital.core.io.IntFormat;
import de.neemann.digital.core.memory.DataField;
import de.neemann.digital.draw.graphics.Style;
import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.model.InverterConfig;
import de.neemann.gui.Screen;
@ -57,6 +58,16 @@ public final class Keys {
public static final Key<String> LABEL
= new Key<>("Label", "");
/**
* The font size
*/
public static final Key<Integer> FONT_SIZE =
new Key.KeyInteger("textFontSize", Style.NORMAL.getFontSize())
.setComboBoxValues(new Integer[]{14, 17, 20, 24, 36, 48, 60})
.setMin(10)
.setMax(70);
/**
* The size of a LED
*/

View File

@ -81,7 +81,7 @@ public class GraphicMinMax implements Graphic {
@Override
public void drawText(Vector p1, Vector p2, String text, Orientation orientation, Style style) {
if (includeText || style == Style.NORMAL_TEXT)
if (includeText || style.mattersAlwaysForSize())
approxTextSize(this, p1, p2, text, orientation, style);
}

View File

@ -7,7 +7,7 @@ import java.awt.*;
/**
* @author hneemann
*/
public class Style {
public final class Style {
/**
* maximal line thickness
*/
@ -38,7 +38,7 @@ public class Style {
* Text which uses this style is always included in sizing!
* Used for text only elements.
*/
public static final Style NORMAL_TEXT = new Style(LINETHICK, false, Color.BLACK);
public static final Style NORMAL_TEXT = new Style(LINETHICK, false, Color.BLACK, 24, null, true);
/**
* thin line used for the graphic in the clock or delay shape
*/
@ -88,19 +88,19 @@ public class Style {
/**
* Used to draw the pin description text
*/
public static final Style SHAPE_PIN = new Style(LINETHIN, false, Color.GRAY, 18, null);
public static final Style SHAPE_PIN = new Style(LINETHIN, false, Color.GRAY, 18, null, false);
/**
* Used to draw the pin description text for splitters
*/
public static final Style SHAPE_SPLITTER = new Style(LINETHIN, false, Color.GRAY, 12, null);
public static final Style SHAPE_SPLITTER = new Style(LINETHIN, false, Color.GRAY, 12, null, false);
/**
* Used to draw the pin description text
*/
public static final Style WIRE_VALUE = new Style(LINETHICK, false, new Color(50, 162, 50), 12, null);
public static final Style WIRE_VALUE = new Style(LINETHICK, false, new Color(50, 162, 50), 12, null, false);
/**
* Used to draw the wire bit number
*/
public static final Style WIRE_BITS = new Style(LINETHIN, false, WIRE.color, 12, null);
public static final Style WIRE_BITS = new Style(LINETHIN, false, WIRE.color, 12, null, false);
/**
* highlight color used for the circles to mark an element
*/
@ -118,9 +118,10 @@ public class Style {
private final float[] dash;
private final Stroke stroke;
private final Font font;
private final boolean mattersForSize;
private Style(int thickness, boolean filled, Color color, float[] dash) {
this(thickness, filled, color, 24, dash);
this(thickness, filled, color, 24, dash, false);
}
/**
@ -130,16 +131,27 @@ public class Style {
* @param filled true if polygons needs to be filled
* @param color the color to use
*/
public Style(int thickness, boolean filled, Color color) {
this(thickness, filled, color, 24, null);
private Style(int thickness, boolean filled, Color color) {
this(thickness, filled, color, 24, null, false);
}
private Style(int thickness, boolean filled, Color color, int fontsize, float[] dash) {
/**
* Creates a new style
*
* @param thickness the line thickness
* @param filled true if polygons needs to be filled
* @param color the color to use
* @param fontsize font size
* @param dash dash intervals, null is allowed for a solid line
* @param mattersForSize always include in shape size measurement
*/
private Style(int thickness, boolean filled, Color color, int fontsize, float[] dash, boolean mattersForSize) {
this.thickness = thickness;
this.filled = filled;
this.color = color;
this.fontsize = fontsize;
this.dash = dash;
this.mattersForSize = mattersForSize;
stroke = new BasicStroke(thickness, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10f, dash, 0f);
font = new Font("Arial", Font.PLAIN, fontsize);
@ -155,7 +167,7 @@ public class Style {
/**
* @return true if polygons and circles are filled
*/
public boolean isFilled() {
boolean isFilled() {
return filled;
}
@ -190,7 +202,7 @@ public class Style {
/**
* @return the dash style
*/
public float[] getDash() {
float[] getDash() {
return dash;
}
@ -209,4 +221,36 @@ public class Style {
else return WIRE_LOW;
}
/**
* If this flag is set, the text is always to include in size estimation.
*
* @return the mattersForSize flag
*/
boolean mattersAlwaysForSize() {
return mattersForSize;
}
/**
* Creates a new style, based on the current style
*
* @param fontsize the new font size
* @param mattersForSize the mattersForSize flag
* @return Style the derived style with the given font size and mattersForSize flag.
*/
public Style deriveStyle(int fontsize, boolean mattersForSize) {
return new Style(thickness, filled, color, fontsize, dash, mattersForSize);
}
/**
* Creates a new style, based on the current style
*
* @param thickness the line thickness
* @param filled filled flag for polygons
* @param color the color
* @return the new style
*/
public Style deriveStyle(int thickness, boolean filled, Color color) {
return new Style(thickness, filled, color, fontsize, dash, mattersForSize);
}
}

View File

@ -19,7 +19,7 @@ import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
* The n-chan floating gate FET shape
*/
public class FGFETShapeN extends FETShape {
static final Style CHARGED_GATE = new Style(6, false, Color.RED);
static final Style CHARGED_GATE = Style.NORMAL.deriveStyle(6, false, Color.RED);
private final boolean programmed;

View File

@ -197,7 +197,7 @@ public class GenericShape implements Shape {
.add(1, height);
if (color != Color.WHITE)
graphic.drawPolygon(polygon, new Style(1, !graphic.isFlagSet(Graphic.LATEX), color));
graphic.drawPolygon(polygon, Style.NORMAL.deriveStyle(1, !graphic.isFlagSet(Graphic.LATEX), color));
graphic.drawPolygon(polygon, Style.NORMAL);
if (invert) {

View File

@ -39,7 +39,7 @@ public class LEDShape implements Shape {
this.inputs = inputs;
this.label = attr.getLabel();
this.size = attr.get(Keys.SIZE) * SIZE;
onStyle = new Style(1, true, attr.get(Keys.COLOR));
onStyle = Style.NORMAL.deriveStyle(1, true, attr.get(Keys.COLOR));
}
@Override

View File

@ -39,7 +39,7 @@ public class LightBulbShape implements Shape {
*/
public LightBulbShape(ElementAttributes attr, PinDescriptions inputs, PinDescriptions outputs) {
this.inputs = inputs;
style = new Style(1, true, attr.get(Keys.COLOR));
style = Style.NORMAL.deriveStyle(1, true, attr.get(Keys.COLOR));
}
@Override

View File

@ -24,8 +24,8 @@ import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
* @author hneemann
*/
public class RotEncoderShape implements Shape {
private static final Style KNOB = new Style(Style.MAXLINETHICK, true, Color.DARK_GRAY);
private static final Style MARKER = new Style(Style.MAXLINETHICK, false, Color.LIGHT_GRAY);
private static final Style KNOB = Style.NORMAL.deriveStyle(Style.MAXLINETHICK, true, Color.DARK_GRAY);
private static final Style MARKER = Style.NORMAL.deriveStyle(Style.MAXLINETHICK, false, Color.LIGHT_GRAY);
private static final Vector CENTER = new Vector(SIZE2 - SIZE * 2, SIZE2);
private final String label;
private final PinDescriptions outputs;

View File

@ -62,8 +62,8 @@ public abstract class SevenShape implements Shape {
* @param attr the attributes
*/
public SevenShape(ElementAttributes attr) {
onStyle = new Style(1, true, attr.get(Keys.COLOR));
offStyle = new Style(1, true, new Color(230, 230, 230));
onStyle = Style.NORMAL.deriveStyle(1, true, attr.get(Keys.COLOR));
offStyle = Style.NORMAL.deriveStyle(1, true, new Color(230, 230, 230));
}
@Override

View File

@ -20,7 +20,7 @@ import static de.neemann.digital.draw.shapes.GenericShape.SIZE2;
*/
public class TestCaseShape implements Shape {
private static final Style TESTSTYLE = new Style(0, true, new Color(0, 255, 0, 40));
private static final Style TESTSTYLE = Style.NORMAL.deriveStyle(0, true, new Color(0, 255, 0, 40));
private final String label;
/**

View File

@ -12,6 +12,8 @@ import de.neemann.digital.draw.graphics.Style;
import de.neemann.digital.draw.graphics.Vector;
import de.neemann.digital.lang.Lang;
import java.awt.*;
/**
* Simple text
*
@ -19,6 +21,7 @@ import de.neemann.digital.lang.Lang;
*/
public class TextShape implements Shape {
private final String text;
private final int fontSize;
/**
* Create a new instance
@ -42,6 +45,7 @@ public class TextShape implements Shape {
text = Lang.get("elem_Text");
this.text = text;
fontSize = attr.get(Keys.FONT_SIZE);
}
@Override
@ -57,13 +61,14 @@ public class TextShape implements Shape {
@Override
public void drawTo(Graphic graphic, Style highLight) {
StringBuilder sb = new StringBuilder();
Style style = Style.NORMAL.deriveStyle(fontSize, true);
Vector pos = new Vector(0, 0);
final int dy = (Style.NORMAL_TEXT.getFontSize() * 20) / 16;
final int dy = (style.getFontSize() * 20) / 16;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '\n') {
if (sb.length() > 0) {
graphic.drawText(pos, pos.add(1, 0), sb.toString(), Orientation.LEFTTOP, Style.NORMAL_TEXT);
graphic.drawText(pos, pos.add(1, 0), sb.toString(), Orientation.LEFTTOP, style);
sb.setLength(0);
}
pos = pos.add(0, dy);
@ -71,6 +76,6 @@ public class TextShape implements Shape {
sb.append(c);
}
if (sb.length() > 0)
graphic.drawText(pos, pos.add(1, 0), sb.toString(), Orientation.LEFTTOP, Style.NORMAL_TEXT);
graphic.drawText(pos, pos.add(1, 0), sb.toString(), Orientation.LEFTTOP, style);
}
}

View File

@ -27,7 +27,8 @@ public class DummyElement implements Element {
* The TextElement description
*/
public static final ElementTypeDescription TEXTDESCRIPTION = new ElementTypeDescription("Text", DummyElement.class)
.addAttribute(Keys.DESCRIPTION);
.addAttribute(Keys.DESCRIPTION)
.addAttribute(Keys.FONT_SIZE);
/**
* Creates a new dummy element

View File

@ -966,6 +966,8 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
<string name="key_inputBits_tt">Die Anzahl der Ausgangsbits muss größer sein als die Anzahl der Eingangsbits.</string>
<string name="key_outputBits">Anzahl Ausgangsbits</string><!-- BitExtender -->
<string name="key_outputBits_tt">Die Anzahl der Ausgangsbits muss größer sein als die Anzahl der Eingangsbits.</string>
<string name="key_textFontSize">Schriftgröße</string>
<string name="key_textFontSize_tt">Legt die für diesen Text zu verwendende Schriftgröße fest.</string>
<string name="mod_insertWire">Leitung eingefügt.</string>
<string name="mod_insertCopied">Aus Zwischenablage eingefügt.</string>

View File

@ -956,6 +956,8 @@ The names of the variables may not be unique.</string>
<string name="key_inputBits_tt">The number of output bits must be greater than the number of input bits.</string>
<string name="key_outputBits">Output Bit Width</string><!-- BitExtender -->
<string name="key_outputBits_tt">The number of output bits must be greater than the number of input bits.</string>
<string name="key_textFontSize">Font Size</string>
<string name="key_textFontSize_tt">Sets the font size to use for this text.</string>
<string name="mod_insertWire">Inserted wire.</string>
<string name="mod_insertCopied">Insert from clipboard.</string>