From d24e3ba741418daf8ecb46b15caf7af5aef810b5 Mon Sep 17 00:00:00 2001
From: weisj <31143295+weisJ@users.noreply.github.com>
Date: Wed, 29 Sep 2021 23:50:44 +0200
Subject: [PATCH] Icons: Replace svgSalamander with JSVG
---
core/build.gradle.kts | 4 +-
.../components/ColoredRadioButton.java | 2 +-
.../core/documentation/CreateUITable.java | 77 ++---
.../github/weisj/darklaf/icon/AllIcons.java | 2 +-
gradle.properties | 2 +-
property-loader/build.gradle.kts | 2 +-
.../properties/icons/CustomThemedIcon.java | 37 +--
.../darklaf/properties/icons/DarkSVGIcon.java | 167 ++++-------
.../icons/DarkSVGIconDomProcessor.java | 48 +++
.../properties/icons/IconColorMapper.java | 278 +-----------------
.../darklaf/properties/icons/IconLoader.java | 41 ++-
.../properties/icons/ThemedSVGIcon.java | 40 ++-
.../icons/ThemedSVGIconParserProvider.java | 158 ++++++++++
.../src/main/module/module-info.java | 2 +-
.../properties/icons/SVGImageTest.java | 20 +-
settings.gradle.kts | 2 +-
16 files changed, 383 insertions(+), 499 deletions(-)
create mode 100644 property-loader/src/main/java/com/github/weisj/darklaf/properties/icons/DarkSVGIconDomProcessor.java
create mode 100644 property-loader/src/main/java/com/github/weisj/darklaf/properties/icons/ThemedSVGIconParserProvider.java
diff --git a/core/build.gradle.kts b/core/build.gradle.kts
index e75df4ad..80c82b8d 100644
--- a/core/build.gradle.kts
+++ b/core/build.gradle.kts
@@ -19,7 +19,7 @@ dependencies {
implementation(projects.darklafWindows)
implementation(projects.darklafMacos)
implementation(libs.swingDslLafSupport)
- implementation(libs.svgSalamander)
+ implementation(libs.jsvg)
compileOnly(libs.nullabilityAnnotations)
compileOnly(libs.swingx)
@@ -27,7 +27,7 @@ dependencies {
compileOnly(toolLibs.autoservice.annotations)
annotationProcessor(toolLibs.autoservice.processor)
- testImplementation(libs.svgSalamander)
+ testImplementation(libs.jsvg)
testImplementation(libs.swingx)
testImplementation(testLibs.bundles.miglayout)
testImplementation(testLibs.swingDslInspector)
diff --git a/core/src/main/java/com/github/weisj/darklaf/components/ColoredRadioButton.java b/core/src/main/java/com/github/weisj/darklaf/components/ColoredRadioButton.java
index e4fd38fb..c873b0e9 100644
--- a/core/src/main/java/com/github/weisj/darklaf/components/ColoredRadioButton.java
+++ b/core/src/main/java/com/github/weisj/darklaf/components/ColoredRadioButton.java
@@ -1,7 +1,7 @@
/*
* MIT License
*
- * Copyright (c) 2020-2021 Jannis Weis
+ * Copyright (c) 2020-2022 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,
diff --git a/core/src/test/java/com/github/weisj/darklaf/core/documentation/CreateUITable.java b/core/src/test/java/com/github/weisj/darklaf/core/documentation/CreateUITable.java
index ee23e66d..dd212df2 100644
--- a/core/src/test/java/com/github/weisj/darklaf/core/documentation/CreateUITable.java
+++ b/core/src/test/java/com/github/weisj/darklaf/core/documentation/CreateUITable.java
@@ -1,7 +1,7 @@
/*
* MIT License
*
- * Copyright (c) 2020-2021 Jannis Weis
+ * Copyright (c) 2020-2022 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,
@@ -21,12 +21,12 @@
package com.github.weisj.darklaf.core.documentation;
import java.awt.*;
+import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
-import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -40,23 +40,12 @@ import com.github.weisj.darklaf.DarkLaf;
import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.components.border.DropShadowBorder;
import com.github.weisj.darklaf.defaults.SampleRenderer;
-import com.github.weisj.darklaf.properties.icons.DarkSVGIcon;
-import com.github.weisj.darklaf.properties.icons.EmptyIcon;
-import com.github.weisj.darklaf.properties.icons.IconColorMapper;
-import com.github.weisj.darklaf.properties.icons.StateIcon;
+import com.github.weisj.darklaf.properties.icons.*;
import com.github.weisj.darklaf.properties.parser.ParseResult;
import com.github.weisj.darklaf.properties.parser.Parser;
import com.github.weisj.darklaf.properties.parser.PrimitiveParser;
import com.github.weisj.darklaf.theme.Theme;
-import com.github.weisj.darklaf.util.ColorUtil;
-import com.github.weisj.darklaf.util.ImageUtil;
-import com.github.weisj.darklaf.util.StringUtil;
-import com.github.weisj.darklaf.util.SystemInfo;
-import com.github.weisj.darklaf.util.Types;
-import com.kitfox.svg.LinearGradient;
-import com.kitfox.svg.SVGDiagram;
-import com.kitfox.svg.SVGElement;
-import com.kitfox.svg.app.beans.SVGIcon;
+import com.github.weisj.darklaf.util.*;
public class CreateUITable {
@@ -131,8 +120,8 @@ public class CreateUITable {
}
private UIDefaults setupThemeDefaults(final Theme theme) {
+ LafManager.installTheme(theme);
Parser.setDebugMode(true);
- LafManager.setTheme(theme);
UIDefaults defaults = new DarkLaf() {
@Override
public Theme getTheme() {
@@ -140,7 +129,6 @@ public class CreateUITable {
}
}.getDefaults();
Parser.setDebugMode(false);
- LafManager.installTheme(theme);
currentDefaults = UIManager.getLookAndFeelDefaults();
return defaults;
}
@@ -286,43 +274,36 @@ public class CreateUITable {
}
private String parseSVGIcon(final DarkSVGIcon value, final int ident) {
- SVGIcon icon = value.getSVGIcon();
StringBuilder sb = new StringBuilder(StringUtil.repeat(IDENT, ident)).append("
\n");
try {
- readFile(icon.getSvgURI().toURL(), sb, ident + 1);
- } catch (final IOException e) {
- e.printStackTrace();
- }
- sb.append(StringUtil.repeat(IDENT, ident)).append(" | \n");
-
- String svg = sb.toString();
-
- SVGDiagram svgDiagram = icon.getSvgUniverse().getDiagram(icon.getSvgURI());
- SVGElement defs = svgDiagram.getElement("colors");
- if (defs != null) {
- List> children = defs.getChildren(null);
- for (Object child : children) {
- if (child instanceof LinearGradient) {
- float opacity = IconColorMapper.getOpacity((LinearGradient) child, currentDefaults, null);
- if (opacity < 0) opacity = 1;
- Color color = IconColorMapper.getColor((LinearGradient) child, currentDefaults, null);
- String id = ((LinearGradient) child).getId();
- String match = "=\"url\\(#" + id + "\\)\"";
- String fillReplacement = "fill=\"#" + ColorUtil.toHex(color) + "\"";
- if (opacity != 1) {
- fillReplacement += " fill-opacity=\"" + opacity + "\"";
- svg = svg.replaceAll("fill-opacity=\"[^\"]*\"", "");
- }
- svg = svg.replaceAll("fill" + match, fillReplacement);
-
- String strokeReplacement = "stroke=\"#" + ColorUtil.toHex(color) + "\"";
- if (opacity != 1) strokeReplacement += " stroke-opacity=\"" + opacity + "\"";
- svg = svg.replaceAll("stroke" + match, strokeReplacement);
+ readFile(value.getURI().toURL(), sb, ident + 1);
+ sb.append(StringUtil.repeat(IDENT, ident)).append("\n");
+
+ String svg = sb.toString();
+
+ for (Map.Entry entry : ThemedSVGIconParserProvider.getNamedColors(
+ new CustomThemedIcon(value, currentDefaults, CustomThemedIcon.MergeMode.REMOVE_REFERENCES))
+ .entrySet()) {
+ String id = entry.getKey();
+ String match = "=\"url\\(#" + id + "\\)\"";
+ String colorHex = ColorUtil.toHex(ColorUtil.removeAlpha(entry.getValue()));
+ String fillReplacement = "fill=\"#" + colorHex + "\"";
+
+ float opacity = entry.getValue().getAlpha() / 255f;
+ if (opacity != 1) {
+ fillReplacement += " fill-opacity=\"" + opacity + "\"";
}
+ svg = svg.replaceAll("fill" + match, fillReplacement);
+
+ String strokeReplacement = "stroke=\"#" + colorHex + "\"";
+ if (opacity != 1) strokeReplacement += " stroke-opacity=\"" + opacity + "\"";
+ svg = svg.replaceAll("stroke" + match, strokeReplacement);
}
svg = svg.replaceAll("(\\n.*)* \\s+", "");
+ return svg;
+ } catch (final Exception e) {
+ throw new RuntimeException(e);
}
- return svg;
}
private void readFile(final URL url, final StringBuilder builder, final int ident) throws IOException {
diff --git a/core/src/test/java/com/github/weisj/darklaf/icon/AllIcons.java b/core/src/test/java/com/github/weisj/darklaf/icon/AllIcons.java
index 895e4c09..9efd3807 100644
--- a/core/src/test/java/com/github/weisj/darklaf/icon/AllIcons.java
+++ b/core/src/test/java/com/github/weisj/darklaf/icon/AllIcons.java
@@ -1,7 +1,7 @@
/*
* MIT License
*
- * Copyright (c) 2020-2021 Jannis Weis
+ * Copyright (c) 2020-2022 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,
diff --git a/gradle.properties b/gradle.properties
index bf059f77..4144a111 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -29,7 +29,7 @@ net.ltgt.errorprone.version = 2.0.2
# Dependencies
# Libraries
-svgSalamander.version = 1.1.2.4
+jsvg.version = 0.0.1
swingDsl.version = 0.1.3
swingx.version = 1.6.1
nullabilityAnnotations.version = 23.0.0
diff --git a/property-loader/build.gradle.kts b/property-loader/build.gradle.kts
index f724f126..9e6fcfa2 100644
--- a/property-loader/build.gradle.kts
+++ b/property-loader/build.gradle.kts
@@ -5,7 +5,7 @@ plugins {
dependencies {
api(projects.darklafUtils)
- implementation(libs.svgSalamander)
+ implementation(libs.jsvg)
implementation(libs.visualPaddings)
compileOnly(libs.nullabilityAnnotations)
compileOnly(toolLibs.errorprone.annotations)
diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/properties/icons/CustomThemedIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/properties/icons/CustomThemedIcon.java
index 303e94cb..e5606c5d 100644
--- a/property-loader/src/main/java/com/github/weisj/darklaf/properties/icons/CustomThemedIcon.java
+++ b/property-loader/src/main/java/com/github/weisj/darklaf/properties/icons/CustomThemedIcon.java
@@ -1,7 +1,7 @@
/*
* MIT License
*
- * Copyright (c) 2019-2021 Jannis Weis
+ * Copyright (c) 2019-2022 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,
@@ -22,12 +22,10 @@ package com.github.weisj.darklaf.properties.icons;
import java.net.URI;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
-import java.util.function.Supplier;
import com.github.weisj.darklaf.properties.PropertyLoader;
-import com.kitfox.svg.SVGUniverse;
-import com.kitfox.svg.app.beans.SVGIcon;
public class CustomThemedIcon extends ThemedSVGIcon implements MutableThemedIcon {
@@ -36,16 +34,6 @@ public class CustomThemedIcon extends ThemedSVGIcon implements MutableThemedIcon
private Map