support 2bytes name
This commit is contained in:
@@ -62,13 +62,13 @@ define(
|
||||
var fileReader = new FileReader();
|
||||
|
||||
fileReader.onload = function (e) {
|
||||
try {
|
||||
//try {
|
||||
options.success && options.success(getttf(e.target.result, options));
|
||||
}
|
||||
catch (exp) {
|
||||
alert(exp.message);
|
||||
throw exp;
|
||||
}
|
||||
// }
|
||||
// catch (exp) {
|
||||
// alert(exp.message);
|
||||
// throw exp;
|
||||
// }
|
||||
|
||||
loading.hide();
|
||||
};
|
||||
|
||||
@@ -202,11 +202,10 @@ define(
|
||||
return cff;
|
||||
},
|
||||
|
||||
write: function (
|
||||
/* writer, font */
|
||||
) {
|
||||
write: function (writer, font) {
|
||||
throw 'not support write cff table';
|
||||
},
|
||||
|
||||
size: function (font) {
|
||||
throw 'not support get cff table size';
|
||||
}
|
||||
|
||||
@@ -234,7 +234,9 @@ define(
|
||||
var glyfSupport = {};
|
||||
glyfSupport = glyf.compound ? glyfSupport : getFlags(glyf, glyfSupport);
|
||||
|
||||
var glyfSize = glyf.compound ? sizeofCompound(glyf, hinting) : sizeofSimple(glyf, glyfSupport, hinting);
|
||||
var glyfSize = glyf.compound
|
||||
? sizeofCompound(glyf, hinting)
|
||||
: sizeofSimple(glyf, glyfSupport, hinting);
|
||||
var size = glyfSize;
|
||||
|
||||
// 4字节对齐
|
||||
|
||||
@@ -46,7 +46,7 @@ define(
|
||||
// 读取字符名字
|
||||
for (i = 0; i < count; ++i) {
|
||||
nameRecord = nameRecordTbl[i];
|
||||
nameRecord.name = reader.readString(offset + nameRecord.offset, nameRecord.length);
|
||||
nameRecord.name = reader.readBytes(offset + nameRecord.offset, nameRecord.length);
|
||||
}
|
||||
|
||||
var names = {};
|
||||
@@ -73,7 +73,9 @@ define(
|
||||
&& nameRecord.encoding === encoding
|
||||
&& nameRecord.language === language
|
||||
&& nameIdTbl[nameRecord.nameId]) {
|
||||
names[nameIdTbl[nameRecord.nameId]] = string.stringify(nameRecord.name);
|
||||
names[nameIdTbl[nameRecord.nameId]] = language === 0
|
||||
? string.getUTF8String(nameRecord.name)
|
||||
: string.getUCS2String(nameRecord.name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,8 +50,8 @@ define(
|
||||
var pascalStringLength = ttf.tables.post.length - (pascalStringOffset - this.offset);
|
||||
var pascalStringBytes = reader.readBytes(reader.offset, pascalStringLength);
|
||||
|
||||
tbl.glyphNameIndex = glyphNameIndex;
|
||||
tbl.names = string.readPascalString(pascalStringBytes);
|
||||
tbl.nameIndex = glyphNameIndex; // 设置glyf名字索引
|
||||
tbl.names = string.getPascalString(pascalStringBytes); // glyf名字数组
|
||||
}
|
||||
// deprecated
|
||||
else if (format === 2.5) {
|
||||
@@ -84,13 +84,13 @@ define(
|
||||
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]);
|
||||
var nameIndex = ttf.support.post.nameIndex;
|
||||
for (var i = 0, l = nameIndex.length; i < l; i++) {
|
||||
writer.writeUint16(nameIndex[i]);
|
||||
}
|
||||
|
||||
// write names
|
||||
ttf.support.post.glyphNames.forEach(function (name) {
|
||||
ttf.support.post.names.forEach(function (name) {
|
||||
writer.writeBytes(name);
|
||||
});
|
||||
}
|
||||
@@ -109,35 +109,35 @@ define(
|
||||
}
|
||||
|
||||
// version 2
|
||||
var glyphNames = [];
|
||||
var nameIndexs = [];
|
||||
var size = 34 + numberOfGlyphs * 2; // header + numberOfGlyphs * 2
|
||||
var glyphNames = [];
|
||||
var nameIndexArr = [];
|
||||
var nameIndex = 0;
|
||||
|
||||
// 获取 name的大小
|
||||
for (var i = 0; i < numberOfGlyphs; i++) {
|
||||
// .notdef
|
||||
if (i === 0) {
|
||||
nameIndexs.push(0);
|
||||
nameIndexArr.push(0);
|
||||
}
|
||||
else {
|
||||
var glyf = ttf.glyf[i];
|
||||
var unicode = glyf.unicode ? glyf.unicode[0] : 0;
|
||||
var unicodeNameIndex = unicodeName[unicode];
|
||||
if (undefined !== unicodeNameIndex) {
|
||||
nameIndexs.push(unicodeNameIndex);
|
||||
nameIndexArr.push(unicodeNameIndex);
|
||||
}
|
||||
else {
|
||||
// 这里需要注意,"" 有可能是"\3" length不为0,但是是空字符串
|
||||
var name = glyf.name;
|
||||
if (!name || name.charCodeAt(0) < 32) {
|
||||
nameIndexs.push(258 + nameIndex++);
|
||||
nameIndexArr.push(258 + nameIndex++);
|
||||
glyphNames.push([0]);
|
||||
size++;
|
||||
}
|
||||
else {
|
||||
nameIndexs.push(258 + nameIndex++);
|
||||
var bytes = string.getPascalStringBytes(name); // pascal string bytes
|
||||
nameIndexArr.push(258 + nameIndex++);
|
||||
var bytes = string.toPascalStringBytes(name); // pascal string bytes
|
||||
glyphNames.push(bytes);
|
||||
size += bytes.length;
|
||||
}
|
||||
@@ -146,8 +146,8 @@ define(
|
||||
}
|
||||
|
||||
ttf.support.post = {
|
||||
nameIndexs: nameIndexs,
|
||||
glyphNames: glyphNames
|
||||
nameIndex: nameIndexArr,
|
||||
names: glyphNames
|
||||
};
|
||||
|
||||
return size;
|
||||
|
||||
@@ -103,9 +103,9 @@ define(
|
||||
glyf[i].leftSideBearing = item.leftSideBearing;
|
||||
});
|
||||
|
||||
// name
|
||||
// format = 2 的post表会携带glyf name信息
|
||||
if (ttf.post && 2 === ttf.post.format) {
|
||||
var nameIndex = ttf.post.glyphNameIndex;
|
||||
var nameIndex = ttf.post.nameIndex;
|
||||
var names = ttf.post.names;
|
||||
nameIndex.forEach(function (nameIndex, i) {
|
||||
if (nameIndex <= 257) {
|
||||
@@ -127,18 +127,21 @@ define(
|
||||
delete ttf.tables;
|
||||
delete ttf.hmtx;
|
||||
delete ttf.loca;
|
||||
delete ttf.post.glyphNameIndex;
|
||||
delete ttf.post.nameIndex;
|
||||
delete ttf.post.names;
|
||||
|
||||
// 不携带hinting信息则删除hint相关表
|
||||
if (!this.options.hinting) {
|
||||
delete ttf.fpgm;
|
||||
delete ttf.cvt;
|
||||
delete ttf.prep;
|
||||
|
||||
ttf.glyf.forEach(function (glyf) {
|
||||
delete glyf.instructions;
|
||||
});
|
||||
}
|
||||
|
||||
// 复合字形转简单字形
|
||||
if (this.options.compound2simple && ttf.maxp.maxComponentElements) {
|
||||
ttf.glyf.forEach(function (glyf) {
|
||||
if (glyf.compound) {
|
||||
|
||||
@@ -20,7 +20,19 @@ define(
|
||||
* @return {string} 转换后字符串
|
||||
*/
|
||||
function stringify(str) {
|
||||
return decodeURIComponent(encodeURIComponent(str).replace(/%00([\x00-\xff])/g, '$1'));
|
||||
if (!str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
var newStr = '';
|
||||
for (var i = 0, l = str.length, ch; i < l; i++) {
|
||||
ch = str.charCodeAt(i);
|
||||
if (ch === 0) {
|
||||
continue;
|
||||
}
|
||||
newStr += String.fromCharCode(ch);
|
||||
}
|
||||
return newStr;
|
||||
}
|
||||
|
||||
var string = {
|
||||
@@ -64,7 +76,7 @@ define(
|
||||
toUTF8Bytes: function (str) {
|
||||
str = stringify(str);
|
||||
var byteArray = [];
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
for (var i = 0, l = str.length; i < l; i++) {
|
||||
if (str.charCodeAt(i) <= 0x7F) {
|
||||
byteArray.push(str.charCodeAt(i));
|
||||
}
|
||||
@@ -86,12 +98,9 @@ define(
|
||||
*/
|
||||
toUCS2Bytes: function (str) {
|
||||
str = stringify(str);
|
||||
// Code is taken here:
|
||||
// http://stackoverflow.com/questions/6226189/how-to-convert-a-string-to-bytearray
|
||||
var byteArray = [];
|
||||
var ch;
|
||||
|
||||
for (var i = 0; i < str.length; ++i) {
|
||||
for (var i = 0, l = str.length, ch; i < l; i++) {
|
||||
ch = str.charCodeAt(i);
|
||||
byteArray.push(ch >> 8);
|
||||
byteArray.push(ch & 0xFF);
|
||||
@@ -100,19 +109,74 @@ define(
|
||||
return byteArray;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 获取pascal string 字节数组
|
||||
* @param {string} str 字符串
|
||||
* @return {Array.<byte>} byteArray byte数组
|
||||
*/
|
||||
toPascalStringBytes: function (str) {
|
||||
var bytes = [];
|
||||
var length = str ? (str.length < 256 ? str.length : 255) : 0;
|
||||
bytes.push(length);
|
||||
|
||||
for (var i = 0, l = str.length; i < l; i++) {
|
||||
var c = str.charCodeAt(i);
|
||||
// non-ASCII characters are substituted with '*'
|
||||
bytes.push(c < 128 ? c : 42);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
},
|
||||
|
||||
/**
|
||||
* utf8字节转字符串
|
||||
*
|
||||
* @param {Array} bytes 字节
|
||||
* @return {string} 字符串
|
||||
*/
|
||||
getUTF8String: function (bytes) {
|
||||
var str = '';
|
||||
for (var i = 0, l = bytes.length; i < l; i++) {
|
||||
if (bytes[i] < 0x7F) {
|
||||
str += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
else {
|
||||
str += '%' + (256 + bytes[i]).toString(16).slice(1);
|
||||
}
|
||||
}
|
||||
return decodeURIComponent(str);
|
||||
},
|
||||
|
||||
/**
|
||||
* ucs2字节转字符串
|
||||
*
|
||||
* @param {Array} bytes 字节
|
||||
* @return {string} 字符串
|
||||
*/
|
||||
getUCS2String: function (bytes) {
|
||||
var str = '';
|
||||
for (var i = 0, l = bytes.length; i < l; i += 2) {
|
||||
str += String.fromCharCode((bytes[i] << 8) + bytes[i + 1]);
|
||||
}
|
||||
return str;
|
||||
},
|
||||
|
||||
/**
|
||||
* 读取 pascal string
|
||||
*
|
||||
* @param {Array.<byte>} byteArray byte数组
|
||||
* @return {Array.<string>} 读取后的字符串数组
|
||||
*/
|
||||
readPascalString: function (byteArray) {
|
||||
getPascalString: function (byteArray) {
|
||||
var strArray = [];
|
||||
var i = 0;
|
||||
var l = byteArray.length;
|
||||
|
||||
while (i < l) {
|
||||
var strLength = byteArray[i++];
|
||||
var str = '';
|
||||
|
||||
while (strLength-- > 0 && i < l) {
|
||||
str += String.fromCharCode(byteArray[i++]);
|
||||
}
|
||||
@@ -122,26 +186,7 @@ define(
|
||||
}
|
||||
|
||||
return strArray;
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取pascal string 字节数组
|
||||
* @param {string} str 字符串
|
||||
* @return {Array.<byte>} byteArray byte数组
|
||||
*/
|
||||
getPascalStringBytes: function (str) {
|
||||
var bytes = [];
|
||||
var length = str ? (str.length < 256 ? str.length : 255) : 0;
|
||||
bytes.push(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
var c = str.charCodeAt(i);
|
||||
// non-ASCII characters are substituted with '*'
|
||||
bytes.push(c < 128 ? c : 42);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return string;
|
||||
|
||||
Reference in New Issue
Block a user