1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# -*- coding: utf-8 -*-
from fontTools.misc.textTools import num2binary
from fontTools.ttLib.tables.ttProgram import streamOpcodeDict, opcodeDict
from io import BytesIO
class InstructionStream(object):
"""
:param program_bytes: The program bytecode.
:type program_bytes: bytes
The instruction stream.
"""
def __init__(self, instruction_processor=None, program_bytes=b"") -> None:
self.ip = instruction_processor
self.io = BytesIO(program_bytes)
self._num_bytes = len(program_bytes)
def __len__(self):
return self._num_bytes
def __repr__(self) -> str:
"""
Return the instructions from the bytecode in the current stream
starting at the beginning.
"""
return self.get_assembly()
def __str__(self) -> str:
"""
Return the instructions from the bytecode in the current stream
starting at the beginning.
"""
return self.get_assembly()
def move_instruction_pointer(self, bytes_offset: int) -> None:
"""
:param bytes_offset: The offset in bytes. May be positive or negative.
:type bytes_offset: int
Move the instruction pointer inside the current stream, relative to the
current pointer position.
"""
self.io.seek(bytes_offset, 1) # 1 = relative to current position
def read_byte(self):
"""
Read a byte from the instruction stream and advance the instruction
pointer. Returns the value as a tuple of (byte, int).
"""
b = self.io.read(1)
if not b:
return False
return b, int.from_bytes(b, byteorder="big", signed=False)
def read_word(self):
"""
Read a word from the instruction stream and advance the instruction
pointer. Returns the value as a tuple of (word, int).
"""
w = self.io.read(2)
if not w:
return False
return w, int.from_bytes(w, byteorder="big", signed=True)
def rewind(self) -> None:
"""
Rewind the instruction pointer to the beginning of the stream.
"""
self.io.seek(0)
# Getting the assembly code
@property
def vtt_assembly(self) -> str:
"""
Return the instructions from the bytecode in the current stream as VTT
assembly code.
"""
return self.get_assembly(dialect="vtt", end="\n")
def get_assembly(self, dialect="ttx", end="\n") -> str:
"""
Return the instructions from the bytecode in the current stream as
assembly code in the specified dialect, "ttx" or "vtt".
"""
vtt = dialect == "vtt"
ttx = dialect == "ttx"
self.rewind()
asm = ""
indent = 0
while True:
opcode = self.io.read(1)
if not opcode:
asm = asm.strip()
if ttx:
return asm
elif vtt:
if asm:
return f"#PUSHOFF{end}" + asm.strip() + f"{end}#PUSHON"
return ""
else:
# Unknown dialect
raise NotImplementedError
opcode = int.from_bytes(opcode, byteorder="big", signed=False)
cmd_info = streamOpcodeDict.get(opcode, None)
if cmd_info is None:
cmd_info = opcodeDict.get(opcode, None)
if cmd_info is None:
print(
asm + "\n"
"Illegal opcode 0x%02x at offset 0x%04x."
% (int(opcode), self.io.tell(),)
)
raise KeyError
cmd_name, arg_bits, base_opcode, name = cmd_info
args = []
if cmd_name in ("EIF", "ELSE", "ENDF"):
indent -= 1
if cmd_name in ("NPUSHB", "NPUSHW", "PUSHB", "PUSHW"):
# PUSH instructions read their arguments from the stream
if cmd_name.startswith("PUSH"):
# Take number of arguments from the opcode
num_args = opcode - base_opcode + 1
else:
# Take number of arguments from the stream
_, num_args = self.read_byte()
if cmd_name.endswith("B"):
for n in range(num_args):
_, i = self.read_byte()
args.append(str(i))
else:
for n in range(num_args):
_, i = self.read_word()
args.append(str(i))
arg_bits = 0 # Don't output bits for push instructions
if arg_bits == 0:
if ttx:
arg_bitstring = " "
else:
arg_bitstring = ""
else:
if ttx:
arg_bitstring = num2binary(opcode - base_opcode, arg_bits)
elif vtt:
arg_bitstring = self.bitstring_to_mnemonic(
cmd_name, num2binary(opcode - base_opcode, arg_bits)
)
else:
# Unknown dialect
raise NotImplementedError
if ttx:
if cmd_name in ("NPUSHB", "NPUSHW", "PUSHB", "PUSHW"):
num_args = len(args)
val = "value" if num_args == 1 else "values"
asm += (
f"\n{' ' * indent}{cmd_name}[{arg_bitstring}]"
f"\t/* {num_args} {val} pushed */"
)
else:
asm += (
f"\n{' ' * indent}{cmd_name}[{arg_bitstring}]"
f"\t/* {name} */"
)
if args:
asm += f"\n{' ' * indent}{' '.join(args)}"
elif vtt:
if cmd_name in ("NPUSHB", "NPUSHW", "PUSHB", "PUSHW"):
# Format as generic #PUSH for VTT assembly output
cmd_name = "#PUSH"
asm += f"{end}{' ' * indent}{cmd_name}, {', '.join(args)}"
elif cmd_name in ("JMPR", "JROF"):
# Special formatting for jump instructions
if cmd_name == "JPMR":
args = ("*",)
elif cmd_name == "JROF":
args = ("*", "*")
asm += f"{end}#PUSHON"
asm += f"{end}{' ' * indent}{cmd_name}, {', '.join(args)}"
asm += f"{end}#PUSHOFF"
else:
asm += (
f"{end}{' ' * indent}{cmd_name}[{arg_bitstring}]"
f"\t/* {name} */"
)
else:
# Unknown dialect
raise NotImplementedError
if cmd_name in ("ELSE", "FDEF", "IF"):
indent += 1
def bitstring_to_mnemonic(self, cmd_name: str, bitstring: str) -> str:
"""
Return VTT mnemonics for a bit string
"""
if cmd_name in ("SVTCA", "SPVTCA", "SFVTCA", "IUP"):
# Direction
if bitstring == "0":
return "Y" # Y axis
return "X" # X axis
elif cmd_name in ("SPVTL", "SFVTL", "SDPVTL"):
# Line relation
if bitstring == "0":
return "r" # parallel to line
return "R" # perpendicular to line
elif cmd_name in ("MDAP", "MIAP"):
# Rounding
if bitstring == "0":
return "r" # do not round distance
return "R" # round distance
elif cmd_name in ("SHP", "SHC", "SHZ"):
# Reference Point Usage
if bitstring == "0":
return "2" # Use rp2
return "1" # Use rp1
elif cmd_name in ("MSIRP",):
# Reference Point Autoset
if bitstring == "0":
return "m" # Do not set rp0
return "M" # Set rp0 to point number on the stack
elif cmd_name in ("GC", "MD"):
# Outline
if bitstring == "0":
return "N" # Use gridfitted outline
return "O" # Use original outline
elif cmd_name in ("ROUND", "NROUND"):
# Color
return self.bitstring_to_color_mnemonic(bitstring)
elif cmd_name in ("MDRP", "MIRP"):
flags = ""
# Reference Point Autoset
if bitstring[0] == "0":
flags += "m"
else:
flags += "M"
# Minimum Distance
if bitstring[1] == "0":
flags += "<"
else:
flags += ">"
# Rounding
if bitstring[2] == "0":
flags += "r" # do not round distance
else:
flags += "R" # round distance
# Color
return flags + self.bitstring_to_color_mnemonic(bitstring[3:])
# Unknown command
raise KeyError
def bitstring_to_color_mnemonic(self, bitstring: str) -> str:
"""
Return VTT distance color mnemonics for a bit string
"""
if bitstring == "00":
return "Gr" # Gray
elif bitstring == "01":
return "Bl" # Black
elif bitstring == "10":
return "Wh" # White
# "11" is not defined
raise KeyError
|