interpolatableHelpers.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. from fontTools.pens.basePen import AbstractPen, BasePen, DecomposingPen
  2. from fontTools.pens.pointPen import AbstractPointPen, SegmentToPointPen
  3. from fontTools.pens.recordingPen import RecordingPen, DecomposingRecordingPen
  4. from fontTools.misc.transform import Transform
  5. from collections import defaultdict, deque
  6. from math import sqrt, copysign, atan2, pi
  7. import itertools
  8. import logging
  9. log = logging.getLogger("fontTools.varLib.interpolatable")
  10. def rot_list(l, k):
  11. """Rotate list by k items forward. Ie. item at position 0 will be
  12. at position k in returned list. Negative k is allowed."""
  13. return l[-k:] + l[:-k]
  14. class PerContourPen(BasePen):
  15. def __init__(self, Pen, glyphset=None):
  16. BasePen.__init__(self, glyphset)
  17. self._glyphset = glyphset
  18. self._Pen = Pen
  19. self._pen = None
  20. self.value = []
  21. def _moveTo(self, p0):
  22. self._newItem()
  23. self._pen.moveTo(p0)
  24. def _lineTo(self, p1):
  25. self._pen.lineTo(p1)
  26. def _qCurveToOne(self, p1, p2):
  27. self._pen.qCurveTo(p1, p2)
  28. def _curveToOne(self, p1, p2, p3):
  29. self._pen.curveTo(p1, p2, p3)
  30. def _closePath(self):
  31. self._pen.closePath()
  32. self._pen = None
  33. def _endPath(self):
  34. self._pen.endPath()
  35. self._pen = None
  36. def _newItem(self):
  37. self._pen = pen = self._Pen()
  38. self.value.append(pen)
  39. class PerContourOrComponentPen(PerContourPen):
  40. def addComponent(self, glyphName, transformation):
  41. self._newItem()
  42. self.value[-1].addComponent(glyphName, transformation)
  43. class SimpleRecordingPointPen(AbstractPointPen):
  44. def __init__(self):
  45. self.value = []
  46. def beginPath(self, identifier=None, **kwargs):
  47. pass
  48. def endPath(self) -> None:
  49. pass
  50. def addPoint(self, pt, segmentType=None):
  51. self.value.append((pt, False if segmentType is None else True))
  52. def vdiff_hypot2(v0, v1):
  53. s = 0
  54. for x0, x1 in zip(v0, v1):
  55. d = x1 - x0
  56. s += d * d
  57. return s
  58. def vdiff_hypot2_complex(v0, v1):
  59. s = 0
  60. for x0, x1 in zip(v0, v1):
  61. d = x1 - x0
  62. s += d.real * d.real + d.imag * d.imag
  63. # This does the same but seems to be slower:
  64. # s += (d * d.conjugate()).real
  65. return s
  66. def matching_cost(G, matching):
  67. return sum(G[i][j] for i, j in enumerate(matching))
  68. def min_cost_perfect_bipartite_matching_scipy(G):
  69. n = len(G)
  70. rows, cols = linear_sum_assignment(G)
  71. assert (rows == list(range(n))).all()
  72. return list(cols), matching_cost(G, cols)
  73. def min_cost_perfect_bipartite_matching_munkres(G):
  74. n = len(G)
  75. cols = [None] * n
  76. for row, col in Munkres().compute(G):
  77. cols[row] = col
  78. return cols, matching_cost(G, cols)
  79. def min_cost_perfect_bipartite_matching_bruteforce(G):
  80. n = len(G)
  81. if n > 6:
  82. raise Exception("Install Python module 'munkres' or 'scipy >= 0.17.0'")
  83. # Otherwise just brute-force
  84. permutations = itertools.permutations(range(n))
  85. best = list(next(permutations))
  86. best_cost = matching_cost(G, best)
  87. for p in permutations:
  88. cost = matching_cost(G, p)
  89. if cost < best_cost:
  90. best, best_cost = list(p), cost
  91. return best, best_cost
  92. try:
  93. from scipy.optimize import linear_sum_assignment
  94. min_cost_perfect_bipartite_matching = min_cost_perfect_bipartite_matching_scipy
  95. except ImportError:
  96. try:
  97. from munkres import Munkres
  98. min_cost_perfect_bipartite_matching = (
  99. min_cost_perfect_bipartite_matching_munkres
  100. )
  101. except ImportError:
  102. min_cost_perfect_bipartite_matching = (
  103. min_cost_perfect_bipartite_matching_bruteforce
  104. )
  105. def contour_vector_from_stats(stats):
  106. # Don't change the order of items here.
  107. # It's okay to add to the end, but otherwise, other
  108. # code depends on it. Search for "covariance".
  109. size = sqrt(abs(stats.area))
  110. return (
  111. copysign((size), stats.area),
  112. stats.meanX,
  113. stats.meanY,
  114. stats.stddevX * 2,
  115. stats.stddevY * 2,
  116. stats.correlation * size,
  117. )
  118. def matching_for_vectors(m0, m1):
  119. n = len(m0)
  120. identity_matching = list(range(n))
  121. costs = [[vdiff_hypot2(v0, v1) for v1 in m1] for v0 in m0]
  122. (
  123. matching,
  124. matching_cost,
  125. ) = min_cost_perfect_bipartite_matching(costs)
  126. identity_cost = sum(costs[i][i] for i in range(n))
  127. return matching, matching_cost, identity_cost
  128. def points_characteristic_bits(points):
  129. bits = 0
  130. for pt, b in reversed(points):
  131. bits = (bits << 1) | b
  132. return bits
  133. _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR = 4
  134. def points_complex_vector(points):
  135. vector = []
  136. if not points:
  137. return vector
  138. points = [complex(*pt) for pt, _ in points]
  139. n = len(points)
  140. assert _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR == 4
  141. points.extend(points[: _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR - 1])
  142. while len(points) < _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR:
  143. points.extend(points[: _NUM_ITEMS_PER_POINTS_COMPLEX_VECTOR - 1])
  144. for i in range(n):
  145. # The weights are magic numbers.
  146. # The point itself
  147. p0 = points[i]
  148. vector.append(p0)
  149. # The vector to the next point
  150. p1 = points[i + 1]
  151. d0 = p1 - p0
  152. vector.append(d0 * 3)
  153. # The turn vector
  154. p2 = points[i + 2]
  155. d1 = p2 - p1
  156. vector.append(d1 - d0)
  157. # The angle to the next point, as a cross product;
  158. # Square root of, to match dimentionality of distance.
  159. cross = d0.real * d1.imag - d0.imag * d1.real
  160. cross = copysign(sqrt(abs(cross)), cross)
  161. vector.append(cross * 4)
  162. return vector
  163. def add_isomorphisms(points, isomorphisms, reverse):
  164. reference_bits = points_characteristic_bits(points)
  165. n = len(points)
  166. # if points[0][0] == points[-1][0]:
  167. # abort
  168. if reverse:
  169. points = points[::-1]
  170. bits = points_characteristic_bits(points)
  171. else:
  172. bits = reference_bits
  173. vector = points_complex_vector(points)
  174. assert len(vector) % n == 0
  175. mult = len(vector) // n
  176. mask = (1 << n) - 1
  177. for i in range(n):
  178. b = ((bits << (n - i)) & mask) | (bits >> i)
  179. if b == reference_bits:
  180. isomorphisms.append(
  181. (rot_list(vector, -i * mult), n - 1 - i if reverse else i, reverse)
  182. )
  183. def find_parents_and_order(glyphsets, locations):
  184. parents = [None] + list(range(len(glyphsets) - 1))
  185. order = list(range(len(glyphsets)))
  186. if locations:
  187. # Order base master first
  188. bases = (i for i, l in enumerate(locations) if all(v == 0 for v in l.values()))
  189. if bases:
  190. base = next(bases)
  191. logging.info("Base master index %s, location %s", base, locations[base])
  192. else:
  193. base = 0
  194. logging.warning("No base master location found")
  195. # Form a minimum spanning tree of the locations
  196. try:
  197. from scipy.sparse.csgraph import minimum_spanning_tree
  198. graph = [[0] * len(locations) for _ in range(len(locations))]
  199. axes = set()
  200. for l in locations:
  201. axes.update(l.keys())
  202. axes = sorted(axes)
  203. vectors = [tuple(l.get(k, 0) for k in axes) for l in locations]
  204. for i, j in itertools.combinations(range(len(locations)), 2):
  205. graph[i][j] = vdiff_hypot2(vectors[i], vectors[j])
  206. tree = minimum_spanning_tree(graph)
  207. rows, cols = tree.nonzero()
  208. graph = defaultdict(set)
  209. for row, col in zip(rows, cols):
  210. graph[row].add(col)
  211. graph[col].add(row)
  212. # Traverse graph from the base and assign parents
  213. parents = [None] * len(locations)
  214. order = []
  215. visited = set()
  216. queue = deque([base])
  217. while queue:
  218. i = queue.popleft()
  219. visited.add(i)
  220. order.append(i)
  221. for j in sorted(graph[i]):
  222. if j not in visited:
  223. parents[j] = i
  224. queue.append(j)
  225. except ImportError:
  226. pass
  227. log.info("Parents: %s", parents)
  228. log.info("Order: %s", order)
  229. return parents, order
  230. def transform_from_stats(stats, inverse=False):
  231. # https://cookierobotics.com/007/
  232. a = stats.varianceX
  233. b = stats.covariance
  234. c = stats.varianceY
  235. delta = (((a - c) * 0.5) ** 2 + b * b) ** 0.5
  236. lambda1 = (a + c) * 0.5 + delta # Major eigenvalue
  237. lambda2 = (a + c) * 0.5 - delta # Minor eigenvalue
  238. theta = atan2(lambda1 - a, b) if b != 0 else (pi * 0.5 if a < c else 0)
  239. trans = Transform()
  240. if lambda2 < 0:
  241. # XXX This is a hack.
  242. # The problem is that the covariance matrix is singular.
  243. # This happens when the contour is a line, or a circle.
  244. # In that case, the covariance matrix is not a good
  245. # representation of the contour.
  246. # We should probably detect this earlier and avoid
  247. # computing the covariance matrix in the first place.
  248. # But for now, we just avoid the division by zero.
  249. lambda2 = 0
  250. if inverse:
  251. trans = trans.translate(-stats.meanX, -stats.meanY)
  252. trans = trans.rotate(-theta)
  253. trans = trans.scale(1 / sqrt(lambda1), 1 / sqrt(lambda2))
  254. else:
  255. trans = trans.scale(sqrt(lambda1), sqrt(lambda2))
  256. trans = trans.rotate(theta)
  257. trans = trans.translate(stats.meanX, stats.meanY)
  258. return trans
  259. class LerpGlyphSet:
  260. def __init__(self, glyphset1, glyphset2, factor=0.5):
  261. self.glyphset1 = glyphset1
  262. self.glyphset2 = glyphset2
  263. self.factor = factor
  264. def __getitem__(self, glyphname):
  265. return LerpGlyph(glyphname, self)
  266. class LerpGlyph:
  267. def __init__(self, glyphname, glyphset):
  268. self.glyphset = glyphset
  269. self.glyphname = glyphname
  270. def draw(self, pen):
  271. recording1 = DecomposingRecordingPen(self.glyphset.glyphset1)
  272. self.glyphset.glyphset1[self.glyphname].draw(recording1)
  273. recording2 = DecomposingRecordingPen(self.glyphset.glyphset2)
  274. self.glyphset.glyphset2[self.glyphname].draw(recording2)
  275. factor = self.glyphset.factor
  276. for (op1, args1), (op2, args2) in zip(recording1.value, recording2.value):
  277. if op1 != op2:
  278. raise ValueError("Mismatching operations: %s, %s" % (op1, op2))
  279. mid_args = [
  280. (x1 + (x2 - x1) * factor, y1 + (y2 - y1) * factor)
  281. for (x1, y1), (x2, y2) in zip(args1, args2)
  282. ]
  283. getattr(pen, op1)(*mid_args)
  284. def lerp_recordings(recording1, recording2, factor=0.5):
  285. pen = RecordingPen()
  286. value = pen.value
  287. for (op1, args1), (op2, args2) in zip(recording1.value, recording2.value):
  288. if op1 != op2:
  289. raise ValueError("Mismatched operations: %s, %s" % (op1, op2))
  290. if op1 == "addComponent":
  291. mid_args = args1 # XXX Interpolate transformation?
  292. else:
  293. mid_args = [
  294. (x1 + (x2 - x1) * factor, y1 + (y2 - y1) * factor)
  295. for (x1, y1), (x2, y2) in zip(args1, args2)
  296. ]
  297. value.append((op1, mid_args))
  298. return pen