psCharStrings.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. """psCharStrings.py -- module implementing various kinds of CharStrings:
  2. CFF dictionary data and Type1/Type2 CharStrings.
  3. """
  4. from fontTools.misc.fixedTools import (
  5. fixedToFloat,
  6. floatToFixed,
  7. floatToFixedToStr,
  8. strToFixedToFloat,
  9. )
  10. from fontTools.misc.textTools import bytechr, byteord, bytesjoin, strjoin
  11. from fontTools.pens.boundsPen import BoundsPen
  12. import struct
  13. import logging
  14. log = logging.getLogger(__name__)
  15. def read_operator(self, b0, data, index):
  16. if b0 == 12:
  17. op = (b0, byteord(data[index]))
  18. index = index + 1
  19. else:
  20. op = b0
  21. try:
  22. operator = self.operators[op]
  23. except KeyError:
  24. return None, index
  25. value = self.handle_operator(operator)
  26. return value, index
  27. def read_byte(self, b0, data, index):
  28. return b0 - 139, index
  29. def read_smallInt1(self, b0, data, index):
  30. b1 = byteord(data[index])
  31. return (b0 - 247) * 256 + b1 + 108, index + 1
  32. def read_smallInt2(self, b0, data, index):
  33. b1 = byteord(data[index])
  34. return -(b0 - 251) * 256 - b1 - 108, index + 1
  35. def read_shortInt(self, b0, data, index):
  36. (value,) = struct.unpack(">h", data[index : index + 2])
  37. return value, index + 2
  38. def read_longInt(self, b0, data, index):
  39. (value,) = struct.unpack(">l", data[index : index + 4])
  40. return value, index + 4
  41. def read_fixed1616(self, b0, data, index):
  42. (value,) = struct.unpack(">l", data[index : index + 4])
  43. return fixedToFloat(value, precisionBits=16), index + 4
  44. def read_reserved(self, b0, data, index):
  45. assert NotImplementedError
  46. return NotImplemented, index
  47. def read_realNumber(self, b0, data, index):
  48. number = ""
  49. while True:
  50. b = byteord(data[index])
  51. index = index + 1
  52. nibble0 = (b & 0xF0) >> 4
  53. nibble1 = b & 0x0F
  54. if nibble0 == 0xF:
  55. break
  56. number = number + realNibbles[nibble0]
  57. if nibble1 == 0xF:
  58. break
  59. number = number + realNibbles[nibble1]
  60. return float(number), index
  61. t1OperandEncoding = [None] * 256
  62. t1OperandEncoding[0:32] = (32) * [read_operator]
  63. t1OperandEncoding[32:247] = (247 - 32) * [read_byte]
  64. t1OperandEncoding[247:251] = (251 - 247) * [read_smallInt1]
  65. t1OperandEncoding[251:255] = (255 - 251) * [read_smallInt2]
  66. t1OperandEncoding[255] = read_longInt
  67. assert len(t1OperandEncoding) == 256
  68. t2OperandEncoding = t1OperandEncoding[:]
  69. t2OperandEncoding[28] = read_shortInt
  70. t2OperandEncoding[255] = read_fixed1616
  71. cffDictOperandEncoding = t2OperandEncoding[:]
  72. cffDictOperandEncoding[29] = read_longInt
  73. cffDictOperandEncoding[30] = read_realNumber
  74. cffDictOperandEncoding[255] = read_reserved
  75. realNibbles = [
  76. "0",
  77. "1",
  78. "2",
  79. "3",
  80. "4",
  81. "5",
  82. "6",
  83. "7",
  84. "8",
  85. "9",
  86. ".",
  87. "E",
  88. "E-",
  89. None,
  90. "-",
  91. ]
  92. realNibblesDict = {v: i for i, v in enumerate(realNibbles)}
  93. maxOpStack = 193
  94. def buildOperatorDict(operatorList):
  95. oper = {}
  96. opc = {}
  97. for item in operatorList:
  98. if len(item) == 2:
  99. oper[item[0]] = item[1]
  100. else:
  101. oper[item[0]] = item[1:]
  102. if isinstance(item[0], tuple):
  103. opc[item[1]] = item[0]
  104. else:
  105. opc[item[1]] = (item[0],)
  106. return oper, opc
  107. t2Operators = [
  108. # opcode name
  109. (1, "hstem"),
  110. (3, "vstem"),
  111. (4, "vmoveto"),
  112. (5, "rlineto"),
  113. (6, "hlineto"),
  114. (7, "vlineto"),
  115. (8, "rrcurveto"),
  116. (10, "callsubr"),
  117. (11, "return"),
  118. (14, "endchar"),
  119. (15, "vsindex"),
  120. (16, "blend"),
  121. (18, "hstemhm"),
  122. (19, "hintmask"),
  123. (20, "cntrmask"),
  124. (21, "rmoveto"),
  125. (22, "hmoveto"),
  126. (23, "vstemhm"),
  127. (24, "rcurveline"),
  128. (25, "rlinecurve"),
  129. (26, "vvcurveto"),
  130. (27, "hhcurveto"),
  131. # (28, 'shortint'), # not really an operator
  132. (29, "callgsubr"),
  133. (30, "vhcurveto"),
  134. (31, "hvcurveto"),
  135. ((12, 0), "ignore"), # dotsection. Yes, there a few very early OTF/CFF
  136. # fonts with this deprecated operator. Just ignore it.
  137. ((12, 3), "and"),
  138. ((12, 4), "or"),
  139. ((12, 5), "not"),
  140. ((12, 8), "store"),
  141. ((12, 9), "abs"),
  142. ((12, 10), "add"),
  143. ((12, 11), "sub"),
  144. ((12, 12), "div"),
  145. ((12, 13), "load"),
  146. ((12, 14), "neg"),
  147. ((12, 15), "eq"),
  148. ((12, 18), "drop"),
  149. ((12, 20), "put"),
  150. ((12, 21), "get"),
  151. ((12, 22), "ifelse"),
  152. ((12, 23), "random"),
  153. ((12, 24), "mul"),
  154. ((12, 26), "sqrt"),
  155. ((12, 27), "dup"),
  156. ((12, 28), "exch"),
  157. ((12, 29), "index"),
  158. ((12, 30), "roll"),
  159. ((12, 34), "hflex"),
  160. ((12, 35), "flex"),
  161. ((12, 36), "hflex1"),
  162. ((12, 37), "flex1"),
  163. ]
  164. def getIntEncoder(format):
  165. if format == "cff":
  166. twoByteOp = bytechr(28)
  167. fourByteOp = bytechr(29)
  168. elif format == "t1":
  169. twoByteOp = None
  170. fourByteOp = bytechr(255)
  171. else:
  172. assert format == "t2"
  173. twoByteOp = bytechr(28)
  174. fourByteOp = None
  175. def encodeInt(
  176. value,
  177. fourByteOp=fourByteOp,
  178. bytechr=bytechr,
  179. pack=struct.pack,
  180. unpack=struct.unpack,
  181. twoByteOp=twoByteOp,
  182. ):
  183. if -107 <= value <= 107:
  184. code = bytechr(value + 139)
  185. elif 108 <= value <= 1131:
  186. value = value - 108
  187. code = bytechr((value >> 8) + 247) + bytechr(value & 0xFF)
  188. elif -1131 <= value <= -108:
  189. value = -value - 108
  190. code = bytechr((value >> 8) + 251) + bytechr(value & 0xFF)
  191. elif twoByteOp is not None and -32768 <= value <= 32767:
  192. code = twoByteOp + pack(">h", value)
  193. elif fourByteOp is None:
  194. # Backwards compatible hack: due to a previous bug in FontTools,
  195. # 16.16 fixed numbers were written out as 4-byte ints. When
  196. # these numbers were small, they were wrongly written back as
  197. # small ints instead of 4-byte ints, breaking round-tripping.
  198. # This here workaround doesn't do it any better, since we can't
  199. # distinguish anymore between small ints that were supposed to
  200. # be small fixed numbers and small ints that were just small
  201. # ints. Hence the warning.
  202. log.warning(
  203. "4-byte T2 number got passed to the "
  204. "IntType handler. This should happen only when reading in "
  205. "old XML files.\n"
  206. )
  207. code = bytechr(255) + pack(">l", value)
  208. else:
  209. code = fourByteOp + pack(">l", value)
  210. return code
  211. return encodeInt
  212. encodeIntCFF = getIntEncoder("cff")
  213. encodeIntT1 = getIntEncoder("t1")
  214. encodeIntT2 = getIntEncoder("t2")
  215. def encodeFixed(f, pack=struct.pack):
  216. """For T2 only"""
  217. value = floatToFixed(f, precisionBits=16)
  218. if value & 0xFFFF == 0: # check if the fractional part is zero
  219. return encodeIntT2(value >> 16) # encode only the integer part
  220. else:
  221. return b"\xff" + pack(">l", value) # encode the entire fixed point value
  222. realZeroBytes = bytechr(30) + bytechr(0xF)
  223. def encodeFloat(f):
  224. # For CFF only, used in cffLib
  225. if f == 0.0: # 0.0 == +0.0 == -0.0
  226. return realZeroBytes
  227. # Note: 14 decimal digits seems to be the limitation for CFF real numbers
  228. # in macOS. However, we use 8 here to match the implementation of AFDKO.
  229. s = "%.8G" % f
  230. if s[:2] == "0.":
  231. s = s[1:]
  232. elif s[:3] == "-0.":
  233. s = "-" + s[2:]
  234. nibbles = []
  235. while s:
  236. c = s[0]
  237. s = s[1:]
  238. if c == "E":
  239. c2 = s[:1]
  240. if c2 == "-":
  241. s = s[1:]
  242. c = "E-"
  243. elif c2 == "+":
  244. s = s[1:]
  245. nibbles.append(realNibblesDict[c])
  246. nibbles.append(0xF)
  247. if len(nibbles) % 2:
  248. nibbles.append(0xF)
  249. d = bytechr(30)
  250. for i in range(0, len(nibbles), 2):
  251. d = d + bytechr(nibbles[i] << 4 | nibbles[i + 1])
  252. return d
  253. class CharStringCompileError(Exception):
  254. pass
  255. class SimpleT2Decompiler(object):
  256. def __init__(self, localSubrs, globalSubrs, private=None, blender=None):
  257. self.localSubrs = localSubrs
  258. self.localBias = calcSubrBias(localSubrs)
  259. self.globalSubrs = globalSubrs
  260. self.globalBias = calcSubrBias(globalSubrs)
  261. self.private = private
  262. self.blender = blender
  263. self.reset()
  264. def reset(self):
  265. self.callingStack = []
  266. self.operandStack = []
  267. self.hintCount = 0
  268. self.hintMaskBytes = 0
  269. self.numRegions = 0
  270. self.vsIndex = 0
  271. def execute(self, charString):
  272. self.callingStack.append(charString)
  273. needsDecompilation = charString.needsDecompilation()
  274. if needsDecompilation:
  275. program = []
  276. pushToProgram = program.append
  277. else:
  278. pushToProgram = lambda x: None
  279. pushToStack = self.operandStack.append
  280. index = 0
  281. while True:
  282. token, isOperator, index = charString.getToken(index)
  283. if token is None:
  284. break # we're done!
  285. pushToProgram(token)
  286. if isOperator:
  287. handlerName = "op_" + token
  288. handler = getattr(self, handlerName, None)
  289. if handler is not None:
  290. rv = handler(index)
  291. if rv:
  292. hintMaskBytes, index = rv
  293. pushToProgram(hintMaskBytes)
  294. else:
  295. self.popall()
  296. else:
  297. pushToStack(token)
  298. if needsDecompilation:
  299. charString.setProgram(program)
  300. del self.callingStack[-1]
  301. def pop(self):
  302. value = self.operandStack[-1]
  303. del self.operandStack[-1]
  304. return value
  305. def popall(self):
  306. stack = self.operandStack[:]
  307. self.operandStack[:] = []
  308. return stack
  309. def push(self, value):
  310. self.operandStack.append(value)
  311. def op_return(self, index):
  312. if self.operandStack:
  313. pass
  314. def op_endchar(self, index):
  315. pass
  316. def op_ignore(self, index):
  317. pass
  318. def op_callsubr(self, index):
  319. subrIndex = self.pop()
  320. subr = self.localSubrs[subrIndex + self.localBias]
  321. self.execute(subr)
  322. def op_callgsubr(self, index):
  323. subrIndex = self.pop()
  324. subr = self.globalSubrs[subrIndex + self.globalBias]
  325. self.execute(subr)
  326. def op_hstem(self, index):
  327. self.countHints()
  328. def op_vstem(self, index):
  329. self.countHints()
  330. def op_hstemhm(self, index):
  331. self.countHints()
  332. def op_vstemhm(self, index):
  333. self.countHints()
  334. def op_hintmask(self, index):
  335. if not self.hintMaskBytes:
  336. self.countHints()
  337. self.hintMaskBytes = (self.hintCount + 7) // 8
  338. hintMaskBytes, index = self.callingStack[-1].getBytes(index, self.hintMaskBytes)
  339. return hintMaskBytes, index
  340. op_cntrmask = op_hintmask
  341. def countHints(self):
  342. args = self.popall()
  343. self.hintCount = self.hintCount + len(args) // 2
  344. # misc
  345. def op_and(self, index):
  346. raise NotImplementedError
  347. def op_or(self, index):
  348. raise NotImplementedError
  349. def op_not(self, index):
  350. raise NotImplementedError
  351. def op_store(self, index):
  352. raise NotImplementedError
  353. def op_abs(self, index):
  354. raise NotImplementedError
  355. def op_add(self, index):
  356. raise NotImplementedError
  357. def op_sub(self, index):
  358. raise NotImplementedError
  359. def op_div(self, index):
  360. raise NotImplementedError
  361. def op_load(self, index):
  362. raise NotImplementedError
  363. def op_neg(self, index):
  364. raise NotImplementedError
  365. def op_eq(self, index):
  366. raise NotImplementedError
  367. def op_drop(self, index):
  368. raise NotImplementedError
  369. def op_put(self, index):
  370. raise NotImplementedError
  371. def op_get(self, index):
  372. raise NotImplementedError
  373. def op_ifelse(self, index):
  374. raise NotImplementedError
  375. def op_random(self, index):
  376. raise NotImplementedError
  377. def op_mul(self, index):
  378. raise NotImplementedError
  379. def op_sqrt(self, index):
  380. raise NotImplementedError
  381. def op_dup(self, index):
  382. raise NotImplementedError
  383. def op_exch(self, index):
  384. raise NotImplementedError
  385. def op_index(self, index):
  386. raise NotImplementedError
  387. def op_roll(self, index):
  388. raise NotImplementedError
  389. def op_blend(self, index):
  390. if self.numRegions == 0:
  391. self.numRegions = self.private.getNumRegions()
  392. numBlends = self.pop()
  393. numOps = numBlends * (self.numRegions + 1)
  394. if self.blender is None:
  395. del self.operandStack[
  396. -(numOps - numBlends) :
  397. ] # Leave the default operands on the stack.
  398. else:
  399. argi = len(self.operandStack) - numOps
  400. end_args = tuplei = argi + numBlends
  401. while argi < end_args:
  402. next_ti = tuplei + self.numRegions
  403. deltas = self.operandStack[tuplei:next_ti]
  404. delta = self.blender(self.vsIndex, deltas)
  405. self.operandStack[argi] += delta
  406. tuplei = next_ti
  407. argi += 1
  408. self.operandStack[end_args:] = []
  409. def op_vsindex(self, index):
  410. vi = self.pop()
  411. self.vsIndex = vi
  412. self.numRegions = self.private.getNumRegions(vi)
  413. t1Operators = [
  414. # opcode name
  415. (1, "hstem"),
  416. (3, "vstem"),
  417. (4, "vmoveto"),
  418. (5, "rlineto"),
  419. (6, "hlineto"),
  420. (7, "vlineto"),
  421. (8, "rrcurveto"),
  422. (9, "closepath"),
  423. (10, "callsubr"),
  424. (11, "return"),
  425. (13, "hsbw"),
  426. (14, "endchar"),
  427. (21, "rmoveto"),
  428. (22, "hmoveto"),
  429. (30, "vhcurveto"),
  430. (31, "hvcurveto"),
  431. ((12, 0), "dotsection"),
  432. ((12, 1), "vstem3"),
  433. ((12, 2), "hstem3"),
  434. ((12, 6), "seac"),
  435. ((12, 7), "sbw"),
  436. ((12, 12), "div"),
  437. ((12, 16), "callothersubr"),
  438. ((12, 17), "pop"),
  439. ((12, 33), "setcurrentpoint"),
  440. ]
  441. class T2WidthExtractor(SimpleT2Decompiler):
  442. def __init__(
  443. self,
  444. localSubrs,
  445. globalSubrs,
  446. nominalWidthX,
  447. defaultWidthX,
  448. private=None,
  449. blender=None,
  450. ):
  451. SimpleT2Decompiler.__init__(self, localSubrs, globalSubrs, private, blender)
  452. self.nominalWidthX = nominalWidthX
  453. self.defaultWidthX = defaultWidthX
  454. def reset(self):
  455. SimpleT2Decompiler.reset(self)
  456. self.gotWidth = 0
  457. self.width = 0
  458. def popallWidth(self, evenOdd=0):
  459. args = self.popall()
  460. if not self.gotWidth:
  461. if evenOdd ^ (len(args) % 2):
  462. # For CFF2 charstrings, this should never happen
  463. assert (
  464. self.defaultWidthX is not None
  465. ), "CFF2 CharStrings must not have an initial width value"
  466. self.width = self.nominalWidthX + args[0]
  467. args = args[1:]
  468. else:
  469. self.width = self.defaultWidthX
  470. self.gotWidth = 1
  471. return args
  472. def countHints(self):
  473. args = self.popallWidth()
  474. self.hintCount = self.hintCount + len(args) // 2
  475. def op_rmoveto(self, index):
  476. self.popallWidth()
  477. def op_hmoveto(self, index):
  478. self.popallWidth(1)
  479. def op_vmoveto(self, index):
  480. self.popallWidth(1)
  481. def op_endchar(self, index):
  482. self.popallWidth()
  483. class T2OutlineExtractor(T2WidthExtractor):
  484. def __init__(
  485. self,
  486. pen,
  487. localSubrs,
  488. globalSubrs,
  489. nominalWidthX,
  490. defaultWidthX,
  491. private=None,
  492. blender=None,
  493. ):
  494. T2WidthExtractor.__init__(
  495. self,
  496. localSubrs,
  497. globalSubrs,
  498. nominalWidthX,
  499. defaultWidthX,
  500. private,
  501. blender,
  502. )
  503. self.pen = pen
  504. self.subrLevel = 0
  505. def reset(self):
  506. T2WidthExtractor.reset(self)
  507. self.currentPoint = (0, 0)
  508. self.sawMoveTo = 0
  509. self.subrLevel = 0
  510. def execute(self, charString):
  511. self.subrLevel += 1
  512. super().execute(charString)
  513. self.subrLevel -= 1
  514. if self.subrLevel == 0:
  515. self.endPath()
  516. def _nextPoint(self, point):
  517. x, y = self.currentPoint
  518. point = x + point[0], y + point[1]
  519. self.currentPoint = point
  520. return point
  521. def rMoveTo(self, point):
  522. self.pen.moveTo(self._nextPoint(point))
  523. self.sawMoveTo = 1
  524. def rLineTo(self, point):
  525. if not self.sawMoveTo:
  526. self.rMoveTo((0, 0))
  527. self.pen.lineTo(self._nextPoint(point))
  528. def rCurveTo(self, pt1, pt2, pt3):
  529. if not self.sawMoveTo:
  530. self.rMoveTo((0, 0))
  531. nextPoint = self._nextPoint
  532. self.pen.curveTo(nextPoint(pt1), nextPoint(pt2), nextPoint(pt3))
  533. def closePath(self):
  534. if self.sawMoveTo:
  535. self.pen.closePath()
  536. self.sawMoveTo = 0
  537. def endPath(self):
  538. # In T2 there are no open paths, so always do a closePath when
  539. # finishing a sub path. We avoid spurious calls to closePath()
  540. # because its a real T1 op we're emulating in T2 whereas
  541. # endPath() is just a means to that emulation
  542. if self.sawMoveTo:
  543. self.closePath()
  544. #
  545. # hint operators
  546. #
  547. # def op_hstem(self, index):
  548. # self.countHints()
  549. # def op_vstem(self, index):
  550. # self.countHints()
  551. # def op_hstemhm(self, index):
  552. # self.countHints()
  553. # def op_vstemhm(self, index):
  554. # self.countHints()
  555. # def op_hintmask(self, index):
  556. # self.countHints()
  557. # def op_cntrmask(self, index):
  558. # self.countHints()
  559. #
  560. # path constructors, moveto
  561. #
  562. def op_rmoveto(self, index):
  563. self.endPath()
  564. self.rMoveTo(self.popallWidth())
  565. def op_hmoveto(self, index):
  566. self.endPath()
  567. self.rMoveTo((self.popallWidth(1)[0], 0))
  568. def op_vmoveto(self, index):
  569. self.endPath()
  570. self.rMoveTo((0, self.popallWidth(1)[0]))
  571. def op_endchar(self, index):
  572. self.endPath()
  573. args = self.popallWidth()
  574. if args:
  575. from fontTools.encodings.StandardEncoding import StandardEncoding
  576. # endchar can do seac accent bulding; The T2 spec says it's deprecated,
  577. # but recent software that shall remain nameless does output it.
  578. adx, ady, bchar, achar = args
  579. baseGlyph = StandardEncoding[bchar]
  580. self.pen.addComponent(baseGlyph, (1, 0, 0, 1, 0, 0))
  581. accentGlyph = StandardEncoding[achar]
  582. self.pen.addComponent(accentGlyph, (1, 0, 0, 1, adx, ady))
  583. #
  584. # path constructors, lines
  585. #
  586. def op_rlineto(self, index):
  587. args = self.popall()
  588. for i in range(0, len(args), 2):
  589. point = args[i : i + 2]
  590. self.rLineTo(point)
  591. def op_hlineto(self, index):
  592. self.alternatingLineto(1)
  593. def op_vlineto(self, index):
  594. self.alternatingLineto(0)
  595. #
  596. # path constructors, curves
  597. #
  598. def op_rrcurveto(self, index):
  599. """{dxa dya dxb dyb dxc dyc}+ rrcurveto"""
  600. args = self.popall()
  601. for i in range(0, len(args), 6):
  602. (
  603. dxa,
  604. dya,
  605. dxb,
  606. dyb,
  607. dxc,
  608. dyc,
  609. ) = args[i : i + 6]
  610. self.rCurveTo((dxa, dya), (dxb, dyb), (dxc, dyc))
  611. def op_rcurveline(self, index):
  612. """{dxa dya dxb dyb dxc dyc}+ dxd dyd rcurveline"""
  613. args = self.popall()
  614. for i in range(0, len(args) - 2, 6):
  615. dxb, dyb, dxc, dyc, dxd, dyd = args[i : i + 6]
  616. self.rCurveTo((dxb, dyb), (dxc, dyc), (dxd, dyd))
  617. self.rLineTo(args[-2:])
  618. def op_rlinecurve(self, index):
  619. """{dxa dya}+ dxb dyb dxc dyc dxd dyd rlinecurve"""
  620. args = self.popall()
  621. lineArgs = args[:-6]
  622. for i in range(0, len(lineArgs), 2):
  623. self.rLineTo(lineArgs[i : i + 2])
  624. dxb, dyb, dxc, dyc, dxd, dyd = args[-6:]
  625. self.rCurveTo((dxb, dyb), (dxc, dyc), (dxd, dyd))
  626. def op_vvcurveto(self, index):
  627. "dx1? {dya dxb dyb dyc}+ vvcurveto"
  628. args = self.popall()
  629. if len(args) % 2:
  630. dx1 = args[0]
  631. args = args[1:]
  632. else:
  633. dx1 = 0
  634. for i in range(0, len(args), 4):
  635. dya, dxb, dyb, dyc = args[i : i + 4]
  636. self.rCurveTo((dx1, dya), (dxb, dyb), (0, dyc))
  637. dx1 = 0
  638. def op_hhcurveto(self, index):
  639. """dy1? {dxa dxb dyb dxc}+ hhcurveto"""
  640. args = self.popall()
  641. if len(args) % 2:
  642. dy1 = args[0]
  643. args = args[1:]
  644. else:
  645. dy1 = 0
  646. for i in range(0, len(args), 4):
  647. dxa, dxb, dyb, dxc = args[i : i + 4]
  648. self.rCurveTo((dxa, dy1), (dxb, dyb), (dxc, 0))
  649. dy1 = 0
  650. def op_vhcurveto(self, index):
  651. """dy1 dx2 dy2 dx3 {dxa dxb dyb dyc dyd dxe dye dxf}* dyf? vhcurveto (30)
  652. {dya dxb dyb dxc dxd dxe dye dyf}+ dxf? vhcurveto
  653. """
  654. args = self.popall()
  655. while args:
  656. args = self.vcurveto(args)
  657. if args:
  658. args = self.hcurveto(args)
  659. def op_hvcurveto(self, index):
  660. """dx1 dx2 dy2 dy3 {dya dxb dyb dxc dxd dxe dye dyf}* dxf?
  661. {dxa dxb dyb dyc dyd dxe dye dxf}+ dyf?
  662. """
  663. args = self.popall()
  664. while args:
  665. args = self.hcurveto(args)
  666. if args:
  667. args = self.vcurveto(args)
  668. #
  669. # path constructors, flex
  670. #
  671. def op_hflex(self, index):
  672. dx1, dx2, dy2, dx3, dx4, dx5, dx6 = self.popall()
  673. dy1 = dy3 = dy4 = dy6 = 0
  674. dy5 = -dy2
  675. self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
  676. self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
  677. def op_flex(self, index):
  678. dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4, dx5, dy5, dx6, dy6, fd = self.popall()
  679. self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
  680. self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
  681. def op_hflex1(self, index):
  682. dx1, dy1, dx2, dy2, dx3, dx4, dx5, dy5, dx6 = self.popall()
  683. dy3 = dy4 = 0
  684. dy6 = -(dy1 + dy2 + dy3 + dy4 + dy5)
  685. self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
  686. self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
  687. def op_flex1(self, index):
  688. dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4, dx5, dy5, d6 = self.popall()
  689. dx = dx1 + dx2 + dx3 + dx4 + dx5
  690. dy = dy1 + dy2 + dy3 + dy4 + dy5
  691. if abs(dx) > abs(dy):
  692. dx6 = d6
  693. dy6 = -dy
  694. else:
  695. dx6 = -dx
  696. dy6 = d6
  697. self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
  698. self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
  699. # misc
  700. def op_and(self, index):
  701. raise NotImplementedError
  702. def op_or(self, index):
  703. raise NotImplementedError
  704. def op_not(self, index):
  705. raise NotImplementedError
  706. def op_store(self, index):
  707. raise NotImplementedError
  708. def op_abs(self, index):
  709. raise NotImplementedError
  710. def op_add(self, index):
  711. raise NotImplementedError
  712. def op_sub(self, index):
  713. raise NotImplementedError
  714. def op_div(self, index):
  715. num2 = self.pop()
  716. num1 = self.pop()
  717. d1 = num1 // num2
  718. d2 = num1 / num2
  719. if d1 == d2:
  720. self.push(d1)
  721. else:
  722. self.push(d2)
  723. def op_load(self, index):
  724. raise NotImplementedError
  725. def op_neg(self, index):
  726. raise NotImplementedError
  727. def op_eq(self, index):
  728. raise NotImplementedError
  729. def op_drop(self, index):
  730. raise NotImplementedError
  731. def op_put(self, index):
  732. raise NotImplementedError
  733. def op_get(self, index):
  734. raise NotImplementedError
  735. def op_ifelse(self, index):
  736. raise NotImplementedError
  737. def op_random(self, index):
  738. raise NotImplementedError
  739. def op_mul(self, index):
  740. raise NotImplementedError
  741. def op_sqrt(self, index):
  742. raise NotImplementedError
  743. def op_dup(self, index):
  744. raise NotImplementedError
  745. def op_exch(self, index):
  746. raise NotImplementedError
  747. def op_index(self, index):
  748. raise NotImplementedError
  749. def op_roll(self, index):
  750. raise NotImplementedError
  751. #
  752. # miscellaneous helpers
  753. #
  754. def alternatingLineto(self, isHorizontal):
  755. args = self.popall()
  756. for arg in args:
  757. if isHorizontal:
  758. point = (arg, 0)
  759. else:
  760. point = (0, arg)
  761. self.rLineTo(point)
  762. isHorizontal = not isHorizontal
  763. def vcurveto(self, args):
  764. dya, dxb, dyb, dxc = args[:4]
  765. args = args[4:]
  766. if len(args) == 1:
  767. dyc = args[0]
  768. args = []
  769. else:
  770. dyc = 0
  771. self.rCurveTo((0, dya), (dxb, dyb), (dxc, dyc))
  772. return args
  773. def hcurveto(self, args):
  774. dxa, dxb, dyb, dyc = args[:4]
  775. args = args[4:]
  776. if len(args) == 1:
  777. dxc = args[0]
  778. args = []
  779. else:
  780. dxc = 0
  781. self.rCurveTo((dxa, 0), (dxb, dyb), (dxc, dyc))
  782. return args
  783. class T1OutlineExtractor(T2OutlineExtractor):
  784. def __init__(self, pen, subrs):
  785. self.pen = pen
  786. self.subrs = subrs
  787. self.reset()
  788. def reset(self):
  789. self.flexing = 0
  790. self.width = 0
  791. self.sbx = 0
  792. T2OutlineExtractor.reset(self)
  793. def endPath(self):
  794. if self.sawMoveTo:
  795. self.pen.endPath()
  796. self.sawMoveTo = 0
  797. def popallWidth(self, evenOdd=0):
  798. return self.popall()
  799. def exch(self):
  800. stack = self.operandStack
  801. stack[-1], stack[-2] = stack[-2], stack[-1]
  802. #
  803. # path constructors
  804. #
  805. def op_rmoveto(self, index):
  806. if self.flexing:
  807. return
  808. self.endPath()
  809. self.rMoveTo(self.popall())
  810. def op_hmoveto(self, index):
  811. if self.flexing:
  812. # We must add a parameter to the stack if we are flexing
  813. self.push(0)
  814. return
  815. self.endPath()
  816. self.rMoveTo((self.popall()[0], 0))
  817. def op_vmoveto(self, index):
  818. if self.flexing:
  819. # We must add a parameter to the stack if we are flexing
  820. self.push(0)
  821. self.exch()
  822. return
  823. self.endPath()
  824. self.rMoveTo((0, self.popall()[0]))
  825. def op_closepath(self, index):
  826. self.closePath()
  827. def op_setcurrentpoint(self, index):
  828. args = self.popall()
  829. x, y = args
  830. self.currentPoint = x, y
  831. def op_endchar(self, index):
  832. self.endPath()
  833. def op_hsbw(self, index):
  834. sbx, wx = self.popall()
  835. self.width = wx
  836. self.sbx = sbx
  837. self.currentPoint = sbx, self.currentPoint[1]
  838. def op_sbw(self, index):
  839. self.popall() # XXX
  840. #
  841. def op_callsubr(self, index):
  842. subrIndex = self.pop()
  843. subr = self.subrs[subrIndex]
  844. self.execute(subr)
  845. def op_callothersubr(self, index):
  846. subrIndex = self.pop()
  847. nArgs = self.pop()
  848. # print nArgs, subrIndex, "callothersubr"
  849. if subrIndex == 0 and nArgs == 3:
  850. self.doFlex()
  851. self.flexing = 0
  852. elif subrIndex == 1 and nArgs == 0:
  853. self.flexing = 1
  854. # ignore...
  855. def op_pop(self, index):
  856. pass # ignore...
  857. def doFlex(self):
  858. finaly = self.pop()
  859. finalx = self.pop()
  860. self.pop() # flex height is unused
  861. p3y = self.pop()
  862. p3x = self.pop()
  863. bcp4y = self.pop()
  864. bcp4x = self.pop()
  865. bcp3y = self.pop()
  866. bcp3x = self.pop()
  867. p2y = self.pop()
  868. p2x = self.pop()
  869. bcp2y = self.pop()
  870. bcp2x = self.pop()
  871. bcp1y = self.pop()
  872. bcp1x = self.pop()
  873. rpy = self.pop()
  874. rpx = self.pop()
  875. # call rrcurveto
  876. self.push(bcp1x + rpx)
  877. self.push(bcp1y + rpy)
  878. self.push(bcp2x)
  879. self.push(bcp2y)
  880. self.push(p2x)
  881. self.push(p2y)
  882. self.op_rrcurveto(None)
  883. # call rrcurveto
  884. self.push(bcp3x)
  885. self.push(bcp3y)
  886. self.push(bcp4x)
  887. self.push(bcp4y)
  888. self.push(p3x)
  889. self.push(p3y)
  890. self.op_rrcurveto(None)
  891. # Push back final coords so subr 0 can find them
  892. self.push(finalx)
  893. self.push(finaly)
  894. def op_dotsection(self, index):
  895. self.popall() # XXX
  896. def op_hstem3(self, index):
  897. self.popall() # XXX
  898. def op_seac(self, index):
  899. "asb adx ady bchar achar seac"
  900. from fontTools.encodings.StandardEncoding import StandardEncoding
  901. asb, adx, ady, bchar, achar = self.popall()
  902. baseGlyph = StandardEncoding[bchar]
  903. self.pen.addComponent(baseGlyph, (1, 0, 0, 1, 0, 0))
  904. accentGlyph = StandardEncoding[achar]
  905. adx = adx + self.sbx - asb # seac weirdness
  906. self.pen.addComponent(accentGlyph, (1, 0, 0, 1, adx, ady))
  907. def op_vstem3(self, index):
  908. self.popall() # XXX
  909. class T2CharString(object):
  910. operandEncoding = t2OperandEncoding
  911. operators, opcodes = buildOperatorDict(t2Operators)
  912. decompilerClass = SimpleT2Decompiler
  913. outlineExtractor = T2OutlineExtractor
  914. def __init__(self, bytecode=None, program=None, private=None, globalSubrs=None):
  915. if program is None:
  916. program = []
  917. self.bytecode = bytecode
  918. self.program = program
  919. self.private = private
  920. self.globalSubrs = globalSubrs if globalSubrs is not None else []
  921. self._cur_vsindex = None
  922. def getNumRegions(self, vsindex=None):
  923. pd = self.private
  924. assert pd is not None
  925. if vsindex is not None:
  926. self._cur_vsindex = vsindex
  927. elif self._cur_vsindex is None:
  928. self._cur_vsindex = pd.vsindex if hasattr(pd, "vsindex") else 0
  929. return pd.getNumRegions(self._cur_vsindex)
  930. def __repr__(self):
  931. if self.bytecode is None:
  932. return "<%s (source) at %x>" % (self.__class__.__name__, id(self))
  933. else:
  934. return "<%s (bytecode) at %x>" % (self.__class__.__name__, id(self))
  935. def getIntEncoder(self):
  936. return encodeIntT2
  937. def getFixedEncoder(self):
  938. return encodeFixed
  939. def decompile(self):
  940. if not self.needsDecompilation():
  941. return
  942. subrs = getattr(self.private, "Subrs", [])
  943. decompiler = self.decompilerClass(subrs, self.globalSubrs, self.private)
  944. decompiler.execute(self)
  945. def draw(self, pen, blender=None):
  946. subrs = getattr(self.private, "Subrs", [])
  947. extractor = self.outlineExtractor(
  948. pen,
  949. subrs,
  950. self.globalSubrs,
  951. self.private.nominalWidthX,
  952. self.private.defaultWidthX,
  953. self.private,
  954. blender,
  955. )
  956. extractor.execute(self)
  957. self.width = extractor.width
  958. def calcBounds(self, glyphSet):
  959. boundsPen = BoundsPen(glyphSet)
  960. self.draw(boundsPen)
  961. return boundsPen.bounds
  962. def compile(self, isCFF2=False):
  963. if self.bytecode is not None:
  964. return
  965. opcodes = self.opcodes
  966. program = self.program
  967. if isCFF2:
  968. # If present, remove return and endchar operators.
  969. if program and program[-1] in ("return", "endchar"):
  970. program = program[:-1]
  971. elif program and not isinstance(program[-1], str):
  972. raise CharStringCompileError(
  973. "T2CharString or Subr has items on the stack after last operator."
  974. )
  975. bytecode = []
  976. encodeInt = self.getIntEncoder()
  977. encodeFixed = self.getFixedEncoder()
  978. i = 0
  979. end = len(program)
  980. while i < end:
  981. token = program[i]
  982. i = i + 1
  983. if isinstance(token, str):
  984. try:
  985. bytecode.extend(bytechr(b) for b in opcodes[token])
  986. except KeyError:
  987. raise CharStringCompileError("illegal operator: %s" % token)
  988. if token in ("hintmask", "cntrmask"):
  989. bytecode.append(program[i]) # hint mask
  990. i = i + 1
  991. elif isinstance(token, int):
  992. bytecode.append(encodeInt(token))
  993. elif isinstance(token, float):
  994. bytecode.append(encodeFixed(token))
  995. else:
  996. assert 0, "unsupported type: %s" % type(token)
  997. try:
  998. bytecode = bytesjoin(bytecode)
  999. except TypeError:
  1000. log.error(bytecode)
  1001. raise
  1002. self.setBytecode(bytecode)
  1003. def needsDecompilation(self):
  1004. return self.bytecode is not None
  1005. def setProgram(self, program):
  1006. self.program = program
  1007. self.bytecode = None
  1008. def setBytecode(self, bytecode):
  1009. self.bytecode = bytecode
  1010. self.program = None
  1011. def getToken(self, index, len=len, byteord=byteord, isinstance=isinstance):
  1012. if self.bytecode is not None:
  1013. if index >= len(self.bytecode):
  1014. return None, 0, 0
  1015. b0 = byteord(self.bytecode[index])
  1016. index = index + 1
  1017. handler = self.operandEncoding[b0]
  1018. token, index = handler(self, b0, self.bytecode, index)
  1019. else:
  1020. if index >= len(self.program):
  1021. return None, 0, 0
  1022. token = self.program[index]
  1023. index = index + 1
  1024. isOperator = isinstance(token, str)
  1025. return token, isOperator, index
  1026. def getBytes(self, index, nBytes):
  1027. if self.bytecode is not None:
  1028. newIndex = index + nBytes
  1029. bytes = self.bytecode[index:newIndex]
  1030. index = newIndex
  1031. else:
  1032. bytes = self.program[index]
  1033. index = index + 1
  1034. assert len(bytes) == nBytes
  1035. return bytes, index
  1036. def handle_operator(self, operator):
  1037. return operator
  1038. def toXML(self, xmlWriter, ttFont=None):
  1039. from fontTools.misc.textTools import num2binary
  1040. if self.bytecode is not None:
  1041. xmlWriter.dumphex(self.bytecode)
  1042. else:
  1043. index = 0
  1044. args = []
  1045. while True:
  1046. token, isOperator, index = self.getToken(index)
  1047. if token is None:
  1048. break
  1049. if isOperator:
  1050. if token in ("hintmask", "cntrmask"):
  1051. hintMask, isOperator, index = self.getToken(index)
  1052. bits = []
  1053. for byte in hintMask:
  1054. bits.append(num2binary(byteord(byte), 8))
  1055. hintMask = strjoin(bits)
  1056. line = " ".join(args + [token, hintMask])
  1057. else:
  1058. line = " ".join(args + [token])
  1059. xmlWriter.write(line)
  1060. xmlWriter.newline()
  1061. args = []
  1062. else:
  1063. if isinstance(token, float):
  1064. token = floatToFixedToStr(token, precisionBits=16)
  1065. else:
  1066. token = str(token)
  1067. args.append(token)
  1068. if args:
  1069. # NOTE: only CFF2 charstrings/subrs can have numeric arguments on
  1070. # the stack after the last operator. Compiling this would fail if
  1071. # this is part of CFF 1.0 table.
  1072. line = " ".join(args)
  1073. xmlWriter.write(line)
  1074. def fromXML(self, name, attrs, content):
  1075. from fontTools.misc.textTools import binary2num, readHex
  1076. if attrs.get("raw"):
  1077. self.setBytecode(readHex(content))
  1078. return
  1079. content = strjoin(content)
  1080. content = content.split()
  1081. program = []
  1082. end = len(content)
  1083. i = 0
  1084. while i < end:
  1085. token = content[i]
  1086. i = i + 1
  1087. try:
  1088. token = int(token)
  1089. except ValueError:
  1090. try:
  1091. token = strToFixedToFloat(token, precisionBits=16)
  1092. except ValueError:
  1093. program.append(token)
  1094. if token in ("hintmask", "cntrmask"):
  1095. mask = content[i]
  1096. maskBytes = b""
  1097. for j in range(0, len(mask), 8):
  1098. maskBytes = maskBytes + bytechr(binary2num(mask[j : j + 8]))
  1099. program.append(maskBytes)
  1100. i = i + 1
  1101. else:
  1102. program.append(token)
  1103. else:
  1104. program.append(token)
  1105. self.setProgram(program)
  1106. class T1CharString(T2CharString):
  1107. operandEncoding = t1OperandEncoding
  1108. operators, opcodes = buildOperatorDict(t1Operators)
  1109. def __init__(self, bytecode=None, program=None, subrs=None):
  1110. super().__init__(bytecode, program)
  1111. self.subrs = subrs
  1112. def getIntEncoder(self):
  1113. return encodeIntT1
  1114. def getFixedEncoder(self):
  1115. def encodeFixed(value):
  1116. raise TypeError("Type 1 charstrings don't support floating point operands")
  1117. def decompile(self):
  1118. if self.bytecode is None:
  1119. return
  1120. program = []
  1121. index = 0
  1122. while True:
  1123. token, isOperator, index = self.getToken(index)
  1124. if token is None:
  1125. break
  1126. program.append(token)
  1127. self.setProgram(program)
  1128. def draw(self, pen):
  1129. extractor = T1OutlineExtractor(pen, self.subrs)
  1130. extractor.execute(self)
  1131. self.width = extractor.width
  1132. class DictDecompiler(object):
  1133. operandEncoding = cffDictOperandEncoding
  1134. def __init__(self, strings, parent=None):
  1135. self.stack = []
  1136. self.strings = strings
  1137. self.dict = {}
  1138. self.parent = parent
  1139. def getDict(self):
  1140. assert len(self.stack) == 0, "non-empty stack"
  1141. return self.dict
  1142. def decompile(self, data):
  1143. index = 0
  1144. lenData = len(data)
  1145. push = self.stack.append
  1146. while index < lenData:
  1147. b0 = byteord(data[index])
  1148. index = index + 1
  1149. handler = self.operandEncoding[b0]
  1150. value, index = handler(self, b0, data, index)
  1151. if value is not None:
  1152. push(value)
  1153. def pop(self):
  1154. value = self.stack[-1]
  1155. del self.stack[-1]
  1156. return value
  1157. def popall(self):
  1158. args = self.stack[:]
  1159. del self.stack[:]
  1160. return args
  1161. def handle_operator(self, operator):
  1162. operator, argType = operator
  1163. if isinstance(argType, tuple):
  1164. value = ()
  1165. for i in range(len(argType) - 1, -1, -1):
  1166. arg = argType[i]
  1167. arghandler = getattr(self, "arg_" + arg)
  1168. value = (arghandler(operator),) + value
  1169. else:
  1170. arghandler = getattr(self, "arg_" + argType)
  1171. value = arghandler(operator)
  1172. if operator == "blend":
  1173. self.stack.extend(value)
  1174. else:
  1175. self.dict[operator] = value
  1176. def arg_number(self, name):
  1177. if isinstance(self.stack[0], list):
  1178. out = self.arg_blend_number(self.stack)
  1179. else:
  1180. out = self.pop()
  1181. return out
  1182. def arg_blend_number(self, name):
  1183. out = []
  1184. blendArgs = self.pop()
  1185. numMasters = len(blendArgs)
  1186. out.append(blendArgs)
  1187. out.append("blend")
  1188. dummy = self.popall()
  1189. return blendArgs
  1190. def arg_SID(self, name):
  1191. return self.strings[self.pop()]
  1192. def arg_array(self, name):
  1193. return self.popall()
  1194. def arg_blendList(self, name):
  1195. """
  1196. There may be non-blend args at the top of the stack. We first calculate
  1197. where the blend args start in the stack. These are the last
  1198. numMasters*numBlends) +1 args.
  1199. The blend args starts with numMasters relative coordinate values, the BlueValues in the list from the default master font. This is followed by
  1200. numBlends list of values. Each of value in one of these lists is the
  1201. Variable Font delta for the matching region.
  1202. We re-arrange this to be a list of numMaster entries. Each entry starts with the corresponding default font relative value, and is followed by
  1203. the delta values. We then convert the default values, the first item in each entry, to an absolute value.
  1204. """
  1205. vsindex = self.dict.get("vsindex", 0)
  1206. numMasters = (
  1207. self.parent.getNumRegions(vsindex) + 1
  1208. ) # only a PrivateDict has blended ops.
  1209. numBlends = self.pop()
  1210. args = self.popall()
  1211. numArgs = len(args)
  1212. # The spec says that there should be no non-blended Blue Values,.
  1213. assert numArgs == numMasters * numBlends
  1214. value = [None] * numBlends
  1215. numDeltas = numMasters - 1
  1216. i = 0
  1217. prevVal = 0
  1218. while i < numBlends:
  1219. newVal = args[i] + prevVal
  1220. prevVal = newVal
  1221. masterOffset = numBlends + (i * numDeltas)
  1222. blendList = [newVal] + args[masterOffset : masterOffset + numDeltas]
  1223. value[i] = blendList
  1224. i += 1
  1225. return value
  1226. def arg_delta(self, name):
  1227. valueList = self.popall()
  1228. out = []
  1229. if valueList and isinstance(valueList[0], list):
  1230. # arg_blendList() has already converted these to absolute values.
  1231. out = valueList
  1232. else:
  1233. current = 0
  1234. for v in valueList:
  1235. current = current + v
  1236. out.append(current)
  1237. return out
  1238. def calcSubrBias(subrs):
  1239. nSubrs = len(subrs)
  1240. if nSubrs < 1240:
  1241. bias = 107
  1242. elif nSubrs < 33900:
  1243. bias = 1131
  1244. else:
  1245. bias = 32768
  1246. return bias