svg importer separated to multiple files

This commit is contained in:
honfika@gmail.com
2015-12-27 12:12:02 +01:00
parent 16a27789e3
commit 49778b88c1
16 changed files with 2636 additions and 2282 deletions

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
import java.awt.Color;
/**
*
* @author JPEXS
*/
class SvgBitmapFill extends SvgFill {
public String patternTransform;
public int characterId;
@Override
public Color toColor() {
return Color.BLACK;
}
}

View File

@@ -0,0 +1,389 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
import java.awt.Color;
/**
*
* @author JPEXS
*/
class SvgColor extends SvgFill {
public static final Color TRANSPARENT = new Color(0, true);
public static final SvgColor SVG_TRANSPARENT = new SvgColor(TRANSPARENT);
public Color color;
public SvgColor(int r, int g, int b, int opacity) {
this(new Color(r, g, b, opacity));
}
public SvgColor(int r, int g, int b) {
this(new Color(r, g, b));
}
public SvgColor(Color color) {
this.color = color;
}
@Override
public Color toColor() {
return this.color;
}
public static SvgColor parse(String colorString) {
if (colorString == null) {
return null;
}
// named colors from: http://www.w3.org/TR/SVG/types.html#ColorKeywords
switch (colorString) {
case "none":
return SVG_TRANSPARENT;
case "aliceblue":
return new SvgColor(240, 248, 255);
case "antiquewhite":
return new SvgColor(250, 235, 215);
case "aqua":
return new SvgColor(0, 255, 255);
case "aquamarine":
return new SvgColor(127, 255, 212);
case "azure":
return new SvgColor(240, 255, 255);
case "beige":
return new SvgColor(245, 245, 220);
case "bisque":
return new SvgColor(255, 228, 196);
case "black":
return new SvgColor(0, 0, 0);
case "blanchedalmond":
return new SvgColor(255, 235, 205);
case "blue":
return new SvgColor(0, 0, 255);
case "blueviolet":
return new SvgColor(138, 43, 226);
case "brown":
return new SvgColor(165, 42, 42);
case "burlywood":
return new SvgColor(222, 184, 135);
case "cadetblue":
return new SvgColor(95, 158, 160);
case "chartreuse":
return new SvgColor(127, 255, 0);
case "chocolate":
return new SvgColor(210, 105, 30);
case "coral":
return new SvgColor(255, 127, 80);
case "cornflowerblue":
return new SvgColor(100, 149, 237);
case "cornsilk":
return new SvgColor(255, 248, 220);
case "crimson":
return new SvgColor(220, 20, 60);
case "cyan":
return new SvgColor(0, 255, 255);
case "darkblue":
return new SvgColor(0, 0, 139);
case "darkcyan":
return new SvgColor(0, 139, 139);
case "darkgoldenrod":
return new SvgColor(184, 134, 11);
case "darkgray":
return new SvgColor(169, 169, 169);
case "darkgreen":
return new SvgColor(0, 100, 0);
case "darkgrey":
return new SvgColor(169, 169, 169);
case "darkkhaki":
return new SvgColor(189, 183, 107);
case "darkmagenta":
return new SvgColor(139, 0, 139);
case "darkolivegreen":
return new SvgColor(85, 107, 47);
case "darkorange":
return new SvgColor(255, 140, 0);
case "darkorchid":
return new SvgColor(153, 50, 204);
case "darkred":
return new SvgColor(139, 0, 0);
case "darksalmon":
return new SvgColor(233, 150, 122);
case "darkseagreen":
return new SvgColor(143, 188, 143);
case "darkslateblue":
return new SvgColor(72, 61, 139);
case "darkslategray":
return new SvgColor(47, 79, 79);
case "darkslategrey":
return new SvgColor(47, 79, 79);
case "darkturquoise":
return new SvgColor(0, 206, 209);
case "darkviolet":
return new SvgColor(148, 0, 211);
case "deeppink":
return new SvgColor(255, 20, 147);
case "deepskyblue":
return new SvgColor(0, 191, 255);
case "dimgray":
return new SvgColor(105, 105, 105);
case "dimgrey":
return new SvgColor(105, 105, 105);
case "dodgerblue":
return new SvgColor(30, 144, 255);
case "firebrick":
return new SvgColor(178, 34, 34);
case "floralwhite":
return new SvgColor(255, 250, 240);
case "forestgreen":
return new SvgColor(34, 139, 34);
case "fuchsia":
return new SvgColor(255, 0, 255);
case "gainsboro":
return new SvgColor(220, 220, 220);
case "ghostwhite":
return new SvgColor(248, 248, 255);
case "gold":
return new SvgColor(255, 215, 0);
case "goldenrod":
return new SvgColor(218, 165, 32);
case "gray":
return new SvgColor(128, 128, 128);
case "grey":
return new SvgColor(128, 128, 128);
case "green":
return new SvgColor(0, 128, 0);
case "greenyellow":
return new SvgColor(173, 255, 47);
case "honeydew":
return new SvgColor(240, 255, 240);
case "hotpink":
return new SvgColor(255, 105, 180);
case "indianred":
return new SvgColor(205, 92, 92);
case "indigo":
return new SvgColor(75, 0, 130);
case "ivory":
return new SvgColor(255, 255, 240);
case "khaki":
return new SvgColor(240, 230, 140);
case "lavender":
return new SvgColor(230, 230, 250);
case "lavenderblush":
return new SvgColor(255, 240, 245);
case "lawngreen":
return new SvgColor(124, 252, 0);
case "lemonchiffon":
return new SvgColor(255, 250, 205);
case "lightblue":
return new SvgColor(173, 216, 230);
case "lightcoral":
return new SvgColor(240, 128, 128);
case "lightcyan":
return new SvgColor(224, 255, 255);
case "lightgoldenrodyellow":
return new SvgColor(250, 250, 210);
case "lightgray":
return new SvgColor(211, 211, 211);
case "lightgreen":
return new SvgColor(144, 238, 144);
case "lightgrey":
return new SvgColor(211, 211, 211);
case "lightpink":
return new SvgColor(255, 182, 193);
case "lightsalmon":
return new SvgColor(255, 160, 122);
case "lightseagreen":
return new SvgColor(32, 178, 170);
case "lightskyblue":
return new SvgColor(135, 206, 250);
case "lightslategray":
return new SvgColor(119, 136, 153);
case "lightslategrey":
return new SvgColor(119, 136, 153);
case "lightsteelblue":
return new SvgColor(176, 196, 222);
case "lightyellow":
return new SvgColor(255, 255, 224);
case "lime":
return new SvgColor(0, 255, 0);
case "limegreen":
return new SvgColor(50, 205, 50);
case "linen":
return new SvgColor(250, 240, 230);
case "magenta":
return new SvgColor(255, 0, 255);
case "maroon":
return new SvgColor(128, 0, 0);
case "mediumaquamarine":
return new SvgColor(102, 205, 170);
case "mediumblue":
return new SvgColor(0, 0, 205);
case "mediumorchid":
return new SvgColor(186, 85, 211);
case "mediumpurple":
return new SvgColor(147, 112, 219);
case "mediumseagreen":
return new SvgColor(60, 179, 113);
case "mediumslateblue":
return new SvgColor(123, 104, 238);
case "mediumspringgreen":
return new SvgColor(0, 250, 154);
case "mediumturquoise":
return new SvgColor(72, 209, 204);
case "mediumvioletred":
return new SvgColor(199, 21, 133);
case "midnightblue":
return new SvgColor(25, 25, 112);
case "mintcream":
return new SvgColor(245, 255, 250);
case "mistyrose":
return new SvgColor(255, 228, 225);
case "moccasin":
return new SvgColor(255, 228, 181);
case "navajowhite":
return new SvgColor(255, 222, 173);
case "navy":
return new SvgColor(0, 0, 128);
case "oldlace":
return new SvgColor(253, 245, 230);
case "olive":
return new SvgColor(128, 128, 0);
case "olivedrab":
return new SvgColor(107, 142, 35);
case "orange":
return new SvgColor(255, 165, 0);
case "orangered":
return new SvgColor(255, 69, 0);
case "orchid":
return new SvgColor(218, 112, 214);
case "palegoldenrod":
return new SvgColor(238, 232, 170);
case "palegreen":
return new SvgColor(152, 251, 152);
case "paleturquoise":
return new SvgColor(175, 238, 238);
case "palevioletred":
return new SvgColor(219, 112, 147);
case "papayawhip":
return new SvgColor(255, 239, 213);
case "peachpuff":
return new SvgColor(255, 218, 185);
case "peru":
return new SvgColor(205, 133, 63);
case "pink":
return new SvgColor(255, 192, 203);
case "plum":
return new SvgColor(221, 160, 221);
case "powderblue":
return new SvgColor(176, 224, 230);
case "purple":
return new SvgColor(128, 0, 128);
case "red":
return new SvgColor(255, 0, 0);
case "rosybrown":
return new SvgColor(188, 143, 143);
case "royalblue":
return new SvgColor(65, 105, 225);
case "saddlebrown":
return new SvgColor(139, 69, 19);
case "salmon":
return new SvgColor(250, 128, 114);
case "sandybrown":
return new SvgColor(244, 164, 96);
case "seagreen":
return new SvgColor(46, 139, 87);
case "seashell":
return new SvgColor(255, 245, 238);
case "sienna":
return new SvgColor(160, 82, 45);
case "silver":
return new SvgColor(192, 192, 192);
case "skyblue":
return new SvgColor(135, 206, 235);
case "slateblue":
return new SvgColor(106, 90, 205);
case "slategray":
return new SvgColor(112, 128, 144);
case "slategrey":
return new SvgColor(112, 128, 144);
case "snow":
return new SvgColor(255, 250, 250);
case "springgreen":
return new SvgColor(0, 255, 127);
case "steelblue":
return new SvgColor(70, 130, 180);
case "tan":
return new SvgColor(210, 180, 140);
case "teal":
return new SvgColor(0, 128, 128);
case "thistle":
return new SvgColor(216, 191, 216);
case "tomato":
return new SvgColor(255, 99, 71);
case "turquoise":
return new SvgColor(64, 224, 208);
case "violet":
return new SvgColor(238, 130, 238);
case "wheat":
return new SvgColor(245, 222, 179);
case "white":
return new SvgColor(255, 255, 255);
case "whitesmoke":
return new SvgColor(245, 245, 245);
case "yellow":
return new SvgColor(255, 255, 0);
case "yellowgreen":
return new SvgColor(154, 205, 50);
}
if (colorString.startsWith("#")) {
String s = colorString.substring(1);
if (s.length() == 3) {
s = "" + s.charAt(0) + s.charAt(0) + s.charAt(1) + s.charAt(1) + s.charAt(2) + s.charAt(2);
}
int i = Integer.parseInt(s, 16);
return new SvgColor(new Color(i, false));
} else if (colorString.startsWith("rgb")) {
colorString = colorString.substring(3).trim();
if (colorString.startsWith("(") && colorString.endsWith(")")) {
colorString = colorString.substring(1, colorString.length() - 1);
String[] args = colorString.split(",");
if (args.length == 3) {
String a0 = args[0].trim();
String a1 = args[1].trim();
String a2 = args[2].trim();
if (a0.endsWith("%") && a1.endsWith("%") && a2.endsWith("%")) {
int r = (int) Math.round(Integer.parseInt(a0.substring(0, a0.length() - 1)) * 255.0 / 100);
int g = (int) Math.round(Integer.parseInt(a1.substring(0, a1.length() - 1)) * 255.0 / 100);
int b = (int) Math.round(Integer.parseInt(a2.substring(0, a2.length() - 1)) * 255.0 / 100);
return new SvgColor(r, g, b);
} else {
int r = Integer.parseInt(a0);
int g = Integer.parseInt(a1);
int b = Integer.parseInt(a2);
return new SvgColor(r, g, b);
}
}
}
}
return null;
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
import java.awt.Color;
/**
*
* @author JPEXS
*/
abstract class SvgFill implements Cloneable {
public abstract Color toColor();
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
import java.awt.Color;
import java.util.List;
/**
*
* @author JPEXS
*/
abstract class SvgGradient extends SvgFill {
public List<SvgStop> stops;
public SvgGradientUnits gradientUnits;
public String gradientTransform;
public SvgSpreadMethod spreadMethod;
public SvgInterpolation interpolation;
@Override
public Color toColor() {
if (stops.isEmpty()) {
return Color.BLACK;
}
return stops.get(0).color;
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
/**
*
* @author JPEXS
*/
enum SvgGradientUnits {
USER_SPACE_ON_USE, OBJECT_BOUNDING_BOX
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
/**
*
* @author JPEXS
*/
enum SvgInterpolation {
SRGB, LINEAR_RGB
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
/**
*
* @author JPEXS
*/
enum SvgLineCap {
BUTT, ROUND, SQUARE
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
/**
*
* @author JPEXS
*/
enum SvgLineJoin {
MITER, ROUND, BEVEL
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
/**
*
* @author JPEXS
*/
class SvgLinearGradient extends SvgGradient {
public String x1;
public String y1;
public String x2;
public String y2;
//xlink?
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
/**
*
* @author JPEXS
*/
class SvgRadialGradient extends SvgGradient {
public String cx;
public String cy;
public String r;
public String fx;
public String fy;
//xlink?
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
/**
*
* @author JPEXS
*/
enum SvgSpreadMethod {
PAD, REFLECT, REPEAT
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
import java.awt.Color;
/**
*
* @author JPEXS
*/
class SvgStop implements Comparable<SvgStop> {
public Color color;
public double offset;
public SvgStop(Color color, double offset) {
this.color = color;
this.offset = offset;
}
@Override
public int compareTo(SvgStop o) {
return (int) Math.signum(offset - o.offset);
}
}

View File

@@ -0,0 +1,598 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.importers.svg;
import com.jpexs.decompiler.flash.importers.ShapeImporter;
import com.jpexs.decompiler.flash.tags.base.ImageTag;
import com.jpexs.helpers.Helper;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author JPEXS
*/
class SvgStyle implements Cloneable {
public Color color;
public SvgFill fill;
public double opacity;
public double fillOpacity;
public SvgFill strokeFill;
public Color stopColor;
public double stopOpacity;
public double strokeWidth;
public double strokeOpacity;
public SvgLineCap strokeLineCap;
public SvgLineJoin strokeLineJoin;
public double strokeMiterLimit;
public SvgStyle parentStyle;
private final SvgImporter importer;
private final Random random = new Random();
public SvgStyle(SvgImporter importer) {
this.importer = importer;
fill = new SvgColor(Color.black);
fillOpacity = 1;
strokeFill = null;
strokeWidth = 1;
strokeOpacity = 1;
opacity = 1;
stopOpacity = 1;
stopColor = null;
strokeLineCap = SvgLineCap.BUTT;
strokeLineJoin = SvgLineJoin.MITER;
strokeMiterLimit = 4;
}
public SvgStyle(SvgStyle parentStyle) {
this(parentStyle.importer);
this.parentStyle = parentStyle;
}
public SvgFill getFillWithOpacity() {
if (fill == null) {
return null;
}
if (!(fill instanceof SvgColor)) {
return fill;
}
Color fillColor = ((SvgColor) fill).color;
int opacity = (int) Math.round(this.opacity * fillOpacity * 255);
if (opacity == 255) {
return fill;
}
return new SvgColor(fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue(), opacity);
}
public SvgFill getStrokeFillWithOpacity() {
if (strokeFill == null) {
return null;
}
if (!(strokeFill instanceof SvgColor)) {
return strokeFill;
}
Color strokeFillColor = ((SvgColor) strokeFill).color;
int opacity = (int) Math.round(this.opacity * strokeOpacity * 255);
if (opacity == 255) {
return strokeFill;
}
return new SvgColor(strokeFillColor.getRed(), strokeFillColor.getGreen(), strokeFillColor.getBlue(), opacity);
}
public SvgFill getStrokeColorWithOpacity() {
if (strokeFill == null) {
return null;
}
if (!(strokeFill instanceof SvgColor)) {
return strokeFill;
}
Color strokeColor = ((SvgColor) strokeFill).color;
int opacity = (int) Math.round(this.opacity * strokeOpacity * 255);
if (opacity == 255) {
return strokeFill;
}
return new SvgColor(strokeColor.getRed(), strokeColor.getGreen(), strokeColor.getBlue(), opacity);
}
@Override
public SvgStyle clone() {
try {
SvgStyle ret = (SvgStyle) super.clone();
return ret;
} catch (CloneNotSupportedException ex) {
throw new RuntimeException();
}
}
//FIXME - matrices
private SvgFill parseGradient(Map<String, Element> idMap, Element el, SvgStyle style) {
SvgGradientUnits gradientUnits = null;
String gradientTransform = null;
SvgSpreadMethod spreadMethod = null;
SvgInterpolation interpolation = null;
String x1 = null;
String y1 = null;
String x2 = null;
String y2 = null;
String cx = null;
String cy = null;
String fx = null;
String fy = null;
String r = null;
List<SvgStop> stops = new ArrayList<>();
//inheritance:
if (el.hasAttribute("xlink:href")) {
String parent = el.getAttribute("xlink:href");
if (parent.startsWith("#")) {
String parentId = parent.substring(1);
Element parent_el = idMap.get(parentId);
if (parent_el == null) {
importer.showWarning("fillNotSupported", "Parent gradient not found.");
return new SvgColor(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
if ("linearGradient".equals(el.getTagName()) && parent_el.getTagName().equals(el.getTagName())) {
SvgLinearGradient parentFill = (SvgLinearGradient) parseGradient(idMap, parent_el, style);
gradientUnits = parentFill.gradientUnits;
gradientTransform = parentFill.gradientTransform;
spreadMethod = parentFill.spreadMethod;
x1 = parentFill.x1;
y1 = parentFill.y1;
x2 = parentFill.x2;
y2 = parentFill.y2;
interpolation = parentFill.interpolation;
stops = parentFill.stops;
}
if ("radialGradient".equals(el.getTagName()) && parent_el.getTagName().equals(el.getTagName())) {
SvgRadialGradient parentFill = (SvgRadialGradient) parseGradient(idMap, parent_el, style);
gradientUnits = parentFill.gradientUnits;
gradientTransform = parentFill.gradientTransform;
spreadMethod = parentFill.spreadMethod;
cx = parentFill.cx;
cy = parentFill.cy;
fx = parentFill.fx;
fy = parentFill.fy;
r = parentFill.r;
interpolation = parentFill.interpolation;
stops = parentFill.stops;
}
} else {
importer.showWarning("fillNotSupported", "Parent gradient invalid.");
return new SvgColor(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
}
if (el.hasAttribute("gradientUnits")) {
switch (el.getAttribute("gradientUnits")) {
case "userSpaceOnUse":
gradientUnits = SvgGradientUnits.USER_SPACE_ON_USE;
break;
case "objectBoundingBox":
gradientUnits = SvgGradientUnits.OBJECT_BOUNDING_BOX;
break;
default:
importer.showWarning("fillNotSupported", "Unsupported gradientUnits: " + el.getAttribute("gradientUnits"));
return new SvgColor(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
}
if (el.hasAttribute("gradientTransform")) {
gradientTransform = el.getAttribute("gradientTransform");
}
if (el.hasAttribute("spreadMethod")) {
switch (el.getAttribute("spreadMethod")) {
case "pad":
spreadMethod = SvgSpreadMethod.PAD;
break;
case "reflect":
spreadMethod = SvgSpreadMethod.REFLECT;
break;
case "repeat":
spreadMethod = SvgSpreadMethod.REPEAT;
break;
}
}
if (el.hasAttribute("x1")) {
x1 = el.getAttribute("x1").trim();
}
if (el.hasAttribute("y1")) {
y1 = el.getAttribute("y1").trim();
}
if (el.hasAttribute("x2")) {
x2 = el.getAttribute("x2").trim();
}
if (el.hasAttribute("y2")) {
y2 = el.getAttribute("y2").trim();
}
if (el.hasAttribute("cx")) {
cx = el.getAttribute("cx").trim();
}
if (el.hasAttribute("cy")) {
cy = el.getAttribute("cy").trim();
}
if (el.hasAttribute("fx")) {
fx = el.getAttribute("fx").trim();
}
if (el.hasAttribute("fy")) {
fy = el.getAttribute("fy").trim();
}
if (el.hasAttribute("r")) {
r = el.getAttribute("r").trim();
}
if (el.hasAttribute("color-interpolation") && interpolation == null) { //prefer inherit
switch (el.getAttribute("color-interpolation")) {
case "sRGB":
interpolation = SvgInterpolation.SRGB;
break;
case "linearRGB":
interpolation = SvgInterpolation.LINEAR_RGB;
break;
case "auto": //without preference, put SRGB there
interpolation = SvgInterpolation.SRGB;
break;
}
}
if (interpolation == null) {
interpolation = SvgInterpolation.SRGB;
}
if (gradientUnits == null) {
gradientUnits = SvgGradientUnits.OBJECT_BOUNDING_BOX;
}
if (spreadMethod == null) {
spreadMethod = SvgSpreadMethod.PAD;
}
if (x1 == null) {
x1 = "0%";
}
if (y1 == null) {
y1 = "0%";
}
if (x2 == null) {
x2 = "100%";
}
if (y2 == null) {
y2 = "0%";
}
if (cx == null) {
cx = "50%";
}
if (cy == null) {
cy = "50%";
}
if (r == null) {
r = "50%";
}
if (fx == null) {
fx = cx;
}
if (fy == null) {
fy = cy;
}
NodeList stopNodes = el.getElementsByTagName("stop");
boolean stopsCleared = false;
for (int i = 0; i < stopNodes.getLength(); i++) {
Node node = stopNodes.item(i);
if (node instanceof Element) {
Element stopEl = (Element) node;
SvgStyle newStyle = style.apply(stopEl, idMap);
String offsetStr = stopEl.getAttribute("offset");
double offset;
if (offsetStr.endsWith("%")) {
offset = Double.parseDouble(offsetStr.substring(0, offsetStr.length() - 1)) / 100;
} else {
offset = Double.parseDouble(offsetStr);
}
Color color = newStyle.stopColor;
if (color == null) {
color = Color.BLACK;
}
int alpha = (int) Math.round(newStyle.stopOpacity * 255);
color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
if (!stopsCleared) { //It has some stop nodes -> remove all inherited stops
stopsCleared = true;
stops = new ArrayList<>();
}
stops.add(new SvgStop(color, offset));
}
}
if ("linearGradient".equals(el.getTagName())) {
SvgLinearGradient ret = new SvgLinearGradient();
ret.x1 = x1;
ret.y1 = y1;
ret.x2 = x2;
ret.y2 = y2;
ret.spreadMethod = spreadMethod;
ret.gradientTransform = gradientTransform;
ret.gradientUnits = gradientUnits;
ret.stops = stops;
ret.interpolation = interpolation;
return ret;
} else if ("radialGradient".equals(el.getTagName())) {
SvgRadialGradient ret = new SvgRadialGradient();
ret.cx = cx;
ret.cy = cy;
ret.fx = fx;
ret.fy = fy;
ret.r = r;
ret.spreadMethod = spreadMethod;
ret.gradientTransform = gradientTransform;
ret.gradientUnits = gradientUnits;
ret.stops = stops;
ret.interpolation = interpolation;
return ret;
} else {
return null;
}
}
private Color parseColor(String rgbStr) {
SvgFill fill = parseFill(new HashMap<>(), rgbStr, null);
return fill.toColor();
}
private SvgFill parseFill(Map<String, Element> idMap, String rgbStr, SvgStyle style) {
if (rgbStr == null) {
return null;
}
Pattern idPat = Pattern.compile("url\\(#([^)]+)\\).*");
java.util.regex.Matcher mPat = idPat.matcher(rgbStr);
if (mPat.matches()) {
String elementId = mPat.group(1);
Element e = idMap.get(elementId);
if (e != null) {
String tagName = e.getTagName();
if ("linearGradient".equals(tagName)) {
return parseGradient(idMap, e, new SvgStyle(style)); //? new style
}
if ("radialGradient".equals(tagName)) {
return parseGradient(idMap, e, new SvgStyle(style)); //? new style
}
if ("pattern".equals(tagName)) {
Element element = null;
NodeList childNodes = e.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i) instanceof Element) {
if (element != null) {
element = null;
break;
}
element = (Element) childNodes.item(i);
}
}
if (element != null && "image".equals(element.getTagName())) {
String attr = element.getAttribute("xlink:href").trim();
// this is ugly, but supports the format which is exported by FFDec
if (attr.startsWith("data:image/") && attr.contains("base64,")) {
String base64 = attr.substring(attr.indexOf("base64,") + 7);
byte[] data = Helper.base64StringToByteArray(base64);
try {
ImageTag imageTag = new ShapeImporter().addImage(importer.shapeTag, data, 0);
SvgBitmapFill bitmapFill = new SvgBitmapFill();
bitmapFill.characterId = imageTag.characterID;
if (e.hasAttribute("patternTransform")) {
bitmapFill.patternTransform = e.getAttribute("patternTransform");
}
return bitmapFill;
} catch (IOException ex) {
Logger.getLogger(ShapeImporter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
importer.showWarning("fillNotSupported", "Unknown fill style. Random color assigned.");
return new SvgColor(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
rgbStr = rgbStr.substring(elementId.length() + 6).trim(); // remove url(#...)
}
SvgColor result = SvgColor.parse(rgbStr);
if (result == null) {
importer.showWarning("fillNotSupported", "Unknown fill style. Random color assigned.");
return new SvgColor(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
return result;
}
private void applyStyle(Map<String, Element> idMap, SvgStyle style, String name, String value) {
if (value == null || value.length() == 0) {
return;
}
switch (name) {
case "color": {
Color color = parseColor(value);
if (color != null) {
style.color = color == SvgColor.TRANSPARENT ? null : color;
}
}
break;
case "fill": {
SvgFill fill = parseFill(idMap, value, style);
if (fill != null) {
style.fill = fill == SvgColor.SVG_TRANSPARENT ? null : fill;
}
}
break;
case "stop-color": {
if ("currentColor".equals(value)) {
if (style.parentStyle != null) {
style.stopColor = style.parentStyle.color;
}
} else if ("inherit".equals(value)) {
importer.showWarning(value + "StopColorNotSupported", "The stop color value '" + value + "' is not supported.");
} else {
style.stopColor = parseColor(value);
}
}
break;
case "fill-opacity": {
double opacity = Double.parseDouble(value);
style.fillOpacity = opacity;
}
break;
case "stop-opacity": {
if ("inherit".equals(value)) {
importer.showWarning(value + "StopOpacityNotSupported", "The stop opacity value '" + value + "' is not supported.");
} else {
double stopOpacity = Double.parseDouble(value);
style.stopOpacity = stopOpacity;
}
}
break;
case "stroke": {
SvgFill strokeFill = parseFill(idMap, value, style);
if (strokeFill != null) {
style.strokeFill = strokeFill == SvgColor.SVG_TRANSPARENT ? null : strokeFill;
}
}
break;
case "stroke-width": {
double strokeWidth = Double.parseDouble(value);
style.strokeWidth = strokeWidth;
}
break;
case "stroke-opacity": {
double opacity = Double.parseDouble(value);
style.strokeOpacity = opacity;
}
break;
case "stroke-linecap": {
switch (value) {
case "butt":
style.strokeLineCap = SvgLineCap.BUTT;
break;
case "round":
style.strokeLineCap = SvgLineCap.ROUND;
break;
case "square":
style.strokeLineCap = SvgLineCap.SQUARE;
break;
}
}
break;
case "stroke-linejoin": {
switch (value) {
case "miter":
style.strokeLineJoin = SvgLineJoin.MITER;
break;
case "round":
style.strokeLineJoin = SvgLineJoin.ROUND;
break;
case "bevel":
style.strokeLineJoin = SvgLineJoin.BEVEL;
break;
}
}
break;
case "stroke-miterlimit": {
double strokeMiterLimit = Double.parseDouble(value);
style.strokeMiterLimit = strokeMiterLimit;
}
case "opacity": {
double opacity = Double.parseDouble(value);
style.opacity = opacity;
}
break;
}
}
public SvgStyle apply(Element element, Map<String, Element> idMap) {
SvgStyle result = clone();
String[] styles = new String[]{
"color", "fill", "fill-opacity",
"stroke", "stroke-width", "stroke-opacity", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit",
"opacity", "stop-color", "stop-opacity"
};
for (String style : styles) {
if (element.hasAttribute(style)) {
String attr = element.getAttribute(style).trim();
applyStyle(idMap, result, style, attr);
}
}
if (element.hasAttribute("style")) {
String[] styleDefs = element.getAttribute("style").split(";");
for (String styleDef : styleDefs) {
String[] parts = styleDef.split(":", 2);
applyStyle(idMap, result, parts[0].trim(), parts[1].trim());
}
}
return result;
}
}