add compound to simple glyf

This commit is contained in:
kekee000 2015-04-23 00:32:59 +08:00
parent 5dced5d6df
commit 1ed877185c
9 changed files with 189 additions and 55 deletions

View File

@ -46,6 +46,21 @@
<button data-action="save" type="button" class="btn btn-flat btn-sm"><i class="ico ico-left i-save"></i>保存项目</button>
<div class="btn-group">
<button type="button" class="btn btn-flat btn-sm dropdown-toggle" data-toggle="dropdown">
工具
<span class="drop ico i-down"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a data-action="setting-glyf-name">生成字形名称</a></li>
<li><a data-action="setting-glyf-clearname">清除字形名称</a></li>
<li><a data-action="setting-optimize">优化字体</a></li>
<li><a data-action="setting-compound2simple">复合字形转简单字形</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-flat btn-sm dropdown-toggle" data-toggle="dropdown">
@ -56,9 +71,6 @@
<ul class="dropdown-menu" role="menu">
<li><a data-action="setting-name">字体信息</a></li>
<li><a data-action="setting-metrics">字体度量</a></li>
<li><a data-action="setting-glyf-name">生成字形名称</a></li>
<li><a data-action="setting-glyf-clearname">清除字形名称</a></li>
<li><a data-action="setting-optimize">优化字体</a></li>
<li><a data-action="setting-editor">编辑器</a></li>
<li><a data-action="setting-import-and-export">导入和导出</a></li>
</ul>

View File

@ -251,8 +251,7 @@ define(
},
'setting-glyf-name': function () {
var ttf = program.ttfManager.get();
if (ttf) {
if (program.ttfManager.get()) {
if (window.confirm('生成的字形名称会覆盖原来的名称,确定生成?')) {
program.ttfManager.genGlyfName(program.viewer.getSelected());
}
@ -260,8 +259,7 @@ define(
},
'setting-glyf-clearname': function () {
var ttf = program.ttfManager.get();
if (ttf) {
if (program.ttfManager.get()) {
program.ttfManager.clearGlyfName(program.viewer.getSelected());
}
},
@ -275,6 +273,12 @@ define(
}
},
'setting-compound2simple': function () {
if (program.ttfManager.get()) {
program.ttfManager.compound2simple(program.viewer.getSelected());
}
},
'setting-editor': function () {
var SettingEditor = settingSupport.editor;
!new SettingEditor({

View File

@ -37,12 +37,6 @@ define(
},
validate: function () {
var unicode = $('#setting-glyf-unicode').val();
if (unicode && !unicode.match(unicodeREG)) {
alert('unicode设置不正确!');
return false;
}
var setting = this.getFields();
if (setting.leftSideBearing === undefined

View File

@ -200,40 +200,51 @@ define(
*/
Setting.prototype.getFields = function (setting) {
setting = setting || {};
var unicodeREG = /^(?:\$[A-F0-9]+)(?:\,\$[A-F0-9]+)*$/gi;
this.getDialog().find('[data-field]').each(function (i, item) {
var field = item.getAttribute('data-field');
var type = item.getAttribute('data-type') || item.type;
var originValue = item.value.trim();
var val;
var fieldValue;
if (type === 'checkbox') {
val = !!item.checked;
}
else if (type === 'unicode') {
val = item.value.trim() === '' ? [] : item.value.split(',').map(function (u) {
return Number('0x' + u.slice(1));
});
if (!originValue) {
val = [];
}
else if (unicodeREG.test(originValue)) {
val = originValue.split(',').map(function (u) {
return Number('0x' + u.slice(1));
});
}
else {
val = originValue.split('').map(function (u) {
return u.charCodeAt(0);
});
}
}
else if (type === 'datetime-local') {
if (item.value) {
val = Date.parse(item.value.replace('T', ' '));
if (originValue) {
val = Date.parse(originValue.replace('T', ' '));
}
}
else if (type === 'number') {
if (item.value) {
fieldValue = +item.value;
if (originValue) {
originValue = +originValue;
if (item.getAttribute('data-ceil')) {
fieldValue = Math.floor(fieldValue);
originValue = Math.floor(originValue);
}
val = fieldValue;
val = originValue;
}
}
else {
item = $(item);
fieldValue = item.val().trim();
if (fieldValue) {
val = fieldValue;
if (originValue) {
val = originValue;
}
}

View File

@ -182,7 +182,7 @@ define(
/**
* 删除指定字形
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {this}
*/
Manager.prototype.removeGlyf = function (indexList) {
@ -200,7 +200,7 @@ define(
* 设置unicode代码
*
* @param {string} unicode unicode代码
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {this}
*/
Manager.prototype.setUnicode = function (unicode, indexList) {
@ -219,7 +219,7 @@ define(
/**
* 生成字形名称
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {this}
*/
Manager.prototype.genGlyfName = function (indexList) {
@ -238,7 +238,7 @@ define(
/**
* 清除字形名称
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {this}
*/
Manager.prototype.clearGlyfName = function (indexList) {
@ -258,7 +258,7 @@ define(
* 添加并体替换指定的glyf
*
* @param {Array} glyfList 添加的列表
* @param {Array} indexList 需要替换的索引列表
* @param {Array=} indexList 需要替换的索引列表
* @return {this}
*/
Manager.prototype.appendGlyf = function (glyfList, indexList) {
@ -293,7 +293,7 @@ define(
/**
* 调整glyf位置
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @param {Object} setting 选项
* @return {boolean}
*/
@ -314,7 +314,7 @@ define(
/**
* 调整glyf
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @param {Object} setting 选项
* @return {boolean}
*/
@ -379,7 +379,7 @@ define(
/**
* 获取glyfList
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {Array} glyflist
*/
Manager.prototype.getGlyf = function (indexList) {
@ -458,6 +458,22 @@ define(
};
};
/**
* 复合字形转简单字形
* @param {Array=} indexList 选中的字形索引
* @return {this}
*/
Manager.prototype.compound2simple = function (indexList) {
var result = this.ttf.compound2simple(indexList);
if (result.length) {
result.forEach(function (g) {
g.modify = 'edit';
});
this.fireChange(true);
}
};
/**
* 撤销
* @return {this}
@ -532,8 +548,6 @@ define(
}
};
/**
* 注销
*/

View File

@ -16,6 +16,7 @@ define(
var pathAdjust = require('graphics/pathAdjust');
var pathCeil = require('graphics/pathCeil');
var computeBoundingBox = require('graphics/computeBoundingBox');
var compound2simpleglyf = require('./util/compound2simpleglyf');
var glyfAdjust = require('./util/glyfAdjust');
var optimizettf = require('./util/optimizettf');
var config = require('./data/default');
@ -316,7 +317,7 @@ define(
* 设置unicode代码
*
* @param {string} unicode unicode代码 $E021, $22
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {Array} 改变的glyf
*/
TTF.prototype.setUnicode = function (unicode, indexList) {
@ -366,7 +367,7 @@ define(
/**
* 生成字形名称
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {Array} 改变的glyf
*/
TTF.prototype.genGlyfName = function (indexList) {
@ -405,7 +406,7 @@ define(
/**
* 清除字形名称
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {Array} 改变的glyf
*/
TTF.prototype.clearGlyfName = function (indexList) {
@ -434,7 +435,7 @@ define(
* 添加并体替换指定的glyf
*
* @param {Array} glyfList 添加的列表
* @param {Array} indexList 需要替换的索引列表
* @param {Array=} indexList 需要替换的索引列表
* @return {Array} 改变的glyf
*/
TTF.prototype.appendGlyf = function (glyfList, indexList) {
@ -460,7 +461,7 @@ define(
/**
* 调整glyf位置
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @param {Object} setting 选项
* @return {Array} 改变的glyf
*/
@ -480,7 +481,7 @@ define(
/**
* 调整glyf
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @param {Object} setting 选项
* @return {boolean}
*/
@ -534,7 +535,7 @@ define(
/**
* 获取glyf列表
*
* @param {Array} indexList 索引列表
* @param {Array=} indexList 索引列表
* @return {Array} glyflist
*/
TTF.prototype.getGlyf = function (indexList) {
@ -793,14 +794,34 @@ define(
/**
* 优化ttf字形信息
*
* @param {Object} glyf glyfobject
* @param {string} index 需要替换的索引列表
* @return {Array} 改变的glyf
*/
TTF.prototype.optimize = function () {
return optimizettf(this.ttf);
};
/**
* 复合字形转简单字形
* @param {Array=} indexList 索引列表
* @return {Array} 改变的glyf
*/
TTF.prototype.compound2simple = function (indexList) {
var ttf = this.ttf;
if (!ttf.maxp.maxComponentElements) {
return [];
}
var list = this.getGlyf(indexList).filter(function (glyf) {
if (glyf.compound) {
compound2simpleglyf(glyf, ttf);
return true;
}
});
return list;
};
return TTF;
}

View File

@ -18,7 +18,7 @@ define(
var Reader = require('./reader');
var postName = require('./enum/postName');
var error = require('./error');
var compound2simpleglyf = require('./util/compound2simpleglyf');
/**
@ -62,6 +62,8 @@ define(
error.raise(10204);
}
ttf.readOptions = this.options;
// 读取支持的表数据
Object.keys(supportTables).forEach(function (tableName) {
if (ttf.tables[tableName]) {
@ -123,6 +125,7 @@ define(
* @param {Object} ttf ttf对象
*/
function cleanTables(ttf) {
delete ttf.readOptions;
delete ttf.tables;
delete ttf.hmtx;
delete ttf.loca;
@ -137,18 +140,31 @@ define(
delete glyf.instructions;
});
}
if (this.options.compound2simple && ttf.maxp.maxComponentElements) {
ttf.glyf.forEach(function (glyf) {
if (glyf.compound) {
compound2simpleglyf(glyf, ttf);
}
});
ttf.maxp.maxComponentElements = 0;
ttf.maxp.maxComponentDepth = 0;
}
}
/**
* TTF的构造函数
* @param {Object} options 写入参数
* @param {boolean} hinting 保留hinting信息
* @param {boolean} options.hinting 保留hinting信息
* @param {boolean} options.compound2simple 复合字形转简单字形
* @constructor
*/
function TTFReader(options) {
this.options = options || {
hinting: false // 不保留hints信息
options = options || {};
this.options = {
hinting: options.hinting || false, // 不保留hints信息
compound2simple: options.compound2simple || false // 复合字形转简单字形
};
}

View File

@ -196,14 +196,13 @@ define(
/**
* TTFWriter的构造函数
* @param {Object} options 写入参数
* @param {boolean} hinting 保留hinting信息
* @param {boolean} removeCompositeGlyf 是否移除复合图元
* @param {boolean} options.hinting 保留hinting信息
* @constructor
*/
function TTFWriter(options) {
this.options = options || {
hinting: false, // 不保留hints信息
removeCompositeGlyf: false // 是否移除复合图元
options = options || {};
this.options = {
hinting: options.hinting || false // 不保留hints信息
};
}

View File

@ -0,0 +1,63 @@
/**
* @file ttf复合字形转简单字形
* @author mengke01(kekee000@gmail.com)
*/
define(
function (require) {
var pathCeil = require('graphics/pathCeil');
var pathTransform = require('graphics/pathTransform');
var lang = require('common/lang');
/**
* ttf复合字形转简单字形
*
* @param {Object} glyf glyf对象
* @param {Object} ttf ttfObject对象
* @return {Object} 转换后的对象
*/
function compound2simpleglyf(glyf, ttf) {
if (!glyf.compound || !glyf.glyfs) {
return glyf;
}
var glyfs = glyf.glyfs;
var compoundContours = [];
glyfs.forEach(function (g) {
var glyph = ttf.glyf[g.glyphIndex];
if (!glyph) {
return;
}
var contours = lang.clone(glyph.contours); // 这里需要进行matrix变换需要复制一份
var transform = g.transform;
for (var i = 0, l = contours.length; i < l; i++) {
pathTransform(
contours[i],
transform.a,
transform.b,
transform.c,
transform.d,
transform.e,
transform.f
);
compoundContours.push(pathCeil(contours[i]));
}
});
glyf.contours = compoundContours;
delete glyf.compound;
delete glyf.glyfs;
// 这里hinting信息会失效删除hinting信息
delete glyf.instructions;
return glyf;
}
return compound2simpleglyf;
}
);