I am working on a project where I have to create design assets dynamically in the browser. For the past few months I have been using canvas to do this, but I am now being asked if I can create vector based assets directly from the client.
After investigating HTML5 SVG, it seems like I might be able to do this, and after playing around with raphael.js and svg.js, I decided I like svg.js better. Much more lightweight, and < IE9 support is not an issue for me.
My main concern with using SVG is that I have to render text to my svg's, and have the text vector based and available in the final asset. Not only this, I need to be able to perfectly measure the text's bounding area (to the nearest pixel). In this I see 2 problems:
Just using a standard
draw.text()
call to create a<text>
element is no good as it means I must bundle the font with the asset, which is not possible due to licensing reasons.Calling
text.bbox()
to get the bounding area of a text element is inaccurate when it comes to height. I believe it simply returns the height for the tallest/lowest characters in the font rather than the characters actually being used, much likegetClientRect()
does with standard html elements.
I see a solution which could solve these problems, and this is where I am looking for advice:
I believe I could manually render glyphs using draw.path()
with path information pulled from an SVG font. This way my final asset will have vector based glyphs in it which are not tied to any font, and hopefully a call to bbox()
after drawing a path is more accurate than with text.
Does anyone know if is it possible to pull any of this glyph information using svg.js? Or must I do that manually by parsing the xml and pulling the path information out of there? Also does anyone have any idea on how kerning could potentially work in this situation? Or even better do you know any other way of solving problems 1 & 2 above without doing this manual and complicated method?