Пример #1
0
 def test_init(self):
     mock_parent = InvisibleObject(ORIGIN, parent=None)
     test_pen = Pen("#eeeeee")
     test_brush = Brush("#dddddd")
     path = Path((Unit(5), Unit(6)), test_pen, test_brush, mock_parent)
     assert path.pos == Point(Unit(5), Unit(6))
     assert path.pen == test_pen
     assert path.brush == test_brush
Пример #2
0
 def test_length_no_parents(self):
     spanner = MockSpanner2D(
         Point(Unit(1), Unit(2)),
         neoscore.document.pages[0],
         Point(Unit(5), Unit(7)),
         neoscore.document.pages[0],
     )
     # math.sqrt(((5-1)**2) + ((7-2)**2))
     assert_almost_equal(spanner.spanner_2d_length, Unit(6.4031242374328485))
Пример #3
0
 def test__ge__(self):
     assert Unit(1) >= Unit(0)
     assert Unit(1) >= Unit(1)
     assert not Unit(1) >= Unit(2)
     assert Unit(1) >= MockUnit(0.4)
     assert Unit(1) >= MockUnit(0.5)
     assert not Unit(1) >= MockUnit(0.6)
     with pytest.raises(AttributeError):
         Unit(1) >= 1
Пример #4
0
 def test_init(self):
     mock_parent = InvisibleObject((Unit(10), Unit(11)), parent=None)
     test_object = Text((Unit(5), Unit(6)), "testing", self.font,
                        mock_parent)
     assert test_object.x == GraphicUnit(5)
     assert test_object.y == GraphicUnit(6)
     assert test_object.text == "testing"
     assert test_object.font == self.font
     assert test_object.parent == mock_parent
Пример #5
0
 def test_init(self):
     mock_parent = InvisibleObject((Unit(10), Unit(11)), parent=self.staff)
     test_object = MusicText((Unit(5), Unit(6)), "accidentalFlat",
                             mock_parent, self.font)
     assert test_object.x == GraphicUnit(5)
     assert test_object.y == GraphicUnit(6)
     assert test_object.text == "\ue260"
     assert test_object.font == self.font
     assert test_object.parent == mock_parent
Пример #6
0
 def test_create_qt_path_with_move(self):
     qt_path = PathInterface.create_qt_path(
         [ResolvedMoveTo(Unit(10), Unit(12))])
     assert qt_path.elementCount() == 1
     el_0 = qt_path.elementAt(0)
     assert el_0.isMoveTo()
     assert el_0.x == 10
     assert el_0.y == 12
     assert qt_path.currentPosition().x() == 10
     assert qt_path.currentPosition().y() == 12
Пример #7
0
 def end_y_settable(self):
     spanner = MockSpanner2D(
         Point(Unit(0), Unit(1)), None, Point(Unit(2), Unit(3)), None
     )
     assert spanner.end_pos == Point(Unit(2), Unit(3))
     spanner.end_y = Unit(10)
     assert spanner.end_pos == Point(Unit(2), Unit(10))
Пример #8
0
 def test__sub__(self):
     assert isinstance(Unit(1) - Unit(2), Unit)
     assert isinstance(Unit(1) - MockUnit(2), Unit)
     assert Unit(1) - Unit(2) == Unit(-1)
     assert Unit(1) - MockUnit(2) == Unit(-3)
     with pytest.raises(AttributeError):
         Unit(1) + 2
Пример #9
0
 def test__add__(self):
     assert isinstance(Unit(1) + Unit(2), Unit)
     assert isinstance(Unit(1) + MockUnit(2), Unit)
     assert Unit(1) + Unit(2) == Unit(3)
     assert Unit(1) + MockUnit(2) == Unit(5)
     with pytest.raises(AttributeError):
         Unit(1) + 2
Пример #10
0
 def test_length_with_parents(self):
     parent_1 = InvisibleObject((Unit(1), Unit(2)), None)
     parent_2 = InvisibleObject((Unit(11), Unit(12)), None)
     spanner = MockSpanner2D(
         Point(Unit(1), Unit(2)), parent_1, Point(Unit(4), Unit(5)), parent_2
     )
     # math.sqrt(((15-2)**2) + ((17-4)**2))
     assert_almost_equal(spanner.spanner_2d_length, Unit(18.384776310850235))
