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 for i in *.ttf; do
ttx -t glyf "$i"
done
for i in *.ttx; do
python setflag.py "$i" python setflag.py "$i"
done echo "Set flag for $i"
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"
done done
ttfautohint -n -w G -x 0 -f latn WorkSans-Hairline#1.ttf WorkSans-Hairline.ttf for i in Hairline Thin; do
echo "WorkSans-Hairline.ttf hinted" ttfautohint -n -w G -x 0 -f latn WorkSans-$i#1.ttf WorkSans-$i.ttf
ttfautohint -n -w G -x 0 -f latn WorkSans-Thin#1.ttf WorkSans-Thin.ttf echo "WorkSans-$i.ttf hinted"
echo "WorkSans-Thin.ttf hinted" done
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-ExtraLight.ctrl WorkSans-ExtraLight#1.ttf WorkSans-ExtraLight.ttf for i in ExtraLight Light Regular Medium SemiBold Bold ExtraBold Black ; do
echo "WorkSans-ExtraLight.ttf hinted" ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-$i.ctrl WorkSans-$i#1.ttf WorkSans-$i.ttf
ttfautohint -n -w G -x 0 -f latn --control-file=hinting/WorkSans-Light.ctrl WorkSans-Light#1.ttf WorkSans-Light.ttf echo "WorkSans-$i.ttf hinted"
echo "WorkSans-Light.ttf hinted" done
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 *#1.ttf; do for i in *#1.ttf; do
rm "$i" 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. # This python script finds and replaces the flags="0x0" with flags="0x4" to set the ROUND_XY_TO_GRID bit in a TTF.
# 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 import os
# ... but I don't know how to do that import sys
from fontTools.ttLib import TTFont
from fontTools.ttx import makeOutputFileName
# import the modules that we need. (re is for regex) inputTTF = sys.argv[1]
import os, re, sys
eachTTX = sys.argv[1]
# set the working directory for a shortcut # set the working directory for a shortcut
# os.chdir('/Users/weihuang/Google Drive/Type Design/Google/Outputs/Test/Hinting') # os.chdir('/Users/weihuang/Google Drive/Type Design/Google/Outputs/Test/Hinting')
# open the source file and read it # open the source file and read it
fh = file(eachTTX, 'r') font = TTFont(inputTTF)
subject = fh.read() extension = os.path.splitext(inputTTF)[1]
fh.close() if 'glyf' in font:
glyf = font['glyf']
# create the pattern object. Note the "r". In case you're unfamiliar with Python for glyphname in glyf.glyphs:
# this is to set the string as raw so we don't have to escape our escape characters glyph = glyf.glyphs[glyphname]
pattern = re.compile(r'<component (.+)flags="(0x0)"(.+)') if glyph.isComposite():
# do the replace glyph.expand(glyf)
result = pattern.sub(r'<component \1flags="0x4"\3', subject) for component in glyph.components:
component.flags = 0x4
# write the file glyph.compact(glyf)
f_out = file(eachTTX, 'w') outputTTF = makeOutputFileName(inputTTF, '', extension)
f_out.write(result) font.save(outputTTF)
f_out.close()