Displaying SVG from string as picture object works ok on MacOs but not on Windows

Inside a class, I define a string variable to store the content of a svg file (XML) so I can manipulate its color and opacity and then convert it to a picture object with:

Var svgPicture As Picture = Picture.FromData(svgStringWithNewColor)

so, svgPicture then displays on a DesktopCanvas (same with Canvas) on Paint method with DrawPicture. On MacOs it shows ok but not on Windows. If I drop the svg file to Xojo it displays fine on a canvas with Backdrop property.

Tryed converting XML string to memory block with this, same result:

Var m As New MemoryBlock(1024)
m.CString(0) = svgStringWithNewColor
Var svgPicture As Picture = Picture.FromData(m)

Does anyone knows what could be the cause of the problem?

macOS is doing the heavy lifting for you with SVGs, which are supported out of the box by the operating system. Windows didn’t have a native mechanism until the Windows 10 Anniversary Update. Being that it is an extension of Direct2D, I’m sure Xojo could extend the framework to support this, but you’d need to open a feature request.

As for the need to draw SVGs on Windows from your Xojo applications, there are a number of solutions available including Einhuiger’s, open source implementations ([1], [2]), and my own take (GraffitiSVG) which uses the native funcitonality on macOS and JavaScript on Windows.

2 Likes

Do you know where this is documented? I didn’t find any Apple documentation that says that CoreGraphics can gender SVGs.

I do not, and I don’t know how long it has been functional. I suspect it was added around the time of Xcode 12 to support SVGs in Asset Catalogs. So 2020 or thereabout?

Both Einhuger and MBS have plugins that can rasterise an SVG into a Xojo picture.

(we use the MBS plugin which produces a better result than the Einhuger plugin)

3 Likes

Good to know. Thanks, Kevin.

One:

Two:

2 Likes

Thanks you all. I’m going to try both options.
Anthony, very clear explanation. I look GraffitiSVG but I need to display the picture on the paint event of a canvas object inside a class, can’t use a DesktopHTMLViewer.

Greetings!

Since September/2020

Universal scalable format: SVG

Interesting. However, the discussion suggests that Xcode renders vector formats down to a pixel format such as PNG. If this is true, Xcode could be using an internal library to do it, since I still don’t see any macOS framework support for it.

I tried both options. With option one (xojo-drawsvg) and Xojo 2023 it won’t work, I think because the need of using old GDI Plus. Option two (Xojo-SVGDocument) works but the resulting image is quite flawed, It differs quite a bit from the original.

Nope, it is framework integrated, as PDF.

The simple proof is that Xojo’s Picture.FromData(svgString) renders the SVG XML just asking the macOS Framework to do so and it works.

SVG IS the resolution independent graphics format of the market.

It started by web and then basically became the universal format for all 2D vector drawings

Windows has API support for it since Windows 10.0.14393 (in 2016), and enhanced it in 2017, only Xojo does not use it.

It’s in a private OS framework called CoreSVG. Supported on both macOS and iOS/iPadOS. The OS just handles it. Here’s a Swift implementation if you want to browse it.