2016-06-15 18:46:59 +04:00
|
|
|
"""
|
2017-02-20 09:06:50 -06:00
|
|
|
Tool to find wrong contour order between different masters, and
|
2016-06-15 18:46:59 +04:00
|
|
|
other interpolatability (or lack thereof) issues.
|
2017-02-26 10:42:31 -08:00
|
|
|
|
|
|
|
Call as:
|
|
|
|
$ fonttools varLib.interpolatable font1 font2 ...
|
2016-06-15 18:46:59 +04:00
|
|
|
"""
|
|
|
|
|
2017-02-21 12:08:07 -06:00
|
|
|
from fontTools.pens.basePen import AbstractPen, BasePen
|
2017-02-21 12:10:30 -06:00
|
|
|
from fontTools.pens.recordingPen import RecordingPen
|
2017-02-20 13:18:08 -06:00
|
|
|
from fontTools.pens.statisticsPen import StatisticsPen
|
2016-06-24 17:48:06 -04:00
|
|
|
import itertools
|
2016-06-15 18:46:59 +04:00
|
|
|
|
2017-02-20 14:10:20 -06:00
|
|
|
|
|
|
|
class PerContourPen(BasePen):
|
2016-06-15 18:46:59 +04:00
|
|
|
def __init__(self, Pen, glyphset=None):
|
|
|
|
BasePen.__init__(self, glyphset)
|
|
|
|
self._glyphset = glyphset
|
|
|
|
self._Pen = Pen
|
2017-02-20 18:24:21 -06:00
|
|
|
self._pen = None
|
2016-06-15 18:46:59 +04:00
|
|
|
self.value = []
|
|
|
|
def _moveTo(self, p0):
|
|
|
|
self._newItem()
|
2017-02-20 18:24:21 -06:00
|
|
|
self._pen.moveTo(p0)
|
2016-06-15 18:46:59 +04:00
|
|
|
def _lineTo(self, p1):
|
2017-02-20 18:24:21 -06:00
|
|
|
self._pen.lineTo(p1)
|
2016-06-15 18:46:59 +04:00
|
|
|
def _qCurveToOne(self, p1, p2):
|
2017-02-20 18:24:21 -06:00
|
|
|
self._pen.qCurveTo(p1, p2)
|
2016-06-15 18:46:59 +04:00
|
|
|
def _curveToOne(self, p1, p2, p3):
|
2017-02-20 18:24:21 -06:00
|
|
|
self._pen.curveTo(p1, p2, p3)
|
2016-06-15 18:46:59 +04:00
|
|
|
def _closePath(self):
|
2017-02-20 18:24:21 -06:00
|
|
|
self._pen.closePath()
|
|
|
|
self._pen = None
|
2016-06-15 18:46:59 +04:00
|
|
|
def _endPath(self):
|
2017-02-20 18:24:21 -06:00
|
|
|
self._pen.endPath()
|
|
|
|
self._pen = None
|
2017-02-20 14:10:20 -06:00
|
|
|
|
|
|
|
def _newItem(self):
|
2017-02-22 10:33:57 -06:00
|
|
|
self._pen = pen = self._Pen()
|
2017-02-20 18:24:21 -06:00
|
|
|
self.value.append(pen)
|
2017-02-20 14:10:20 -06:00
|
|
|
|
|
|
|
class PerContourOrComponentPen(PerContourPen):
|
|
|
|
|
2016-06-15 18:46:59 +04:00
|
|
|
def addComponent(self, glyphName, transformation):
|
|
|
|
self._newItem()
|
|
|
|
self.value[-1].addComponent(glyphName, transformation)
|
|
|
|
|
|
|
|
|
2016-06-24 17:48:06 -04:00
|
|
|
def _vdiff(v0, v1):
|
|
|
|
return tuple(b-a for a,b in zip(v0,v1))
|
|
|
|
def _vlen(vec):
|
2016-06-15 18:46:59 +04:00
|
|
|
v = 0
|
2016-06-24 17:48:06 -04:00
|
|
|
for x in vec:
|
|
|
|
v += x*x
|
2016-06-15 18:46:59 +04:00
|
|
|
return v
|
|
|
|
|
2016-06-24 17:48:06 -04:00
|
|
|
def _matching_cost(G, matching):
|
|
|
|
return sum(G[i][j] for i,j in enumerate(matching))
|
|
|
|
|
|
|
|
def min_cost_perfect_bipartite_matching(G):
|
|
|
|
n = len(G)
|
2017-01-19 07:18:42 -08:00
|
|
|
try:
|
|
|
|
from scipy.optimize import linear_sum_assignment
|
|
|
|
rows, cols = linear_sum_assignment(G)
|
2017-01-19 15:39:48 +00:00
|
|
|
assert (rows == list(range(n))).all()
|
|
|
|
return list(cols), _matching_cost(G, cols)
|
2017-01-19 07:18:42 -08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
from munkres import Munkres
|
|
|
|
cols = [None] * n
|
|
|
|
for row,col in Munkres().compute(G):
|
|
|
|
cols[row] = col
|
|
|
|
return cols, _matching_cost(G, cols)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if n > 6:
|
|
|
|
raise Exception("Install Python module 'munkres' or 'scipy >= 0.17.0'")
|
|
|
|
|
|
|
|
# Otherwise just brute-force
|
|
|
|
permutations = itertools.permutations(range(n))
|
|
|
|
best = list(next(permutations))
|
|
|
|
best_cost = _matching_cost(G, best)
|
|
|
|
for p in permutations:
|
|
|
|
cost = _matching_cost(G, p)
|
|
|
|
if cost < best_cost:
|
|
|
|
best, best_cost = list(p), cost
|
|
|
|
return best, best_cost
|
2016-06-24 17:48:06 -04:00
|
|
|
|
|
|
|
|
2016-11-02 15:31:27 -07:00
|
|
|
def test(glyphsets, glyphs=None, names=None):
|
2016-06-15 18:46:59 +04:00
|
|
|
|
2016-11-02 15:31:27 -07:00
|
|
|
if names is None:
|
|
|
|
names = glyphsets
|
2016-06-15 18:46:59 +04:00
|
|
|
if glyphs is None:
|
|
|
|
glyphs = glyphsets[0].keys()
|
|
|
|
|
2016-06-24 17:48:06 -04:00
|
|
|
hist = []
|
2016-06-15 18:46:59 +04:00
|
|
|
for glyph_name in glyphs:
|
|
|
|
#print()
|
2016-11-02 15:31:27 -07:00
|
|
|
#print(glyph_name)
|
2016-06-15 18:46:59 +04:00
|
|
|
|
|
|
|
try:
|
|
|
|
allVectors = []
|
2017-02-19 19:24:40 -06:00
|
|
|
for glyphset,name in zip(glyphsets, names):
|
2016-06-15 18:46:59 +04:00
|
|
|
#print('.', end='')
|
|
|
|
glyph = glyphset[glyph_name]
|
|
|
|
|
2017-02-22 10:33:57 -06:00
|
|
|
perContourPen = PerContourOrComponentPen(RecordingPen, glyphset=glyphset)
|
2016-06-15 18:46:59 +04:00
|
|
|
glyph.draw(perContourPen)
|
|
|
|
contourPens = perContourPen.value
|
|
|
|
del perContourPen
|
|
|
|
|
|
|
|
contourVectors = []
|
|
|
|
allVectors.append(contourVectors)
|
|
|
|
for contour in contourPens:
|
2017-02-20 13:02:52 -06:00
|
|
|
stats = StatisticsPen(glyphset=glyphset)
|
2017-02-21 12:02:04 -06:00
|
|
|
contour.replay(stats)
|
2017-02-20 12:46:55 -06:00
|
|
|
size = abs(stats.area) ** .5 * .5
|
2016-06-15 18:46:59 +04:00
|
|
|
vector = (
|
2017-02-19 19:00:18 -06:00
|
|
|
int(size),
|
2017-02-20 12:46:55 -06:00
|
|
|
int(stats.meanX),
|
|
|
|
int(stats.meanY),
|
|
|
|
int(stats.stddevX * 2),
|
|
|
|
int(stats.stddevY * 2),
|
|
|
|
int(stats.correlation * size),
|
2016-06-15 18:46:59 +04:00
|
|
|
)
|
|
|
|
contourVectors.append(vector)
|
|
|
|
#print(vector)
|
|
|
|
|
|
|
|
# Check each master against the next one in the list.
|
2016-11-02 15:31:27 -07:00
|
|
|
for i,(m0,m1) in enumerate(zip(allVectors[:-1],allVectors[1:])):
|
2016-10-29 13:38:27 +02:00
|
|
|
if len(m0) != len(m1):
|
2016-11-02 15:31:27 -07:00
|
|
|
print('%s: %s+%s: Glyphs not compatible!!!!!' % (glyph_name, names[i], names[i+1]))
|
2016-10-29 13:38:27 +02:00
|
|
|
continue
|
2016-06-24 17:48:06 -04:00
|
|
|
if not m0:
|
|
|
|
continue
|
|
|
|
costs = [[_vlen(_vdiff(v0,v1)) for v1 in m1] for v0 in m0]
|
|
|
|
matching, matching_cost = min_cost_perfect_bipartite_matching(costs)
|
|
|
|
if matching != list(range(len(m0))):
|
2016-11-02 15:31:27 -07:00
|
|
|
print('%s: %s+%s: Glyph has wrong contour/component order: %s' % (glyph_name, names[i], names[i+1], matching)) #, m0, m1)
|
2016-06-24 17:48:06 -04:00
|
|
|
break
|
|
|
|
upem = 2048
|
2017-10-22 12:03:52 +01:00
|
|
|
item_cost = round((matching_cost / len(m0) / len(m0[0])) ** .5 / upem * 100)
|
2016-06-24 17:48:06 -04:00
|
|
|
hist.append(item_cost)
|
|
|
|
threshold = 7
|
|
|
|
if item_cost >= threshold:
|
2016-11-02 15:31:27 -07:00
|
|
|
print('%s: %s+%s: Glyph has very high cost: %d%%' % (glyph_name, names[i], names[i+1], item_cost))
|
2016-06-15 18:46:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
except ValueError as e:
|
2017-02-19 19:24:40 -06:00
|
|
|
print('%s: %s: math error %s; skipping glyph.' % (glyph_name, name, e))
|
|
|
|
print(contour.value)
|
2016-11-02 15:31:27 -07:00
|
|
|
#raise
|
2016-06-24 17:48:06 -04:00
|
|
|
#for x in hist:
|
|
|
|
# print(x)
|
2016-06-15 18:46:59 +04:00
|
|
|
|
|
|
|
def main(args):
|
|
|
|
filenames = args
|
|
|
|
glyphs = None
|
|
|
|
#glyphs = ['uni08DB', 'uniFD76']
|
2016-06-24 17:48:06 -04:00
|
|
|
#glyphs = ['uni08DE', 'uni0034']
|
|
|
|
#glyphs = ['uni08DE', 'uni0034', 'uni0751', 'uni0753', 'uni0754', 'uni08A4', 'uni08A4.fina', 'uni08A5.fina']
|
2016-11-02 15:31:27 -07:00
|
|
|
|
|
|
|
from os.path import basename
|
|
|
|
names = [basename(filename).rsplit('.', 1)[0] for filename in filenames]
|
|
|
|
|
2016-06-15 18:46:59 +04:00
|
|
|
from fontTools.ttLib import TTFont
|
|
|
|
fonts = [TTFont(filename) for filename in filenames]
|
2016-11-02 15:31:27 -07:00
|
|
|
|
2016-06-15 18:46:59 +04:00
|
|
|
glyphsets = [font.getGlyphSet() for font in fonts]
|
2016-11-02 15:31:27 -07:00
|
|
|
test(glyphsets, glyphs=glyphs, names=names)
|
2016-06-15 18:46:59 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
main(sys.argv[1:])
|