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,253
Last edited:

joseluis

Active Member
Licensed User
Longtime User
This library is a blessing for me now! because it enables me store the game graphics in svg so that it will adapt to any screen size. Thank you! I made some tests with my drawings and I've not had many problems when using the optimized svg output from Inkscape. Sometimes it doesn't pick gradients, and it changes some colors but I think I could live with that.

By the way, there's a new svg library for android licensed as LGPL, with a big list of supported features. But I don't know how it compares with the one you made because I can't find an equivalent list of features for it. Any thoughts?
 

joseluis

Active Member
Licensed User
Longtime User
I made a program that tests the correct rendering using the w3 SVG examples, by comparing them with the corresponding png image. The source code is attached to this post.

The only modifications I've done to the original SVG files was to change the units from centimeters, inches and % to simple integers (when it was appropiate), in order to avoid the NumberFormatException.
e.g. "12cm" was changed to "1200" and "4in" was changed to "4000".
Additionally, texts were transparent when the fill property wasn't specified, so in those cases I added a fill="black".
Also had to change color formats in 3-hex to 6-hex to be correctly rendered. e.g. from "#F60" to "#FF6600".

@agraham I would like to know if you intend to fix this library, because I plan to use it very much, and if you don't feel like it, I would love to have access to the source code so I can try to do it myself. Thank you in advance.

