javascript - Manual SVG text rendering using svg.js + glyphs pulled from SVG font -


i working on project have create design assets dynamically in browser. past few months have been using canvas this, being asked if can create vector based assets directly client.

after investigating html5 svg, seems might able this, , after playing around raphael.js , svg.js, decided svg.js better. more lightweight, , < ie9 support not issue me.

my main concern using svg have render text svg's, , have text vector based , available in final asset. not this, need able measure text's bounding area (to nearest pixel). in see 2 problems:

  • just using standard draw.text() call create <text> element no means must bundle font asset, not possible due licensing reasons.

  • calling text.bbox() bounding area of text element inaccurate when comes height. believe returns height tallest/lowest characters in font rather characters being used, getclientrect() standard html elements.

i see solution solve these problems, , looking advice:

i believe manually render glyphs using draw.path() path information pulled svg font. way final asset have vector based glyphs in not tied font, , call bbox() after drawing path more accurate text.

does know if possible pull of glyph information using svg.js? or must manually parsing xml , pulling path information out of there? have idea on how kerning potentially work in situation? or better know other way of solving problems 1 & 2 above without doing manual , complicated method?

i've taken half hearted attempt @ before, indeed possible not straightforward. extract information font svg font xml need to...

document.queryselector("glyph[unicode=a]").getattribute("d"); 

this path of letter a. however, path in different coordinate system, w3c:

unlike standard graphics in svg, initial coordinate system has y-axis pointing downward (see initial coordinate system), design grid svg fonts, along initial coordinate system glyphs, has y-axis pointing upward consistency accepted industry practice many popular font formats.

this means have transform coordinates 1 system other (this in shouldn't hard) , scale desired size

then must paint next letter after first one, there's horiz-adv-x attribute of glyphs must scale according text size. though svg fonts account kerning, in practice none of browser engines support svg text implement it, spacing doesn't vary according glyph pairs.

depending on specific requirements process can go easy nightmarish. can end implementing mini-text rendering engine if scope creeps up.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -