from struct import pack from svgpathtools import * import math import random def encodeU8(num): return pack(" 63: self.type = Stitch.TYPE_LONG else: self.type = Stitch.TYPE_SHORT self.encodePoint(self.point - Stitch.lastPoint, b) Stitch.lastPoint = complex( int(round(self.point.real)), int(round(self.point.imag))) def encodePoint(self, point, b): self.encodeCoordinate(point.real, b) self.encodeCoordinate(point.imag, b) def encodeCoordinate(self, coordinate, b): if coordinate > 2047 or coordinate < -2047: raise Exception("Coordinate movement too big: {}".format(coordinate)) if self.type == Stitch.TYPE_SHORT: b.extend([ (int(round(coordinate)) & 0x7F) ]) else: total = self.type + ( int(round(coordinate)) & 0xFFF ) b.extend([ ((total & 0xFF00) >> 8), total & 0xFF]) def encodeForCSewSeg(self, b): b.extend(encodeS16(self.point.real)) b.extend(encodeS16(self.point.imag)) def length(self): return self.line.length() # Flips the start and end points of a stitch def reverse(self): self.line = Line(start=self.line.end, end=self.line.start) class ColorChange: TYPE_COLOR_CHANGE_left = 0xFE TYPE_COLOR_CHANGE_right = 0xB0 def __init__(self, colorIndex, indexInColorList): self.colorIndex = colorIndex self.indexInColorList = indexInColorList def encode(self, b): b.extend([ ColorChange.TYPE_COLOR_CHANGE_left ]) b.extend([ ColorChange.TYPE_COLOR_CHANGE_right ]) b.extend([ (self.indexInColorList-1) & 0xFF ])