From 90632398632f9c47ea5c3fba3b39bcfce336fc9d Mon Sep 17 00:00:00 2001 From: kekee000 Date: Fri, 17 Apr 2015 22:53:22 +0800 Subject: [PATCH] add remove postscript name add read windows names set post table to format 3 --- index.html | 1 + src/fonteditor/controller/actions.js | 7 +++ src/fonteditor/widget/ttfmanager.js | 19 +++++++ src/ttf/data/empty.js | 2 +- src/ttf/table/name.js | 19 ++++++- src/ttf/table/post.js | 84 ++++++++++++++++------------ src/ttf/ttf.js | 28 ++++++++++ src/ttf/ttfreader.js | 2 + src/ttf/util/readWindowsAllCodes.js | 2 + 9 files changed, 123 insertions(+), 41 deletions(-) diff --git a/index.html b/index.html index 7333b3f..0d6e1c2 100644 --- a/index.html +++ b/index.html @@ -57,6 +57,7 @@
  • 字体信息
  • 字体度量
  • 生成字形名称
  • +
  • 清除字形名称
  • 编辑器
  • 导入和导出
  • diff --git a/src/fonteditor/controller/actions.js b/src/fonteditor/controller/actions.js index 24dde34..c8eaa08 100644 --- a/src/fonteditor/controller/actions.js +++ b/src/fonteditor/controller/actions.js @@ -259,6 +259,13 @@ define( } }, + 'setting-glyf-clearname': function () { + var ttf = program.ttfManager.get(); + if (ttf) { + program.ttfManager.clearGlyfName(program.viewer.getSelected()); + } + }, + 'setting-editor': function () { var SettingEditor = settingSupport.editor; !new SettingEditor({ diff --git a/src/fonteditor/widget/ttfmanager.js b/src/fonteditor/widget/ttfmanager.js index 31a5a83..538693f 100644 --- a/src/fonteditor/widget/ttfmanager.js +++ b/src/fonteditor/widget/ttfmanager.js @@ -235,6 +235,25 @@ define( return this; }; + /** + * 清除字形名称 + * + * @param {Array} indexList 索引列表 + * @return {this} + */ + Manager.prototype.clearGlyfName = function (indexList) { + + var list = this.ttf.clearGlyfName(indexList); + if (list.length) { + list.forEach(function (g) { + g.modify = 'edit'; + }); + this.fireChange(true); + } + + return this; + }; + /** * 添加并体替换指定的glyf * diff --git a/src/ttf/data/empty.js b/src/ttf/data/empty.js index 86e5a58..c07a7c1 100644 --- a/src/ttf/data/empty.js +++ b/src/ttf/data/empty.js @@ -114,7 +114,7 @@ define( "maxMemType42": 0, "minMemType1": 0, "maxMemType1": 1, - "format": 2 + "format": 3 }, "OS/2": { "version": 4, diff --git a/src/ttf/table/name.js b/src/ttf/table/name.js index 3bafb43..6361d22 100644 --- a/src/ttf/table/name.js +++ b/src/ttf/table/name.js @@ -54,14 +54,27 @@ define( var names = {}; - // 读取windows下的name records - var platform = platformTbl.Microsoft; - var encoding = encodingTbl.win.UCS2; + // mac 下的english name + var platform = platformTbl.Macintosh; + var encoding = encodingTbl.mac.Default; + var language = 0; + + // 如果有windows 下的 english,则用windows下的 name + if (nameRecordTbl.some(function (record) { + return record.platform === platformTbl.Microsoft + && record.encoding === encodingTbl.win.UCS2 + && record.language === 1033; + })) { + platform = platformTbl.Microsoft; + encoding = encodingTbl.win.UCS2; + language = 1033; + } for (i = 0; i < count; ++i) { nameRecord = nameRecordTbl[i]; if (nameRecord.platform === platform && nameRecord.encoding === encoding + && nameRecord.language === language && nameIdTbl[nameRecord.nameId]) { names[nameIdTbl[nameRecord.nameId]] = string.stringify(nameRecord.name); } diff --git a/src/ttf/table/post.js b/src/ttf/table/post.js index aa16a4d..7969fdb 100644 --- a/src/ttf/table/post.js +++ b/src/ttf/table/post.js @@ -25,8 +25,7 @@ define( ['minMemType42', struct.Uint32], ['maxMemType42', struct.Uint32], ['minMemType1', struct.Uint32], - ['maxMemType1', struct.Uint32], - ['numberOfGlyphs', struct.Uint16] + ['maxMemType1', struct.Uint32] ] ); @@ -36,19 +35,15 @@ define( { read: function (reader, ttf) { - var tbl = null; var format = reader.readFixed(this.offset); + // 读取表头 + var tbl = new Posthead(this.offset).read(reader, ttf); // format2 if (format === 2) { - - // 读取表头 - tbl = new Posthead(this.offset).read(reader, ttf); - tbl.format = format; - - var numberOfGlyphs = ttf.maxp.numGlyphs; - + var numberOfGlyphs = reader.readUint16(); var glyphNameIndex = []; + for (var i = 0; i < numberOfGlyphs; ++i) { glyphNameIndex.push(reader.readUint16()); } @@ -60,10 +55,9 @@ define( tbl.glyphNameIndex = glyphNameIndex; tbl.names = string.readPascalString(pascalStringBytes); } - else { - tbl = { - format: format - }; + // deprecated + else if (format === 2.5) { + tbl.format = 3; } return tbl; @@ -71,11 +65,13 @@ define( write: function (writer, ttf) { - var numberOfGlyphs = ttf.glyf.length; - var post = ttf.post || {}; + + var post = ttf.post || { + format: 3 + }; // write header - writer.writeFixed(2); // format + writer.writeFixed(post.format); // format writer.writeFixed(post.italicAngle || 0); // italicAngle writer.writeInt16(post.underlinePosition || 0); // underlinePosition writer.writeInt16(post.underlineThickness || 0); // underlineThickness @@ -84,22 +80,37 @@ define( writer.writeUint32(post.maxMemType42 || 0); // maxMemType42 writer.writeUint32(post.minMemType1 || 0); // minMemType1 writer.writeUint32(post.maxMemType1 || 0); // maxMemType1 - writer.writeUint16(numberOfGlyphs); // numberOfGlyphs - // write glyphNameIndex - var nameIndexs = ttf.support.post.nameIndexs; - for (var i = 0, l = nameIndexs.length; i < l; i++) { - writer.writeUint16(nameIndexs[i]); + // version 3 不设置post信息 + if (post.format === 2) { + var numberOfGlyphs = ttf.glyf.length; + writer.writeUint16(numberOfGlyphs); // numberOfGlyphs + // write glyphNameIndex + var nameIndexs = ttf.support.post.nameIndexs; + for (var i = 0, l = nameIndexs.length; i < l; i++) { + writer.writeUint16(nameIndexs[i]); + } + + // write names + ttf.support.post.glyphNames.forEach(function (name) { + writer.writeBytes(name); + }); } - - // write names - ttf.support.post.glyphNames.forEach(function (name) { - writer.writeBytes(name); - }); }, size: function (ttf) { + var numberOfGlyphs = ttf.glyf.length; + ttf.post = ttf.post || {}; + ttf.post.format = ttf.post.format || 3; + ttf.post.maxMemType1 = numberOfGlyphs; + + // version 3 不设置post信息 + if (post.format === 3 || post.format === 1) { + return 34; + } + + // version 2 var glyphNames = []; var nameIndexs = []; var size = 34 + numberOfGlyphs * 2; // header + numberOfGlyphs * 2 @@ -122,21 +133,20 @@ define( // 这里需要注意,"" 有可能是"\3" length不为0,但是是空字符串 var name = glyf.name; if (!name || name.charCodeAt(0) < 32) { - name = string.getUnicodeName(unicode); + nameIndexs.push(258 + nameIndex++); + glyphNames.push([0]); + size++; + } + else { + nameIndexs.push(258 + nameIndex++); + var bytes = string.getPascalStringBytes(name); // pascal string bytes + glyphNames.push(bytes); + size += bytes.length; } - - nameIndexs.push(258 + nameIndex++); - var bytes = string.getPascalStringBytes(name); // pascal string bytes - glyphNames.push(bytes); - size += bytes.length; } } } - ttf.post = ttf.post || {}; - ttf.post.format = ttf.post.format || 0x2; - ttf.post.maxMemType1 = numberOfGlyphs; - ttf.support.post = { nameIndexs: nameIndexs, glyphNames: glyphNames diff --git a/src/ttf/ttf.js b/src/ttf/ttf.js index c88460c..853243a 100644 --- a/src/ttf/ttf.js +++ b/src/ttf/ttf.js @@ -401,6 +401,34 @@ define( return list; }; + /** + * 清除字形名称 + * + * @param {Array} indexList 索引列表 + * @return {Array} 改变的glyf + */ + TTF.prototype.clearGlyfName = function (indexList) { + var glyf = this.ttf.glyf; + var list = []; + if (indexList && indexList.length) { + list = indexList.map(function (item) { + return glyf[item]; + }); + } + else { + list = glyf; + } + + if (list.length) { + + list.forEach(function (g) { + delete g.name; + }); + } + + return list; + }; + /** * 添加并体替换指定的glyf * diff --git a/src/ttf/ttfreader.js b/src/ttf/ttfreader.js index f467b2c..df155d2 100644 --- a/src/ttf/ttfreader.js +++ b/src/ttf/ttfreader.js @@ -128,6 +128,8 @@ define( delete ttf.loca; delete ttf.post.glyphNameIndex; delete ttf.post.names; + delete ttf.cmap; + if (!this.options.hinting) { delete ttf.fpgm; delete ttf.cvt; diff --git a/src/ttf/util/readWindowsAllCodes.js b/src/ttf/util/readWindowsAllCodes.js index aff4778..d3ea657 100644 --- a/src/ttf/util/readWindowsAllCodes.js +++ b/src/ttf/util/readWindowsAllCodes.js @@ -104,6 +104,8 @@ define( } } + delete codes[65535]; + } }