def test_create_path(self): """ Test creating path commands from SBody objects. """ shapes = [ shape.Line((10, 10), (50, 10)), shape.BezierCurve((70, 10), (80, 30), (50, 10), (80, 40)), shape.BezierCurve((80, 50), (70, 70), (80, 40), (50, 70)), shape.Line((50, 70), (10, 70)), ] self.assertEquals(self.geda_writer._create_path(shapes), [ 'H 3 10 0 0 -1 -1 0 -1 -1 -1 -1 -1 5', 'M 100,100', 'L 500,100', 'C 700,100 800,300 800,400', 'C 800,500 700,700 500,700', 'L 100,700', ]) shapes.append(shape.Line((10, 70), (10, 10))) self.assertEquals(self.geda_writer._create_path(shapes), [ 'H 3 10 0 0 -1 -1 0 -1 -1 -1 -1 -1 6', 'M 100,100', 'L 500,100', 'C 700,100 800,300 800,400', 'C 800,500 700,700 500,700', 'L 100,700', 'z', ])
def test_is_valid_path(self): """ Tests if SBody objects contain valid paths.""" shapes = [ shape.Line((10, 10), (50, 10)), #L 500,100 shape.BezierCurve((70, 10), (80, 30), (50, 10), (80, 40)), #C 700,100 800,300 800,400 shape.BezierCurve((80, 50), (70, 70), (80, 40), (50, 70)), #C 800,500 700,700 500,700 shape.Line((50, 70), (10, 70)), #L 100,700 ] body = components.SBody() body.shapes = shapes self.assertTrue(self.geda_writer.is_valid_path(body)) body.add_shape(shape.Line((10, 70), (10, 10))) self.assertTrue(self.geda_writer.is_valid_path(body)) shapes = [ shape.Line((10, 10), (50, 10)), #L 500,100 shape.BezierCurve((70, 10), (80, 30), (50, 10), (80, 40)), #C 700,100 800,300 800,400 shape.Line((50, 70), (10, 70)), #L 100,700 ] body.shapes = shapes self.assertFalse(self.geda_writer.is_valid_path(body)) body.add_shape(shape.Circle(0, 0, 10)) self.assertFalse(self.geda_writer.is_valid_path(body))
def test_convert_bezier(self): """ Tests converting BezierCurve objects to path commands. """ curve = shape.BezierCurve((9, -10), (11, -10), (3, -12), (17, -12)) self.assertEquals(self.geda_writer._convert_bezier(curve), [ 'H 3 10 0 0 -1 -1 0 -1 -1 -1 -1 -1 2', 'M 30,-120', 'C 90,-100 110,-100 170,-120', ])
def _parse_H(self, stream, params): """ Parses a SVG-like path provided path into a list of simple shapes. The gEDA formats allows only line and curve segments with absolute coordinates. Hence, shapes are either Line or BezierCurve objects. The method processes the stream data according to the number of lines in *params*. Returns a list of Line and BezierCurve shapes. """ params['extra_id'] = self.path_counter.next() num_lines = params['num_lines'] mirrored = self._is_mirrored_command(params) command = stream.readline().strip().split(self.DELIMITER) if command[0] != 'M': raise GEDAError('found invalid path in gEDA file') def get_coords(string, mirrored): """ Get coordinates from string with comma-sparated notation.""" x, y = [int(value) for value in string.strip().split(',')] if mirrored: x = -x return (self.x_to_px(x), self.y_to_px(y)) shapes = [] current_pos = initial_pos = (get_coords(command[1], mirrored)) ## loop over the remaining lines of commands (after 'M') for _ in range(num_lines - 1): command = stream.readline().strip().split(self.DELIMITER) ## draw line from current to given position if command[0] == 'L': assert (len(command) == 2) end_pos = get_coords(command[1], mirrored) shape_ = shape.Line(current_pos, end_pos) current_pos = end_pos ## draw curve from current to given position elif command[0] == 'C': assert (len(command) == 4) control1 = get_coords(command[1], mirrored) control2 = get_coords(command[2], mirrored) end_pos = get_coords(command[3], mirrored) shape_ = shape.BezierCurve(control1, control2, current_pos, end_pos) current_pos = end_pos ## end of sub-path, straight line from current to initial position elif command[0] in ['z', 'Z']: shape_ = shape.Line(current_pos, initial_pos) else: raise GEDAError("invalid command type in path '%s'" % command[0]) ## store style parameters in shape's style dict self._save_parameters_to_object(shape_, params) shapes.append(shape_) return shapes