How to use svgpathtools - 10 common examples

To help you get started, we’ve selected a few svgpathtools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mathandy / svgpathtools / test / test_parsing.py View on Github external
self.assertTrue(np.array_equal(expected_tf_rotation, tf_rotation))

        # Try a test with no offset specified
        self.assertTrue(np.array_equal(
            construct_rotation_tf(50, 0, 0),
            svgpathtools.parser.parse_transform('rotate(50)')
        ))

        expected_tf_skewx = np.identity(3)
        expected_tf_skewx[0, 1] = np.tan(40.0 * np.pi/180.0)
        tf_skewx = svgpathtools.parser.parse_transform('skewX(40)')
        self.assertTrue(np.array_equal(expected_tf_skewx, tf_skewx))

        expected_tf_skewy = np.identity(3)
        expected_tf_skewy[1, 0] = np.tan(30.0 * np.pi / 180.0)
        tf_skewy = svgpathtools.parser.parse_transform('skewY(30)')
        self.assertTrue(np.array_equal(expected_tf_skewy, tf_skewy))

        self.assertTrue(np.array_equal(
            tf_rotation.dot(tf_translate).dot(tf_skewx).dot(tf_scale),
            svgpathtools.parser.parse_transform(
                """rotate(-10 50 100)
                   translate(-36 45.5)
github mathandy / svgpathtools / test / test_parsing.py View on Github external
tf_scale = svgpathtools.parser.parse_transform('scale(10 0.5)')
        self.assertTrue(np.array_equal(expected_tf_scale, tf_scale))

        tf_rotation = svgpathtools.parser.parse_transform('rotate(-10 50 100)')
        expected_tf_rotation = construct_rotation_tf(-10, 50, 100)
        self.assertTrue(np.array_equal(expected_tf_rotation, tf_rotation))

        # Try a test with no offset specified
        self.assertTrue(np.array_equal(
            construct_rotation_tf(50, 0, 0),
            svgpathtools.parser.parse_transform('rotate(50)')
        ))

        expected_tf_skewx = np.identity(3)
        expected_tf_skewx[0, 1] = np.tan(40.0 * np.pi/180.0)
        tf_skewx = svgpathtools.parser.parse_transform('skewX(40)')
        self.assertTrue(np.array_equal(expected_tf_skewx, tf_skewx))

        expected_tf_skewy = np.identity(3)
        expected_tf_skewy[1, 0] = np.tan(30.0 * np.pi / 180.0)
        tf_skewy = svgpathtools.parser.parse_transform('skewY(30)')
        self.assertTrue(np.array_equal(expected_tf_skewy, tf_skewy))

        self.assertTrue(np.array_equal(
            tf_rotation.dot(tf_translate).dot(tf_skewx).dot(tf_scale),
            svgpathtools.parser.parse_transform(
                """rotate(-10 50 100)
                   translate(-36 45.5)
github mathandy / svgpathtools / test / test_parsing.py View on Github external
svgpathtools.parser.parse_transform('translate(-36)')
        ))

        # Now specify y
        expected_tf_translate[1, 2] = 45.5
        tf_translate = svgpathtools.parser.parse_transform(
            'translate(-36 45.5)')
        self.assertTrue(np.array_equal(expected_tf_translate, tf_translate))

        # Try a test with no y specified
        expected_tf_scale = np.identity(3)
        expected_tf_scale[0, 0] = 10
        expected_tf_scale[1, 1] = 10
        self.assertTrue(np.array_equal(
            expected_tf_scale,
            svgpathtools.parser.parse_transform('scale(10)')
        ))

        # Now specify y
        expected_tf_scale[1, 1] = 0.5
        tf_scale = svgpathtools.parser.parse_transform('scale(10 0.5)')
        self.assertTrue(np.array_equal(expected_tf_scale, tf_scale))

        tf_rotation = svgpathtools.parser.parse_transform('rotate(-10 50 100)')
        expected_tf_rotation = construct_rotation_tf(-10, 50, 100)
        self.assertTrue(np.array_equal(expected_tf_rotation, tf_rotation))

        # Try a test with no offset specified
        self.assertTrue(np.array_equal(
            construct_rotation_tf(50, 0, 0),
            svgpathtools.parser.parse_transform('rotate(50)')
        ))
github mathandy / svgpathtools / test / test_parsing.py View on Github external
tf_translate = svgpathtools.parser.parse_transform(
            'translate(-36 45.5)')
        self.assertTrue(np.array_equal(expected_tf_translate, tf_translate))

        # Try a test with no y specified
        expected_tf_scale = np.identity(3)
        expected_tf_scale[0, 0] = 10
        expected_tf_scale[1, 1] = 10
        self.assertTrue(np.array_equal(
            expected_tf_scale,
            svgpathtools.parser.parse_transform('scale(10)')
        ))

        # Now specify y
        expected_tf_scale[1, 1] = 0.5
        tf_scale = svgpathtools.parser.parse_transform('scale(10 0.5)')
        self.assertTrue(np.array_equal(expected_tf_scale, tf_scale))

        tf_rotation = svgpathtools.parser.parse_transform('rotate(-10 50 100)')
        expected_tf_rotation = construct_rotation_tf(-10, 50, 100)
        self.assertTrue(np.array_equal(expected_tf_rotation, tf_rotation))

        # Try a test with no offset specified
        self.assertTrue(np.array_equal(
            construct_rotation_tf(50, 0, 0),
            svgpathtools.parser.parse_transform('rotate(50)')
        ))

        expected_tf_skewx = np.identity(3)
        expected_tf_skewx[0, 1] = np.tan(40.0 * np.pi/180.0)
        tf_skewx = svgpathtools.parser.parse_transform('skewX(40)')
        self.assertTrue(np.array_equal(expected_tf_skewx, tf_skewx))
github mathandy / svgpathtools / test / test_parsing.py View on Github external
def test_transform(self):

        tf_matrix = svgpathtools.parser.parse_transform(
            'matrix(1.0 2.0 3.0 4.0 5.0 6.0)')
        expected_tf_matrix = np.identity(3)
        expected_tf_matrix[0:2, 0:3] = np.array([[1.0, 3.0, 5.0],
                                                 [2.0, 4.0, 6.0]])
        self.assertTrue(np.array_equal(expected_tf_matrix, tf_matrix))

        # Try a test with no y specified
        expected_tf_translate = np.identity(3)
        expected_tf_translate[0, 2] = -36
        self.assertTrue(np.array_equal(
            expected_tf_translate,
            svgpathtools.parser.parse_transform('translate(-36)')
        ))

        # Now specify y
        expected_tf_translate[1, 2] = 45.5
github mathandy / svgpathtools / test / test_parsing.py View on Github external
expected_tf_matrix = np.identity(3)
        expected_tf_matrix[0:2, 0:3] = np.array([[1.0, 3.0, 5.0],
                                                 [2.0, 4.0, 6.0]])
        self.assertTrue(np.array_equal(expected_tf_matrix, tf_matrix))

        # Try a test with no y specified
        expected_tf_translate = np.identity(3)
        expected_tf_translate[0, 2] = -36
        self.assertTrue(np.array_equal(
            expected_tf_translate,
            svgpathtools.parser.parse_transform('translate(-36)')
        ))

        # Now specify y
        expected_tf_translate[1, 2] = 45.5
        tf_translate = svgpathtools.parser.parse_transform(
            'translate(-36 45.5)')
        self.assertTrue(np.array_equal(expected_tf_translate, tf_translate))

        # Try a test with no y specified
        expected_tf_scale = np.identity(3)
        expected_tf_scale[0, 0] = 10
        expected_tf_scale[1, 1] = 10
        self.assertTrue(np.array_equal(
            expected_tf_scale,
            svgpathtools.parser.parse_transform('scale(10)')
        ))

        # Now specify y
        expected_tf_scale[1, 1] = 0.5
        tf_scale = svgpathtools.parser.parse_transform('scale(10 0.5)')
        self.assertTrue(np.array_equal(expected_tf_scale, tf_scale))
github mathandy / svgpathtools / svgpathtools / path.py View on Github external
def split(self, t):
        """returns two segments, whose union is this segment and which join at
        self.point(t)."""
        pt = self.point(t)
        return Line(self.start, pt), Line(pt, self.end)
github mathandy / svgpathtools / svgpathtools / path.py View on Github external
This can be undone with:
    >>> numpy.seterr(**old_numpy_error_settings)
    """
    assert 0 <= t <= 1
    dseg = seg.derivative(t)

    # Note: dseg might be numpy value, use np.seterr(invalid='raise')
    try:
        unit_tangent = dseg/abs(dseg)
    except (ZeroDivisionError, FloatingPointError):
        # This may be a removable singularity, if so we just need to compute
        # the limit.
        # Note: limit{{dseg / abs(dseg)} = sqrt(limit{dseg**2 / abs(dseg)**2})
        dseg_poly = seg.poly().deriv()
        dseg_abs_squared_poly = (real(dseg_poly) ** 2 +
                                 imag(dseg_poly) ** 2)
        try:
            unit_tangent = csqrt(rational_limit(dseg_poly**2,
                                            dseg_abs_squared_poly, t))
        except ValueError:
            bef = seg.poly().deriv()(t - 1e-4)
            aft = seg.poly().deriv()(t + 1e-4)
            mes = ("Unit tangent appears to not be well-defined at "
                   "t = {}, \n".format(t) +
                   "seg.poly().deriv()(t - 1e-4) = {}\n".format(bef) +
                   "seg.poly().deriv()(t + 1e-4) = {}".format(aft))
            raise ValueError(mes)
    return unit_tangent
github mathandy / svgpathtools / svgpathtools / path.py View on Github external
>>> np.seterr(**old)
    """

    dz = self.derivative(t)
    ddz = self.derivative(t, n=2)
    dx, dy = dz.real, dz.imag
    ddx, ddy = ddz.real, ddz.imag
    old_np_seterr = np.seterr(invalid='raise')
    try:
        kappa = abs(dx*ddy - dy*ddx)/sqrt(dx*dx + dy*dy)**3
    except (ZeroDivisionError, FloatingPointError):
        # tangent vector is zero at t, use polytools to find limit
        p = self.poly()
        dp = p.deriv()
        ddp = dp.deriv()
        dx, dy = real(dp), imag(dp)
        ddx, ddy = real(ddp), imag(ddp)
        f2 = (dx*ddy - dy*ddx)**2
        g2 = (dx*dx + dy*dy)**3
        lim2 = rational_limit(f2, g2, t)
        if lim2 < 0:  # impossible, must be numerical error
            return 0
        kappa = sqrt(lim2)
    finally:
        np.seterr(**old_np_seterr)
    return kappa
github mathandy / svgpathtools / svgpathtools / path.py View on Github external
"""

    dz = self.derivative(t)
    ddz = self.derivative(t, n=2)
    dx, dy = dz.real, dz.imag
    ddx, ddy = ddz.real, ddz.imag
    old_np_seterr = np.seterr(invalid='raise')
    try:
        kappa = abs(dx*ddy - dy*ddx)/sqrt(dx*dx + dy*dy)**3
    except (ZeroDivisionError, FloatingPointError):
        # tangent vector is zero at t, use polytools to find limit
        p = self.poly()
        dp = p.deriv()
        ddp = dp.deriv()
        dx, dy = real(dp), imag(dp)
        ddx, ddy = real(ddp), imag(ddp)
        f2 = (dx*ddy - dy*ddx)**2
        g2 = (dx*dx + dy*dy)**3
        lim2 = rational_limit(f2, g2, t)
        if lim2 < 0:  # impossible, must be numerical error
            return 0
        kappa = sqrt(lim2)
    finally:
        np.seterr(**old_np_seterr)
    return kappa