psCharStrings.py 42 KB

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