|
21 | 21 | import javafx.beans.property.SimpleObjectProperty; |
22 | 22 | import javafx.scene.text.Font; |
23 | 23 | import org.jackhuang.hmcl.Metadata; |
| 24 | +import org.jackhuang.hmcl.java.JavaRuntime; |
24 | 25 | import org.jackhuang.hmcl.util.Lazy; |
25 | 26 | import org.jackhuang.hmcl.util.io.JarUtils; |
26 | 27 | import org.jackhuang.hmcl.util.platform.OperatingSystem; |
27 | 28 | import org.jackhuang.hmcl.util.platform.SystemUtils; |
28 | 29 |
|
| 30 | +import java.lang.invoke.MethodHandle; |
| 31 | +import java.lang.invoke.MethodHandles; |
| 32 | +import java.lang.invoke.MethodType; |
29 | 33 | import java.net.MalformedURLException; |
30 | 34 | import java.nio.file.Files; |
31 | 35 | import java.nio.file.Path; |
@@ -120,15 +124,51 @@ public static Font findByFcMatch() { |
120 | 124 | return null; |
121 | 125 |
|
122 | 126 | try { |
123 | | - String path = SystemUtils.run(fcMatch.toString(), |
| 127 | + String result = SystemUtils.run(fcMatch.toString(), |
124 | 128 | ":lang=" + Locale.getDefault().toLanguageTag(), |
125 | | - "--format", "%{file}").trim(); |
| 129 | + "--format", "%{family}\\n%{file}").trim(); |
| 130 | + |
| 131 | + String[] results = result.split("\\n"); |
| 132 | + if (results.length != 2 || results[0].isEmpty() || results[1].isEmpty()) { |
| 133 | + LOG.warning("Unexpected output from fc-match: " + result); |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + String family = results[0].trim(); |
| 138 | + String path = results[1]; |
| 139 | + |
126 | 140 | Path file = Paths.get(path).toAbsolutePath().normalize(); |
127 | 141 | if (!Files.isRegularFile(file)) { |
128 | 142 | LOG.warning("Font file does not exist: " + path); |
129 | 143 | return null; |
130 | 144 | } |
131 | 145 |
|
| 146 | + if (JavaRuntime.CURRENT_VERSION >= 9) { |
| 147 | + try { |
| 148 | + MethodHandle methodHandle = MethodHandles.publicLookup().findStatic(Font.class, "loadFonts", |
| 149 | + MethodType.methodType(Font[].class, String.class, double.class)); |
| 150 | + |
| 151 | + Font[] fonts = (Font[]) methodHandle.invokeExact(file.toUri().toURL().toExternalForm(), DEFAULT_FONT_SIZE); |
| 152 | + if (fonts == null) { |
| 153 | + LOG.warning("Failed to load font from " + path); |
| 154 | + return null; |
| 155 | + } else if (fonts.length == 0) { |
| 156 | + LOG.warning("No fonts loaded from " + path); |
| 157 | + return null; |
| 158 | + } |
| 159 | + |
| 160 | + for (Font font : fonts) { |
| 161 | + if (font.getFamily().equalsIgnoreCase(family)) { |
| 162 | + return font; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + LOG.warning(String.format("Family '%s' not found in font file '%s'", family, path)); |
| 167 | + return fonts[0]; |
| 168 | + } catch (NoSuchMethodException | IllegalAccessException ignored) { |
| 169 | + } |
| 170 | + } |
| 171 | + |
132 | 172 | Font font = Font.loadFont(file.toUri().toURL().toExternalForm(), DEFAULT_FONT_SIZE); |
133 | 173 | if (font == null) |
134 | 174 | LOG.warning("Failed to load font from " + path); |
|
0 commit comments