|
1 | 1 | """Draw paths.""" |
2 | 2 |
|
| 3 | +from collections import deque |
3 | 4 | from math import atan2, cos, isclose, pi, radians, sin, tan |
| 5 | +from re import finditer |
4 | 6 |
|
5 | 7 | from ..matrix import Matrix |
6 | 8 | from .utils import normalize, point |
@@ -26,17 +28,15 @@ def path(svg, node, font_size): |
26 | 28 | svg.stream.move_to(*current_point) |
27 | 29 | last_letter = None |
28 | 30 |
|
29 | | - while string: |
| 31 | + matches = deque(match.groups() for match in finditer( |
| 32 | + f'([{PATH_LETTERS}])([^{PATH_LETTERS}]*)', string.strip())) |
| 33 | + while matches: |
| 34 | + letter, string = matches.popleft() |
30 | 35 | string = string.strip() |
31 | | - if string.split(' ', 1)[0] in PATH_LETTERS: |
32 | | - letter, string = (f'{string} ').split(' ', 1) |
33 | | - if last_letter in (None, 'z', 'Z') and letter not in 'mM': |
34 | | - node.vertices.append(current_point) |
35 | | - first_path_point = current_point |
36 | | - elif letter == 'M': |
37 | | - letter = 'L' |
38 | | - elif letter == 'm': |
39 | | - letter = 'l' |
| 36 | + |
| 37 | + if last_letter in (None, 'z', 'Z') and letter not in 'mM': |
| 38 | + node.vertices.append(current_point) |
| 39 | + first_path_point = current_point |
40 | 40 |
|
41 | 41 | if last_letter in (None, 'm', 'M', 'z', 'Z'): |
42 | 42 | first_path_point = None |
@@ -72,14 +72,12 @@ def path(svg, node, font_size): |
72 | 72 |
|
73 | 73 | # rx=0 or ry=0 means straight line |
74 | 74 | if not rx or not ry: |
75 | | - if string and string[0] not in PATH_LETTERS: |
| 75 | + if string := string.strip(): |
76 | 76 | # As we replace the current operation by l, we must be sure |
77 | 77 | # that the next letter is set to the real current letter (a |
78 | 78 | # or A) in case it’s omitted |
79 | | - next_letter = f'{letter} ' |
80 | | - else: |
81 | | - next_letter = '' |
82 | | - string = f'L {x3} {y3} {next_letter}{string}' |
| 79 | + matches.appendleft((letter, string)) |
| 80 | + matches.appendleft(('l', f'{x3} {y3}')) |
83 | 81 | continue |
84 | 82 |
|
85 | 83 | # Cancel the rotation of the second point |
@@ -277,5 +275,7 @@ def path(svg, node, font_size): |
277 | 275 | if letter not in 'zZ': |
278 | 276 | node.vertices.append(current_point) |
279 | 277 |
|
280 | | - string = string.strip() |
| 278 | + if string := string.strip(): |
| 279 | + next_letter = {'m': 'l', 'M': 'L'}.get(letter, letter) |
| 280 | + matches.appendleft((next_letter, string)) |
281 | 281 | last_letter = letter |