Dave Jarvis' Repositories

git clone /repo/kmcaster.git

Round the scaled font down to guarantee fit

Author DaveJarvis <email>
Date 2020-07-19 13:30:51 GMT-0700
Commit f5efe1dcb73a62828c1cadbe229de1d4fdb2a459
Parent 5e7b425
Delta 13 lines added, 17 lines removed, 4-line decrease
src/main/com/whitemagicsoftware/kmcaster/EventHandler.java
public class EventHandler implements PropertyChangeListener {
- private static final Font DEFAULT_FONT = new Font( "DejaVu Sans", BOLD, 12 );
- private static final Color COLOUR_LABEL = new Color( 33, 33, 33 );
+ private static final Font LABEL_FONT = new Font( "DejaVu Sans", BOLD, 12 );
+ private static final Color LABEL_COLOUR = new Color( 33, 33, 33 );
private final HardwareImages mHardwareImages;
final HardwareState state, final String value ) {
if( state.isModifier() ) {
- //final var pressed = parseBoolean( value );
- //System.out.println( "Modifier pressed: " + pressed );
+ System.out.println( parseBoolean( value ) );
}
else {
final var label = new JLabel( text );
- label.setFont( DEFAULT_FONT );
+ label.setFont( LABEL_FONT );
label.setSize( width, height );
- label.setForeground( COLOUR_LABEL );
- //label.setBorder( BorderFactory.createLineBorder( Color.RED ) );
+ label.setForeground( LABEL_COLOUR );
- final var scaledFont = scale( label, r, graphics );
+ final var scaledFont = scaleFont( label, r, graphics );
label.setFont( scaledFont );
* string within the bounds of the given {@link Rectangle}.
*/
- private Font scale(
+ public Font scaleFont(
final JLabel label, final Rectangle dst, final Graphics graphics ) {
assert label != null;
float scaledPt = scaledFont.getSize();
- while( maxSizePt - minSizePt > 2 ) {
+ while( maxSizePt - minSizePt > 1f ) {
scaledFont = scaledFont.deriveFont( scaledPt );
-// final var layout = new TextLayout( text, scaledFont, frc );
-// final float fontWidthPx = layout.getVisibleAdvance();
- final var fm = graphics.getFontMetrics(scaledFont);
- final float fontWidthPx = (float)fm.getStringBounds( text, graphics ).getWidth();
+ final var layout = new TextLayout( text, scaledFont, frc );
+ final var fontWidthPx = layout.getVisibleAdvance();
final var metrics = scaledFont.getLineMetrics( text, frc );
- final float fontHeightPx = metrics.getHeight();
+ final var fontHeightPx = metrics.getHeight();
if( (fontWidthPx > dstWidthPx) || (fontHeightPx > dstHeightPx) ) {
}
- return scaledFont;
+ return scaledFont.deriveFont( (float) Math.floor( scaledPt ) );
}
}
src/main/com/whitemagicsoftware/kmcaster/HardwareComponent.java
/**
* Repaints this component by changing its mutable state. The new state
- * must have been previously registered by caling {@link #put(Object, Image)}.
+ * must have been previously registered via {@link #put(Object, Image)}.
*
* @param state The new state.