almosr / android-svg-code-render

Convert SVG files into Java source and use it in your Android app
Apache License 2.0
24 stars 3 forks source link

SVG throws when loading image. #30

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Load a complex SVG.

What is the expected output? What do you see instead?

I expect to see the SVG.

Instead I get a stack trace...

01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764): 
java.lang.NullPointerException
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVGParser.parseColourComponent(SVGParser.java:3254)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVGParser.parseColour(SVGParser.java:3236)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVGParser.parseColourSpecifer(SVGParser.java:3198)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVGParser.parsePaintSpecifier(SVGParser.java:3187)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVGParser.processStyleProperty(SVGParser.java:2652)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.CSSParser.parseDeclarations(CSSParser.java:736)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.CSSParser.parseRule(CSSParser.java:667)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.CSSParser.parseRuleset(CSSParser.java:649)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.CSSParser.parse(CSSParser.java:293)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVGParser.parseCSSStyleSheet(SVGParser.java:3974)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVGParser.endElement(SVGParser.java:776)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:156)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVGParser.parse(SVGParser.java:576)
01-21 16:05:45.683: E/org.jw.jwlibrary.mobile.svg.SvgView(4764):    at 
com.caverock.androidsvg.SVG.getFromInputStream(SVG.java:143)

What version of the product are you using? On what operating system?

1.2 on Android 4.0.x

I'm drawing the SVG straight to the canvas in OnDraw()

Original issue reported on code.google.com by nadiasve...@gmail.com on 21 Jan 2014 at 9:02

GoogleCodeExporter commented 9 years ago
Hi. Thanks for the report.

Can you possibly attach the file that caused the problem?  If that is not 
possible, can you try reducing the file to a simple form that still exhibits 
the problem?

Original comment by paul.leb...@gmail.com on 21 Jan 2014 at 9:17

GoogleCodeExporter commented 9 years ago
I'll do you one better, here's the fix:

// Parse a colour component value (0..255 or 0%-100%)
   private static int parseColourComponent(final TextScanner scan)
         throws SAXException {
      float comp = scan.nextFloat();
      if (scan.consume('%')) {
         // Spec says to follow CSS rules, which says out-of-range values
should
         // be clamped.
         comp = comp < 0 ? 0 : comp > 100 ? 100 : comp;
         return (int) (comp * 255.0 / 100.0);
      } else {
         comp = comp < 0 ? 0 : comp > 255 ? 255 : comp;
         return (int) comp;
      }
   }

Original comment by nadiasve...@gmail.com on 21 Jan 2014 at 9:47

GoogleCodeExporter commented 9 years ago
Hmmm...  Neither SVG1.1, SVG 1.2 or CSS 2.1 allows float values in "rgb()" 
colour values.  So technically this is not a bug.  SVG2 does allow float 
values, however.

Was the file generated by a commercial program, or was it hand-written?  
Chances are I will still merge this patch, since it is better to be permissive. 
 But it would still be good to know.

Original comment by paul.leb...@gmail.com on 21 Jan 2014 at 10:23

GoogleCodeExporter commented 9 years ago
Commercial. Adobe InDesign.

Original comment by nadiasve...@gmail.com on 21 Jan 2014 at 10:39

GoogleCodeExporter commented 9 years ago
Just so I know I am coping properly with the type of values InDesign generates, 
would you mind pasting an actual example an faulty rgb() value from one of your 
file please?

Better still, an actual file would still be useful for my test suite if you 
have one you can share.  Thanks.

Original comment by paul.leb...@gmail.com on 26 Jan 2014 at 1:54

GoogleCodeExporter commented 9 years ago
Correction to my comment #3.  Number (float) values are allowed in colour 
components that are percentages.  This is allowed by the CSS spec.  The fact 
that the SVG spec says integer is an error.

I now suspect that InDesign wasn't producing bad files at all and was just 
using floats in percentage colour components (eg. "rgb(0, 0, 33.33%)").

Original comment by paul.leb...@gmail.com on 27 Jan 2014 at 9:25

GoogleCodeExporter commented 9 years ago
Fixed.

Issue30: Allow numbers (floats) in rgb colour components.

Original comment by paul.leb...@gmail.com on 27 Jan 2014 at 9:26