GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use a faster regex-based parser for SVG paths · Kozea/WeasyPrint@f6570c3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • weasyprint/svg
    • path.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎weasyprint/svg/path.py‎

Copy file name to clipboardExpand all lines: weasyprint/svg/path.py
+16-16Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Draw paths."""
22

3+
from collections import deque
34
from math import atan2, cos, isclose, pi, radians, sin, tan
5+
from re import finditer
46

57
from ..matrix import Matrix
68
from .utils import normalize, point
@@ -26,17 +28,15 @@ def path(svg, node, font_size):
2628
svg.stream.move_to(*current_point)
2729
last_letter = None
2830

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()
3035
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
4040

4141
if last_letter in (None, 'm', 'M', 'z', 'Z'):
4242
first_path_point = None
@@ -72,14 +72,12 @@ def path(svg, node, font_size):
7272

7373
# rx=0 or ry=0 means straight line
7474
if not rx or not ry:
75-
if string and string[0] not in PATH_LETTERS:
75+
if string := string.strip():
7676
# As we replace the current operation by l, we must be sure
7777
# that the next letter is set to the real current letter (a
7878
# 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}'))
8381
continue
8482

8583
# Cancel the rotation of the second point
@@ -277,5 +275,7 @@ def path(svg, node, font_size):
277275
if letter not in 'zZ':
278276
node.vertices.append(current_point)
279277

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))
281281
last_letter = letter

0 commit comments

Comments
0 (0)
You are visitor 01402605 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.