开发者

Which is the best SVG to NSBezierPath library/class?

I am looking for the best SVG to NSBezierPath parser, it's ok if the class/library only takes a string of SVG commands, here is an example:

M 435.722 403.542 h -232.44 v -293 h 232.44 c 0 0 0 35.108 0 81.019 c 0.049 2.551 0.079 5.135 0.154 7.748 c -0.208 15.567 12.1 13.618 19.624 28.192 c 2.584 5.005 5.875 30.5 4.875  34.5 c -7 19 -28.707 22.875 -24.653 44.854 c 0 2.667 0 5.31 0 7.923 C 435.722 364.425 435.722 403.542 435.722 403.542 z

The class/library doesn'开发者_运维知识库t neccessarily have to parse the .svg file itself, but it should be able to handle all SVG commands and support relative as well as absolute coordinates (in other words, it should be fully compatible with the SVG 1.1 specs found here).

I have found some classes on the web, but they were all limited and failed with my svg commands above. Which is the best SVG to NSBezierPath parser around these days?


Write a program to parse the SVG file using an XML parser.

This will give you your array of paths which you can feed one by one into the following code:

https://www.touchnoc.com/one_way_ticket_from_svg_to_nsbezierpath

** Make sure to look for the code for iOS on the above page.

However, the above code only works for absolute path coordinates not relative path coordinates. Adobe Illustrator outputs only relative path coordinates. You can open your svg from illustrator in a program called sketch (http://www.bohemiancoding.com/sketch/) and export the svg will give you your file in absolute path coordinates.

Your paths are failing probably because they are using relative coordinates - small c, where most of the scripts I found can convert absolute, or big C.

And, there you go. The whole process is not easy but it is doable! I am speaking from experience.


I don't think something like that is available.

But you should be able to parse that yourself very easily, since the commands map directly to NSBezierPath methods.

It's also helpful that the command comes first, so you could do the following:

  • split into array by spaces
  • take first element and map to command
  • take next x elements as parameters (x is known depending on command)
  • call the matching method on NSBezierPath

If the string is very long, you can also do the same by parsing it stream-wise.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