Font Weights
Philip Race
philip.race at oracle.com
Thu May 29 23:09:45 UTC 2025
https://bugs.openjdk.org/browse/JDK-8090423
but part of it is that we aren't doing extended families - clearer with
this one
https://bugs.openjdk.org/browse/JDK-8344037
So a chunk of work.
And here's the bug to report the weights
https://bugs.openjdk.org/browse/JDK-8092191
-phil.
On 5/29/25 3:43 PM, Philip Race wrote:
> There's probably a bug on this but I think the font look up code may
> need some work
>
> I think the CSS code may be using family + weight but then it reaches
> com/sun/javafx/font/PrismFontLoader.java
>
> // REMIND. Some day need to have better granularity.
>
> boolean bold = weight != null &&
> weight.ordinal() >= FontWeight.BOLD.ordinal();
>
> And we don't have an API to return the FontWeight so you can't figure
> it out for yourself.
> I really should fix all of the above.
>
> Is the CSS code part all good though ?
>
> Looking at CssParser I'm not sure what its doing is the intention of
> bolder and lighter
> } else if ("bolder".equals(ident)) {
> weight = FontWeight.BOLD.name();
> } else if ("lighter".equals(ident)) {
> weight = FontWeight.LIGHT.name();
>
> I assume they are meant to say find something *relative*, not *absolute*
> Perhaps in the absence of the API to find the real weight it wasn't
> implementable ?
>
> -phil
>
> On 5/29/25 11:33 AM, Dirk Lemmermann wrote:
>> 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/1728d270/attachment-0001.htm>
More information about the openjfx-dev
mailing list