Beispiel #1
0
 def test_shape(self):
     beam = Beam(
         (Mm(1), Mm(2)),
         self.left_parent,
         (Mm(3), Mm(4)),
         self.right_parent,
     )
     thickness = beam.beam_thickness
     assert len(beam.elements) == 5
     assert_path_els_equal(beam.elements[0],
                           MoveTo(Point(Mm(0), Mm(0)), beam))
     assert_path_els_equal(beam.elements[1],
                           LineTo(Point(Mm(3), Mm(4)), self.right_parent))
     assert_path_els_equal(
         beam.elements[2],
         LineTo(
             Point(Mm(3),
                   Mm(4) + thickness),
             self.right_parent,
         ),
     )
     assert_path_els_equal(
         beam.elements[3],
         LineTo(
             Point(Mm(0), thickness),
             beam,
         ),
     )
     assert_path_els_equal(
         beam.elements[4],
         MoveTo(
             Point(Mm(0), Mm(0)),
             beam,
         ),
     )
Beispiel #2
0
 def test_close_subpath(self):
     path = Path((Unit(5), Unit(6)))
     path.line_to(Unit(10), Unit(10))
     path.line_to(Unit(10), Unit(100))
     path.close_subpath()
     assert len(path.elements) == 4
     assert_path_els_equal(path.elements[3], MoveTo(ORIGIN, path))
Beispiel #3
0
 def test_move_to_with_parent(self):
     path = Path(ORIGIN)
     parent = InvisibleObject((Unit(100), Unit(50)))
     path.move_to(Unit(10), Unit(11), parent)
     assert len(path.elements) == 1
     assert_path_els_equal(
         path.elements[0], MoveTo(Point(Unit(10), Unit(11)), parent)
     )
Beispiel #4
0
 def test_line_to(self):
     path = Path((Unit(5), Unit(6)))
     path.line_to(Unit(10), Unit(12))
     assert len(path.elements) == 2
     assert_path_els_equal(path.elements[0], MoveTo(ORIGIN, path))
     assert_path_els_equal(path.elements[1], LineTo(Point(Unit(10), Unit(12)), path))
     resolved_els = path._resolve_path_elements()
     assert resolved_els == [
         ResolvedMoveTo(ZERO, ZERO),
         ResolvedLineTo(Unit(10), Unit(12)),
     ]
Beispiel #5
0
    def move_to(self, x: Unit, y: Unit, parent: Optional[Parent] = None):
        """Close the current sub-path and start a new one.

        A point parent may be passed as well, anchored the target point to
        a separate `GraphicObject`. In this case, the coordinates passed will be
        considered relative to the parent.

        Args:
            x: The end x position
            y: The end y position
            parent: An optional parent, whose position the target coordinate will
                be relative to.
        """
        self.elements.append(MoveTo(Point(x, y), parent or self))
Beispiel #6
0
    def cubic_to(
        self,
        control_1_x: Unit,
        control_1_y: Unit,
        control_2_x: Unit,
        control_2_y: Unit,
        end_x: Unit,
        end_y: Unit,
        control_1_parent: Optional[Parent] = None,
        control_2_parent: Optional[Parent] = None,
        end_parent: Optional[Parent] = None,
    ):
        """Draw a cubic bezier curve from the current position to a new point.

        If the path is empty, this will add two elements, an initial
        `MoveTo(Point(Unit(0), Unit(0)), self)` and the requested
        `CurveTo`.

        Args:
            control_1_x: The x coordinate of the first control point.
            control_1_y: The y coordinate of the first control point.
            control_2_x: The x coordinate of the second control point.
            control_2_y: The y coordinate of the second control point.
            end_x: The x coordinate of the curve target.
            end_y: The y coordinate of the curve target.
            control_1_parent: An optional parent for
                the first control point. Defaults to `self`.
            control_2_parent: An optional parent for
                the second control point. Defaults to `self`.
            end_parent: An optional parent for the
                curve target. Defaults to `self`.
        """
        c1 = ControlPoint(
            Point(control_1_x, control_1_y),
            control_1_parent or self,
        )
        c2 = ControlPoint(
            Point(control_2_x, control_2_y),
            control_2_parent or self,
        )
        if not len(self.elements):
            # Needed to ensure bounding rect / length calculations are correct
            self.elements.append(MoveTo(Point(Unit(0), Unit(0)), self))
        self.elements.append(CurveTo(c1, c2, Point(end_x, end_y), end_parent or self))
