TupleVariation.py 31 KB

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