Exemple #1
0
def svgToPath(filename):
    """Return QPainterPath instance from an svg file.
       No colors will be included. If the file contains multiple paths,
       they will be connected to form one single path.
       It will also return a boolean indicates if the path is closed or not."""
    path = QPainterPath()
    info = findPath(filename)
    start = QPointF(0, 0)
    last_cp = start     # last control point, for S cubic bezier curve.
    last_qp = start     # last control point, for T quadratic bezier curve.
    for idx in range(len(info)):
        line = info[idx]
        cmd = line[0]
        if (cmd.upper() == 'Z'):
            path.closeSubpath()
            continue

        coords = re.split(r'\s+|,|(?<=\d)(?=-)', line[1])
        if (cmd.upper() == 'V'):
            # only last coordinate matters.
            coord = eval(coords[-1])
            verticalLineTo(path, coord, cmd)
            continue

        if (cmd.upper() == 'H'):
            # only last coordinate matters.
            coord = eval(coords[-1])
            horizontalLineTo(path, coord, cmd)
            continue

        # pair two values into one
        coords = [x+','+y for x, y in zip(coords[::2], coords[1::2])]
        coords = list(map(getPoint, coords))

        if (cmd.upper() == 'M'):
            # if m is at the start of the path
            if (line == info[0]):
                start = coords[0]
                moveTo(path, start, 'M')
            # m is not at the start of the path
            else:
                path.closeSubpath()
                lineTo(path, coords[0], cmd)
            for i in range(1, len(coords)):
                lineTo(path, coords[i], cmd)
            continue

        if (cmd.upper() == 'L'):
            for coord in coords:
                lineTo(path, coord, cmd)
            continue

        if (cmd.upper() == 'C'):
            for i in range(len(coords)//3):
                # Saving coordinates for smoothcurve command
                last_cp = cubicTo(path, *coords[i*3:i*3+3], absolute=cmd)
            continue

        if (cmd.upper() == 'S'):
            if not (info[idx-1][0].upper() in 'SC'):
                last_cp = path.currentPoint()
            for i in range(len(coords)//2):
                last_cp = smoothCubicTo(path, last_cp,
                                        *coords[i*2:i*2+2], absolute=cmd)
            continue

        if (cmd.upper() == 'Q'):
            for i in range(len(coords)//2):
                # Saving coordinates for T smooth curve command
                last_qp = quadTo(path, *coords[i*2:i*2+2], absolute=cmd)
            continue

        if (cmd.upper() == 'T'):
            if not (info[idx-1][0].upper() in 'QT'):
                last_qp = path.currentPoint()
            for coord in coords:
                last_qp = smoothQuadTo(path, last_qp, coord, absolute=cmd)
            continue
        raise Exception('svg file contains command {}, which is not supported'
                        .format(cmd))

    closed = True
    if ((abs((path.currentPosition() - start).x()) > 1) and
       (abs((path.currentPosition() - start).y()) > 1)):
        closed = False
    path.translate(-start)
    path.translate(-path.boundingRect().center())
    return path, closed