Beispiel #7
0
 def test_cubic_to_with_no_parents(self):
     path = Path((Unit(5), Unit(6)))
     path.cubic_to(Unit(10), Unit(11), ZERO, Unit(1), Unit(5), Unit(6))
     assert len(path.elements) == 2
     assert_path_els_equal(path.elements[0], MoveTo(ORIGIN, path))
     assert_path_els_equal(
         path.elements[1],
         CurveTo(
             ControlPoint(Point(Unit(10), Unit(11)), path),
             ControlPoint(Point(ZERO, Unit(1)), path),
             Point(Unit(5), Unit(6)),
             path,
         ),
     )
     resolved_els = path._resolve_path_elements()
     assert resolved_els == [
         ResolvedMoveTo(ZERO, ZERO),
         ResolvedCurveTo(Unit(10), Unit(11), ZERO, Unit(1), Unit(5), Unit(6)),
     ]
Beispiel #8
0
    def line_to(self, x: Unit, y: Unit, parent: Optional[Parent] = None):
        """Draw a path from the current position to a new point.

        A point parent may be passed as well, anchored the target point to
        a separate GraphicObject. In this case, the coordinates passed will be
        considered relative to the parent.

        If the path is empty, this will add two elements, an initial
        `MoveTo(Point(Unit(0), Unit(0)), self)` and the requested
        `LineTo`.

        Args:
            x: The end x position
            y: The end y position
            parent: An optional parent, whose position the target coordinate
                will be relative to.
        """
        if not len(self.elements):
            # Needed to ensure bounding rect / length calculations are correct
            self.elements.append(MoveTo(Point(Unit(0), Unit(0)), self))
        self.elements.append(LineTo(Point(x, y), parent or self))
Beispiel #9
0
 def test_cubic_to_with_parents(self):
     path = Path((Unit(Unit(100)), Unit(Unit(200))))
     parent_1 = InvisibleObject((Unit(Unit(10)), Unit(Unit(20))))
     parent_2 = InvisibleObject((Unit(Unit(30)), Unit(Unit(40))))
     parent_3 = InvisibleObject((Unit(Unit(50)), Unit(Unit(60))))
     path.cubic_to(
         Unit(1),
         Unit(2),
         Unit(3),
         Unit(4),
         Unit(5),
         Unit(6),
         parent_1,
         parent_2,
         parent_3,
     )
     assert len(path.elements) == 2
     assert_path_els_equal(path.elements[0], MoveTo(ORIGIN, path))
     assert_path_els_equal(
         path.elements[1],
         CurveTo(
             ControlPoint(Point(Unit(1), Unit(2)), parent_1),
             ControlPoint(Point(Unit(3), Unit(4)), parent_2),
             Point(Unit(5), Unit(6)),
             parent_3,
         ),
     )
     resolved_els = path._resolve_path_elements()
     assert resolved_els == [
         ResolvedMoveTo(ZERO, ZERO),
         ResolvedCurveTo(
             Unit(10 + 1 - 100),
             Unit(20 + 2 - 200),
             Unit(30 + 3 - 100),
             Unit(40 + 4 - 200),
             Unit(50 + 5 - 100),
             Unit(60 + 6 - 200),
         ),
     ]
Beispiel #10
0
 def test_straight_line(self):
     path = Path.straight_line((Unit(5), Unit(6)), (Unit(10), Unit(11)))
     assert path.pos == Point(Unit(5), Unit(6))
     assert len(path.elements) == 2
     assert_path_els_equal(path.elements[0], MoveTo(ORIGIN, path))
     assert_path_els_equal(path.elements[1], LineTo(Point(Unit(10), Unit(11)), path))
Beispiel #11
0
 def test_move_to_with_no_parent(self):
     path = Path((Unit(5), Unit(6)))
     path.move_to(Unit(10), Unit(11))
     assert len(path.elements) == 1
     assert_path_els_equal(path.elements[0], MoveTo(Point(Unit(10), Unit(11)), path))