Fonts: support specifying preferred font family for easy using another font (e.g. Inter) for all components

This commit is contained in:
Karl Tauber
2022-05-31 11:26:17 +02:00
parent 92cd6f8f34
commit ec77746a43
7 changed files with 208 additions and 14 deletions

View File

@@ -114,6 +114,11 @@ public abstract class FlatLaf
private Consumer<UIDefaults> postInitialization; private Consumer<UIDefaults> postInitialization;
private List<Function<Object, Object>> uiDefaultsGetters; private List<Function<Object, Object>> uiDefaultsGetters;
private static String preferredFontFamily;
private static String preferredLightFontFamily;
private static String preferredSemiboldFontFamily;
private static String preferredMonospacedFontFamily;
/** /**
* Sets the application look and feel to the given LaF * Sets the application look and feel to the given LaF
* using {@link UIManager#setLookAndFeel(javax.swing.LookAndFeel)}. * using {@link UIManager#setLookAndFeel(javax.swing.LookAndFeel)}.
@@ -631,6 +636,13 @@ public abstract class FlatLaf
if( uiFont == null ) if( uiFont == null )
uiFont = createCompositeFont( Font.SANS_SERIF, Font.PLAIN, 12 ); uiFont = createCompositeFont( Font.SANS_SERIF, Font.PLAIN, 12 );
// use preferred font family (if specified)
if( preferredFontFamily != null ) {
FontUIResource preferredFont = createCompositeFont( preferredFontFamily, uiFont.getStyle(), uiFont.getSize() );
if( !ActiveFont.isFallbackFont( preferredFont ) || ActiveFont.isDialogFamily( preferredFontFamily ) )
uiFont = preferredFont;
}
// get/remove "defaultFont" from defaults if set in properties files // get/remove "defaultFont" from defaults if set in properties files
// (use remove() to avoid that ActiveFont.createValue() gets invoked) // (use remove() to avoid that ActiveFont.createValue() gets invoked)
Object defaultFont = defaults.remove( "defaultFont" ); Object defaultFont = defaults.remove( "defaultFont" );
@@ -1323,6 +1335,90 @@ public abstract class FlatLaf
private static boolean getUIMethodInitialized; private static boolean getUIMethodInitialized;
private static MethodHandle getUIMethod; private static MethodHandle getUIMethod;
/**
* Returns the preferred font family to be used for (nearly) all fonts; or {@code null}.
*
* @since 3
*/
public static String getPreferredFontFamily() {
return preferredFontFamily;
}
/**
* Sets the preferred font family to be used for (nearly) all fonts.
* <p>
* <strong>Note</strong>: This must be invoked <strong>before</strong> setting
* the application look and feel.
*
* @since 3
*/
public static void setPreferredFontFamily( String preferredFontFamily ) {
FlatLaf.preferredFontFamily = preferredFontFamily;
}
/**
* Returns the preferred font family to be used for "light" fonts; or {@code null}.
*
* @since 3
*/
public static String getPreferredLightFontFamily() {
return preferredLightFontFamily;
}
/**
* Sets the preferred font family to be used for "light" fonts.
* <p>
* <strong>Note</strong>: This must be invoked <strong>before</strong> setting
* the application look and feel.
*
* @since 3
*/
public static void setPreferredLightFontFamily( String preferredLightFontFamily ) {
FlatLaf.preferredLightFontFamily = preferredLightFontFamily;
}
/**
* Returns the preferred font family to be used for "semibold" fonts; or {@code null}.
*
* @since 3
*/
public static String getPreferredSemiboldFontFamily() {
return preferredSemiboldFontFamily;
}
/**
* Sets the preferred font family to be used for "semibold" fonts.
* <p>
* <strong>Note</strong>: This must be invoked <strong>before</strong> setting
* the application look and feel.
*
* @since 3
*/
public static void setPreferredSemiboldFontFamily( String preferredSemiboldFontFamily ) {
FlatLaf.preferredSemiboldFontFamily = preferredSemiboldFontFamily;
}
/**
* Returns the preferred font family to be used for monospaced fonts; or {@code null}.
*
* @since 3
*/
public static String getPreferredMonospacedFontFamily() {
return preferredMonospacedFontFamily;
}
/**
* Sets the preferred font family to be used for monospaced fonts.
* <p>
* <strong>Note</strong>: This must be invoked <strong>before</strong> setting
* the application look and feel.
*
* @since 3
*/
public static void setPreferredMonospacedFontFamily( String preferredMonospacedFontFamily ) {
FlatLaf.preferredMonospacedFontFamily = preferredMonospacedFontFamily;
}
//---- class FlatUIDefaults ----------------------------------------------- //---- class FlatUIDefaults -----------------------------------------------
private class FlatUIDefaults private class FlatUIDefaults
@@ -1457,9 +1553,16 @@ public abstract class FlatLaf
// create font for family // create font for family
if( families != null && !families.isEmpty() ) { if( families != null && !families.isEmpty() ) {
String preferredFamily = preferredFamily( families );
if( preferredFamily != null ) {
Font font = createCompositeFont( preferredFamily, newStyle, newSize );
if( !isFallbackFont( font ) || isDialogFamily( preferredFamily ) )
return toUIResource( font );
}
for( String family : families ) { for( String family : families ) {
Font font = createCompositeFont( family, newStyle, newSize ); Font font = createCompositeFont( family, newStyle, newSize );
if( !isFallbackFont( font ) || family.equalsIgnoreCase( Font.DIALOG ) ) if( !isFallbackFont( font ) || isDialogFamily( family ) )
return toUIResource( font ); return toUIResource( font );
} }
} }
@@ -1488,9 +1591,26 @@ public abstract class FlatLaf
: new FontUIResource( font ); : new FontUIResource( font );
} }
private boolean isFallbackFont( Font font ) { private static boolean isFallbackFont( Font font ) {
return Font.DIALOG.equalsIgnoreCase( font.getFamily() ); return Font.DIALOG.equalsIgnoreCase( font.getFamily() );
} }
private static boolean isDialogFamily( String family ) {
return family.equalsIgnoreCase( Font.DIALOG );
}
private static String preferredFamily( List<String> families ) {
for( String family : families ) {
family = family.toLowerCase( Locale.ENGLISH );
if( family.endsWith( " light" ) || family.endsWith( "-thin" ) )
return preferredLightFontFamily;
if( family.endsWith( " semibold" ) || family.endsWith( "-medium" ) )
return preferredSemiboldFontFamily;
if( family.equals( "monospaced" ) )
return preferredMonospacedFontFamily;
}
return null;
}
} }
//---- class ImageIconUIResource ------------------------------------------ //---- class ImageIconUIResource ------------------------------------------

