skia-python icon indicating copy to clipboard operation
skia-python copied to clipboard

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

Open mountainstorm opened this issue 1 year ago • 2 comments

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

mountainstorm avatar Jan 25 '25 18:01 mountainstorm

But you are talking about SVG having the capability of embedding a image from disk? I believe we don't bind most of the SVGDom methods, at the moment.

HinTak avatar Jan 25 '25 19:01 HinTak

Okay, we actually don't bind SkSVGDOM::Builder at the moment. https://github.com/google/skia/blob/42110712af31e9b3e7f7e80cf59a185998f236bb/modules/svg/include/SkSVGDOM.h#L27 . Contribution welcome...

HinTak avatar Jan 25 '25 19:01 HinTak