File: _nanosvg.pyx

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (662 lines) | stat: -rw-r--r-- 21,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#----------------------------------------------------------------------
# Name:        wx.svg._nanosvg.pyx
# Purpose:     Cython-based wrappers for the nanosvg C code. See
#              https://github.com/memononen/nanosvg
#
# Author:      Robin Dunn
#
# Created:     23-July-2019
# Copyright:   (c) 2019-2020 by Total Control Software
# Licence:     wxWindows license
#----------------------------------------------------------------------
"""
NanoSVG is a "simple stupid single-header-file SVG parser" from
https://github.com/memononen/nanosvg. The output of the parser is a collection
of data structures representing cubic bezier shapes.

NanoSVG supports a wide range of SVG features. The library is well suited for
anything from rendering scalable icons in your editor application to prototyping
a game. There is not a full coverage of the SVG specification, however the
features needed for typical icons or even more advanced vector images are
present.

The shapes in the SVG images are transformed by the viewBox and converted to
specified units. That is, you should get the same looking data as you designed
in your favorite app.

NanoSVG can return the paths in few different units. For example if you want to
render an image, you may choose to get the paths in pixels, or if you are
feeding the data into a CNC-cutter, you may want to use millimeters.

The units passed to NanoSVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or
'in'. DPI (dots-per-inch) controls how the unit conversion is done.

If you don't know or care about the units stuff, "px" and 96 should get you
going.

This module implements a Cython-based wrapper for the NanoSVG code, providing
access to the parsed SVG data as a nested collection of objects and properties.
Note that these classes are essentially read-only. There is no support (yet?)
for manipulating the SVG shape info in memory.
"""

import sys

from cpython.buffer cimport (
    Py_buffer, PyObject_CheckBuffer, PyObject_GetBuffer, PyBUF_SIMPLE,
    PyBuffer_Release)

#----------------------------------------------------------------------------
# Replicate the C enums and values for Python, dropping the leading 'N'

cpdef enum SVGpaintType:
    SVG_PAINT_NONE = NSVG_PAINT_NONE
    SVG_PAINT_COLOR = NSVG_PAINT_COLOR
    SVG_PAINT_LINEAR_GRADIENT = NSVG_PAINT_LINEAR_GRADIENT
    SVG_PAINT_RADIAL_GRADIENT = NSVG_PAINT_RADIAL_GRADIENT

cpdef enum SVGspreadType:
    SVG_SPREAD_PAD = NSVG_SPREAD_PAD
    SVG_SPREAD_REFLECT = NSVG_SPREAD_REFLECT
    SVG_SPREAD_REPEAT = NSVG_SPREAD_REPEAT

cpdef enum SVGlineJoin:
    SVG_JOIN_MITER = NSVG_JOIN_MITER
    SVG_JOIN_ROUND = NSVG_JOIN_ROUND
    SVG_JOIN_BEVEL = NSVG_JOIN_BEVEL

cpdef enum SVGlineCap:
    SVG_CAP_BUTT = NSVG_CAP_BUTT
    SVG_CAP_ROUND = NSVG_CAP_ROUND
    SVG_CAP_SQUARE = NSVG_CAP_SQUARE

cpdef enum SVGfillRule:
    SVG_FILLRULE_NONZERO = NSVG_FILLRULE_NONZERO
    SVG_FILLRULE_EVENODD = NSVG_FILLRULE_EVENODD

cpdef enum SVGflags:
    SVG_FLAGS_VISIBLE = NSVG_FLAGS_VISIBLE


#----------------------------------------------------------------------------
# Cython classes for wrapping the nanosvg structs

