TupleVariation.py 29 KB

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