Commit 6db84bf4 authored by Nico Mack's avatar Nico Mack

Added method to directly fetch java Shapes from SVG files

parent 98967c61
......@@ -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.loadSVGResource(baseUrl.toURL(), imagePath, properties);
SVGIcon vector = SvgUtils.loadSVGIcon(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$
......
......@@ -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.SVGElementException;
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 loadSVGResource(URL baseUrl, String resourcePath, SvgProperties properties) {
public static SVGIcon loadSVGIcon(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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment