Thank you for this port, nice work. Here are some things that I found, in case it helps. Line numbers are for nanoSvg.bas in v1.00.
How I tested
I drew about 50 SVG files (37 small test files and some real logos) at 24, 72 and 360 pixels, and compared every drawing with the same file drawn by a browser (Edge), pixel by pixel.
- Shapes, paths, groups, transforms and icons differ from the browser by less than 1 of 255 per color on average, along the edges only. Very good.
- B4J and B4A give exactly the same pixels. B4i differs in a few drawings by less than 0.001 of 255, which is just rounding.
- Time for 360 x 360 pixels, release mode: 2-5 ms in B4J on a desktop, about 3 ms on a recent Android phone, about 75 ms on an iPhone 8 Plus. Most of it is the rasterizing, not the parsing, so on iOS it is worth keeping the bitmap.
Things in the port that I changed
- A crash in CreateGradient (line 1073). This line becomes
nextRef.equals(ref) in B4A and B4J, so it throws a NullPointerException when nextRef is Null:
If nextRef = ref Then Exit
That happens for a gradient without stops whose reference is empty or points at nothing, for example <linearGradient id="a"/> used as a fill. It also happens for any gradient that inherits its stops with a plain href (SVG 2), since only xlink:href is read. The fix is one line before it:
If nextRef = Null Then Exit
If nextRef = ref Then Exit
- B4i does not compile it. A variable named
cmd gives "cmd is not a valid identifier" (Objective-C has a hidden _cmd in every method). It is the parameter of GetArgsPerElement (line 1677) and the local variable in the path parser (line 1963 and on). With another name, everything compiles and runs in B4i 10.31.
- Three named colors (lines 505, 511, 512). The table is packed as blue-green-red:
- orchid is 0xDA70D6 and should be 0xD670DA
- skyblue is 0xEBCD87 and should be 0xEBCE87
- snow is 0xFAFFFA and should be 0xFAFAFF
The other 144 match NanoSVG.
- "transparent" (line 514) is in the color table as 0x000000, so it draws opaque black. I removed it from the table and treat
fill="transparent" and stroke="transparent" like none.
- rgb() with percentages (lines 630-632). This gives 127 for 50 % and 229 for 90 %, where NanoSVG gives 128 and 230, because the C code multiplies as float:
rgbi(0) = RoundF(rgbf(0) * 2.55)
This gives NanoSVG's values:
rgbi(0) = RoundF(rgbf(0) * 255 / 100)
- Render, automatic scaling (lines 232-233). The axis is chosen from the SVG's own proportions and not the view's, so a 200 x 100 image in a 400 x 100 view is drawn 400 x 200 and clipped. In the automatic mode, taking the smaller of the two scales fits the image inside the view:
If mScale = 0 Then
Scale = Scale * Min(ww / gImgW, hh / gImgH)
Else
Scale = Scale * IIf(mScale = 1, ww / gImgW, hh / gImgH)
End If
- Render on iOS. It draws at the view's size, which is in points on iOS, so the image is coarse on a 2x or 3x screen. Drawing at the view's size times
GetDeviceLayoutValues.NonnormalizedScale, and letting the image view fit it, makes it sharp. I replaced Render with a sub that takes a size in pixels and returns a B4XBitmap, which also removes the dependency on XUI Views.
- Default parameter values (
Align As Int = 5 in Render) compile in B4J 10.70 and B4A 14.00, but are a syntax error in B4J 10.50 and B4A 13.40. Maybe worth a line about the versions needed.
Things that come from NanoSVG itself
These are not mistakes in the port, the C code does the same. I changed them in my copy, since a browser draws these files differently:
- A gradient in the shape's own box (objectBoundingBox, the default) written with plain numbers,
x2="1", is drawn as one color. NanoSVG only takes percentages there. A gradientTransform on such a gradient turns around the wrong point, and a slanted or radial gradient in a box that is not square gets the wrong shape.
- A gradient is read at each pixel's corner and not its middle, so it is half a pixel off. It shows at small sizes.
- A shape is drawn wherever it stands: inside
<metadata>, inside an element of another namespace, or inside a <clipPath> that is not in <defs>.
- A style sheet inside CDATA (
<style><![CDATA[ ... ]]></style>, as older Illustrator and Inkscape write it) is not read, so everything that gets its color from it is black.
- A comment ends at the first
>, so the second of two elements in the same comment is drawn.
- currentColor is drawn gray. I let the caller give the color, which is handy for icons.
Limits to know about
Not drawn: text, clipPath (the shape is drawn unclipped), use, masks, filters and embedded images. A style rule is only read when it is for a class (
.name, or several with commas).
rgba() and
hsl() are drawn gray. Files written by Inkscape, with its own elements and attributes, are drawn fine.
The license
NanoSVG is under the zlib license, which asks that its notice stays in source distributions and that altered versions are marked as such. It would be good to have the notice at the top of the class.
I am happy to share the changed code if you want it.