Merge pull request #5 from moyogo/setflag

Use fonttools to set component flag
This commit is contained in:
Wei H 2015-03-20 09:49:33 +01:00
commit 897e711bf8
2 changed files with 28 additions and 52 deletions

View File

@ -1,38 +1,16 @@
for i in *.ttf; do
ttx -t glyf "$i"
done
for i in *.ttx; do
python setflag.py "$i"
done
for i in *.ttx; do
name=$(basename "$i" .ttx)
path=$(dirname "$i")
ttx -m "$path/$name.ttf" "$i"
rm "$i"
rm "$path/$name.ttf"
# mv "$path/$name#1.ttf" "$path/$name.ttf"
echo "Set flag for $i"
done
ttfautohint -n -w G -x 0 -f latn WorkSans-Hairline#1.ttf WorkSans-Hairline.ttf
echo "WorkSans-Hairline.ttf hinted"
ttfautohint -n -w G -x 0 -f latn WorkSans-Thin#1.ttf WorkSans-Thin.ttf
echo "WorkSans-Thin.ttf hinted"
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-ExtraLight.ctrl WorkSans-ExtraLight#1.ttf WorkSans-ExtraLight.ttf
echo "WorkSans-ExtraLight.ttf hinted"
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-Light.ctrl WorkSans-Light#1.ttf WorkSans-Light.ttf
echo "WorkSans-Light.ttf hinted"
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-Regular.ctrl WorkSans-Regular#1.ttf WorkSans-Regular.ttf
echo "WorkSans-Regular.ttf hinted"
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-Medium.ctrl WorkSans-Medium#1.ttf WorkSans-Medium.ttf
echo "WorkSans-Medium.ttf hinted"
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-SemiBold.ctrl WorkSans-SemiBold#1.ttf WorkSans-SemiBold.ttf
echo "WorkSans-SemiBold.ttf hinted"
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-Bold.ctrl WorkSans-Bold#1.ttf WorkSans-Bold.ttf
echo "WorkSans-Bold.ttf hinted"
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-ExtraBold.ctrl WorkSans-ExtraBold#1.ttf WorkSans-ExtraBold.ttf
echo "WorkSans-ExtraBold.ttf hinted"
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-Black.ctrl WorkSans-Black#1.ttf WorkSans-Black.ttf
echo "WorkSans-Black.ttf hinted"
for i in Hairline Thin; do
ttfautohint -n -w G -x 0 -f latn WorkSans-$i#1.ttf WorkSans-$i.ttf
echo "WorkSans-$i.ttf hinted"
done
for i in ExtraLight Light Regular Medium SemiBold Bold ExtraBold Black ; do
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-$i.ctrl WorkSans-$i#1.ttf WorkSans-$i.ttf
echo "WorkSans-$i.ttf hinted"
done
for i in *#1.ttf; do
rm "$i"

View File

@ -1,27 +1,25 @@
# This python script finds and replaces the flags="0x0" with flags="0x4" to set the ROUND_XY_TO_GRID bit in a TTX.
# I'm sure this could be done in a much more efficient way, i.e. actually calling fontTools directly instead of dumping and recompiling the GLYF table with TTX
# ... but I don't know how to do that
# This python script finds and replaces the flags="0x0" with flags="0x4" to set the ROUND_XY_TO_GRID bit in a TTF.
import os
import sys
from fontTools.ttLib import TTFont
from fontTools.ttx import makeOutputFileName
# import the modules that we need. (re is for regex)
import os, re, sys
eachTTX = sys.argv[1]
inputTTF = sys.argv[1]
# set the working directory for a shortcut
# os.chdir('/Users/weihuang/Google Drive/Type Design/Google/Outputs/Test/Hinting')
# open the source file and read it
fh = file(eachTTX, 'r')
subject = fh.read()
fh.close()
# create the pattern object. Note the "r". In case you're unfamiliar with Python
# this is to set the string as raw so we don't have to escape our escape characters
pattern = re.compile(r'<component (.+)flags="(0x0)"(.+)')
# do the replace
result = pattern.sub(r'<component \1flags="0x4"\3', subject)
# write the file
f_out = file(eachTTX, 'w')
f_out.write(result)
f_out.close()
font = TTFont(inputTTF)
extension = os.path.splitext(inputTTF)[1]
if 'glyf' in font:
glyf = font['glyf']
for glyphname in glyf.glyphs:
glyph = glyf.glyphs[glyphname]
if glyph.isComposite():
glyph.expand(glyf)
for component in glyph.components:
component.flags = 0x4
glyph.compact(glyf)
outputTTF = makeOutputFileName(inputTTF, '', extension)
font.save(outputTTF)