psCharStrings.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. elif s.endswith("000"):
  235. significantDigits = s.rstrip("0")
  236. s = "%sE%d" % (significantDigits, len(s) - len(significantDigits))
  237. else:
  238. dotIndex = s.find(".")
  239. eIndex = s.find("E")
  240. if dotIndex != -1 and eIndex != -1:
  241. integerPart = s[:dotIndex]
  242. fractionalPart = s[dotIndex + 1 : eIndex]
  243. exponent = int(s[eIndex + 1 :])
  244. newExponent = exponent - len(fractionalPart)
  245. if newExponent == 1:
  246. s = "%s%s0" % (integerPart, fractionalPart)
  247. else:
  248. s = "%s%sE%d" % (integerPart, fractionalPart, newExponent)
  249. if s.startswith((".0", "-.0")):
  250. sign, s = s.split(".", 1)
  251. s = "%s%sE-%d" % (sign, s.lstrip("0"), len(s))
  252. nibbles = []
  253. while s:
  254. c = s[0]
  255. s = s[1:]
  256. if c == "E":
  257. c2 = s[:1]
  258. if c2 == "-":
  259. s = s[1:]
  260. c = "E-"
  261. elif c2 == "+":
  262. s = s[1:]
  263. if s.startswith("0"):
  264. s = s[1:]
  265. nibbles.append(realNibblesDict[c])
  266. nibbles.append(0xF)
  267. if len(nibbles) % 2:
  268. nibbles.append(0xF)
  269. d = bytechr(30)
  270. for i in range(0, len(nibbles), 2):
  271. d = d + bytechr(nibbles[i] << 4 | nibbles[i + 1])
  272. return d
  273. class CharStringCompileError(Exception):
  274. pass
  275. class SimpleT2Decompiler(object):
  276. def __init__(self, localSubrs, globalSubrs, private=None, blender=None):
  277. self.localSubrs = localSubrs
  278. self.localBias = calcSubrBias(localSubrs)
  279. self.globalSubrs = globalSubrs
  280. self.globalBias = calcSubrBias(globalSubrs)
  281. self.private = private
  282. self.blender = blender
  283. self.reset()
  284. def reset(self):
  285. self.callingStack = []
  286. self.operandStack = []
  287. self.hintCount = 0
  288. self.hintMaskBytes = 0
  289. self.numRegions = 0
  290. self.vsIndex = 0
  291. def execute(self, charString, *, pushToStack=None):
  292. self.callingStack.append(charString)
  293. needsDecompilation = charString.needsDecompilation()
  294. if needsDecompilation:
  295. program = []
  296. pushToProgram = program.append
  297. else:
  298. pushToProgram = lambda x: None
  299. if pushToStack is None:
  300. pushToStack = self.operandStack.append
  301. index = 0
  302. while True:
  303. token, isOperator, index = charString.getToken(index)
  304. if token is None:
  305. break # we're done!
  306. pushToProgram(token)
  307. if isOperator:
  308. handlerName = "op_" + token
  309. handler = getattr(self, handlerName, None)
  310. if handler is not None:
  311. rv = handler(index)
  312. if rv:
  313. hintMaskBytes, index = rv
  314. pushToProgram(hintMaskBytes)
  315. else:
  316. self.popall()
  317. else:
  318. pushToStack(token)
  319. if needsDecompilation:
  320. charString.setProgram(program)
  321. del self.callingStack[-1]
  322. def pop(self):
  323. value = self.operandStack[-1]
  324. del self.operandStack[-1]
  325. return value
  326. def popall(self):
  327. stack = self.operandStack[:]
  328. self.operandStack[:] = []
  329. return stack
  330. def push(self, value):
  331. self.operandStack.append(value)
  332. def op_return(self, index):
  333. if self.operandStack:
  334. pass
  335. def op_endchar(self, index):
  336. pass
  337. def op_ignore(self, index):
  338. pass
  339. def op_callsubr(self, index):
  340. subrIndex = self.pop()
  341. subr = self.localSubrs[subrIndex + self.localBias]
  342. self.execute(subr)
  343. def op_callgsubr(self, index):
  344. subrIndex = self.pop()
  345. subr = self.globalSubrs[subrIndex + self.globalBias]
  346. self.execute(subr)
  347. def op_hstem(self, index):
  348. self.countHints()
  349. def op_vstem(self, index):
  350. self.countHints()
  351. def op_hstemhm(self, index):
  352. self.countHints()
  353. def op_vstemhm(self, index):
  354. self.countHints()
  355. def op_hintmask(self, index):
  356. if not self.hintMaskBytes:
  357. self.countHints()
  358. self.hintMaskBytes = (self.hintCount + 7) // 8
  359. hintMaskBytes, index = self.callingStack[-1].getBytes(index, self.hintMaskBytes)
  360. return hintMaskBytes, index
  361. op_cntrmask = op_hintmask
  362. def countHints(self):
  363. args = self.popall()
  364. self.hintCount = self.hintCount + len(args) // 2
  365. # misc
  366. def op_and(self, index):
  367. raise NotImplementedError
  368. def op_or(self, index):
  369. raise NotImplementedError
  370. def op_not(self, index):
  371. raise NotImplementedError
  372. def op_store(self, index):
  373. raise NotImplementedError
  374. def op_abs(self, index):
  375. raise NotImplementedError
  376. def op_add(self, index):
  377. raise NotImplementedError
  378. def op_sub(self, index):
  379. raise NotImplementedError
  380. def op_div(self, index):
  381. raise NotImplementedError
  382. def op_load(self, index):
  383. raise NotImplementedError
  384. def op_neg(self, index):
  385. raise NotImplementedError
  386. def op_eq(self, index):
  387. raise NotImplementedError
  388. def op_drop(self, index):
  389. raise NotImplementedError
  390. def op_put(self, index):
  391. raise NotImplementedError
  392. def op_get(self, index):
  393. raise NotImplementedError
  394. def op_ifelse(self, index):
  395. raise NotImplementedError
  396. def op_random(self, index):
  397. raise NotImplementedError
  398. def op_mul(self, index):
  399. raise NotImplementedError
  400. def op_sqrt(self, index):
  401. raise NotImplementedError
  402. def op_dup(self, index):
  403. raise NotImplementedError
  404. def op_exch(self, index):
  405. raise NotImplementedError
  406. def op_index(self, index):
  407. raise NotImplementedError
  408. def op_roll(self, index):
  409. raise NotImplementedError
  410. def op_blend(self, index):
  411. if self.numRegions == 0:
  412. self.numRegions = self.private.getNumRegions()
  413. numBlends = self.pop()
  414. numOps = numBlends * (self.numRegions + 1)
  415. if self.blender is None:
  416. del self.operandStack[
  417. -(numOps - numBlends) :
  418. ] # Leave the default operands on the stack.
  419. else:
  420. argi = len(self.operandStack) - numOps
  421. end_args = tuplei = argi + numBlends
  422. while argi < end_args:
  423. next_ti = tuplei + self.numRegions
  424. deltas = self.operandStack[tuplei:next_ti]
  425. delta = self.blender(self.vsIndex, deltas)
  426. self.operandStack[argi] += delta
  427. tuplei = next_ti
  428. argi += 1
  429. self.operandStack[end_args:] = []
  430. def op_vsindex(self, index):
  431. vi = self.pop()
  432. self.vsIndex = vi
  433. self.numRegions = self.private.getNumRegions(vi)
  434. t1Operators = [
  435. # opcode name
  436. (1, "hstem"),
  437. (3, "vstem"),
  438. (4, "vmoveto"),
  439. (5, "rlineto"),
  440. (6, "hlineto"),
  441. (7, "vlineto"),
  442. (8, "rrcurveto"),
  443. (9, "closepath"),
  444. (10, "callsubr"),
  445. (11, "return"),
  446. (13, "hsbw"),
  447. (14, "endchar"),
  448. (21, "rmoveto"),
  449. (22, "hmoveto"),
  450. (30, "vhcurveto"),
  451. (31, "hvcurveto"),
  452. ((12, 0), "dotsection"),
  453. ((12, 1), "vstem3"),
  454. ((12, 2), "hstem3"),
  455. ((12, 6), "seac"),
  456. ((12, 7), "sbw"),
  457. ((12, 12), "div"),
  458. ((12, 16), "callothersubr"),
  459. ((12, 17), "pop"),
  460. ((12, 33), "setcurrentpoint"),
  461. ]
  462. class T2StackUseExtractor(SimpleT2Decompiler):
  463. def execute(self, charString):
  464. maxStackUse = 0
  465. def pushToStack(value):
  466. nonlocal maxStackUse
  467. self.operandStack.append(value)
  468. maxStackUse = max(maxStackUse, len(self.operandStack))
  469. super().execute(charString, pushToStack=pushToStack)
  470. return maxStackUse
  471. class T2WidthExtractor(SimpleT2Decompiler):
  472. def __init__(
  473. self,
  474. localSubrs,
  475. globalSubrs,
  476. nominalWidthX,
  477. defaultWidthX,
  478. private=None,
  479. blender=None,
  480. ):
  481. SimpleT2Decompiler.__init__(self, localSubrs, globalSubrs, private, blender)
  482. self.nominalWidthX = nominalWidthX
  483. self.defaultWidthX = defaultWidthX
  484. def reset(self):
  485. SimpleT2Decompiler.reset(self)
  486. self.gotWidth = 0
  487. self.width = 0
  488. def popallWidth(self, evenOdd=0):
  489. args = self.popall()
  490. if not self.gotWidth:
  491. if evenOdd ^ (len(args) % 2):
  492. # For CFF2 charstrings, this should never happen
  493. assert (
  494. self.defaultWidthX is not None
  495. ), "CFF2 CharStrings must not have an initial width value"
  496. self.width = self.nominalWidthX + args[0]
  497. args = args[1:]
  498. else:
  499. self.width = self.defaultWidthX
  500. self.gotWidth = 1
  501. return args
  502. def countHints(self):
  503. args = self.popallWidth()
  504. self.hintCount = self.hintCount + len(args) // 2
  505. def op_rmoveto(self, index):
  506. self.popallWidth()
  507. def op_hmoveto(self, index):
  508. self.popallWidth(1)
  509. def op_vmoveto(self, index):
  510. self.popallWidth(1)
  511. def op_endchar(self, index):
  512. self.popallWidth()
  513. class T2OutlineExtractor(T2WidthExtractor):
  514. def __init__(
  515. self,
  516. pen,
  517. localSubrs,
  518. globalSubrs,
  519. nominalWidthX,
  520. defaultWidthX,
  521. private=None,
  522. blender=None,
  523. ):
  524. T2WidthExtractor.__init__(
  525. self,
  526. localSubrs,
  527. globalSubrs,
  528. nominalWidthX,
  529. defaultWidthX,
  530. private,
  531. blender,
  532. )
  533. self.pen = pen
  534. self.subrLevel = 0
  535. def reset(self):
  536. T2WidthExtractor.reset(self)
  537. self.currentPoint = (0, 0)
  538. self.sawMoveTo = 0
  539. self.subrLevel = 0
  540. def execute(self, charString):
  541. self.subrLevel += 1
  542. super().execute(charString)
  543. self.subrLevel -= 1
  544. if self.subrLevel == 0:
  545. self.endPath()
  546. def _nextPoint(self, point):
  547. x, y = self.currentPoint
  548. point = x + point[0], y + point[1]
  549. self.currentPoint = point
  550. return point
  551. def rMoveTo(self, point):
  552. self.pen.moveTo(self._nextPoint(point))
  553. self.sawMoveTo = 1
  554. def rLineTo(self, point):
  555. if not self.sawMoveTo:
  556. self.rMoveTo((0, 0))
  557. self.pen.lineTo(self._nextPoint(point))
  558. def rCurveTo(self, pt1, pt2, pt3):
  559. if not self.sawMoveTo:
  560. self.rMoveTo((0, 0))
  561. nextPoint = self._nextPoint
  562. self.pen.curveTo(nextPoint(pt1), nextPoint(pt2), nextPoint(pt3))
  563. def closePath(self):
  564. if self.sawMoveTo:
  565. self.pen.closePath()
  566. self.sawMoveTo = 0
  567. def endPath(self):
  568. # In T2 there are no open paths, so always do a closePath when
  569. # finishing a sub path. We avoid spurious calls to closePath()
  570. # because its a real T1 op we're emulating in T2 whereas
  571. # endPath() is just a means to that emulation
  572. if self.sawMoveTo:
  573. self.closePath()
  574. #
  575. # hint operators
  576. #
  577. # def op_hstem(self, index):
  578. # self.countHints()
  579. # def op_vstem(self, index):
  580. # self.countHints()
  581. # def op_hstemhm(self, index):
  582. # self.countHints()
  583. # def op_vstemhm(self, index):
  584. # self.countHints()
  585. # def op_hintmask(self, index):
  586. # self.countHints()
  587. # def op_cntrmask(self, index):
  588. # self.countHints()
  589. #
  590. # path constructors, moveto
  591. #
  592. def op_rmoveto(self, index):
  593. self.endPath()
  594. self.rMoveTo(self.popallWidth())
  595. def op_hmoveto(self, index):
  596. self.endPath()
  597. self.rMoveTo((self.popallWidth(1)[0], 0))
  598. def op_vmoveto(self, index):
  599. self.endPath()
  600. self.rMoveTo((0, self.popallWidth(1)[0]))
  601. def op_endchar(self, index):
  602. self.endPath()
  603. args = self.popallWidth()
  604. if args:
  605. from fontTools.encodings.StandardEncoding import StandardEncoding
  606. # endchar can do seac accent bulding; The T2 spec says it's deprecated,
  607. # but recent software that shall remain nameless does output it.
  608. adx, ady, bchar, achar = args
  609. baseGlyph = StandardEncoding[bchar]
  610. self.pen.addComponent(baseGlyph, (1, 0, 0, 1, 0, 0))
  611. accentGlyph = StandardEncoding[achar]
  612. self.pen.addComponent(accentGlyph, (1, 0, 0, 1, adx, ady))
  613. #
  614. # path constructors, lines
  615. #
  616. def op_rlineto(self, index):
  617. args = self.popall()
  618. for i in range(0, len(args), 2):
  619. point = args[i : i + 2]
  620. self.rLineTo(point)
  621. def op_hlineto(self, index):
  622. self.alternatingLineto(1)
  623. def op_vlineto(self, index):
  624. self.alternatingLineto(0)
  625. #
  626. # path constructors, curves
  627. #
  628. def op_rrcurveto(self, index):
  629. """{dxa dya dxb dyb dxc dyc}+ rrcurveto"""
  630. args = self.popall()
  631. for i in range(0, len(args), 6):
  632. (
  633. dxa,
  634. dya,
  635. dxb,
  636. dyb,
  637. dxc,
  638. dyc,
  639. ) = args[i : i + 6]
  640. self.rCurveTo((dxa, dya), (dxb, dyb), (dxc, dyc))
  641. def op_rcurveline(self, index):
  642. """{dxa dya dxb dyb dxc dyc}+ dxd dyd rcurveline"""
  643. args = self.popall()
  644. for i in range(0, len(args) - 2, 6):
  645. dxb, dyb, dxc, dyc, dxd, dyd = args[i : i + 6]
  646. self.rCurveTo((dxb, dyb), (dxc, dyc), (dxd, dyd))
  647. self.rLineTo(args[-2:])
  648. def op_rlinecurve(self, index):
  649. """{dxa dya}+ dxb dyb dxc dyc dxd dyd rlinecurve"""
  650. args = self.popall()
  651. lineArgs = args[:-6]
  652. for i in range(0, len(lineArgs), 2):
  653. self.rLineTo(lineArgs[i : i + 2])
  654. dxb, dyb, dxc, dyc, dxd, dyd = args[-6:]
  655. self.rCurveTo((dxb, dyb), (dxc, dyc), (dxd, dyd))
  656. def op_vvcurveto(self, index):
  657. "dx1? {dya dxb dyb dyc}+ vvcurveto"
  658. args = self.popall()
  659. if len(args) % 2:
  660. dx1 = args[0]
  661. args = args[1:]
  662. else:
  663. dx1 = 0
  664. for i in range(0, len(args), 4):
  665. dya, dxb, dyb, dyc = args[i : i + 4]
  666. self.rCurveTo((dx1, dya), (dxb, dyb), (0, dyc))
  667. dx1 = 0
  668. def op_hhcurveto(self, index):
  669. """dy1? {dxa dxb dyb dxc}+ hhcurveto"""
  670. args = self.popall()
  671. if len(args) % 2:
  672. dy1 = args[0]
  673. args = args[1:]
  674. else:
  675. dy1 = 0
  676. for i in range(0, len(args), 4):
  677. dxa, dxb, dyb, dxc = args[i : i + 4]
  678. self.rCurveTo((dxa, dy1), (dxb, dyb), (dxc, 0))
  679. dy1 = 0
  680. def op_vhcurveto(self, index):
  681. """dy1 dx2 dy2 dx3 {dxa dxb dyb dyc dyd dxe dye dxf}* dyf? vhcurveto (30)
  682. {dya dxb dyb dxc dxd dxe dye dyf}+ dxf? vhcurveto
  683. """
  684. args = self.popall()
  685. while args:
  686. args = self.vcurveto(args)
  687. if args:
  688. args = self.hcurveto(args)
  689. def op_hvcurveto(self, index):
  690. """dx1 dx2 dy2 dy3 {dya dxb dyb dxc dxd dxe dye dyf}* dxf?
  691. {dxa dxb dyb dyc dyd dxe dye dxf}+ dyf?
  692. """
  693. args = self.popall()
  694. while args:
  695. args = self.hcurveto(args)
  696. if args:
  697. args = self.vcurveto(args)
  698. #
  699. # path constructors, flex
  700. #
  701. def op_hflex(self, index):
  702. dx1, dx2, dy2, dx3, dx4, dx5, dx6 = self.popall()
  703. dy1 = dy3 = dy4 = dy6 = 0
  704. dy5 = -dy2
  705. self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
  706. self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
  707. def op_flex(self, index):
  708. dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4, dx5, dy5, dx6, dy6, fd = self.popall()
  709. self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
  710. self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
  711. def op_hflex1(self, index):
  712. dx1, dy1, dx2, dy2, dx3, dx4, dx5, dy5, dx6 = self.popall()
  713. dy3 = dy4 = 0
  714. dy6 = -(dy1 + dy2 + dy3 + dy4 + dy5)
  715. self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
  716. self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
  717. def op_flex1(self, index):
  718. dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4, dx5, dy5, d6 = self.popall()
  719. dx = dx1 + dx2 + dx3 + dx4 + dx5
  720. dy = dy1 + dy2 + dy3 + dy4 + dy5
  721. if abs(dx) > abs(dy):
  722. dx6 = d6
  723. dy6 = -dy
  724. else:
  725. dx6 = -dx
  726. dy6 = d6
  727. self.rCurveTo((dx1, dy1), (dx2, dy2), (dx3, dy3))
  728. self.rCurveTo((dx4, dy4), (dx5, dy5), (dx6, dy6))
  729. # misc
  730. def op_and(self, index):
  731. raise NotImplementedError
  732. def op_or(self, index):
  733. raise NotImplementedError
  734. def op_not(self, index):
  735. raise NotImplementedError
  736. def op_store(self, index):
  737. raise NotImplementedError
  738. def op_abs(self, index):
  739. raise NotImplementedError
  740. def op_add(self, index):
  741. raise NotImplementedError
  742. def op_sub(self, index):
  743. raise NotImplementedError
  744. def op_div(self, index):
  745. num2 = self.pop()
  746. num1 = self.pop()
  747. d1 = num1 // num2
  748. d2 = num1 / num2
  749. if d1 == d2:
  750. self.push(d1)
  751. else:
  752. self.push(d2)
  753. def op_load(self, index):
  754. raise NotImplementedError
  755. def op_neg(self, index):
  756. raise NotImplementedError
  757. def op_eq(self, index):
  758. raise NotImplementedError
  759. def op_drop(self, index):
  760. raise NotImplementedError
  761. def op_put(self, index):
  762. raise NotImplementedError
  763. def op_get(self, index):
  764. raise NotImplementedError
  765. def op_ifelse(self, index):
  766. raise NotImplementedError
  767. def op_random(self, index):
  768. raise NotImplementedError
  769. def op_mul(self, index):
  770. raise NotImplementedError
  771. def op_sqrt(self, index):
  772. raise NotImplementedError
  773. def op_dup(self, index):
  774. raise NotImplementedError
  775. def op_exch(self, index):
  776. raise NotImplementedError
  777. def op_index(self, index):
  778. raise NotImplementedError
  779. def op_roll(self, index):
  780. raise NotImplementedError
  781. #
  782. # miscellaneous helpers
  783. #
  784. def alternatingLineto(self, isHorizontal):
  785. args = self.popall()
  786. for arg in args:
  787. if isHorizontal:
  788. point = (arg, 0)
  789. else:
  790. point = (0, arg)
  791. self.rLineTo(point)
  792. isHorizontal = not isHorizontal
  793. def vcurveto(self, args):
  794. dya, dxb, dyb, dxc = args[:4]
  795. args = args[4:]
  796. if len(args) == 1:
  797. dyc = args[0]
  798. args = []
  799. else:
  800. dyc = 0
  801. self.rCurveTo((0, dya), (dxb, dyb), (dxc, dyc))
  802. return args
  803. def hcurveto(self, args):
  804. dxa, dxb, dyb, dyc = args[:4]
  805. args = args[4:]
  806. if len(args) == 1:
  807. dxc = args[0]
  808. args = []
  809. else:
  810. dxc = 0
  811. self.rCurveTo((dxa, 0), (dxb, dyb), (dxc, dyc))
  812. return args
  813. class T1OutlineExtractor(T2OutlineExtractor):
  814. def __init__(self, pen, subrs):
  815. self.pen = pen
  816. self.subrs = subrs
  817. self.reset()
  818. def reset(self):
  819. self.flexing = 0
  820. self.width = 0
  821. self.sbx = 0
  822. T2OutlineExtractor.reset(self)
  823. def endPath(self):
  824. if self.sawMoveTo:
  825. self.pen.endPath()
  826. self.sawMoveTo = 0
  827. def popallWidth(self, evenOdd=0):
  828. return self.popall()
  829. def exch(self):
  830. stack = self.operandStack
  831. stack[-1], stack[-2] = stack[-2], stack[-1]
  832. #
  833. # path constructors
  834. #
  835. def op_rmoveto(self, index):
  836. if self.flexing:
  837. return
  838. self.endPath()
  839. self.rMoveTo(self.popall())
  840. def op_hmoveto(self, index):
  841. if self.flexing:
  842. # We must add a parameter to the stack if we are flexing
  843. self.push(0)
  844. return
  845. self.endPath()
  846. self.rMoveTo((self.popall()[0], 0))
  847. def op_vmoveto(self, index):
  848. if self.flexing:
  849. # We must add a parameter to the stack if we are flexing
  850. self.push(0)
  851. self.exch()
  852. return
  853. self.endPath()
  854. self.rMoveTo((0, self.popall()[0]))
  855. def op_closepath(self, index):
  856. self.closePath()
  857. def op_setcurrentpoint(self, index):
  858. args = self.popall()
  859. x, y = args
  860. self.currentPoint = x, y
  861. def op_endchar(self, index):
  862. self.endPath()
  863. def op_hsbw(self, index):
  864. sbx, wx = self.popall()
  865. self.width = wx
  866. self.sbx = sbx
  867. self.currentPoint = sbx, self.currentPoint[1]
  868. def op_sbw(self, index):
  869. self.popall() # XXX
  870. #
  871. def op_callsubr(self, index):
  872. subrIndex = self.pop()
  873. subr = self.subrs[subrIndex]
  874. self.execute(subr)
  875. def op_callothersubr(self, index):
  876. subrIndex = self.pop()
  877. nArgs = self.pop()
  878. # print nArgs, subrIndex, "callothersubr"
  879. if subrIndex == 0 and nArgs == 3:
  880. self.doFlex()
  881. self.flexing = 0
  882. elif subrIndex == 1 and nArgs == 0:
  883. self.flexing = 1
  884. # ignore...
  885. def op_pop(self, index):
  886. pass # ignore...
  887. def doFlex(self):
  888. finaly = self.pop()
  889. finalx = self.pop()
  890. self.pop() # flex height is unused
  891. p3y = self.pop()
  892. p3x = self.pop()
  893. bcp4y = self.pop()
  894. bcp4x = self.pop()
  895. bcp3y = self.pop()
  896. bcp3x = self.pop()
  897. p2y = self.pop()
  898. p2x = self.pop()
  899. bcp2y = self.pop()
  900. bcp2x = self.pop()
  901. bcp1y = self.pop()
  902. bcp1x = self.pop()
  903. rpy = self.pop()
  904. rpx = self.pop()
  905. # call rrcurveto
  906. self.push(bcp1x + rpx)
  907. self.push(bcp1y + rpy)
  908. self.push(bcp2x)
  909. self.push(bcp2y)
  910. self.push(p2x)
  911. self.push(p2y)
  912. self.op_rrcurveto(None)
  913. # call rrcurveto
  914. self.push(bcp3x)
  915. self.push(bcp3y)
  916. self.push(bcp4x)
  917. self.push(bcp4y)
  918. self.push(p3x)
  919. self.push(p3y)
  920. self.op_rrcurveto(None)
  921. # Push back final coords so subr 0 can find them
  922. self.push(finalx)
  923. self.push(finaly)
  924. def op_dotsection(self, index):
  925. self.popall() # XXX
  926. def op_hstem3(self, index):
  927. self.popall() # XXX
  928. def op_seac(self, index):
  929. "asb adx ady bchar achar seac"
  930. from fontTools.encodings.StandardEncoding import StandardEncoding
  931. asb, adx, ady, bchar, achar = self.popall()
  932. baseGlyph = StandardEncoding[bchar]
  933. self.pen.addComponent(baseGlyph, (1, 0, 0, 1, 0, 0))
  934. accentGlyph = StandardEncoding[achar]
  935. adx = adx + self.sbx - asb # seac weirdness
  936. self.pen.addComponent(accentGlyph, (1, 0, 0, 1, adx, ady))
  937. def op_vstem3(self, index):
  938. self.popall() # XXX
  939. class T2CharString(object):
  940. operandEncoding = t2OperandEncoding
  941. operators, opcodes = buildOperatorDict(t2Operators)
  942. decompilerClass = SimpleT2Decompiler
  943. outlineExtractor = T2OutlineExtractor
  944. def __init__(self, bytecode=None, program=None, private=None, globalSubrs=None):
  945. if program is None:
  946. program = []
  947. self.bytecode = bytecode
  948. self.program = program
  949. self.private = private
  950. self.globalSubrs = globalSubrs if globalSubrs is not None else []
  951. self._cur_vsindex = None
  952. def getNumRegions(self, vsindex=None):
  953. pd = self.private
  954. assert pd is not None
  955. if vsindex is not None:
  956. self._cur_vsindex = vsindex
  957. elif self._cur_vsindex is None:
  958. self._cur_vsindex = pd.vsindex if hasattr(pd, "vsindex") else 0
  959. return pd.getNumRegions(self._cur_vsindex)
  960. def __repr__(self):
  961. if self.bytecode is None:
  962. return "<%s (source) at %x>" % (self.__class__.__name__, id(self))
  963. else:
  964. return "<%s (bytecode) at %x>" % (self.__class__.__name__, id(self))
  965. def getIntEncoder(self):
  966. return encodeIntT2
  967. def getFixedEncoder(self):
  968. return encodeFixed
  969. def decompile(self):
  970. if not self.needsDecompilation():
  971. return
  972. subrs = getattr(self.private, "Subrs", [])
  973. decompiler = self.decompilerClass(subrs, self.globalSubrs, self.private)
  974. decompiler.execute(self)
  975. def draw(self, pen, blender=None):
  976. subrs = getattr(self.private, "Subrs", [])
  977. extractor = self.outlineExtractor(
  978. pen,
  979. subrs,
  980. self.globalSubrs,
  981. self.private.nominalWidthX,
  982. self.private.defaultWidthX,
  983. self.private,
  984. blender,
  985. )
  986. extractor.execute(self)
  987. self.width = extractor.width
  988. def calcBounds(self, glyphSet):
  989. boundsPen = BoundsPen(glyphSet)
  990. self.draw(boundsPen)
  991. return boundsPen.bounds
  992. def compile(self, isCFF2=False):
  993. if self.bytecode is not None:
  994. return
  995. opcodes = self.opcodes
  996. program = self.program
  997. if isCFF2:
  998. # If present, remove return and endchar operators.
  999. if program and program[-1] in ("return", "endchar"):
  1000. program = program[:-1]
  1001. elif program and not isinstance(program[-1], str):
  1002. raise CharStringCompileError(
  1003. "T2CharString or Subr has items on the stack after last operator."
  1004. )
  1005. bytecode = []
  1006. encodeInt = self.getIntEncoder()
  1007. encodeFixed = self.getFixedEncoder()
  1008. i = 0
  1009. end = len(program)
  1010. while i < end:
  1011. token = program[i]
  1012. i = i + 1
  1013. if isinstance(token, str):
  1014. try:
  1015. bytecode.extend(bytechr(b) for b in opcodes[token])
  1016. except KeyError:
  1017. raise CharStringCompileError("illegal operator: %s" % token)
  1018. if token in ("hintmask", "cntrmask"):
  1019. bytecode.append(program[i]) # hint mask
  1020. i = i + 1
  1021. elif isinstance(token, int):
  1022. bytecode.append(encodeInt(token))
  1023. elif isinstance(token, float):
  1024. bytecode.append(encodeFixed(token))
  1025. else:
  1026. assert 0, "unsupported type: %s" % type(token)
  1027. try:
  1028. bytecode = bytesjoin(bytecode)
  1029. except TypeError:
  1030. log.error(bytecode)
  1031. raise
  1032. self.setBytecode(bytecode)
  1033. def needsDecompilation(self):
  1034. return self.bytecode is not None
  1035. def setProgram(self, program):
  1036. self.program = program
  1037. self.bytecode = None
  1038. def setBytecode(self, bytecode):
  1039. self.bytecode = bytecode
  1040. self.program = None
  1041. def getToken(self, index, len=len, byteord=byteord, isinstance=isinstance):
  1042. if self.bytecode is not None:
  1043. if index >= len(self.bytecode):
  1044. return None, 0, 0
  1045. b0 = byteord(self.bytecode[index])
  1046. index = index + 1
  1047. handler = self.operandEncoding[b0]
  1048. token, index = handler(self, b0, self.bytecode, index)
  1049. else:
  1050. if index >= len(self.program):
  1051. return None, 0, 0
  1052. token = self.program[index]
  1053. index = index + 1
  1054. isOperator = isinstance(token, str)
  1055. return token, isOperator, index
  1056. def getBytes(self, index, nBytes):
  1057. if self.bytecode is not None:
  1058. newIndex = index + nBytes
  1059. bytes = self.bytecode[index:newIndex]
  1060. index = newIndex
  1061. else:
  1062. bytes = self.program[index]
  1063. index = index + 1
  1064. assert len(bytes) == nBytes
  1065. return bytes, index
  1066. def handle_operator(self, operator):
  1067. return operator
  1068. def toXML(self, xmlWriter, ttFont=None):
  1069. from fontTools.misc.textTools import num2binary
  1070. if self.bytecode is not None:
  1071. xmlWriter.dumphex(self.bytecode)
  1072. else:
  1073. index = 0
  1074. args = []
  1075. while True:
  1076. token, isOperator, index = self.getToken(index)
  1077. if token is None:
  1078. break
  1079. if isOperator:
  1080. if token in ("hintmask", "cntrmask"):
  1081. hintMask, isOperator, index = self.getToken(index)
  1082. bits = []
  1083. for byte in hintMask:
  1084. bits.append(num2binary(byteord(byte), 8))
  1085. hintMask = strjoin(bits)
  1086. line = " ".join(args + [token, hintMask])
  1087. else:
  1088. line = " ".join(args + [token])
  1089. xmlWriter.write(line)
  1090. xmlWriter.newline()
  1091. args = []
  1092. else:
  1093. if isinstance(token, float):
  1094. token = floatToFixedToStr(token, precisionBits=16)
  1095. else:
  1096. token = str(token)
  1097. args.append(token)
  1098. if args:
  1099. # NOTE: only CFF2 charstrings/subrs can have numeric arguments on
  1100. # the stack after the last operator. Compiling this would fail if
  1101. # this is part of CFF 1.0 table.
  1102. line = " ".join(args)
  1103. xmlWriter.write(line)
  1104. def fromXML(self, name, attrs, content):
  1105. from fontTools.misc.textTools import binary2num, readHex
  1106. if attrs.get("raw"):
  1107. self.setBytecode(readHex(content))
  1108. return
  1109. content = strjoin(content)
  1110. content = content.split()
  1111. program = []
  1112. end = len(content)
  1113. i = 0
  1114. while i < end:
  1115. token = content[i]
  1116. i = i + 1
  1117. try:
  1118. token = int(token)
  1119. except ValueError:
  1120. try:
  1121. token = strToFixedToFloat(token, precisionBits=16)
  1122. except ValueError:
  1123. program.append(token)
  1124. if token in ("hintmask", "cntrmask"):
  1125. mask = content[i]
  1126. maskBytes = b""
  1127. for j in range(0, len(mask), 8):
  1128. maskBytes = maskBytes + bytechr(binary2num(mask[j : j + 8]))
  1129. program.append(maskBytes)
  1130. i = i + 1
  1131. else:
  1132. program.append(token)
  1133. else:
  1134. program.append(token)
  1135. self.setProgram(program)
  1136. class T1CharString(T2CharString):
  1137. operandEncoding = t1OperandEncoding
  1138. operators, opcodes = buildOperatorDict(t1Operators)
  1139. def __init__(self, bytecode=None, program=None, subrs=None):
  1140. super().__init__(bytecode, program)
  1141. self.subrs = subrs
  1142. def getIntEncoder(self):
  1143. return encodeIntT1
  1144. def getFixedEncoder(self):
  1145. def encodeFixed(value):
  1146. raise TypeError("Type 1 charstrings don't support floating point operands")
  1147. def decompile(self):
  1148. if self.bytecode is None:
  1149. return
  1150. program = []
  1151. index = 0
  1152. while True:
  1153. token, isOperator, index = self.getToken(index)
  1154. if token is None:
  1155. break
  1156. program.append(token)
  1157. self.setProgram(program)
  1158. def draw(self, pen):
  1159. extractor = T1OutlineExtractor(pen, self.subrs)
  1160. extractor.execute(self)
  1161. self.width = extractor.width
  1162. class DictDecompiler(object):
  1163. operandEncoding = cffDictOperandEncoding
  1164. def __init__(self, strings, parent=None):
  1165. self.stack = []
  1166. self.strings = strings
  1167. self.dict = {}
  1168. self.parent = parent
  1169. def getDict(self):
  1170. assert len(self.stack) == 0, "non-empty stack"
  1171. return self.dict
  1172. def decompile(self, data):
  1173. index = 0
  1174. lenData = len(data)
  1175. push = self.stack.append
  1176. while index < lenData:
  1177. b0 = byteord(data[index])
  1178. index = index + 1
  1179. handler = self.operandEncoding[b0]
  1180. value, index = handler(self, b0, data, index)
  1181. if value is not None:
  1182. push(value)
  1183. def pop(self):
  1184. value = self.stack[-1]
  1185. del self.stack[-1]
  1186. return value
  1187. def popall(self):
  1188. args = self.stack[:]
  1189. del self.stack[:]
  1190. return args
  1191. def handle_operator(self, operator):
  1192. operator, argType = operator
  1193. if isinstance(argType, tuple):
  1194. value = ()
  1195. for i in range(len(argType) - 1, -1, -1):
  1196. arg = argType[i]
  1197. arghandler = getattr(self, "arg_" + arg)
  1198. value = (arghandler(operator),) + value
  1199. else:
  1200. arghandler = getattr(self, "arg_" + argType)
  1201. value = arghandler(operator)
  1202. if operator == "blend":
  1203. self.stack.extend(value)
  1204. else:
  1205. self.dict[operator] = value
  1206. def arg_number(self, name):
  1207. if isinstance(self.stack[0], list):
  1208. out = self.arg_blend_number(self.stack)
  1209. else:
  1210. out = self.pop()
  1211. return out
  1212. def arg_blend_number(self, name):
  1213. out = []
  1214. blendArgs = self.pop()
  1215. numMasters = len(blendArgs)
  1216. out.append(blendArgs)
  1217. out.append("blend")
  1218. dummy = self.popall()
  1219. return blendArgs
  1220. def arg_SID(self, name):
  1221. return self.strings[self.pop()]
  1222. def arg_array(self, name):
  1223. return self.popall()
  1224. def arg_blendList(self, name):
  1225. """
  1226. There may be non-blend args at the top of the stack. We first calculate
  1227. where the blend args start in the stack. These are the last
  1228. numMasters*numBlends) +1 args.
  1229. The blend args starts with numMasters relative coordinate values, the BlueValues in the list from the default master font. This is followed by
  1230. numBlends list of values. Each of value in one of these lists is the
  1231. Variable Font delta for the matching region.
  1232. 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
  1233. the delta values. We then convert the default values, the first item in each entry, to an absolute value.
  1234. """
  1235. vsindex = self.dict.get("vsindex", 0)
  1236. numMasters = (
  1237. self.parent.getNumRegions(vsindex) + 1
  1238. ) # only a PrivateDict has blended ops.
  1239. numBlends = self.pop()
  1240. args = self.popall()
  1241. numArgs = len(args)
  1242. # The spec says that there should be no non-blended Blue Values,.
  1243. assert numArgs == numMasters * numBlends
  1244. value = [None] * numBlends
  1245. numDeltas = numMasters - 1
  1246. i = 0
  1247. prevVal = 0
  1248. while i < numBlends:
  1249. newVal = args[i] + prevVal
  1250. prevVal = newVal
  1251. masterOffset = numBlends + (i * numDeltas)
  1252. blendList = [newVal] + args[masterOffset : masterOffset + numDeltas]
  1253. value[i] = blendList
  1254. i += 1
  1255. return value
  1256. def arg_delta(self, name):
  1257. valueList = self.popall()
  1258. out = []
  1259. if valueList and isinstance(valueList[0], list):
  1260. # arg_blendList() has already converted these to absolute values.
  1261. out = valueList
  1262. else:
  1263. current = 0
  1264. for v in valueList:
  1265. current = current + v
  1266. out.append(current)
  1267. return out
  1268. def calcSubrBias(subrs):
  1269. nSubrs = len(subrs)
  1270. if nSubrs < 1240:
  1271. bias = 107
  1272. elif nSubrs < 33900:
  1273. bias = 1131
  1274. else:
  1275. bias = 32768
  1276. return bias