Skip to content

Commit e4bfbef

Browse files
committed
Allow custom fallback fonts for Resources.ensureStrFonts()
1 parent fccbef2 commit e4bfbef

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ This allows faster delivery of bug-fixes/optimizations/improvements to MTR 4, wh
1313
- MTR 3 / NTE Compatibility Improvements:
1414
- Fix: NTE Eyecandy object now recognizes the "translation"/"rotation"/"scale"/"mirror" fields, which is used by NTE but does not get parsed correctly in MTR 4.
1515
- Fix: For OBJ texture with path traversal (Like `../` / `..\`), it is once again recognized, solving some missing texture issues when loading NTE packs.
16-
- Fix: Automatically migrate gangways/barrier in MTR 3, fixing some missing gangway texture issues for legacy trains.
1716
- Enhancements: Improve model loading speed and memory usage for MTR.
1817
- Enhancements: Minor frame-rate boost in complex world.
1918
- Enhancements: Add car auto-filling in siding screen
@@ -35,6 +34,7 @@ This allows faster delivery of bug-fixes/optimizations/improvements to MTR 4, wh
3534
- Provide a slightly more rich API for logging, similar to the web's console API seen in browsers. (console.log, console.warn etc.)
3635
- The `print` function will be retained, redirects to `console.log` under the hood.
3736
- The source/line no. of the print/console log statements may be viewed by enabling **View Script Log Source** in JCM's settings.
37+
- `Resources.ensureStrFonts()` now allows you to append an unlimited amount of AWT fallback fonts.
3838
- Add `MinecraftClient.spawnParticleInWorld()` to spawn vanilla particles in the current world via scripts.
3939
- Add `MinecraftClient.renderDistance()` to obtain the currently configured world render distance.
4040
- Add `MinecraftClient.getWorldPlayers()` to obtain all player's `PlayerEntity` within the current render distance.

fabric/src/main/java/com/lx862/mtrscripting/util/ScriptResourceUtil.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,28 @@ public static FontRenderContext getFontRenderContext() {
197197
return FONT_CONTEXT;
198198
}
199199

200-
public static AttributedString ensureStrFonts(String text, Font font) {
200+
public static AttributedString ensureStrFonts(String text, Font primaryFont, Font... fallbackFonts) {
201201
AttributedString result = new AttributedString(text);
202202
if (text.isEmpty()) return result;
203-
result.addAttribute(TextAttribute.FONT, font, 0, text.length());
203+
result.addAttribute(TextAttribute.FONT, primaryFont, 0, text.length());
204204
for (int characterIndex = 0; characterIndex < text.length(); characterIndex++) {
205205
final char character = text.charAt(characterIndex);
206-
if (!font.canDisplay(character)) {
206+
if (!primaryFont.canDisplay(character)) {
207207
Font defaultFont = null;
208+
for(final Font testFont : fallbackFonts) {
209+
if (testFont.canDisplay(character)) {
210+
defaultFont = testFont;
211+
break;
212+
}
213+
}
214+
// Still no hope, find all system fonts...
208215
for (final Font testFont : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
209216
if (testFont.canDisplay(character)) {
210217
defaultFont = testFont;
211218
break;
212219
}
213220
}
214-
final Font newFont = (defaultFont == null ? new Font(null) : defaultFont)
215-
.deriveFont(font.getStyle(), font.getSize2D());
221+
final Font newFont = (defaultFont == null ? new Font(null) : defaultFont).deriveFont(primaryFont.getStyle(), primaryFont.getSize2D());
216222
result.addAttribute(TextAttribute.FONT, newFont, characterIndex, characterIndex + 1);
217223
}
218224
}

0 commit comments

Comments
 (0)