[varLib] Shuffle

This commit is contained in:
Behdad Esfahbod 2016-04-13 00:33:42 -07:00
parent c16086afaa
commit ef4aa7e7f0

View File

@ -6,55 +6,15 @@ from __future__ import unicode_literals
from fontTools.misc.py23 import *
def getMasterLocationsSortKeyFunc(locations, axisOrder=[]):
assert {} in locations, "Base master not found."
axisPoints = {}
for loc in locations:
if len(loc) != 1:
continue
axis = next(iter(loc))
value = loc[axis]
if axis not in axisPoints:
axisPoints[axis] = {0}
assert value not in axisPoints[axis]
axisPoints[axis].add(value)
class MutatorModel(object):
def getKey(axisPoints, axisOrder):
def sign(v):
return -1 if v < 0 else +1 if v > 0 else 0
def key(loc):
rank = len(loc)
onPointAxes = [axis for axis,value in loc.items() if value in axisPoints[axis]]
orderedAxes = [axis for axis in axisOrder if axis in loc]
orderedAxes.extend([axis for axis in sorted(loc.keys()) if axis not in axisOrder])
return (
rank, # First, order by increasing rank
-len(onPointAxes), # Next, by decreasing number of onPoint axes
tuple(axisOrder.index(axis) if axis in axisOrder else 0x10000 for axis in orderedAxes), # Next, by known axes
tuple(orderedAxes), # Next, by all axes
tuple(sign(loc[axis]) for axis in orderedAxes), # Next, by signs of axis values
tuple(abs(loc[axis]) for axis in orderedAxes), # Next, by absolute value of axis values
)
return key
ret = getKey(axisPoints, axisOrder)
ret.axisPoints = axisPoints
return ret
def sortMasterLocations(locations, axisOrder=[]):
"""
Sort masters.
Locations must be in normalized space. Ie. base master
is at origin (0).
"""
return sorted(locations, key=getMasterLocationsSortKeyFunc(locations, axisOrder=axisOrder))
class MutatorModel(object):
def __init__(self, locations, axisOrder=[]):
keyFunc = getMasterLocationsSortKeyFunc(locations, axisOrder=axisOrder)
keyFunc = self.getMasterLocationsSortKeyFunc(locations, axisOrder=axisOrder)
axisPoints = keyFunc.axisPoints
self.origLocations = locations
self.locations = sorted(locations, key=keyFunc)
@ -63,6 +23,42 @@ class MutatorModel(object):
self._computeMasterSupports(axisPoints)
@staticmethod
def getMasterLocationsSortKeyFunc(locations, axisOrder=[]):
assert {} in locations, "Base master not found."
axisPoints = {}
for loc in locations:
if len(loc) != 1:
continue
axis = next(iter(loc))
value = loc[axis]
if axis not in axisPoints:
axisPoints[axis] = {0}
assert value not in axisPoints[axis]
axisPoints[axis].add(value)
def getKey(axisPoints, axisOrder):
def sign(v):
return -1 if v < 0 else +1 if v > 0 else 0
def key(loc):
rank = len(loc)
onPointAxes = [axis for axis,value in loc.items() if value in axisPoints[axis]]
orderedAxes = [axis for axis in axisOrder if axis in loc]
orderedAxes.extend([axis for axis in sorted(loc.keys()) if axis not in axisOrder])
return (
rank, # First, order by increasing rank
-len(onPointAxes), # Next, by decreasing number of onPoint axes
tuple(axisOrder.index(axis) if axis in axisOrder else 0x10000 for axis in orderedAxes), # Next, by known axes
tuple(orderedAxes), # Next, by all axes
tuple(sign(loc[axis]) for axis in orderedAxes), # Next, by signs of axis values
tuple(abs(loc[axis]) for axis in orderedAxes), # Next, by absolute value of axis values
)
return key
ret = getKey(axisPoints, axisOrder)
ret.axisPoints = axisPoints
return ret
@staticmethod
def lowerBound(value, lst):
if any(v < value for v in lst):
@ -128,8 +124,8 @@ locations = [
{'wght':+180},
{},
]
from pprint import pprint
assert sortMasterLocations(locations, axisOrder=['wght']) == \
model = MutatorModel(locations, axisOrder=['wght'])
assert model.locations == \
[{},
{u'wght': -100},
{u'wght': -180},
@ -138,7 +134,8 @@ assert sortMasterLocations(locations, axisOrder=['wght']) == \
{u'wdth': 0.3},
{u'wdth': 0.3, u'wght': 180},
{u'wdth': 0.3, u'wght': 120},
{u'wdth': 0.2, u'wght': 120}]
model = MutatorModel(locations)
{u'wdth': 0.2, u'wght': 120},
]
from pprint import pprint
pprint(model.locations)
pprint(model.supports)