jmecn / jme3-freetype-font

freetype font support for jme3
BSD 3-Clause "New" or "Revised" License
3 stars 0 forks source link

扫描系统字体 #4

Open jmecn opened 2 months ago

jmecn commented 2 months ago

自动扫描系统字体,实现类似 font config 的匹配机制。允许用户通过 new Font("宋体", 12) 这种方式来定义字体。

Mac

/System/Library/Fonts /Library/Fonts $HOME/Library/Fonts

Linux

/usr/share/fonts /usr/share/X11/fonts

Android

/system/fonts

JDK

System.getProperty("java.home") + "/lib/fonts" System.getProperty("java.home") + "/conf/fonts"

Oracle JDK 的逻辑字体和物理字体 https://docs.oracle.com/en/java/javase/21/intl/font-configuration-files.html

jmecn commented 2 months ago

Windows

扫描 C:\Windows\Fonts 或者 System.getenv("windir") + "\Fonts"

首先通过 "windir" 拿到 系统目录,再找到 Fonts 目录,扫描所有的字体文件名。

如果 windir 拿到的是 C:\windows\System32 或 C:\windows\System 这种名字,需要把 System 和 System32 替换成 "Fonts"

然后读取注册表中的 "Software\Microsoft\Windows NT\CurrentVersion\Fonts",获取字体名到字体文件名的映射。

Java可以通过 Perferences 来读取注册表。

Perferences systemRoot = Perferences.systemRoot();
systemRoot.get("Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts")

参考 https://github.com/openjdk/jfx/blob/master/modules/javafx.graphics/src/main/native-font/fontpath.c ,然后用 Java 实现。