add ttf transform

This commit is contained in:
mkwiser 2014-10-14 22:55:09 +08:00
parent d69fa7735f
commit 2b227c06b1
3 changed files with 88 additions and 51 deletions

View File

@ -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);
}
};
/**

View File

@ -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;
}
}
});
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;
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.unitsPerEm = ttf.head.unitsPerEm ? Math.round(ttf.head.unitsPerEm) : 512;
return ttf;
}
@ -165,9 +181,12 @@ 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,10 +211,13 @@ 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);
}

View File

@ -46,46 +46,42 @@ define(
list.forEach(function(g) {
if (scale !== 1) {
g.contours.forEach(function(contour) {
pathAdjust(contour, scale, scale);
pathCeil(contour);
});
// 重新计算xminxmaxyminymax
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);
}
}
// 重新计算xminxmaxyminymax
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);
});
}