Speeds up font compilation by around 200%

Cython is used to compile some hot paths into native Python extensions.
These hot paths were identified through running ufocompile with the hotshot
profiler and then converting file by file to Cython, starting with the "hottest"
paths and continuing until returns were deminishing. This means that only a few
Python files were converted to Cython.

Closes #23
Closes #20 (really this time)
This commit is contained in:
Rasmus Andersson 2017-09-03 23:03:17 -04:00
parent 31ae014e0c
commit 8234b62ab7
108 changed files with 26933 additions and 110 deletions

1
misc/pylib/fontbuild/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.c

View file

@ -18,6 +18,7 @@ import os
import sys
from booleanOperations import BooleanOperationManager
from cu2qu.ufo import fonts_to_quadratic
from fontTools.misc.transform import Transform
from robofab.world import OpenFont
@ -201,25 +202,25 @@ class FontProject:
saveOTF(font, ttfName, self.glyphOrder, truetype=True)
def transformGlyphMembers(g, m):
g.width = int(g.width * m.a)
g.Transform(m)
for a in g.anchors:
p = Point(a.p)
p.Transform(m)
a.p = p
for c in g.components:
# Assumes that components have also been individually transformed
p = Point(0,0)
d = Point(c.deltas[0])
d.Transform(m)
p.Transform(m)
d1 = d - p
c.deltas[0].x = d1.x
c.deltas[0].y = d1.y
s = Point(c.scale)
s.Transform(m)
#c.scale = s
# def transformGlyphMembers(g, m):
# g.width = int(g.width * m.a)
# g.Transform(m)
# for a in g.anchors:
# p = Point(a.p)
# p.Transform(m)
# a.p = p
# for c in g.components:
# # Assumes that components have also been individually transformed
# p = Point(0,0)
# d = Point(c.deltas[0])
# d.Transform(m)
# p.Transform(m)
# d1 = d - p
# c.deltas[0].x = d1.x
# c.deltas[0].y = d1.y
# s = Point(c.scale)
# s.Transform(m)
# #c.scale = s
def swapContours(f,gName1,gName2):

View file

@ -108,13 +108,18 @@ def findCorner(pp, nn):
# print "parallel lines", np.arctan2(prev[1],prev[0]), np.arctan2(next[1],next[0])
# print prev, next
assert 0, "parallel lines"
if glyph.name is None:
# Never happens, but here to fix a bug in Python 2.7 with -OO
print ''
# if glyph.name is None:
# # Never happens, but here to fix a bug in Python 2.7 with -OO
# print ''
return lineIntersect(pStart, pEnd, nStart, nEnd)
def lineIntersect((x1,y1),(x2,y2),(x3,y3),(x4,y4)):
def lineIntersect(p1, p2, p3, p4):
x1, y1 = p1
x2, y2 = p2
x3, y3 = p3
x4, y4 = p4
x12 = x1 - x2
x34 = x3 - x4
y12 = y1 - y2

View file

@ -59,8 +59,8 @@ def italicize(glyph, angle=12, stemWidth=180, xoffset=-50):
ga, subsegments = segmentGlyph(glyph,25)
va, e = glyphToMesh(ga)
n = len(va)
grad = mapEdges(lambda a,(p,n): normalize(p-a), va, e)
cornerWeights = mapEdges(lambda a,(p,n): normalize(p-a).dot(normalize(a-n)), grad, e)[:,0].reshape((-1,1))
grad = mapEdges(lambda a, pn: normalize(pn[0]-a), va, e)
cornerWeights = mapEdges(lambda a, pn: normalize(pn[0]-a).dot(normalize(a-pn[1])), grad, e)[:,0].reshape((-1,1))
smooth = np.ones((n,1)) * CURVE_CORRECTION_WEIGHT
controlPoints = findControlPointsInMesh(glyph, va, subsegments)
@ -182,7 +182,7 @@ def findControlPointsInMesh(glyph, va, subsegments):
def recompose(v, grad, e, smooth=1, P=None, distance=None):
n = len(v)
if distance == None:
distance = mapEdges(lambda a,(p,n): norm(p - a), v, e)
distance = mapEdges(lambda a, pn: norm(pn[0] - a), v, e)
if (P == None):
P = mP(v,e)
P += np.identity(n) * smooth
@ -233,7 +233,7 @@ def getNormal(a,b,c):
def edgeNormals(v,e):
"Assumes a mesh where each vertex has exactly least two edges"
return mapEdges(lambda a,(p,n) : getNormal(a,p,n),v,e)
return mapEdges(lambda a, pn : getNormal(a,pn[0],pn[1]),v,e)
def rangePrevNext(count):
@ -268,10 +268,10 @@ def copyGradDetails(a,b,e,scale=15):
def copyMeshDetails(va,vb,e,scale=5,smooth=.01):
gradA = mapEdges(lambda a,(p,n): normalize(p-a), va, e)
gradB = mapEdges(lambda a,(p,n): normalize(p-a), vb, e)
gradA = mapEdges(lambda a, pn: normalize(pn[0]-a), va, e)
gradB = mapEdges(lambda a, pn: normalize(pn[0]-a), vb, e)
grad = copyGradDetails(gradA, gradB, e, scale)
grad = mapEdges(lambda a,(p,n): normalize(a), grad, e)
grad = mapEdges(lambda a, pn: normalize(a), grad, e)
return recompose(vb, grad, e, smooth=smooth)
@ -282,7 +282,7 @@ def condenseGlyph(glyph, scale=.8, stemWidth=185):
normals = edgeNormals(va,e)
cn = va.dot(np.array([[scale, 0],[0,1]]))
grad = mapEdges(lambda a,(p,n): normalize(p-a), cn, e)
grad = mapEdges(lambda a, pn: normalize(pn[0]-a), cn, e)
# ograd = mapEdges(lambda a,(p,n): normalize(p-a), va, e)
cn[:,0] -= normals[:,0] * stemWidth * .5 * (1 - scale)

View file

@ -269,7 +269,7 @@ class Mix:
def getFGlyph(self, master, gname):
if isinstance(master.font, Mix):
return font.mixGlyphs(gname)
return master.font.mixGlyphs(gname)
return master.ffont.getGlyph(gname)
def getGlyphMasters(self,gname):

View file

@ -0,0 +1,19 @@
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [
Extension("decomposeGlyph", ["decomposeGlyph.pyx"]),
Extension("alignpoints", ["alignpoints.pyx"]),
Extension("Build", ["Build.pyx"]),
Extension("convertCurves", ["convertCurves.pyx"]),
Extension("mitreGlyph", ["mitreGlyph.pyx"]),
Extension("mix", ["mix.pyx"]),
Extension("italics", ["italics.pyx"]),
]
setup(
name = 'copy',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)