mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-13 05:46:59 -04:00
启动时尝试从 HMCL_DIRECTORY 中加载字体 (#3663)
* 启动时尝试从 HMCL_DIRECTORY 中加载字体 * update * update
This commit is contained in:
parent
a45b636b1c
commit
7adfa0e3d1
@ -24,7 +24,9 @@ import javafx.beans.binding.Bindings;
|
|||||||
import javafx.beans.binding.ObjectBinding;
|
import javafx.beans.binding.ObjectBinding;
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
import javafx.scene.text.Font;
|
import javafx.scene.text.Font;
|
||||||
|
import org.jackhuang.hmcl.Metadata;
|
||||||
import org.jackhuang.hmcl.ui.FXUtils;
|
import org.jackhuang.hmcl.ui.FXUtils;
|
||||||
|
import org.jackhuang.hmcl.util.Lazy;
|
||||||
import org.jackhuang.hmcl.util.Pair;
|
import org.jackhuang.hmcl.util.Pair;
|
||||||
import org.jackhuang.hmcl.util.io.FileUtils;
|
import org.jackhuang.hmcl.util.io.FileUtils;
|
||||||
|
|
||||||
@ -57,29 +59,41 @@ public class Theme {
|
|||||||
Color.web("#B71C1C") // red
|
Color.web("#B71C1C") // red
|
||||||
};
|
};
|
||||||
|
|
||||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
private static final Lazy<Font> FONT = new Lazy<>(() -> {
|
||||||
private static Optional<Font> font;
|
String[] fileNames = {"font.ttf", "font.otf", "font.woff"};
|
||||||
|
|
||||||
private static Optional<Font> tryLoadFont() {
|
for (String fileName : fileNames) {
|
||||||
//noinspection OptionalAssignedToNull
|
Path path = Paths.get(fileName).toAbsolutePath();
|
||||||
if (font != null) {
|
if (Files.isRegularFile(path)) {
|
||||||
return font;
|
try {
|
||||||
}
|
Font font = Font.loadFont(path.toUri().toURL().toExternalForm(), 0);
|
||||||
|
if (font != null) {
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
} catch (MalformedURLException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
Path path = Paths.get("font.ttf");
|
LOG.warning("Failed to load font " + path);
|
||||||
if (!Files.isRegularFile(path)) {
|
|
||||||
path = Paths.get("font.otf");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Files.isRegularFile(path)) {
|
|
||||||
try {
|
|
||||||
return font = Optional.ofNullable(Font.loadFont(path.toAbsolutePath().toUri().toURL().toExternalForm(), 0));
|
|
||||||
} catch (MalformedURLException ignored) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return font = Optional.empty();
|
for (String fileName : fileNames) {
|
||||||
}
|
Path path = Metadata.HMCL_DIRECTORY.resolve(fileName);
|
||||||
|
if (Files.isRegularFile(path)) {
|
||||||
|
try {
|
||||||
|
Font font = Font.loadFont(path.toUri().toURL().toExternalForm(), 0);
|
||||||
|
if (font != null) {
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
} catch (MalformedURLException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG.warning("Failed to load font " + path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
public static Theme getTheme() {
|
public static Theme getTheme() {
|
||||||
Theme theme = config().getTheme();
|
Theme theme = config().getTheme();
|
||||||
@ -172,10 +186,10 @@ public class Theme {
|
|||||||
|
|
||||||
Pair<String, String> fontStyle = null;
|
Pair<String, String> fontStyle = null;
|
||||||
if (fontFamily == null) {
|
if (fontFamily == null) {
|
||||||
Optional<Font> font = tryLoadFont();
|
Font font = FONT.get();
|
||||||
if (font.isPresent()) {
|
if (font != null) {
|
||||||
fontFamily = font.get().getFamily();
|
fontFamily = font.getFamily();
|
||||||
fontStyle = parseFontStyle(font.get().getStyle());
|
fontStyle = parseFontStyle(font.getStyle());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,8 +34,8 @@ public final class Lazy<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public T get() {
|
public T get() {
|
||||||
if (value == null) {
|
if (supplier != null) {
|
||||||
value = Objects.requireNonNull(supplier.get());
|
value = supplier.get();
|
||||||
supplier = null;
|
supplier = null;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user