Skip to content

SVGDOM images don't work due to lack of resource providers #307

@mountainstorm

Description

@mountainstorm

Is your feature request related to a problem? Please describe.

We can render SVG to an image using code like that below. This works for paths etc, but images in the SVG don't render as there are no resource providers.

    skia_stream = skia.Stream.MakeFromFile(svg_path)
    skia_svg = skia.SVGDOM.MakeFromStream(skia_stream)

    svg_width, svg_height = skia_svg.containerSize()
    surface_width, surface_height = int(svg_width), int(svg_height)

    surface = skia.Surface(surface_width, surface_height)
    with surface as canvas:
        skia_svg.render(canvas)

    with io.BytesIO(surface.makeImageSnapshot().encodeToData()) as f:
        img = Image.open(f)
        img.save(path, quality=85)

Describe the solution you'd like

Skia provides capability for both file and base64 based href resources - an example of which you be seen here https://github.com/google/skia/blob/main/tools/viewer/SvgSlide.cpp

Specifically this code

    auto predecode = skresources::ImageDecodeStrategy::kPreDecode;
    auto rp = skresources::DataURIResourceProviderProxy::Make(
            skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()), predecode),
            predecode, ToolUtils::TestFontMgr());

    fDom = SkSVGDOM::Builder()
                   .setFontManager(ToolUtils::TestFontMgr())
                   .setResourceProvider(std::move(rp))
                   .setTextShapingFactory(SkShapers::BestAvailable())
                   .make(*stream);

Describe alternatives you've considered

The only work around I've found is to manually parse the SVG and attempt to insert the image yourself - but this results proves challenging for anything but the most trivial files

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions