variableScalar.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from fontTools.varLib.models import VariationModel, normalizeValue, piecewiseLinearMap
  2. def Location(loc):
  3. return tuple(sorted(loc.items()))
  4. class VariableScalar:
  5. """A scalar with different values at different points in the designspace."""
  6. def __init__(self, location_value={}):
  7. self.values = {}
  8. self.axes = {}
  9. for location, value in location_value.items():
  10. self.add_value(location, value)
  11. def __repr__(self):
  12. items = []
  13. for location, value in self.values.items():
  14. loc = ",".join(["%s=%i" % (ax, loc) for ax, loc in location])
  15. items.append("%s:%i" % (loc, value))
  16. return "(" + (" ".join(items)) + ")"
  17. @property
  18. def does_vary(self):
  19. values = list(self.values.values())
  20. return any(v != values[0] for v in values[1:])
  21. @property
  22. def axes_dict(self):
  23. if not self.axes:
  24. raise ValueError(
  25. ".axes must be defined on variable scalar before interpolating"
  26. )
  27. return {ax.axisTag: ax for ax in self.axes}
  28. def _normalized_location(self, location):
  29. location = self.fix_location(location)
  30. normalized_location = {}
  31. for axtag in location.keys():
  32. if axtag not in self.axes_dict:
  33. raise ValueError("Unknown axis %s in %s" % (axtag, location))
  34. axis = self.axes_dict[axtag]
  35. normalized_location[axtag] = normalizeValue(
  36. location[axtag], (axis.minValue, axis.defaultValue, axis.maxValue)
  37. )
  38. return Location(normalized_location)
  39. def fix_location(self, location):
  40. location = dict(location)
  41. for tag, axis in self.axes_dict.items():
  42. if tag not in location:
  43. location[tag] = axis.defaultValue
  44. return location
  45. def add_value(self, location, value):
  46. if self.axes:
  47. location = self.fix_location(location)
  48. self.values[Location(location)] = value
  49. def fix_all_locations(self):
  50. self.values = {
  51. Location(self.fix_location(l)): v for l, v in self.values.items()
  52. }
  53. @property
  54. def default(self):
  55. self.fix_all_locations()
  56. key = Location({ax.axisTag: ax.defaultValue for ax in self.axes})
  57. if key not in self.values:
  58. raise ValueError("Default value could not be found")
  59. # I *guess* we could interpolate one, but I don't know how.
  60. return self.values[key]
  61. def value_at_location(self, location, model_cache=None, avar=None):
  62. loc = location
  63. if loc in self.values.keys():
  64. return self.values[loc]
  65. values = list(self.values.values())
  66. return self.model(model_cache, avar).interpolateFromMasters(loc, values)
  67. def model(self, model_cache=None, avar=None):
  68. if model_cache is not None:
  69. key = tuple(self.values.keys())
  70. if key in model_cache:
  71. return model_cache[key]
  72. locations = [dict(self._normalized_location(k)) for k in self.values.keys()]
  73. if avar is not None:
  74. mapping = avar.segments
  75. locations = [
  76. {
  77. k: piecewiseLinearMap(v, mapping[k]) if k in mapping else v
  78. for k, v in location.items()
  79. }
  80. for location in locations
  81. ]
  82. m = VariationModel(locations)
  83. if model_cache is not None:
  84. model_cache[key] = m
  85. return m
  86. def get_deltas_and_supports(self, model_cache=None, avar=None):
  87. values = list(self.values.values())
  88. return self.model(model_cache, avar).getDeltasAndSupports(values)
  89. def add_to_variation_store(self, store_builder, model_cache=None, avar=None):
  90. deltas, supports = self.get_deltas_and_supports(model_cache, avar)
  91. store_builder.setSupports(supports)
  92. index = store_builder.storeDeltas(deltas)
  93. return int(self.default), index