def __generate_horizontal_line_to(self, command, coords=CoordinateSystem.ABSOLUTE): """Generates string output for horizontal line command""" end_point = Point(command[0], self.__current_pos.y) if coords == CoordinateSystem.RELATIVE: end_point = self.__current_pos + Point(command[0], 0.0) elif coords != CoordinateSystem.ABSOLUTE: raise WrongCoordinateSystem() self.__update_current_pos(end_point) return self.__print_add_line(end_point)
def __generate_quadratic_curve(self, command, coords=CoordinateSystem.ABSOLUTE): """Generates string output for quadratic curve command""" control_point = Point(command[0], command[1]) end_point = Point(command[2], command[3]) if coords == CoordinateSystem.RELATIVE: control_point = self.__current_pos + control_point end_point = self.__current_pos + end_point elif coords != CoordinateSystem.ABSOLUTE: raise WrongCoordinateSystem() self.__update_current_pos(end_point) self.__update_last_quadratic_control_point(control_point) return self.__print_quadratic(end_point, control_point)
def __generate_cubic_smooth_curve(self, command, coords=CoordinateSystem.ABSOLUTE): """Generates string output for cubic smooth curve command""" first_control_point = 2 * self.__current_pos - self.__last_cubic_control_point second_control_point = Point(command[0], command[1]) end_point = Point(command[2], command[3]) if coords == CoordinateSystem.RELATIVE: second_control_point = self.__current_pos + second_control_point end_point = self.__current_pos + end_point elif coords != CoordinateSystem.ABSOLUTE: raise WrongCoordinateSystem() self.__update_current_pos(end_point) self.__update_last_cubic_control_point(second_control_point) return self.__print_cubic(end_point, first_control_point, second_control_point)
def __generate_elliptical_curve(self, command, coords=CoordinateSystem.ABSOLUTE): """Generates string output for elliptical curve command""" if command[0] != command[1]: raise NotImplementedError("Sorry, but a/A command with different x/y radius not implemented yet.") radius = command[0] # angle = command[2] # large_arc_flag = command[3] sweep_flag = 'true' if command[4] == 1 else 'false' end_point = Point(command[5], command[6]) if coords == CoordinateSystem.RELATIVE: end_point = self.__current_pos + end_point elif coords != CoordinateSystem.ABSOLUTE: raise WrongCoordinateSystem() center_point = (end_point + self.__current_pos) / 2 self.__update_current_pos(end_point) return self.__print_elliptical( center_point, radius, 0.0, 360.0, sweep_flag)
def __generate_move_to(self, command, coords=CoordinateSystem.ABSOLUTE): """Generates string output for move commands""" end_point = Point(command[0], command[1]) if coords == CoordinateSystem.RELATIVE: end_point = self.__current_pos + end_point elif coords != CoordinateSystem.ABSOLUTE: raise WrongCoordinateSystem() self.__update_current_pos(end_point) return self.__print_move_to(end_point)
def __init__(self): """Setup generator""" self.__parser = Parser() self.__current_pos = Point(0.0, 0.0) self.__last_cubic_control_point = None self.__last_quadratic_control_point = None self.__command_map = { 'm': self.__generate_move_to, 'l': self.__generate_line_to, 'h': self.__generate_horizontal_line_to, 'v': self.__generate_vertical_line_to, 'c': self.__generate_cubic_curve, 's': self.__generate_cubic_smooth_curve, 'q': self.__generate_quadratic_curve, 't': self.__generate_quadratic_smooth_curve, 'a': self.__generate_elliptical_curve, 'z': self.__generate_close }