Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TULIP
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
14
Issues
14
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
NUI
TULIP
Commits
6db84bf4
Commit
6db84bf4
authored
Aug 31, 2020
by
Nico Mack
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added method to directly fetch java Shapes from SVG files
parent
98967c61
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
8 deletions
+100
-8
TULIP/src/lu/list/itis/dkd/tui/content/builder/BaseLogoBuilder.java
...lu/list/itis/dkd/tui/content/builder/BaseLogoBuilder.java
+1
-1
TULIP/src/lu/list/itis/dkd/tui/utility/svg/SvgUtils.java
TULIP/src/lu/list/itis/dkd/tui/utility/svg/SvgUtils.java
+99
-7
No files found.
TULIP/src/lu/list/itis/dkd/tui/content/builder/BaseLogoBuilder.java
View file @
6db84bf4
...
...
@@ -166,7 +166,7 @@ public abstract class BaseLogoBuilder<B extends BaseLogoBuilder<B>> extends Cont
properties
.
nameSpace
=
this
.
identifier
;
properties
.
strokeColour
=
Color
.
white
;
SVGIcon
vector
=
SvgUtils
.
loadSVG
Resource
(
baseUrl
.
toURL
(),
imagePath
,
properties
);
SVGIcon
vector
=
SvgUtils
.
loadSVG
Icon
(
baseUrl
.
toURL
(),
imagePath
,
properties
);
original
=
SvgUtils
.
renderSVG
(
vector
,
new
Point
(
ScreenCoordinates
.
class
));
}
catch
(
MalformedURLException
exception
)
{
throw
new
BuildException
(
StringUtils
.
build
(
"The image {} could not be read!"
,
imagePath
),
exception
);
//$NON-NLS-1$
...
...
TULIP/src/lu/list/itis/dkd/tui/utility/svg/SvgUtils.java
View file @
6db84bf4
...
...
@@ -22,14 +22,18 @@ import lu.list.itis.dkd.tui.utility.MathUtils;
import
lu.list.itis.dkd.tui.utility.Point
;
import
com.google.common.base.Strings
;
import
com.kitfox.svg.Group
;
import
com.kitfox.svg.Path
;
import
com.kitfox.svg.SVGCache
;
import
com.kitfox.svg.SVGDiagram
;
import
com.kitfox.svg.SVGElement
;
import
com.kitfox.svg.SVGE
lementE
xception
;
import
com.kitfox.svg.SVGException
;
import
com.kitfox.svg.SVGRoot
;
import
com.kitfox.svg.SVGUniverse
;
import
com.kitfox.svg.TransformableElement
;
import
com.kitfox.svg.animation.AnimationElement
;
import
com.kitfox.svg.app.beans.SVGIcon
;
import
com.kitfox.svg.xml.StyleAttribute
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -37,6 +41,7 @@ import org.slf4j.LoggerFactory;
import
java.awt.Color
;
import
java.awt.Dimension
;
import
java.awt.Graphics2D
;
import
java.awt.Shape
;
import
java.awt.geom.AffineTransform
;
import
java.awt.image.BufferedImage
;
import
java.io.BufferedReader
;
...
...
@@ -48,6 +53,7 @@ import java.net.URI;
import
java.net.URL
;
import
java.text.DecimalFormat
;
import
java.text.DecimalFormatSymbols
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
...
...
@@ -72,6 +78,8 @@ public class SvgUtils {
private
static
final
String
STROKE_COLOUR_PROPERTY
=
"stroke"
;
private
static
final
String
STROKE_OPACITY_PROPERTY
=
"stroke-opacity"
;
private
static
final
String
STROKE_WIDTH_PROPERTY
=
"stroke-width"
;
private
static
final
String
TRANSFORM_PROPERTY
=
"transform"
;
private
static
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
SvgUtils
.
class
.
getSimpleName
());
...
...
@@ -127,7 +135,8 @@ public class SvgUtils {
// ---------------------------------------------------------------------------
@SuppressWarnings
(
"unchecked"
)
private
static
void
inspectChildren
(
List
<
SVGElement
>
children
,
SvgProperties
properties
)
{
private
static
void
applyProperties
(
List
<
SVGElement
>
children
,
SvgProperties
properties
)
{
for
(
SVGElement
element
:
children
)
{
try
{
if
((
properties
.
fillColour
!=
null
)
&&
element
.
hasAttribute
(
FILL_COLOUR_PROPERTY
,
AnimationElement
.
AT_AUTO
))
{
...
...
@@ -157,14 +166,54 @@ public class SvgUtils {
if
((
properties
.
strokeWidth
>=
0
)
&&
element
.
hasAttribute
(
STROKE_WIDTH_PROPERTY
,
AnimationElement
.
AT_AUTO
))
{
element
.
setAttribute
(
STROKE_WIDTH_PROPERTY
,
AnimationElement
.
AT_AUTO
,
Integer
.
toString
(
properties
.
strokeWidth
));
}
}
catch
(
SVGElementException
e
)
{
if
(
element
instanceof
TransformableElement
)
{
((
TransformableElement
)
element
).
updateTime
(
0
);
// StyleAttribute attribute = element.getPresAbsolute(TRANSFORM_PROPERTY);
// AffineTransform elementTransform =
// SVGElement.parseSingleTransform(attribute.getStringValue());
}
}
catch
(
SVGException
e
)
{
LOGGER
.
error
(
"Failed to inspect SVG element {}!"
,
element
);
//$NON-NLS-1$
}
if
(
element
.
getNumChildren
()
>
0
)
{
inspectChildren
(
element
.
getChildren
(
null
),
properties
);
applyProperties
(
element
.
getChildren
(
null
),
properties
);
}
}
}
// ---------------------------------------------------------------------------
@SuppressWarnings
(
"unchecked"
)
private
static
List
<
Shape
>
getScaledShape
(
List
<
SVGElement
>
children
,
AffineTransform
parentTransform
)
{
List
<
Shape
>
shapes
=
new
ArrayList
<>();
for
(
SVGElement
element
:
children
)
{
try
{
AffineTransform
childTransform
=
(
parentTransform
!=
null
)
?
(
AffineTransform
)
parentTransform
.
clone
()
:
new
AffineTransform
();
if
((
element
instanceof
Group
)
&&
element
.
hasAttribute
(
TRANSFORM_PROPERTY
,
AnimationElement
.
AT_AUTO
))
{
StyleAttribute
attribute
=
element
.
getPresAbsolute
(
TRANSFORM_PROPERTY
);
AffineTransform
elementTransform
=
SVGElement
.
parseSingleTransform
(
attribute
.
getStringValue
());
childTransform
.
concatenate
(
elementTransform
);
}
if
(
element
instanceof
Path
)
{
Shape
shape
=
((
Path
)
element
).
getShape
();
shapes
.
add
(
childTransform
.
createTransformedShape
(
shape
));
}
shapes
.
addAll
(
getScaledShape
(
element
.
getChildren
(
null
),
childTransform
));
}
catch
(
SVGException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
}
return
shapes
;
}
// ---------------------------------------------------------------------------
...
...
@@ -181,7 +230,7 @@ public class SvgUtils {
// ---------------------------------------------------------------------------
@SuppressWarnings
(
"unchecked"
)
public
static
SVGIcon
loadSVG
Resource
(
URL
baseUrl
,
String
resourcePath
,
SvgProperties
properties
)
{
public
static
SVGIcon
loadSVG
Icon
(
URL
baseUrl
,
String
resourcePath
,
SvgProperties
properties
)
{
SVGIcon
icon
=
null
;
try
{
...
...
@@ -202,12 +251,12 @@ public class SvgUtils {
try
(
Reader
streamReader
=
new
InputStreamReader
(
resourceStream
);
Reader
bufferedReader
=
new
BufferedReader
(
streamReader
))
{
uri
=
universe
.
loadSVG
(
bufferedReader
,
namespace
);
// $NON-NLS-1$
uri
=
universe
.
loadSVG
(
bufferedReader
,
namespace
);
}
SVGDiagram
diagram
=
universe
.
getDiagram
(
uri
);
SVGRoot
root
=
diagram
.
getRoot
();
inspectChildren
(
root
.
getChildren
(
null
),
properties
);
applyProperties
(
root
.
getChildren
(
null
),
properties
);
icon
=
new
SVGIcon
();
icon
.
setSvgURI
(
uri
);
...
...
@@ -219,6 +268,49 @@ public class SvgUtils {
return
icon
;
}
// ---------------------------------------------------------------------------
/**
* @param baseUrl
* @param resourcePath
* @param name
* @return
*/
// ---------------------------------------------------------------------------
@SuppressWarnings
(
"unchecked"
)
public
static
List
<
Shape
>
loadSVGShape
(
URL
baseUrl
,
String
resourcePath
,
SvgProperties
properties
)
{
List
<
Shape
>
shapes
=
null
;
try
{
SVGUniverse
universe
=
SVGCache
.
getSVGUniverse
();
InputStream
resourceStream
;
URL
resourceUrl
;
URI
uri
;
String
namespace
=
(!
Strings
.
isNullOrEmpty
(
properties
.
nameSpace
))
?
properties
.
nameSpace
:
resourcePath
;
if
(
baseUrl
!=
null
)
{
StringBuilder
path
=
new
StringBuilder
(
FILE_PREFIX
);
path
.
append
(
baseUrl
.
getPath
()).
append
(
resourcePath
);
resourceUrl
=
new
URL
(
path
.
toString
());
resourceStream
=
resourceUrl
.
openStream
();
}
else
{
resourceStream
=
TangibleApplication
.
class
.
getResource
(
resourcePath
).
openStream
();
}
try
(
Reader
streamReader
=
new
InputStreamReader
(
resourceStream
);
Reader
bufferedReader
=
new
BufferedReader
(
streamReader
))
{
uri
=
universe
.
loadSVG
(
bufferedReader
,
namespace
);
}
SVGDiagram
diagram
=
universe
.
getDiagram
(
uri
);
SVGRoot
root
=
diagram
.
getRoot
();
shapes
=
getScaledShape
(
root
.
getChildren
(
null
),
null
);
}
catch
(
IOException
e
)
{
LOGGER
.
error
(
"Failed to load SVG resource {}!"
,
resourcePath
);
//$NON-NLS-1$
}
return
shapes
;
}
// ---------------------------------------------------------------------------
/**
* @param svg
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment