From e002cf4e098cd5a95214f4cd28b35ac8fa4eb1cf Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 10 Aug 2018 19:29:52 +0100 Subject: [PATCH] Add __len__ method that returns the number of contours in Path --- src/python/pathops/_pathops.pyx | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/python/pathops/_pathops.pyx b/src/python/pathops/_pathops.pyx index 12e96fc..1bf5354 100644 --- a/src/python/pathops/_pathops.pyx +++ b/src/python/pathops/_pathops.pyx @@ -210,6 +210,14 @@ cdef class Path: s.append("path.%s(%s)" % (method, args)) return "\n".join(s) + def __repr__(self): + return "" % ( + hex(id(self)), self.countContours() + ) + + def __len__(self): + return self.countContours() + cpdef addPath(self, Path path): self.path.addPath(path.path) @@ -300,6 +308,24 @@ cdef class Path: def points(self): return self.getPoints() + cdef int countContours(self) except -1: + if self.path.isEmpty(): + return 0 + cdef int i, n, count + cdef uint8_t *verbs + count = self.path.countVerbs() + verbs = PyMem_Malloc(count) + if not verbs: + raise MemoryError() + try: + self.path.getVerbs(verbs, count) + n = 0 + for i in range(count): + if verbs[i] == kMove_Verb: + n += 1 + return n + finally: + PyMem_Free(verbs) @property def contours(self): cdef SkPath temp