cdef class SVGimageBase:
    """
    A SVGimageBase can be created either from an SVG file or from an in-memory
    buffer containing the SVG XML code. The result is a collection of cubic
    bezier shapes, with fill, stroke, gradients, paths and other information.

    This class is a Cython-based wrapper around the nanosvg ``NSVGimage`` structure,
    providing just the basic wrapped functionality from nanosvg. Please see the
    :class:`wx.svg.SVGimage` class for a derived implementation that adds
    functionality for integrating with wxPython.
    """
    cdef NSVGimage *_ptr
    cdef NSVGrasterizer *_rasterizer

    def __cinit__(self):
        self._ptr = NULL
        self._rasterizer = NULL

    def __dealloc__(self):
        if self._ptr != NULL:
            nsvgDelete(self._ptr)
        if self._rasterizer != NULL:
            nsvgDeleteRasterizer(self._rasterizer)

    cdef _check_ptr(self):
        if self._ptr == NULL:
            raise ValueError("SVG not yet loaded")

    cdef _set_ptr(self, NSVGimage *ptr, str errmsg='Unable to parse SVG'):
        if self._ptr != NULL:
            nsvgDelete(self._ptr)
        if self._rasterizer != NULL:
            nsvgDeleteRasterizer(self._rasterizer)
            self._rasterizer = NULL
        if ptr == NULL:
            raise ValueError(errmsg)
        self._ptr = ptr


    @staticmethod
    cdef SVGimageBase from_ptr(NSVGimage *ptr):
        obj = SVGimageBase()
        obj._ptr = ptr
        return obj


    @classmethod
    def CreateFromFile(cls, str filename, str units='px', float dpi=96):
        """
        Loads an SVG image from a file.

        :param str `filename`: Name of the file to load the SVG image from
        :param str `units`: One of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'
        :param float `dpi`: controls how the unit conversion is done

        :rtype: An instance of ``cls`` (usually a :class:`SVGimage`)
        """
        name = filename.encode(sys.getfilesystemencoding())
        units_b = units.encode('utf-8')
        cdef SVGimageBase img = cls()
        img._set_ptr(nsvgParseFromFile(name, units_b, dpi),
                     'Unable to parse SVG file {}'.format(filename))
        return img


    @classmethod
    def CreateFromBytes(cls, bytes buffer, str units='px', float dpi=96, bint do_copy=True):
        """
        Loads an SVG image from a bytes object.

        :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
        :param bool `do_copy`: indicates if the given bytes object should be
            copied to avoid in-place modification. This should be set to True
            if the given `buffer` object may ever be reused in any capacity.
            If the given `buffer` will only be used once, and the cost of copying
            it is problematic, then `do_copy` can be set to False.

        :rtype: An instance of ``cls`` (usually a :class:`SVGimage`)
        """

        if do_copy:
            # `nsvgParse` will end up modifying the char-array passed to it in-place
            # which will violate the immutability of python `bytes` objects.
            # To avoid this we're going to copy the given `buffer` object into
            # an entirely separate portion of memory. Unfortunately, python is a
            # step ahead of the game and optimizes copying byte strings into just
            # returning the same object (because they're immutable!), so to truly
            # get a different byte string we'll copy it via converting to a bytearray
            # and back:
            buffer = bytes(bytearray(buffer))

        units_b = units.encode('utf-8')
        cdef SVGimageBase img = cls()
        img._set_ptr(nsvgParse(buffer, units_b, dpi),
                     'Unable to parse SVG buffer')
        return img


    def __repr__(self) -> str:
        if self._ptr:
            return "SVGimageBase: size ({}, {})".format(self.width, self.height)
        else:
            return "SVGimageBase: <uninitialized>"


    def RasterizeToBuffer(self, object buf, float tx=0.0, float ty=0.0, float scale=1.0,
                         int width=-1, int height=-1, int stride=-1) -> bytes:
        """
        Renders the SVG image to an existing buffer as a series of RGBA values.

        The buffer object must support the Python buffer-protocol, be writable,
        and be at least ``width * height * 4`` bytes long. Possibilities include
        bytearrays, memoryviews, numpy arrays, etc.

        :param `buf`: An object supporting the buffer protocol where the RGBA bytes will be written
        :param float `tx`: Image horizontal offset (applied after scaling)
        :param float `ty`: Image vertical offset (applied after scaling)
        :param float `scale`: Image scale
        :param int `width`: width of the image to render, defaults to width from the SVG file
        :param int `height`: height of the image to render, defaults to height from the SVG file
        :param int `stride`: number of bytes per scan line in the destination buffer, typically ``width * 4``
        """
        self._check_ptr()
        if self._rasterizer == NULL:
            self._rasterizer = nsvgCreateRasterizer()

        if width == -1:
            width = self.width
        if height == -1:
            height = self.height
        if stride == -1:
            stride = width * 4;

        if not PyObject_CheckBuffer(buf):
            raise ValueError("Object does not support the python buffer protocol")

        cdef Py_buffer view
        if PyObject_GetBuffer(buf, &view, PyBUF_SIMPLE) != 0:
            raise ValueError("PyObject_GetBuffer failed")
        if view.len < height * stride:
            PyBuffer_Release(&view)
            raise ValueError("Buffer object is smaller than height * stride")

        nsvgRasterize(self._rasterizer, self._ptr, tx, ty, scale, <unsigned char*>view.buf,
                      width, height, stride)
        PyBuffer_Release(&view)


    def Rasterize(self, float tx=0.0, float ty=0.0, float scale=1.0,
                  int width=-1, int height=-1, int stride=-1) -> bytes:
        """
        Renders the SVG image to a ``bytes`` object as a series of RGBA values.

        :param float `tx`: Image horizontal offset (applied after scaling)
        :param float `ty`: Image vertical offset (applied after scaling)
        :param float `scale`: Image scale
        :param int `width`: width of the image to render, defaults to width from the SVG file
        :param int `height`: height of the image to render, defaults to height from the SVG file
        :param int `stride`: number of bytes per scan line in the destination buffer, typically ``width * 4``

        :returns: A bytearray object containing the raw RGBA pixel color values
        """
        self._check_ptr()
        if self._rasterizer == NULL:
            self._rasterizer = nsvgCreateRasterizer()

        if width == -1:
            width = self.width
        if height == -1:
            height = self.height
        if stride == -1:
            stride = width * 4;

        buf = bytes(bytearray(height * stride))
        nsvgRasterize(self._rasterizer, self._ptr, tx, ty, scale, buf,
                      width, height, stride)
        return buf

    @property
    def width(self) -> float:
        """
        Returns the width of the SVG image
        """
        self._check_ptr()
        return self._ptr.width

    @property
    def height(self) -> float:
        """
        Returns the height of the SVG image
        """
        self._check_ptr()
        return self._ptr.height

    @property
    def shapes(self):
        """
        A generator that iterates over the :class:`SVGshape` objects that comprise the SVG image
        """
        self._check_ptr()
        cdef NSVGshape *shape = self._ptr.shapes
        while shape != NULL:
            yield SVGshape.from_ptr(shape)
            shape = shape.next