Пример #11
0
 def test_length_with_self_parent(self):
     parent = MockSpanner2D(
         Point(Unit(1), Unit(2)), None, Point(Unit(0), Unit(0)), None
     )
     spanner = MockSpanner2D(
         Point(Unit(3), Unit(7)), parent, Point(Unit(4), Unit(5)), None
     )
     # math.sqrt((4**2) + (5**2))
     assert_almost_equal(spanner.spanner_2d_length, Unit(6.4031242374328485))
Пример #12
0
 def test_descendants(self):
     root = InvisibleObject((Unit(0), Unit(0)))
     child_1 = InvisibleObject((Unit(0), Unit(0)), parent=root)
     child_2 = InvisibleObject((Unit(0), Unit(0)), parent=root)
     subchild_1 = InvisibleObject((Unit(0), Unit(0)), parent=child_2)
     subchild_2 = InvisibleObject((Unit(0), Unit(0)), parent=child_2)
     descendants = list(root.descendants)
     descendants_set = set(descendants)
     # Assert no duplicates were yielded
     assert len(descendants) == len(descendants_set)
     # Assert root itself was not in the descendants list
     assert root not in descendants_set
     # Assert descendants content
     assert {child_1, child_2, subchild_1, subchild_2} == descendants_set
Пример #13
0
 def test_interface_generation(self):
     pen = Pen(
         Color("#eeddcc"),
         Unit(2),
         PenPattern.DASH,
         PenJoinStyle.MITER,
         PenCapStyle.ROUND,
     )
     assert pen.interface == PenInterface(
         Color("#eeddcc"),
         Unit(2),
         PenPattern.DASH,
         PenJoinStyle.MITER,
         PenCapStyle.ROUND,
     )
Пример #14
0
 def test_create_qt_path_with_line(self):
     qt_path = PathInterface.create_qt_path(
         [ResolvedLineTo(Unit(10), Unit(12))])
     assert qt_path.elementCount() == 2
     # Note how an initial line-to implicitly adds a move-to at the origin
     el_0 = qt_path.elementAt(0)
     assert el_0.isMoveTo()
     assert el_0.x == 0
     assert el_0.y == 0
     el_1 = qt_path.elementAt(1)
     assert el_1.isLineTo()
     assert el_1.x == 10
     assert el_1.y == 12
     assert qt_path.currentPosition().x() == 10
     assert qt_path.currentPosition().y() == 12
Пример #15
0
 def test_map_between_with_common_parent(self):
     parent = InvisibleObject((Unit(5), Unit(6)),
                              neoscore.document.pages[0])
     source = InvisibleObject((Unit(1), Unit(2)), parent)
     destination = InvisibleObject((Unit(3), Unit(10)), parent)
     relative_pos = map_between(source, destination)
     expected = Point(Unit(2), Unit(8))
     assert_almost_equal(relative_pos, expected)
Пример #16
0
 def test__eq__(self):
     p1 = Point(Unit(5), Unit(6))
     p2 = Point(Unit(5), Unit(6))
     p3 = Point(Unit(5), Unit(1234))
     p4 = Point(Unit(1234), Unit(6))
     assert p1 == p2
     assert p1 != p3
     assert p1 != p4
Пример #17
0
 def test_line_to_with_parent(self):
     path = Path((Unit(5), Unit(6)))
     parent = InvisibleObject((Unit(100), Unit(50)))
     path.line_to(Unit(1), Unit(3), parent)
     assert path.elements[-1].parent == parent
     resolved_els = path._resolve_path_elements()
     assert resolved_els == [
         ResolvedMoveTo(ZERO, ZERO),
         ResolvedLineTo(Unit(100 + 1 - 5), Unit(50 + 3 - 6)),
     ]
Пример #18
0
 def test__lt__(self):
     assert Unit(1) < Unit(2)
     assert not Unit(1) < Unit(0)
     assert Unit(1) < MockUnit(1)
     assert not Unit(1) < MockUnit(0.5)
     with pytest.raises(AttributeError):
         Unit(1) < 5
Пример #19
0
 def test_setters_update_interface(self):
     pen = Pen(
         Color("#eeddcc"),
         Unit(2),
         PenPattern.DASH,
         PenJoinStyle.MITER,
         PenCapStyle.ROUND,
     )
     pen.color = Color("#ffffff")
     assert pen.interface.color == Color("#ffffff")
     pen.thickness = Unit(1)
     assert pen.interface.thickness == Unit(1)
     pen.pattern = PenPattern.SOLID
     assert pen.interface.pattern == PenPattern.SOLID
     pen.join_style = PenJoinStyle.MITER
     assert pen.interface.join_style == PenJoinStyle.MITER
     pen.cap_style = PenCapStyle.ROUND
     assert pen.interface.cap_style == PenCapStyle.ROUND
