Skip to content

Commit

Permalink
Ensure all used fonts support fallback fonts by using methods that pr…
Browse files Browse the repository at this point in the history
…eserve the underlying Font2D handle.

Update darklaf to version 2.4.7 for fallback font support with darklaf themes.
  • Loading branch information
weisJ committed Sep 9, 2020
1 parent be0681c commit 4ca685e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ commons-math3.version=3.6.1
commons-net.version=3.7
commons-pool2.version=2.8.1
commons-text.version=1.9
darklaf.version=2.4.5
darklaf.version=2.4.7
dec.version=0.1.2
dnsjava.version=2.1.9
equalsverifier.version=3.4.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.swing.UIManager;

import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.gui.JMeterUIDefaults;
import org.jCharts.axisChart.AxisChart;
import org.jCharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition;
import org.jCharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer;
Expand Down Expand Up @@ -82,7 +83,7 @@ public class AxisGraph extends JPanel {

private static final Font FONT_DEFAULT = UIManager.getDefaults().getFont("TextField.font");

protected Font valueFont = new Font("SansSerif", Font.PLAIN, (int) Math.round(FONT_DEFAULT.getSize() * 0.6));
protected Font valueFont = JMeterUIDefaults.createFont("SansSerif", Font.PLAIN, (int) Math.round(FONT_DEFAULT.getSize() * 0.6));

protected Color[] color = { Color.YELLOW };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
import org.apache.jorphan.gui.JMeterUIDefaults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -283,7 +284,7 @@ private JPanel createSmtpSettings() {
MailerModel.MailAuthType.NONE.toString(),
MailerModel.MailAuthType.SSL.toString(),
MailerModel.MailAuthType.TLS.toString()});
authTypeCombo.setFont(new Font("SansSerif", Font.PLAIN, 10)); // $NON-NLS-1$
authTypeCombo.setFont(JMeterUIDefaults.createFont("SansSerif", Font.PLAIN, 10)); // $NON-NLS-1$
authTypePane.add(authTypeCombo, BorderLayout.CENTER);

