JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.42k stars 272 forks source link

Font support in properties files #384

Closed ebourg closed 3 years ago

ebourg commented 3 years ago

If I'm not mistaken the properties file used to customize FlatLaf doesn't support fonts. Would that make sense to add it ?

DevCharly commented 3 years ago

Yes, that is missing. I simply did not need it so far...

I plan to add some kind of "typography" support to FlatLaf 2.0 that should make it easier to use different fonts in a consistent way. E.g. larger fonts for titles/headings or smaller fonts for descriptions or inline help. E.g.:

Also for CSS styling (PR #341) it would be nice to be able to change the font.

So, it will come...

DevCharly commented 3 years ago

Fonts are now supported in properties files (in main branch/snapshots).

Documentation is here: https://www.formdev.com/flatlaf/properties-files/#font

And here comes typography: PR #396 😉

ebourg commented 3 years ago

Really nice and well thought out, thank you. I wouldn't have bothered with the +/- modifiers on the style, but that's nice to have.

It could be further refined by supporting different font weights, for example :

defaultFont = 13 semibold Arial

would translate to:

    Font font = new Font("Arial", Font.PLAIN, 13);
    Map<TextAttribute, Object> attributes = new HashMap<>();
    attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
    font = font.deriveFont(attributes);
DevCharly commented 3 years ago

I wouldn't have bothered with the +/- modifiers on the style

Yes, I know. Had implemented +/- size and thought this would be also a good idea for style, but after finishing implementation I realized that this is more or less useless because the base font is usually plain...

Regarding semibold, this would be great, would love to use semibold for headers, but I tried this in the past and now again and I can not see any visual difference (on Windows 10; Java 8, 16 and 17). semibold looks exactly the same as plain. Does this work on your system?

ebourg commented 3 years ago

I haven't tried, but I guess this requires that font variants of various weights are registered with something like this:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ClassLoader loader = getClass().getClassLoader();
for (String variant : new String[] { "Light", "Regular", "SemiBold", "Medium", "Bold", "ExtraBold"}) {
    ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, loader.getResourceAsStream("Foo-" + variant + ".ttf")));
}
DevCharly commented 3 years ago

Hmm, maybe, but I'm not confident...

Looking at the result of GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(), font "Segoe UI Bold" is assigned to family "Segoe UI",

java.awt.Font[family=Segoe UI,name=Segoe UI Bold,style=plain,size=1]

but font "Segoe UI Semibold" is in its own family:

java.awt.Font[family=Segoe UI Semibold,name=Segoe UI Semibold,style=plain,size=1]

So it seems that AWT/Swing does not assign Semibold (or Light, Semilight, ...) fonts to the correct family.

Anyway, this works: 😃

Font font = new Font( "Segoe UI Semibold", Font.PLAIN, 12 );

Works also in properties files: 😄

h1.font = +12 "Segoe UI Semibold"
DevCharly commented 3 years ago

There is a very simple reason that getting semibold (or any other weight) font via Font.deriveFont( Map attributes ) does not work. It is not implemented.

The weight is only checked for >= 2f to set bold flag. See: https://github.com/openjdk/jdk/blob/bfcf6a29a16bc12d77a897fbec304868957c3188/src/java.desktop/share/classes/java/awt/Font.java#L691 and https://github.com/openjdk/jdk/blob/bfcf6a29a16bc12d77a897fbec304868957c3188/src/java.desktop/share/classes/java/awt/Font.java#L790

Often it is very useful to debug into JRE functions... 😄

ebourg commented 3 years ago

Haha good catch 👍