🎉 Integrate exporter with svgclean.

This commit is contained in:
Andrey Antukh 2021-01-13 13:32:53 +01:00
parent 70d464189f
commit f66ddcaa2d
8 changed files with 165 additions and 75 deletions

File diff suppressed because one or more lines are too long

View file

@ -69,9 +69,12 @@
(defn- do-svg-clean
[ctx data]
(let [res (promise)
cleaner (->> (graal/source "js" "require('svgclean')")
(graal/eval! ctx))
resultp (graal/invoke-member cleaner "optimize" data)]
bindings (graal/get-bindings ctx "js")
optimize (-> (graal/get-bindings ctx "js")
(graal/get-member "svgc")
(graal/get-member "optimize"))
resultp (graal/invoke optimize data)]
(graal/invoke-member resultp "then"
(reify Consumer
(accept [_ val]

View file

@ -34,8 +34,8 @@
(.eval ^Context ctx ^Source source))
(defn ^Value get-bindings
[ctx]
(.getBindings ^Context ctx))
[ctx lang]
(.getBindings ^Context ctx ^String lang))
(defn ^Value get-member
[vobj name]

View file

@ -11,6 +11,7 @@
[app.common.data :as d]
[app.common.pages :as cp]
[app.common.spec :as us]
["./svgclean" :as svgc]
["xml-js" :as xml]
["child_process" :as chp]
["os" :as os]
@ -114,17 +115,16 @@
(-> (write-file! pbmpath stdout)
(p/then (constantly pbmpath)))))
(p/then trace-color-mask)
(p/then read-file)
(p/then clean-svg)
(p/then (fn [svgpath]
(p/let [data (read-file svgpath)
data (parse-xml data)
(p/then (fn [data]
(p/let [data (parse-xml data)
data (get-in data ["elements" 0])]
{:svgpath svgpath
:color color
{:color color
:svgdata data}))))))
(join-color-layers [layers]
(log/info :fn :join-color-layers :layers (map :svgpath layers))
(log/info :fn :join-color-layers)
(loop [main (-> (:svgdata (first layers))
(assoc "elements" []))
layers (seq layers)]
@ -142,13 +142,9 @@
(-> (p/all (map (partial generate-color-mask ppmpath) colors))
(p/then join-color-layers)))
(clean-svg [svgpath]
(log/info :fn :clean-svg :svgpath svgpath)
(let [basepath (path/dirname svgpath)
basename (path/basename svgpath ".svg")
svgpath' (path/join basepath (str basename "-optimized.svg"))]
(-> (run-cmd! (str "svgcleaner " svgpath " " svgpath'))
(p/then (constantly svgpath')))))
(clean-svg [data]
(log/info :fn :clean-svg)
(svgc/optimize data))
(trace-single-node [{:keys [data] :as node}]
(log/info :fn :trace-single-node)

File diff suppressed because one or more lines are too long

View file

@ -11,9 +11,9 @@
regenerator-runtime "^0.13.4"
"@types/node@*":
version "14.14.14"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.14.tgz#f7fd5f3cc8521301119f63910f0fb965c7d761ae"
integrity sha512-UHnOPWVWV1z+VV8k6L1HhG7UbGBgIdghqF3l9Ny9ApPghbjICXkUJSd/b9gOgQfjM1r+37cipdw/HJ3F6ICEnQ==
version "14.14.20"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.20.tgz#f7974863edd21d1f8a494a73e8e2b3658615c340"
integrity sha512-Y93R97Ouif9JEOWPIUyU+eyIdyRqQR0I8Ez1dzku4hDx34NWh4HbtIc3WNzwB1Y9ULvNGeu5B8h8bVL5cAk4/A==
"@types/q@^1.5.1":
version "1.5.4"
@ -241,12 +241,12 @@ cache-content-type@^1.0.0:
ylru "^1.2.0"
call-bind@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/call-bind/-/call-bind-1.0.0.tgz#24127054bb3f9bdcb4b1fb82418186072f77b8ce"
integrity sha512-AEXsYIyyDY3MCzbwdhzG3Jx1R0J2wetQyUynn6dYHAO+bg8l1k7jwZtRv4ryryFs7EP+NDlikJlVe59jr0cM2w==
version "1.0.2"
resolved "https://registry.yarnpkg.com/call-bind/-/call-bind-1.0.2.tgz#b1d4e89e688119c3c9a903ad30abb2f6a919be3c"
integrity sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==
dependencies:
function-bind "^1.1.1"
get-intrinsic "^1.0.0"
get-intrinsic "^1.0.2"
chalk@^2.4.1:
version "2.4.2"
@ -332,9 +332,9 @@ cookies@~0.8.0:
keygrip "~1.1.0"
core-js-pure@^3.0.0:
version "3.8.1"
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.8.1.tgz#23f84048f366fdfcf52d3fd1c68fec349177d119"
integrity sha512-Se+LaxqXlVXGvmexKGPvnUIYC1jwXu1H6Pkyb3uBM5d8/NELMYCHs/4/roD7721NxrTLyv7e5nXd5/QLBO+10g==
version "3.8.2"
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.8.2.tgz#286f885c0dac1cdcd6d78397392abc25ddeca225"
integrity sha512-v6zfIQqL/pzTVAbZvYUozsxNfxcFb6Ks3ZfEbuneJl3FW9Jb8F6vLWB6f+qTmAu72msUdyb84V8d/yBFf7FNnw==
core-util-is@~1.0.0:
version "1.0.2"
@ -463,16 +463,16 @@ delegates@^1.0.0:
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=
depd@^1.1.2, depd@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=
depd@~2.0.0:
depd@^2.0.0, depd@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==
depd@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=
des.js@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/des.js/-/des.js-1.0.1.tgz#5382142e1bdc53f85d86d53e5f4aa7deb91e0843"
@ -684,7 +684,7 @@ function-bind@^1.1.1:
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d"
integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==
get-intrinsic@^1.0.0:
get-intrinsic@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/get-intrinsic/-/get-intrinsic-1.0.2.tgz#6820da226e50b24894e08859469dc68361545d49"
integrity sha512-aeX0vrFm21ILl3+JpFFRNe9aUvp6VFZb2/CTbgLb8j75kOhvoNYjt9d8KA/tJG4gSo8nzEDedRl0h7vDmBYRVg==
@ -933,9 +933,9 @@ koa-convert@^1.2.0:
koa-compose "^3.0.0"
koa@^2.13.0:
version "2.13.0"
resolved "https://registry.yarnpkg.com/koa/-/koa-2.13.0.tgz#25217e05efd3358a7e5ddec00f0a380c9b71b501"
integrity sha512-i/XJVOfPw7npbMv67+bOeXr3gPqOAw6uh5wFyNs3QvJ47tUx3M3V9rIE0//WytY42MKz4l/MXKyGkQ2LQTfLUQ==
version "2.13.1"
resolved "https://registry.yarnpkg.com/koa/-/koa-2.13.1.tgz#6275172875b27bcfe1d454356a5b6b9f5a9b1051"
integrity sha512-Lb2Dloc72auj5vK4X4qqL7B5jyDPQaZucc9sR/71byg7ryoD1NCaCm63CShk9ID9quQvDEi1bGR/iGjCG7As3w==
dependencies:
accepts "^1.3.5"
cache-content-type "^1.0.0"
@ -944,7 +944,7 @@ koa@^2.13.0:
cookies "~0.8.0"
debug "~3.1.0"
delegates "^1.0.0"
depd "^1.1.2"
depd "^2.0.0"
destroy "^1.0.4"
encodeurl "^1.0.2"
escape-html "^1.0.3"
@ -1007,17 +1007,17 @@ miller-rabin@^4.0.0:
bn.js "^4.0.0"
brorand "^1.0.1"
mime-db@1.44.0:
version "1.44.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.44.0.tgz#fa11c5eb0aca1334b4233cb4d52f10c5a6272f92"
integrity sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==
mime-db@1.45.0:
version "1.45.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.45.0.tgz#cceeda21ccd7c3a745eba2decd55d4b73e7879ea"
integrity sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==
mime-types@^2.1.18, mime-types@~2.1.24:
version "2.1.27"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.27.tgz#47949f98e279ea53119f5722e0f34e529bec009f"
integrity sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==
version "2.1.28"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.28.tgz#1160c4757eab2c5363888e005273ecf79d2a0ecd"
integrity sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==
dependencies:
mime-db "1.44.0"
mime-db "1.45.0"
minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1:
version "1.0.1"
@ -1471,9 +1471,9 @@ shadow-cljs-jar@1.3.2:
integrity sha512-XmeffAZHv8z7451kzeq9oKh8fh278Ak+UIOGGrapyqrFBB773xN8vMQ3O7J7TYLnb9BUwcqadKkmgaq7q6fhZg==
shadow-cljs@^2.11.8:
version "2.11.10"
resolved "https://registry.yarnpkg.com/shadow-cljs/-/shadow-cljs-2.11.10.tgz#0d55263a5ad31cac48d07fe9c719821122ec969d"
integrity sha512-/QbBjT75Dn+SY5C7ntu0VSYrId9Jj2EjncxpnB9E9OJn7mpdbmK3zs+PIC/6gpPdBr4gUhfagauD6AJEmsI6xg==
version "2.11.13"
resolved "https://registry.yarnpkg.com/shadow-cljs/-/shadow-cljs-2.11.13.tgz#ac229923bd2cb7f69e816ecd856b2250d4acfff3"
integrity sha512-igdiPxa9zx5sp66O/zD3SygL2SNobZUKjF74x+eVDfmRue4dudtqtAthMZwnB82SFx1Xh9LpmQGDK+45RvFhZQ==
dependencies:
node-libs-browser "^2.2.1"
readline-sync "^1.4.7"
@ -1608,9 +1608,9 @@ tar-fs@^2.0.0:
tar-stream "^2.1.4"
tar-stream@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-2.1.4.tgz#c4fb1a11eb0da29b893a5b25476397ba2d053bfa"
integrity sha512-o3pS2zlG4gxr67GmFYBLlq+dM8gyRGUOvsrHclSkvtVtQbjV0s/+ZE8OpICbaj8clrX3tjeHngYGP7rweaBnuw==
version "2.2.0"
resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-2.2.0.tgz#acad84c284136b060dc3faa64474aa9aebd77287"
integrity sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==
dependencies:
bl "^4.0.3"
end-of-stream "^1.4.1"
@ -1750,9 +1750,9 @@ ws@^3.0.0:
ultron "~1.1.0"
ws@^7.2.3:
version "7.4.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.1.tgz#a333be02696bd0e54cea0434e21dcc8a9ac294bb"
integrity sha512-pTsP8UAfhy3sk1lSk/O/s4tjD0CRwvMnzvwr4OKGX7ZvqZtUyx4KIJB5JWbkykPoc55tixMGgTNoh3k4FkNGFQ==
version "7.4.2"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.2.tgz#782100048e54eb36fe9843363ab1c68672b261dd"
integrity sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==
xml-js@^1.6.11:
version "1.6.11"

View file

@ -1,16 +1,13 @@
const plugins = [
{removeDimensions: true},
// {removeXMLNS: false},
{removeScriptElement: true},
{removeViewBox: false},
{moveElemsAttrsToGroup: false},
{
convertPathData: {
{convertPathData: {
lineShorthands: false,
curveSmoothShorthands: false,
forceAbsolutePath: true,
},
},
}}
];
const svgc = require("./src/svgclean.js");
@ -20,4 +17,3 @@ exports.optimize = function(data) {
return svgc.optimize(inst, data)
.then((result) => result.data);
};

View file

@ -6,9 +6,10 @@
"scripts": {
"test": "set NODE_ENV=test && mocha",
"lint": "jshint --show-non-errors .",
"build": "browserify -r./main.js:svgclean -o bundle.js",
"build": "browserify -s svgc main.js -o bundle.js",
"minify": "terser bundle.js -c -m -o bundle.min.js",
"dist": "npm run build && npm run minify"
"copy": "cp ./bundle.min.js ../../backend/resources/svgclean.js && cp ./bundle.min.js ../../exporter/src/app/http/svgclean.js",
"dist": "npm run build && npm run minify && npm run copy"
},
"dependencies": {
"chalk": "^4.1.0",