nerd-fonts/bin/scripts/optimize-original-source.sh
Fini Jastrow 73ae4a96b4 CI: Optimize original glyph svgs
[why]
Often the SVGs are rather detailed and result in a big
original-source.otf, which then again results in bigger than needed
patched fonts.

[how]
Typically people suggest using svgo to make SVGs smaller, but that just
tackles the representation of the icon, i.e. the actual svg file. That
does not help us at all. We do not need small svg files, we need simple
icons with few points and lines. svgo does not have that capability.

Instead Inkscape's 'Simplify' is used. Repeated use can destroy a glyph,
so we need a scale down margin to stop 'over-simplification'.

The values given for the margin at the moment are purely empirical, the
current glyphs survive repeated use of the new simplification script and
still look good.

The resultant original-source.otf file size is approximately similar to
the previously achieved by Ryan's manual work.

[note]
We need a newer Inkscape, thus update to Ubuntu 22.04

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
2022-09-19 16:08:22 +02:00

54 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
# Nerd Fonts Version: 2.2.2
# Script Version: 1.0.0
set -e
# Do not optimize files that are smaller than
size_limit=0
# Do not optimize files, where the new size is more than X% of current size
size_ratio_max=80
function get_size {
du -b "$1" | sed 's/\([0-9]\)[^0-9].*$/\1/'
}
echo
echo "Checking for SVG simplifications"
dry=`[ "$1" != "doit" ] && echo "dry" || true`
if [ -n "${dry}" ]; then
echo " This is a dry run: no actual modifications are performed"
echo " Specify 'doit' as parameter to actually optimize the svgs"
else
echo " Files might be MODIFIED by this run ('doit' specified)"
fi
svg_dir="../../src/svgs"
symbols=()
while IFS= read -d $'\0' -r file ; do
symbols=("${symbols[@]}" "$file")
done < <(find "${svg_dir}" -name "*.svg" -print0)
echo "Found ${#symbols[@]} svgs in ${svg_dir}"
count=0
for s in "${symbols[@]}"; do
inkscape "$s" "--actions=select-all;object-simplify-path;export-filename:temp.svg;export-do;quit-inkscape" 2>/dev/null
old_size=$(get_size "$s")
new_size=$(get_size "temp.svg")
if [ "${old_size}" -lt "${size_limit}" ]; then
continue
fi
ratio=`dc -e "${new_size} 100 * ${old_size} / p"`
if [ "${ratio}" -lt "${size_ratio_max}" ]; then
echo "Simplification for `basename ${s}` (${old_size} -> ${new_size}) ${ratio}%"
count=`dc -e "${count} 1 + p"`
if [ -z "${dry}" ]; then
mv temp.svg "${s}"
fi
fi
done
echo "Found ${count} svgs to simplify"
rm temp.svg