From 2b227c06b1e1ac324bf8c9db8c15f66e2dfbdcc4 Mon Sep 17 00:00:00 2001 From: mkwiser Date: Tue, 14 Oct 2014 22:55:09 +0800 Subject: [PATCH] add ttf transform --- src/fonteditor/widget/glyfviewer.js | 21 ++++++-- src/ttf/svg2ttfobject.js | 44 ++++++++++++----- src/ttf/ttf.js | 74 +++++++++++++++-------------- 3 files changed, 88 insertions(+), 51 deletions(-) diff --git a/src/fonteditor/widget/glyfviewer.js b/src/fonteditor/widget/glyfviewer.js index 29f14fa..7830a67 100644 --- a/src/fonteditor/widget/glyfviewer.js +++ b/src/fonteditor/widget/glyfviewer.js @@ -120,6 +120,13 @@ define( function downlistener(e) { var me = this; + if (e.keyCode >= 112 && e.keyCode <= 119 && e.keyCode !== 116) { + e.preventDefault(); + e.stopPropagation(); + me.fire('function', { + keyCode: e.keyCode + }); + } // 保存 if (83 === e.keyCode && e.ctrlKey) { e.preventDefault(); @@ -189,11 +196,9 @@ define( var me = this; me.downlistener = lang.bind(downlistener, this); - document.body.addEventListener('keydown', this.downlistener); - $(document.body).on('click', function(e) { var focused = me.main.get(0) === e.target || me.main.get(0).contains(e.target); - me.listening = focused; + focused ? me.focus() : me.blur(); }); me.capture = new MouseCapture(me.main.get(0), { @@ -245,14 +250,20 @@ define( * 获取焦点 */ GlyfViewer.prototype.focus = function() { - this.listening = true; + if (!this.listening) { + this.listening = true; + document.body.addEventListener('keydown', this.downlistener); + } }; /** * 失去焦点 */ GlyfViewer.prototype.blur = function() { - this.listening = false; + if (this.listening) { + this.listening = false; + document.body.addEventListener('keydown', this.downlistener); + } }; /** diff --git a/src/ttf/svg2ttfobject.js b/src/ttf/svg2ttfobject.js index bd1327d..97a985d 100644 --- a/src/ttf/svg2ttfobject.js +++ b/src/ttf/svg2ttfobject.js @@ -74,15 +74,31 @@ define( glyf.xMax = bound.x + bound.width; glyf.yMin = bound.y; glyf.yMax = bound.y + bound.height; - glyf.advanceWidth = Math.round(glyf.advanceWidth); + + if (glyf.leftSideBearing) { + glyf.leftSideBearing = Math.round(glyf.leftSideBearing); + } + else { + glyf.leftSideBearing = glyf.xMin; + } + + if (glyf.advanceWidth) { + glyf.advanceWidth = Math.round(glyf.advanceWidth); + } + else { + glyf.advanceWidth = glyf.xMax + 10; + } } }); + + if (undefined !== ttf.head.xMin && undefined !== ttf.head.yMax) { + ttf.head.xMin = Math.round(ttf.head.xMin); + ttf.head.xMax = Math.round(ttf.head.xMax); + ttf.head.yMin = Math.round(ttf.head.yMin); + ttf.head.yMax = Math.round(ttf.head.yMax); + } - ttf.head.xMin = Math.round(ttf.head.xMin); - ttf.head.xMax = Math.round(ttf.head.xMax); - ttf.head.yMin = Math.round(ttf.head.yMin); - ttf.head.yMax = Math.round(ttf.head.yMax); - ttf.head.unitsPerEm = ttf.head.unitsPerEm ? Math.round(ttf.head.unitsPerEm) : 0; + ttf.head.unitsPerEm = ttf.head.unitsPerEm ? Math.round(ttf.head.unitsPerEm) : 512; return ttf; } @@ -165,10 +181,13 @@ define( // 解析glyf var d, unicode; if (missingNode) { - var missing = { - advanceWidth: +(missingNode.getAttribute('horiz-adv-x') || 0) - }; - + + var missing = {}; + + if (missingNode.getAttribute('horiz-adv-x')) { + missing.advanceWidth = +missingNode.getAttribute('horiz-adv-x'); + } + if ((d = missingNode.getAttribute('d'))) { missing.contours = svg2contours(d); } @@ -192,9 +211,12 @@ define( var node = glyfNodes[i]; var glyf = { - advanceWidth: +(node.getAttribute('horiz-adv-x') || 0), name: node.getAttribute('glyph-name') || node.getAttribute('name') || '' }; + + if (node.getAttribute('horiz-adv-x')) { + glyf.advanceWidth = +node.getAttribute('horiz-adv-x'); + } if ((unicode = node.getAttribute('unicode'))) { glyf.unicode = unicode.split('').map(unicodeMap); diff --git a/src/ttf/ttf.js b/src/ttf/ttf.js index 8e40a3d..a9bd787 100644 --- a/src/ttf/ttf.js +++ b/src/ttf/ttf.js @@ -46,46 +46,42 @@ define( list.forEach(function(g) { if (scale !== 1) { - g.contours.forEach(function(contour) { pathAdjust(contour, scale, scale); pathCeil(contour); }); - - // 重新计算xmin,xmax,ymin,ymax - if ( - undefined === g.xMin - || undefined === g.yMax - || undefined === g.leftSideBearing - || undefined === g.advanceWidth - ) { - var bound = computeBoundingBox.computePathBox.apply(this, g.contours); - - g.xMin = bound.x; - g.xMax = bound.x + bound.width; - g.yMin = bound.y; - g.yMax = bound.y + bound.height; - - g.leftSideBearing = g.xMin; - - // 如果设置了advanceWidth就是用默认的,否则为10 - if (undefined !== g.advanceWidth) { - g.advanceWidth = Math.round(g.advanceWidth * scale); - } - else { - g.advanceWidth = g.xMax + 10; - } - - } - else { - g.xMin = Math.round(g.xMin * scale); - g.xMax = Math.round(g.xMax * scale); - g.yMin = Math.round(g.yMin * scale); - g.yMax = Math.round(g.yMax * scale); - g.leftSideBearing = Math.round(g.leftSideBearing * scale); - g.advanceWidth = Math.round(g.advanceWidth * scale); - } } + + // 重新计算xmin,xmax,ymin,ymax + if ( + undefined === g.xMin + || undefined === g.yMax + || undefined === g.leftSideBearing + || undefined === g.advanceWidth + ) { + var bound = computeBoundingBox.computePathBox.apply(this, g.contours); + + g.xMin = bound.x; + g.xMax = bound.x + bound.width; + g.yMin = bound.y; + g.yMax = bound.y + bound.height; + + g.leftSideBearing = g.xMin; + + // 如果设置了advanceWidth就是用默认的,否则为10 + g.advanceWidth = g.xMax + 10; + } + else { + g.xMin = Math.round(g.xMin * scale); + g.xMax = Math.round(g.xMax * scale); + g.yMin = Math.round(g.yMin * scale); + g.yMax = Math.round(g.yMax * scale); + g.leftSideBearing = Math.round(g.leftSideBearing * scale); + g.advanceWidth = Math.round(g.advanceWidth * scale); + } + + //console.log(g.xMin, g.xMax, g.yMin, g.yMax, g.leftSideBearing, g.advanceWidth); + ttf.glyf.push(g); }); @@ -303,6 +299,7 @@ define( else { g.advanceWidth += offset; } + if (g.contours && g.contours.length) { g.contours.forEach(function(contour) { pathAdjust(contour, 1, 1, offset); @@ -312,6 +309,8 @@ define( else if (undefined !== setting.rightSideBearing) { g.advanceWidth = g.xMax + setting.rightSideBearing; } + + //console.log(g.xMin, g.xMax, g.yMin, g.yMax, g.leftSideBearing, g.advanceWidth); }); } @@ -395,6 +394,7 @@ define( g.leftSideBearing = g.xMin; g.advanceWidth = g.xMax + rightSideBearing; } + //console.log(g.xMin, g.xMax, g.yMin, g.yMax, g.leftSideBearing, g.advanceWidth); }); } // 缩放到embox @@ -420,7 +420,9 @@ define( pathAdjust(contour, 1, 1, 0, yOffset); pathCeil(contour); }); + var box = computeBoundingBox.computePathBox.apply(null, g.contours); + g.xMin = box.x; g.xMax = box.x + box.width; g.yMin = box.y; @@ -430,6 +432,8 @@ define( g.advanceWidth = g.xMax + rightSideBearing; } } + + //console.log(g.xMin, g.xMax, g.yMin, g.yMax, g.leftSideBearing, g.advanceWidth); }); }