B4A Library SVG (Scalable Vector Graphics) library.

In this thread bergapappa pointed out the existence of this SVG rendering library for Android. Having played with it to assess its viability I decided it would not be much more work to go ahead and tidy up and comment my test code and post it as a Basic4android library.

The Basic4android library is a very thin wrapper over the simple API of the rendering library. That library is released under the Apache 2.0 license, a link to the license is included in the library help.

The library implements what it calls "a subset of the SVG Basic 1.1 specification". You will probably need to do some research and some trial and error to utilise this library to its full extent.

EDIT:- Version 1.1 now posted. See post #4 for details.

EDIT:- Version 1.2 now posted. See post #5 for details.

EDIT:- Version 1.3 now posted. See post #15 for details.

EDIT:- Version 1.4 now posted. See post #18 for details.

EDIT:- Version 1.5 now posted. See post #19 for details.
 

Attachments

  • SVG1.5.zip
    50.6 KB · Views: 2,221
Last edited:

agraham

Expert
Licensed User
Longtime User
Version 1.1 is now posted. Though the svg-android library seems quite a nice bit of work the original author seems to have not reponded to bug fix requests so another programmer has created a fork of the source, applied some fixes and made some improvements.

Version 1.1 now posted includes that later fork with the source compiled into the Basic4android library. This should make it easier for me to see the exact Java source code line where any errors are occcurring which was not possible with the original separate library and so raises the possibility of fixing any bugs that surface - assuming that I can understand what is going on!

If you previously used version 1.0 of this library delete the svg-android-1.1 jar and xml files as they are out of date and no longer needed.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Version 1.2 now posted. While trawling through the code understanding what it was doing I noticed a typo that disabled stroke-linecap, now fixed. I have trawled the source code looking for Elements and Attributes and have created a spreadsheet that is included in the library archive. Hopefully this is better than no documentation at all.

Having access to the source I now have a fairly good idea of how the library works and can modify and extend it if necessary. If you find a problem then make the smallest possible SVG file that displays it and post it and I'll have a look at fixing it.
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
I've spent a couple of hours today playing with the SVG library. I know very little about the spec, and I am using inkscape to generate the svg xml files, as I have used it before.

There are a couple of things I've noticed, which may save others some time.

Searching through the internet suggested that using the option to save optimized SVG was sensible for use with the Basic specification, this gives you some choices on how to optimize the output. One of which is simplify colours. This changes the hex output for white from xffffff to xFFF, which is not understood by the library and gets rendered as blue.

The second thing is that nested groups are not supported, the output from which is not displayed. As the whole file is already in a group, any groups created make their contents disappear.

@AGraham if you have the time, would you mind checking if these are outside of the subset that is implemented or just not working.

Thanks

Steve
 

agraham

Expert
Licensed User
Longtime User
I can confirm that simplified colours are not supported. If necessary I could implement it but I'd rather not as it's a bit inefficient as the library is largely cas-insensitive at the moment.

I would have expected nested groups to work as there does seem to be provision in the code for multiple levels of hiding of groups. Can you post a simple SVG example that doesn't work for you and I'll trace it through.

Also note despite being implied in programs source code comments the quadratic bezier path functions Q/q and T/t are not implemented. I'm looking at that.
 

stevel05

Expert
Licensed User
Longtime User
I don't think simplified colours are really necessary, it's now noted to avoid them, hopefully this thread will be found by anyone coming across it.

I've included an example of the nested group problem in a test project.

I don't think I would have come across the quadratic bezier path functions for my project, but noted. Thanks
 

Attachments

  • svgTest.zip
    24.2 KB · Views: 676

agraham

Expert
Licensed User
Longtime User
The problem is not in grouping, in fact it is the nested group that draws in your example. The problem is that the library seems to be deliberately structured so that rect, line, circle, ellipse, polygon, polyline, path & text are only drawn and/or filled if a stroke or fill attribute is present for each of them.

