File indexing completed on 2024-05-19 07:41:18

0001 #!/usr/bin/env bash
0002 
0003 if ! command -v svgo >/dev/null
0004 then
0005     echo "Please install svgo: npm install svgo"
0006     exit 1
0007 fi
0008 # regarding convertStyleToAttrs, see: https://github.com/svg/svgo/issues/489
0009 # regarding convertPathData, see: https://github.com/svg/svgo/issues/490
0010 ARGS="--pretty --disable=convertStyleToAttrs --disable=convertPathData"
0011 
0012 function generatePng {
0013     inkscape -z -D $1 --export-png=$2 --export-width=200 --export-background=transparent > /dev/null
0014 }
0015 
0016 # args: pngA pngB final.svg temp.svg
0017 function evaluateOptimization {
0018 #     that regex is to just take A from "A (B)"
0019     res=`compare -metric MAE $1 $2 /dev/null 2>&1 | sed "s/^\\([0-9]*\\).*/\\1/"` #-fuzz 5
0020     if [ "$res" -gt 100 ]; then
0021         echo "huuuuge difference of $res in $3"
0022     else
0023         mv $4 $3
0024     fi
0025 }
0026 
0027 find . -name "*.svg" -size 4k -print0 | while IFS= read -r -d '' file
0028 do
0029     echo "doing... $file"
0030     generatePng "$file" /tmp/A.png
0031     svgo -i "$file" -o "$file".tmp.svg $ARGS
0032     generatePng "$file".tmp.svg /tmp/B.png
0033     evaluateOptimization /tmp/A.png /tmp/B.png "$file" "$file".tmp.svg
0034 done
0035 
0036 find . -name "*.svgz" -print0 | while IFS= read -r -d '' file
0037 do
0038     echo "z-doing... $file"
0039     generatePng "$file" /tmp/A.png
0040     gunzip "$file" -S .svgz -c | svgo -i - $ARGS | gzip -c > "$file".tmp.svgz
0041     generatePng "$file".tmp.svgz /tmp/B.png
0042     evaluateOptimization /tmp/A.png /tmp/B.png "$file" "$file".tmp.svgz
0043 done