convert-svgs-to-pngs.sh (3007B)
1 #!/bin/bash 2 # This file is in the public domain. 3 # Convert local SVGs to the PNGs for display. 4 5 # Please make sure you have the relevant fonts installed - in the case of the builtin SVGs, these are the following: 6 # All: 7 # - Roboto (Arch: `ttf-roboto`) 8 # ADs: 9 # - Inter (Arch: `inter-font`) 10 # - Source Sans Pro (Arch: `adobe-source-sans-fonts`) 11 # Err: 12 # - JetBrains Mono (Arch: `ttf-jetbrains-mono`) 13 14 set -eu 15 16 # General Config 17 echo "${CONVERSION_TOOL:=inkscape}" &> /dev/null; # Which conversion tool to use - "im" (imagemagick), "gm" (graphicsmagick, can produce weird font results) and "inkscape" 18 echo "${TARGET_RESOLUTION:="768x576"}" &> /dev/null; # The final output resolution 19 echo "${MULTITHREAD:=0}" &> /dev/null; # Whether to parallelize it or not - can cause issues with inkscape somehow, idk 20 echo "${MULTITHREAD_COUNT:=0}" &> /dev/null; # How many instances to run before waiting again, if MULTITHREAD is non-falsey 21 22 # Imagemagick/Graphicsmagick Options 23 echo "${IM_SCALE_RESOLUTION:="1536x1152"}" &> /dev/null; # The resolution we set when telling magick to parse the SVG - higher than TARGET_RESOLUTION due to some blurry imagery caused by imagemagick otherwise. 24 25 # Convert a specific image 26 convertSvgToPng() { 27 case "$CONVERSION_TOOL" in 28 "im") 29 magick convert -density "$TARGET_RESOLUTION" "$1" -resize "$TARGET_RESOLUTION" "$2"; 30 ;; 31 "gm") 32 gm convert -density "$TARGET_RESOLUTION" "$1" -resize "$TARGET_RESOLUTION" "$2"; 33 ;; 34 "inkscape") 35 inkscape -w "$width" -h "$height" "$1" --export-png-color-mode=RGB_8 -o "$2" 2>&1 | grep -v -E 'data:font/|font-(family|style|weight|display)|unicode-range|end_font_face_cb: font face rule limited support' | sed '/^$/d' 36 ;; 37 *) 38 echo "FATAL: Invalid tool \"$CONVERSION_TOOL\"" 1>&2 39 ;; 40 esac; 41 } 42 43 # Setup Size Variables 44 if [[ "$CONVERSION_TOOL" == "inkscape" ]]; then 45 width=$(echo -n "$TARGET_RESOLUTION" | sed 's/x/ /' | awk '{print $1}') 46 height=$(echo -n "$TARGET_RESOLUTION" | sed 's/x/ /' | awk '{print $2}') 47 fi; 48 49 # Loop over every directory with imagesfor dir in err ads; 50 ctr=0; 51 for dir in err ads; 52 do 53 mkdir -p "${dir}/png" 54 55 # Convert them 56 for svg in "${dir}/svg/"*; 57 do 58 png=$(sed 's/svg/png/g' <<< "$svg"); 59 echo "Converting $svg to $png with $CONVERSION_TOOL"; 60 if [[ "$MULTITHREAD" != "" ]] && [[ "$MULTITHREAD" != "0" ]] && [[ "$MULTITHREAD" != "no" ]] && [[ "$MULTITHREAD" != "false" ]]; then 61 ctr=$((ctr + 1)) 62 convertSvgToPng "$svg" "$png" & 63 if [[ $ctr -gt ${MULTITHREAD_COUNT:=4} ]]; then # Inkscape can crash completely if we run too many instances of it at once 64 ctr=0; 65 wait; 66 fi; 67 else 68 convertSvgToPng "$svg" "$png"; 69 fi; 70 if which pngnq &>/dev/null; then 71 echo "Optimizing $png with pngnq" 72 pngnq -s1 < "$png" > "${png}".compact 73 mv "${png}".compact "$png" 74 else 75 echo "Skipping pngnq optimization for $png: pngnq not installed." 76 fi; 77 done; 78 79 wait; 80 done;