Browse Source

Added font.properties. Fonts can be created without specifying the actual font name. In this case the font of the parent laf is used and adjusted.

pull/97/head
weisj 5 years ago
parent
commit
283e617ada
  1. 6
      core/src/main/java/com/github/weisj/darklaf/DarkLaf.java
  2. 66
      core/src/main/resources/com/github/weisj/darklaf/properties/font.properties
  3. 1
      core/src/main/resources/com/github/weisj/darklaf/properties/ui/slider.properties
  4. 1
      core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabFrame.properties
  5. 2
      core/src/test/java/defaults/UIManagerDefaults.java
  6. 100
      property-loader/src/main/java/com/github/weisj/darklaf/PropertyLoader.java

6
core/src/main/java/com/github/weisj/darklaf/DarkLaf.java

@ -218,6 +218,7 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
currentTheme.loadGlobals(uiProps, defaults);
installGlobals(uiProps, defaults);
loadFontProperties(uiProps, defaults);
currentTheme.loadUIProperties(uiProps, defaults);
currentTheme.loadIconProperties(uiProps, defaults);
currentTheme.loadPlatformProperties(uiProps, defaults);
@ -230,6 +231,11 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
new HTMLEditorKit().setStyleSheet(styleSheet);
}
private void loadFontProperties(final Properties uiProps, final UIDefaults defaults) {
Properties fontProps = PropertyLoader.loadProperties(DarkLaf.class, "font", "properties/");
PropertyLoader.putProperties(fontProps, uiProps, defaults);
}
private void loadSystemOverwrites(final Properties uiProps, final UIDefaults defaults) {
Properties overwrites = PropertyLoader.loadProperties(DarkLaf.class, "overwrites", "properties/");
overwrites.values().removeIf(v -> System.getProperty(SYSTEM_PROPERTY_PREFIX + v.toString()) == null);

66
core/src/main/resources/com/github/weisj/darklaf/properties/font.properties

@ -0,0 +1,66 @@
# suppress inspection "UnusedProperty" for whole file
#
# MIT License
#
# Copyright (c) 2020 Jannis Weis
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
Button.font = withSize(12)withStyle(0)
CheckBox.font = withSize(12)withStyle(0)
CheckBoxMenuItem.font = withSize(12)withStyle(0)
ColorChooser.font = withSize(12)withStyle(0)
ComboBox.font = withSize(12)withStyle(0)
DesktopIcon.font = withSize(12)withStyle(0)
EditorPane.font = withSize(12)withStyle(0)
FormattedTextField.font = withSize(12)withStyle(0)
IconButton.font = withSize(12)withStyle(0)
Label.font = withSize(12)withStyle(0)
List.font = withSize(12)withStyle(0)
Menu.font = withSize(12)withStyle(0)
MenuBar.font = withSize(12)withStyle(0)
MenuItem.font = withSize(12)withStyle(0)
OptionPane.font = withSize(12)withStyle(0)
Panel.font = withSize(12)withStyle(0)
PasswordField.font = withSize(12)withStyle(0)
PopupMenu.font = withSize(12)withStyle(0)
ProgressBar.font = withSize(12)withStyle(0)
RadioButton.font = withSize(12)withStyle(0)
RadioButtonMenuItem.font = withSize(12)withStyle(0)
ScrollPane.font = withSize(12)withStyle(0)
Slider.font = withSize(12)withStyle(0)
Spinner.font = withSize(12)withStyle(0)
TabbedPane.font = withSize(12)withStyle(0)
TabFrameTab.font = from(Label.font)withSize(11)withStyle(0)
Table.font = withSize(12)withStyle(0)
TableHeader.font = withSize(12)withStyle(0)
TextArea.font = withSize(12)withStyle(0)
TextField.font = withSize(12)withStyle(0)
TextPane.font = withSize(12)withStyle(0)
TitledBorder.font = withSize(12)withStyle(0)
ToggleButton.font = withSize(12)withStyle(0)
ToolBar.font = withSize(12)withStyle(0)
ToolTip.font = withSize(12)withStyle(0)
Tree.font = withSize(12)withStyle(0)
Viewport.font = withSize(12)withStyle(0)
MenuItem.acceleratorFont = withSize(10)withStyle(0)
RadioButtonMenuItem.acceleratorFont = withSize(10)withStyle(0)
Menu.acceleratorFont = withSize(10)withStyle(0)
CheckBoxMenuItem.acceleratorFont = withSize(10)withStyle(0)
InternalFrame.titleFont = withSize(11)withStyle(0)

1
core/src/main/resources/com/github/weisj/darklaf/properties/ui/slider.properties

@ -23,7 +23,6 @@
# SOFTWARE.
#
SliderUI = com.github.weisj.darklaf.ui.slider.DarkSliderUI
Slider.font = Dialog-0-10
Slider.tickColor = %textForegroundSecondary
Slider.disabledTickColor = %textForegroundInactive
Slider.selectedTrackColor = %controlFillSecondary

1
core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabFrame.properties

@ -51,7 +51,6 @@ TabFramePopup.borderColor = %border
TabFrameTabLabelUI = com.github.weisj.darklaf.ui.tabframe.DarkTabFrameTabLabelUI
TabFrameTabContainerUI = com.github.weisj.darklaf.ui.tabframe.DarkTabFrameTabContainerUI
TabFrameTab.border = com.github.weisj.darklaf.ui.tabframe.DarkTabFrameTabBorder
TabFrameTab.font = Dialog-0-11
TabFrameTab.foreground = %textForeground
TabFrameTab.selectedForeground = %textForegroundHighlight
TabFrameTab.selectedBackground = %backgroundSelectedSecondary

2
core/src/test/java/defaults/UIManagerDefaults.java

@ -116,10 +116,10 @@ public class UIManagerDefaults implements ItemListener, ComponentDemo {
buildItemsMap();
final Vector<String> comboBoxItems = new Vector<>(50);
FontMetrics fm = comboBox.getFontMetrics(comboBox.getFont());
for (final Object key : items.keySet()) {
comboBoxItems.add((String) key);
}
comboBox.removeItemListener(this);
comboBox.setModel(new DefaultComboBoxModel<>(comboBoxItems));
comboBox.setSelectedIndex(-1);

100
property-loader/src/main/java/com/github/weisj/darklaf/PropertyLoader.java

@ -27,6 +27,7 @@ import com.github.weisj.darklaf.icons.DarkUIAwareIcon;
import com.github.weisj.darklaf.icons.EmptyIcon;
import com.github.weisj.darklaf.icons.IconLoader;
import com.github.weisj.darklaf.util.ColorUtil;
import com.github.weisj.darklaf.util.Pair;
import com.github.weisj.darklaf.util.PropertyValue;
import com.github.weisj.darklaf.util.StringUtil;
@ -56,6 +57,9 @@ public final class PropertyLoader {
private static final String AWARE_KEY = "[aware]";
private static final String THEMED_KEY = "[themed]";
private static final String REFERENCE_PREFIX = "%";
private static final String FONT_FROM = "from";
private static final String FONT_SIZE = "withSize";
private static final String FONT_STYLE = "withStyle";
private static final Collection<ObjectRequest> objectsToLoad = new HashSet<>();
private static final Map<AttributedCharacterIterator.Attribute, Integer> attributes = Collections.singletonMap(TextAttribute.KERNING,
@ -99,7 +103,7 @@ public final class PropertyLoader {
final UIDefaults currentDefaults, final IconLoader iconLoader) {
for (final String key : properties.stringPropertyNames()) {
final String value = properties.getProperty(key);
Object parsed = parseValue(key, value, accumulator, iconLoader);
Object parsed = parseValue(key, value, accumulator, currentDefaults, iconLoader);
if (parsed instanceof ObjectRequest) {
objectsToLoad.add((ObjectRequest) parsed);
} else if (parsed != null) {
@ -110,9 +114,9 @@ public final class PropertyLoader {
}
}
private static Object parseValue(final String key, final String value,
final Map<Object, Object> defaults, final IconLoader iconLoader) {
return parseValue(key, value, false, defaults, iconLoader);
private static Object parseValue(final String key, final String value, final Properties accumulator,
final UIDefaults currentDefaults, final IconLoader iconLoader) {
return parseValue(key, value, false, accumulator, currentDefaults, iconLoader);
}
private static String parseKey(final String key) {
@ -121,8 +125,8 @@ public final class PropertyLoader {
private static Object parseValue(final String propertyKey, final String value,
final boolean ignoreRequest, final Map<Object, Object> defaults,
final IconLoader iconLoader) {
final boolean ignoreRequest, final Map<Object, Object> accumulator,
final UIDefaults currentDefaults, final IconLoader iconLoader) {
if (PropertyValue.NULL.equals(value)) {
return null;
}
@ -143,8 +147,8 @@ public final class PropertyLoader {
|| key.endsWith("Component")
|| key.endsWith("Renderer"))) {
return new ObjectRequest(key, value);
} else if (key.endsWith(".font")) {
returnVal = parseFont(value);
} else if (key.toLowerCase().endsWith("font")) {
returnVal = parseFont(key, value, accumulator, currentDefaults);
} else if (key.endsWith(".icon") || key.endsWith("Icon")) {
returnVal = parseIcon(value, iconLoader);
} else if (key.endsWith("Size") || key.endsWith(".size")) {
@ -153,11 +157,11 @@ public final class PropertyLoader {
returnVal = null;
} else if (value.startsWith("%")) {
String val = value.substring(1);
if (!defaults.containsKey(val)) {
if (!accumulator.containsKey(val)) {
LOGGER.warning("Could not reference value '" + val + "'while loading '" + key + "'. " +
"May be a forward reference");
}
returnVal = defaults.get(val);
returnVal = accumulator.get(val);
}
if (returnVal instanceof LoadError) {
final Color color = ColorUtil.fromHex(value, null);
@ -182,21 +186,77 @@ public final class PropertyLoader {
private static Object parseInsets(final String value) {
final List<String> numbers = StringUtil.split(value, ",");
return new InsetsUIResource(
Integer.parseInt(numbers.get(0)),
Integer.parseInt(numbers.get(1)),
Integer.parseInt(numbers.get(2)),
Integer.parseInt(numbers.get(3)));
Integer.parseInt(numbers.get(0)),
Integer.parseInt(numbers.get(1)),
Integer.parseInt(numbers.get(2)),
Integer.parseInt(numbers.get(3)));
}
@SuppressWarnings("MagicConstant")
private static Object parseFont(final String key, final String value, final Map<Object, Object> accumulator,
final UIDefaults currentDefaults) {
String val = value;
Font base = null;
int size = -1;
int style = -1;
while (true) {
if (val.startsWith(FONT_FROM)) {
Pair<Font, String> result = parseFrom(val, accumulator, currentDefaults);
base = result.getFirst();
val = result.getSecond();
} else if (val.startsWith(FONT_SIZE)) {
Pair<Integer, String> result = parseFontAttribute(FONT_SIZE, val);
size = result.getFirst();
val = result.getSecond();
} else if (val.startsWith(FONT_STYLE)) {
Pair<Integer, String> result = parseFontAttribute(FONT_STYLE, val);
style = result.getFirst();
val = result.getSecond();
} else {
break;
}
}
if (base == null) base = parseExplicitFont(value);
if (base == null && accumulator.get(key) instanceof Font) base = (Font) accumulator.get(key);
if (base == null) base = currentDefaults.getFont(key);
if (base == null) base = new Font("Dialog", Font.PLAIN, 12);
if (size > 0) base = base.deriveFont((float) size);
if (style >= 0) base = base.deriveFont(style);
return new FontUIResource(base.deriveFont(attributes));
}
private static Object parseFont(final String value) {
Font font;
private static Font parseExplicitFont(final String value) {
try {
final String[] decode = value.split("-");
font = new FontUIResource(decode[0], Integer.parseInt(decode[1]), Integer.parseInt(decode[2]));
return new FontUIResource(decode[0], Integer.parseInt(decode[1]), Integer.parseInt(decode[2]));
} catch (final Exception e) {
font = new FontUIResource("Dialog", Font.PLAIN, 12);
return null;
}
return new FontUIResource(font.deriveFont(attributes));
}
private static Pair<Integer, String> parseFontAttribute(final String identifier, final String val) {
String key = val.substring(identifier.length() + 1);
int lastIndex = key.indexOf(')');
String rest = key.substring(lastIndex + 1);
key = key.substring(0, lastIndex);
try {
return new Pair<>(Integer.parseInt(key), rest);
} catch (NumberFormatException e) {
return new Pair<>(-1, rest);
}
}
private static Pair<Font, String> parseFrom(final String val,
final Map<Object, Object> accumulator,
final UIDefaults currentDefaults) {
String key = val.substring(FONT_FROM.length() + 1);
int index = key.indexOf(')');
String rest = key.substring(index + 1);
key = key.substring(0, index);
Font font = null;
if (accumulator.get(key) instanceof Font) font = (Font) accumulator.get(key);
if (font == null) font = currentDefaults.getFont(key);
return new Pair<>(font, rest);
}
private static Icon parseIcon(final String value, final IconLoader iconLoader) {
@ -284,7 +344,7 @@ public final class PropertyLoader {
} else {
Object obj = parseObject(value);
if (obj == null) {
obj = parseValue(key, value, true, defaults, ICON_LOADER);
obj = parseValue(key, value, true, defaults, defaults, ICON_LOADER);
if (obj instanceof ObjectRequest) {
LOGGER.severe("Failed to resolve object. " + this);
return;

Loading…
Cancel
Save