def transform(transform_string, font_size, normalized_diagonal):
    """Get a matrix corresponding to the transform string."""
    # TODO: merge with gather_anchors and css.validation.properties.transform
    transformations = re.findall(
        r'(\w+) ?\( ?(.*?) ?\)', normalize(transform_string))
    matrix = Matrix()

    for transformation_type, transformation in transformations:
        values = [
            size(value, font_size, normalized_diagonal)
            for value in transformation.split(' ')]
        if transformation_type == 'matrix':
            matrix = Matrix(*values) @ matrix
        elif transformation_type == 'rotate':
            if len(values) == 3:
                matrix = Matrix(e=values[1], f=values[2]) @ matrix
            matrix = Matrix(
                cos(radians(float(values[0]))),
                sin(radians(float(values[0]))),
                -sin(radians(float(values[0]))),
                cos(radians(float(values[0])))) @ matrix
            if len(values) == 3:
                matrix = Matrix(e=-values[1], f=-values[2]) @ matrix
        elif transformation_type.startswith('skew'):
            if len(values) == 1:
                values.append(0)
            if transformation_type in ('skewX', 'skew'):
                matrix = Matrix(
                    c=tan(radians(float(values.pop(0))))) @ matrix
            if transformation_type in ('skewY', 'skew'):
                matrix = Matrix(
                    b=tan(radians(float(values.pop(0))))) @ matrix
        elif transformation_type.startswith('translate'):
            if len(values) == 1:
                values.append(0)
            if transformation_type in ('translateX', 'translate'):
                matrix = Matrix(e=values.pop(0)) @ matrix
            if transformation_type in ('translateY', 'translate'):
                matrix = Matrix(f=values.pop(0)) @ matrix
        elif transformation_type.startswith('scale'):
            if len(values) == 1:
                values.append(values[0])
            if transformation_type in ('scaleX', 'scale'):
                matrix = Matrix(a=values.pop(0)) @ matrix
            if transformation_type in ('scaleY', 'scale'):
                matrix = Matrix(d=values.pop(0)) @ matrix

    return matrix
Get a matrix corresponding to the transform string.
weasyprint/svg/utils.py