View File

@@ -4,8 +4,8 @@ Inter font
This sub-project contains fonts from the Inter font family and bundles them into This sub-project contains fonts from the Inter font family and bundles them into
an easy-to-use and redistributable JAR. an easy-to-use and redistributable JAR.
**Note:** This font requires **Java 10 or later**. It is displayed too large in **Note**: This font does not work correctly in older Java 8 versions (before
Java 8 and 9. 8u212) and in Java 9 because it is displayed way too large.
Font home page: https://rsms.me/inter/ Font home page: https://rsms.me/inter/
@@ -18,7 +18,8 @@ License:
How to install? How to install?
--------------- ---------------
Invoke the `install()` method once in your `main()` method (on AWT thread): Invoke the `install()` method once (e.g. in your `main()` method; on AWT
thread):
~~~java ~~~java
FlatInterFont.install(); FlatInterFont.install();
@@ -28,6 +29,16 @@ FlatInterFont.install();
How to use? How to use?
----------- -----------
Use as default font:
~~~java
FlatLaf.setPreferredFontFamily( FlatInterFont.FAMILY );
FlatLaf.setPreferredLightFontFamily( FlatInterFont.FAMILY_LIGHT );
FlatLaf.setPreferredSemiboldFontFamily( FlatInterFont.FAMILY_SEMIBOLD );
~~~
Create fonts:
~~~java ~~~java
// basic styles // basic styles
new Font( FlatInterFont.FAMILY, Font.PLAIN, 12 ); new Font( FlatInterFont.FAMILY, Font.PLAIN, 12 );
@@ -48,6 +59,10 @@ new Font( FlatInterFont.FAMILY_SEMIBOLD, Font.ITALIC, 12 );
Download Download
-------- --------
Not yet available.
<!--
FlatLaf Fonts binaries are available on **Maven Central**. FlatLaf Fonts binaries are available on **Maven Central**.
If you use Maven or Gradle, add a dependency with following coordinates to your If you use Maven or Gradle, add a dependency with following coordinates to your
@@ -60,3 +75,5 @@ build script:
Otherwise download `flatlaf-fonts-inter-<version>.jar` here: Otherwise download `flatlaf-fonts-inter-<version>.jar` here:
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.formdev/flatlaf-fonts-inter/badge.svg?style=flat-square&color=007ec6)](https://maven-badges.herokuapp.com/maven-central/com.formdev/flatlaf-fonts-inter) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.formdev/flatlaf-fonts-inter/badge.svg?style=flat-square&color=007ec6)](https://maven-badges.herokuapp.com/maven-central/com.formdev/flatlaf-fonts-inter)
-->

View File

@@ -14,11 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
// Version format: <font-version>-<build-number> // Version format: <font-version>[-<build-number>]
// For maven compatibility, <font-version> should be in format <major>.<minor>[.<micro>]. // For maven compatibility, <font-version> should be in format <major>.<minor>[.<micro>].
// <build-number> is usually '1' and should be incremented only if a new release is // <build-number> is optional and should be incremented only if a new release is
// necessary, but the <font-version> has not changed. // necessary, but the <font-version> has not changed.
version = "3.19-1" version = "3.19"
plugins { plugins {
`java-library` `java-library`

View File

@@ -25,11 +25,35 @@ import java.io.InputStream;
/** /**
* The Inter font family. * The Inter font family.
* <p> * <p>
* <strong>Note:</strong> This font requires <strong>Java 10 or later</strong>. * <strong>Note</strong>: This font does not work correctly in older Java 8 versions
* It is displayed too large in Java 8 and 9. * (before 8u212) and in Java 9 because it is displayed way too large.
* <p> * <p>
* Font home page: <a href="https://rsms.me/inter/">https://rsms.me/inter/</a><br> * Font home page: <a href="https://rsms.me/inter/">https://rsms.me/inter/</a><br>
* GitHub project: <a href="https://github.com/rsms/inter">https://github.com/rsms/inter</a> * GitHub project: <a href="https://github.com/rsms/inter">https://github.com/rsms/inter</a>
* <p>
* To install the font, invoke following once (e.g. in your {@code main()} method; on AWT thread):
* <pre>{@code
* FlatInterFont.install();
* }</pre>
* <p>
* Use as default font:
* <pre>{@code
* FlatLaf.setPreferredFontFamily( FlatInterFont.FAMILY );
* FlatLaf.setPreferredLightFontFamily( FlatInterFont.FAMILY_LIGHT );
* FlatLaf.setPreferredSemiboldFontFamily( FlatInterFont.FAMILY_SEMIBOLD );
* }</pre>
* <p>
* Create fonts:
* <pre>{@code
* new Font( FlatInterFont.FAMILY, Font.PLAIN, 12 );
* new Font( FlatInterFont.FAMILY, Font.ITALIC, 12 );
* new Font( FlatInterFont.FAMILY, Font.BOLD, 12 );
* new Font( FlatInterFont.FAMILY, Font.BOLD | Font.ITALIC, 12 );
* new Font( FlatInterFont.FAMILY_LIGHT, Font.PLAIN, 12 );
* new Font( FlatInterFont.FAMILY_LIGHT, Font.ITALIC, 12 );
* new Font( FlatInterFont.FAMILY_SEMIBOLD, Font.PLAIN, 12 );
* new Font( FlatInterFont.FAMILY_SEMIBOLD, Font.ITALIC, 12 );
* }</pre>
* *
* @author Karl Tauber * @author Karl Tauber
*/ */

View File

@@ -15,7 +15,8 @@ License:
How to install? How to install?
--------------- ---------------
Invoke the `install()` method once in your `main()` method (on AWT thread): Invoke the `install()` method once (e.g. in your `main()` method; on AWT
thread):
~~~java ~~~java
FlatJetBrainsMonoFont.install(); FlatJetBrainsMonoFont.install();
@@ -25,6 +26,14 @@ FlatJetBrainsMonoFont.install();
How to use? How to use?
----------- -----------
Use as default monospaced font:
~~~java
FlatLaf.setPreferredMonospacedFontFamily( FlatJetBrainsMonoFont.FAMILY );
~~~
Create fonts:
~~~java ~~~java
// basic styles // basic styles
new Font( FlatJetBrainsMonoFont.FAMILY, Font.PLAIN, 12 ); new Font( FlatJetBrainsMonoFont.FAMILY, Font.PLAIN, 12 );
@@ -37,6 +46,10 @@ new Font( FlatJetBrainsMonoFont.FAMILY, Font.BOLD | Font.ITALIC, 12 );
Download Download
-------- --------
Not yet available.
<!--
FlatLaf Fonts binaries are available on **Maven Central**. FlatLaf Fonts binaries are available on **Maven Central**.
If you use Maven or Gradle, add a dependency with following coordinates to your If you use Maven or Gradle, add a dependency with following coordinates to your
@@ -49,3 +62,5 @@ build script:
Otherwise download `flatlaf-fonts-jetbrains-mono-<version>.jar` here: Otherwise download `flatlaf-fonts-jetbrains-mono-<version>.jar` here:
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.formdev/flatlaf-fonts-jetbrains-mono/badge.svg?style=flat-square&color=007ec6)](https://maven-badges.herokuapp.com/maven-central/com.formdev/flatlaf-fonts-jetbrains-mono) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.formdev/flatlaf-fonts-jetbrains-mono/badge.svg?style=flat-square&color=007ec6)](https://maven-badges.herokuapp.com/maven-central/com.formdev/flatlaf-fonts-jetbrains-mono)
-->

View File

@@ -14,11 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
// Version format: <font-version>-<build-number> // Version format: <font-version>[-<build-number>]
// For maven compatibility, <font-version> should be in format <major>.<minor>[.<micro>]. // For maven compatibility, <font-version> should be in format <major>.<minor>[.<micro>].
// <build-number> is usually '1' and should be incremented only if a new release is // <build-number> is optional and should be incremented only if a new release is
// necessary, but the <font-version> has not changed. // necessary, but the <font-version> has not changed.
version = "2.242-1" version = "2.242"
plugins { plugins {
`java-library` `java-library`

View File

@@ -27,6 +27,24 @@ import java.io.InputStream;
* <p> * <p>
* Font home page: <a href="https://www.jetbrains.com/mono">https://www.jetbrains.com/mono</a><br> * Font home page: <a href="https://www.jetbrains.com/mono">https://www.jetbrains.com/mono</a><br>
* GitHub project: <a href="https://github.com/JetBrains/JetBrainsMono">https://github.com/JetBrains/JetBrainsMono</a> * GitHub project: <a href="https://github.com/JetBrains/JetBrainsMono">https://github.com/JetBrains/JetBrainsMono</a>
* <p>
* To install the font, invoke following once (e.g. in your {@code main()} method; on AWT thread):
* <pre>{@code
* FlatJetBrainsMonoFont.install();
* }</pre>
* <p>
* Use as default monospaced font:
* <pre>{@code
* FlatLaf.setPreferredMonospacedFontFamily( FlatJetBrainsMonoFont.FAMILY );
* }</pre>
* <p>
* Create fonts:
* <pre>{@code
* new Font( FlatJetBrainsMonoFont.FAMILY, Font.PLAIN, 12 );
* new Font( FlatJetBrainsMonoFont.FAMILY, Font.ITALIC, 12 );
* new Font( FlatJetBrainsMonoFont.FAMILY, Font.BOLD, 12 );
* new Font( FlatJetBrainsMonoFont.FAMILY, Font.BOLD | Font.ITALIC, 12 );
* }</pre>
* *
* @author Karl Tauber * @author Karl Tauber
*/ */