JPanel credPane = new JPanel(new BorderLayout());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -66,6 +65,7 @@
import org.apache.jorphan.gui.GuiUtils;
import org.apache.jorphan.gui.JFactory;
import org.apache.jorphan.gui.JLabeledTextField;
import org.apache.jorphan.gui.JMeterUIDefaults;
import org.apache.jorphan.math.StatCalculatorLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -373,10 +373,12 @@ public void makeGraph() {
graphPanel.setPointShape(StatGraphProperties.getPointShapeMap().get(pointShapeLine.getSelectedItem()));
graphPanel.setStrokeWidth(Float.parseFloat((String) strokeWidthList.getSelectedItem()));

graphPanel.setTitleFont(new Font(StatGraphProperties.getFontNameMap().get(titleFontNameList.getSelectedItem()),
graphPanel.setTitleFont(JMeterUIDefaults.createFont(
StatGraphProperties.getFontNameMap().get(titleFontNameList.getSelectedItem()),
StatGraphProperties.getFontStyleMap().get(titleFontStyleList.getSelectedItem()),
Integer.parseInt((String) titleFontSizeList.getSelectedItem())));
graphPanel.setLegendFont(new Font(StatGraphProperties.getFontNameMap().get(fontNameList.getSelectedItem()),
graphPanel.setLegendFont(JMeterUIDefaults.createFont(
StatGraphProperties.getFontNameMap().get(fontNameList.getSelectedItem()),
StatGraphProperties.getFontStyleMap().get(fontStyleList.getSelectedItem()),
Integer.parseInt((String) fontSizeList.getSelectedItem())));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.apache.jorphan.gui.GuiUtils;
import org.apache.jorphan.gui.JFactory;
import org.apache.jorphan.gui.JLabeledTextField;
import org.apache.jorphan.gui.JMeterUIDefaults;
import org.apache.jorphan.gui.NumberRenderer;
import org.apache.jorphan.gui.ObjectTableModel;
import org.apache.jorphan.gui.ObjectTableSorter;
Expand Down Expand Up @@ -622,13 +623,16 @@ public void makeGraph() {
graphPanel.setLegendPlacement(StatGraphProperties.getPlacementNameMap()
.get(legendPlacementList.getSelectedItem()));

graphPanel.setTitleFont(new Font(StatGraphProperties.getFontNameMap().get(titleFontNameList.getSelectedItem()),
graphPanel.setTitleFont(JMeterUIDefaults.createFont(
StatGraphProperties.getFontNameMap().get(titleFontNameList.getSelectedItem()),
StatGraphProperties.getFontStyleMap().get(titleFontStyleList.getSelectedItem()),
Integer.parseInt((String) titleFontSizeList.getSelectedItem())));
graphPanel.setLegendFont(new Font(StatGraphProperties.getFontNameMap().get(fontNameList.getSelectedItem()),
graphPanel.setLegendFont(JMeterUIDefaults.createFont(
StatGraphProperties.getFontNameMap().get(fontNameList.getSelectedItem()),
StatGraphProperties.getFontStyleMap().get(fontStyleList.getSelectedItem()),
Integer.parseInt((String) fontSizeList.getSelectedItem())));
graphPanel.setValueFont(new Font(StatGraphProperties.getFontNameMap().get(valueFontNameList.getSelectedItem()),
graphPanel.setValueFont(JMeterUIDefaults.createFont(
StatGraphProperties.getFontNameMap().get(valueFontNameList.getSelectedItem()),
StatGraphProperties.getFontStyleMap().get(valueFontStyleList.getSelectedItem()),
Integer.parseInt((String) valueFontSizeList.getSelectedItem())));

Expand Down Expand Up @@ -926,7 +930,7 @@ private JPanel createGraphSelectionSubPane() {

private JPanel createGraphTitlePane() {
JPanel titleNamePane = new JPanel(new BorderLayout());
syncWithName.setFont(new Font("SansSerif", Font.PLAIN, 10));
syncWithName.setFont(JMeterUIDefaults.createFont("SansSerif", Font.PLAIN, 10));
titleNamePane.add(graphTitle, BorderLayout.CENTER);
titleNamePane.add(syncWithName, BorderLayout.EAST);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public JSyntaxTextArea(int rows, int cols, boolean disableUndo) {
this.disableUndo = disableUndo;
if (USER_FONT_FAMILY != null) {
int fontSize = USER_FONT_SIZE > 0 ? USER_FONT_SIZE : getFont().getSize();
setFont(new Font(USER_FONT_FAMILY, Font.PLAIN, fontSize));
setFont(JMeterUIDefaults.createFont(USER_FONT_FAMILY, Font.PLAIN, fontSize));
if (log.isDebugEnabled()) {
log.debug("Font is set to: {}", getFont());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.UIResource;
import javax.swing.text.StyleContext;

import org.apache.jorphan.gui.ui.TextAreaUIWithUndo;
import org.apache.jorphan.gui.ui.TextFieldUIWithUndo;
Expand Down Expand Up @@ -141,6 +142,10 @@ public void install() {
});
}

public static Font createFont(String family, int style, int size) {
return stripUiResource(StyleContext.getDefaultStyleContext().getFont(family, style, size));
}

private static void addScaledFont(UIDefaults defaults, String output, String input, float scale) {
addDerivedFont(defaults, output, input, f -> f.deriveFont(f.getSize2D() * scale));
}
Expand All @@ -154,7 +159,7 @@ private static Font map(Font input, Function<Font, Font> mapper) {
// Note: we drop UIResource here so LaF treats the font as user-provided rather than
// LaF-provided.
if (input instanceof UIResource) {
output = new Font(output.getAttributes());
output = stripUiResource(output);
}
return output;
}
Expand Down Expand Up @@ -237,9 +242,23 @@ private void scaleFonts(UIDefaults defaults) {
}
}

/**
* Ensures the font isn't of type {@link UIResource}.
*
* @param font the font.
* @return font which does not implement {@link UIResource}.
*/
private static Font stripUiResource(Font font) {
if (font instanceof UIResource) {
return new NonUIResourceFont(font);
}
return font;
}

/**
* Ensures {@code oldFont} and {@code newFont} either both implement {@link UIResource},
* or none of them implement.
*
* @param oldFont old font
* @param newFont new font
* @return Font (when oldFont does not implement UIResource) or FontUIResource (when oldFont implements UIResource)
Expand All @@ -249,8 +268,19 @@ private static Font sameUiResource(Font oldFont, Font newFont) {
boolean O = newFont instanceof FontUIResource;
// This is a beautiful smile, isn't it?
if (o ^ O) {
return o ? new FontUIResource(newFont) : new Font(newFont.getAttributes());
return o ? new FontUIResource(newFont) : stripUiResource(newFont);
}
return newFont;
}

/**
* Non UIResource wrapper for fonts which preserves the underlying {@link sun.font.Font2D}.
* This way the font behaves the same way with respect to fallback fonts
* (i.e. if the {@link sun.font.Font2D} base is of type {@link sun.font.CompositeFont}).
*/
private static class NonUIResourceFont extends Font {
private NonUIResourceFont(Font font) {
super(font);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

Expand Down Expand Up @@ -69,10 +67,6 @@ public class UrlConfigGui extends JPanel implements ChangeListener {

private int tabFileUploadIndex = 2;

private static final Font FONT_DEFAULT = UIManager.getDefaults().getFont("TextField.font");

private static final Font FONT_SMALL = new Font("SansSerif", Font.PLAIN, (int) Math.round(FONT_DEFAULT.getSize() * 0.8));

private HTTPArgumentsPanel argsPanel;

private HTTPFileArgsPanel filesPanel;
Expand Down

0 comments on commit 4ca685e

Please sign in to comment.