From d333655881846a2a88ef7da6d894037dc552580a Mon Sep 17 00:00:00 2001 From: kekee000 Date: Wed, 30 Aug 2023 21:10:16 +0800 Subject: [PATCH] fix: fix minRightSideBearing, maxComponentElements calc --- package.json | 2 +- src/ttf/table/OS2.js | 25 +++++++++++++------------ test/debug.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 test/debug.js diff --git a/package.json b/package.json index 54938f0..685d04d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fonteditor-core", - "version": "2.2.0", + "version": "2.2.1", "description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.", "keywords": [ "sfnt", diff --git a/src/ttf/table/OS2.js b/src/ttf/table/OS2.js index 4931daf..688a299 100644 --- a/src/ttf/table/OS2.js +++ b/src/ttf/table/OS2.js @@ -177,8 +177,7 @@ export default table.create( }); } }); - - maxComponentElements++; + maxComponentElements = Math.max(maxComponentElements, glyf.glyfs.length); maxCompositePoints = Math.max(maxCompositePoints, compositePoints); maxCompositeContours = Math.max(maxCompositeContours, compositeContours); } @@ -198,30 +197,32 @@ export default table.create( } // 统计边界信息 - if (glyf.xMin < xMin) { + if (null != glyf.xMin && glyf.xMin < xMin) { xMin = glyf.xMin; } - if (glyf.yMin < yMin) { + if (null != glyf.yMin && glyf.yMin < yMin) { yMin = glyf.yMin; } - if (glyf.xMax > xMax) { + if (null != glyf.xMax && glyf.xMax > xMax) { xMax = glyf.xMax; } - if (glyf.yMax > yMax) { + if (null != glyf.yMax && glyf.yMax > yMax) { yMax = glyf.yMax; } 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); - - xAvgCharWidth += glyf.advanceWidth; - - glyfNotEmpty++; + if (null != glyf.xMax) { + minRightSideBearing = Math.min(minRightSideBearing, glyf.advanceWidth - glyf.xMax); + xMaxExtent = Math.max(xMaxExtent, glyf.xMax); + } + if (null != glyf.advanceWidth) { + xAvgCharWidth += glyf.advanceWidth; + glyfNotEmpty++; + } let unicodes = glyf.unicode; diff --git a/test/debug.js b/test/debug.js new file mode 100644 index 0000000..10eef9d --- /dev/null +++ b/test/debug.js @@ -0,0 +1,14 @@ +const fs = require('fs'); +const Font = require('../').Font; + +const buffer = fs.readFileSync('/System/Library/Fonts/NewYork.ttf'); +const font = Font.create(buffer, { + type: 'ttf' +}); +console.log(font.get()['OS/2']); + +const font1 = Font.create(font.write({type: 'ttf'}), { + type: 'ttf' +}); + +console.log(font1.get()['OS/2']); \ No newline at end of file