Forked from astehlik/download-google-web-fonts-for-local-hosting-fetch.sh
Created
May 21, 2014 11:06
-
-
Save rutger1140/65ab7f78647da774715d to your computer and use it in GitHub Desktop.
Download Google webfonts for local usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Use with bash 4.0+ | |
#========================================================================================== | |
# Original Script from Clemens Lang, neverpanic.de | |
# https://neverpanic.de/blog/2014/03/19/downloading-google-web-fonts-for-local-hosting/ | |
# Modified by Chris Jung, campino2k.de | |
# * Rename Files to be compatible with Windows File System (remove spaces and colon) | |
# * Add "local()" to src like Google does to skip downloading if System has font installed | |
#========================================================================================== | |
declare -a families | |
families+=('PT Sans:400') | |
families+=('PT Sans:700') | |
families+=('PT Sans:400italic') | |
families+=('PT Sans:700italic') | |
families+=('PT Serif:400') | |
families+=('PT Serif:700') | |
families+=('PT Serif:400italic') | |
families+=('PT Serif:700italic') | |
url="http://fonts.googleapis.com/css" | |
css="font.css" | |
declare -A useragent | |
useragent[eot]='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)' | |
useragent[woff]='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0' | |
useragent[svg]='Mozilla/4.0 (iPad; CPU OS 4_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/4.1 Mobile/9A405 Safari/7534.48.3' | |
useragent[ttf]='Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16' | |
>"$css" | |
for family in "${families[@]}"; do | |
echo -n "Downloading ${family}... " | |
printf "@font-face {\n" >>"$css" | |
fontname=$(echo "$family" | awk -F : '{print $1}') | |
fontnameescaped="$(echo "$family" | sed -E 's/( |:)/_/g')" | |
fontstyle=$(echo "$family" | awk -F : '{print $2}') | |
fontweight=$(echo "$fontstyle" | sed -E 's/italic$//g') | |
printf "\tfont-family: '%s';\n" "$(echo "$family" | awk -F : '{print $1}')" >>"$css" | |
case "$fontstyle" in | |
*italic) | |
printf "\tfont-style: italic;\n" >>"$css" | |
;; | |
*) | |
printf "\tfont-style: normal;\n" >>"$css" | |
;; | |
esac | |
printf "\tfont-weight: %s;\n" "${fontweight:-normal}" >>"$css" | |
printf "\tsrc:\n" >>"$css" | |
local_name=$(curl -sf --get --data-urlencode "family=$family" "$url" | grep -E "src:" | sed -E "s/^.*src: local\\('([^']+)'\\),.*$/\\1/g") | |
local_postscript_name=$(curl -sf --get --data-urlencode "family=$family" "$url" | grep -E "src:" | sed -E "s/^.*, local\\('([^']+)'\\),.*$/\\1/g") | |
printf "\t\tlocal('%s'),\n" "$local_name" >>"$css" | |
printf "\t\tlocal('%s'),\n" "$local_postscript_name" >>"$css" | |
for uakey in eot woff ttf svg; do | |
echo -n "$uakey " | |
if [ "$uakey" != "svg" ]; then | |
pattern="http:\\/\\/[^\\)]+\\.$uakey" | |
else | |
pattern="http:\\/\\/[^\\)]+" | |
fi | |
file=$(curl -sf -A "${useragent[$uakey]}" --get --data-urlencode "family=$family" "$url" | grep -Eo "$pattern" | sort -u) | |
printf "\t\t/* from %s */\n" "$file" >>"$css" | |
if [ "$uakey" == "svg" ]; then | |
svgname=$(echo "$file" | sed -E 's/^[^#]+#(.*)$/\1/g') | |
fi | |
curl -sfL "$file" -o "${fontnameescaped}.$uakey" | |
case "$uakey" in | |
eot) | |
printf "\t\turl('%s?#iefix') format('embedded-opentype'),\n" "${fontnameescaped}.$uakey" >>"$css" | |
;; | |
woff) | |
printf "\t\turl('%s') format('woff'),\n" "${fontnameescaped}.$uakey" >>"$css" | |
;; | |
ttf) | |
printf "\t\turl('%s') format('ttf'),\n" "${fontnameescaped}.$uakey" >>"$css" | |
;; | |
svg) | |
printf "\t\turl('%s#%s') format('svg');\n" "${fontnameescaped}.${uakey}" "$svgname" >>"$css" | |
;; | |
esac | |
done | |
printf "}\n" >>"$css" | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment