From 9c7ecca459d570cd7050885cea9bfc85ced939ad Mon Sep 17 00:00:00 2001 From: "honfika@gmail.com" Date: Sun, 27 Dec 2015 20:22:53 +0100 Subject: [PATCH] svg fixes --- .../flash/exporters/ShapeExporter.java | 18 +-- .../flash/importers/svg/SvgColor.java | 6 +- .../flash/importers/svg/SvgImporter.java | 12 +- .../flash/importers/svg/SvgStyle.java | 124 +++++++++++------- 4 files changed, 93 insertions(+), 67 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/ShapeExporter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/ShapeExporter.java index 7db211829..36a214035 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/ShapeExporter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/ShapeExporter.java @@ -30,7 +30,6 @@ import com.jpexs.decompiler.flash.exporters.shape.CanvasShapeExporter; import com.jpexs.decompiler.flash.helpers.BMPFile; import com.jpexs.decompiler.flash.helpers.ImageHelper; import com.jpexs.decompiler.flash.tags.Tag; -import com.jpexs.decompiler.flash.tags.base.CharacterTag; import com.jpexs.decompiler.flash.tags.base.RenderContext; import com.jpexs.decompiler.flash.tags.base.ShapeTag; import com.jpexs.decompiler.flash.tags.enums.ImageFormat; @@ -81,28 +80,25 @@ public class ShapeExporter { int currentIndex = 1; for (final Tag t : tags) { if (t instanceof ShapeTag) { + final ShapeTag st = (ShapeTag) t; if (evl != null) { evl.handleExportingEvent("shape", currentIndex, count, t.getName()); } - int characterID = 0; - if (t instanceof CharacterTag) { - characterID = ((CharacterTag) t).getCharacterId(); - } - String ext = "svg"; + int characterID = st.getCharacterId(); + String ext = ".svg"; if (settings.mode == ShapeExportMode.PNG) { - ext = "png"; + ext = ".png"; } if (settings.mode == ShapeExportMode.BMP) { - ext = "bmp"; + ext = ".bmp"; } if (settings.mode == ShapeExportMode.CANVAS) { - ext = "html"; + ext = ".html"; } - final File file = new File(outdir + File.separator + characterID + "." + ext); + final File file = new File(outdir + File.separator + Helper.makeFileName(st.getCharacterExportFileName() + ext)); new RetryTask(() -> { - ShapeTag st = (ShapeTag) t; switch (settings.mode) { case SVG: try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(file))) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgColor.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgColor.java index 72dbe3a71..8c251f1ea 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgColor.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgColor.java @@ -364,9 +364,9 @@ class SvgColor extends SvgFill { 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); + int r = (int) Math.round(Double.parseDouble(a0.substring(0, a0.length() - 1)) * 255.0 / 100); + int g = (int) Math.round(Double.parseDouble(a1.substring(0, a1.length() - 1)) * 255.0 / 100); + int b = (int) Math.round(Double.parseDouble(a2.substring(0, a2.length() - 1)) * 255.0 / 100); return new SvgColor(r, g, b); } else { int r = Integer.parseInt(a0); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgImporter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgImporter.java index ea5dc1c94..ec5e7a881 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgImporter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgImporter.java @@ -27,6 +27,7 @@ import com.jpexs.decompiler.flash.exporters.shape.BitmapExporter; import com.jpexs.decompiler.flash.importers.ShapeImporter; import com.jpexs.decompiler.flash.importers.SvgPathReader; import com.jpexs.decompiler.flash.tags.DefineShape4Tag; +import com.jpexs.decompiler.flash.tags.ExportAssetsTag; import com.jpexs.decompiler.flash.tags.Tag; import com.jpexs.decompiler.flash.tags.base.ShapeTag; import com.jpexs.decompiler.flash.types.ColorTransform; @@ -985,14 +986,14 @@ public class SvgImporter { if (!new File(name + ".original.svg").exists()) { URL svgUrl = new URL("http://www.w3.org/Graphics/SVG/Test/20061213/svggen/" + name + ".svg"); byte[] svgData = Helper.readStream(svgUrl.openStream()); - Helper.writeFile(name + ".original.svg", svgData); + Helper.writeFile(name + ".orig.svg", svgData); URL pngUrl = new URL("http://www.w3.org/Graphics/SVG/Test/20061213/png/full-" + name + ".png"); byte[] pngData = Helper.readStream(pngUrl.openStream()); - Helper.writeFile(name + ".original.png", pngData); + Helper.writeFile(name + ".orig.png", pngData); } - String svgDataS = Helper.readTextFile(name + ".original.svg"); + String svgDataS = Helper.readTextFile(name + ".orig.svg"); SWF swf = new SWF(); DefineShape4Tag st = new DefineShape4Tag(swf); st = (DefineShape4Tag) (new SvgImporter().importSvg(st, svgDataS)); @@ -1002,6 +1003,11 @@ public class SvgImporter { List li = new ArrayList<>(); li.add(st); ImageIO.write(si.getBufferedImage(), "PNG", new File(name + ".imported.png")); + ExportAssetsTag eat = new ExportAssetsTag(swf); + eat.tags.add(st.getCharacterId()); + eat.names.add(name); + swf.addTag(eat); + swf.assignExportNamesToSymbols(); new ShapeExporter().exportShapes(null, "./outex/", new ReadOnlyTagList(li), new ShapeExportSettings(ShapeExportMode.SVG, 1), null); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgStyle.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgStyle.java index 87f6a1398..1d5af35b9 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgStyle.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/importers/svg/SvgStyle.java @@ -22,6 +22,7 @@ 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; @@ -512,8 +513,8 @@ class SvgStyle implements Cloneable { return result; } - private void applyStyle(Map idMap, SvgStyle style, SvgStyle parentStyle, SvgStyleProperty styleProperty, String value) { - boolean inherit = styleProperty.isInherited(); + private boolean applyStyle(Map idMap, SvgStyle style, SvgStyle parentStyle, SvgStyleProperty styleProperty, String value) { + boolean inherit = false; if ("inherit".equals(value)) { value = ""; inherit = true; @@ -639,73 +640,96 @@ class SvgStyle implements Cloneable { ok = false; } - if (!ok && inherit) { - switch (name) { - case "color": - style.color = parentStyle.color; - break; - case "fill": - style.fill = parentStyle.fill; - break; - case "fill-opacity": - style.fillOpacity = parentStyle.opacity; - break; - case "stroke": - style.strokeFill = parentStyle.strokeFill; - break; - case "stroke-width": - style.strokeWidth = parentStyle.strokeWidth; - break; - case "stroke-opacity": - style.strokeOpacity = parentStyle.opacity; - break; - case "stroke-linecap": - style.strokeLineCap = parentStyle.strokeLineCap; - break; - case "stroke-linejoin": - style.strokeLineJoin = parentStyle.strokeLineJoin; - break; - case "stroke-miterlimit": - style.strokeMiterLimit = parentStyle.strokeMiterLimit; - break; - case "opacity": - style.opacity = parentStyle.opacity; - break; - case "stop-color": - style.stopColor = parentStyle.stopColor; - break; - case "stop-opacity": - style.stopOpacity = parentStyle.stopOpacity; - break; - } + if (inherit) { + applyInheritedStyle(style, parentStyle, styleProperty); + ok = true; + } + + return ok; + } + + private void applyInheritedStyle(SvgStyle style, SvgStyle parentStyle, SvgStyleProperty styleProperty) { + String name = styleProperty.name(); + switch (name) { + case "color": + style.color = parentStyle.color; + break; + case "fill": + style.fill = parentStyle.fill; + break; + case "fill-opacity": + style.fillOpacity = parentStyle.opacity; + break; + case "stroke": + style.strokeFill = parentStyle.strokeFill; + break; + case "stroke-width": + style.strokeWidth = parentStyle.strokeWidth; + break; + case "stroke-opacity": + style.strokeOpacity = parentStyle.opacity; + break; + case "stroke-linecap": + style.strokeLineCap = parentStyle.strokeLineCap; + break; + case "stroke-linejoin": + style.strokeLineJoin = parentStyle.strokeLineJoin; + break; + case "stroke-miterlimit": + style.strokeMiterLimit = parentStyle.strokeMiterLimit; + break; + case "opacity": + style.opacity = parentStyle.opacity; + break; + case "stop-color": + style.stopColor = parentStyle.stopColor; + break; + case "stop-opacity": + style.stopOpacity = parentStyle.stopOpacity; + break; } } public SvgStyle apply(Element element, Map idMap) { SvgStyle result = new SvgStyle(importer); - for (SvgStyleProperty styleProperty : SvgStyleProperty.getProperties()) { - String name = styleProperty.name(); - if (element.hasAttribute(name)) { - String attr = element.getAttribute(name).trim(); - applyStyle(idMap, result, this, styleProperty, attr); - } - } - + Map styleValues = new HashMap<>(); if (element.hasAttribute("style")) { String[] styleDefs = element.getAttribute("style").split(";"); for (String styleDef : styleDefs) { + if (!styleDef.contains(":")) { + continue; + } + String[] parts = styleDef.split(":", 2); String name = parts[0].trim(); + String value = parts[1].trim(); SvgStyleProperty styleProperty = SvgStyleProperty.getByName(name); if (styleProperty == null) { importer.showWarning(name + "StyleNotSupported", "The style '" + name + "' is not supported."); } else { - applyStyle(idMap, result, this, styleProperty, parts[1].trim()); + styleValues.put(name, value); } } } + for (SvgStyleProperty styleProperty : SvgStyleProperty.getProperties()) { + String name = styleProperty.name(); + boolean ok = false; + if (styleValues.containsKey(name)) { + ok = applyStyle(idMap, result, this, styleProperty, styleValues.get(name)); + } + + if (!ok && element.hasAttribute(name)) { + String attr = element.getAttribute(name).trim(); + ok = applyStyle(idMap, result, this, styleProperty, attr); + } + + if (!ok && styleProperty.isInherited()) { + applyInheritedStyle(result, this, styleProperty); + } + } + return result; } }