So in the unnested group neither rect draws because neither has a stroke attribute.

I know little about SVG but as this behaviour is deeply embedded in the structure of the library I wonder if this is a requirement of SVG Basic rather than an oversight by the original author and his successor who appear to know what they are doing with SVG.
 

stevel05

Expert
Licensed User
Longtime User
Thanks for that, I did look for an editor that stated it was SVG Basic compliant, but to no avail. I guess the only thing to do is either understand enough about the svg Basic standard to be able to edit the output of an editor, create the files by hand, or play with them until it works!
 

agraham

Expert
Licensed User
Longtime User
rect, line, circle, ellipse, polygon, polyline, path & text are only drawn and/or filled if a stroke or fill attribute is present for each of them
This is not in fact true, as I found out when I put a background in. In the absence of a stroke or fill attribute they default to drawing as black fill. I think I can fix this for the next version so they properly inherit from any settings in the group element.
 

stevel05

Expert
Licensed User
Longtime User
Thanks Andrew, I like the idea of SVG's as they can provide a consistent image across the differing devices, provided you take care of the aspect ratio differences. Unlike scaling bitmaps, the quality of which I find varies.

I probably won't use them extensively, but it's nice to know they will work when you do need them, so thanks for taking the time to make that happen.

Steve
 

agraham

Expert
Licensed User
Longtime User
Unlike scaling bitmaps, the quality of which I find varies.
Unfortunately, because you are rendering a more detailed drawing to a limited resolution bitmap, you will see the same variability in quality as the SVG is generated at full resolution then scaled to be drawn on the canvas.
 

stevel05

Expert
Licensed User
Longtime User
Yes that makes sense now you've pointed it out as the width & height of the SVG object returns the original sizes.
 

agraham

Expert
Licensed User
Longtime User
Version1.3 now posted. It turns out that the original code was not quite as good an implementation as it claimed. :( This new version has undergone significant internal revision to properly support the inheritance of attribute values and in the process support for the "style" attribute was removed as it did nothing other than providing an alternative syntax for an attribute/value pair. I could reimplement it if necessary.

The Path rules Q/q and T/t for quadratic Bezier curves are now implemented.

"font-family" support is provided for the default Android fonts with supported values of "default", "serif", "sans-serif" and "monospace".

WriteToStream and CreateFromStream allow parsed pictures to be saved and restored.

The SVGitems spreadsheet of supported elements and attributes should now be pretty accurate.
 

stevel05

Expert
Licensed User
Longtime User
Thank you Andrew, I plan to play with it a bit more over the next few days for a project.
 

stevel05

Expert
Licensed User
Longtime User
I loaded V1.3 and tried loading a file I have been working with (on v1.2) and got a com.larvalabs.svgandroid.SVGParseException:java.lang.NullPointerException error.

On checking the file against the spreadsheet, two elements appear in the file that are not on the sheet, these are:

stroke-miterlimit
and
stroke-linecap

Although, they did work on V1.2.

Is the <metadata> just ignored?

Would you mind casting your eye over the attached file?

Thanks

Steve
 

Attachments

  • menu.zip
    950 bytes · Views: 509

agraham

Expert
Licensed User
Longtime User
Version 1.4 posted. It's still a bit buggy as I have restructured parts of it again to parse a lot faster. There was a typo in "rect" that threw a null pointer exception that is fixed but you may still find problems with inheritance until I post the next version. stroke and fill values should inherit OK.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Version 1.5 now posted. I'm pretty pleased with the way this has turned out in the end. This version should be pretty rugged and will substitute missing attribute values with zero and not throw exceptions when it doesn't get exactly what it expects. I have cut and pasted various bits of SVG code from Scalable Vector Graphics (SVG) 1.1 and the functionality that is supported seems fine, although the first one you try is bound to fail!
 

stevel05

Expert
Licensed User
Longtime User
No, I'm glad to report that my test svg loaded OK. Thanks again for working on this.
 
Top