Snippets/decompose-ttf: remove overlaps with skia-pathops
This commit is contained in:
parent
290ee78e3d
commit
3ad201cbe5
@ -9,10 +9,18 @@ from fontTools.ttLib import TTFont
|
|||||||
from fontTools.pens.recordingPen import DecomposingRecordingPen
|
from fontTools.pens.recordingPen import DecomposingRecordingPen
|
||||||
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pathops
|
||||||
|
except ImportError:
|
||||||
|
sys.exit(
|
||||||
|
"This script requires the skia-pathops module. "
|
||||||
|
"`pip install skia-pathops` and then retry."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if len(sys.argv) != 3:
|
if len(sys.argv) != 3:
|
||||||
print("usage: decompose-ttf.py fontfile.ttf outfile.ttf")
|
print("usage: decompose-ttf.py fontfile.ttf outfile.ttf")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
src = sys.argv[1]
|
src = sys.argv[1]
|
||||||
dst = sys.argv[2]
|
dst = sys.argv[2]
|
||||||
@ -24,10 +32,22 @@ with TTFont(src) as f:
|
|||||||
for glyphName in glyphSet.keys():
|
for glyphName in glyphSet.keys():
|
||||||
if not glyfTable[glyphName].isComposite():
|
if not glyfTable[glyphName].isComposite():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# record TTGlyph outlines without components
|
||||||
dcPen = DecomposingRecordingPen(glyphSet)
|
dcPen = DecomposingRecordingPen(glyphSet)
|
||||||
glyphSet[glyphName].draw(dcPen)
|
glyphSet[glyphName].draw(dcPen)
|
||||||
|
|
||||||
|
# replay recording onto a skia-pathops Path
|
||||||
|
path = pathops.Path()
|
||||||
|
pathPen = path.getPen()
|
||||||
|
dcPen.replay(pathPen)
|
||||||
|
|
||||||
|
# remove overlaps
|
||||||
|
path.simplify()
|
||||||
|
|
||||||
|
# create new TTGlyph from Path
|
||||||
ttPen = TTGlyphPen(None)
|
ttPen = TTGlyphPen(None)
|
||||||
dcPen.replay(ttPen)
|
path.draw(ttPen)
|
||||||
glyfTable[glyphName] = ttPen.glyph()
|
glyfTable[glyphName] = ttPen.glyph()
|
||||||
|
|
||||||
f.save(dst)
|
f.save(dst)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user