Skip to content

Instantly share code, notes, and snippets.

@duairc
Created February 15, 2014 02:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duairc/9013675 to your computer and use it in GitHub Desktop.
Save duairc/9013675 to your computer and use it in GitHub Desktop.
Given the name of a font family, generate webfonts + a CSS file on a Linux system
#!/bin/sh
clean () {
perl -pe 's/(\([si]\)\s*)+//' | tr '"' ' ' | awk '{$1=""; print $0}' | xargs echo
}
subset () {
python /usr/share/googlefontdirectory-tools/tools/subset/subset.py --subset=latin+latin-ext+greek+greek-ext+cyrillic+cyrillic-ext --nmr --null --roundtrip --script "$1" "$2"
}
to_ttf () {
echo "Open(\"$1\")\nGenerate(\"$2\")" | fontforge -lang=ff -script
}
do_font () {
output=$(fc-query "$1")
family=$(echo "$output" | grep 'family:' | tr '(' '\n' | head -1 | clean)
fullname=$(echo "$output" | grep 'fullname:' | clean)
slant=$(echo "$output" | grep 'slant:' | clean)
weight=$(echo "$output" | grep 'weight:' | clean)
file=$(echo "$output" | grep 'file:' | clean | perl -pe 's{^.*/}{};s/\..*$//')
fontstyle=$([ "$slant" -gt 0 ] && echo 'italic' || echo 'normal')
fontweight=$(if [ "$weight" -gt 100 ]; then
echo 'bold'
elif [ "$weight" -lt 50 ]; then
echo 'lighter'
else
echo 'normal'
fi)
to_ttf "$1" "fonts/$file.full.ttf"
subset "fonts/$file.full.ttf" "fonts/$file.ttf"
rm "fonts/$file.full.ttf"
webify "fonts/$file.ttf"
base64=$(cat "fonts/$file.woff" | base64 | tr -d '\n')
echo "@font-face {
font-family: \"$family\";
src: url(\"../fonts/$file.eot\");
src:
local(\"$fullname\")," >> css/fonts.css
if [ "$fullname" != "$file" ]; then
echo " local(\"$file\")," >> css/fonts.css
fi
#echo " url(data:application/font-woff;charset=utf-8;base64,$base64) format(\"woff\")," >> css/fonts.css
echo " url(\"../fonts/$file.eot?#iefix\") format(\"embedded-opentype\"),
url(\"../fonts/$file.woff\") format(\"woff\"),
url(\"../fonts/$file.ttf\") format(\"truetype\");
font-weight: $fontweight;
font-style: $fontstyle;
}
" >> css/fonts.css
}
mkdir -p fonts
mkdir -p css
for family in "$@"; do
fc-list "$family" | cut -d: -f1 | xargs readlink -f | sort -u | grep -v Condensed | while read font; do
echo "$font"
do_font "$font"
done
done
@dpawson
Copy link

dpawson commented Dec 8, 2015

Missing 'webify' routine? Any url please? Line 33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment