For fonts with localized names, JavaFX cannot find the font by the default name on Windows
Philip Race
philip.race at oracle.com
Mon Jul 14 19:57:37 UTC 2025
https://bugs.openjdk.org/browse/JDK-8362197
-phil
On 7/14/25 12:47 PM, Philip Race wrote:
> Font.font will, on Windows, use IDWriteFontCollection::FindFamilyName(..)
> The docs for that appear to be silent on whether the matching process
> will check all localized names,
> but it sounds like it must not. I don't see an alternative look up
> API, such as one that accepts a locale arg.
>
> https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefontcollection
>
>
> It seems like the app (which in this case means the FX implementation)
> will have to do this itself which is going to be tedious.
>
> We would need to compare with every localized name of every font on
> the system looking for a match.
>
> And one annoying aspect of this is that until you've done that
> exhaustive search you don't
> know if the name the application supplied is present at all on the system.
>
> How would you know that someone mis-spelled Arial as Ariel and not
> that Ariel is the German localized name for Arial ?
>
> So failed lookups will be slow.
>
> In Java 2D we already do this but I'd have hoped DW used by FX was
> better than this than GDI used by 2D.
>
> Also note that
> (1) fixing this for DW wouldn't help Linux or Mac so there'd need to
> be separate implementations if they also don't do it automatically
> (2) There isn't any FX API which lets you enumerate or access
> localized names, so as you note, that also is an issue.
> Although I'm actually a little surprised FX finds 幼圆 but reports
> YouYuan. I would have thought it would be consistent.
>
> I'm also a little surprised that it has taken this long for anyone to
> even implicitly ask for FX to support localized font names.
> Java2D has had this support for a very long time.
>
> -phil.
>
> On 7/12/25 4:18 AM, Glavo wrote:
>> Hi,
>>
>> We recently noticed a problem: For fonts with localized names,
>> Font.font(String) can only find the font based on the localized name
>> in the current locale.
>>
>> For example, the Chinese version of Windows comes with a font called
>> "YouYuan", and its Chinese name is "幼圆".
>> When the system language is Chinese, JavaFX has the following behaviors:
>>
>> jshell> Font.font("YouYuan")
>> $2 ==> Font[name=System Regular, family=System, style=Regular,
>> size=13.333333015441895]
>>
>> jshell> Font.font("幼圆")
>> $3 ==> Font[name=YouYuan, family=YouYuan, style=Regular,
>> size=13.333333015441895]
>>
>> jshell> $3.getFamily()
>> $4 ==> "YouYuan"
>>
>>
>> As you can see, we cannot find the font based on the English name, we
>> can only use the Chinese name.
>> But Font::getName() returns the English name, so we can't get the
>> Chinese name from the Font.
>> This makes it impossible to generate a style sheet based on a
>> Font object, because
>>
>> "-fx-font-family: \"%s\";".formatted(font.getFamily())
>>
>> will not work with these fonts.
>>
>> The only workaround I can think of is to generate a mapping table
>> from English names to Chinese names like this:
>>
>>
>> Font.getFamilies().stream().collect(Collectors.toMap(it ->
>> Font.font(it).getFamily(), Function.identity()))
>>
>>
>> But this seems like a lot of overhead :(
>>
>> So, I want JavaFX to provide the following features:
>>
>> 1. Regardless of the current system language, Font.font(String)
>> should be able to find the font by its English name;
>> 2. Provide a new method Font::getLocalizedFamily() to get the
>> localized name of the font.
>>
>> Glavo
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20250714/a3dc538e/attachment-0001.htm>
More information about the openjfx-dev
mailing list