Пример #20
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)),
     ]
Пример #21
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))
Пример #22
0
    def length(self) -> Unit:
        """The breakable length of the path.

        This is calculated automatically from path contents. By extension,
        this means that by default all `Path` objects will automatically
        wrap in `Flowable`s.
        """
        # Find the positions of every path element relative to the path
        min_x = Unit(float("inf"))
        max_x = Unit(-float("inf"))
        for element in self.elements:
            # Determine element X relative to self
            relative_x = map_between_x(self, element)
            # Now update min/max accordingly
            if relative_x > max_x:
                max_x = relative_x
            if relative_x < min_x:
                min_x = relative_x
        return max_x - min_x
Пример #23
0
    def test_map_between(self):
        source = InvisibleObject((Unit(5), Unit(6)),
                                 neoscore.document.pages[1])
        destination = InvisibleObject((Unit(99), Unit(90)),
                                      neoscore.document.pages[4])
        relative_pos = map_between(source, destination)

        page_1_pos = canvas_pos_of(neoscore.document.pages[1])
        page_4_pos = canvas_pos_of(neoscore.document.pages[4])

        expected = (page_4_pos + Point(Unit(99), Unit(90))) - (
            page_1_pos + Point(Unit(5), Unit(6)))
        assert_almost_equal(relative_pos, expected)
Пример #24
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))
Пример #25
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))
Пример #26
0
 def test_map_between_where_src_parent_is_dest(self):
     destination = InvisibleObject((Unit(3), Unit(10)),
                                   neoscore.document.pages[0])
     source = InvisibleObject((Unit(1), Unit(2)), destination)
     relative_pos = map_between(source, destination)
     expected = Point(Unit(-1), Unit(-2))
     assert_almost_equal(relative_pos, expected)
Пример #27
0
    def test_descendants_of_exact_class(self):
        # Use two new mock classes for type filter testing
        class MockDifferentClass1(InvisibleObject):
            pass

        class MockDifferentClass2(MockDifferentClass1):
            pass

        root = InvisibleObject((Unit(0), Unit(0)))
        child_1 = InvisibleObject((Unit(0), Unit(0)), parent=root)
        child_2 = MockDifferentClass1((Unit(0), Unit(0)), parent=root)
        subchild_1 = MockDifferentClass2((Unit(0), Unit(0)), parent=child_2)
        subchild_2 = MockDifferentClass2((Unit(0), Unit(0)), parent=child_2)
        descendants = list(
            root.descendants_of_exact_class(MockDifferentClass1))
        descendants_set = set(descendants)
        # Assert no duplicates were yielded
        assert len(descendants) == len(descendants_set)
        # Assert root itself was not in the descendants list
        assert root not in descendants_set
        # Assert descendants content
        assert {child_2} == descendants_set
Пример #28
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)
     )
Пример #29
0
    def distance_to_next_of_type(self, staff_object: StaffObject) -> Unit:
        """Find the x distance until the next occurrence of an object's type.

        If the object is the last of its type, this gives the remaining length
        of the staff after the object.

        This is useful for determining rendering behavior of `StaffObject`s
        who are active until another of their type occurs,
        such as `KeySignature`s, or `Clef`s.
        """
        start_x = map_between_x(self, cast(Positioned, staff_object))
        all_others_of_class = (
            item
            for item in self.descendants_of_exact_class(type(staff_object))
            if item != staff_object)
        closest_x = Unit(float("inf"))
        for item in all_others_of_class:
            relative_x = map_between_x(self, item)
            if start_x < relative_x < closest_x:
                closest_x = relative_x
        if closest_x == Unit(float("inf")):
            return self.length - start_x
        return closest_x - start_x
Пример #30
0
 def test_from_existing(self):
     original = Pen(
         Color("#eeddcc"),
         Unit(2),
         PenPattern.DASH,
         PenJoinStyle.MITER,
         PenCapStyle.ROUND,
     )
     clone = Pen.from_existing(original)
     assert id(original) != id(clone)
     assert original.color == clone.color
     assert original.thickness == clone.thickness
     assert original.pattern == clone.pattern
     assert original.join_style == clone.join_style
     assert original.cap_style == clone.cap_style