Skip to content

Commit

Permalink
Merge pull request #29 from meerk40t/tatarize-meerk40tweaks
Browse files Browse the repository at this point in the history
Final Tweaks
  • Loading branch information
tatarize authored Dec 23, 2019
2 parents 909eaf5 + 7bc2cec commit efe37e2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = svg.elements
version = 0.7.4
version = 0.7.5
description = Svg Elements Parsing
long_description_content_type=text/markdown
long_description = file: README.md
Expand Down
38 changes: 25 additions & 13 deletions svg/elements/svg_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4505,8 +4505,8 @@ def __init__(self, *args, **kwargs):
if SVG_ATTR_DATA in p:
self.parse(p[SVG_ATTR_DATA])
elif isinstance(p, Subpath):
self._segments = []
self._segments.extend(map(copy, list(p)))
self._segments = list(p.segments(transformed=False))
Shape.__init__(self,p._path)
elif isinstance(args[0], Shape):
self._segments = list(p.segments(transformed=False))
elif isinstance(args[0], str):
Expand Down Expand Up @@ -4656,10 +4656,8 @@ def __eq__(self, other):
return NotImplemented
if len(self) != len(other):
return False
p = self.__copy__()
p.reify()
q = other.__copy__()
q.reify()
p = abs(self)
q = abs(other)
for s, o in zip(q._segments, p._segments):
if not s == o:
return False
Expand Down Expand Up @@ -5117,8 +5115,8 @@ def __init__(self, *args, **kwargs):
s = args[0]
self.x = s.x
self.y = s.y
self.width = s.w
self.height = s.h
self.width = s.width
self.height = s.height
self.rx = s.rx
self.ry = s.ry
self._validate_rect()
Expand Down Expand Up @@ -6157,6 +6155,12 @@ def __ne__(self, other):
return NotImplemented
return not self == other

def segments(self, transformed=True):
path = self._path
if transformed:
return [s * path.transform for s in path._segments[self._start:self._end + 1]]
return path._segments[self._start:self._end + 1]

def index_to_path_index(self, index):
if index < 0:
return self._end + index + 1
Expand Down Expand Up @@ -6373,9 +6377,13 @@ def __init__(self, *args, **kwargs):
self.data = b64decode(self.url[23:])
elif self.url.startswith("data:image/svg+xml;base64,"):
self.data = b64decode(self.url[26:])
self.image = None
self.image_width = None
self.image_height = None
if 'image' in kwargs:
self.image = kwargs['image']
self.image_width, self.image_height = self.image.size
else:
self.image = None
self.image_width = None
self.image_height = None
if SVG_ATTR_WIDTH in kwargs:
self.viewbox.physical_width = Length(kwargs[SVG_ATTR_WIDTH]).value()
if SVG_ATTR_HEIGHT in kwargs:
Expand Down Expand Up @@ -6675,7 +6683,7 @@ class SVG:
def __init__(self, f):
self.f = f

def elements(self, reify=True, ppi=DEFAULT_PPI, width=1, height=1, color="black"):
def elements(self, reify=True, ppi=DEFAULT_PPI, width=1, height=1, color="black", transform=None):
"""
Parses the SVG file.
Style elements are split into their proper values.
Expand Down Expand Up @@ -6741,7 +6749,11 @@ def elements are not processed.
viewbox = Viewbox(values)
viewbox.render(ppi=ppi, width=width, height=height)
yield viewbox
new_transform = viewbox.transform()
if transform is not None:
new_transform = transform + ' ' + viewbox.transform()
transform = None
else:
new_transform = viewbox.transform()
width = viewbox.viewbox_width
height = viewbox.viewbox_height
values[SVG_VIEWBOX_TRANSFORM] = new_transform
Expand Down
30 changes: 30 additions & 0 deletions test/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,33 @@ def test_element_merge(self):
self.assertEqual(r.values['id'], values['id'])
self.assertNotEqual(r.values['random'], values['random'])
self.assertEqual(r.values['tat'], 'awesome')

r = Rect(fill='red')
self.assertEqual(r.fill, '#f00')

def test_element_propagate(self):

values = {'id': 'my_id', 'random': True}
r = Rect(values, random=False, tat='awesome')
r = Rect(r)
self.assertEqual(r.values['id'], values['id'])
self.assertNotEqual(r.values['random'], values['random'])
self.assertEqual(r.values['tat'], 'awesome')

r = Rect(fill='red')
r = Rect(r)
self.assertEqual(r.fill, '#f00')
r = Rect(stroke='red')
r = Rect(r)
self.assertEqual(r.stroke, '#f00')

r = Rect(width=20)
r = Rect(r)
self.assertEqual(r.width, 20)

p = Path('M0,0 20,0 0,20z M20,20 40,20 20,40z', fill='red')
p2 = Path(p.subpath(1))
p2[0].start = None
self.assertEqual(p2, 'M20,20 40,20 20,40z')
self.assertEqual(p2.fill, 'red')

0 comments on commit efe37e2

Please sign in to comment.