Typography: support deriving font from any other font (was always from defaultFont) (issue #384)

This commit is contained in:
Karl Tauber
2021-10-17 16:54:05 +02:00
parent 80297f113f
commit d34619824c
2 changed files with 39 additions and 14 deletions

View File

@@ -527,7 +527,7 @@ public abstract class FlatLaf
// use active value for all fonts to allow changing fonts in all components with:
// UIManager.put( "defaultFont", myFont );
// (this is similar as in Nimbus L&F)
Object activeFont = new ActiveFont( null, -1, 0, 0, 0, 0 );
Object activeFont = new ActiveFont( null, null, -1, 0, 0, 0, 0 );
// override fonts
for( Object key : defaults.keySet() ) {
@@ -611,7 +611,7 @@ public abstract class FlatLaf
/** @since 1.1 */
public static ActiveValue createActiveFontValue( float scaleFactor ) {
return new ActiveFont( null, -1, 0, 0, 0, scaleFactor );
return new ActiveFont( null, null, -1, 0, 0, 0, scaleFactor );
}
/**
@@ -1182,6 +1182,7 @@ public abstract class FlatLaf
static class ActiveFont
implements ActiveValue
{
private final String baseFontKey;
private final List<String> families;
private final int style;
private final int styleChange;
@@ -1191,7 +1192,9 @@ public abstract class FlatLaf
// cache (scaled/derived) font
private FontUIResource font;
private Font lastDefaultFont;
private Font lastBaseFont;
private boolean inCreateValue;
/**
* @param families list of font families, or {@code null}
@@ -1202,9 +1205,10 @@ public abstract class FlatLaf
* @param relativeSize added to size of base font, or {@code 0}
* @param scaleSize multiply size of base font, or {@code 0}
*/
ActiveFont( List<String> families, int style, int styleChange,
ActiveFont( String baseFontKey, List<String> families, int style, int styleChange,
int absoluteSize, int relativeSize, float scaleSize )
{
this.baseFontKey = baseFontKey;
this.families = families;
this.style = style;
this.styleChange = styleChange;
@@ -1215,16 +1219,30 @@ public abstract class FlatLaf
@Override
public Object createValue( UIDefaults table ) {
Font defaultFont = UIManager.getFont( "defaultFont" );
if( inCreateValue )
throw new IllegalStateException( "FlatLaf: endless recursion in font" );
// fallback (to avoid NPE in case that this is used in another Laf)
if( defaultFont == null )
defaultFont = UIManager.getFont( "Label.font" );
Font baseFont = null;
if( lastDefaultFont != defaultFont ) {
lastDefaultFont = defaultFont;
inCreateValue = true;
try {
if( baseFontKey != null )
baseFont = (Font) UIDefaultsLoader.lazyUIManagerGet( baseFontKey );
font = derive( defaultFont, fontSize -> UIScale.scale( fontSize ) );
if( baseFont == null )
baseFont = UIManager.getFont( "defaultFont" );
// fallback (to avoid NPE in case that this is used in another Laf)
if( baseFont == null )
baseFont = UIManager.getFont( "Label.font" );
} finally {
inCreateValue = false;
}
if( lastBaseFont != baseFont ) {
lastBaseFont = baseFont;
font = derive( baseFont, fontSize -> UIScale.scale( fontSize ) );
}
return font;

View File

@@ -1000,7 +1000,7 @@ class UIDefaultsLoader
}
/**
* Syntax: [normal] [bold|+bold|-bold] [italic|+italic|-italic] [<size>|+<incr>|-<decr>|<percent>%] [family[, family]]
* Syntax: [normal] [bold|+bold|-bold] [italic|+italic|-italic] [<size>|+<incr>|-<decr>|<percent>%] [family[, family]] [$baseFontKey]
*/
private static Object parseFont( String value ) {
Object font = fontCache.get( value );
@@ -1013,6 +1013,7 @@ class UIDefaultsLoader
int relativeSize = 0;
float scaleSize = 0;
List<String> families = null;
String baseFontKey = null;
// use StreamTokenizer to split string because it supports quoted strings
StreamTokenizer st = new StreamTokenizer( new StringReader( value ) );
@@ -1062,6 +1063,12 @@ class UIDefaultsLoader
scaleSize = parseInteger( param.substring( 0, param.length() - 1 ), true ) / 100f;
else
absoluteSize = parseInteger( param, true );
} else if( firstChar == '$' ) {
// reference to base font
if( baseFontKey != null )
throw new IllegalArgumentException( "baseFontKey specified more than once in '" + value + "'" );
baseFontKey = param.substring( 1 );
} else {
// font family
if( families == null )
@@ -1088,7 +1095,7 @@ class UIDefaultsLoader
throw new IllegalArgumentException( "can not use '+italic' and '-italic' in '" + value + "'" );
}
font = new FlatLaf.ActiveFont( families, style, styleChange, absoluteSize, relativeSize, scaleSize );
font = new FlatLaf.ActiveFont( baseFontKey, families, style, styleChange, absoluteSize, relativeSize, scaleSize );
fontCache.put( value, font );
return font;
}
@@ -1227,7 +1234,7 @@ class UIDefaultsLoader
* For use in LazyValue to get value for given key from UIManager and report error
* if not found. If key is prefixed by '?', then no error is reported.
*/
private static Object lazyUIManagerGet( String uiKey ) {
static Object lazyUIManagerGet( String uiKey ) {
boolean optional = false;
if( uiKey.startsWith( OPTIONAL_PREFIX ) ) {
uiKey = uiKey.substring( OPTIONAL_PREFIX.length() );