├── .gitignore ├── LICENSE ├── README.md ├── SvgNode.png ├── build.gradle ├── contributors.txt ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main └── java ├── eu └── hansolo │ └── fx │ └── svgnode │ ├── Demo.java │ ├── Helper.java │ ├── SvgNode.java │ ├── SvgPath.java │ └── SvgPathBuilder.java └── module-info.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | /.gradle 26 | /.idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # svgnode 2 | A JavaFX Canvas based node that can be used to visualize multiple SVG paths with their fill and stroke 3 | 4 | ## SvgNode 5 | A JavaFX control that uses a Canvas node to visualize multiple SVG path elements. 6 | Each SVG path can have it's own 7 | - Fill 8 | - Stroke 9 | - StrokeWidth 10 | - LineCap 11 | - LineJoin 12 | - Effect 13 | 14 | 15 | ## Overview 16 | ![Overview](https://raw.githubusercontent.com/HanSolo/svgnode/master/SvgNode.png) 17 | -------------------------------------------------------------------------------- /SvgNode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HanSolo/svgnode/ba3109357c97e95255cb3d0f582f854b12e4da6f/SvgNode.png -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'application' 4 | id 'org.openjfx.javafxplugin' version '0.0.7' 5 | } 6 | 7 | group 'eu.hansolo.fx' 8 | version '1.0-SNAPSHOT' 9 | 10 | ext { 11 | appName = "SvgNode" 12 | appLauncher = "SvgNode" 13 | appPackaging = "SvgNode" 14 | appModuleName = "svgnode" 15 | appMainClass = "eu.hansolo.fx.Demo" 16 | } 17 | 18 | mainClassName = "${ext.appModuleName}/${ext.appMainClass}" 19 | 20 | javafx { 21 | modules = [ "javafx.base", "javafx.graphics", "javafx.controls" ] 22 | } 23 | 24 | 25 | repositories { 26 | mavenCentral() 27 | } 28 | 29 | dependencies { 30 | 31 | } 32 | 33 | jar { 34 | manifest { 35 | attributes( 36 | 'Build-Timestamp' : new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()), 37 | 'Build-Tool' : "Gradle ${gradle.gradleVersion}", 38 | 'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})", 39 | 'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}", 40 | 'Application-Version': version, 41 | //'Git-Revision' : gitRevision, 42 | //'Git-Abb-Revision' : gitAbbRevision 43 | ) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /contributors.txt: -------------------------------------------------------------------------------- 1 | Gerrit Grunwald 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HanSolo/svgnode/ba3109357c97e95255cb3d0f582f854b12e4da6f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'svgnode' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/eu/hansolo/fx/svgnode/Demo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 by Gerrit Grunwald 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package eu.hansolo.fx.svgnode; 18 | 19 | import javafx.application.Application; 20 | import javafx.scene.effect.BlurType; 21 | import javafx.scene.effect.DropShadow; 22 | import javafx.scene.paint.Color; 23 | import javafx.scene.shape.StrokeLineCap; 24 | import javafx.scene.shape.StrokeLineJoin; 25 | import javafx.stage.Stage; 26 | import javafx.scene.layout.StackPane; 27 | import javafx.scene.Scene; 28 | 29 | 30 | /** 31 | * User: hansolo 32 | * Date: 2019-07-10 33 | * Time: 11:23 34 | */ 35 | public class Demo extends Application { 36 | private SvgPath svgPath1; 37 | private SvgPath svgPath2; 38 | private SvgPath svgPath3; 39 | private SvgPath svgPath4; 40 | private SvgPath svgPath5; 41 | private SvgPath svgPath6; 42 | private SvgPath svgPath7; 43 | private SvgNode svgNode; 44 | 45 | @Override public void init() { 46 | svgPath1 = SvgPathBuilder.create() 47 | .path("M235.135,235.135C159.925,310.345 135.231,275.075 26.004,422.225C17.921,433.114 20.496,448.544 31.694,456.194L33.637,457.521C38,460.501 43.224,461.88 48.503,461.642C58.83,461.176 80.859,464.825 116.106,492.951C167.374,533.862 226.035,500.058 252.374,473.719C281.232,450.167 320.774,395.21 285.236,340.081C273.056,321.188 266.597,306.174 263.368,294.601C258.079,275.643 275.644,258.078 294.602,263.367C306.175,266.596 321.188,273.055 340.082,285.235C395.21,320.773 450.168,281.233 473.72,252.373C500.06,226.033 533.863,167.373 492.952,116.105C464.826,80.858 461.176,58.829 461.643,48.502C461.881,43.223 460.503,37.999 457.522,33.636L456.195,31.693C448.545,20.495 433.115,17.921 422.226,26.003C275.075,135.231 310.344,159.925 235.135,235.135Z") 48 | .fill(Color.web("#FD6F71")) 49 | .stroke(Color.TRANSPARENT) 50 | .strokeWidth(0) 51 | .build(); 52 | 53 | svgPath2 = SvgPathBuilder.create() 54 | .path("M85.968,403.053C106.035,377.514 146.121,331.223 206.362,303.757C214.498,300.047 224.098,301.902 230.29,308.353C252.48,331.471 295.699,389.535 234.658,451.558C177.284,509.854 117.679,460.332 87.834,431.163C80.132,423.636 79.314,411.522 85.968,403.053Z") 55 | .fill(Color.web("#85E7FF")) 56 | .stroke(Color.TRANSPARENT) 57 | .strokeWidth(0) 58 | .build(); 59 | 60 | svgPath3 = SvgPathBuilder.create() 61 | .path("M109.686,409.31C101.985,401.784 101.167,389.668 107.82,381.2C113.481,373.995 120.737,365.137 129.588,355.518C110.289,373.237 95.743,390.609 85.966,403.054C79.313,411.521 80.131,423.637 87.832,431.164C117.677,460.334 177.283,509.855 234.656,451.559C238.431,447.724 241.803,443.904 244.81,440.106C191.186,481.359 137.507,436.5 109.686,409.31Z") 62 | .fill(Color.web("#57D0E6")) 63 | .stroke(Color.TRANSPARENT) 64 | .strokeWidth(0) 65 | .build(); 66 | 67 | svgPath4 = SvgPathBuilder.create() 68 | .path("M61.163,438.023C64.067,435.193 62.465,430.262 58.452,429.679L45.283,427.766C43.689,427.534 42.312,426.534 41.599,425.09L35.71,413.157C33.915,409.52 28.729,409.52 26.935,413.157L21.046,425.09C20.333,426.534 18.956,427.535 17.362,427.766L4.193,429.679C0.18,430.262 -1.421,435.194 1.482,438.023L11.01,447.311C12.164,448.435 12.689,450.054 12.417,451.641L10.169,464.757C9.484,468.754 13.679,471.801 17.268,469.914L29.046,463.722C30.471,462.973 32.174,462.973 33.599,463.722L45.377,469.914C48.966,471.801 53.161,468.753 52.476,464.757L50.227,451.642C49.955,450.055 50.48,448.436 51.634,447.312L61.163,438.023Z") 69 | .fill(Color.web("#FADC60")) 70 | .stroke(Color.TRANSPARENT) 71 | .strokeWidth(0) 72 | .build(); 73 | 74 | svgPath5 = SvgPathBuilder.create() 75 | .path("M403.053,85.968C377.514,106.035 331.223,146.121 303.757,206.362C300.047,214.498 301.902,224.098 308.353,230.29C331.471,252.48 389.535,295.699 451.558,234.658C509.854,177.284 460.332,117.679 431.163,87.834C423.636,80.132 411.52,79.315 403.053,85.968Z") 76 | .fill(Color.web("#85E7FF")) 77 | .stroke(Color.TRANSPARENT) 78 | .strokeWidth(0) 79 | .build(); 80 | 81 | svgPath6 = SvgPathBuilder.create() 82 | .path("M330.206,208.437C323.755,202.245 321.902,192.645 325.61,184.509C339.693,153.62 358.724,128.045 377.37,107.736C352.891,130.259 323.473,163.116 303.756,206.362C300.046,214.498 301.9,224.098 308.352,230.29C331.47,252.48 389.534,295.699 451.556,234.658C455.511,230.765 458.954,226.863 461.957,222.959C404.341,268.579 351.895,229.255 330.206,208.437Z") 83 | .fill(Color.web("#57D0E6")) 84 | .stroke(Color.TRANSPARENT) 85 | .strokeWidth(0) 86 | .build(); 87 | 88 | svgPath7 = SvgPathBuilder.create() 89 | .path("M463.722,33.601C462.972,32.175 462.972,30.473 463.722,29.048L469.914,17.27C471.8,13.681 468.753,9.486 464.757,10.171L451.642,12.421C450.055,12.693 448.436,12.167 447.311,11.014L438.023,1.485C435.192,-1.419 430.262,0.184 429.677,4.196L427.763,17.365C427.531,18.959 426.531,20.336 425.087,21.049L413.154,26.939C409.517,28.734 409.517,33.918 413.154,35.713L425.087,41.603C426.531,42.316 427.532,43.693 427.763,45.287L429.677,58.456C430.26,62.469 435.192,64.072 438.023,61.167L447.311,51.638C448.435,50.485 450.054,49.959 451.642,50.231L464.757,52.481C468.753,53.166 471.801,48.971 469.914,45.382L463.722,33.601Z") 90 | .fill(Color.web("#FADC60")) 91 | .stroke(Color.TRANSPARENT) 92 | .strokeWidth(0) 93 | .build(); 94 | 95 | svgNode = new SvgNode(svgPath1, svgPath2, svgPath3, svgPath4, svgPath5, svgPath6, svgPath7); 96 | svgNode.setPrefSize(512, 512); 97 | } 98 | 99 | @Override public void start(Stage stage) { 100 | StackPane pane = new StackPane(svgNode); 101 | 102 | Scene scene = new Scene(pane); 103 | 104 | stage.setTitle("SvgNode"); 105 | stage.setScene(scene); 106 | stage.show(); 107 | } 108 | 109 | @Override public void stop() { 110 | System.exit(0); 111 | } 112 | 113 | public static void main(String[] args) { 114 | launch(args); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/eu/hansolo/fx/svgnode/Helper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 by Gerrit Grunwald 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package eu.hansolo.fx.svgnode; 18 | 19 | public class Helper { 20 | 21 | public static final double clamp(final double min, final double max, final double value) { 22 | if (value < min) return min; 23 | if (value > max) return max; 24 | return value; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/eu/hansolo/fx/svgnode/SvgNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 by Gerrit Grunwald 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package eu.hansolo.fx.svgnode; 18 | 19 | import javafx.beans.value.ChangeListener; 20 | import javafx.collections.FXCollections; 21 | import javafx.collections.ListChangeListener; 22 | import javafx.collections.ObservableList; 23 | import javafx.geometry.Dimension2D; 24 | import javafx.scene.canvas.Canvas; 25 | import javafx.scene.canvas.GraphicsContext; 26 | import javafx.scene.layout.Region; 27 | 28 | import java.util.Arrays; 29 | import java.util.List; 30 | 31 | 32 | /** 33 | * User: hansolo 34 | * Date: 2019-07-10 35 | * Time: 07:14 36 | */ 37 | public class SvgNode extends Region { 38 | private static final double PREFERRED_WIDTH = -1; 39 | private static final double PREFERRED_HEIGHT = -1; 40 | private static final double MINIMUM_WIDTH = 5; 41 | private static final double MINIMUM_HEIGHT = 5; 42 | private static final double MAXIMUM_WIDTH = 4096; 43 | private static final double MAXIMUM_HEIGHT = 4096; 44 | private static double aspectRatio; 45 | private boolean keepAspect; 46 | private boolean dirty; 47 | private double size; 48 | private double width; 49 | private double height; 50 | private Canvas canvas; 51 | private GraphicsContext ctx; 52 | private ObservableList shapes; 53 | private double scaleX; 54 | private double scaleY; 55 | private ChangeListener dirtyListener; 56 | 57 | 58 | // ******************** Constructors ************************************** 59 | public SvgNode() { 60 | this(new SvgPath[]{}); 61 | } 62 | public SvgNode(final SvgPath... shapes) { 63 | this(true, Arrays.asList(shapes)); 64 | } 65 | public SvgNode(final boolean keepAspect, final List shapes) { 66 | this.shapes = FXCollections.observableArrayList(shapes); 67 | this.scaleX = 1.0; 68 | this.scaleY = 1.0; 69 | this.keepAspect = keepAspect; 70 | this.dirty = true; 71 | this.dirtyListener = (o, ov, nv) -> redraw(); 72 | 73 | initGraphics(); 74 | registerListeners(); 75 | } 76 | 77 | 78 | // ******************** Initialization ************************************ 79 | private void initGraphics() { 80 | setPrefSize(PREFERRED_WIDTH, PREFERRED_HEIGHT); 81 | 82 | canvas = new Canvas(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE); 83 | ctx = canvas.getGraphicsContext2D(); 84 | 85 | getChildren().setAll(canvas); 86 | } 87 | 88 | private void registerListeners() { 89 | widthProperty().addListener(o -> resize()); 90 | heightProperty().addListener(o -> resize()); 91 | shapes.addListener((ListChangeListener) c -> { 92 | while(c.next()) { 93 | if (c.wasAdded()) { 94 | c.getAddedSubList().forEach(svgPath -> svgPath.dirtyProperty().addListener(dirtyListener)); 95 | } else if (c.wasRemoved()) { 96 | c.getRemoved().forEach(svgPath -> svgPath.dirtyProperty().removeListener(dirtyListener)); 97 | } 98 | } 99 | redraw(); 100 | }); 101 | shapes.forEach(svgPath -> svgPath.dirtyProperty().addListener(dirtyListener)); 102 | } 103 | 104 | 105 | // ******************** Methods ******************************************* 106 | @Override protected double computeMinWidth(final double HEIGHT) { return MINIMUM_WIDTH; } 107 | @Override protected double computeMinHeight(final double WIDTH) { return MINIMUM_HEIGHT; } 108 | @Override protected double computePrefWidth(final double HEIGHT) { return super.computePrefWidth(HEIGHT); } 109 | @Override protected double computePrefHeight(final double WIDTH) { return super.computePrefHeight(WIDTH); } 110 | @Override protected double computeMaxWidth(final double HEIGHT) { return MAXIMUM_WIDTH; } 111 | @Override protected double computeMaxHeight(final double WIDTH) { return MAXIMUM_HEIGHT; } 112 | 113 | public ObservableList getShapes() { return shapes; } 114 | 115 | public Dimension2D getCanvasDimension() { return new Dimension2D(canvas.getWidth(), canvas.getHeight()); } 116 | public void setCanvasDimension(final double width, final double height) { 117 | canvas.setWidth(width); 118 | canvas.setHeight(height); 119 | } 120 | 121 | public boolean getKeepAspect() { return keepAspect; } 122 | public void setKeepAspect(final boolean keepAspect) { 123 | this.keepAspect = keepAspect; 124 | resize(); 125 | } 126 | 127 | 128 | // ******************** Resizing ****************************************** 129 | private void resize() { 130 | width = getWidth() - getInsets().getLeft() - getInsets().getRight(); 131 | height = getHeight() - getInsets().getTop() - getInsets().getBottom(); 132 | size = width < height ? width : height; 133 | 134 | if (width > 0 && height > 0) { 135 | if (canvas.getWidth() == Region.USE_PREF_SIZE) { canvas.setWidth(width); } 136 | if (canvas.getHeight() == Region.USE_PREF_SIZE) { canvas.setHeight(height); } 137 | if (canvas.getWidth() != Region.USE_PREF_SIZE && canvas.getHeight() != Region.USE_PREF_SIZE) { 138 | aspectRatio = canvas.getHeight() / canvas.getWidth(); 139 | } 140 | 141 | if (keepAspect) { 142 | if (aspectRatio * width > height) { 143 | width = 1 / (aspectRatio / height); 144 | } else if (1 / (aspectRatio / height) > width) { 145 | height = aspectRatio * width; 146 | } 147 | } 148 | 149 | scaleX = width / canvas.getWidth(); 150 | scaleY = height / canvas.getHeight(); 151 | 152 | canvas.relocate((getWidth() - width) * 0.5, (getHeight() - height) * 0.5); 153 | 154 | canvas.setScaleX(scaleX); 155 | canvas.setScaleY(scaleY); 156 | canvas.setTranslateX((canvas.getWidth() * scaleX - canvas.getWidth()) / 2); 157 | canvas.setTranslateY((canvas.getHeight() * scaleY - canvas.getHeight()) / 2); 158 | 159 | if (dirty) { redraw(); } 160 | } 161 | } 162 | 163 | private void redraw() { 164 | ctx.clearRect(0, 0, width, height); 165 | shapes.forEach(svgPath -> { 166 | svgPath.draw(ctx); 167 | svgPath.dirtyReset(); 168 | }); 169 | dirty = false; 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/main/java/eu/hansolo/fx/svgnode/SvgPath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 by Gerrit Grunwald 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package eu.hansolo.fx.svgnode; 18 | 19 | import javafx.beans.property.BooleanProperty; 20 | import javafx.beans.property.BooleanPropertyBase; 21 | import javafx.beans.property.DoubleProperty; 22 | import javafx.beans.property.DoublePropertyBase; 23 | import javafx.beans.property.ObjectProperty; 24 | import javafx.beans.property.ObjectPropertyBase; 25 | import javafx.beans.property.ReadOnlyBooleanProperty; 26 | import javafx.beans.property.StringProperty; 27 | import javafx.beans.property.StringPropertyBase; 28 | import javafx.scene.canvas.GraphicsContext; 29 | import javafx.scene.effect.Effect; 30 | import javafx.scene.paint.Color; 31 | import javafx.scene.paint.Paint; 32 | import javafx.scene.shape.FillRule; 33 | import javafx.scene.shape.StrokeLineCap; 34 | import javafx.scene.shape.StrokeLineJoin; 35 | 36 | 37 | public class SvgPath { 38 | private String _path; 39 | private StringProperty path; 40 | private Paint _fill; 41 | private ObjectProperty fill; 42 | private Paint _stroke; 43 | private ObjectProperty stroke; 44 | private double _strokeWidth; 45 | private DoubleProperty strokeWidth; 46 | private FillRule _fillRule; 47 | private ObjectProperty fillRule; 48 | private Effect _effect; 49 | private ObjectProperty effect; 50 | private boolean _visible; 51 | private BooleanProperty visible; 52 | private StrokeLineJoin _lineJoin; 53 | private ObjectProperty lineJoin; 54 | private StrokeLineCap _lineCap; 55 | private ObjectProperty lineCap; 56 | private BooleanProperty dirty; 57 | 58 | 59 | // ******************* Constructors *************************************** 60 | public SvgPath() { 61 | this("", Color.BLACK, Color.BLACK, 1.0, FillRule.NON_ZERO, null, true); 62 | } 63 | public SvgPath(final String path, final Paint fill, final Paint stroke, final double strokeWidth, final FillRule fillRule, final Effect effect, final boolean visible) { 64 | _path = path; 65 | _fill = fill; 66 | _stroke = stroke; 67 | _strokeWidth = Helper.clamp(0, Double.MAX_VALUE, strokeWidth); 68 | _fillRule = fillRule; 69 | _effect = effect; 70 | _visible = visible; 71 | _lineJoin = StrokeLineJoin.MITER; 72 | _lineCap = StrokeLineCap.SQUARE; 73 | dirty = new BooleanPropertyBase(false) { 74 | @Override public Object getBean() { return SvgPath.this; } 75 | @Override public String getName() { return "dirty"; } 76 | }; 77 | } 78 | 79 | 80 | // ******************* Methods ******************************************** 81 | public String getPath() { return null == path ? _path : path.get(); } 82 | public void setPath(final String path) { 83 | if (null == this.path) { 84 | _path = path; 85 | dirty.set(true); 86 | } else { 87 | this.path.set(path); 88 | } 89 | } 90 | public StringProperty pathProperty() { 91 | if (null == path) { 92 | path = new StringPropertyBase(_path) { 93 | @Override protected void invalidated() { dirty.set(true); } 94 | @Override public Object getBean() { return SvgPath.this; } 95 | @Override public String getName() { return "path"; } 96 | }; 97 | _path = null; 98 | } 99 | return path; 100 | } 101 | 102 | public Paint getFill() { return null == fill ? _fill : fill.get(); } 103 | public void setFill(final Paint fill) { 104 | if (null == this.fill) { 105 | _fill = fill; 106 | dirty.set(true); 107 | } else { 108 | this.fill.set(fill); 109 | } 110 | } 111 | public ObjectProperty fillProperty() { 112 | if (null == fill) { 113 | fill = new ObjectPropertyBase<>(_fill) { 114 | @Override protected void invalidated() { dirty.set(true); } 115 | @Override public Object getBean() { return SvgPath.this; } 116 | @Override public String getName() { return "fill"; } 117 | }; 118 | _fill = null; 119 | } 120 | return fill; 121 | } 122 | 123 | public Paint getStroke() { return null == stroke ? _stroke : stroke.get(); } 124 | public void setStroke(final Paint stroke) { 125 | if (null == this.stroke) { 126 | _stroke = stroke; 127 | dirty.set(true); 128 | } else { 129 | this.stroke.set(stroke); 130 | } 131 | } 132 | public ObjectProperty strokeProperty() { 133 | if (null == stroke) { 134 | stroke = new ObjectPropertyBase<>(_stroke) { 135 | @Override protected void invalidated() { dirty.set(true); } 136 | @Override public Object getBean() { return SvgPath.this; } 137 | @Override public String getName() { return "stroke"; } 138 | }; 139 | _stroke = null; 140 | } 141 | return stroke; 142 | } 143 | 144 | public double getStrokeWidth() { return null == strokeWidth ? _strokeWidth : strokeWidth.get(); } 145 | public void setStrokeWidth(final double strokeWidth) { 146 | if (null == this.strokeWidth) { 147 | _strokeWidth = Helper.clamp(0, Double.MAX_VALUE, strokeWidth); 148 | dirty.set(true); 149 | } else { 150 | this.strokeWidth.set(strokeWidth); 151 | } 152 | } 153 | public DoubleProperty strokeWidthProperty() { 154 | if (null == strokeWidth) { 155 | strokeWidth = new DoublePropertyBase(_strokeWidth) { 156 | @Override protected void invalidated() { 157 | set(Helper.clamp(0, Double.MAX_VALUE, get())); 158 | dirty.set(true); 159 | } 160 | @Override public Object getBean() { return SvgPath.this; } 161 | @Override public String getName() { return "strokeWidth"; } 162 | }; 163 | } 164 | return strokeWidth; 165 | } 166 | 167 | public FillRule getFillRule() { return null == fillRule ? _fillRule : fillRule.get(); } 168 | public void setFillRule(final FillRule fillRule) { 169 | if (null == this.fillRule) { 170 | _fillRule = fillRule; 171 | dirty.set(true); 172 | } else { 173 | this.fillRule.set(fillRule); 174 | } 175 | } 176 | public ObjectProperty fillRuleProperty() { 177 | if (null == fillRule) { 178 | fillRule = new ObjectPropertyBase<>(_fillRule) { 179 | @Override protected void invalidated() { dirty.set(true); } 180 | @Override public Object getBean() { return SvgPath.this; } 181 | @Override public String getName() { return "fillRule"; } 182 | }; 183 | _fillRule = null; 184 | } 185 | return fillRule; 186 | } 187 | 188 | public Effect getEffect() { return null == effect ? _effect : effect.get(); } 189 | public void setEffect(final Effect effect) { 190 | if (null == this.effect) { 191 | _effect = effect; 192 | dirty.set(true); 193 | } else { 194 | this.effect.set(effect); 195 | } 196 | } 197 | public ObjectProperty effectProperty() { 198 | if (null == effect) { 199 | effect = new ObjectPropertyBase<>(_effect) { 200 | @Override protected void invalidated() { dirty.set(true); } 201 | @Override public Object getBean() { return SvgPath.this; } 202 | @Override public String getName() { return "effect"; } 203 | }; 204 | _effect = null; 205 | } 206 | return effect; 207 | } 208 | 209 | public boolean isVisible() { return null == visible ? _visible : visible.get(); } 210 | public void setVisible(final boolean visible) { 211 | if (null == this.visible) { 212 | _visible = visible; 213 | dirty.set(true); 214 | } else { 215 | this.visible.set(visible); 216 | } 217 | } 218 | public BooleanProperty visibleProperty() { 219 | if (null == visible) { 220 | visible = new BooleanPropertyBase(_visible) { 221 | @Override protected void invalidated() { dirty.set(true); } 222 | @Override public Object getBean() { return SvgPath.this; } 223 | @Override public String getName() { return "visible"; } 224 | }; 225 | } 226 | return visible; 227 | } 228 | 229 | public boolean isDirty() { return dirty.get(); } 230 | protected void dirtyReset() { dirty.set(false); } 231 | public ReadOnlyBooleanProperty dirtyProperty() { return dirty; } 232 | 233 | public StrokeLineJoin getLineJoin() { return null == lineJoin ? _lineJoin : lineJoin.get(); } 234 | public void setLineJoin(final StrokeLineJoin lineJoin) { 235 | if (null == this.lineJoin) { 236 | _lineJoin = lineJoin; 237 | dirty.set(true); 238 | } else { 239 | this.lineJoin.set(lineJoin); 240 | } 241 | } 242 | public ObjectProperty lineJoinProperty() { 243 | if (null == lineJoin) { 244 | lineJoin = new ObjectPropertyBase(_lineJoin) { 245 | @Override protected void invalidated() { dirty.set(true); } 246 | @Override public Object getBean() { return SvgPath.this; } 247 | @Override public String getName() { return "lineJoin"; } 248 | }; 249 | _lineJoin = null; 250 | } 251 | return lineJoin; 252 | } 253 | 254 | public StrokeLineCap getLineCap() { return null == lineCap ? _lineCap : lineCap.get(); } 255 | public void setLineCap(final StrokeLineCap lineCap) { 256 | if (null == this.lineCap) { 257 | _lineCap = lineCap; 258 | dirty.set(true); 259 | } else { 260 | this.lineCap.set(lineCap); 261 | } 262 | } 263 | public ObjectProperty lineCapProperty() { 264 | if (null == lineCap) { 265 | lineCap = new ObjectPropertyBase(_lineCap) { 266 | @Override protected void invalidated() { dirty.set(true); } 267 | @Override public Object getBean() { return SvgPath.this; } 268 | @Override public String getName() { return "lineCap"; } 269 | }; 270 | _lineCap = null; 271 | } 272 | return lineCap; 273 | } 274 | 275 | public void draw(final GraphicsContext ctx) { 276 | if (isVisible()) { 277 | 278 | ctx.save(); 279 | 280 | ctx.setEffect(getEffect()); 281 | ctx.setLineJoin(getLineJoin()); 282 | ctx.setLineCap(getLineCap()); 283 | ctx.setFill(getFill()); 284 | ctx.setStroke(getStroke()); 285 | ctx.beginPath(); 286 | 287 | SVGParser p = new SVGParser(getPath()); 288 | p.allowComma = false; 289 | boolean largeArcFlag = false, sweepFlag = false; 290 | double rx, ry, a; 291 | double x, y, lastX = -Double.MAX_VALUE, lastY = -Double.MAX_VALUE; 292 | double c1x, c1y, lastC1X = -Double.MAX_VALUE, lastC1Y = -Double.MAX_VALUE; 293 | double c2x, c2y, lastC2X = -Double.MAX_VALUE, lastC2Y = -Double.MAX_VALUE; 294 | long elementCount = 0; 295 | while (!p.isDone()) { 296 | p.allowComma = false; 297 | char cmd = p.getChar(); 298 | switch (cmd) { 299 | case 'M': 300 | x = p.f(); 301 | y = p.f(); 302 | ctx.moveTo(x, y); 303 | lastX = x; 304 | lastY = y; 305 | while (p.nextIsNumber()) { 306 | x = p.f(); 307 | y = p.f(); 308 | ctx.lineTo(x, y); 309 | lastX = x; 310 | lastY = y; 311 | } 312 | elementCount++; 313 | break; 314 | case 'm': 315 | if (elementCount > 0) { 316 | x = p.f() - lastX; 317 | y = p.f() - lastY; 318 | ctx.moveTo(x, y); // move relative 319 | lastX = x; 320 | lastY = y; 321 | } else { 322 | x = p.f(); 323 | y = p.f(); 324 | ctx.moveTo(x, y); 325 | lastX = x; 326 | lastY = y; 327 | } 328 | while (p.nextIsNumber()) { 329 | x = p.f() - lastX; 330 | y = p.f() - lastY; 331 | ctx.lineTo(p.f(), p.f()); // move relative 332 | lastX = x; 333 | lastY = y; 334 | } 335 | elementCount++; 336 | break; 337 | case 'L': 338 | do { 339 | x = p.f(); 340 | y = p.f(); 341 | ctx.lineTo(x, y); 342 | lastX = x; 343 | lastY = y; 344 | } while (p.nextIsNumber()); 345 | elementCount++; 346 | break; 347 | case 'l': 348 | do { 349 | x = p.f() - lastX; 350 | y = p.f() - lastY; 351 | ctx.lineTo(x, y); // move relative 352 | lastX = x; 353 | lastY = y; 354 | } while (p.nextIsNumber()); 355 | elementCount++; 356 | break; 357 | case 'H': 358 | do { 359 | x = p.f(); 360 | ctx.lineTo(x, lastY); 361 | lastX = x; 362 | } while (p.nextIsNumber()); 363 | elementCount++; 364 | break; 365 | case 'h': 366 | do { 367 | x = p.f(); 368 | ctx.lineTo(x, lastY); // move relative 369 | lastX = x; 370 | } while (p.nextIsNumber()); 371 | elementCount++; 372 | break; 373 | case 'V': 374 | do { 375 | y = p.f(); 376 | ctx.lineTo(lastX, y); 377 | lastY = y; 378 | } while (p.nextIsNumber()); 379 | elementCount++; 380 | break; 381 | case 'v': 382 | do { 383 | y = p.f(); 384 | ctx.lineTo(lastX, y); // move relative 385 | lastY = y; 386 | } while (p.nextIsNumber()); 387 | elementCount++; 388 | break; 389 | case 'Q': 390 | do { 391 | c1x = p.f(); 392 | c1y = p.f(); 393 | x = p.f(); 394 | y = p.f(); 395 | ctx.quadraticCurveTo(c1x, c1y, x, y); 396 | lastC1X = c1x; 397 | lastC1Y = c1y; 398 | lastX = x; 399 | lastY = y; 400 | } while (p.nextIsNumber()); 401 | elementCount++; 402 | break; 403 | case 'q': 404 | do { 405 | c1x = p.f() - lastC1X; 406 | c1y = p.f() - lastC1Y; 407 | x = p.f() - lastX; 408 | y = p.f() - lastY; 409 | ctx.quadraticCurveTo(c1x, c1y, x, y); // relative move 410 | lastC1X = c1x; 411 | lastC1Y = c1y; 412 | lastX = x; 413 | lastY = y; 414 | } while (p.nextIsNumber()); 415 | elementCount++; 416 | break; 417 | /* 418 | case 'T': 419 | do { 420 | ctx.quadraticCurveToSmooth(p.f(), p.f()); 421 | } while (p.nextIsNumber()); 422 | break; 423 | case 't': 424 | do { 425 | ctx.quadraticCurveToSmoothRel(p.f(), p.f()); 426 | } while (p.nextIsNumber()); 427 | break; 428 | */ 429 | case 'C': 430 | do { 431 | c1x = p.f(); 432 | c1y = p.f(); 433 | c2x = p.f(); 434 | c2y = p.f(); 435 | x = p.f(); 436 | y = p.f(); 437 | ctx.bezierCurveTo(c1x, c1y, c2x, c2y, x, y); 438 | lastC1X = c1x; 439 | lastC1Y = c1y; 440 | lastC2X = c2x; 441 | lastC2Y = c2y; 442 | lastX = x; 443 | lastY = y; 444 | } while (p.nextIsNumber()); 445 | elementCount++; 446 | break; 447 | case 'c': 448 | do { 449 | c1x = p.f() - lastC1X; 450 | c1y = p.f() - lastC1Y; 451 | c2x = p.f() - lastC2X; 452 | c2y = p.f() - lastC2Y; 453 | x = p.f() - lastX; 454 | y = p.f() - lastY; 455 | ctx.bezierCurveTo(c1x, c1y, c2x, c2y, x, y); // move relative 456 | lastC1X = c1x; 457 | lastC1Y = c1y; 458 | lastC2X = c2x; 459 | lastC2Y = c2y; 460 | lastX = x; 461 | lastY = y; 462 | } while (p.nextIsNumber()); 463 | elementCount++; 464 | break; 465 | /* 466 | case 'S': 467 | do { 468 | ctx.bezierCurveToSmooth(p.f(), p.f(), p.f(), p.f()); 469 | } while (p.nextIsNumber()); 470 | elementCount++; 471 | break; 472 | case 's': 473 | do { 474 | ctx.bezierCurveToSmoothRel(p.f(), p.f(), p.f(), p.f()); 475 | } while (p.nextIsNumber()); 476 | elementCount++; 477 | break; 478 | */ 479 | case 'A': 480 | do { 481 | rx = p.f(); 482 | ry = p.f(); 483 | a = p.a(); 484 | largeArcFlag = p.b(); 485 | sweepFlag = p.b(); 486 | x = p.f(); 487 | y = p.f(); 488 | //ctx.arcTo(rx, ry, a, largeArcFlag, sweepFlag, x, y); 489 | ctx.arc(x, y, rx, ry, 0, a); 490 | lastX = x; 491 | lastY = y; 492 | } while (p.nextIsNumber()); 493 | elementCount++; 494 | break; 495 | case 'a': 496 | do { 497 | rx = p.f(); 498 | ry = p.f(); 499 | a = p.a(); 500 | largeArcFlag = p.b(); 501 | sweepFlag = p.b(); 502 | x = p.f() - lastX; 503 | y = p.f() - lastY; 504 | ctx.arc(x, y, rx, ry, 0, a); // move relative 505 | lastX = x; 506 | lastY = y; 507 | 508 | } while (p.nextIsNumber()); 509 | elementCount++; 510 | break; 511 | case 'Z': 512 | case 'z': 513 | ctx.closePath(); 514 | elementCount++; 515 | break; 516 | default: 517 | throw new IllegalArgumentException("invalid command (" + cmd + ") in SVG polygon at pos=" + p.pos); 518 | } 519 | p.allowComma = false; 520 | } 521 | 522 | ctx.fill(); 523 | ctx.stroke(); 524 | 525 | ctx.restore(); 526 | } 527 | } 528 | 529 | static class SVGParser { 530 | final String svgpath; 531 | final int length; 532 | int pos; 533 | boolean allowComma; 534 | double lastX; 535 | double lastY; 536 | 537 | 538 | public SVGParser(final String SVG_PATH) { 539 | svgpath = SVG_PATH; 540 | length = SVG_PATH.length(); 541 | } 542 | 543 | 544 | public boolean isDone() { return (toNextNonWsp() >= length); } 545 | 546 | public char getChar() { return svgpath.charAt(pos++); } 547 | 548 | public boolean nextIsNumber() { 549 | if (toNextNonWsp() < length) { 550 | switch (svgpath.charAt(pos)) { 551 | case '-': 552 | case '+': 553 | case '0': case '1': case '2': case '3': case '4': 554 | case '5': case '6': case '7': case '8': case '9': 555 | case '.': return true; 556 | } 557 | } 558 | return false; 559 | } 560 | 561 | public double f() { return getDouble(); } 562 | 563 | public double a() { return Math.toRadians(getDouble()); } 564 | 565 | public double getDouble() { 566 | int start = toNextNonWsp(); 567 | int end = toNumberEnd(); 568 | allowComma = true; 569 | 570 | if (start < end) { 571 | String flstr = svgpath.substring(start, end); 572 | try { 573 | return Double.parseDouble(flstr); 574 | } catch (NumberFormatException e) { } 575 | throw new IllegalArgumentException("invalid double (" + flstr + ") in polygon at pos=" + start); 576 | } 577 | throw new IllegalArgumentException("end of polygon looking for double"); 578 | } 579 | 580 | public boolean b() { 581 | toNextNonWsp(); 582 | allowComma = true; 583 | if (pos < length) { 584 | char flag = svgpath.charAt(pos); 585 | switch (flag) { 586 | case '0': pos++; return false; 587 | case '1': pos++; return true; 588 | } 589 | throw new IllegalArgumentException("invalid boolean flag (" + flag + ") in polygon at pos=" + pos); 590 | } 591 | throw new IllegalArgumentException("end of polygon looking for boolean"); 592 | } 593 | 594 | private int toNextNonWsp() { 595 | boolean canBeComma = allowComma; 596 | while (pos < length) { 597 | switch (svgpath.charAt(pos)) { 598 | case ',': 599 | if (!canBeComma) { return pos; } 600 | canBeComma = false; 601 | break; 602 | case ' ': 603 | case '\t': 604 | case '\r': 605 | case '\n': 606 | break; 607 | default: 608 | return pos; 609 | } 610 | pos++; 611 | } 612 | return pos; 613 | } 614 | 615 | private int toNumberEnd() { 616 | boolean allowSign = true; 617 | boolean hasExp = false; 618 | boolean hasDecimal = false; 619 | while (pos < length) { 620 | switch (svgpath.charAt(pos)) { 621 | case '-': 622 | case '+': 623 | if (!allowSign) return pos; 624 | allowSign = false; 625 | break; 626 | case '0': case '1': case '2': case '3': case '4': 627 | case '5': case '6': case '7': case '8': case '9': 628 | allowSign = false; 629 | break; 630 | case 'E': case 'e': 631 | if (hasExp) return pos; 632 | hasExp = allowSign = true; 633 | break; 634 | case '.': 635 | if (hasExp || hasDecimal) return pos; 636 | hasDecimal = true; 637 | allowSign = false; 638 | break; 639 | default: 640 | return pos; 641 | } 642 | pos++; 643 | } 644 | return pos; 645 | } 646 | } 647 | } 648 | -------------------------------------------------------------------------------- /src/main/java/eu/hansolo/fx/svgnode/SvgPathBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 by Gerrit Grunwald 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package eu.hansolo.fx.svgnode; 18 | 19 | import javafx.beans.property.BooleanProperty; 20 | import javafx.beans.property.DoubleProperty; 21 | import javafx.beans.property.ObjectProperty; 22 | import javafx.beans.property.Property; 23 | import javafx.beans.property.SimpleBooleanProperty; 24 | import javafx.beans.property.SimpleDoubleProperty; 25 | import javafx.beans.property.SimpleObjectProperty; 26 | import javafx.beans.property.SimpleStringProperty; 27 | import javafx.beans.property.StringProperty; 28 | import javafx.scene.effect.Effect; 29 | import javafx.scene.paint.Paint; 30 | import javafx.scene.shape.FillRule; 31 | import javafx.scene.shape.StrokeLineCap; 32 | import javafx.scene.shape.StrokeLineJoin; 33 | 34 | import java.util.HashMap; 35 | 36 | 37 | public class SvgPathBuilder> { 38 | private HashMap properties = new HashMap<>(); 39 | 40 | 41 | // ******************** Constructors ************************************** 42 | protected SvgPathBuilder() {} 43 | 44 | 45 | // ******************** Methods ******************************************* 46 | public static final SvgPathBuilder create() { 47 | return new SvgPathBuilder(); 48 | } 49 | 50 | public final B path(final String path) { 51 | properties.put("path", new SimpleStringProperty(path)); 52 | return (B)this; 53 | } 54 | 55 | public final B fill(final Paint fill) { 56 | properties.put("fill", new SimpleObjectProperty<>(fill)); 57 | return (B)this; 58 | } 59 | 60 | public final B stroke(final Paint stroke) { 61 | properties.put("stroke", new SimpleObjectProperty<>(stroke)); 62 | return (B)this; 63 | } 64 | 65 | public final B strokeWidth(final double strokeWidth) { 66 | properties.put("strokeWidth", new SimpleDoubleProperty(strokeWidth)); 67 | return (B)this; 68 | } 69 | 70 | public final B fillRule(final FillRule fillRule) { 71 | properties.put("fillRule", new SimpleObjectProperty<>(fillRule)); 72 | return (B)this; 73 | } 74 | 75 | public final B effect(final Effect effect) { 76 | properties.put("effect", new SimpleObjectProperty<>(effect)); 77 | return (B)this; 78 | } 79 | 80 | public final B visible(final boolean visible) { 81 | properties.put("visible", new SimpleBooleanProperty(visible)); 82 | return (B)this; 83 | } 84 | 85 | public final B lineJoin(final StrokeLineJoin lineJoin) { 86 | properties.put("lineJoin", new SimpleObjectProperty<>(lineJoin)); 87 | return (B)this; 88 | } 89 | 90 | public final B lineCap(final StrokeLineCap lineCap) { 91 | properties.put("lineCap", new SimpleObjectProperty<>(lineCap)); 92 | return (B)this; 93 | } 94 | 95 | public final SvgPath build() { 96 | final SvgPath svgPath = new SvgPath(); 97 | for (String key : properties.keySet()) { 98 | if ("path".equals(key)) { 99 | svgPath.setPath(((StringProperty) properties.get(key)).get()); 100 | } else if ("fill".equals(key)) { 101 | svgPath.setFill(((ObjectProperty) properties.get(key)).get()); 102 | } else if ("stroke".equals(key)) { 103 | svgPath.setStroke(((ObjectProperty) properties.get(key)).get()); 104 | } else if ("lineWidth".equals(key)) { 105 | svgPath.setStrokeWidth(((DoubleProperty) properties.get(key)).get()); 106 | } else if ("fillRule".equals(key)) { 107 | svgPath.setFillRule(((ObjectProperty) properties.get(key)).get()); 108 | } else if ("effect".equals(key)) { 109 | svgPath.setEffect(((ObjectProperty) properties.get(key)).get()); 110 | } else if("visible".equals(key)) { 111 | svgPath.setVisible(((BooleanProperty) properties.get(key)).get()); 112 | } else if("lineJoin".equals(key)) { 113 | svgPath.setLineJoin(((ObjectProperty) properties.get(key)).get()); 114 | } else if("lineCap".equals(key)) { 115 | svgPath.setLineCap(((ObjectProperty) properties.get(key)).get()); 116 | } 117 | } 118 | return svgPath; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | module svgnode { 2 | // Java 3 | requires java.base; 4 | 5 | // Java-FX 6 | requires transitive javafx.base; 7 | requires transitive javafx.graphics; 8 | requires transitive javafx.controls; 9 | 10 | exports eu.hansolo.fx.svgnode; 11 | } --------------------------------------------------------------------------------