add hmtx writer
This commit is contained in:
parent
ed475288e8
commit
9a6681ced4
@ -6816,7 +6816,7 @@
|
||||
}
|
||||
],
|
||||
"unicode": [57399],
|
||||
"advanceWidth": 519,
|
||||
"advanceWidth": 569,
|
||||
"leftSideBearing": 10,
|
||||
"name": "uniE037\u0007"
|
||||
},
|
||||
|
@ -408,8 +408,13 @@ define(
|
||||
|
||||
// 设置其他表的信息
|
||||
var xMin = 16384, yMin = 16384, xMax = -16384, yMax = -16384;
|
||||
|
||||
var advanceWidthMax = -1, minLeftSideBearing = 16384, minRightSideBearing = 16384, xMaxExtent = -16384;
|
||||
ttf.glyf.forEach(function(glyf) {
|
||||
advanceWidthMax = Math.max(advanceWidthMax, glyf.advanceWidth);
|
||||
minLeftSideBearing = Math.min(minLeftSideBearing, glyf.leftSideBearing);
|
||||
minRightSideBearing = Math.min(minRightSideBearing, glyf.advanceWidth - glyf.xMax);
|
||||
xMaxExtent = Math.max(xMaxExtent, glyf.xMax);
|
||||
|
||||
if (glyf.xMin < xMin) {
|
||||
xMin = glyf.xMin;
|
||||
}
|
||||
@ -424,6 +429,13 @@ define(
|
||||
}
|
||||
});
|
||||
|
||||
// rewrite hhea
|
||||
ttf.hhea.advanceWidthMax = advanceWidthMax;
|
||||
ttf.hhea.minLeftSideBearing = minLeftSideBearing;
|
||||
ttf.hhea.minRightSideBearing = minRightSideBearing;
|
||||
ttf.hhea.xMaxExtent = xMaxExtent;
|
||||
|
||||
// rewrite head
|
||||
ttf.head.xMin = xMin;
|
||||
ttf.head.yMin = yMin;
|
||||
ttf.head.xMax = xMax;
|
||||
|
@ -52,7 +52,39 @@ define(
|
||||
},
|
||||
|
||||
write: function(writer, ttf) {
|
||||
|
||||
var numOfLongHorMetrics = ttf.hhea.numOfLongHorMetrics;
|
||||
for (var i = 0; i < numOfLongHorMetrics; ++i) {
|
||||
writer.writeUint16(ttf.glyf[i].advanceWidth);
|
||||
writer.writeInt16(ttf.glyf[i].leftSideBearing);
|
||||
}
|
||||
|
||||
// 最后一个宽度
|
||||
var advanceWidth = ttf.glyf[numOfLongHorMetrics - 1].advanceWidth;
|
||||
var numOfLast = ttf.glyf.length - numOfLongHorMetrics;
|
||||
|
||||
for (var i = 0; i < numOfLast; ++i) {
|
||||
writer.writeInt16(ttf.glyf[numOfLongHorMetrics + i].leftSideBearing);
|
||||
}
|
||||
|
||||
return writer;
|
||||
},
|
||||
size: function(ttf) {
|
||||
// 计算同最后一个advanceWidth相等的元素个数
|
||||
var numOfLast = 0;
|
||||
var advanceWidth = ttf.glyf[ttf.glyf.length - 1].advanceWidth; // 最后一个advanceWidth
|
||||
for(var i = ttf.glyf.length - 2; i >= 0; i--) {
|
||||
if (advanceWidth == ttf.glyf[i].advanceWidth) {
|
||||
numOfLast++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ttf.hhea.numOfLongHorMetrics = ttf.glyf.length - numOfLast;
|
||||
|
||||
return 4 * ttf.hhea.numOfLongHorMetrics + 2 * numOfLast;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
@ -13,6 +13,8 @@ define(
|
||||
function(require) {
|
||||
var table = require('./table');
|
||||
var struct = require('./struct');
|
||||
var string = require('../util/string');
|
||||
|
||||
var Posthead = table.create(
|
||||
'posthead',
|
||||
[
|
||||
@ -29,27 +31,6 @@ define(
|
||||
]
|
||||
);
|
||||
|
||||
/**
|
||||
* 读取poststring
|
||||
*
|
||||
* @param {Array.<byte>} byteArray byte数组
|
||||
* @return {Array.<string>} 读取后的字符串数组
|
||||
*/
|
||||
function readPascalString(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]);
|
||||
}
|
||||
strArray.push(str);
|
||||
}
|
||||
return strArray;
|
||||
}
|
||||
|
||||
var post = table.create(
|
||||
'post',
|
||||
[
|
||||
@ -73,7 +54,7 @@ define(
|
||||
var pascalStringOffset = reader.offset;
|
||||
var pascalStringLength = ttf.tables.post.length - (pascalStringOffset - this.offset);
|
||||
var pascalStringBytes = reader.readBytes(reader.offset, pascalStringLength);
|
||||
tbl.names = readPascalString(pascalStringBytes);
|
||||
tbl.names = string.readPascalString(pascalStringBytes);
|
||||
}
|
||||
|
||||
return tbl;
|
||||
|
@ -77,6 +77,11 @@ define(
|
||||
var cmapWriter = new Writer(new ArrayBuffer(cmapTbl.size(ttf)));
|
||||
cmapTbl.write(cmapWriter, ttf);
|
||||
|
||||
// 写入hmtx
|
||||
var hmtxTbl = new supportTables['hmtx']();
|
||||
var hmtxWriter = new Writer(new ArrayBuffer(hmtxTbl.size(ttf)));
|
||||
hmtxTbl.write(hmtxWriter, ttf);
|
||||
|
||||
|
||||
// 读取测试
|
||||
|
||||
@ -87,19 +92,29 @@ define(
|
||||
var locaReader = new Reader(locaWriter.getBuffer());
|
||||
locaTbl.offset = 0;
|
||||
ttf.loca = locaTbl.read(locaReader, ttf);
|
||||
console.log('loca readed');
|
||||
console.log(ttf.loca);
|
||||
|
||||
var glyfReader = new Reader(glyfWriter.getBuffer());
|
||||
glyfTbl.offset = 0;
|
||||
ttf.glyfReaded = glyfTbl.read(glyfReader, ttf);
|
||||
|
||||
var glyf = glyfTbl.read(glyfReader, ttf);
|
||||
console.log('glyf readed');
|
||||
console.log(glyf);
|
||||
|
||||
var cmapReader = new Reader(cmapWriter.getBuffer());
|
||||
cmapTbl.offset = 0;
|
||||
ttf.cmap = cmapTbl.read(cmapReader, ttf);
|
||||
|
||||
var readWindowsAllCodes = require('./util/readWindowsAllCodes');
|
||||
console.log('cmap readed');
|
||||
console.log(readWindowsAllCodes(ttf));
|
||||
|
||||
|
||||
var hmtxReader = new Reader(hmtxWriter.getBuffer());
|
||||
hmtxTbl.offset = 0;
|
||||
var hmtx = hmtxTbl.read(hmtxReader, ttf);
|
||||
console.log('hmtx readed');
|
||||
console.log(hmtx);
|
||||
|
||||
throw 'test';
|
||||
|
||||
// var ttfSize = 20 + tableList.length * 16;
|
||||
|
85
src/ttf/util/string.js
Normal file
85
src/ttf/util/string.js
Normal file
@ -0,0 +1,85 @@
|
||||
/**
|
||||
* @file string.js
|
||||
* @author mengke01
|
||||
* @date
|
||||
* @description
|
||||
* string工具箱
|
||||
*
|
||||
* see svg2ttf @ github
|
||||
*/
|
||||
|
||||
define(
|
||||
function(require) {
|
||||
|
||||
|
||||
var string = {
|
||||
|
||||
/**
|
||||
* 转换成utf8的字节数组
|
||||
*
|
||||
* @param {string} str 字符串
|
||||
* @return {Array.<byte>} 字节数组
|
||||
*/
|
||||
toUTF8Bytes: function(str) {
|
||||
var byteArray = [];
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
if (str.charCodeAt(i) <= 0x7F) {
|
||||
byteArray.push(str.charCodeAt(i));
|
||||
}
|
||||
else {
|
||||
var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
|
||||
for (var j = 0; j < h.length; j++) {
|
||||
byteArray.push(parseInt(h[j], 16));
|
||||
}
|
||||
}
|
||||
}
|
||||
return byteArray;
|
||||
},
|
||||
|
||||
/**
|
||||
* 转换成usc2的字节数组
|
||||
*
|
||||
* @param {string} str 字符串
|
||||
* @return {Array.<byte>} 字节数组
|
||||
*/
|
||||
toUCS2Bytes: function(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) {
|
||||
ch = str.charCodeAt(i);
|
||||
byteArray.push(ch >> 8);
|
||||
byteArray.push(ch & 0xFF);
|
||||
}
|
||||
|
||||
return byteArray;
|
||||
},
|
||||
|
||||
/**
|
||||
* 读取poststring
|
||||
*
|
||||
* @param {Array.<byte>} byteArray byte数组
|
||||
* @return {Array.<string>} 读取后的字符串数组
|
||||
*/
|
||||
readPascalString: 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]);
|
||||
}
|
||||
strArray.push(str);
|
||||
}
|
||||
return strArray;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return string;
|
||||
}
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user