specializer.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. # -*- coding: utf-8 -*-
  2. """T2CharString operator specializer and generalizer.
  3. PostScript glyph drawing operations can be expressed in multiple different
  4. ways. For example, as well as the ``lineto`` operator, there is also a
  5. ``hlineto`` operator which draws a horizontal line, removing the need to
  6. specify a ``dx`` coordinate, and a ``vlineto`` operator which draws a
  7. vertical line, removing the need to specify a ``dy`` coordinate. As well
  8. as decompiling :class:`fontTools.misc.psCharStrings.T2CharString` objects
  9. into lists of operations, this module allows for conversion between general
  10. and specific forms of the operation.
  11. """
  12. from fontTools.cffLib import maxStackLimit
  13. def stringToProgram(string):
  14. if isinstance(string, str):
  15. string = string.split()
  16. program = []
  17. for token in string:
  18. try:
  19. token = int(token)
  20. except ValueError:
  21. try:
  22. token = float(token)
  23. except ValueError:
  24. pass
  25. program.append(token)
  26. return program
  27. def programToString(program):
  28. return " ".join(str(x) for x in program)
  29. def programToCommands(program, getNumRegions=None):
  30. """Takes a T2CharString program list and returns list of commands.
  31. Each command is a two-tuple of commandname,arg-list. The commandname might
  32. be empty string if no commandname shall be emitted (used for glyph width,
  33. hintmask/cntrmask argument, as well as stray arguments at the end of the
  34. program (🤷).
  35. 'getNumRegions' may be None, or a callable object. It must return the
  36. number of regions. 'getNumRegions' takes a single argument, vsindex. It
  37. returns the numRegions for the vsindex.
  38. The Charstring may or may not start with a width value. If the first
  39. non-blend operator has an odd number of arguments, then the first argument is
  40. a width, and is popped off. This is complicated with blend operators, as
  41. there may be more than one before the first hint or moveto operator, and each
  42. one reduces several arguments to just one list argument. We have to sum the
  43. number of arguments that are not part of the blend arguments, and all the
  44. 'numBlends' values. We could instead have said that by definition, if there
  45. is a blend operator, there is no width value, since CFF2 Charstrings don't
  46. have width values. I discussed this with Behdad, and we are allowing for an
  47. initial width value in this case because developers may assemble a CFF2
  48. charstring from CFF Charstrings, which could have width values.
  49. """
  50. seenWidthOp = False
  51. vsIndex = 0
  52. lenBlendStack = 0
  53. lastBlendIndex = 0
  54. commands = []
  55. stack = []
  56. it = iter(program)
  57. for token in it:
  58. if not isinstance(token, str):
  59. stack.append(token)
  60. continue
  61. if token == "blend":
  62. assert getNumRegions is not None
  63. numSourceFonts = 1 + getNumRegions(vsIndex)
  64. # replace the blend op args on the stack with a single list
  65. # containing all the blend op args.
  66. numBlends = stack[-1]
  67. numBlendArgs = numBlends * numSourceFonts + 1
  68. # replace first blend op by a list of the blend ops.
  69. stack[-numBlendArgs:] = [stack[-numBlendArgs:]]
  70. lenStack = len(stack)
  71. lenBlendStack += numBlends + lenStack - 1
  72. lastBlendIndex = lenStack
  73. # if a blend op exists, this is or will be a CFF2 charstring.
  74. continue
  75. elif token == "vsindex":
  76. vsIndex = stack[-1]
  77. assert type(vsIndex) is int
  78. elif (not seenWidthOp) and token in {
  79. "hstem",
  80. "hstemhm",
  81. "vstem",
  82. "vstemhm",
  83. "cntrmask",
  84. "hintmask",
  85. "hmoveto",
  86. "vmoveto",
  87. "rmoveto",
  88. "endchar",
  89. }:
  90. seenWidthOp = True
  91. parity = token in {"hmoveto", "vmoveto"}
  92. if lenBlendStack:
  93. # lenBlendStack has the number of args represented by the last blend
  94. # arg and all the preceding args. We need to now add the number of
  95. # args following the last blend arg.
  96. numArgs = lenBlendStack + len(stack[lastBlendIndex:])
  97. else:
  98. numArgs = len(stack)
  99. if numArgs and (numArgs % 2) ^ parity:
  100. width = stack.pop(0)
  101. commands.append(("", [width]))
  102. if token in {"hintmask", "cntrmask"}:
  103. if stack:
  104. commands.append(("", stack))
  105. commands.append((token, []))
  106. commands.append(("", [next(it)]))
  107. else:
  108. commands.append((token, stack))
  109. stack = []
  110. if stack:
  111. commands.append(("", stack))
  112. return commands
  113. def _flattenBlendArgs(args):
  114. token_list = []
  115. for arg in args:
  116. if isinstance(arg, list):
  117. token_list.extend(arg)
  118. token_list.append("blend")
  119. else:
  120. token_list.append(arg)
  121. return token_list
  122. def commandsToProgram(commands):
  123. """Takes a commands list as returned by programToCommands() and converts
  124. it back to a T2CharString program list."""
  125. program = []
  126. for op, args in commands:
  127. if any(isinstance(arg, list) for arg in args):
  128. args = _flattenBlendArgs(args)
  129. program.extend(args)
  130. if op:
  131. program.append(op)
  132. return program
  133. def _everyN(el, n):
  134. """Group the list el into groups of size n"""
  135. l = len(el)
  136. if l % n != 0:
  137. raise ValueError(el)
  138. for i in range(0, l, n):
  139. yield el[i : i + n]
  140. class _GeneralizerDecombinerCommandsMap(object):
  141. @staticmethod
  142. def rmoveto(args):
  143. if len(args) != 2:
  144. raise ValueError(args)
  145. yield ("rmoveto", args)
  146. @staticmethod
  147. def hmoveto(args):
  148. if len(args) != 1:
  149. raise ValueError(args)
  150. yield ("rmoveto", [args[0], 0])
  151. @staticmethod
  152. def vmoveto(args):
  153. if len(args) != 1:
  154. raise ValueError(args)
  155. yield ("rmoveto", [0, args[0]])
  156. @staticmethod
  157. def rlineto(args):
  158. if not args:
  159. raise ValueError(args)
  160. for args in _everyN(args, 2):
  161. yield ("rlineto", args)
  162. @staticmethod
  163. def hlineto(args):
  164. if not args:
  165. raise ValueError(args)
  166. it = iter(args)
  167. try:
  168. while True:
  169. yield ("rlineto", [next(it), 0])
  170. yield ("rlineto", [0, next(it)])
  171. except StopIteration:
  172. pass
  173. @staticmethod
  174. def vlineto(args):
  175. if not args:
  176. raise ValueError(args)
  177. it = iter(args)
  178. try:
  179. while True:
  180. yield ("rlineto", [0, next(it)])
  181. yield ("rlineto", [next(it), 0])
  182. except StopIteration:
  183. pass
  184. @staticmethod
  185. def rrcurveto(args):
  186. if not args:
  187. raise ValueError(args)
  188. for args in _everyN(args, 6):
  189. yield ("rrcurveto", args)
  190. @staticmethod
  191. def hhcurveto(args):
  192. l = len(args)
  193. if l < 4 or l % 4 > 1:
  194. raise ValueError(args)
  195. if l % 2 == 1:
  196. yield ("rrcurveto", [args[1], args[0], args[2], args[3], args[4], 0])
  197. args = args[5:]
  198. for args in _everyN(args, 4):
  199. yield ("rrcurveto", [args[0], 0, args[1], args[2], args[3], 0])
  200. @staticmethod
  201. def vvcurveto(args):
  202. l = len(args)
  203. if l < 4 or l % 4 > 1:
  204. raise ValueError(args)
  205. if l % 2 == 1:
  206. yield ("rrcurveto", [args[0], args[1], args[2], args[3], 0, args[4]])
  207. args = args[5:]
  208. for args in _everyN(args, 4):
  209. yield ("rrcurveto", [0, args[0], args[1], args[2], 0, args[3]])
  210. @staticmethod
  211. def hvcurveto(args):
  212. l = len(args)
  213. if l < 4 or l % 8 not in {0, 1, 4, 5}:
  214. raise ValueError(args)
  215. last_args = None
  216. if l % 2 == 1:
  217. lastStraight = l % 8 == 5
  218. args, last_args = args[:-5], args[-5:]
  219. it = _everyN(args, 4)
  220. try:
  221. while True:
  222. args = next(it)
  223. yield ("rrcurveto", [args[0], 0, args[1], args[2], 0, args[3]])
  224. args = next(it)
  225. yield ("rrcurveto", [0, args[0], args[1], args[2], args[3], 0])
  226. except StopIteration:
  227. pass
  228. if last_args:
  229. args = last_args
  230. if lastStraight:
  231. yield ("rrcurveto", [args[0], 0, args[1], args[2], args[4], args[3]])
  232. else:
  233. yield ("rrcurveto", [0, args[0], args[1], args[2], args[3], args[4]])
  234. @staticmethod
  235. def vhcurveto(args):
  236. l = len(args)
  237. if l < 4 or l % 8 not in {0, 1, 4, 5}:
  238. raise ValueError(args)
  239. last_args = None
  240. if l % 2 == 1:
  241. lastStraight = l % 8 == 5
  242. args, last_args = args[:-5], args[-5:]
  243. it = _everyN(args, 4)
  244. try:
  245. while True:
  246. args = next(it)
  247. yield ("rrcurveto", [0, args[0], args[1], args[2], args[3], 0])
  248. args = next(it)
  249. yield ("rrcurveto", [args[0], 0, args[1], args[2], 0, args[3]])
  250. except StopIteration:
  251. pass
  252. if last_args:
  253. args = last_args
  254. if lastStraight:
  255. yield ("rrcurveto", [0, args[0], args[1], args[2], args[3], args[4]])
  256. else:
  257. yield ("rrcurveto", [args[0], 0, args[1], args[2], args[4], args[3]])
  258. @staticmethod
  259. def rcurveline(args):
  260. l = len(args)
  261. if l < 8 or l % 6 != 2:
  262. raise ValueError(args)
  263. args, last_args = args[:-2], args[-2:]
  264. for args in _everyN(args, 6):
  265. yield ("rrcurveto", args)
  266. yield ("rlineto", last_args)
  267. @staticmethod
  268. def rlinecurve(args):
  269. l = len(args)
  270. if l < 8 or l % 2 != 0:
  271. raise ValueError(args)
  272. args, last_args = args[:-6], args[-6:]
  273. for args in _everyN(args, 2):
  274. yield ("rlineto", args)
  275. yield ("rrcurveto", last_args)
  276. def _convertBlendOpToArgs(blendList):
  277. # args is list of blend op args. Since we are supporting
  278. # recursive blend op calls, some of these args may also
  279. # be a list of blend op args, and need to be converted before
  280. # we convert the current list.
  281. if any([isinstance(arg, list) for arg in blendList]):
  282. args = [
  283. i
  284. for e in blendList
  285. for i in (_convertBlendOpToArgs(e) if isinstance(e, list) else [e])
  286. ]
  287. else:
  288. args = blendList
  289. # We now know that blendList contains a blend op argument list, even if
  290. # some of the args are lists that each contain a blend op argument list.
  291. # Convert from:
  292. # [default font arg sequence x0,...,xn] + [delta tuple for x0] + ... + [delta tuple for xn]
  293. # to:
  294. # [ [x0] + [delta tuple for x0],
  295. # ...,
  296. # [xn] + [delta tuple for xn] ]
  297. numBlends = args[-1]
  298. # Can't use args.pop() when the args are being used in a nested list
  299. # comprehension. See calling context
  300. args = args[:-1]
  301. l = len(args)
  302. numRegions = l // numBlends - 1
  303. if not (numBlends * (numRegions + 1) == l):
  304. raise ValueError(blendList)
  305. defaultArgs = [[arg] for arg in args[:numBlends]]
  306. deltaArgs = args[numBlends:]
  307. numDeltaValues = len(deltaArgs)
  308. deltaList = [
  309. deltaArgs[i : i + numRegions] for i in range(0, numDeltaValues, numRegions)
  310. ]
  311. blend_args = [a + b + [1] for a, b in zip(defaultArgs, deltaList)]
  312. return blend_args
  313. def generalizeCommands(commands, ignoreErrors=False):
  314. result = []
  315. mapping = _GeneralizerDecombinerCommandsMap
  316. for op, args in commands:
  317. # First, generalize any blend args in the arg list.
  318. if any([isinstance(arg, list) for arg in args]):
  319. try:
  320. args = [
  321. n
  322. for arg in args
  323. for n in (
  324. _convertBlendOpToArgs(arg) if isinstance(arg, list) else [arg]
  325. )
  326. ]
  327. except ValueError:
  328. if ignoreErrors:
  329. # Store op as data, such that consumers of commands do not have to
  330. # deal with incorrect number of arguments.
  331. result.append(("", args))
  332. result.append(("", [op]))
  333. else:
  334. raise
  335. func = getattr(mapping, op, None)
  336. if func is None:
  337. result.append((op, args))
  338. continue
  339. try:
  340. for command in func(args):
  341. result.append(command)
  342. except ValueError:
  343. if ignoreErrors:
  344. # Store op as data, such that consumers of commands do not have to
  345. # deal with incorrect number of arguments.
  346. result.append(("", args))
  347. result.append(("", [op]))
  348. else:
  349. raise
  350. return result
  351. def generalizeProgram(program, getNumRegions=None, **kwargs):
  352. return commandsToProgram(
  353. generalizeCommands(programToCommands(program, getNumRegions), **kwargs)
  354. )
  355. def _categorizeVector(v):
  356. """
  357. Takes X,Y vector v and returns one of r, h, v, or 0 depending on which
  358. of X and/or Y are zero, plus tuple of nonzero ones. If both are zero,
  359. it returns a single zero still.
  360. >>> _categorizeVector((0,0))
  361. ('0', (0,))
  362. >>> _categorizeVector((1,0))
  363. ('h', (1,))
  364. >>> _categorizeVector((0,2))
  365. ('v', (2,))
  366. >>> _categorizeVector((1,2))
  367. ('r', (1, 2))
  368. """
  369. if not v[0]:
  370. if not v[1]:
  371. return "0", v[:1]
  372. else:
  373. return "v", v[1:]
  374. else:
  375. if not v[1]:
  376. return "h", v[:1]
  377. else:
  378. return "r", v
  379. def _mergeCategories(a, b):
  380. if a == "0":
  381. return b
  382. if b == "0":
  383. return a
  384. if a == b:
  385. return a
  386. return None
  387. def _negateCategory(a):
  388. if a == "h":
  389. return "v"
  390. if a == "v":
  391. return "h"
  392. assert a in "0r"
  393. return a
  394. def _convertToBlendCmds(args):
  395. # return a list of blend commands, and
  396. # the remaining non-blended args, if any.
  397. num_args = len(args)
  398. stack_use = 0
  399. new_args = []
  400. i = 0
  401. while i < num_args:
  402. arg = args[i]
  403. i += 1
  404. if not isinstance(arg, list):
  405. new_args.append(arg)
  406. stack_use += 1
  407. else:
  408. prev_stack_use = stack_use
  409. # The arg is a tuple of blend values.
  410. # These are each (master 0,delta 1..delta n, 1)
  411. # Combine as many successive tuples as we can,
  412. # up to the max stack limit.
  413. num_sources = len(arg) - 1
  414. blendlist = [arg]
  415. stack_use += 1 + num_sources # 1 for the num_blends arg
  416. # if we are here, max stack is the CFF2 max stack.
  417. # I use the CFF2 max stack limit here rather than
  418. # the 'maxstack' chosen by the client, as the default
  419. # maxstack may have been used unintentionally. For all
  420. # the other operators, this just produces a little less
  421. # optimization, but here it puts a hard (and low) limit
  422. # on the number of source fonts that can be used.
  423. #
  424. # Make sure the stack depth does not exceed (maxstack - 1), so
  425. # that subroutinizer can insert subroutine calls at any point.
  426. while (
  427. (i < num_args)
  428. and isinstance(args[i], list)
  429. and stack_use + num_sources < maxStackLimit
  430. ):
  431. blendlist.append(args[i])
  432. i += 1
  433. stack_use += num_sources
  434. # blendList now contains as many single blend tuples as can be
  435. # combined without exceeding the CFF2 stack limit.
  436. num_blends = len(blendlist)
  437. # append the 'num_blends' default font values
  438. blend_args = []
  439. for arg in blendlist:
  440. blend_args.append(arg[0])
  441. for arg in blendlist:
  442. assert arg[-1] == 1
  443. blend_args.extend(arg[1:-1])
  444. blend_args.append(num_blends)
  445. new_args.append(blend_args)
  446. stack_use = prev_stack_use + num_blends
  447. return new_args
  448. def _addArgs(a, b):
  449. if isinstance(b, list):
  450. if isinstance(a, list):
  451. if len(a) != len(b) or a[-1] != b[-1]:
  452. raise ValueError()
  453. return [_addArgs(va, vb) for va, vb in zip(a[:-1], b[:-1])] + [a[-1]]
  454. else:
  455. a, b = b, a
  456. if isinstance(a, list):
  457. assert a[-1] == 1
  458. return [_addArgs(a[0], b)] + a[1:]
  459. return a + b
  460. def _argsStackUse(args):
  461. stackLen = 0
  462. maxLen = 0
  463. for arg in args:
  464. if type(arg) is list:
  465. # Blended arg
  466. maxLen = max(maxLen, stackLen + _argsStackUse(arg))
  467. stackLen += arg[-1]
  468. else:
  469. stackLen += 1
  470. return max(stackLen, maxLen)
  471. def specializeCommands(
  472. commands,
  473. ignoreErrors=False,
  474. generalizeFirst=True,
  475. preserveTopology=False,
  476. maxstack=48,
  477. ):
  478. # We perform several rounds of optimizations. They are carefully ordered and are:
  479. #
  480. # 0. Generalize commands.
  481. # This ensures that they are in our expected simple form, with each line/curve only
  482. # having arguments for one segment, and using the generic form (rlineto/rrcurveto).
  483. # If caller is sure the input is in this form, they can turn off generalization to
  484. # save time.
  485. #
  486. # 1. Combine successive rmoveto operations.
  487. #
  488. # 2. Specialize rmoveto/rlineto/rrcurveto operators into horizontal/vertical variants.
  489. # We specialize into some, made-up, variants as well, which simplifies following
  490. # passes.
  491. #
  492. # 3. Merge or delete redundant operations, to the extent requested.
  493. # OpenType spec declares point numbers in CFF undefined. As such, we happily
  494. # change topology. If client relies on point numbers (in GPOS anchors, or for
  495. # hinting purposes(what?)) they can turn this off.
  496. #
  497. # 4. Peephole optimization to revert back some of the h/v variants back into their
  498. # original "relative" operator (rline/rrcurveto) if that saves a byte.
  499. #
  500. # 5. Combine adjacent operators when possible, minding not to go over max stack size.
  501. #
  502. # 6. Resolve any remaining made-up operators into real operators.
  503. #
  504. # I have convinced myself that this produces optimal bytecode (except for, possibly
  505. # one byte each time maxstack size prohibits combining.) YMMV, but you'd be wrong. :-)
  506. # A dynamic-programming approach can do the same but would be significantly slower.
  507. #
  508. # 7. For any args which are blend lists, convert them to a blend command.
  509. # 0. Generalize commands.
  510. if generalizeFirst:
  511. commands = generalizeCommands(commands, ignoreErrors=ignoreErrors)
  512. else:
  513. commands = list(commands) # Make copy since we modify in-place later.
  514. # 1. Combine successive rmoveto operations.
  515. for i in range(len(commands) - 1, 0, -1):
  516. if "rmoveto" == commands[i][0] == commands[i - 1][0]:
  517. v1, v2 = commands[i - 1][1], commands[i][1]
  518. commands[i - 1] = ("rmoveto", [v1[0] + v2[0], v1[1] + v2[1]])
  519. del commands[i]
  520. # 2. Specialize rmoveto/rlineto/rrcurveto operators into horizontal/vertical variants.
  521. #
  522. # We, in fact, specialize into more, made-up, variants that special-case when both
  523. # X and Y components are zero. This simplifies the following optimization passes.
  524. # This case is rare, but OCD does not let me skip it.
  525. #
  526. # After this round, we will have four variants that use the following mnemonics:
  527. #
  528. # - 'r' for relative, ie. non-zero X and non-zero Y,
  529. # - 'h' for horizontal, ie. zero X and non-zero Y,
  530. # - 'v' for vertical, ie. non-zero X and zero Y,
  531. # - '0' for zeros, ie. zero X and zero Y.
  532. #
  533. # The '0' pseudo-operators are not part of the spec, but help simplify the following
  534. # optimization rounds. We resolve them at the end. So, after this, we will have four
  535. # moveto and four lineto variants:
  536. #
  537. # - 0moveto, 0lineto
  538. # - hmoveto, hlineto
  539. # - vmoveto, vlineto
  540. # - rmoveto, rlineto
  541. #
  542. # and sixteen curveto variants. For example, a '0hcurveto' operator means a curve
  543. # dx0,dy0,dx1,dy1,dx2,dy2,dx3,dy3 where dx0, dx1, and dy3 are zero but not dx3.
  544. # An 'rvcurveto' means dx3 is zero but not dx0,dy0,dy3.
  545. #
  546. # There are nine different variants of curves without the '0'. Those nine map exactly
  547. # to the existing curve variants in the spec: rrcurveto, and the four variants hhcurveto,
  548. # vvcurveto, hvcurveto, and vhcurveto each cover two cases, one with an odd number of
  549. # arguments and one without. Eg. an hhcurveto with an extra argument (odd number of
  550. # arguments) is in fact an rhcurveto. The operators in the spec are designed such that
  551. # all four of rhcurveto, rvcurveto, hrcurveto, and vrcurveto are encodable for one curve.
  552. #
  553. # Of the curve types with '0', the 00curveto is equivalent to a lineto variant. The rest
  554. # of the curve types with a 0 need to be encoded as a h or v variant. Ie. a '0' can be
  555. # thought of a "don't care" and can be used as either an 'h' or a 'v'. As such, we always
  556. # encode a number 0 as argument when we use a '0' variant. Later on, we can just substitute
  557. # the '0' with either 'h' or 'v' and it works.
  558. #
  559. # When we get to curve splines however, things become more complicated... XXX finish this.
  560. # There's one more complexity with splines. If one side of the spline is not horizontal or
  561. # vertical (or zero), ie. if it's 'r', then it limits which spline types we can encode.
  562. # Only hhcurveto and vvcurveto operators can encode a spline starting with 'r', and
  563. # only hvcurveto and vhcurveto operators can encode a spline ending with 'r'.
  564. # This limits our merge opportunities later.
  565. #
  566. for i in range(len(commands)):
  567. op, args = commands[i]
  568. if op in {"rmoveto", "rlineto"}:
  569. c, args = _categorizeVector(args)
  570. commands[i] = c + op[1:], args
  571. continue
  572. if op == "rrcurveto":
  573. c1, args1 = _categorizeVector(args[:2])
  574. c2, args2 = _categorizeVector(args[-2:])
  575. commands[i] = c1 + c2 + "curveto", args1 + args[2:4] + args2
  576. continue
  577. # 3. Merge or delete redundant operations, to the extent requested.
  578. #
  579. # TODO
  580. # A 0moveto that comes before all other path operations can be removed.
  581. # though I find conflicting evidence for this.
  582. #
  583. # TODO
  584. # "If hstem and vstem hints are both declared at the beginning of a
  585. # CharString, and this sequence is followed directly by the hintmask or
  586. # cntrmask operators, then the vstem hint operator (or, if applicable,
  587. # the vstemhm operator) need not be included."
  588. #
  589. # "The sequence and form of a CFF2 CharString program may be represented as:
  590. # {hs* vs* cm* hm* mt subpath}? {mt subpath}*"
  591. #
  592. # https://www.microsoft.com/typography/otspec/cff2charstr.htm#section3.1
  593. #
  594. # For Type2 CharStrings the sequence is:
  595. # w? {hs* vs* cm* hm* mt subpath}? {mt subpath}* endchar"
  596. # Some other redundancies change topology (point numbers).
  597. if not preserveTopology:
  598. for i in range(len(commands) - 1, -1, -1):
  599. op, args = commands[i]
  600. # A 00curveto is demoted to a (specialized) lineto.
  601. if op == "00curveto":
  602. assert len(args) == 4
  603. c, args = _categorizeVector(args[1:3])
  604. op = c + "lineto"
  605. commands[i] = op, args
  606. # and then...
  607. # A 0lineto can be deleted.
  608. if op == "0lineto":
  609. del commands[i]
  610. continue
  611. # Merge adjacent hlineto's and vlineto's.
  612. # In CFF2 charstrings from variable fonts, each
  613. # arg item may be a list of blendable values, one from
  614. # each source font.
  615. if i and op in {"hlineto", "vlineto"} and (op == commands[i - 1][0]):
  616. _, other_args = commands[i - 1]
  617. assert len(args) == 1 and len(other_args) == 1
  618. try:
  619. new_args = [_addArgs(args[0], other_args[0])]
  620. except ValueError:
  621. continue
  622. commands[i - 1] = (op, new_args)
  623. del commands[i]
  624. continue
  625. # 4. Peephole optimization to revert back some of the h/v variants back into their
  626. # original "relative" operator (rline/rrcurveto) if that saves a byte.
  627. for i in range(1, len(commands) - 1):
  628. op, args = commands[i]
  629. prv, nxt = commands[i - 1][0], commands[i + 1][0]
  630. if op in {"0lineto", "hlineto", "vlineto"} and prv == nxt == "rlineto":
  631. assert len(args) == 1
  632. args = [0, args[0]] if op[0] == "v" else [args[0], 0]
  633. commands[i] = ("rlineto", args)
  634. continue
  635. if op[2:] == "curveto" and len(args) == 5 and prv == nxt == "rrcurveto":
  636. assert (op[0] == "r") ^ (op[1] == "r")
  637. if op[0] == "v":
  638. pos = 0
  639. elif op[0] != "r":
  640. pos = 1
  641. elif op[1] == "v":
  642. pos = 4
  643. else:
  644. pos = 5
  645. # Insert, while maintaining the type of args (can be tuple or list).
  646. args = args[:pos] + type(args)((0,)) + args[pos:]
  647. commands[i] = ("rrcurveto", args)
  648. continue
  649. # 5. Combine adjacent operators when possible, minding not to go over max stack size.
  650. stackUse = _argsStackUse(commands[-1][1]) if commands else 0
  651. for i in range(len(commands) - 1, 0, -1):
  652. op1, args1 = commands[i - 1]
  653. op2, args2 = commands[i]
  654. new_op = None
  655. # Merge logic...
  656. if {op1, op2} <= {"rlineto", "rrcurveto"}:
  657. if op1 == op2:
  658. new_op = op1
  659. else:
  660. l = len(args2)
  661. if op2 == "rrcurveto" and l == 6:
  662. new_op = "rlinecurve"
  663. elif l == 2:
  664. new_op = "rcurveline"
  665. elif (op1, op2) in {("rlineto", "rlinecurve"), ("rrcurveto", "rcurveline")}:
  666. new_op = op2
  667. elif {op1, op2} == {"vlineto", "hlineto"}:
  668. new_op = op1
  669. elif "curveto" == op1[2:] == op2[2:]:
  670. d0, d1 = op1[:2]
  671. d2, d3 = op2[:2]
  672. if d1 == "r" or d2 == "r" or d0 == d3 == "r":
  673. continue
  674. d = _mergeCategories(d1, d2)
  675. if d is None:
  676. continue
  677. if d0 == "r":
  678. d = _mergeCategories(d, d3)
  679. if d is None:
  680. continue
  681. new_op = "r" + d + "curveto"
  682. elif d3 == "r":
  683. d0 = _mergeCategories(d0, _negateCategory(d))
  684. if d0 is None:
  685. continue
  686. new_op = d0 + "r" + "curveto"
  687. else:
  688. d0 = _mergeCategories(d0, d3)
  689. if d0 is None:
  690. continue
  691. new_op = d0 + d + "curveto"
  692. # Make sure the stack depth does not exceed (maxstack - 1), so
  693. # that subroutinizer can insert subroutine calls at any point.
  694. args1StackUse = _argsStackUse(args1)
  695. combinedStackUse = max(args1StackUse, len(args1) + stackUse)
  696. if new_op and combinedStackUse < maxstack:
  697. commands[i - 1] = (new_op, args1 + args2)
  698. del commands[i]
  699. stackUse = combinedStackUse
  700. else:
  701. stackUse = args1StackUse
  702. # 6. Resolve any remaining made-up operators into real operators.
  703. for i in range(len(commands)):
  704. op, args = commands[i]
  705. if op in {"0moveto", "0lineto"}:
  706. commands[i] = "h" + op[1:], args
  707. continue
  708. if op[2:] == "curveto" and op[:2] not in {"rr", "hh", "vv", "vh", "hv"}:
  709. l = len(args)
  710. op0, op1 = op[:2]
  711. if (op0 == "r") ^ (op1 == "r"):
  712. assert l % 2 == 1
  713. if op0 == "0":
  714. op0 = "h"
  715. if op1 == "0":
  716. op1 = "h"
  717. if op0 == "r":
  718. op0 = op1
  719. if op1 == "r":
  720. op1 = _negateCategory(op0)
  721. assert {op0, op1} <= {"h", "v"}, (op0, op1)
  722. if l % 2:
  723. if op0 != op1: # vhcurveto / hvcurveto
  724. if (op0 == "h") ^ (l % 8 == 1):
  725. # Swap last two args order
  726. args = args[:-2] + args[-1:] + args[-2:-1]
  727. else: # hhcurveto / vvcurveto
  728. if op0 == "h": # hhcurveto
  729. # Swap first two args order
  730. args = args[1:2] + args[:1] + args[2:]
  731. commands[i] = op0 + op1 + "curveto", args
  732. continue
  733. # 7. For any series of args which are blend lists, convert the series to a single blend arg.
  734. for i in range(len(commands)):
  735. op, args = commands[i]
  736. if any(isinstance(arg, list) for arg in args):
  737. commands[i] = op, _convertToBlendCmds(args)
  738. return commands
  739. def specializeProgram(program, getNumRegions=None, **kwargs):
  740. return commandsToProgram(
  741. specializeCommands(programToCommands(program, getNumRegions), **kwargs)
  742. )
  743. if __name__ == "__main__":
  744. import sys
  745. if len(sys.argv) == 1:
  746. import doctest
  747. sys.exit(doctest.testmod().failed)
  748. import argparse
  749. parser = argparse.ArgumentParser(
  750. "fonttools cffLib.specializer",
  751. description="CFF CharString generalizer/specializer",
  752. )
  753. parser.add_argument("program", metavar="command", nargs="*", help="Commands.")
  754. parser.add_argument(
  755. "--num-regions",
  756. metavar="NumRegions",
  757. nargs="*",
  758. default=None,
  759. help="Number of variable-font regions for blend opertaions.",
  760. )
  761. parser.add_argument(
  762. "--font",
  763. metavar="FONTFILE",
  764. default=None,
  765. help="CFF2 font to specialize.",
  766. )
  767. parser.add_argument(
  768. "-o",
  769. "--output-file",
  770. type=str,
  771. help="Output font file name.",
  772. )
  773. options = parser.parse_args(sys.argv[1:])
  774. if options.program:
  775. getNumRegions = (
  776. None
  777. if options.num_regions is None
  778. else lambda vsIndex: int(
  779. options.num_regions[0 if vsIndex is None else vsIndex]
  780. )
  781. )
  782. program = stringToProgram(options.program)
  783. print("Program:")
  784. print(programToString(program))
  785. commands = programToCommands(program, getNumRegions)
  786. print("Commands:")
  787. print(commands)
  788. program2 = commandsToProgram(commands)
  789. print("Program from commands:")
  790. print(programToString(program2))
  791. assert program == program2
  792. print("Generalized program:")
  793. print(programToString(generalizeProgram(program, getNumRegions)))
  794. print("Specialized program:")
  795. print(programToString(specializeProgram(program, getNumRegions)))
  796. if options.font:
  797. from fontTools.ttLib import TTFont
  798. font = TTFont(options.font)
  799. cff2 = font["CFF2"].cff.topDictIndex[0]
  800. charstrings = cff2.CharStrings
  801. for glyphName in charstrings.keys():
  802. charstring = charstrings[glyphName]
  803. charstring.decompile()
  804. getNumRegions = charstring.private.getNumRegions
  805. charstring.program = specializeProgram(
  806. charstring.program, getNumRegions, maxstack=maxStackLimit
  807. )
  808. if options.output_file is None:
  809. from fontTools.misc.cliTools import makeOutputFileName
  810. outfile = makeOutputFileName(
  811. options.font, overWrite=True, suffix=".specialized"
  812. )
  813. else:
  814. outfile = options.output_file
  815. if outfile:
  816. print("Saving", outfile)
  817. font.save(outfile)