From 991729b6841afc39aced4a9b4a2bd9f5801bc74c Mon Sep 17 00:00:00 2001 From: Glavo Date: Sun, 24 Mar 2024 19:59:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BB=8E=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=8A=A0=E8=BD=BD=E5=AD=97=E4=BD=93=20(#2950?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jackhuang/hmcl/setting/Theme.java | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Theme.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Theme.java index c3d172903..2f14a5834 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Theme.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Theme.java @@ -23,6 +23,7 @@ import com.google.gson.stream.JsonWriter; import javafx.beans.binding.Bindings; import javafx.beans.binding.ObjectBinding; import javafx.scene.paint.Color; +import javafx.scene.text.Font; import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.io.FileUtils; @@ -30,8 +31,12 @@ import org.jackhuang.hmcl.util.io.IOUtils; import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Locale; import java.util.Objects; import java.util.Optional; @@ -82,6 +87,30 @@ public class Theme { return cssCharset; } + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + private static Optional font; + + private static Optional tryLoadFont() { + //noinspection OptionalAssignedToNull + if (font != null) { + return font; + } + + Path path = Paths.get("font.ttf"); + 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(); + } + public static Theme getTheme() { Theme theme = config().getTheme(); return theme == null ? BLUE : theme; @@ -121,9 +150,24 @@ public class Theme { String css = "/assets/css/blue.css"; String fontFamily = System.getProperty("hmcl.font.override", overrideFontFamily); + String fontStyle = null; + if (fontFamily == null) { + Optional font = tryLoadFont(); + if (font.isPresent()) { + fontFamily = font.get().getFamily(); + fontStyle = font.get().getStyle(); + } + } if (fontFamily != null || !this.color.equalsIgnoreCase(BLUE.color)) { Color textFill = getForegroundColor(); + String fontCss = ""; + if (fontFamily != null) { + fontCss = "-fx-font-family: \"" + fontFamily + "\";"; + if (fontStyle != null && !fontStyle.isEmpty()) + fontCss += " -fx-font-style: \"" + fontStyle + "\";"; + } + try { File temp = File.createTempFile("hmcl", ".css"); String themeText = IOUtils.readFullyAsString(Theme.class.getResourceAsStream("/assets/css/custom.css")) @@ -134,7 +178,7 @@ public class Theme { .replace("%base-rippler-color%", String.format("rgba(%d, %d, %d, 0.3)", (int) Math.ceil(paint.getRed() * 256), (int) Math.ceil(paint.getGreen() * 256), (int) Math.ceil(paint.getBlue() * 256))) .replace("%disabled-font-color%", String.format("rgba(%d, %d, %d, 0.7)", (int) Math.ceil(textFill.getRed() * 256), (int) Math.ceil(textFill.getGreen() * 256), (int) Math.ceil(textFill.getBlue() * 256))) .replace("%font-color%", getColorDisplayName(getForegroundColor())) - .replace("%font%", Optional.ofNullable(fontFamily).map(f -> "-fx-font-family: \"" + f + "\";").orElse("")); + .replace("%font%", fontCss); FileUtils.writeText(temp, themeText, getCssCharset()); temp.deleteOnExit(); css = temp.toURI().toString();