diff --git a/index.html b/index.html index c8d94f4..5c1469b 100644 --- a/index.html +++ b/index.html @@ -41,8 +41,9 @@ diff --git a/src/editor/Editor.js b/src/editor/Editor.js index 15702c4..31ab417 100644 --- a/src/editor/Editor.js +++ b/src/editor/Editor.js @@ -13,6 +13,7 @@ define( var modeSupport = require('./mode/support'); var ContextMenu = require('./menu/ContextMenu'); var commandSupport = require('./command/support'); + var clipboard = require('./util/clipboard'); var History = require('./util/History'); // editor 的控制器 @@ -149,8 +150,7 @@ define( * @return {this} */ Editor.prototype.setClipBoard = function(shapes) { - Editor.ClipBoardData = null; - Editor.ClipBoardData = shapes; + clipboard.set(shapes, 'editor-shape'); return this; }; @@ -170,7 +170,7 @@ define( * @return {this} */ Editor.prototype.getClipBoard = function() { - return Editor.ClipBoardData ? lang.clone(Editor.ClipBoardData) : null; + return clipboard.get('editor-shape'); }; /** diff --git a/src/editor/controller/initFont.js b/src/editor/controller/initFont.js index 2d89a73..93f48d3 100644 --- a/src/editor/controller/initFont.js +++ b/src/editor/controller/initFont.js @@ -29,7 +29,10 @@ define( // 不需要在此保存contours delete font.contours; - font.rightSideBearing = font.advanceWidth - font.xMax; + + var advanceWidth = font.advanceWidth || this.options.unitsPerEm; + + font.rightSideBearing = advanceWidth - (font.xMax || advanceWidth); this.font = font; // 设置字形 @@ -61,7 +64,7 @@ define( fontLayer.refresh(); // 设置参考线 - this.rightSideBearing.p0.x = originX + font.advanceWidth * scale; + this.rightSideBearing.p0.x = originX + advanceWidth * scale; this.axisLayer.refresh(); this.setMode(); @@ -230,6 +233,7 @@ define( this.font.name = options.name; } + this.fire('change'); return this; } diff --git a/src/editor/mode/shapes.js b/src/editor/mode/shapes.js index 766d2ef..3f0669f 100644 --- a/src/editor/mode/shapes.js +++ b/src/editor/mode/shapes.js @@ -199,14 +199,15 @@ define( if (this.currentPoint) { this.currentGroup.finishTransform(this.currentPoint, this.render.camera); this.currentPoint = null; + this.fire('change'); } else if (this.currentGroup.mode == 'move') { this.currentGroup.finishTransform(this.currentPoint, this.render.camera); this.currentGroup.setMode('scale'); + this.fire('change'); } this.render.setCursor('default'); - this.fire('change'); }, /** diff --git a/src/fonteditor/widget/clipboard.js b/src/editor/util/clipboard.js similarity index 100% rename from src/fonteditor/widget/clipboard.js rename to src/editor/util/clipboard.js diff --git a/src/fonteditor/controller/default.js b/src/fonteditor/controller/default.js index 937b9f3..055bfd2 100644 --- a/src/fonteditor/controller/default.js +++ b/src/fonteditor/controller/default.js @@ -10,7 +10,7 @@ define( function(require) { var lang = require('common/lang'); - var clipboard = require('../widget/clipboard'); + var clipboard = require('editor/util/clipboard'); var string = require('common/string'); // 获取ttf的编辑选项 @@ -139,7 +139,7 @@ define( if (program.editor.isEditing()) { // 如果是正在编辑的 if (program.data.editingIndex !== -1) { - program.ttfManager.updateGlyf(program.editor.getFont(), program.data.editingIndex); + program.ttfManager.replaceGlyf(program.editor.getFont(), program.data.editingIndex); } // 否则新建font else { diff --git a/src/fonteditor/controller/ttf.js b/src/fonteditor/controller/ttf.js index 91eefb1..fe75374 100644 --- a/src/fonteditor/controller/ttf.js +++ b/src/fonteditor/controller/ttf.js @@ -10,7 +10,7 @@ define( function(require) { var lang = require('common/lang'); - var clipboard = require('../widget/clipboard'); + var clipboard = require('editor/util/clipboard'); var string = require('common/string'); return { diff --git a/src/fonteditor/dialog/setting-adjust-pos.js b/src/fonteditor/dialog/setting-adjust-pos.js index 13216e9..b6032d9 100644 --- a/src/fonteditor/dialog/setting-adjust-pos.js +++ b/src/fonteditor/dialog/setting-adjust-pos.js @@ -35,7 +35,7 @@ define( return require('./setting').derive({ - title: '调整字形', + title: '调整字形位置', getTpl: function() { return tpl; diff --git a/src/fonteditor/dialog/setting-glyf.js b/src/fonteditor/dialog/setting-glyf.js new file mode 100644 index 0000000..c3314ef --- /dev/null +++ b/src/fonteditor/dialog/setting-glyf.js @@ -0,0 +1,72 @@ +/** + * @file setting-adjust-pos.js + * @author mengke01 + * @date + * @description + * 设置自动调整字形位置 + */ + +define( + function(require) { + + var tpl = '' + + '
' + + '
' + + 'unicode' + + '' + + '
' + + '  可以设置多个unicode,例如:"$21,$22,$23"' + + '
' + + '
' + + '
' + + '左边轴' + + '' + + '
' + + '
' + + '
' + + '
' + + '右边轴' + + '' + + '
' + + '
'; + + + return require('./setting').derive({ + + title: '调整字形', + + getTpl: function() { + return tpl; + }, + + set: function(setting) { + this.setFields(setting || {}); + }, + + validate: function() { + + var unicode = $('#glyf-unicode').val(); + if (unicode && !unicode.match(/^(?:\$[A-F0-9]+)(?:\,\$[A-F0-9]+)*$/i)) { + alert('unicode设置不正确!'); + return false; + } + + var setting = this.getFields(); + + if(setting.leftSideBearing === undefined + && setting.rightSideBearing === undefined + && setting.unicode === undefined + ) { + alert('没有设置项目!'); + return false; + } + else { + return setting; + } + } + + }); + } +); diff --git a/src/fonteditor/dialog/setting-unicode.js b/src/fonteditor/dialog/setting-unicode.js index a24f125..6344988 100644 --- a/src/fonteditor/dialog/setting-unicode.js +++ b/src/fonteditor/dialog/setting-unicode.js @@ -15,7 +15,7 @@ define( + '' + '' diff --git a/src/fonteditor/dialog/setting.js b/src/fonteditor/dialog/setting.js index 2383f44..ad1e3c9 100644 --- a/src/fonteditor/dialog/setting.js +++ b/src/fonteditor/dialog/setting.js @@ -89,15 +89,20 @@ define( this.getDialog().find('[data-field]').each(function(i, item) { var field = item.getAttribute('data-field'); - + var type = item.getAttribute('data-type') || item.type; if (undefined === setting[field]) { return; } - if (item.type == 'checkbox') { + if (type == 'checkbox') { item.checked = setting[field] ? 'checked' : ''; } - else if (item.type == 'datetime-local') { + else if (type == 'unicode') { + item.value = (setting[field] || []).map(function(u) { + return '$' + u.toString(16); + }).join(','); + } + else if (type == 'datetime-local') { var val = setting[field]; var date; if (typeof(val) === 'string') { @@ -131,18 +136,24 @@ define( this.getDialog().find('[data-field]').each(function(i, item) { var field = item.getAttribute('data-field'); + var type = item.getAttribute('data-type') || item.type; - if (item.type == 'checkbox') { + if (type == 'checkbox') { if (item.checked) { setting[field] = true; } } - else if (item.type == 'datetime-local') { + else if (type == 'unicode') { + setting[field] = item.value.trim() == '' ? [] : item.value.split(',').map(function(u) { + return Number('0x' + u.slice(1)); + }); + } + else if (type == 'datetime-local') { if (item.value) { setting[field] = Date.parse(item.value.replace('T', ' ')); } } - else if (item.type == 'number') { + else if (type == 'number') { if (item.value) { var val = +item.value; if (item.getAttribute('data-ceil')) { diff --git a/src/fonteditor/widget/actions.js b/src/fonteditor/widget/actions.js index 4148ec2..e6591eb 100644 --- a/src/fonteditor/widget/actions.js +++ b/src/fonteditor/widget/actions.js @@ -137,30 +137,53 @@ define( dlg.show(); }, + // 调整字形 + 'setting-glyf': function() { + var ttf = program.ttfManager.get(); + if (ttf) { + + // 如果仅选择一个字形,则填充现有值 + var selected = program.viewer.getSelected(); + if (selected.length) { + var glyf = program.ttfManager.getGlyf(selected)[0]; + + !new setting['glyf']({ + onChange: function(setting) { + program.ttfManager.updateGlyf(setting, selected[0]); + } + }).show({ + unicode: glyf.unicode, + leftSideBearing: glyf.leftSideBearing, + rightSideBearing: glyf.advanceWidth - glyf.xMax + }); + } + } + }, + // 调整字形位置 'setting-adjust-pos': function() { var ttf = program.ttfManager.get(); if (ttf) { - var dlg = new setting['adjust-pos']({ + // 如果仅选择一个字形,则填充现有值 + var selected = program.viewer.getSelected(); + var opt = {}; + + if (selected.length === 1) { + var glyf = program.ttfManager.getGlyf(selected)[0]; + opt = { + unicode: glyf.unicode, + leftSideBearing: glyf.leftSideBearing, + rightSideBearing: glyf.advanceWidth - glyf.xMax + }; + } + + !new setting['adjust-pos']({ onChange: function(setting) { setTimeout(function() { program.ttfManager.adjustGlyfPos(setting, selected); }, 20); } - }); - - // 如果仅选择一个字形,则填充现有值 - var selected = program.viewer.getSelected(); - if (selected.length === 1) { - var glyf = program.ttfManager.getGlyf(selected)[0]; - dlg.show({ - leftSideBearing: glyf.leftSideBearing || '', - rightSideBearing: glyf.advanceWidth - glyf.xMax || '' - }); - } - else { - dlg.show(); - } + }).show(opt); } }, diff --git a/src/fonteditor/widget/setting.js b/src/fonteditor/widget/setting.js index fcf67d0..24bca61 100644 --- a/src/fonteditor/widget/setting.js +++ b/src/fonteditor/widget/setting.js @@ -14,7 +14,8 @@ define( 'adjust-pos': require('../dialog/setting-adjust-pos'), 'adjust-glyf': require('../dialog/setting-adjust-glyf'), 'metrics': require('../dialog/setting-metrics'), - 'online': require('../dialog/font-online') + 'online': require('../dialog/font-online'), + 'glyf': require('../dialog/setting-glyf') }; } ); diff --git a/src/fonteditor/widget/ttfmanager.js b/src/fonteditor/widget/ttfmanager.js index 8dddb0c..b86466e 100644 --- a/src/fonteditor/widget/ttfmanager.js +++ b/src/fonteditor/widget/ttfmanager.js @@ -183,14 +183,14 @@ define( }; /** - * 更新指定的glyf + * 替换指定的glyf * * @param {Object} glyf glyfobject * @param {string} index 需要替换的索引列表 * @return {this} */ - Manager.prototype.updateGlyf = function(glyf, index) { - var list = this.ttf.updateGlyf(glyf, index); + Manager.prototype.replaceGlyf = function(glyf, index) { + var list = this.ttf.replaceGlyf(glyf, index); if (list.length) { list[0].modify = 'edit'; this.fireChange(true); @@ -239,6 +239,43 @@ define( return this; }; + /** + * 设置glyf + * + * @param {Object} setting 选项 + * @param {Array} index 索引 + * @return {boolean} + */ + Manager.prototype.updateGlyf = function(setting, index) { + + var glyf = this.getGlyf([index])[0]; + var changed = false; + + if (setting.unicode.toString() != glyf.unicode.toString()) { + glyf.unicode = setting.unicode; + glyf.modify = 'edit'; + changed = true; + } + + if ( + (undefined !== setting.leftSideBearing && setting.leftSideBearing != glyf.leftSideBearing) + || (undefined !== setting.rightSideBearing && setting.rightSideBearing + (glyf.xMax || 0) != glyf.advanceWidth) + ) { + var list = this.ttf.adjustGlyfPos(setting, [index]); + if (list.length) { + list.forEach(function(g) { + g.modify = 'edit'; + }); + changed = true; + } + } + + changed && this.fireChange(true); + + return this; + }; + + /** * 获取glyfList * diff --git a/src/ttf/ttf.js b/src/ttf/ttf.js index af54006..9fad02c 100644 --- a/src/ttf/ttf.js +++ b/src/ttf/ttf.js @@ -293,21 +293,6 @@ define( return result; }; - /** - * 更新指定的glyf - * - * @param {Object} glyf glyfobject - * @param {string} index 需要替换的索引列表 - * @return {Array} 改变的glyf - */ - TTF.prototype.updateGlyf = function(glyf, index) { - if (index >=0 && index < this.ttf.glyf.length) { - this.ttf.glyf[index] = glyf; - return [glyf]; - } - return []; - }; - /** * 调整glyf位置 @@ -500,6 +485,21 @@ define( } }; + /** + * 更新指定的glyf + * + * @param {Object} glyf glyfobject + * @param {string} index 需要替换的索引列表 + * @return {Array} 改变的glyf + */ + TTF.prototype.replaceGlyf = function(glyf, index) { + if (index >=0 && index < this.ttf.glyf.length) { + this.ttf.glyf[index] = glyf; + return [glyf]; + } + return []; + }; + /** * 设置glyf * diff --git a/ttf.html b/ttf.html index 6a9ad2c..8fdad54 100644 --- a/ttf.html +++ b/ttf.html @@ -41,8 +41,9 @@