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 — Merge pull request #2913 from Kozea/fast-svg-paths · Kozea/WeasyPrint@590bf63

File tree

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

‎weasyprint/svg/__init__.py‎

Copy file name to clipboardExpand all lines: weasyprint/svg/__init__.py
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Render SVG images."""
22

33
import re
4+
from collections import deque
45
from contextlib import suppress
56
from math import cos, hypot, pi, radians, sin, sqrt
67
from xml.etree import ElementTree
@@ -94,7 +95,7 @@ def __init__(self, wrapper, style):
9495

9596
self.attrib = wrapper.etree_element.attrib.copy()
9697

97-
self.vertices = []
98+
self.vertices = deque()
9899
self.bounding_box = None
99100

100101
def copy(self):
@@ -633,8 +634,8 @@ def draw_markers(self, node, font_size, fill_stroke):
633634

634635
while node.vertices:
635636
# Calculate position and angle
636-
point = node.vertices.pop(0)
637-
angles = node.vertices.pop(0) if node.vertices else None
637+
point = node.vertices.popleft()
638+
angles = node.vertices.popleft() if node.vertices else None
638639
if angles:
639640
if position == 'start':
640641
angle = pi - angles[0]
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
Collapse file

‎weasyprint/svg/shapes.py‎

Copy file name to clipboardExpand all lines: weasyprint/svg/shapes.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def line(svg, node, font_size):
9595
svg.stream.move_to(x1, y1)
9696
svg.stream.line_to(x2, y2)
9797
angle = atan2(y2 - y1, x2 - x1)
98-
node.vertices = [(x1, y1), (pi - angle, angle), (x2, y2)]
98+
node.vertices.extend(((x1, y1), (pi - angle, angle), (x2, y2)))
9999

100100

101101
def polygon(svg, node, font_size):
@@ -110,7 +110,7 @@ def polyline(svg, node, font_size):
110110
if points:
111111
x, y, points = point(svg, points, font_size)
112112
svg.stream.move_to(x, y)
113-
node.vertices = [(x, y)]
113+
node.vertices.append((x, y))
114114
while points:
115115
x_old, y_old = x, y
116116
x, y, points = point(svg, points, font_size)

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.