fix(gui): use same font loader as code viewer (#584)
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package jadx.gui.settings;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.GraphicsDevice;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Window;
|
||||
import java.awt.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
@@ -23,9 +20,9 @@ import org.slf4j.LoggerFactory;
|
||||
import jadx.api.JadxArgs;
|
||||
import jadx.cli.JadxCLIArgs;
|
||||
import jadx.gui.ui.codearea.EditorTheme;
|
||||
import jadx.gui.utils.FontUtils;
|
||||
import jadx.gui.utils.LangLocale;
|
||||
import jadx.gui.utils.NLS;
|
||||
import jadx.gui.utils.Utils;
|
||||
|
||||
public class JadxSettings extends JadxCLIArgs {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JadxSettings.class);
|
||||
@@ -295,22 +292,21 @@ public class JadxSettings extends JadxCLIArgs {
|
||||
if (fontStr.isEmpty()) {
|
||||
return DEFAULT_FONT;
|
||||
}
|
||||
return Font.decode(fontStr);
|
||||
try {
|
||||
return FontUtils.loadByStr(fontStr);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Failed to load font: {}, reset to default", fontStr, e);
|
||||
setFont(DEFAULT_FONT);
|
||||
return DEFAULT_FONT;
|
||||
}
|
||||
}
|
||||
|
||||
public void setFont(@Nullable Font font) {
|
||||
if (font == null) {
|
||||
this.fontStr = "";
|
||||
return;
|
||||
} else {
|
||||
this.fontStr = FontUtils.convertToStr(font);
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(font.getFontName());
|
||||
String fontStyleName = Utils.getFontStyleName(font.getStyle()).replaceAll(" ", "");
|
||||
if (!fontStyleName.isEmpty()) {
|
||||
sb.append('-').append(fontStyleName.toUpperCase());
|
||||
}
|
||||
sb.append('-').append(font.getSize());
|
||||
this.fontStr = sb.toString();
|
||||
}
|
||||
|
||||
public String getEditorThemePath() {
|
||||
|
||||
@@ -14,6 +14,7 @@ import say.swing.JFontChooser;
|
||||
|
||||
import jadx.gui.ui.MainWindow;
|
||||
import jadx.gui.ui.codearea.EditorTheme;
|
||||
import jadx.gui.utils.FontUtils;
|
||||
import jadx.gui.utils.LangLocale;
|
||||
import jadx.gui.utils.NLS;
|
||||
import jadx.gui.utils.Utils;
|
||||
@@ -267,7 +268,7 @@ public class JadxSettingsWindow extends JDialog {
|
||||
|
||||
private String getFontLabelStr() {
|
||||
Font font = settings.getFont();
|
||||
String fontStyleName = Utils.getFontStyleName(font.getStyle());
|
||||
String fontStyleName = FontUtils.convertFontStyleToString(font.getStyle());
|
||||
return NLS.str("preferences.font") + ": " + font.getFontName() + ' ' + fontStyleName + ' ' + font.getSize();
|
||||
}
|
||||
|
||||
|
||||
@@ -90,6 +90,7 @@ import jadx.gui.update.JadxUpdate;
|
||||
import jadx.gui.update.JadxUpdate.IUpdateCallback;
|
||||
import jadx.gui.update.data.Release;
|
||||
import jadx.gui.utils.CacheObject;
|
||||
import jadx.gui.utils.FontUtils;
|
||||
import jadx.gui.utils.JumpPosition;
|
||||
import jadx.gui.utils.Link;
|
||||
import jadx.gui.utils.NLS;
|
||||
@@ -154,7 +155,7 @@ public class MainWindow extends JFrame {
|
||||
this.cacheObject = new CacheObject();
|
||||
|
||||
resetCache();
|
||||
registerBundledFonts();
|
||||
FontUtils.registerBundledFonts();
|
||||
initUI();
|
||||
initMenuAndToolbar();
|
||||
Utils.setWindowIcons(this);
|
||||
@@ -931,13 +932,6 @@ public class MainWindow extends JFrame {
|
||||
setSize((int) (w * WINDOW_RATIO), (int) (h * WINDOW_RATIO));
|
||||
}
|
||||
|
||||
public static void registerBundledFonts() {
|
||||
GraphicsEnvironment grEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
if (Utils.FONT_HACK != null) {
|
||||
grEnv.registerFont(Utils.FONT_HACK);
|
||||
}
|
||||
}
|
||||
|
||||
private void setEditorTheme(String editorThemePath) {
|
||||
try {
|
||||
editorTheme = Theme.load(getClass().getResourceAsStream(editorThemePath));
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package jadx.gui.utils;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.swing.text.StyleContext;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jadx.core.utils.exceptions.JadxRuntimeException;
|
||||
|
||||
public class FontUtils {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FontUtils.class);
|
||||
|
||||
public static final Font FONT_HACK = openFontTTF("Hack-Regular");
|
||||
|
||||
public static void registerBundledFonts() {
|
||||
GraphicsEnvironment grEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
if (FontUtils.FONT_HACK != null) {
|
||||
grEnv.registerFont(FontUtils.FONT_HACK);
|
||||
}
|
||||
}
|
||||
|
||||
public static Font loadByStr(String fontDesc) {
|
||||
String[] parts = fontDesc.split("-");
|
||||
if (parts.length != 3) {
|
||||
throw new JadxRuntimeException("Unsupported font description format: " + fontDesc);
|
||||
}
|
||||
String name = parts[0];
|
||||
int style = parseFontStyle(parts[1]);
|
||||
int size = Integer.parseInt(parts[2]);
|
||||
|
||||
StyleContext sc = StyleContext.getDefaultStyleContext();
|
||||
Font font = sc.getFont(name, style, size);
|
||||
if (font == null) {
|
||||
throw new JadxRuntimeException("Font not found: " + fontDesc);
|
||||
}
|
||||
return font;
|
||||
}
|
||||
|
||||
public static String convertToStr(Font font) {
|
||||
return font.getFontName()
|
||||
+ '-' + convertFontStyleToString(font.getStyle())
|
||||
+ '-' + font.getSize();
|
||||
}
|
||||
|
||||
public static String convertFontStyleToString(int style) {
|
||||
if (style == 0) {
|
||||
return "plain";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if ((style & Font.BOLD) != 0) {
|
||||
sb.append("bold");
|
||||
}
|
||||
if ((style & Font.ITALIC) != 0) {
|
||||
sb.append(" italic");
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
private static int parseFontStyle(String str) {
|
||||
int style = 0;
|
||||
if (str.contains("bold")) {
|
||||
style |= Font.BOLD;
|
||||
}
|
||||
if (str.contains("italic")) {
|
||||
style |= Font.ITALIC;
|
||||
}
|
||||
return style;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Font openFontTTF(String name) {
|
||||
String fontPath = "/fonts/" + name + ".ttf";
|
||||
try (InputStream is = Utils.class.getResourceAsStream(fontPath)) {
|
||||
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
|
||||
return font.deriveFont(12f);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Failed load font by path: {}", fontPath, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private FontUtils() {
|
||||
}
|
||||
}
|
||||
@@ -28,8 +28,6 @@ public class Utils {
|
||||
private static final ImageIcon ICON_ABSTRACT = openIcon("abstract_co");
|
||||
private static final ImageIcon ICON_NATIVE = openIcon("native_co");
|
||||
|
||||
public static final Font FONT_HACK = openFontTTF("Hack-Regular");
|
||||
|
||||
/**
|
||||
* The minimum about of memory in bytes we are trying to keep free, otherwise the application may run out of heap
|
||||
* which ends up in a Java garbage collector running "amok" (CPU utilization 100% for each core and the UI is
|
||||
@@ -60,18 +58,6 @@ public class Utils {
|
||||
return Toolkit.getDefaultToolkit().createImage(resource);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Font openFontTTF(String name) {
|
||||
String fontPath = "/fonts/" + name + ".ttf";
|
||||
try (InputStream is = Utils.class.getResourceAsStream(fontPath)) {
|
||||
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
|
||||
return font.deriveFont(12f);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Failed load font by path: {}", fontPath, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void addKeyBinding(JComponent comp, KeyStroke key, String id, Action action) {
|
||||
comp.getInputMap().put(key, id);
|
||||
comp.getActionMap().put(id, action);
|
||||
@@ -180,21 +166,6 @@ public class Utils {
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getFontStyleName(int style) {
|
||||
if (style == 0) {
|
||||
return "plain";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if ((style & Font.BOLD) != 0) {
|
||||
sb.append("bold");
|
||||
}
|
||||
if ((style & Font.ITALIC) != 0) {
|
||||
sb.append(" italic");
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
public static void setWindowIcons(Window window) {
|
||||
List<Image> icons = new ArrayList<>();
|
||||
icons.add(Utils.openImage("/logos/jadx-logo-16px.png"));
|
||||
|
||||
Reference in New Issue
Block a user