Speed up SVG rasterization
This commit is contained in:
184
tools/SVGExport.hx
Normal file
184
tools/SVGExport.hx
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
package;
|
||||||
|
|
||||||
|
|
||||||
|
import format.SVG;
|
||||||
|
import haxe.io.Path;
|
||||||
|
import helpers.LogHelper;
|
||||||
|
import helpers.PathHelper;
|
||||||
|
import helpers.PlatformHelper;
|
||||||
|
import openfl.display.Bitmap;
|
||||||
|
import openfl.display.BitmapData;
|
||||||
|
import openfl.display.Shape;
|
||||||
|
import openfl.geom.Matrix;
|
||||||
|
import project.Architecture;
|
||||||
|
import sys.io.File;
|
||||||
|
import sys.io.Process;
|
||||||
|
import sys.FileSystem;
|
||||||
|
|
||||||
|
|
||||||
|
class SVGExport {
|
||||||
|
|
||||||
|
|
||||||
|
#if (neko && (haxe_210 || haxe3))
|
||||||
|
public static function __init__ () {
|
||||||
|
|
||||||
|
var haxePath = Sys.getEnv ("HAXEPATH");
|
||||||
|
var command = (haxePath != null && haxePath != "") ? haxePath + "/haxelib" : "haxelib";
|
||||||
|
|
||||||
|
var process = new Process (command, [ "path", "lime" ]);
|
||||||
|
var path = "";
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
var lines = new Array <String> ();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
var length = lines.length;
|
||||||
|
var line = process.stdout.readLine ();
|
||||||
|
|
||||||
|
if (length > 0 && StringTools.trim (line) == "-D lime") {
|
||||||
|
|
||||||
|
path = StringTools.trim (lines[length - 1]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push (line);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (e:Dynamic) {
|
||||||
|
|
||||||
|
process.close ();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
path += "/legacy/ndll/";
|
||||||
|
|
||||||
|
switch (PlatformHelper.hostPlatform) {
|
||||||
|
|
||||||
|
case WINDOWS:
|
||||||
|
|
||||||
|
untyped $loader.path = $array (path + "Windows/", $loader.path);
|
||||||
|
|
||||||
|
case MAC:
|
||||||
|
|
||||||
|
untyped $loader.path = $array (path + "Mac/", $loader.path);
|
||||||
|
untyped $loader.path = $array (path + "Mac64/", $loader.path);
|
||||||
|
|
||||||
|
case LINUX:
|
||||||
|
|
||||||
|
var arguments = Sys.args ();
|
||||||
|
var raspberryPi = false;
|
||||||
|
|
||||||
|
for (argument in arguments) {
|
||||||
|
|
||||||
|
if (argument == "-rpi") raspberryPi = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raspberryPi) {
|
||||||
|
|
||||||
|
untyped $loader.path = $array (path + "RPi/", $loader.path);
|
||||||
|
|
||||||
|
} else if (PlatformHelper.hostArchitecture == Architecture.X64) {
|
||||||
|
|
||||||
|
untyped $loader.path = $array (path + "Linux64/", $loader.path);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
untyped $loader.path = $array (path + "Linux/", $loader.path);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
#end
|
||||||
|
|
||||||
|
|
||||||
|
public static function main () {
|
||||||
|
|
||||||
|
var arguments = Sys.args ();
|
||||||
|
|
||||||
|
/*if (arguments.length > 0) {
|
||||||
|
|
||||||
|
// When the command-line tools are called from haxelib,
|
||||||
|
// the last argument is the project directory and the
|
||||||
|
// path SWF is the current working directory
|
||||||
|
|
||||||
|
var lastArgument = "";
|
||||||
|
|
||||||
|
for (i in 0...arguments.length) {
|
||||||
|
|
||||||
|
lastArgument = arguments.pop ();
|
||||||
|
if (lastArgument.length > 0) break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
lastArgument = new Path (lastArgument).toString ();
|
||||||
|
|
||||||
|
if (((StringTools.endsWith (lastArgument, "/") && lastArgument != "/") || StringTools.endsWith (lastArgument, "\\")) && !StringTools.endsWith (lastArgument, ":\\")) {
|
||||||
|
|
||||||
|
lastArgument = lastArgument.substr (0, lastArgument.length - 1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (FileSystem.exists (lastArgument) && FileSystem.isDirectory (lastArgument)) {
|
||||||
|
|
||||||
|
Sys.setCwd (lastArgument);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
|
var words = new Array<String> ();
|
||||||
|
|
||||||
|
for (arg in arguments) {
|
||||||
|
|
||||||
|
if (arg == "-verbose") {
|
||||||
|
|
||||||
|
LogHelper.verbose = true;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
words.push (arg);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (words.length > 4 && words[0] == "process") {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
var inputPath = words[1];
|
||||||
|
var width = Std.parseInt (words[2]);
|
||||||
|
var height = Std.parseInt (words[3]);
|
||||||
|
var outputPath = words[4];
|
||||||
|
|
||||||
|
var svg = new SVG (File.getContent (inputPath));
|
||||||
|
var backgroundColor = 0x00FFFFFF;
|
||||||
|
|
||||||
|
var shape = new Shape ();
|
||||||
|
svg.render (shape.graphics, 0, 0, width, height);
|
||||||
|
|
||||||
|
var bitmapData = new BitmapData (width, height, true, backgroundColor);
|
||||||
|
bitmapData.draw (shape);
|
||||||
|
|
||||||
|
File.saveBytes (outputPath, bitmapData.encode ("png"));
|
||||||
|
|
||||||
|
} catch (e:Dynamic) {
|
||||||
|
|
||||||
|
LogHelper.error (e);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
package helpers;
|
package helpers;
|
||||||
|
|
||||||
|
|
||||||
//import openfl.display.Bitmap;
|
|
||||||
//import openfl.display.BitmapData;
|
|
||||||
//import openfl.display.Shape;
|
|
||||||
//import openfl.geom.Matrix;
|
|
||||||
import lime.graphics.Image;
|
import lime.graphics.Image;
|
||||||
|
import lime.graphics.ImageBuffer;
|
||||||
|
import lime.utils.ByteArray;
|
||||||
|
import lime.utils.UInt8Array;
|
||||||
import project.Haxelib;
|
import project.Haxelib;
|
||||||
import project.Platform;
|
import project.Platform;
|
||||||
|
import sys.io.File;
|
||||||
import sys.FileSystem;
|
import sys.FileSystem;
|
||||||
//import format.SVG;
|
|
||||||
|
|
||||||
|
|
||||||
class ImageHelper {
|
class ImageHelper {
|
||||||
@@ -18,8 +17,35 @@ class ImageHelper {
|
|||||||
public static function rasterizeSVG (path:String, width:Int, height:Int, backgroundColor:Int = null):Image {
|
public static function rasterizeSVG (path:String, width:Int, height:Int, backgroundColor:Int = null):Image {
|
||||||
//public static function rasterizeSVG (svg:Dynamic /*SVG*/, width:Int, height:Int, backgroundColor:Int = null):Image {
|
//public static function rasterizeSVG (svg:Dynamic /*SVG*/, width:Int, height:Int, backgroundColor:Int = null):Image {
|
||||||
|
|
||||||
var rasterizer = PathHelper.getHaxelib (new Haxelib ("lime")) + "/templates/bin/batik/batik-rasterizer.jar";
|
|
||||||
var temp = PathHelper.getTemporaryFile (".png");
|
var temp = PathHelper.getTemporaryFile (".png");
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
// try using Lime "legacy" to rasterize SVG first, since it is much faster
|
||||||
|
|
||||||
|
ProcessHelper.runCommand ("", "neko", [ PathHelper.getHaxelib (new Haxelib ("lime")) + "/svg.n", "process", path, Std.string (width), Std.string (height), temp ], true, true);
|
||||||
|
|
||||||
|
if (FileSystem.exists (temp)) {
|
||||||
|
|
||||||
|
var image = Image.fromFile (temp);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
FileSystem.deleteFile (temp);
|
||||||
|
|
||||||
|
} catch (e:Dynamic) {}
|
||||||
|
|
||||||
|
if (image.buffer != null) {
|
||||||
|
|
||||||
|
return image;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (e:Dynamic) {}
|
||||||
|
|
||||||
|
var rasterizer = PathHelper.getHaxelib (new Haxelib ("lime")) + "/templates/bin/batik/batik-rasterizer.jar";
|
||||||
var args = [ "-Dapple.awt.UIElement=true", "-jar", rasterizer, "-d", temp, "-w", Std.string (width), "-h", Std.string (height) ];
|
var args = [ "-Dapple.awt.UIElement=true", "-jar", rasterizer, "-d", temp, "-w", Std.string (width), "-h", Std.string (height) ];
|
||||||
|
|
||||||
if (backgroundColor != null) {
|
if (backgroundColor != null) {
|
||||||
@@ -96,20 +122,6 @@ class ImageHelper {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
/*if (backgroundColor == null) {
|
|
||||||
|
|
||||||
backgroundColor = 0x00FFFFFF;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
var shape = new Shape ();
|
|
||||||
svg.render (shape.graphics, 0, 0, width, height);
|
|
||||||
|
|
||||||
var bitmapData = new BitmapData (width, height, true, backgroundColor);
|
|
||||||
bitmapData.draw (shape);
|
|
||||||
|
|
||||||
return bitmapData;*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
8
tools/svg.hxml
Normal file
8
tools/svg.hxml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
-main SVGExport
|
||||||
|
-neko ../svg.n
|
||||||
|
--macro lime.system.System.includeTools()
|
||||||
|
-lib lime
|
||||||
|
-lib openfl
|
||||||
|
-lib svg
|
||||||
|
-D lime-legacy
|
||||||
|
--remap flash:openfl
|
||||||
Reference in New Issue
Block a user