Skip to content

Commit

Permalink
fix: fix no glyph contours write error, add writeZeroContoursGlyfData…
Browse files Browse the repository at this point in the history
… option
  • Loading branch information
kekee000 committed Aug 31, 2023
1 parent 9a9ce8b commit 9df6a9e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ console.log(Object.keys(fontObject));
const buffer = font.write({
// support ttf, woff, woff2, eot, svg
type: 'woff',
// save font hinting
// save font hinting, default false
hinting: true,
// write glyf data when simple glyph has no contours, default false
writeZeroContoursGlyfData: false,
// deflate function for woff, eg. pako.deflate
deflate: undefined,
// for user to overwrite head.xMin, head.xMax, head.yMin, head.yMax, hhea etc.
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ export namespace FontEditor {
*/
hinting?: boolean;

/**
* write glyf data when simple glyph has no contours, default false
*/
writeZeroContoursGlyfData?: boolean;

/**
* svg output meta data
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fonteditor-core",
"version": "2.2.2",
"version": "2.3.0",
"description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.",
"keywords": [
"sfnt",
Expand Down
13 changes: 9 additions & 4 deletions src/ttf/table/glyf/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,17 @@ export default function parseGlyf(reader, ttf, offset) {
// 读取简单字形
if (numberOfContours >= 0) {
// endPtsOfConturs
const endPtsOfContours = [];
if (numberOfContours >= 0) {
glyf.endPtsOfContours = [];
if (numberOfContours > 0) {
for (i = 0; i < numberOfContours; i++) {
endPtsOfContours.push(reader.readUint16());
glyf.endPtsOfContours.push(reader.readUint16());
}
glyf.endPtsOfContours = endPtsOfContours;
}
else {
delete glyf.xMin;
delete glyf.yMin;
delete glyf.xMax;
delete glyf.yMax;
}

// instructions
Expand Down
9 changes: 7 additions & 2 deletions src/ttf/table/glyf/sizeof.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import glyFlag from '../../enum/glyFlag';
* @param {Object} glyf glyf对象
* @param {Object} glyfSupport glyf相关统计
* @param {boolean} hinting 是否保留hints
* @param {boolean} writeZeroContoursGlyfData 是否写空轮廓 glyph
* @return {number} size大小
*/
function sizeofSimple(glyf, glyfSupport, hinting) {
function sizeofSimple(glyf, glyfSupport, hinting, writeZeroContoursGlyfData) {
if (!writeZeroContoursGlyfData && (!glyf.contours || !glyf.contours.length)) {
return 0;
}

// fixed header + endPtsOfContours
let result = 12
Expand Down Expand Up @@ -221,13 +225,14 @@ export default function sizeof(ttf) {
ttf.support.glyf = [];
let tableSize = 0;
const hinting = ttf.writeOptions ? ttf.writeOptions.hinting : false;
const writeZeroContoursGlyfData = ttf.writeOptions ? ttf.writeOptions.writeZeroContoursGlyfData : false;
ttf.glyf.forEach((glyf) => {
let glyfSupport = {};
glyfSupport = glyf.compound ? glyfSupport : getFlags(glyf, glyfSupport);

const glyfSize = glyf.compound
? sizeofCompound(glyf, hinting)
: sizeofSimple(glyf, glyfSupport, hinting);
: sizeofSimple(glyf, glyfSupport, hinting, writeZeroContoursGlyfData);
let size = glyfSize;

// 4字节对齐
Expand Down
8 changes: 5 additions & 3 deletions src/ttf/table/glyf/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import componentFlag from '../../enum/componentFlag';
* @return {Object} 写入器
*/
export default function write(writer, ttf) {

const hinting = ttf.writeOptions ? ttf.writeOptions.hinting : false;
const writeZeroContoursGlyfData = ttf.writeOptions ? ttf.writeOptions.writeZeroContoursGlyfData : false;
ttf.glyf.forEach((glyf, index) => {

// 非复合图元没有轮廓则不写
if (!glyf.compound && !writeZeroContoursGlyfData && (!glyf.contours || !glyf.contours.length)) {
return;
}
// header
writer.writeInt16(glyf.compound ? -1 : (glyf.contours || []).length);
writer.writeInt16(glyf.xMin);
Expand Down Expand Up @@ -102,7 +105,6 @@ export default function write(writer, ttf) {

}
else {

let endPtsOfContours = -1;
(glyf.contours || []).forEach((contour) => {
endPtsOfContours += contour.length;
Expand Down
3 changes: 2 additions & 1 deletion src/ttf/ttfwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const SUPPORT_TABLES = [
export default class TTFWriter {
constructor(options = {}) {
this.options = {
writeZeroContoursGlyfData: options.writeZeroContoursGlyfData || false, // 不写入空 glyf 数据
hinting: options.hinting || false, // 不保留hints信息
support: options.support // 自定义的导出表结构,可以自己修改某些表项目
};
Expand Down Expand Up @@ -205,7 +206,7 @@ export default class TTFWriter {
}
});
}

ttf.writeOptions.writeZeroContoursGlyfData = !!this.options.writeZeroContoursGlyfData;
ttf.writeOptions.hinting = !!this.options.hinting;
ttf.writeOptions.tables = tables.sort();
}
Expand Down
1 change: 1 addition & 0 deletions test/example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fs.writeFileSync(`${baseDir}/output/font.eot`, utils.toBuffer(utils.ttf2eot(util
const out = font.write({
type: 'woff2',
toBuffer: true,
writeZeroContoursGlyfData: true,
});
fs.writeFileSync(`${baseDir}/output/font.woff`, out);
});
Expand Down

0 comments on commit 9df6a9e

Please sign in to comment.