Skip to content

Commit

Permalink
Overload operators for path operations
Browse files Browse the repository at this point in the history
  • Loading branch information
spakin committed Dec 25, 2023
1 parent a733007 commit 6c6ce89
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions simpinkscr/simple_inkscape_scripting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,53 @@ def skew_path(self, angles, around='center'):
iobj.set('d', iobj.path.transform(tr))
return self

def _do_path_op(self, op, other):
'''Move the other path object into our group, raise it to the top,
destructively apply a path operation, and return the new path.'''
# Complain if the other object is not a path.
if not isinstance(other, SimplePathObject):
_abend(_('path operations can be applied only to path objects'))

# Move the other path into our group (if any).
parent2 = other.get_parent()
if parent2 is not None:
parent2.ungroup(other)
parent1 = self.get_parent()
if parent1 is not None:
parent1.append(other)

# Raise the other path above ours so the operation behaves
# consistently and as expected.
other.z_order('top')

# Apply the named path operation.
new_objs = apply_path_operation(op, [self, other])
return new_objs

def __add__(self, other):
'Destructively apply a path-union operation.'
return self._do_path_op('union', other)[0]

def __sub__(self, other):
'Destructively apply a path-difference operation.'
return self._do_path_op('difference', other)[0]

def __mul__(self, other):
'Destructively apply a path-intersection operation.'
return self._do_path_op('intersection', other)[0]

def __xor__(self, other):
'Destructively apply a path-exclusion operation.'
return self._do_path_op('exclusion', other)[0]

def __truediv__(self, other):
'Destructively apply a path-division operation.'
return self._do_path_op('division', other)

def __floordiv__(self, other):
'Destructively apply a path-cut operation.'
return self._do_path_op('cut', other)


class SimpleTextObject(SimpleObject):
'''A SimpleTextObject is a SimpleObject to which additional text can
Expand Down

0 comments on commit 6c6ce89

Please sign in to comment.