Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External fonts #397

Merged
merged 2 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ timestamp
/.classpath
/.project
/properties_local.gradle
/data/fonts/
3 changes: 2 additions & 1 deletion resources/megameklab/resources/Dialogs.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ ConfigurationDialog.chkShowExtinct.tooltip=Whether to show options that have gon
ConfigurationDialog.chkUnofficialIgnoreYear.text=Unofficial Ignores Year
ConfigurationDialog.chkUnofficialIgnoreYear.tooltip=If the tech level is set to unofficial, the intro/extinct year will be ignored.
ConfigurationDialog.cbFont.text=Font Family:
ConfigurationDialog.cbFont.tooltip=Select the typeface to use when printing record sheets
ConfigurationDialog.cbFont.tooltip=<html>Select the typeface to use when printing record sheets.<br/>\
Additional truetype fonts can be made available to MML by placing the font file in the data/fonts directory.</html>
ConfigurationDialog.txtFontDisplay.text=The quick brown fox jumps over the lazy dog.
ConfigurationDialog.chkShowQuirks.text=Show quirks
ConfigurationDialog.chkShowQuirks.tooltip=Displays featured design quirks
Expand Down
51 changes: 37 additions & 14 deletions src/megameklab/com/MegaMekLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import java.io.IOException;
import java.io.PrintStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.swing.UIManager;
Expand All @@ -40,31 +42,25 @@
import megamek.common.logging.LogLevel;
import megamek.common.logging.MMLogger;
import megamek.common.preference.PreferenceManager;
import megamek.common.util.MegaMekFile;
import megameklab.com.ui.StartupGUI;
import megameklab.com.util.CConfig;
import megameklab.com.util.UnitUtil;

public class MegaMekLab {
public static final String VERSION = "0.47.3-SNAPSHOT";

private static final String FILENAME_BT_CLASSIC_FONT = "btclassic/BTLogo_old.ttf"; //$NON-NLS-1$

private static MMLogger logger = null;

public static void main(String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name","MegaMekLab");
redirectOutput();
//add classic battletech font
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
File btFontFile = new MegaMekFile(Configuration.fontsDir(), FILENAME_BT_CLASSIC_FONT).getFile();
Font btFont = Font.createFont(Font.TRUETYPE_FONT, btFontFile);
System.out.println("Loaded Font: " + btFont.getName());
ge.registerFont(btFont);
} catch (IOException | FontFormatException e) {
System.out.println("Error Registering BT Classic Font! Error: " + e.getMessage());
// Register any fonts in the fonts directory
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
List<Font> fontList = new ArrayList<>();
collectFontsFromDir(Configuration.fontsDir(), fontList);
for (Font font : fontList) {
ge.registerFont(font);
}
startup();
}
Expand All @@ -91,6 +87,33 @@ private static void redirectOutput() {
}
}

/**
* Recursively search a directory and attempt to create a truetype font from
* every file with the ttf suffix
*
* @param dir The directory to search
* @param list The list to add fonts to as they are created
*/
private static void collectFontsFromDir(File dir, List<Font> list) {
final String METHOD_NAME = "collectFontsFromDir(File, List<Font>)"; //$NON-NLS-1$
File[] files = dir.listFiles();
if (null != files) {
for (File f : files) {
if (f.isDirectory() && !f.getName().startsWith(".")) {
collectFontsFromDir(f, list);
} else if (f.getName().toLowerCase().endsWith(".ttf")) {
try {
list.add(Font.createFont(Font.TRUETYPE_FONT, f));
} catch (IOException | FontFormatException ex) {
getLogger().warning(MegaMekLab.class, METHOD_NAME,
"Error creating font from " + f);
getLogger().error(MegaMekLab.class, METHOD_NAME, ex);
}
}
}
}
}

public static MMLogger getLogger() {
if (null == logger) {
logger = DefaultMmLogger.getInstance();
Expand Down Expand Up @@ -158,8 +181,8 @@ public static double calculateMaxScreenWidth() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
double maxWidth = 0;
for (int i = 0; i < gs.length; i++) {
Rectangle b = gs[i].getDefaultConfiguration().getBounds();
for (GraphicsDevice g : gs) {
Rectangle b = g.getDefaultConfiguration().getBounds();
if (b.getWidth() > maxWidth) { // Update the max size found on this monitor
maxWidth = b.getWidth();
}
Expand Down
1 change: 1 addition & 0 deletions src/megameklab/com/util/ConfigurationDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private void loadPrintingPanel(ResourceBundle resourceMap) {
gbc.gridy = 0;
panPrinting.add(new JLabel(resourceMap.getString("ConfigurationDialog.cbFont.text")), gbc);
gbc.gridx = 1;
cbFont.setToolTipText(resourceMap.getString("ConfigurationDialog.cbFont.tooltip")); //$NON-NLS-1$
panPrinting.add(cbFont, gbc);
cbFont.addActionListener(ev -> updateFont());

Expand Down