GeezerVison is showing github.com — Merge pull request #2913 from Kozea/fast-svg-paths · Kozea/WeasyPrint@590bf63
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: weasyprint/svg/__init__.py +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | """Render SVG images.""" | 2 | 2 |
| 3 | 3 | import re | | 4 | +from collections import deque | 4 | 5 | from contextlib import suppress | 5 | 6 | from math import cos, hypot, pi, radians, sin, sqrt | 6 | 7 | from xml.etree import ElementTree | @@ -94,7 +95,7 @@ def __init__(self, wrapper, style): | 94 | 95 |
| 95 | 96 | self.attrib = wrapper.etree_element.attrib.copy() | 96 | 97 |
| 97 | | - self.vertices = [] | | 98 | + self.vertices = deque() | 98 | 99 | self.bounding_box = None | 99 | 100 |
| 100 | 101 | def copy(self): | @@ -633,8 +634,8 @@ def draw_markers(self, node, font_size, fill_stroke): | 633 | 634 |
| 634 | 635 | while node.vertices: | 635 | 636 | # 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 | 638 | 639 | if angles: | 639 | 640 | if position == 'start': | 640 | 641 | angle = pi - angles[0] | |
Collapse file Copy file name to clipboardExpand all lines: weasyprint/svg/path.py +16-16Lines changed: 16 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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 |
Collapse file Copy file name to clipboardExpand all lines: weasyprint/svg/shapes.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -95,7 +95,7 @@ def line(svg, node, font_size): | 95 | 95 | svg.stream.move_to(x1, y1) | 96 | 96 | svg.stream.line_to(x2, y2) | 97 | 97 | 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))) | 99 | 99 |
| 100 | 100 |
| 101 | 101 | def polygon(svg, node, font_size): | @@ -110,7 +110,7 @@ def polyline(svg, node, font_size): | 110 | 110 | if points: | 111 | 111 | x, y, points = point(svg, points, font_size) | 112 | 112 | svg.stream.move_to(x, y) | 113 | | - node.vertices = [(x, y)] | | 113 | + node.vertices.append((x, y)) | 114 | 114 | while points: | 115 | 115 | x_old, y_old = x, y | 116 | 116 | x, y, points = point(svg, points, font_size) | |
|