Author Topic: Conversion SVG <-> Sketch XML  (Read 5333 times)

derula

  • Hydlaa Citizen
  • *
  • Posts: 405
  • My main: Jamona Shikon
    • View Profile
    • Ugly Horst Tld.
Conversion SVG <-> Sketch XML
« on: July 11, 2009, 06:55:12 am »
Hi.

I'm not quite sure where to post this (feel free to move it around if you like) or if there's any interest at all. But I think it's quite neat so here I am.

I've written a little utility in Ruby that can convert between SVG files and the XML format Planeshift uses to export sketches / user drawn maps.

The direction Sketch -> SVG is pretty trivial and lossless, however the other way round isn't. In fact my tool ignores mostly everything in the SVG file - except things Planeshift understands. And by "understands", I mean: can also be achieved through the in-game editor. I don't know if that's the same. Anyway, here an example:



For those of you who know Ruby (I'ld think most of you do ;D), there's also the (fully documented) PlaneShiftSketchXML class which allows you to easily modify the XML files yourself. Example (from the documentation):

Code: [Select]
require 'planeshift_sketch_xml'
Dir.foreach('~/.PlaneShift/sketches') do |name|
  sketch = PlaneShiftSketchXML.load_from(name)
  sketch.add_text(sketch.width - 300, sketch.height - 50, 'signature')
  sketch.save_to(name)
end

This would add a signature to all of your saved sketches... I know, somewhat limited example, just to show how simple it is.

Main drawback of converting SVG files to sketches is the 200 elements limit Planeshift sets on the imported sketches. On the other hand, if this limit wasn't there I wouldn't have released this, because who knows what noob would spam the server with Megabytes of XML...

To run the thing, you need to have Ruby 1.8 installed, either through your distribution's package system (Linux) or as a download package from here. Simply open a console window, navigate to the folder you've extracted the zip file to, and type 'ruby svg2sketchxml.rb' or 'ruby sketchxml2svg.rb', without the quotes, appending the name of the source and dest files. For example:

Code: [Select]
ruby svg2sketchxml mymap.svg mymap.xml
You can then copy the mymap.xml file into the sketches subfolder of your PS settings directory and import it from within the game.

[Under Linux, you can alternatively run './<script>.rb' instead of 'ruby <script>.rb']

Feel free to report any bug, but please don't ask about how to get ruby running here or how to code in it... this is not the place for it.

I hope some of you find it useful. Here's the download: PlaneShift Sketch XML API, SVG converters and documentation

Please read the text file in the icons subdirectory if you can't see any icons in SVG files converted from sketches. AFAIK, PS license forbids me to bundle those icons with the zip, thus I've written a description where to find them.

geloren

  • Traveller
  • *
  • Posts: 41
    • View Profile
Re: Conversion SVG <-> Sketch XML
« Reply #1 on: July 13, 2009, 08:15:31 am »
Some very nice work you've done here. *geloren is quite impressed*

Thumbs up, that's all i can say.

greetz

weltall

  • Associate Developer
  • Veteran
  • *
  • Posts: 1671
    • View Profile
    • http://weltall.heliohost.org
Re: Conversion SVG <-> Sketch XML
« Reply #2 on: July 13, 2009, 09:19:04 am »
i agree quite a nice work keep it up  :thumbup:

derula

  • Hydlaa Citizen
  • *
  • Posts: 405
  • My main: Jamona Shikon
    • View Profile
    • Ugly Horst Tld.
Re: Conversion SVG <-> Sketch XML
« Reply #3 on: July 18, 2009, 02:50:48 pm »
Thanks a lot to both of you... I'ld love to see any result if anyone actually uses it :)
(and I can't wait to continue playing... just one more week... ^.^)

weltall

  • Associate Developer
  • Veteran
  • *
  • Posts: 1671
    • View Profile
    • http://weltall.heliohost.org
Re: Conversion SVG <-> Sketch XML
« Reply #4 on: July 18, 2009, 03:09:58 pm »
i got this error while trying to convert a pic which was a bit complex (most probably using unsupported features). maybe it could be an idea to skip these  unsupported statement?
SVG2SketchXML 0.1
Simple SVG path to PlaneShift Sketch XML converter
Copyright (c) 2009 by Simon Paridon

Converting /home/weltall/Desktop/disegno.svg...
./svg2sketchxml.rb:345:in `x': wrong number of arguments (0 for 1) (ArgumentError)
   from ./svg2sketchxml.rb:345:in `parse_path'
   from ./svg2sketchxml.rb:196:in `parse_group'
   from /usr/lib/ruby/1.8/rexml/element.rb:892:in `each'
   from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'
   from /usr/lib/ruby/1.8/rexml/element.rb:892:in `each'
   from /usr/lib/ruby/1.8/rexml/element.rb:393:in `each_element'
   from ./svg2sketchxml.rb:195:in `parse_group'
   from ./svg2sketchxml.rb:243:in `parse_group'
   from /usr/lib/ruby/1.8/rexml/element.rb:892:in `each'
   from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'
   from /usr/lib/ruby/1.8/rexml/element.rb:892:in `each'
   from /usr/lib/ruby/1.8/rexml/element.rb:393:in `each_element'
   from ./svg2sketchxml.rb:241:in `parse_group'
   from ./svg2sketchxml.rb:95:in `convert'
   from ./svg2sketchxml.rb:66:in `convert'
   from ./svg2sketchxml.rb:419
   from ./svg2sketchxml.rb:9:in `each_pair'
   from ./svg2sketchxml.rb:8:in `upto'
   from ./svg2sketchxml.rb:8:in `each_pair'
   from ./svg2sketchxml.rb:414

derula

  • Hydlaa Citizen
  • *
  • Posts: 405
  • My main: Jamona Shikon
    • View Profile
    • Ugly Horst Tld.
Re: Conversion SVG <-> Sketch XML
« Reply #5 on: July 18, 2009, 08:10:40 pm »
i got this error while trying to convert a pic which was a bit complex (most probably using unsupported features). maybe it could be an idea to skip these  unsupported statement?

No, this was a bug I only discovered today... along with many others... I'm sorry, they were all pretty obvious :)

I've also added a more precise text conversion. It now uses the Cup and Talon font in converted SVG files (can either be viewed with Firefox or another browser that supports downloadable fonts, or if you install the Cup and Talon font into your system). I've added the font to the package, since it's from here, and it's Freeware, so doesn't apply to the Planeshift license (please correct me if I'm wrong, and I'll remove it from the download package again).

The new version can be downloaded from the same source (here).

Edit: Sorry, it had another stupid bug... I think I fixed it from here, but couldn't really test it. This should only apply to svg2sketchxml.
« Last Edit: July 20, 2009, 10:53:13 am by derula »

weltall

  • Associate Developer
  • Veteran
  • *
  • Posts: 1671
    • View Profile
    • http://weltall.heliohost.org
Re: Conversion SVG <-> Sketch XML
« Reply #6 on: July 22, 2009, 02:13:46 am »
similar one
SketchXML2SVG 0.2
Simple PlaneShift Map XML to SVG converter
Copyright (c) 2009 by Simon Paridon

Converting /home/stefano/Desktop/disegno.svg...
./sketchxml2svg.rb:54: undefined method `width' for nil:NilClass (NoMethodError)
   from ./sketchxml2svg.rb:9:in `each_pair'
   from ./sketchxml2svg.rb:8:in `each_index'
   from ./sketchxml2svg.rb:8:in `each_pair'
   from ./sketchxml2svg.rb:48

derula

  • Hydlaa Citizen
  • *
  • Posts: 405
  • My main: Jamona Shikon
    • View Profile
    • Ugly Horst Tld.
Re: Conversion SVG <-> Sketch XML
« Reply #7 on: July 22, 2009, 01:29:17 pm »
similar one

Er... okay that's not my fault :) you tried to use XML2SVG on an svg file. Probably typo.
Yeah, I know, I should probably make it write an error in that case and not simply crash... Functionality first, error handling last lol. Proper error handling is a todo :)

When I try to use the svg2sketchxml I get this:

This is what you get for not actually testing the darned thing. I'm sorry, I've tested everything first, then added some features and just assumed they would work as well... I'll take a look at this on sunday, probably.

weltall

  • Associate Developer
  • Veteran
  • *
  • Posts: 1671
    • View Profile
    • http://weltall.heliohost.org
Re: Conversion SVG <-> Sketch XML
« Reply #8 on: July 22, 2009, 01:42:34 pm »
similar one

Er... okay that's not my fault :) you tried to use XML2SVG on an svg file. Probably typo.
Yeah, I know, I should probably make it write an error in that case and not simply crash... Functionality first, error handling last lol. Proper error handling is a todo :)

When I try to use the svg2sketchxml I get this:

This is what you get for not actually testing the darned thing. I'm sorry, I've tested everything first, then added some features and just assumed they would work as well... I'll take a look at this on sunday, probably.
ops  :sweatdrop: sorry eheh

my result: http://img195.imageshack.us/img195/3751/schermata41.png really nice indeed :) (this was without limits)
« Last Edit: July 22, 2009, 01:53:54 pm by weltall »

derula

  • Hydlaa Citizen
  • *
  • Posts: 405
  • My main: Jamona Shikon
    • View Profile
    • Ugly Horst Tld.
Re: Conversion SVG <-> Sketch XML
« Reply #9 on: July 24, 2009, 07:51:23 pm »
my result: http://img195.imageshack.us/img195/3751/schermata41.png really nice indeed :) (this was without limits)

Nice ;) though the lines look a bit weird in PS. And I think some lines are missing, no?

@Llyn: I have a quick fix four your error: open svg2sketchxml.rb in a text editor, navigate to line 152, and replace the following:
Code: [Select]
node.attributes['style'].match STROKE or node.attributes['style'].match FILLwith
Code: [Select]
style = (node.attributes['style'] or '')
style.match STROKE or style.match FILL
(that's not a really good fix... hell it's stupid, it is very specific to your error. But it will fix it until I make a better solution. This might include using a proper css parser... but I'm not sure yet.
« Last Edit: July 24, 2009, 08:45:36 pm by derula »

weltall

  • Associate Developer
  • Veteran
  • *
  • Posts: 1671
    • View Profile
    • http://weltall.heliohost.org
Re: Conversion SVG <-> Sketch XML
« Reply #10 on: July 25, 2009, 12:11:31 am »
yes it seems some lines are missing i don't know why

derula

  • Hydlaa Citizen
  • *
  • Posts: 405
  • My main: Jamona Shikon
    • View Profile
    • Ugly Horst Tld.
Re: Conversion SVG <-> Sketch XML
« Reply #11 on: July 25, 2009, 02:50:26 pm »
yes it seems some lines are missing i don't know why

Most likely you've exceeded the 200 elements limit of Planeshift, I'ld assume... did the script show a warning that it did? It might help if you use Inkscape to simplify the paths before converting (might also reduce some of the "scratchy" look of some lines)

Edit: I've opluaded a new version (0.3). News:
- Better reading of color values (now respects svg stroke and fill attributes, parses color values more like in the specification; uses color inheritage where applicable, doesn't write objects with no color at all, .... Still no complete css support though.)
- Error message when trying to convert an invalid file
- Added command line parameter to svg2sketchxml: -cf . This will use the fill color as a stroke color if stroke color is set to none. This was the default in earlier versions, but I figured it's not always wise to do so.
« Last Edit: July 26, 2009, 09:20:55 am by derula »

Dannae

  • Hydlaa Citizen
  • *
  • Posts: 250
    • View Profile
Re: Conversion SVG <-> Sketch XML
« Reply #12 on: August 13, 2009, 02:35:59 pm »
Hi Derula,

I'd love to be able to use your converter, but so far have not been able to get it to work successfully. The only program I have to produce vector files with is Adobe Illustrator. I've attempted saving very extremely basic files from this program in svg format to test convert such as only 1 simple oval or line but I continually get the following result. I'm using Mac OS 10.5.8 and MacRuby 4.

Converting 1.svg...
svg2sketchxml.rb:480:in `parse_path': undefined method `x' for nil:NilClass (NoMethodError)

If you have any idea about what to try next, I'd appreciate it. Thanks.

derula

  • Hydlaa Citizen
  • *
  • Posts: 405
  • My main: Jamona Shikon
    • View Profile
    • Ugly Horst Tld.
Re: Conversion SVG <-> Sketch XML
« Reply #13 on: August 13, 2009, 09:24:50 pm »
Hi Dannae :D

Okay, the problem here is probably that I was ever only able to test it with svg files created by Inkscape.

If you have any idea about what to try next, I'd appreciate it. Thanks.

The first idea would be to call the converter with the -v switch (ruby svg2sketchxml.rb -v 1.svg 1.xml) and tell me what it says. That might already help.

If it doesn't, there's two other ideas: either you get Inkscape (there's a mac version) and try it with that... or you could somehow send me your SVG file and I'll have a look at why it does that (either through ICQ/MSN per my profile, or via e-mail (PN me and you'll get my e-mail address))...

Also: welcome to the forum :P
« Last Edit: August 13, 2009, 09:27:42 pm by derula »

derula

  • Hydlaa Citizen
  • *
  • Posts: 405
  • My main: Jamona Shikon
    • View Profile
    • Ugly Horst Tld.
Re: Conversion SVG <-> Sketch XML
« Reply #14 on: August 15, 2009, 01:12:26 am »
Hi, thanks for SVG file and output, helped a lot. I've fixed that particular bug, and added support for the <line> element that was in that svg file. New version still downloadable from here.