----------------------------
This is the summary of the current state. I've marked with >>> the failed tests that I think are the most important ones:
  1. w3_basic_circle01 PERFECT
  2. w3_basic_ellipse01 INCOMPLETE (bad rotation coordinates)
  3. w3_basic_line01 PERFECT
  4. w3_basic_polygon01 PERFECT
  5. w3_basic_polyline01 PERFECT
  6. w3_basic_rect01 PERFECT
  7. >>>w3_basic_rect02 INCOMPLETE (no rounded corners, bad rotation coordinates)
  8. w3_coords_initalcoords PERFECT
  9. w3_coords_nested OK (text not trimmed)
  10. w3_coords_newcoordsys OK (text not trimmed)
  11. w3_coords_origcordsys OK (text not trimmed)
  12. w3_coords_preserveaspectratio ERROR (Underflow in restore)
  13. w3_coords_rotatescale OK (text not trimmed)
  14. w3_coords_skew OK (bad text spacing)
  15. w3_coords_units ERROR FormatNumberException (doesn't underestand Ems or %)
  16. w3_coords_viewbox INCOMPLETE (bad size and no text)
  17. >>>w3_gradient_lingrad01 INCOMPLETE (no gradient after changing units from % to integers)
  18. w3_gradient_pattern01 INCOMPLETE (all black instead of pattern)
  19. w3_gradient_radgrad01 OK (incorrect gradient size after changing units from % to integers)
  20. w3_mask_mask01 INCOMPLETE (no text)
  21. >>> w3_mask_opacity01 INCOMPLETE (no alpha!)
  22. >>> w3_painting_fillrule-evenodd INCOMPLETE (line crossing circles!, inappropiate filing, no marker)
  23. w3_painting_fillrule-nonzero INCOMPLETE (line crossing circles!, no marker)
  24. w3_painting_inheritance ERROR (NumberFormatException)
  25. w3_painting_linecap BAD (doesn't show anything at all)
  26. w3_painting_linejoin BAD (doesn't show anything at all)
  27. w3_painting_marker-simulated PERFECT
  28. w3_painting_marker OK (no marker)
  29. >>> w3_paths_arcs01 INCOMPLETE (bad arcs and paths rendering)
  30. w3_pats_arcs02 INCOMPLETE (bad arcs and no text)
  31. w3_paths_cubic01 BAD (doesn't show anything)
  32. w3_paths_cubic02 BAD (doesn't show anything)
  33. w3_paths_cuad01 PERFECT
  34. w3_paths_triangle01 PERFECT
 

Attachments

  • SVG_Demo.zip
    175 KB · Views: 450
Last edited:

agraham

Expert
Licensed User
Longtime User
There are no svg or png files in your archive so the demo won't run. I only ever claimed a subset of SVG and not a full implementation. I've given up on Android so do your worst with the Eclipse project attached..
 

Attachments

  • SVG1.5.zip
    53.7 KB · Views: 450

joseluis

Active Member
Licensed User
Longtime User
It's true!! I always forget that zip exporting only includes the files that are added to the Files tab. It's fixed now :)

Thank you so much agraham for all you have done! I assure you I'll do my best worst with this library ;)
 

corwin42

Expert
Licensed User
Longtime User
I've given up on Android so do your worst with the Eclipse project attached..

Sad to hear this. I wondered why you were quite calm in the forum in the past (at least in the android section). Any reason for this decision?

Anyway thanks for your great libraries you have developed for B4A. They are very valueable.
 

vb1992

Well-Known Member
Licensed User
Longtime User
I don't think you know how much you helped us out when we needed
support with the reflection library. (and still need it!)
:sign0188:
:sign0098:

<insert badges here now>
 

airblaster

Active Member
Licensed User
Longtime User
I hope this is ok since agraham said he discontinued the project:
I've taken the Eclipse source and updated the SVG Parser to the newest revision of svg-android-2. The version number I used is 1.6.
 

Attachments

  • SVG.zip
    40.5 KB · Views: 457

airblaster

Active Member
Licensed User
Longtime User
Unfortunately it seems like the new library revision used in unofficial version 1.6 released by me includes changes that are very bad for performance. So you might want to stay with the latest official release (Version 1.5).
 

Magma

Expert
Licensed User
Longtime User
hi there...

i am getting this...

your examples work but...

i am exporting/saving with corel x6 (for example) wmf or mine designs (simple) 2-3 or more curves getting this message when trying to load them:


SVGWrapperInitialize2 (java line: 130)

com.larvalabs.svgandroid.SVGParseException: java.lang.NumberFormatException:

at com.larvalabs.svgandroid.SVGParser.parse(SVGParser.java:236)
at com.larvalabs.svgandroid.SVGParser.getSVGFromInputStream(SVGParser.java:99)
at anywheresoftware.b4a.agraham.svg.SVGWrapper.Initialize2(SVGWrapper.java:130)
at anywheresoftware.b4a.agraham.svg.SVGWrapper.Initialize(SVGWrapper.java:121)
at anywheresoftware.b4a.agraham.svg.main._activity_create(main.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.agraham.svg.main.afterFirstLayout(main.java:89)
at anywheresoftware.b4a.agraham.svg.main.access$100(main.java:16)
at anywheresoftware.b4a.agraham.svg.main$WaitForLayout.run(main.java:74)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)

at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException:
at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl(Native Method)
at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321)
at java.lang.Float.parseFloat(Float.java:288)
at com.larvalabs.svgandroid.SVGParser.getFloatAttr(SVGParser.java:805)
at com.larvalabs.svgandroid.SVGParser.getFloatAttr(SVGParser.java:789)
at com.larvalabs.svgandroid.SVGParser.access$3(SVGParser.java:787)
at com.larvalabs.svgandroid.SVGParser$SVGHandler.startElement(SVGParser.java:1314)
at org.apache.harmony.xml.ExpatParser.startElement(ExpatParser.java:145)
at org.apache.harmony.xml.ExpatParser.append(Native Method)
at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:506)
at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
at com.larvalabs.svgandroid.SVGParser.parse(SVGParser.java:225)
... 19 more
com.larvalabs.svgandroid.SVGParseException: java.lang.NumberFormatException:
** Activity (main) Resume **
 

Controller

Member
Licensed User
Longtime User
Nice, so there are some issues that you could fix?
- very very small number parsing... 0.234234E-5 (can be fixed manually in the svg text)
- circles sometime show a horizontal line trough the center (can be fixed by converting from circle to path)
(had this issue on one svg, while a copy with modified colors didn't)
- complains about 2 or more colors are required for gradients (can be fixed by removing gradients and all gradient definitions)
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
What's the performance on this? How many shapes can it draw for example, while keeping up a framerate of about 30? Can you say, make a shape of any color (settable at run time) and have black lines/curves drawn over it? I assume it supports scaling, what about rotation?

I want to know cause I'll be cloning the cardassian user interface from Star trek deep space 9 and I'd much rather use this than make my own graphics format cause trigonometry seems to be slow on Android (is there a way to speed that up? PSP for example has a processor for handling trig faster than the main CPU)
 

Controller

Member
Licensed User
Longtime User
Can't say much about speed and all features. For speed, you could render to image first, and (re)use the image. From my experience, there are quite some limits and bugs with certain features.
It seems to have issues loading if there are any fadings effects in the svg file - either used or not. Also there are some viewpoints / relative coordinate issues. And many more.
In general, svg support is not that good, neither in browsers, or professional vector graphic programs.
Big plus, however, is that you can read any svg resource as text and modify it before rendering as image.
Also a bit of annoyance is the debug logs for certain unsupported elements.
Still I'm working on a few projects using svgs, so it's not impossible.
 

fernando.oliboni

Active Member
Licensed User
Longtime User
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.



Hi thanks, great job. How could I work with text fonts? How can I transform fonts into svg using your library? Tks
 

cengolo

Member
Licensed User
Longtime User
hi
does the size of SVG matter for this library? i tried to display the attached svg but i get the error:
com.larvalabs.svgandroid.SVGParseException: java.lang.NullPointerException: Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null object reference
 

Attachments

  • svg-deneme.zip
    43.9 KB · Views: 236
Top