From ec77746a43a26e49165676a8fbf2853833720be4 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 31 May 2022 11:26:17 +0200 Subject: [PATCH] Fonts: support specifying preferred font family for easy using another font (e.g. Inter) for all components --- .../java/com/formdev/flatlaf/FlatLaf.java | 124 +++++++++++++++++- flatlaf-fonts/flatlaf-fonts-inter/README.md | 23 +++- .../flatlaf-fonts-inter/build.gradle.kts | 6 +- .../flatlaf/fonts/inter/FlatInterFont.java | 28 +++- .../flatlaf-fonts-jetbrains-mono/README.md | 17 ++- .../build.gradle.kts | 6 +- .../jetbrains_mono/FlatJetBrainsMonoFont.java | 18 +++ 7 files changed, 208 insertions(+), 14 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index 8abd2ad3..b39901ec 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -114,6 +114,11 @@ public abstract class FlatLaf private Consumer postInitialization; private List> 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 * using {@link UIManager#setLookAndFeel(javax.swing.LookAndFeel)}. @@ -631,6 +636,13 @@ public abstract class FlatLaf if( uiFont == null ) 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 // (use remove() to avoid that ActiveFont.createValue() gets invoked) Object defaultFont = defaults.remove( "defaultFont" ); @@ -1323,6 +1335,90 @@ public abstract class FlatLaf private static boolean getUIMethodInitialized; 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. + *

+ * Note: This must be invoked before 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. + *

+ * Note: This must be invoked before 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. + *

+ * Note: This must be invoked before 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. + *

+ * Note: This must be invoked before setting + * the application look and feel. + * + * @since 3 + */ + public static void setPreferredMonospacedFontFamily( String preferredMonospacedFontFamily ) { + FlatLaf.preferredMonospacedFontFamily = preferredMonospacedFontFamily; + } + //---- class FlatUIDefaults ----------------------------------------------- private class FlatUIDefaults @@ -1457,9 +1553,16 @@ public abstract class FlatLaf // create font for family 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 ) { Font font = createCompositeFont( family, newStyle, newSize ); - if( !isFallbackFont( font ) || family.equalsIgnoreCase( Font.DIALOG ) ) + if( !isFallbackFont( font ) || isDialogFamily( family ) ) return toUIResource( font ); } } @@ -1488,9 +1591,26 @@ public abstract class FlatLaf : new FontUIResource( font ); } - private boolean isFallbackFont( Font font ) { + private static boolean isFallbackFont( Font font ) { return Font.DIALOG.equalsIgnoreCase( font.getFamily() ); } + + private static boolean isDialogFamily( String family ) { + return family.equalsIgnoreCase( Font.DIALOG ); + } + + private static String preferredFamily( List 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 ------------------------------------------ diff --git a/flatlaf-fonts/flatlaf-fonts-inter/README.md b/flatlaf-fonts/flatlaf-fonts-inter/README.md index beddf3a3..71742928 100644 --- a/flatlaf-fonts/flatlaf-fonts-inter/README.md +++ b/flatlaf-fonts/flatlaf-fonts-inter/README.md @@ -4,8 +4,8 @@ Inter font This sub-project contains fonts from the Inter font family and bundles them into an easy-to-use and redistributable JAR. -**Note:** This font requires **Java 10 or later**. It is displayed too large in -Java 8 and 9. +**Note**: This font does not work correctly in older Java 8 versions (before +8u212) and in Java 9 because it is displayed way too large. Font home page: https://rsms.me/inter/ @@ -18,7 +18,8 @@ License: 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 FlatInterFont.install(); @@ -28,6 +29,16 @@ FlatInterFont.install(); 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 // basic styles new Font( FlatInterFont.FAMILY, Font.PLAIN, 12 ); @@ -48,6 +59,10 @@ new Font( FlatInterFont.FAMILY_SEMIBOLD, Font.ITALIC, 12 ); Download -------- +Not yet available. + + diff --git a/flatlaf-fonts/flatlaf-fonts-inter/build.gradle.kts b/flatlaf-fonts/flatlaf-fonts-inter/build.gradle.kts index 093be791..c55a7588 100644 --- a/flatlaf-fonts/flatlaf-fonts-inter/build.gradle.kts +++ b/flatlaf-fonts/flatlaf-fonts-inter/build.gradle.kts @@ -14,11 +14,11 @@ * limitations under the License. */ -// Version format: - +// Version format: [-] // For maven compatibility, should be in format .[.]. -// is usually '1' and should be incremented only if a new release is +// is optional and should be incremented only if a new release is // necessary, but the has not changed. -version = "3.19-1" +version = "3.19" plugins { `java-library` diff --git a/flatlaf-fonts/flatlaf-fonts-inter/src/main/java/com/formdev/flatlaf/fonts/inter/FlatInterFont.java b/flatlaf-fonts/flatlaf-fonts-inter/src/main/java/com/formdev/flatlaf/fonts/inter/FlatInterFont.java index 21cd6366..b27d3ad7 100644 --- a/flatlaf-fonts/flatlaf-fonts-inter/src/main/java/com/formdev/flatlaf/fonts/inter/FlatInterFont.java +++ b/flatlaf-fonts/flatlaf-fonts-inter/src/main/java/com/formdev/flatlaf/fonts/inter/FlatInterFont.java @@ -25,11 +25,35 @@ import java.io.InputStream; /** * The Inter font family. *

- * Note: This font requires Java 10 or later. - * It is displayed too large in Java 8 and 9. + * Note: This font does not work correctly in older Java 8 versions + * (before 8u212) and in Java 9 because it is displayed way too large. *

* Font home page: https://rsms.me/inter/
* GitHub project: https://github.com/rsms/inter + *

+ * To install the font, invoke following once (e.g. in your {@code main()} method; on AWT thread): + *

{@code
+ * FlatInterFont.install();
+ * }
+ *

+ * Use as default font: + *

{@code
+ * FlatLaf.setPreferredFontFamily( FlatInterFont.FAMILY );
+ * FlatLaf.setPreferredLightFontFamily( FlatInterFont.FAMILY_LIGHT );
+ * FlatLaf.setPreferredSemiboldFontFamily( FlatInterFont.FAMILY_SEMIBOLD );
+ * }
+ *

+ * Create fonts: + *

{@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 );
+ * }
* * @author Karl Tauber */ diff --git a/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/README.md b/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/README.md index cb47b905..fb2c620e 100644 --- a/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/README.md +++ b/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/README.md @@ -15,7 +15,8 @@ License: 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 FlatJetBrainsMonoFont.install(); @@ -25,6 +26,14 @@ FlatJetBrainsMonoFont.install(); How to use? ----------- +Use as default monospaced font: + +~~~java +FlatLaf.setPreferredMonospacedFontFamily( FlatJetBrainsMonoFont.FAMILY ); +~~~ + +Create fonts: + ~~~java // basic styles new Font( FlatJetBrainsMonoFont.FAMILY, Font.PLAIN, 12 ); @@ -37,6 +46,10 @@ new Font( FlatJetBrainsMonoFont.FAMILY, Font.BOLD | Font.ITALIC, 12 ); Download -------- +Not yet available. + + diff --git a/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/build.gradle.kts b/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/build.gradle.kts index becd9ae3..e72cabc7 100644 --- a/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/build.gradle.kts +++ b/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/build.gradle.kts @@ -14,11 +14,11 @@ * limitations under the License. */ -// Version format: - +// Version format: [-] // For maven compatibility, should be in format .[.]. -// is usually '1' and should be incremented only if a new release is +// is optional and should be incremented only if a new release is // necessary, but the has not changed. -version = "2.242-1" +version = "2.242" plugins { `java-library` diff --git a/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/src/main/java/com/formdev/flatlaf/fonts/jetbrains_mono/FlatJetBrainsMonoFont.java b/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/src/main/java/com/formdev/flatlaf/fonts/jetbrains_mono/FlatJetBrainsMonoFont.java index dbd02607..94a6ba1f 100644 --- a/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/src/main/java/com/formdev/flatlaf/fonts/jetbrains_mono/FlatJetBrainsMonoFont.java +++ b/flatlaf-fonts/flatlaf-fonts-jetbrains-mono/src/main/java/com/formdev/flatlaf/fonts/jetbrains_mono/FlatJetBrainsMonoFont.java @@ -27,6 +27,24 @@ import java.io.InputStream; *

* Font home page: https://www.jetbrains.com/mono
* GitHub project: https://github.com/JetBrains/JetBrainsMono + *

+ * To install the font, invoke following once (e.g. in your {@code main()} method; on AWT thread): + *

{@code
+ * FlatJetBrainsMonoFont.install();
+ * }
+ *

+ * Use as default monospaced font: + *

{@code
+ * FlatLaf.setPreferredMonospacedFontFamily( FlatJetBrainsMonoFont.FAMILY );
+ * }
+ *

+ * Create fonts: + *

{@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 );
+ * }
* * @author Karl Tauber */