Skip to content

Commit 8a15b7d

Browse files
authored
修复使用自定义字体时未正确设置字重的问题 (#3598)
1 parent fce4c13 commit 8a15b7d

File tree

1 file changed

+49
-4
lines changed
  • HMCL/src/main/java/org/jackhuang/hmcl/setting

1 file changed

+49
-4
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/setting/Theme.java

+49-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javafx.scene.paint.Color;
2626
import javafx.scene.text.Font;
2727
import org.jackhuang.hmcl.ui.FXUtils;
28+
import org.jackhuang.hmcl.util.Pair;
2829
import org.jackhuang.hmcl.util.io.FileUtils;
2930

3031
import java.io.File;
@@ -123,19 +124,58 @@ private static String rgba(Color color, double opacity) {
123124
opacity);
124125
}
125126

127+
private static Pair<String, String> parseFontStyle(String style) {
128+
if (style == null) {
129+
return null;
130+
}
131+
132+
style = style.toLowerCase(Locale.ROOT);
133+
134+
String weight;
135+
String posture;
136+
137+
if (style.contains("thin")) {
138+
weight = "100";
139+
} else if (style.contains("extralight") || style.contains("extra light") || style.contains("ultralight") | style.contains("ultra light")) {
140+
weight = "200";
141+
} else if (style.contains("medium")) {
142+
weight = "500";
143+
} else if (style.contains("semibold") || style.contains("semi bold") || style.contains("demibold") || style.contains("demi bold")) {
144+
weight = "600";
145+
} else if (style.contains("extrabold") || style.contains("extra bold") || style.contains("ultrabold") || style.contains("ultra bold")) {
146+
weight = "800";
147+
} else if (style.contains("black") || style.contains("heavy")) {
148+
weight = "900";
149+
} else if (style.contains("light")) {
150+
weight = "lighter";
151+
} else if (style.contains("bold")) {
152+
weight = "bold";
153+
} else {
154+
weight = null;
155+
}
156+
157+
if (style.contains("italic") || style.contains("oblique")) {
158+
posture = "italic";
159+
} else {
160+
posture = null;
161+
}
162+
163+
return Pair.pair(weight, posture);
164+
}
165+
126166
public String[] getStylesheets(String overrideFontFamily) {
127167
String css = "/assets/css/blue.css";
128168

129169
String fontFamily = overrideFontFamily == null
130170
? System.getProperty("hmcl.font.override", System.getenv("HMCL_FONT"))
131171
: overrideFontFamily;
132172

133-
String fontStyle = null;
173+
Pair<String, String> fontStyle = null;
134174
if (fontFamily == null) {
135175
Optional<Font> font = tryLoadFont();
136176
if (font.isPresent()) {
137177
fontFamily = font.get().getFamily();
138-
fontStyle = font.get().getStyle();
178+
fontStyle = parseFontStyle(font.get().getStyle());
139179
}
140180
}
141181

@@ -159,8 +199,13 @@ public String[] getStylesheets(String overrideFontFamily) {
159199
else
160200
themeBuilder.append("-fx-font-family:\"").append(fontFamily).append("\";");
161201

162-
if (fontStyle != null && !fontStyle.isEmpty())
163-
themeBuilder.append("-fx-font-style:\"").append(fontStyle).append("\";");
202+
if (fontStyle != null) {
203+
if (fontStyle.getKey() != null)
204+
themeBuilder.append("-fx-font-weight:").append(fontStyle.getKey()).append(";");
205+
206+
if (fontStyle.getValue() != null)
207+
themeBuilder.append("-fx-font-style:").append(fontStyle.getValue()).append(";");
208+
}
164209

165210
themeBuilder.append('}');
166211

0 commit comments

Comments
 (0)