Created
November 18, 2014 19:26
-
-
Save vaites/ac5e329053117b93637c to your computer and use it in GitHub Desktop.
Convert any EOT/OTF/TTF font to all required @font-face and shows a ready-to-use CSS code
This file contains hidden or 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
#!/bin/bash | |
# dependencies | |
FONTFORGE=`which fontforge` | |
TTFAUTOHINT=`which ttfautohint` | |
# base checks | |
if [ "x$1" == "x" ]; then | |
echo "Usage: ./font-forge.sh [FILE]" | |
exit | |
elif [ ! -f "$1" ]; then | |
echo "File $1 does not exists" | |
exit | |
elif [ "x$FONTFORGE" == "x" ]; then | |
echo "FontForge dependency not found" | |
exit | |
elif [ "x$TTFAUTOHINT" == "x" ]; then | |
echo "TTFAutohint dependency not found" | |
exit | |
fi | |
# filename and extension | |
FILE=$(basename "$1") | |
FILE=`echo $FILE | sed -e 's/ /-/g'` | |
FILE=`echo $FILE | tr '[:upper:]' '[:lower:]'` | |
FONT="${FILE%.*}" | |
FORMAT="${FILE##*.}" | |
# check font format | |
if [ `echo "${FORMAT}" | egrep -c "^(eot|otf|ttf)$"` -eq 0 ]; then | |
echo "Invalid font format" | |
exit | |
fi | |
# create FontForge script | |
echo '#!/usr/bin/env fontforge' > fforge.pe | |
echo 'Open($1)' >> fforge.pe | |
echo 'SetFontNames($1:r,$1:r,$1:r)' >> fforge.pe | |
echo 'Generate($2)' >> fforge.pe | |
chmod +x fforge.pe | |
# base TTF file | |
if [ $FORMAT != "ttf" ]; then | |
./fforge.pe "$1" "${FONT}.ttf" > /dev/null 2>&1 | |
FILE="${FONT}.ttf" | |
elif [ $FILE != "$1" ]; then | |
cp "$1" "${FILE}" | |
fi | |
# apply ttfautohint | |
ttfautohint "${FILE}" "${FONT}-hint.ttf" | |
mv -f "${FONT}-hint.ttf" "${FILE}" | |
# convert to other formats | |
./fforge.pe "${FILE}" "${FONT}.eot" > /dev/null 2>&1 | |
./fforge.pe "${FILE}" "${FONT}.svg" > /dev/null 2>&1 | |
./fforge.pe "${FILE}" "${FONT}.woff" > /dev/null 2>&1 | |
# move all files to fonts folder | |
if [ ! -d fonts ]; then | |
mkdir fonts | |
fi | |
rm -f "${FONT}.afm" | |
mv -f $FONT.* fonts/ | |
# remove FontForge script | |
rm -f fforge.pe | |
# CSS | |
echo "@font-face {" | |
echo " font-family: '${FONT}';" | |
echo " src: url('../fonts/${FONT}.eot');" | |
echo " src: url('../fonts/${FONT}.eot?#iefix') format('embedded-opentype')," | |
echo " url('../fonts/${FONT}.woff') format('woff')," | |
echo " url('../fonts/${FONT}.ttf') format('truetype')," | |
echo " url('../fonts/${FONT}.svg#${FONT}') format('svg');" | |
echo " font-weight: normal;" | |
echo " font-style: normal;" | |
echo "}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment