【发布时间】:2019-10-01 21:23:45
【问题描述】:
我在 paper.js 中有一条路径,女巫描述了一种字母形式。我通过使用 opentype.js 的函数 path.toSVG() 加载字体来获得路径;比我通过 .importSVG() 将它加载到 paper.js;
现在我对 paper.js 进行了一些操作,然后希望将其作为可下载的字体文件返回。所以我的问题是,有没有一种简单的方法可以将它从 paper.js 恢复到 opentype.js?
编辑: 好吧,我假设没有简单的方法。所以我尝试用js手动转换路径:
for(var i=0; i<fragments.children.length; i++) {
var glyphName = fragments.children[i].name;
if (glyphName.indexOf('0x') > -1) {
var unicode = String.fromCharCode(parseInt(glyphName, 16));
glyphName = 'uni' + glyphName.charCodeAt(0);
} else {
var unicode = glyphName.charCodeAt(0);
}
var w = 0;
var glyphPath = new opentype.Path();
//Going through the array with the segments of the paper.js Path
for(var i2 = 0; i2 < fragments.children[i].segments.length; i2++) {
// handle In
var x1 = fragments.children[i].segments[i2].handleIn.x;
var y1 = fragments.children[i].segments[i2].handleIn.y;
// handle Out
var x2 = fragments.children[i].segments[i2].handleOut.x;
var y2 = fragments.children[i].segments[i2].handleOut.y;
// Point
var x = fragments.children[i].segments[i2].point.x;
var y = fragments.children[i].segments[i2].point.y;
if (i2 === 0) {
// if its the first segment use move to
glyphPath.moveTo(x, y);
} else if(x1==0 && y1==0 && x2 == 0 && y2 == 0) {
// if there is no curve use line to
glyphPath.lineTo(x, y);
} else {
// use curve if its a curve
glyphPath.curveTo(x1+x,y1+y, x2+x,y2+y, x,y,);
}
if(i2+1 == fragments.children[i].segments.length) {
glyphPath.close();
}
w = Math.max(w, x);
}
var glyph = new opentype.Glyph({
name: fragments.children[i].name,
unicode: unicode,
advanceWidth: (w + 1),
path: glyphPath
});
}
这让我可以下载一个 opentype 字体文件。但是,如果我在字体查看器中打开字体,则整个表单是颠倒的,并且句柄是错误的。它们的位置似乎是正确的,但不知何故,错误的手柄位于另一个应该在的位置……它们似乎被调换了。我无法弄清楚我错过了什么..
【问题讨论】:
-
在我看来,您的 X/Y 轴倒置(上/下和或左/右)。您可以链接前后字母的参考图片吗?
-
@RogerWillcocks 感谢您对此进行调查。我添加了两个图片链接。第一个显示了它的外观,第二个显示了它现在的外观..
标签: javascript paperjs opentype