Add color_rgba, points, some __repr__s and other tweaks

This commit is contained in:
Robin Dunn
2019-07-26 19:43:13 -07:00
parent a5bd9f39a8
commit ce2046549d
2 changed files with 2645 additions and 17335 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -94,22 +94,26 @@ cdef class SVGimage:
@staticmethod
def from_buffer(const unsigned char[:] buff, str units='px', float dpi=96) -> SVGimage:
def from_bytes(bytes buffer, str units='px', float dpi=96) -> SVGimage:
"""
Loads an SVG image from a buffer object (bytes, bytearray, memoryview, arrary of char, etc.)
Loads an SVG image from a bytes object.
:param buffer `buff`: object containing the SVG data
:param bytes `buffer`: object containing the SVG data
:param str `units`: One of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'
:param float `dpi`: controls how the unit conversion is done
:rtype: SVGimage
"""
cdef const unsigned char *pbuff = &buff[0]
img = SVGimage.from_ptr(nsvgParse(pbuff, bytes(units, 'utf-8'), dpi))
img = SVGimage.from_ptr(nsvgParse(buffer, bytes(units, 'utf-8'), dpi))
if img._ptr == NULL:
raise RuntimeError('Unable to parse SVG buffer')
return img
def __repr__(self):
if self._ptr:
return "SVGimage: size ({}, {})".format(self.width, self.height)
else:
return "SVGimage: <uninitialized>"
@property
def width(self) -> float:
@@ -162,6 +166,12 @@ cdef class SVGshape:
obj._ptr = ptr
return obj
def __repr__(self):
if self._ptr:
return "SVGshape: id:{} bounds:{}".format(self.id, self.bounds)
else:
return "SVGshape: <uninitialized>"
@property
def id(self):
""" Optional 'id' attr of the shape or its group """
@@ -295,11 +305,36 @@ cdef class SVGpath:
if self._ptr == NULL:
raise ValueError("Invalid SVGpath")
def __repr__(self):
if self._ptr:
return "SVGpath: bounds:{}".format(self.bounds)
else:
return "SVGpath: <uninitialized>"
@property
def pts(self) -> list:
""" Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ... """
"""
Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
The return value is a list of floats.
"""
self._check_ptr()
return [self._ptr.pts[i] for i in range(self._ptr.npts)]
return [self._ptr.pts[i] for i in range(self._ptr.npts*2)]
@property
def npts(self) -> int:
""" Number of points """
self._check_ptr()
return self._ptr.npts
@property
def points(self) -> list:
"""
Cubic bezier points: (x0,y0), [(cpx1,cpx1), (cpx2,cpy2), (x1,y1)], ...
The return value is a list of tuples, each containing an x-y pair.
"""
self._check_ptr()
return [(self._ptr.pts[i], self._ptr.pts[i+1])
for i in range(0, self._ptr.npts*2, 2)]
@property
def closed(self) -> bool:
@@ -366,6 +401,15 @@ cdef class SVGpaint:
raise ValueError("Color not valid in this paint object")
return self._ptr.color
@property
def color_rgba(self) -> tuple:
""" Returns color as a RGBA tuple """
c = self.color
return ( c & 0xff,
(c >> 8) & 0xff,
(c >> 16) & 0xff,
(c >> 24) & 0xff)
@property
def gradient(self) -> SVGgradient:
self._check_ptr()