Forked from campino2k/download-google-web-fonts-for-local-hosting-fetch.sh
Last active
August 29, 2015 14:02
-
-
Save rotx/35c0021edef1dd181309 to your computer and use it in GitHub Desktop.
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 | |
#========================================================================================== | |
# 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 | |
# Modified by Robert | |
# * Fix Mac execution | |
# * Fix fonts without local PostScript name | |
# * Fix ttf format | |
# * Add configurable prefix | |
#========================================================================================== | |
declare -a families | |
families+=('Montserrat:400') | |
families+=('Montserrat:700') | |
families+=('Lora:400') | |
families+=('Lora:700') | |
families+=('Alegreya:400') | |
url="http://fonts.googleapis.com/css" | |
css="fonts.css" | |
prefix="../fonts/" | |
declare -a formats | |
declare -a useragent | |
# eot | |
formats+=('eot') | |
useragent+=('Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)') | |
# woff | |
formats+=('woff') | |
useragent+=('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14') | |
# ttf | |
formats+=('ttf') | |
useragent+=('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16') | |
# svg | |
formats+=('svg') | |
useragent+=('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') | |
>"$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" | grep -v "src:") | |
printf "\t\tlocal('%s'),\n" "$local_name" >>"$css" | |
if [ -n "$local_postscript_name" ]; then | |
printf "\t\tlocal('%s'),\n" "$local_postscript_name" >>"$css" | |
fi | |
index=0 | |
for uakey in "${formats[@]}"; do | |
echo -n "$uakey " | |
if [ "$uakey" != "svg" ]; then | |
pattern="http:\\/\\/[^\\)]+\\.$uakey" | |
else | |
pattern="http:\\/\\/[^\\)]+" | |
fi | |
ua=${useragent[$index]} | |
index=$(( $index + 1 )) | |
file=$(curl -sf -A "$ua" --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%s?#iefix') format('embedded-opentype'),\n" "$prefix" "${fontnameescaped}.$uakey" >>"$css" | |
;; | |
woff) | |
printf "\t\turl('%s%s') format('woff'),\n" "$prefix" "${fontnameescaped}.$uakey" >>"$css" | |
;; | |
ttf) | |
printf "\t\turl('%s%s') format('truetype'),\n" "$prefix" "${fontnameescaped}.$uakey" >>"$css" | |
;; | |
svg) | |
printf "\t\turl('%s%s#%s') format('svg');\n" "$prefix" "${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