手动渲染SVG字形

时间:2013-09-05 19:45:53

标签: javascript html5 svg fonts svg.js

我目前正在实现一个简单的SVG文本渲染器。我正在从SVG文件中提取我需要绘制的字符的路径信息。

这是存储在JSON对象中的字体/字符的所有适当信息:

     window.font = {
        horizAdvX: 487,
        unitsPerEm: 2048,
        ascent: 1638,
        descent: -410,
        glyphs: {
            H: {
                horizAdvX: 1382,
                path: 'M147 14q64 133 244 604q-68 29 -90 80q19 14 135 37l27 66q85 217 115 327.5t81 391.5q81 -14 153 -73t126 -147q-8 -11 -17.5 -26t-22 -38.5t-23 -43.5t-27 -55.5t-27 -58.5l-31.5 -69t-32 -71t-35.5 -80.5t-36.5 -81.5q282 39 488 51q77 223 157.5 399.5t136.5 235.5 q42 -3 124.5 -47.5t101.5 -79.5q-69 -65 -143 -210.5t-140 -336.5q41 -38 41 -92q0 -39 -16 -71q-28 4 -76 8q-69 -219 -111.5 -425t-42.5 -319q0 -33 4 -53q-41 2 -65 15t-31 28.5t-18 32t-27 22.5q-26 9 -44.5 74.5t-18.5 110.5q0 160 104 510q-75 -5 -255 -24.5 t-253 -20.5q-166 -392 -231 -750q-73 18 -126 62.5t-98 117.5z'
            },
            u: {
                horizAdvX: 1011,
                path: 'M174 119q0 98 16 174q43 181 146 386.5t221 291.5q44 0 99 -16t83 -48q-18 -19 -51 -68t-93 -161t-116 -244q-24 -55 -55 -162.5t-31 -156.5q0 -22 15 -37q29 11 73.5 62.5t134.5 174.5q6 9 36 49t47 64t42.5 63t44 75.5t31.5 70.5q19 51 33 84.5t49.5 91.5t77.5 98 q53 0 114 -20.5t80 -44.5q-28 -68 -104.5 -220.5t-115 -259.5t-38.5 -204q0 -51 11.5 -92t22.5 -64.5t11 -32.5q0 -3 -2 -5t-4 -2l-2 -1q-28 0 -88 24t-106 56q-35 29 -50.5 76t-15.5 90q0 44 10 74q-10 1 -52.5 -51t-95.5 -123t-67 -88q-57 -61 -88 -64q-70 3 -138.5 47.5 t-84.5 112.5z'
            },
            // rest of chars ...

使用优秀的svg.js库,我已经成功地在渲染过程中实现了这一点:

  1. 使用路径信息绘制角色& SVG.path方法
  2. 对角色应用固定高度
  3. 获取字符路径的边界框
  4. 变换坐标系,使y指向下(SVG字体中的坐标系是'倒置')
  5. 使用前一个字符的宽度作为偏移量
  6. 绘制下一个字符
  7. 重复
  8. 这确实是绘制文字。你可以看到这个工作在这个jsfiddle:

    http://jsfiddle.net/dormisher/KVs7E/2/

    事情是一切都是相同的高度,并且完全相同的距离。我需要知道如何使用SVG字体文件中的horiz-adv-xunites-per-em等属性来获得正确的水平和垂直定位。

    起初看起来很简单,horiz-adv-x只是字体大小上使用的缩放值,所以在水平偏移上使用它,嘿presto!但不,这样做最终会看起来像这样:

    http://jsfiddle.net/dormisher/KVs7E/3/

    我需要弄清楚的是horizAdvX和其余的simillar值的组合是如何用来计算水平推进和放大的。垂直定位。我已经阅读了W3C规范,但就这些内容而言,这对我来说没什么意义。

    如果有人能帮助我,那就太好了!

1 个答案:

答案 0 :(得分:3)

诀窍是,相应地缩放字形,然后将它们移动到正确的位置。我刚刚为svg.js发布了text-morphing extension,它也从svg-font中提取了它的字形。

我走了这条路:

var glyphs = 'My Text'
  , uPerEm = faceElement.getAttribute('units-per-em')
  , h = fontElement.getAttribute('horiz-adv-x')
  , x = 0
  , scale = fontsize / uPerEm


for(var i = 0, len = glyphs.length; i < len; ++i){

  // check if there is kerning for this and the letter before
  // hkern specifies if the 2 letter move together because of to much space(example: Ya)
  if(glyphs[i-1]){
    hkern = this.source.querySelector('hkern[u1="'+glyphs[i-1]+'"][u2="'+glyphs[i]+'"]')
    if(hkern){
      // substract the value from our current x position
      x -= parseFloat(hkern.getAttribute('k')) * scale
    }
  }

  // create a new path array with the d-attribute
  // in cache, all my loaded glyphs are saved
  var p = new SVG.PathArray(cache[glyphs[i]].d)
    , box = p.bbox()

  // scale AND mirror the glyph (note the minus)
  if(box.height && box.width)
    p.size(box.width * scale, -box.height * scale)

  // move the glyph to its position
  p.move(x,(uPerEm - box.y - box.height)*scale)

  // draw the path
  yourSvgRoot.path(p)

  // add up the horiz-adv-x from the glyph or the horiz-adv-x from the font-node if no horiz-adv-x is present
  x += parseFloat(cache[glyphs[i]]['horiz-adv-x'] || h) * scale;

}

大部分代码。我试图添加评论以帮助理解。 基本上我循环我的字形和大小,并根据scalefactor定位它们。我现在不知道flip()会对路径做什么,但我很确定它有一些问题。最好自动计算size()为你做的新路径点。

相关问题