javaparsingsvgbatik

Reading SVG path in Java


I'm reading text files in java where I use the scanner class to read text in SVG format. I have seen answers that recommend Batik. I would like to know how to use Batik only on the path data in SVG format. I want to evaluate the path and extract points that would trace out the path. Can I do this using Batik without having to evaluate the entire file in Batik?

I was trying to create my own path parser but I don't have enough time or skill to do so. I am evaluating tags in SVG format by splitting them using < and /> and I am stuck on evaluating the d attribute of paths. Here's an example of a path that I need to evaluate:

<html>
<body>

<svg width="10000" height="1000">
<path id="square" fill="#0000FF" 
d="M351.3,251 l-3.1-2.2c-0.3-0.2-0.3-0.5-0.1-0.8l2.2-3.1c0.2-0.3,0.5-0.3,0.8-0.1l3.1,2.2
c0.3,0.2,0.3,0.5,0.1,0.8l-2.2,3.1C355,251.1,354.6,251.2,354.3,251z"/>

</body>
</html>

I need the points along this path.


Solution

  • The path parsing with Batik looks fairly simple:

    It appears like you just have to create a class that implements the PathHandler interface.

    class MyPathHandler implements PathHandler
    {
      void  startPath() { ... }
      void  movetoAbs(float x, float y)
      ... etc...
      void  endPath() { ... }
    }
    

    Then do:

      PathParser parser = new PathParser();
      parser.setPathHandler(new MyPathHandler());
      parser.parse("M351.3,251 l-3.1-2.2 ... snip ... C355,251.1,354.6,251.2,354.3,251z");
      parser.doParse();
    

    The moveToAbs() method will be called whenever the PathParser encounters an M command etc.

    If that doesn't work then I was able to find at least four or five instances of SVG path parsing code in Java or JS in about one minute of googling.

    As for the sampling part. That's a lot trickier. You'll need code to measure the length of quadratic and cubic beziers, and arcs. The latter will probably involve converting the arcs (A/a) to centre parameterization (theres information in the spec on how to do that). It's not trivial to do all this. There'll be code for it, if you can find it, in Batik, Firefox, Chrome, Android etc. Perhaps there is also code out there that someone has already built.

    It would be easy if you were doing it in a browser. There have already been questions asked and answered here on that.