Source code elsie/shapes/path.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from ..boxtree.lazy import eval_pair, unpack_point

COMMAND_NARGS = {"M": 1, "L": 1, "C": 3, "S": 2, "Q": 2, "T": 1}


def check_and_unpack_path_commands(commands, box):
    result = []
    for command in commands:
        if (
            not isinstance(command, tuple) and not isinstance(command, list)
        ) or not command:
            raise Exception("Invalid command: '{!r}'".format(command))
        name = command[0]
        nargs = COMMAND_NARGS.get(name)
        if nargs is None:
            raise Exception("Invalid command name: '{}'".format(name))
        if (len(command) - 1) % nargs != 0:
            raise Exception(
                "Invalid number of arguments for command '{}' (got {} arguments)".format(
                    name, len(command) - 1
                )
            )
        unpacked = tuple(unpack_point(p, box) for p in command[1:])
        result.append((name, unpacked))
    return result


def eval_path_commands(commands):
    result = []
    for name, pairs in commands:
        result.append((name, [eval_pair(p) for p in pairs]))
    return result


def path_points_for_end_arrow(commands):
    name, pairs = commands[-1]
    if name not in "CSQT":
        raise Exception(
            "Current version supports path when last command is 'C', 'S', 'Q' ot 'T' "
            "(got '{}')".format(name)
        )
    if len(pairs) >= 2:
        return pairs[-2], pairs[-1]
    if len(commands) >= 2:
        return commands[-2][1][-1], pairs[-1]
    else:
        raise Exception("Unsuported commands for end errow")


def path_update_end_point(commands, point):
    commands[-1][1][-1] = point