From 2d6ffc2b481bf33bf1e8664d563fb5ac32aea253 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 17 Feb 2022 18:32:46 -0500 Subject: [PATCH] Actually read the font directory instead of ignoring it in MHQ --- MekHQ/src/mekhq/MekHQ.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/MekHQ/src/mekhq/MekHQ.java b/MekHQ/src/mekhq/MekHQ.java index 3a8e211746..2244333207 100644 --- a/MekHQ/src/mekhq/MekHQ.java +++ b/MekHQ/src/mekhq/MekHQ.java @@ -60,7 +60,9 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.InputStream; import java.util.List; /** @@ -160,6 +162,9 @@ protected void startup() { UIManager.installLookAndFeel("Flat Dark", "com.formdev.flatlaf.FlatDarkLaf"); UIManager.installLookAndFeel("Flat Darcula", "com.formdev.flatlaf.FlatDarculaLaf"); + // Handle fonts + parseFontDirectory(new File("data/fonts/")); + // Setup user preferences MegaMek.getMMPreferences().loadFromFile(MHQConstants.MM_PREFERENCES_FILE); MegaMekLab.getMMLPreferences().loadFromFile(MHQConstants.MML_PREFERENCES_FILE); @@ -173,6 +178,29 @@ protected void startup() { new StartupScreenPanel(this).getFrame().setVisible(true); } + private static void parseFontDirectory(final File directory) { + final String[] filenames = directory.list(); + if (filenames == null) { + return; + } + + for (final String filename : filenames) { + if (filename.toLowerCase().endsWith(".ttf")) { + try (InputStream fis = new FileInputStream(directory.getPath() + '/' + filename)) { + GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont( + Font.createFont(Font.TRUETYPE_FONT, fis)); + } catch (Exception ex) { + LogManager.getLogger().error("Failed to parse font", ex); + } + } else { + final File file = new File(directory, filename); + if (file.isDirectory()) { + parseFontDirectory(file); + } + } + } + } + @Deprecated // These need to be migrated to the Suite Constants / Suite Options Setup private void setUserPreferences() { PreferencesNode preferences = getMHQPreferences().forClass(MekHQ.class);