interpolatableHelpers.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. from fontTools.ttLib.ttGlyphSet import LerpGlyphSet
  2. from fontTools.pens.basePen import AbstractPen, BasePen, DecomposingPen
  3. from fontTools.pens.pointPen import AbstractPointPen, SegmentToPointPen
  4. from fontTools.pens.recordingPen import RecordingPen, DecomposingRecordingPen
  5. from fontTools.misc.transform import Transform
  6. from collections import defaultdict, deque
  7. from math import sqrt, copysign, atan2, pi
  8. import itertools
  9. import logging
  10. log = logging.getLogger("fontTools.varLib.interpolatable")
  11. class InterpolatableProblem:
  12. NOTHING = "nothing"
  13. MISSING = "missing"
  14. OPEN_PATH = "open_path"
  15. PATH_COUNT = "path_count"
  16. NODE_COUNT = "node_count"
  17. NODE_INCOMPATIBILITY = "node_incompatibility"
  18. CONTOUR_ORDER = "contour_order"
  19. WRONG_START_POINT = "wrong_start_point"
  20. KINK = "kink"
  21. UNDERWEIGHT = "underweight"
  22. OVERWEIGHT = "overweight"
  23. severity = {
  24. MISSING: 1,
  25. OPEN_PATH: 2,
  26. PATH_COUNT: 3,
  27. NODE_COUNT: 4,
  28. NODE_INCOMPATIBILITY: 5,
  29. CONTOUR_ORDER: 6,
  30. WRONG_START_POINT: 7,
  31. KINK: 8,
  32. UNDERWEIGHT: 9,
  33. OVERWEIGHT: 10,
  34. NOTHING: 11,
  35. }
  36. def sort_problems(problems):
  37. """Sort problem groups by their most severe problem type."""
  38. return dict(
  39. sorted(
  40. problems.items(),
  41. key=lambda _: -min(
  42. (
  43. (InterpolatableProblem.severity[p["type"]] + p.get("tolerance", 0))
  44. for p in _[1]
  45. ),
  46. ),
  47. reverse=True,
  48. )
  49. )
  50. def rot_list(l, k):
  51. """Rotate list by k items forward. Ie. item at position 0 will be
  52. at position k in returned list. Negative k is allowed."""
  53. return l[-k:] + l[:-k]
  54. class PerContourPen(BasePen):
  55. def __init__(self, Pen, glyphset=None):
  56. BasePen.__init__(self, glyphset)
  57. self._Pen = Pen
  58. self._pen = None
  59. self.value = []
  60. def _moveTo(self, p0):
  61. self._newItem()
  62. self._pen.moveTo(p0)
  63. def _lineTo(self, p1):
  64. self._pen.lineTo(p1)
  65. def _qCurveToOne(self, p1, p2):
  66. self._pen.qCurveTo(p1, p2)
  67. def _curveToOne(self, p1, p2, p3):
  68. self._pen.curveTo(p1, p2, p3)
  69. def _closePath(self):
  70. self._pen.closePath()
  71. self._pen = None
  72. def _endPath(self):
  73. self._pen.endPath()
  74. self._pen = None
  75. def _newItem(self):
  76. self._pen = pen = self._Pen()
  77. self.value.append(pen)
  78. class PerContourOrComponentPen(PerContourPen):
  79. def addComponent(self, glyphName, transformation):
  80. self._newItem()
  81. self.value[-1].addComponent(glyphName, transformation)
  82. class SimpleRecordingPointPen(AbstractPointPen):
  83. def __init__(self):
  84. self.value = []
  85. def beginPath(self, identifier=None, **kwargs):
  86. pass
  87. def endPath(self) -> None:
  88. pass
  89. def addPoint(self, pt, segmentType=None):
  90. self.value.append((pt, False if segmentType is None else True))
  91. def vdiff_hypot2(v0, v1):
  92. s = 0
  93. for x0, x1 in zip(v0, v1):
  94. d = x1 - x0
  95. s += d * d
  96. return s
  97. def vdiff_hypot2_complex(v0, v1):
  98. s = 0
  99. for x0, x1 in zip(v0, v1):
  100. d = x1 - x0
  101. s += d.real * d.real + d.imag * d.imag
  102. # This does the same but seems to be slower:
  103. # s += (d * d.conjugate()).real
  104. return s
  105. def matching_cost(G, matching):
  106. return sum(G[i][j] for i, j in enumerate(matching))
  107. def min_cost_perfect_bipartite_matching_scipy(G):
  108. n = len(G)
  109. rows, cols = linear_sum_assignment(G)
  110. assert (rows == list(range(n))).all()
  111. # Convert numpy array and integer to Python types,
  112. # to ensure that this is JSON-serializable.
  113. cols = list(int(e) for e in cols)
  114. return list(cols), matching_cost(G, cols)
  115. def min_cost_perfect_bipartite_matching_munkres(G):
  116. n = len(G)
  117. cols = [None] * n
  118. for row, col in Munkres().compute(G):
  119. cols[row] = col
  120. return cols, matching_cost(G, cols)
  121. def min_cost_perfect_bipartite_matching_bruteforce(G):
  122. n = len(G)
  123. if n > 6:
  124. raise Exception("Install Python module 'munkres' or 'scipy >= 0.17.0'")
  125. # Otherwise just brute-force
  126. permutations = itertools.permutations(range(n))
  127. best = list(next(permutations))
  128. best_cost = matching_cost(G, best)
  129. for p in permutations:
  130. cost = matching_cost(G, p)
  131. if cost < best_cost:
  132. best, best_cost = list(p), cost
  133. return best, best_cost
  134. # Prefer `scipy.optimize.linear_sum_assignment` for performance.
  135. # `Munkres` is also supported as a fallback for minimalistic systems
  136. # where installing SciPy is not feasible.
  137. try:
  138. from scipy.optimize import linear_sum_assignment
  139. min_cost_perfect_bipartite_matching = min_cost_perfect_bipartite_matching_scipy
  140. except ImportError:
  141. try:
  142. from munkres import Munkres
  143. min_cost_perfect_bipartite_matching = (
  144. min_cost_perfect_bipartite_matching_munkres
  145. )
  146. except ImportError:
  147. min_cost_perfect_bipartite_matching = (
  148. min_cost_perfect_bipartite_matching_bruteforce
  149. )
  150. def contour_vector_from_stats(stats):
  151. # Don't change the order of items here.
  152. # It's okay to add to the end, but otherwise, other
  153. # code depends on it. Search for "covariance".
  154. size = sqrt(abs(stats.area))
  155. return (
  156. copysign((size), stats.area),
  157. stats.meanX,
  158. stats.meanY,
  159. stats.stddevX * 2,
  160. stats.stddevY * 2,
  161. stats.correlation * size,
  162. )
  163. def matching_for_vectors(m0, m1):
  164. n = len(m0)
  165. costs = [[vdiff_hypot2(v0, v1) for v1 in m1] for v0 in m0]
  166. (
  167. matching,
  168. matching_cost,
  169. ) = min_cost_perfect_bipartite_matching(costs)
  170. identity_cost = sum(costs[i][i] for i in range(n))
  171. return matching, matching_cost, identity_cost
  172. def points_characteristic_bits(points):
  173. bits = 0
  174. for pt, b in reversed(points):
  175. bits = (bits << 1) | b
  176. return bits
  177. _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR = 4
  178. def points_complex_vector(points):
  179. vector = []
  180. if not points:
  181. return vector
  182. points = [complex(*pt) for pt, _ in points]
  183. n = len(points)
  184. assert _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR == 4
  185. points.extend(points[: _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR - 1])
  186. while len(points) < _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR:
  187. points.extend(points[: _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR - 1])
  188. for i in range(n):
  189. # The weights are magic numbers.
  190. # The point itself
  191. p0 = points[i]
  192. vector.append(p0)
  193. # The vector to the next point
  194. p1 = points[i + 1]
  195. d0 = p1 - p0
  196. vector.append(d0 * 3)
  197. # The turn vector
  198. p2 = points[i + 2]
  199. d1 = p2 - p1
  200. vector.append(d1 - d0)
  201. # The angle to the next point, as a cross product;
  202. # Square root of, to match dimentionality of distance.
  203. cross = d0.real * d1.imag - d0.imag * d1.real
  204. cross = copysign(sqrt(abs(cross)), cross)
  205. vector.append(cross * 4)
  206. return vector
  207. def add_isomorphisms(points, isomorphisms, reverse):
  208. reference_bits = points_characteristic_bits(points)
  209. n = len(points)
  210. # if points[0][0] == points[-1][0]:
  211. # abort
  212. if reverse:
  213. points = points[::-1]
  214. bits = points_characteristic_bits(points)
  215. else:
  216. bits = reference_bits
  217. vector = points_complex_vector(points)
  218. assert len(vector) % n == 0
  219. mult = len(vector) // n
  220. mask = (1 << n) - 1
  221. for i in range(n):
  222. b = ((bits << (n - i)) & mask) | (bits >> i)
  223. if b == reference_bits:
  224. isomorphisms.append(
  225. (rot_list(vector, -i * mult), n - 1 - i if reverse else i, reverse)
  226. )
  227. def find_parents_and_order(glyphsets, locations, *, discrete_axes=set()):
  228. parents = [None] + list(range(len(glyphsets) - 1))
  229. order = list(range(len(glyphsets)))
  230. if locations:
  231. # Order base master first
  232. bases = [
  233. i
  234. for i, l in enumerate(locations)
  235. if all(v == 0 for k, v in l.items() if k not in discrete_axes)
  236. ]
  237. if bases:
  238. log.info("Found %s base masters: %s", len(bases), bases)
  239. else:
  240. log.warning("No base master location found")
  241. # Form a minimum spanning tree of the locations
  242. try:
  243. from scipy.sparse.csgraph import minimum_spanning_tree
  244. graph = [[0] * len(locations) for _ in range(len(locations))]
  245. axes = set()
  246. for l in locations:
  247. axes.update(l.keys())
  248. axes = sorted(axes)
  249. vectors = [tuple(l.get(k, 0) for k in axes) for l in locations]
  250. for i, j in itertools.combinations(range(len(locations)), 2):
  251. i_discrete_location = {
  252. k: v for k, v in zip(axes, vectors[i]) if k in discrete_axes
  253. }
  254. j_discrete_location = {
  255. k: v for k, v in zip(axes, vectors[j]) if k in discrete_axes
  256. }
  257. if i_discrete_location != j_discrete_location:
  258. continue
  259. graph[i][j] = vdiff_hypot2(vectors[i], vectors[j])
  260. tree = minimum_spanning_tree(graph, overwrite=True)
  261. rows, cols = tree.nonzero()
  262. graph = defaultdict(set)
  263. for row, col in zip(rows, cols):
  264. graph[row].add(col)
  265. graph[col].add(row)
  266. # Traverse graph from the base and assign parents
  267. parents = [None] * len(locations)
  268. order = []
  269. visited = set()
  270. queue = deque(bases)
  271. while queue:
  272. i = queue.popleft()
  273. visited.add(i)
  274. order.append(i)
  275. for j in sorted(graph[i]):
  276. if j not in visited:
  277. parents[j] = i
  278. queue.append(j)
  279. assert len(order) == len(
  280. parents
  281. ), "Not all masters are reachable; report an issue"
  282. except ImportError:
  283. pass
  284. log.info("Parents: %s", parents)
  285. log.info("Order: %s", order)
  286. return parents, order