Skip to content

Commit

Permalink
relative path support
Browse files Browse the repository at this point in the history
  • Loading branch information
taoari committed Nov 17, 2018
1 parent b4e211f commit f2fc2e0
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions svgpathtools/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -2261,71 +2261,107 @@ def end(self, pt):
self._end = pt
self._segments[-1].end = pt

def d(self, useSandT=False, use_closed_attrib=False):
def d(self, useSandT=False, use_closed_attrib=False, rel=False):
"""Returns a path d-string for the path object.
For an explanation of useSandT and use_closed_attrib, see the
compatibility notes in the README."""

if use_closed_attrib:
self_closed = self.closed(warning_on=False)
self_closed = self.iscontinuous() and self.isclosed()
if self_closed:
segments = self[:-1]
else:
segments = self[:]
else:
self_closed = False
segments = self[:]

current_pos = None
parts = []
previous_segment = None
end = self[-1].end

for segment in segments:
seg_start = segment.start
# If the start of this segment does not coincide with the end of
# the last segment or if this segment is actually the close point
# of a closed path, then we should start a new subpath here.
if current_pos != seg_start or \
(self_closed and seg_start == end and use_closed_attrib):
parts.append('M {},{}'.format(seg_start.real, seg_start.imag))

if rel:
_seg_start = seg_start - current_pos if current_pos is not None else seg_start
else:
_seg_start = seg_start
parts.append('M {},{}'.format(_seg_start.real, _seg_start.imag))

if isinstance(segment, Line):
args = segment.end.real, segment.end.imag
parts.append('L {},{}'.format(*args))
if rel:
_seg_end = segment.end - seg_start
else:
_seg_end = segment.end
parts.append('L {},{}'.format(_seg_end.real, _seg_end.imag))
elif isinstance(segment, CubicBezier):
if useSandT and segment.is_smooth_from(previous_segment,
warning_on=False):
args = (segment.control2.real, segment.control2.imag,
segment.end.real, segment.end.imag)
if rel:
_seg_control2 = segment.control2 - seg_start
_seg_end = segment.end - seg_start
else:
_seg_control2 = segment.control2
_seg_end = segment.end
args = (_seg_control2.real, _seg_control2.imag,
_seg_end.real, _seg_end.imag)
parts.append('S {},{} {},{}'.format(*args))
else:
args = (segment.control1.real, segment.control1.imag,
segment.control2.real, segment.control2.imag,
segment.end.real, segment.end.imag)
if rel:
_seg_control1 = segment.control1 - seg_start
_seg_control2 = segment.control2 - seg_start
_seg_end = segment.end - seg_start
else:
_seg_control1 = segment.control1
_seg_control2 = segment.control2
_seg_end = segment.end
args = (_seg_control1.real, _seg_control1.imag,
_seg_control2.real, _seg_control2.imag,
_seg_end.real, _seg_end.imag)
parts.append('C {},{} {},{} {},{}'.format(*args))
elif isinstance(segment, QuadraticBezier):
if useSandT and segment.is_smooth_from(previous_segment,
warning_on=False):
args = segment.end.real, segment.end.imag
if rel:
_seg_end = segment.end - seg_start
else:
_seg_end = segment.end
args = _seg_end.real, _seg_end.imag
parts.append('T {},{}'.format(*args))
else:
args = (segment.control.real, segment.control.imag,
segment.end.real, segment.end.imag)
if rel:
_seg_control = segment.control - seg_start
_seg_end = segment.end - seg_start
else:
_seg_control = segment.control
_seg_end = segment.end
args = (_seg_control.real, _seg_control.imag,
_seg_end.real, _seg_end.imag)
parts.append('Q {},{} {},{}'.format(*args))

elif isinstance(segment, Arc):
if rel:
_seg_end = segment.end - seg_start
else:
_seg_end = segment.end
args = (segment.radius.real, segment.radius.imag,
segment.rotation,int(segment.large_arc),
int(segment.sweep),segment.end.real, segment.end.imag)
int(segment.sweep),_seg_end.real, _seg_end.imag)
parts.append('A {},{} {} {:d},{:d} {},{}'.format(*args))
current_pos = segment.end
previous_segment = segment

if self_closed:
parts.append('Z')

return ' '.join(parts)

s = ' '.join(parts)
return s if not rel else s.lower()

def joins_smoothly_with(self, previous, wrt_parameterization=False):
"""Checks if this Path object joins smoothly with previous
Expand Down

0 comments on commit f2fc2e0

Please sign in to comment.