Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tomhermans/70222e7166b4cfa9ffce644b086341df to your computer and use it in GitHub Desktop.
Save tomhermans/70222e7166b4cfa9ffce644b086341df to your computer and use it in GitHub Desktop.
#!/bin/bash
# Convert all TTF files in the current directory using pyftsubset
# Creates subsetted versions in TTF, WOFF, and WOFF2 formats
# Usage: ./pyftall.sh [format] [options]
# format: ttf (default), woff, woff2, or all
# Any additional options will be passed to pyftsubset
# You can also add any additional parameters for pyftsubset after the format option:
# pyftall woff2 --unicodes=U+0000-04FF --with-zopfli
# Make sure fonttools is installed pip install fonttools
# add to zsh: echo 'alias pyftall="~/bin/.pyftall.sh"' >> ~/.zshrc
# Check if fonttools is installed
if ! command -v pyftsubset &> /dev/null; then
echo "Error: pyftsubset not found. Please install fonttools:"
echo "pip install fonttools"
exit 1
fi
# Default parameters for subsetting (basic Latin characters)
DEFAULT_PARAMS="--unicodes=U+0000-00FF"
# Determine output format(s)
FORMAT="ttf"
if [ $# -gt 0 ]; then
case "$1" in
ttf|woff|woff2|all)
FORMAT="$1"
shift # Remove the format argument
;;
esac
fi
# Process all TTF files in the current directory
for font in *.ttf; do
# Skip if no TTF files found
if [[ "$font" == "*.ttf" ]]; then
echo "No TTF files found in current directory."
exit 0
fi
echo "Processing $font..."
# Create base output filename (remove .ttf)
basename="${font%.ttf}"
# Process according to requested format(s)
if [[ "$FORMAT" == "ttf" || "$FORMAT" == "all" ]]; then
output="${basename}.subset.ttf"
echo " Creating TTF subset..."
pyftsubset "$font" $DEFAULT_PARAMS "$@" --output-file="$output"
if [ $? -eq 0 ]; then
echo " ✓ Created $output"
else
echo " ✗ Failed to create $output"
fi
fi
if [[ "$FORMAT" == "woff" || "$FORMAT" == "all" ]]; then
output="${basename}.subset.woff"
echo " Creating WOFF subset..."
pyftsubset "$font" $DEFAULT_PARAMS "$@" --flavor=woff --output-file="$output"
if [ $? -eq 0 ]; then
echo " ✓ Created $output"
else
echo " ✗ Failed to create $output"
fi
fi
if [[ "$FORMAT" == "woff2" || "$FORMAT" == "all" ]]; then
output="${basename}.subset.woff2"
echo " Creating WOFF2 subset..."
pyftsubset "$font" $DEFAULT_PARAMS "$@" --flavor=woff2 --output-file="$output"
if [ $? -eq 0 ]; then
echo " ✓ Created $output"
else
echo " ✗ Failed to create $output"
fi
fi
done
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment