#!/usr/bin/env python

import table

def operator_P(input):
	return input < 28 or input in [29,30,31]

# input == 12: extra operator

operators_by_opcode = {
	21: "rmoveto",
	22: "hmoveto",
	4: "vmoveto",
	5: "rlineto",
	6: "hlineto",
	7: "vlineto",
	8: "rrcurveto",
	27: "hhcurveto",
	31: "hvcurveto",
	24: "rcurveline",
	25: "rlinecurve",
	30: "vhcurveto",
	26: "vvcurveto",
	14: "endchar",
	1: "hstem",
	3: "vstem",
	18: "hstemhm",
	23: "vstemhm",
	10: "callsubr",
	29: "callgsubr",
	11: "return",
	#19: "hintmask0", # doubtful
	#20 cntrmask # doubtful
	#hintmask 19+mask
	#cntrmask 20+mask
}

extended_operators_by_opcode = {
	35: "flex",
	34: "hflex",
	36: "hflex1",
	37: "flex1",
	9: "abs",
	10: "add",
	11: "sub",
	12: "div",
	14: "neg",
	23: "random",
	24: "mul",
	26: "sqrt",
	18: "drop",
	28: "exch",
	29: "index",
	30: "roll",
	27: "dup",
	20: "put",
	21: "get",
	3: "and",
	4: "or",
	5: "not",
	15: "eq",
	22: "ifelse",
}

class CharString(table.Table):
	definition = []
	extra_field_names = ["operations"]
	def read_remainder_from_stream(self, stream):
		table.Table.read_remainder_from_stream(self, stream)
		#print "-----------------------------"
		operands = []
		self.operations = []
		while True:
			b0 = stream.read(1)
			if b0 == "": # EOF
				break
			b0 = ord(b0)
			if operator_P(b0):
				if b0 == 12: # extra
					b1 = ord(stream.read(1))
				else:
					pass
				name = operators_by_opcode[b0]
				#print name, operands
				self.operations.append((name, operands))
				operands = []
			else:
				operand = table.read_type_2_operand(stream, b0)
				operands.append(operand)

#if __name__ == "__main__":
