Font Weights
Dirk Lemmermann
dlemmermann at gmail.com
Thu May 29 18:33:54 UTC 2025
I think what I really want is that this feature „simply“ works as documented. I have not looked at the implementation of the font support, yet, but it feels like this shouldn’t be too hard to implement correctly. If somebody knows more about the problem I would love to hear about it.
The official JavaFX CSS reference documentation at https://openjfx.io/javadoc/24/javafx.graphics/javafx/scene/doc-files/cssref.html lists font weights (integers) as possible values:
<font-weight> The font's weight, using the following syntax:
[ normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 ]
If this will never be supported then I think the docs need to reflect that.
Again, the main benefit of this working properly would be that applications wouldn’t have to use the workaround anymore where the font family name will be used directly in all the places where a piece of text has a different weight than the default one.
Dirk
> Am 29.05.2025 um 20:03 schrieb John Hendrikx <john.hendrikx at gmail.com>:
>
>
>
> On 29/05/2025 18:23, Dirk Lemmermann wrote:
>> Hi everyone,
>>
>> Is there any chance we can get font weight working properly so that I can use a font with medium boldness and use it by declaring: -fx-font-weight: bolder; or by declaring -fx-font-weight: 600;? I know I can work around it by using the font family name, e.g. „Rubik Medium“ but this makes it impossible to replace the font at runtime, which is a requirement I am facing right now in order to support users with dyslexia. I was also considering using a variable but this is not supported for font family names (e,g, „-my-font; „Rubik Medium“).
> Just curious, how would a variable help here for the problem having the font selectable at runtime?
>> Any other work arounds I am missing?
> I don't have any easy solutions here, and I'm unsure what is involved in actually making -fx-font-weight work better.
>
> So, all I can offer is something very ugly like having the user select a font, then generating a CSS file, and setting that on the root node; if you keep the styles for font selection separate it may be doable as you'd probably could get away with just replacing a single stylesheet then (ie. mark a TextField with ".font-large" specifically and define what that means in the custom CSS file). This is a bit anti-CSS as you'd prefer to mark such nodes only with their intended function, and determine a suitable font based on that, but styles in FX don't compose.
>
> If you're willing to go in the direction of generating all CSS files based on LESS or SCSS (at runtime mind you, I hate doing this during a build), more is possible:
>
> What I've been doing myself (although I don't allow runtime selection) is to allow users to modify CSS files; using LESS or SCSS one can neatly pack all font related stuff into a single file, so I've sort of solved this by having a `fonts.less` (see below). I then mark styles with one of the custom styles (.light, .regular, etc) to indicate what final font they'll be using. However, to make this work even at run time, I'd have to regenerate all derived CSS files (but as I said, I can do this at runtime anyway) -- so the effort is in then primarily in auto-generating a base fonts CSS file given some user selection, then replacing all stylesheets that were set throughout the application (or just restarting the application).
>
> For now a user could do this manually by only editing `fonts.less` and then (re)starting the application.
>
> Here is the fonts.less file I was talking about (note: there is also a Linux variant as fonts work differently on different platforms as well... perhaps MacOS needs a modified one also, but I never tried there)
>
> fonts.less:
>
> /*
> * The Noto Sans font supports many variations. To get the correct variation
> * in JavaFX, select them as follows:
> *
> * - Black = Noto Sans Blk
> * - Bold = Noto Sans + font-weight: bold
> * - Semi Bold = Noto Sans SemBd
> * - Medium = Noto Sans Med
> * - Regular = Noto Sans + font-weight: normal
> * - Light = Noto Sans Light
> *
> * Note that these names are for Windows. Other platforms can use
> * slightly different names.
> */
>
> .light {
> -fx-font-family: "Noto Sans Light";
> -fx-font-weight: normal;
> }
>
> .regular {
> -fx-font-family: "Noto Sans";
> -fx-font-weight: normal;
> }
>
> .medium {
> -fx-font-family: "Noto Sans Med";
> -fx-font-weight: bold;
> }
>
> .semi-bold {
> -fx-font-family: "Noto Sans SemBd";
> -fx-font-weight: bold;
> }
>
> .bold {
> -fx-font-family: "Noto Sans";
> -fx-font-weight: bold;
> }
>
> .black {
> -fx-font-family: "Noto Sans Blk";
> -fx-font-weight: bold;
> }
>
> fonts-linux.less:
>
> .light {
> -fx-font-family: "Noto Sans Light";
> -fx-font-weight: normal;
> }
>
> .regular {
> -fx-font-family: "Noto Sans";
> -fx-font-weight: normal;
> }
>
> .medium {
> -fx-font-family: "Noto Sans Medium";
> -fx-font-weight: normal;
> }
>
> .semi-bold {
> -fx-font-family: "Noto Sans SemiBold";
> -fx-font-weight: normal;
> }
>
> .bold {
> -fx-font-family: "Noto Sans";
> -fx-font-weight: bold;
> }
>
> .black {
> -fx-font-family: "Noto Sans Black";
> -fx-font-weight: normal;
> }
>
> And using it for some arbitrary style is then done (also with LESS) like:
>
> .style-p3-extra-light {
> -fx-font-size: 15;
> .light;
> }
>
> Not what you hoped for I think :)
>
> --John
>
>>
>> Dirk
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20250529/76c81946/attachment-0001.htm>
More information about the openjfx-dev
mailing list