Add extra spacing for stability information text

The GlyphVector.getVisualBounds() does not include leading or trailing
spaces. Account for this when drawing stability information by
increasing the unitwidth by the length of one space.

Additionally, the code is inefficient in that it creates a new Font
object each and every time a new text string is created for graphical
display. This is unnecessary and adds overhead to the VM memory
requirements.

Fixes #880

Signed-off-by: Billy Olsen <billy.olsen@gmail.com>
This commit is contained in:
Billy Olsen 2021-02-01 20:27:43 -07:00
parent 1942f70b67
commit cde986136e

View File

@ -5,6 +5,7 @@ import static net.sf.openrocket.util.Chars.THETA;
import java.awt.Color; import java.awt.Color;
import java.awt.Font; import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.font.GlyphVector; import java.awt.font.GlyphVector;
@ -34,9 +35,8 @@ public class RocketInfo implements FigureElement {
private static final int MARGIN = 8; private static final int MARGIN = 8;
// Font to use // Font to use
private static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 11); private Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 11);
private static final Font SMALLFONT = new Font(Font.SANS_SERIF, Font.PLAIN, 9); private Font smallFont = new Font(Font.SANS_SERIF, Font.PLAIN, 9);
private final Caret cpCaret = new CPCaret(0,0); private final Caret cpCaret = new CPCaret(0,0);
private final Caret cgCaret = new CGCaret(0,0); private final Caret cgCaret = new CGCaret(0,0);
@ -62,9 +62,6 @@ public class RocketInfo implements FigureElement {
private float x1, x2, y1, y2; private float x1, x2, y1, y2;
public RocketInfo(FlightConfiguration configuration) { public RocketInfo(FlightConfiguration configuration) {
this.configuration = configuration; this.configuration = configuration;
this.stabilityUnits = UnitGroup.stabilityUnits(configuration); this.stabilityUnits = UnitGroup.stabilityUnits(configuration);
@ -79,9 +76,12 @@ public class RocketInfo implements FigureElement {
@Override @Override
public void paint(Graphics2D myG2, double scale, Rectangle visible) { public void paint(Graphics2D myG2, double scale, Rectangle visible) {
this.g2 = myG2; this.g2 = myG2;
this.line = FONT.getLineMetrics("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
this.updateFontSizes();
this.line = font.getLineMetrics("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
myG2.getFontRenderContext()).getHeight() + myG2.getFontRenderContext()).getHeight() +
FONT.getLineMetrics("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", font.getLineMetrics("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
myG2.getFontRenderContext()).getDescent(); myG2.getFontRenderContext()).getDescent();
x1 = visible.x + MARGIN; x1 = visible.x + MARGIN;
@ -145,9 +145,6 @@ public class RocketInfo implements FigureElement {
this.calculatingData = calc; this.calculatingData = calc;
} }
private void drawMainInfo() { private void drawMainInfo() {
GlyphVector name = createText(configuration.getRocket().getName()); GlyphVector name = createText(configuration.getRocket().getName());
GlyphVector lengthLine = createText( GlyphVector lengthLine = createText(
@ -193,19 +190,21 @@ public class RocketInfo implements FigureElement {
at += " "+THETA+"=" + UnitGroup.UNITS_ANGLE.getDefaultUnit().toStringUnit(theta); at += " "+THETA+"=" + UnitGroup.UNITS_ANGLE.getDefaultUnit().toStringUnit(theta);
} }
GlyphVector cgValue = createText( GlyphVector cgValue = createText(getCg());
getCg()); GlyphVector cpValue = createText(getCp());
GlyphVector cpValue = createText( GlyphVector stabValue = createText(getStability());
getCp());
GlyphVector stabValue = createText(
getStability());
//// CG: //// CG:
GlyphVector cgText = createText(trans.get("RocketInfo.cgText") +" "); GlyphVector cgText = createText(trans.get("RocketInfo.cgText"));
//// CP: //// CP:
GlyphVector cpText = createText(trans.get("RocketInfo.cpText") +" "); GlyphVector cpText = createText(trans.get("RocketInfo.cpText"));
//// Stability: //// Stability:
GlyphVector stabText = createText(trans.get("RocketInfo.stabText") + " "); GlyphVector stabText = createText(trans.get("RocketInfo.stabText"));
GlyphVector atText = createSmallText(at); GlyphVector atText = createSmallText(at);
// GlyphVector visual bounds drops the spaces, so we'll add them
FontMetrics fontMetrics = g2.getFontMetrics(cgText.getFont());
int spaceWidth = fontMetrics.stringWidth(" ");
Rectangle2D cgRect = cgValue.getVisualBounds(); Rectangle2D cgRect = cgValue.getVisualBounds();
Rectangle2D cpRect = cpValue.getVisualBounds(); Rectangle2D cpRect = cpValue.getVisualBounds();
@ -215,10 +214,11 @@ public class RocketInfo implements FigureElement {
Rectangle2D stabTextRect = stabText.getVisualBounds(); Rectangle2D stabTextRect = stabText.getVisualBounds();
Rectangle2D atTextRect = atText.getVisualBounds(); Rectangle2D atTextRect = atText.getVisualBounds();
double unitWidth = MathUtil.max(cpRect.getWidth(), cgRect.getWidth(), double unitWidth = MathUtil.max(cpRect.getWidth(), cgRect.getWidth(), stabRect.getWidth());
stabRect.getWidth());
double textWidth = Math.max(cpTextRect.getWidth(), cgTextRect.getWidth()); double textWidth = Math.max(cpTextRect.getWidth(), cgTextRect.getWidth());
// Add an extra space worth of width so the text doesn't run into the values
unitWidth = unitWidth + spaceWidth;
g2.setColor(Color.BLACK); g2.setColor(Color.BLACK);
@ -438,16 +438,23 @@ public class RocketInfo implements FigureElement {
return 3*line; return 3*line;
} }
private synchronized void updateFontSizes() {
float size = Application.getPreferences().getRocketInfoFontSize();
// No change necessary as the font is the same size, just use the existing version
if (font.getSize2D() == size) {
return;
}
// Update the font sizes to whatever the currently selected font size is
font = font.deriveFont(size);
smallFont = smallFont.deriveFont((float)(size - 2.0));
}
private GlyphVector createText(String text) { private GlyphVector createText(String text) {
float size=Application.getPreferences().getRocketInfoFontSize(); return font.createGlyphVector(g2.getFontRenderContext(), text);
return (FONT.deriveFont(size)).createGlyphVector(g2.getFontRenderContext(), text);
} }
private GlyphVector createSmallText(String text) { private GlyphVector createSmallText(String text) {
float size=(float) (Application.getPreferences().getRocketInfoFontSize()-2.0); return smallFont.createGlyphVector(g2.getFontRenderContext(), text);
return (SMALLFONT.deriveFont(size)).createGlyphVector(g2.getFontRenderContext(), text);
} }
public void setCurrentConfig(FlightConfiguration newConfig) { public void setCurrentConfig(FlightConfiguration newConfig) {