1#  Copyright (c) 2021, Manfred Moitzi
2#  License: MIT License
3
4import pytest
5
6pytest.importorskip('matplotlib')
7
8from matplotlib.textpath import TextPath
9from matplotlib.font_manager import FontProperties
10from ezdxf import path
11from ezdxf.math import Vec2
12
13
14class TestFromMatplotlibPath:
15    fp = FontProperties(family='Arial')
16
17    def to_mpath(self, text: str):
18        return TextPath((0, 0), text, size=1, prop=self.fp, usetex=False)
19
20    def test_curve_path_from_text_path(self):
21        mpath = self.to_mpath('obc')
22        paths = list(path.from_matplotlib_path(mpath))
23
24        # Last command is a LINE_TO created by CLOSEPOLY only if the path
25        # isn't closed:
26        assert paths[0][-1].type != \
27               path.Command.LINE_TO, "did not expected LINE_TO as last command"
28
29        commands = paths[0][:-1]
30        assert all((cmd.type == path.Command.CURVE3_TO
31                    for cmd in commands)), "expected only CURVE3_TO commands"
32        assert len(paths) == 5  # 2xo 2xb 1xc
33
34    def test_line_path_from_text_path(self):
35        mpath = self.to_mpath('abc')
36        paths = list(path.from_matplotlib_path(mpath, curves=False))
37        path0 = paths[0]
38        assert all((cmd.type == path.Command.LINE_TO
39                    for cmd in path0)), "expected only LINE_TO commands"
40        assert len(paths) == 5  # 2xa 2xb 1xc
41
42
43from ezdxf.path.converter import MplCmd as MC
44
45
46class TestToMatplotlibPath:
47    def test_no_paths(self):
48        with pytest.raises(ValueError):
49            path.to_matplotlib_path([])
50
51    def test_line_to(self):
52        p = path.Path()
53        p.line_to((4, 5, 6))
54        p.line_to((7, 8, 6))
55        mpath = path.to_matplotlib_path([p])
56        assert tuple(mpath.codes) == (MC.MOVETO, MC.LINETO, MC.LINETO)
57        assert Vec2.list(mpath.vertices) == [(0, 0), (4, 5), (7, 8)]
58
59    def test_curve3_to(self):
60        p = path.Path()
61        p.curve3_to((4, 0, 2), (2, 1, 7))
62        mpath = path.to_matplotlib_path([p])
63        assert tuple(mpath.codes) == (MC.MOVETO, MC.CURVE3, MC.CURVE3)
64        assert Vec2.list(mpath.vertices) == [(0, 0), (2, 1), (4, 0)]
65
66    def test_curve4_to(self):
67        p = path.Path()
68        p.curve4_to((4, 0, 2), (1, 1, 7), (3, 1, 5))
69        mpath = path.to_matplotlib_path([p])
70        assert tuple(mpath.codes) == (
71            MC.MOVETO, MC.CURVE4, MC.CURVE4, MC.CURVE4
72        )
73        assert Vec2.list(mpath.vertices) == [(0, 0), (1, 1), (3, 1), (4, 0)]
74
75    def test_two_paths(self):
76        p1 = path.Path()
77        p1.line_to((4, 5, 6))
78        p2 = path.Path()
79        p2.line_to((7, 8, 6))
80        mpath = path.to_matplotlib_path([p1, p2])
81        assert tuple(mpath.codes) == (
82            MC.MOVETO, MC.LINETO,
83            MC.MOVETO, MC.LINETO,
84        )
85        assert Vec2.list(mpath.vertices) == [
86            (0, 0), (4, 5),
87            (0, 0), (7, 8),
88        ]
89
90
91if __name__ == '__main__':
92    pytest.main([__file__])
93