Here is a way for finding out the bounding box (left, right, width and height) of an SVG path in Ruby. You will need ImageMagick and RMagick for this. In Javascript this is quite simple using the getBBox method, but I couldn’t find a similar thing in Ruby.
First install ImageMagick if you don’t have it already. I like to use Homebrew:
brew install imagemagick
Then install RMagick
gem install rmagick
With these two requirements in place you can find the bounding box using a script like this:
123456789101112131415161718192021222324
require'rubygems'require'RMagick'deffind_bounding_box(path)includeMagick#create a drawing objectdrawing=Magick::Draw.new#create a new image for finding out the offsetcanvas=Image.new(1500,1000){self.background_color='white'}#draw the path into the canvas imagedrawing.pathpathdrawing.drawcanvas#trim the whitespace of the imagecanvas.trim!#here is the bounding box information we are looking for{:x=>canvas.page.x,:y=>canvas.page.y,:width=>canvas.columns,:height=>canvas.rows}end