aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorcodecalm <codecalm@gmail.com>2022-04-10 02:13:39 +0200
committercodecalm <codecalm@gmail.com>2022-04-10 02:13:39 +0200
commita30198e3f468463384a41b169d93d1ec1ca07d12 (patch)
tree20f5ef8e399123d2ea71e88fe56385fab177883b /scripts
parentfix `antenna-bars-off` (diff)
downloadtabler-icons-a30198e3f468463384a41b169d93d1ec1ca07d12.tar.xz
init v2.0 of tabler icons
Diffstat (limited to 'scripts')
-rw-r--r--scripts/optimize.js64
-rw-r--r--scripts/updateUnicode.js72
-rw-r--r--scripts/updateVersion.js47
3 files changed, 183 insertions, 0 deletions
diff --git a/scripts/optimize.js b/scripts/optimize.js
new file mode 100644
index 00000000..d440d401
--- /dev/null
+++ b/scripts/optimize.js
@@ -0,0 +1,64 @@
+import svgpath from 'svgpath'
+import svgParse from 'parse-svg-path'
+import glob from 'glob'
+import fs from 'fs'
+import { basename } from 'path'
+
+const addFloats = (n1, n2) => {
+ return Math.round((parseFloat(n1) + parseFloat(n2)) * 1000) / 1000
+}
+
+const optimizePath = (path) => {
+ let transformed = svgpath(path)
+ .rel()
+ .round(3)
+ .toString()
+
+ return svgParse(transformed).map(function (a) {
+ return a.join(' ')
+ }).join(' ')
+}
+
+const optimizeIcons = (path) => {
+ glob(path, {}, function(er, files) {
+
+ files.forEach(function(file) {
+ let svgFile = fs.readFileSync(file),
+ svgFileContent = svgFile.toString()
+
+ svgFileContent = svgFileContent.replace(/><\/(polyline|line|rect|circle|path)>/g, '/>').
+ replace(/rx="([^"]+)"\s+ry="\1"/g, 'rx="$1"').
+ replace(/\s?\/>/g, ' />').
+ replace(/\n\s*<(line|circle|path|polyline|rect)/g, "\n <$1").
+ replace(/polyline points="([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s([0-9.]+)"/g, 'line x1="$1" y1="$2" x2="$3" y2="$4"').
+ replace(/<path d="([^"]+)"/g, function(f, r1) {
+ r1 = optimizePath(r1)
+
+ return `<path d="${r1}"`
+ }).
+ replace(/d="m/g, 'd="M').
+ replace(/([Aa])\s?([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s?([0-1])\s?([0-1])\s?(-?[0-9.]+)\s?(-?[0-9.]+)/gi, '$1$2 $3 $4 $5 $6 $7 $8').
+ replace(/\n\n+/g, "\n").
+ replace(/<path d="M([0-9.]*) ([0-9.]*)l\s?([-0-9.]*) ([-0-9.]*)"/g, function(f, r1, r2, r3, r4) {
+ return `<line x1="${r1}" y1="${r2}" x2="${addFloats(r1, r3)}" y2="${addFloats(r2, r4)}"`
+ }).
+ replace(/<path d="M([0-9.]*) ([0-9.]*)v\s?([-0-9.]*)"/g, function(f, r1, r2, r3) {
+ return `<line x1="${r1}" y1="${r2}" x2="${r1}" y2="${addFloats(r2, r3)}"`
+ }).
+ replace(/<path d="M([0-9.]*) ([0-9.]*)h\s?([-0-9.]*)"/g, function(f, r1, r2, r3) {
+ return `<line x1="${r1}" y1="${r2}" x2="${addFloats(r1, r3)}" y2="${r2}"`
+ }).
+ replace(/<path d="([^"]+)"/g, function(f, r1) {
+ r1 = r1.replace(/ -0\./g, " -.").replace(/ 0\./g, " .").replace(/\s([a-z])/gi, "$1").replace(/([a-z])\s/gi, "$1")
+ return `<path d="${r1}"`
+ })
+
+ if (svgFile.toString() !== svgFileContent) {
+ console.log(`Optimize icon "${basename(file)}"`);
+ fs.writeFileSync(file, svgFileContent)
+ }
+ })
+ })
+}
+
+optimizeIcons("src/_icons/*.svg")
diff --git a/scripts/updateUnicode.js b/scripts/updateUnicode.js
new file mode 100644
index 00000000..85332d90
--- /dev/null
+++ b/scripts/updateUnicode.js
@@ -0,0 +1,72 @@
+import cp from 'child_process'
+import fs from 'fs'
+import glob from 'glob'
+
+let maxUnicode = 0
+
+const setMaxUnicode = () => {
+ const path = 'src/_icons/*.svg'
+
+ const files = glob.sync(path)
+
+ files.forEach(function(file) {
+ const svgFile = fs.readFileSync(file).toString()
+
+ svgFile.replace(/unicode: "([a-f0-9.]+)"/i, function(m, unicode) {
+ const newUnicode = parseInt(unicode, 16)
+
+ if(newUnicode) {
+ maxUnicode = Math.max(maxUnicode, newUnicode)
+ }
+ })
+ })
+}
+
+const addUnicodeToIcons = (files) => {
+
+ for (const i in files) {
+ const file = files[i]
+
+ if (fs.existsSync(`src/_icons/${file}.svg`)) {
+ let svgFile = fs.readFileSync(`src/_icons/${file}.svg`).toString()
+
+ if (!svgFile.match(/unicode: ([a-f0-9.]+)/i)) {
+ maxUnicode++
+ const unicode = maxUnicode.toString(16)
+
+ if(unicode) {
+ svgFile = svgFile.replace(/---\n<svg>/i, function(m) {
+ return `unicode: "${unicode}"\n${m}`
+ })
+
+ console.log(`Add unicode "${unicode}" to "${file}"`);
+ fs.writeFileSync(`src/_icons/${file}.svg`, svgFile)
+ }
+ } else {
+ console.log(`File ${file} already has unicode`)
+ }
+ } else {
+ console.log(`File ${file} doesn't exists`)
+ }
+ }
+}
+
+const updateIconsUnicode = () => {
+ setMaxUnicode()
+
+ cp.exec(`grep -RiL "unicode: " ./src/_icons/*.svg`, function(err, ret) {
+
+ let newIcons = []
+
+ ret.replace(/src\/_icons\/([a-z0-9-]+)\.svg/g, function(m, fileName) {
+ newIcons.push(fileName)
+ })
+
+ if (newIcons.length) {
+ console.log('newIcons', newIcons);
+ addUnicodeToIcons(newIcons)
+ }
+ })
+}
+
+updateIconsUnicode()
diff --git a/scripts/updateVersion.js b/scripts/updateVersion.js
new file mode 100644
index 00000000..218ffa9a
--- /dev/null
+++ b/scripts/updateVersion.js
@@ -0,0 +1,47 @@
+import p from '../package.json'
+import cp from 'child_process'
+import fs from 'fs'
+import { basename } from 'path'
+
+const setVersionToIcons = (version, files) => {
+ for (const i in files) {
+ const file = files[i]
+
+ if (fs.existsSync(`src/_icons/${file}.svg`)) {
+ let svgFile = fs.readFileSync(`src/_icons/${file}.svg`).toString()
+
+ if (!svgFile.match(/version: ([0-9.]+)/i)) {
+ svgFile = svgFile.replace(/---\n<svg>/i, function(m) {
+ return `version: "${version}"\n${m}`
+ })
+
+ console.log(`Set version to ${version} in "${basename(file)}"`);
+ fs.writeFileSync(`src/_icons/${file}.svg`, svgFile)
+ } else {
+ console.log(`File ${file} already has version`)
+ }
+ } else {
+ console.log(`File ${file} doesn't exists`)
+ }
+ }
+}
+
+const updateIconsVersion = (version) => {
+
+ if (version) {
+ cp.exec(`grep -RiL "version: " ./src/_icons/*.svg`, function(err, ret) {
+
+ let newIcons = []
+
+ ret.replace(/src\/_icons\/([a-z0-9-]+)\.svg/g, function(m, fileName) {
+ newIcons.push(fileName)
+ })
+
+ if (newIcons.length) {
+ setVersionToIcons(version.replace(/\.0$/, ''), newIcons)
+ }
+ })
+ }
+}
+
+updateIconsVersion(p.version)