#----------------------------------------------------------------------------

cdef class SVGshape:
    """
    SVGshape is a set of attributes describing how to draw one shape in the SVG,
    including stroke and fill styles, line styles, and paths. A collection of
    SVGshapes is accessible from the ``shapes`` attribute of :class:`SVGimage`.
    """
    cdef NSVGshape *_ptr

    def __cinit__(self):
        self._ptr = NULL

    def _check_ptr(self):
        if self._ptr == NULL:
            raise ValueError("Invalid SVGshape")

    @staticmethod
    cdef SVGshape from_ptr(NSVGshape *ptr):
        obj = SVGshape()
        obj._ptr = ptr
        return obj

    def __repr__(self):
        if self._ptr:
            return "SVGshape: id:{} bounds:{}".format(self.id.decode('utf-8'), self.bounds)
        else:
            return "SVGshape: <uninitialized>"

    @property
    def id(self):
        """Optional 'id' attr of the shape or its group"""
        self._check_ptr()
        return self._ptr.id

    @property
    def fill(self) -> SVGpaint:
        """:class:`SVGpaint` for the fill"""
        self._check_ptr()
        return SVGpaint.from_ptr(&self._ptr.fill)

    @property
    def stroke(self) -> SVGpaint:
        """:class:`SVGpaint` for the stroke"""
        self._check_ptr()
        return SVGpaint.from_ptr(&self._ptr.stroke)

    @property
    def opacity(self) -> float:
        """Opacity of the shape"""
        self._check_ptr()
        return self._ptr.opacity

    @property
    def strokeWidth(self) -> float:
        """Stroke width (scaled)"""
        self._check_ptr()
        return self._ptr.strokeWidth

    @property
    def strokeDashOffset(self) -> float:
        """Stroke dash offset (scaled)"""
        self._check_ptr()
        return self._ptr.strokeDashOffset

    @property
    def strokeDashArray(self) -> list:
        """Stroke dash array (scaled)"""
        self._check_ptr()
        return [self._ptr.strokeDashArray[i]
                for i in range(self._ptr.strokeDashCount)]

    @property
    def strokeLineJoin(self) -> SVGlineJoin:
        """Stroke join type"""
        self._check_ptr()
        return SVGlineJoin(self._ptr.strokeLineJoin)

    @property
    def strokeLineCap(self) -> SVGlineCap:
        """Stroke cap type"""
        self._check_ptr()
        return SVGlineCap(self._ptr.strokeLineCap)

    @property
    def fillRule(self) -> SVGfillRule:
        """Fill rule"""
        self._check_ptr()
        return SVGfillRule(self._ptr.fillRule)

    @property
    def miterLimit(self) -> float:
        """Miter limit"""
        self._check_ptr()
        return self._ptr.miterLimit

    @property
    def flags(self) -> int:
        """Logical OR of SVG_FLAGS_* flags"""
        self._check_ptr()
        return int(self._ptr.flags)

    @property
    def bounds(self) -> list:
        """Tight bounding box of the shape [minx,miny,maxx,maxy]"""
        self._check_ptr()
        return [self._ptr.bounds[i] for i in range(4)]

    @property
    def minx(self) -> float:
        self._check_ptr()
        return self._ptr.bounds[0]

    @property
    def miny(self) -> float:
        self._check_ptr()
        return self._ptr.bounds[1]

    @property
    def maxx(self) -> float:
        self._check_ptr()
        return self._ptr.bounds[2]

    @property
    def maxy(self) -> float:
        self._check_ptr()
        return self._ptr.bounds[3]

    @property
    def paths(self):
        """
        A generator that iterates over the :class:`SVGpath` objects contained in the SVGshape
        """
        self._check_ptr()
        cdef NSVGpath *path = self._ptr.paths
        while path != NULL:
            yield SVGpath.from_ptr(path)
            path = path.next


