TupleVariation.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. from fontTools.misc.fixedTools import (
  2. fixedToFloat as fi2fl,
  3. floatToFixed as fl2fi,
  4. floatToFixedToStr as fl2str,
  5. strToFixedToFloat as str2fl,
  6. otRound,
  7. )
  8. from fontTools.misc.textTools import safeEval
  9. import array
  10. from collections import Counter, defaultdict
  11. import io
  12. import logging
  13. import struct
  14. import sys
  15. # https://www.microsoft.com/typography/otspec/otvarcommonformats.htm
  16. EMBEDDED_PEAK_TUPLE = 0x8000
  17. INTERMEDIATE_REGION = 0x4000
  18. PRIVATE_POINT_NUMBERS = 0x2000
  19. DELTAS_ARE_ZERO = 0x80
  20. DELTAS_ARE_WORDS = 0x40
  21. DELTAS_ARE_LONGS = 0xC0
  22. DELTAS_SIZE_MASK = 0xC0
  23. DELTA_RUN_COUNT_MASK = 0x3F
  24. POINTS_ARE_WORDS = 0x80
  25. POINT_RUN_COUNT_MASK = 0x7F
  26. TUPLES_SHARE_POINT_NUMBERS = 0x8000
  27. TUPLE_COUNT_MASK = 0x0FFF
  28. TUPLE_INDEX_MASK = 0x0FFF
  29. log = logging.getLogger(__name__)
  30. class TupleVariation(object):
  31. def __init__(self, axes, coordinates):
  32. self.axes = axes.copy()
  33. self.coordinates = list(coordinates)
  34. def __repr__(self):
  35. axes = ",".join(
  36. sorted(["%s=%s" % (name, value) for (name, value) in self.axes.items()])
  37. )
  38. return "<TupleVariation %s %s>" % (axes, self.coordinates)
  39. def __eq__(self, other):
  40. return self.coordinates == other.coordinates and self.axes == other.axes
  41. def getUsedPoints(self):
  42. # Empty set means "all points used".
  43. if None not in self.coordinates:
  44. return frozenset()
  45. used = frozenset([i for i, p in enumerate(self.coordinates) if p is not None])
  46. # Return None if no points used.
  47. return used if used else None
  48. def hasImpact(self):
  49. """Returns True if this TupleVariation has any visible impact.
  50. If the result is False, the TupleVariation can be omitted from the font
  51. without making any visible difference.
  52. """
  53. return any(c is not None for c in self.coordinates)
  54. def toXML(self, writer, axisTags):
  55. writer.begintag("tuple")
  56. writer.newline()
  57. for axis in axisTags:
  58. value = self.axes.get(axis)
  59. if value is not None:
  60. minValue, value, maxValue = value
  61. defaultMinValue = min(value, 0.0) # -0.3 --> -0.3; 0.7 --> 0.0
  62. defaultMaxValue = max(value, 0.0) # -0.3 --> 0.0; 0.7 --> 0.7
  63. if minValue == defaultMinValue and maxValue == defaultMaxValue:
  64. writer.simpletag("coord", axis=axis, value=fl2str(value, 14))
  65. else:
  66. attrs = [
  67. ("axis", axis),
  68. ("min", fl2str(minValue, 14)),
  69. ("value", fl2str(value, 14)),
  70. ("max", fl2str(maxValue, 14)),
  71. ]
  72. writer.simpletag("coord", attrs)
  73. writer.newline()
  74. wrote_any_deltas = False
  75. for i, delta in enumerate(self.coordinates):
  76. if type(delta) == tuple and len(delta) == 2:
  77. writer.simpletag("delta", pt=i, x=delta[0], y=delta[1])
  78. writer.newline()
  79. wrote_any_deltas = True
  80. elif type(delta) == int:
  81. writer.simpletag("delta", cvt=i, value=delta)
  82. writer.newline()
  83. wrote_any_deltas = True
  84. elif delta is not None:
  85. log.error("bad delta format")
  86. writer.comment("bad delta #%d" % i)
  87. writer.newline()
  88. wrote_any_deltas = True
  89. if not wrote_any_deltas:
  90. writer.comment("no deltas")
  91. writer.newline()
  92. writer.endtag("tuple")
  93. writer.newline()
  94. def fromXML(self, name, attrs, _content):
  95. if name == "coord":
  96. axis = attrs["axis"]
  97. value = str2fl(attrs["value"], 14)
  98. defaultMinValue = min(value, 0.0) # -0.3 --> -0.3; 0.7 --> 0.0
  99. defaultMaxValue = max(value, 0.0) # -0.3 --> 0.0; 0.7 --> 0.7
  100. minValue = str2fl(attrs.get("min", defaultMinValue), 14)
  101. maxValue = str2fl(attrs.get("max", defaultMaxValue), 14)
  102. self.axes[axis] = (minValue, value, maxValue)
  103. elif name == "delta":
  104. if "pt" in attrs:
  105. point = safeEval(attrs["pt"])
  106. x = safeEval(attrs["x"])
  107. y = safeEval(attrs["y"])
  108. self.coordinates[point] = (x, y)
  109. elif "cvt" in attrs:
  110. cvt = safeEval(attrs["cvt"])
  111. value = safeEval(attrs["value"])
  112. self.coordinates[cvt] = value
  113. else:
  114. log.warning("bad delta format: %s" % ", ".join(sorted(attrs.keys())))
  115. def compile(self, axisTags, sharedCoordIndices={}, pointData=None):
  116. assert set(self.axes.keys()) <= set(axisTags), (
  117. "Unknown axis tag found.",
  118. self.axes.keys(),
  119. axisTags,
  120. )
  121. tupleData = []
  122. auxData = []
  123. if pointData is None:
  124. usedPoints = self.getUsedPoints()
  125. if usedPoints is None: # Nothing to encode
  126. return b"", b""
  127. pointData = self.compilePoints(usedPoints)
  128. coord = self.compileCoord(axisTags)
  129. flags = sharedCoordIndices.get(coord)
  130. if flags is None:
  131. flags = EMBEDDED_PEAK_TUPLE
  132. tupleData.append(coord)
  133. intermediateCoord = self.compileIntermediateCoord(axisTags)
  134. if intermediateCoord is not None:
  135. flags |= INTERMEDIATE_REGION
  136. tupleData.append(intermediateCoord)
  137. # pointData of b'' implies "use shared points".
  138. if pointData:
  139. flags |= PRIVATE_POINT_NUMBERS
  140. auxData.append(pointData)
  141. auxData.append(self.compileDeltas())
  142. auxData = b"".join(auxData)
  143. tupleData.insert(0, struct.pack(">HH", len(auxData), flags))
  144. return b"".join(tupleData), auxData
  145. def compileCoord(self, axisTags):
  146. result = []
  147. axes = self.axes
  148. for axis in axisTags:
  149. triple = axes.get(axis)
  150. if triple is None:
  151. result.append(b"\0\0")
  152. else:
  153. result.append(struct.pack(">h", fl2fi(triple[1], 14)))
  154. return b"".join(result)
  155. def compileIntermediateCoord(self, axisTags):
  156. needed = False
  157. for axis in axisTags:
  158. minValue, value, maxValue = self.axes.get(axis, (0.0, 0.0, 0.0))
  159. defaultMinValue = min(value, 0.0) # -0.3 --> -0.3; 0.7 --> 0.0
  160. defaultMaxValue = max(value, 0.0) # -0.3 --> 0.0; 0.7 --> 0.7
  161. if (minValue != defaultMinValue) or (maxValue != defaultMaxValue):
  162. needed = True
  163. break
  164. if not needed:
  165. return None
  166. minCoords = []
  167. maxCoords = []
  168. for axis in axisTags:
  169. minValue, value, maxValue = self.axes.get(axis, (0.0, 0.0, 0.0))
  170. minCoords.append(struct.pack(">h", fl2fi(minValue, 14)))
  171. maxCoords.append(struct.pack(">h", fl2fi(maxValue, 14)))
  172. return b"".join(minCoords + maxCoords)
  173. @staticmethod
  174. def decompileCoord_(axisTags, data, offset):
  175. coord = {}
  176. pos = offset
  177. for axis in axisTags:
  178. coord[axis] = fi2fl(struct.unpack(">h", data[pos : pos + 2])[0], 14)
  179. pos += 2
  180. return coord, pos
  181. @staticmethod
  182. def compilePoints(points):
  183. # If the set consists of all points in the glyph, it gets encoded with
  184. # a special encoding: a single zero byte.
  185. #
  186. # To use this optimization, points passed in must be empty set.
  187. # The following two lines are not strictly necessary as the main code
  188. # below would emit the same. But this is most common and faster.
  189. if not points:
  190. return b"\0"
  191. # In the 'gvar' table, the packing of point numbers is a little surprising.
  192. # It consists of multiple runs, each being a delta-encoded list of integers.
  193. # For example, the point set {17, 18, 19, 20, 21, 22, 23} gets encoded as
  194. # [6, 17, 1, 1, 1, 1, 1, 1]. The first value (6) is the run length minus 1.
  195. # There are two types of runs, with values being either 8 or 16 bit unsigned
  196. # integers.
  197. points = list(points)
  198. points.sort()
  199. numPoints = len(points)
  200. result = bytearray()
  201. # The binary representation starts with the total number of points in the set,
  202. # encoded into one or two bytes depending on the value.
  203. if numPoints < 0x80:
  204. result.append(numPoints)
  205. else:
  206. result.append((numPoints >> 8) | 0x80)
  207. result.append(numPoints & 0xFF)
  208. MAX_RUN_LENGTH = 127
  209. pos = 0
  210. lastValue = 0
  211. while pos < numPoints:
  212. runLength = 0
  213. headerPos = len(result)
  214. result.append(0)
  215. useByteEncoding = None
  216. while pos < numPoints and runLength <= MAX_RUN_LENGTH:
  217. curValue = points[pos]
  218. delta = curValue - lastValue
  219. if useByteEncoding is None:
  220. useByteEncoding = 0 <= delta <= 0xFF
  221. if useByteEncoding and (delta > 0xFF or delta < 0):
  222. # we need to start a new run (which will not use byte encoding)
  223. break
  224. # TODO This never switches back to a byte-encoding from a short-encoding.
  225. # That's suboptimal.
  226. if useByteEncoding:
  227. result.append(delta)
  228. else:
  229. result.append(delta >> 8)
  230. result.append(delta & 0xFF)
  231. lastValue = curValue
  232. pos += 1
  233. runLength += 1
  234. if useByteEncoding:
  235. result[headerPos] = runLength - 1
  236. else:
  237. result[headerPos] = (runLength - 1) | POINTS_ARE_WORDS
  238. return result
  239. @staticmethod
  240. def decompilePoints_(numPoints, data, offset, tableTag):
  241. """(numPoints, data, offset, tableTag) --> ([point1, point2, ...], newOffset)"""
  242. assert tableTag in ("cvar", "gvar")
  243. pos = offset
  244. numPointsInData = data[pos]
  245. pos += 1
  246. if (numPointsInData & POINTS_ARE_WORDS) != 0:
  247. numPointsInData = (numPointsInData & POINT_RUN_COUNT_MASK) << 8 | data[pos]
  248. pos += 1
  249. if numPointsInData == 0:
  250. return (range(numPoints), pos)
  251. result = []
  252. while len(result) < numPointsInData:
  253. runHeader = data[pos]
  254. pos += 1
  255. numPointsInRun = (runHeader & POINT_RUN_COUNT_MASK) + 1
  256. point = 0
  257. if (runHeader & POINTS_ARE_WORDS) != 0:
  258. points = array.array("H")
  259. pointsSize = numPointsInRun * 2
  260. else:
  261. points = array.array("B")
  262. pointsSize = numPointsInRun
  263. points.frombytes(data[pos : pos + pointsSize])
  264. if sys.byteorder != "big":
  265. points.byteswap()
  266. assert len(points) == numPointsInRun
  267. pos += pointsSize
  268. result.extend(points)
  269. # Convert relative to absolute
  270. absolute = []
  271. current = 0
  272. for delta in result:
  273. current += delta
  274. absolute.append(current)
  275. result = absolute
  276. del absolute
  277. badPoints = {str(p) for p in result if p < 0 or p >= numPoints}
  278. if badPoints:
  279. log.warning(
  280. "point %s out of range in '%s' table"
  281. % (",".join(sorted(badPoints)), tableTag)
  282. )
  283. return (result, pos)
  284. def compileDeltas(self):
  285. deltaX = []
  286. deltaY = []
  287. if self.getCoordWidth() == 2:
  288. for c in self.coordinates:
  289. if c is None:
  290. continue
  291. deltaX.append(c[0])
  292. deltaY.append(c[1])
  293. else:
  294. for c in self.coordinates:
  295. if c is None:
  296. continue
  297. deltaX.append(c)
  298. bytearr = bytearray()
  299. self.compileDeltaValues_(deltaX, bytearr)
  300. self.compileDeltaValues_(deltaY, bytearr)
  301. return bytearr
  302. @staticmethod
  303. def compileDeltaValues_(deltas, bytearr=None):
  304. """[value1, value2, value3, ...] --> bytearray
  305. Emits a sequence of runs. Each run starts with a
  306. byte-sized header whose 6 least significant bits
  307. (header & 0x3F) indicate how many values are encoded
  308. in this run. The stored length is the actual length
  309. minus one; run lengths are thus in the range [1..64].
  310. If the header byte has its most significant bit (0x80)
  311. set, all values in this run are zero, and no data
  312. follows. Otherwise, the header byte is followed by
  313. ((header & 0x3F) + 1) signed values. If (header &
  314. 0x40) is clear, the delta values are stored as signed
  315. bytes; if (header & 0x40) is set, the delta values are
  316. signed 16-bit integers.
  317. """ # Explaining the format because the 'gvar' spec is hard to understand.
  318. if bytearr is None:
  319. bytearr = bytearray()
  320. pos = 0
  321. numDeltas = len(deltas)
  322. while pos < numDeltas:
  323. value = deltas[pos]
  324. if value == 0:
  325. pos = TupleVariation.encodeDeltaRunAsZeroes_(deltas, pos, bytearr)
  326. elif -128 <= value <= 127:
  327. pos = TupleVariation.encodeDeltaRunAsBytes_(deltas, pos, bytearr)
  328. elif -32768 <= value <= 32767:
  329. pos = TupleVariation.encodeDeltaRunAsWords_(deltas, pos, bytearr)
  330. else:
  331. pos = TupleVariation.encodeDeltaRunAsLongs_(deltas, pos, bytearr)
  332. return bytearr
  333. @staticmethod
  334. def encodeDeltaRunAsZeroes_(deltas, offset, bytearr):
  335. pos = offset
  336. numDeltas = len(deltas)
  337. while pos < numDeltas and deltas[pos] == 0:
  338. pos += 1
  339. runLength = pos - offset
  340. while runLength >= 64:
  341. bytearr.append(DELTAS_ARE_ZERO | 63)
  342. runLength -= 64
  343. if runLength:
  344. bytearr.append(DELTAS_ARE_ZERO | (runLength - 1))
  345. return pos
  346. @staticmethod
  347. def encodeDeltaRunAsBytes_(deltas, offset, bytearr):
  348. pos = offset
  349. numDeltas = len(deltas)
  350. while pos < numDeltas:
  351. value = deltas[pos]
  352. if not (-128 <= value <= 127):
  353. break
  354. # Within a byte-encoded run of deltas, a single zero
  355. # is best stored literally as 0x00 value. However,
  356. # if are two or more zeroes in a sequence, it is
  357. # better to start a new run. For example, the sequence
  358. # of deltas [15, 15, 0, 15, 15] becomes 6 bytes
  359. # (04 0F 0F 00 0F 0F) when storing the zero value
  360. # literally, but 7 bytes (01 0F 0F 80 01 0F 0F)
  361. # when starting a new run.
  362. if value == 0 and pos + 1 < numDeltas and deltas[pos + 1] == 0:
  363. break
  364. pos += 1
  365. runLength = pos - offset
  366. while runLength >= 64:
  367. bytearr.append(63)
  368. bytearr.extend(array.array("b", deltas[offset : offset + 64]))
  369. offset += 64
  370. runLength -= 64
  371. if runLength:
  372. bytearr.append(runLength - 1)
  373. bytearr.extend(array.array("b", deltas[offset:pos]))
  374. return pos
  375. @staticmethod
  376. def encodeDeltaRunAsWords_(deltas, offset, bytearr):
  377. pos = offset
  378. numDeltas = len(deltas)
  379. while pos < numDeltas:
  380. value = deltas[pos]
  381. # Within a word-encoded run of deltas, it is easiest
  382. # to start a new run (with a different encoding)
  383. # whenever we encounter a zero value. For example,
  384. # the sequence [0x6666, 0, 0x7777] needs 7 bytes when
  385. # storing the zero literally (42 66 66 00 00 77 77),
  386. # and equally 7 bytes when starting a new run
  387. # (40 66 66 80 40 77 77).
  388. if value == 0:
  389. break
  390. # Within a word-encoded run of deltas, a single value
  391. # in the range (-128..127) should be encoded literally
  392. # because it is more compact. For example, the sequence
  393. # [0x6666, 2, 0x7777] becomes 7 bytes when storing
  394. # the value literally (42 66 66 00 02 77 77), but 8 bytes
  395. # when starting a new run (40 66 66 00 02 40 77 77).
  396. if (
  397. (-128 <= value <= 127)
  398. and pos + 1 < numDeltas
  399. and (-128 <= deltas[pos + 1] <= 127)
  400. ):
  401. break
  402. if not (-32768 <= value <= 32767):
  403. break
  404. pos += 1
  405. runLength = pos - offset
  406. while runLength >= 64:
  407. bytearr.append(DELTAS_ARE_WORDS | 63)
  408. a = array.array("h", deltas[offset : offset + 64])
  409. if sys.byteorder != "big":
  410. a.byteswap()
  411. bytearr.extend(a)
  412. offset += 64
  413. runLength -= 64
  414. if runLength:
  415. bytearr.append(DELTAS_ARE_WORDS | (runLength - 1))
  416. a = array.array("h", deltas[offset:pos])
  417. if sys.byteorder != "big":
  418. a.byteswap()
  419. bytearr.extend(a)
  420. return pos
  421. @staticmethod
  422. def encodeDeltaRunAsLongs_(deltas, offset, bytearr):
  423. pos = offset
  424. numDeltas = len(deltas)
  425. while pos < numDeltas:
  426. value = deltas[pos]
  427. if -32768 <= value <= 32767:
  428. break
  429. pos += 1
  430. runLength = pos - offset
  431. while runLength >= 64:
  432. bytearr.append(DELTAS_ARE_LONGS | 63)
  433. a = array.array("i", deltas[offset : offset + 64])
  434. if sys.byteorder != "big":
  435. a.byteswap()
  436. bytearr.extend(a)
  437. offset += 64
  438. runLength -= 64
  439. if runLength:
  440. bytearr.append(DELTAS_ARE_LONGS | (runLength - 1))
  441. a = array.array("i", deltas[offset:pos])
  442. if sys.byteorder != "big":
  443. a.byteswap()
  444. bytearr.extend(a)
  445. return pos
  446. @staticmethod
  447. def decompileDeltas_(numDeltas, data, offset=0):
  448. """(numDeltas, data, offset) --> ([delta, delta, ...], newOffset)"""
  449. result = []
  450. pos = offset
  451. while len(result) < numDeltas if numDeltas is not None else pos < len(data):
  452. runHeader = data[pos]
  453. pos += 1
  454. numDeltasInRun = (runHeader & DELTA_RUN_COUNT_MASK) + 1
  455. if (runHeader & DELTAS_SIZE_MASK) == DELTAS_ARE_ZERO:
  456. result.extend([0] * numDeltasInRun)
  457. else:
  458. if (runHeader & DELTAS_SIZE_MASK) == DELTAS_ARE_LONGS:
  459. deltas = array.array("i")
  460. deltasSize = numDeltasInRun * 4
  461. elif (runHeader & DELTAS_SIZE_MASK) == DELTAS_ARE_WORDS:
  462. deltas = array.array("h")
  463. deltasSize = numDeltasInRun * 2
  464. else:
  465. deltas = array.array("b")
  466. deltasSize = numDeltasInRun
  467. deltas.frombytes(data[pos : pos + deltasSize])
  468. if sys.byteorder != "big":
  469. deltas.byteswap()
  470. assert len(deltas) == numDeltasInRun, (len(deltas), numDeltasInRun)
  471. pos += deltasSize
  472. result.extend(deltas)
  473. assert numDeltas is None or len(result) == numDeltas
  474. return (result, pos)
  475. @staticmethod
  476. def getTupleSize_(flags, axisCount):
  477. size = 4
  478. if (flags & EMBEDDED_PEAK_TUPLE) != 0:
  479. size += axisCount * 2
  480. if (flags & INTERMEDIATE_REGION) != 0:
  481. size += axisCount * 4
  482. return size
  483. def getCoordWidth(self):
  484. """Return 2 if coordinates are (x, y) as in gvar, 1 if single values
  485. as in cvar, or 0 if empty.
  486. """
  487. firstDelta = next((c for c in self.coordinates if c is not None), None)
  488. if firstDelta is None:
  489. return 0 # empty or has no impact
  490. if type(firstDelta) in (int, float):
  491. return 1
  492. if type(firstDelta) is tuple and len(firstDelta) == 2:
  493. return 2
  494. raise TypeError(
  495. "invalid type of delta; expected (int or float) number, or "
  496. "Tuple[number, number]: %r" % firstDelta
  497. )
  498. def scaleDeltas(self, scalar):
  499. if scalar == 1.0:
  500. return # no change
  501. coordWidth = self.getCoordWidth()
  502. self.coordinates = [
  503. (
  504. None
  505. if d is None
  506. else d * scalar if coordWidth == 1 else (d[0] * scalar, d[1] * scalar)
  507. )
  508. for d in self.coordinates
  509. ]
  510. def roundDeltas(self):
  511. coordWidth = self.getCoordWidth()
  512. self.coordinates = [
  513. (
  514. None
  515. if d is None
  516. else otRound(d) if coordWidth == 1 else (otRound(d[0]), otRound(d[1]))
  517. )
  518. for d in self.coordinates
  519. ]
  520. def calcInferredDeltas(self, origCoords, endPts):
  521. from fontTools.varLib.iup import iup_delta
  522. if self.getCoordWidth() == 1:
  523. raise TypeError("Only 'gvar' TupleVariation can have inferred deltas")
  524. if None in self.coordinates:
  525. if len(self.coordinates) != len(origCoords):
  526. raise ValueError(
  527. "Expected len(origCoords) == %d; found %d"
  528. % (len(self.coordinates), len(origCoords))
  529. )
  530. self.coordinates = iup_delta(self.coordinates, origCoords, endPts)
  531. def optimize(self, origCoords, endPts, tolerance=0.5, isComposite=False):
  532. from fontTools.varLib.iup import iup_delta_optimize
  533. if None in self.coordinates:
  534. return # already optimized
  535. deltaOpt = iup_delta_optimize(
  536. self.coordinates, origCoords, endPts, tolerance=tolerance
  537. )
  538. if None in deltaOpt:
  539. if isComposite and all(d is None for d in deltaOpt):
  540. # Fix for macOS composites
  541. # https://github.com/fonttools/fonttools/issues/1381
  542. deltaOpt = [(0, 0)] + [None] * (len(deltaOpt) - 1)
  543. # Use "optimized" version only if smaller...
  544. varOpt = TupleVariation(self.axes, deltaOpt)
  545. # Shouldn't matter that this is different from fvar...?
  546. axisTags = sorted(self.axes.keys())
  547. tupleData, auxData = self.compile(axisTags)
  548. unoptimizedLength = len(tupleData) + len(auxData)
  549. tupleData, auxData = varOpt.compile(axisTags)
  550. optimizedLength = len(tupleData) + len(auxData)
  551. if optimizedLength < unoptimizedLength:
  552. self.coordinates = varOpt.coordinates
  553. def __imul__(self, scalar):
  554. self.scaleDeltas(scalar)
  555. return self
  556. def __iadd__(self, other):
  557. if not isinstance(other, TupleVariation):
  558. return NotImplemented
  559. deltas1 = self.coordinates
  560. length = len(deltas1)
  561. deltas2 = other.coordinates
  562. if len(deltas2) != length:
  563. raise ValueError("cannot sum TupleVariation deltas with different lengths")
  564. # 'None' values have different meanings in gvar vs cvar TupleVariations:
  565. # within the gvar, when deltas are not provided explicitly for some points,
  566. # they need to be inferred; whereas for the 'cvar' table, if deltas are not
  567. # provided for some CVT values, then no adjustments are made (i.e. None == 0).
  568. # Thus, we cannot sum deltas for gvar TupleVariations if they contain
  569. # inferred inferred deltas (the latter need to be computed first using
  570. # 'calcInferredDeltas' method), but we can treat 'None' values in cvar
  571. # deltas as if they are zeros.
  572. if self.getCoordWidth() == 2:
  573. for i, d2 in zip(range(length), deltas2):
  574. d1 = deltas1[i]
  575. try:
  576. deltas1[i] = (d1[0] + d2[0], d1[1] + d2[1])
  577. except TypeError:
  578. raise ValueError("cannot sum gvar deltas with inferred points")
  579. else:
  580. for i, d2 in zip(range(length), deltas2):
  581. d1 = deltas1[i]
  582. if d1 is not None and d2 is not None:
  583. deltas1[i] = d1 + d2
  584. elif d1 is None and d2 is not None:
  585. deltas1[i] = d2
  586. # elif d2 is None do nothing
  587. return self
  588. def decompileSharedTuples(axisTags, sharedTupleCount, data, offset):
  589. result = []
  590. for _ in range(sharedTupleCount):
  591. t, offset = TupleVariation.decompileCoord_(axisTags, data, offset)
  592. result.append(t)
  593. return result
  594. def compileSharedTuples(
  595. axisTags, variations, MAX_NUM_SHARED_COORDS=TUPLE_INDEX_MASK + 1
  596. ):
  597. coordCount = Counter()
  598. for var in variations:
  599. coord = var.compileCoord(axisTags)
  600. coordCount[coord] += 1
  601. # In python < 3.7, most_common() ordering is non-deterministic
  602. # so apply a sort to make sure the ordering is consistent.
  603. sharedCoords = sorted(
  604. coordCount.most_common(MAX_NUM_SHARED_COORDS),
  605. key=lambda item: (-item[1], item[0]),
  606. )
  607. return [c[0] for c in sharedCoords if c[1] > 1]
  608. def compileTupleVariationStore(
  609. variations, pointCount, axisTags, sharedTupleIndices, useSharedPoints=True
  610. ):
  611. # pointCount is actually unused. Keeping for API compat.
  612. del pointCount
  613. newVariations = []
  614. pointDatas = []
  615. # Compile all points and figure out sharing if desired
  616. sharedPoints = None
  617. # Collect, count, and compile point-sets for all variation sets
  618. pointSetCount = defaultdict(int)
  619. for v in variations:
  620. points = v.getUsedPoints()
  621. if points is None: # Empty variations
  622. continue
  623. pointSetCount[points] += 1
  624. newVariations.append(v)
  625. pointDatas.append(points)
  626. variations = newVariations
  627. del newVariations
  628. if not variations:
  629. return (0, b"", b"")
  630. n = len(variations[0].coordinates)
  631. assert all(
  632. len(v.coordinates) == n for v in variations
  633. ), "Variation sets have different sizes"
  634. compiledPoints = {
  635. pointSet: TupleVariation.compilePoints(pointSet) for pointSet in pointSetCount
  636. }
  637. tupleVariationCount = len(variations)
  638. tuples = []
  639. data = []
  640. if useSharedPoints:
  641. # Find point-set which saves most bytes.
  642. def key(pn):
  643. pointSet = pn[0]
  644. count = pn[1]
  645. return len(compiledPoints[pointSet]) * (count - 1)
  646. sharedPoints = max(pointSetCount.items(), key=key)[0]
  647. data.append(compiledPoints[sharedPoints])
  648. tupleVariationCount |= TUPLES_SHARE_POINT_NUMBERS
  649. # b'' implies "use shared points"
  650. pointDatas = [
  651. compiledPoints[points] if points != sharedPoints else b""
  652. for points in pointDatas
  653. ]
  654. for v, p in zip(variations, pointDatas):
  655. thisTuple, thisData = v.compile(axisTags, sharedTupleIndices, pointData=p)
  656. tuples.append(thisTuple)
  657. data.append(thisData)
  658. tuples = b"".join(tuples)
  659. data = b"".join(data)
  660. return tupleVariationCount, tuples, data
  661. def decompileTupleVariationStore(
  662. tableTag,
  663. axisTags,
  664. tupleVariationCount,
  665. pointCount,
  666. sharedTuples,
  667. data,
  668. pos,
  669. dataPos,
  670. ):
  671. numAxes = len(axisTags)
  672. result = []
  673. if (tupleVariationCount & TUPLES_SHARE_POINT_NUMBERS) != 0:
  674. sharedPoints, dataPos = TupleVariation.decompilePoints_(
  675. pointCount, data, dataPos, tableTag
  676. )
  677. else:
  678. sharedPoints = []
  679. for _ in range(tupleVariationCount & TUPLE_COUNT_MASK):
  680. dataSize, flags = struct.unpack(">HH", data[pos : pos + 4])
  681. tupleSize = TupleVariation.getTupleSize_(flags, numAxes)
  682. tupleData = data[pos : pos + tupleSize]
  683. pointDeltaData = data[dataPos : dataPos + dataSize]
  684. result.append(
  685. decompileTupleVariation_(
  686. pointCount,
  687. sharedTuples,
  688. sharedPoints,
  689. tableTag,
  690. axisTags,
  691. tupleData,
  692. pointDeltaData,
  693. )
  694. )
  695. pos += tupleSize
  696. dataPos += dataSize
  697. return result
  698. def decompileTupleVariation_(
  699. pointCount, sharedTuples, sharedPoints, tableTag, axisTags, data, tupleData
  700. ):
  701. assert tableTag in ("cvar", "gvar"), tableTag
  702. flags = struct.unpack(">H", data[2:4])[0]
  703. pos = 4
  704. if (flags & EMBEDDED_PEAK_TUPLE) == 0:
  705. peak = sharedTuples[flags & TUPLE_INDEX_MASK]
  706. else:
  707. peak, pos = TupleVariation.decompileCoord_(axisTags, data, pos)
  708. if (flags & INTERMEDIATE_REGION) != 0:
  709. start, pos = TupleVariation.decompileCoord_(axisTags, data, pos)
  710. end, pos = TupleVariation.decompileCoord_(axisTags, data, pos)
  711. else:
  712. start, end = inferRegion_(peak)
  713. axes = {}
  714. for axis in axisTags:
  715. region = start[axis], peak[axis], end[axis]
  716. if region != (0.0, 0.0, 0.0):
  717. axes[axis] = region
  718. pos = 0
  719. if (flags & PRIVATE_POINT_NUMBERS) != 0:
  720. points, pos = TupleVariation.decompilePoints_(
  721. pointCount, tupleData, pos, tableTag
  722. )
  723. else:
  724. points = sharedPoints
  725. deltas = [None] * pointCount
  726. if tableTag == "cvar":
  727. deltas_cvt, pos = TupleVariation.decompileDeltas_(len(points), tupleData, pos)
  728. for p, delta in zip(points, deltas_cvt):
  729. if 0 <= p < pointCount:
  730. deltas[p] = delta
  731. elif tableTag == "gvar":
  732. deltas_x, pos = TupleVariation.decompileDeltas_(len(points), tupleData, pos)
  733. deltas_y, pos = TupleVariation.decompileDeltas_(len(points), tupleData, pos)
  734. for p, x, y in zip(points, deltas_x, deltas_y):
  735. if 0 <= p < pointCount:
  736. deltas[p] = (x, y)
  737. return TupleVariation(axes, deltas)
  738. def inferRegion_(peak):
  739. """Infer start and end for a (non-intermediate) region
  740. This helper function computes the applicability region for
  741. variation tuples whose INTERMEDIATE_REGION flag is not set in the
  742. TupleVariationHeader structure. Variation tuples apply only to
  743. certain regions of the variation space; outside that region, the
  744. tuple has no effect. To make the binary encoding more compact,
  745. TupleVariationHeaders can omit the intermediateStartTuple and
  746. intermediateEndTuple fields.
  747. """
  748. start, end = {}, {}
  749. for axis, value in peak.items():
  750. start[axis] = min(value, 0.0) # -0.3 --> -0.3; 0.7 --> 0.0
  751. end[axis] = max(value, 0.0) # -0.3 --> 0.0; 0.7 --> 0.7
  752. return (start, end)