From 4fae1ac5c4c63a0dc09a6fd3f3f4d2d7d716c306 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 19 Apr 2020 10:22:38 +0200 Subject: [PATCH 1/3] [src/core/fonts.js] Replace some unnecessary `Stream.getBytes(...)` calls with `Stream.skip(...)` instead There's a handful of cases in the code where the intention is simply to advance the `Stream` position, but rather than only doing that the code instead fetches the bytes in question (and without using the result for anything). --- src/core/fonts.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/fonts.js b/src/core/fonts.js index ef030dd95..6e22b9e14 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -1068,7 +1068,7 @@ var Font = (function FontClosure() { var version = stream.getUint16(); // TODO verify all OS/2 tables fields, but currently we validate only those // that give us issues - stream.getBytes(60); // skipping type, misc sizes, panose, unicode ranges + stream.skip(60); // skipping type, misc sizes, panose, unicode ranges var selection = stream.getUint16(); if (version < 4 && selection & 0x0300) { return false; @@ -1078,7 +1078,7 @@ var Font = (function FontClosure() { if (firstChar > lastChar) { return false; } - stream.getBytes(6); // skipping sTypoAscender/Descender/LineGap + stream.skip(6); // skipping sTypoAscender/Descender/LineGap var usWinAscent = stream.getUint16(); if (usWinAscent === 0) { // makes font unreadable by windows @@ -1689,7 +1689,7 @@ var Font = (function FontClosure() { // re-creating the table in format 4 since the encoding // might be changed var segCount = file.getUint16() >> 1; - file.getBytes(6); // skipping range fields + file.skip(6); // skipping range fields var segIndex, segments = []; for (segIndex = 0; segIndex < segCount; segIndex++) { @@ -2162,7 +2162,7 @@ var Font = (function FontClosure() { end = start + length; var version = font.getInt32(); // skip rest to the tables - font.getBytes(28); + font.skip(28); var glyphNames; var valid = true; From 033d27fc25342f309fc208b0f8cc555bc78cdeaa Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 19 Apr 2020 10:29:26 +0200 Subject: [PATCH 2/3] [src/core/fonts.js] Replace some unnecessary `Stream.getUint16()` calls with `Stream.skip(2)` instead There's a handful of cases in the code where the intention is simply to advance the `Stream` position, but rather than only doing that the code instead fetches/computes a Uint16 value (and without using the result for anything). --- src/core/fonts.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/core/fonts.js b/src/core/fonts.js index 6e22b9e14..73f82ac18 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -1590,7 +1590,7 @@ var Font = (function FontClosure() { var start = (file.start ? file.start : 0) + cmap.offset; file.pos = start; - file.getUint16(); // version + file.skip(2); // version var numTables = file.getUint16(); var potentialTable; @@ -1665,8 +1665,7 @@ var Font = (function FontClosure() { } var format = file.getUint16(); - file.getUint16(); // length - file.getUint16(); // language + file.skip(2 + 2); // length + language var hasShortCmap = false; var mappings = []; @@ -1695,7 +1694,7 @@ var Font = (function FontClosure() { for (segIndex = 0; segIndex < segCount; segIndex++) { segments.push({ end: file.getUint16() }); } - file.getUint16(); + file.skip(2); for (segIndex = 0; segIndex < segCount; segIndex++) { segments[segIndex].start = file.getUint16(); } From 695140728a153c02a92f2ce7e1c548f4e2ee1cd5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 19 Apr 2020 11:17:36 +0200 Subject: [PATCH 3/3] [src/core/fonts.js] Improve the `validateOS2Table` function Rather than creating a new `Stream` just to validate the OS/2 TrueType table, it's simpler/better to just pass in a reference to the font data and use that instead (similar to other TrueType helper functions). --- src/core/fonts.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/core/fonts.js b/src/core/fonts.js index 73f82ac18..4aa4183fe 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -1063,23 +1063,23 @@ var Font = (function FontClosure() { ); } - function validateOS2Table(os2) { - var stream = new Stream(os2.data); - var version = stream.getUint16(); + function validateOS2Table(os2, file) { + file.pos = (file.start || 0) + os2.offset; + var version = file.getUint16(); // TODO verify all OS/2 tables fields, but currently we validate only those // that give us issues - stream.skip(60); // skipping type, misc sizes, panose, unicode ranges - var selection = stream.getUint16(); + file.skip(60); // skipping type, misc sizes, panose, unicode ranges + var selection = file.getUint16(); if (version < 4 && selection & 0x0300) { return false; } - var firstChar = stream.getUint16(); - var lastChar = stream.getUint16(); + var firstChar = file.getUint16(); + var lastChar = file.getUint16(); if (firstChar > lastChar) { return false; } - stream.skip(6); // skipping sTypoAscender/Descender/LineGap - var usWinAscent = stream.getUint16(); + file.skip(6); // skipping sTypoAscender/Descender/LineGap + var usWinAscent = file.getUint16(); if (usWinAscent === 0) { // makes font unreadable by windows return false; @@ -2916,7 +2916,7 @@ var Font = (function FontClosure() { data: createCmapTable(newMapping.charCodeToGlyphId, numGlyphsOut), }; - if (!tables["OS/2"] || !validateOS2Table(tables["OS/2"])) { + if (!tables["OS/2"] || !validateOS2Table(tables["OS/2"], font)) { tables["OS/2"] = { tag: "OS/2", data: createOS2Table(