#----------------------------------------------------------------------------
cdef class SVGpath:
    """
    An SVGpath is essentially just a collection of bezier curves, defined by a
    set of floating point coordinates. A collection of SVGpaths is accessible
    from the `paths` attribute of SVGshape.
    """
    cdef NSVGpath *_ptr

    def __cinit__(self):
        self._ptr = NULL

    @staticmethod
    cdef SVGpath from_ptr(NSVGpath *ptr):
        obj = SVGpath()
        obj._ptr = ptr
        return obj

    def _check_ptr(self):
        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], ...
        The return value is a list of floats.
        """
        self._check_ptr()
        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:
        """Flag indicating if shapes should be treated as closed"""
        self._check_ptr()
        return bool(self._ptr.closed)

    @property
    def bounds(self) -> list:
        """Tight bounding box of the shape [minx,miny,maxx,maxy]"""
        self._check_ptr()
        return [self._ptr.bounds[i] for i in range(4)]

    @property
    def minx(self) -> float:
        self._check_ptr()
        return self._ptr.bounds[0]

    @property
    def miny(self) -> float:
        self._check_ptr()
        return self._ptr.bounds[1]

    @property
    def maxx(self) -> float:
        self._check_ptr()
        return self._ptr.bounds[2]

    @property
    def maxy(self) -> float:
        self._check_ptr()
        return self._ptr.bounds[3]


#----------------------------------------------------------------------------
cdef class SVGpaint:
    """
    This class defines how to fill or stroke a shape when rendering the SVG
    image. In other words, how to create the pen or brush. It can be a solid
    color, linear or radial gradients, etc.
    """
    cdef NSVGpaint *_ptr

    def __cinit__(self):
        self._ptr = NULL

    @staticmethod
    cdef SVGpaint from_ptr(NSVGpaint *ptr):
        obj = SVGpaint()
        obj._ptr = ptr
        return obj

    def _check_ptr(self):
        if self._ptr == NULL:
            raise ValueError("Invalid SVGpaint")

    @property
    def type(self) -> SVGpaintType:
        """Flag indicating the type of paint info, solid color or type of gradient"""
        self._check_ptr()
        return SVGpaintType(self._ptr.type)

    @property
    def color(self) -> uint:
        self._check_ptr()
        if self.type != SVG_PAINT_COLOR:
            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()
        if self.type not in [SVG_PAINT_LINEAR_GRADIENT, SVG_PAINT_RADIAL_GRADIENT]:
            raise ValueError("Gradient not valid in this paint object")
        return SVGgradient.from_ptr(self._ptr.gradient)


#----------------------------------------------------------------------------
cdef class SVGgradient:
    """
    A gradient is a method used to fade from one color to another, either
    linearly or radially.
    """
    cdef NSVGgradient *_ptr

    def __cinit__(self):
        self._ptr = NULL

    @staticmethod
    cdef SVGgradient from_ptr(NSVGgradient *ptr):
        obj = SVGgradient()
        obj._ptr = ptr
        return obj

    def _check_ptr(self):
        if self._ptr == NULL:
            raise ValueError("Invalid SVGgradient")

    @property
    def xform(self) -> list:
        """The gradient's transform"""
        self._check_ptr()
        return [self._ptr.xform[i] for i in range(6)]

    @property
    def spread(self) -> int:
        self._check_ptr()
        return int(self._ptr.spread)

    @property
    def fx(self) -> float:
        self._check_ptr()
        return self._ptr.fx

    @property
    def fy(self) -> float:
        self._check_ptr()
        return self._ptr.fy

    @property
    def stops(self):
        """
        A generator that iterates over the :class:`SVGgradientStop` objects contained in the SVGgradient
        """
        self._check_ptr()
        for i in range(self._ptr.nstops):
            yield SVGgradientStop.from_ptr(&self._ptr.stops[i])


#----------------------------------------------------------------------------
cdef class SVGgradientStop:
    """
    A Gradient stop is an offset and a color, which is used when drawing gradients.
    """
    cdef NSVGgradientStop *_ptr

    def __cinit__(self):
        self._ptr = NULL

    @staticmethod
    cdef SVGgradientStop from_ptr(NSVGgradientStop *ptr):
        obj = SVGgradientStop()
        obj._ptr = ptr
        return obj

    def _check_ptr(self):
        if self._ptr == NULL:
            raise ValueError("Invalid SVGgradientStop")

    @property
    def color(self) -> uint:
        self._check_ptr()
        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 offset(self) -> float:
        self._check_ptr()
        return self._ptr.offset


#----------------------------------------------------------------------------