variableScalar.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(
  15. [
  16. f"{ax}={int(coord) if float(coord).is_integer() else coord}"
  17. for ax, coord in location
  18. ]
  19. )
  20. items.append("%s:%i" % (loc, value))
  21. return "(" + (" ".join(items)) + ")"
  22. @property
  23. def does_vary(self):
  24. values = list(self.values.values())
  25. return any(v != values[0] for v in values[1:])
  26. @property
  27. def axes_dict(self):
  28. if not self.axes:
  29. raise ValueError(
  30. ".axes must be defined on variable scalar before interpolating"
  31. )
  32. return {ax.axisTag: ax for ax in self.axes}
  33. def _normalized_location(self, location):
  34. location = self.fix_location(location)
  35. normalized_location = {}
  36. for axtag in location.keys():
  37. if axtag not in self.axes_dict:
  38. raise ValueError("Unknown axis %s in %s" % (axtag, location))
  39. axis = self.axes_dict[axtag]
  40. normalized_location[axtag] = normalizeValue(
  41. location[axtag], (axis.minValue, axis.defaultValue, axis.maxValue)
  42. )
  43. return Location(normalized_location)
  44. def fix_location(self, location):
  45. location = dict(location)
  46. for tag, axis in self.axes_dict.items():
  47. if tag not in location:
  48. location[tag] = axis.defaultValue
  49. return location
  50. def add_value(self, location, value):
  51. if self.axes:
  52. location = self.fix_location(location)
  53. self.values[Location(location)] = value
  54. def fix_all_locations(self):
  55. self.values = {
  56. Location(self.fix_location(l)): v for l, v in self.values.items()
  57. }
  58. @property
  59. def default(self):
  60. self.fix_all_locations()
  61. key = Location({ax.axisTag: ax.defaultValue for ax in self.axes})
  62. if key not in self.values:
  63. raise ValueError("Default value could not be found")
  64. # I *guess* we could interpolate one, but I don't know how.
  65. return self.values[key]
  66. def value_at_location(self, location, model_cache=None, avar=None):
  67. loc = Location(location)
  68. if loc in self.values.keys():
  69. return self.values[loc]
  70. values = list(self.values.values())
  71. loc = dict(self._normalized_location(loc))
  72. return self.model(model_cache, avar).interpolateFromMasters(loc, values)
  73. def model(self, model_cache=None, avar=None):
  74. if model_cache is not None:
  75. key = tuple(self.values.keys())
  76. if key in model_cache:
  77. return model_cache[key]
  78. locations = [dict(self._normalized_location(k)) for k in self.values.keys()]
  79. if avar is not None:
  80. mapping = avar.segments
  81. locations = [
  82. {
  83. k: piecewiseLinearMap(v, mapping[k]) if k in mapping else v
  84. for k, v in location.items()
  85. }
  86. for location in locations
  87. ]
  88. m = VariationModel(locations)
  89. if model_cache is not None:
  90. model_cache[key] = m
  91. return m
  92. def get_deltas_and_supports(self, model_cache=None, avar=None):
  93. values = list(self.values.values())
  94. return self.model(model_cache, avar).getDeltasAndSupports(values)
  95. def add_to_variation_store(self, store_builder, model_cache=None, avar=None):
  96. deltas, supports = self.get_deltas_and_supports(model_cache, avar)
  97. store_builder.setSupports(supports)
  98. index = store_builder.storeDeltas(deltas)
  99. return int(self.default), index