Merge pull request #11823 from Snuffleupagus/validateOS2Table-improvement

[src/core/fonts.js] Improve the `validateOS2Table` function and other code
This commit is contained in:
Tim van der Meij 2020-04-21 00:20:46 +02:00 committed by GitHub
commit a13db5d91a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1063,23 +1063,23 @@ var Font = (function FontClosure() {
); );
} }
function validateOS2Table(os2) { function validateOS2Table(os2, file) {
var stream = new Stream(os2.data); file.pos = (file.start || 0) + os2.offset;
var version = stream.getUint16(); var version = file.getUint16();
// TODO verify all OS/2 tables fields, but currently we validate only those // TODO verify all OS/2 tables fields, but currently we validate only those
// that give us issues // that give us issues
stream.getBytes(60); // skipping type, misc sizes, panose, unicode ranges file.skip(60); // skipping type, misc sizes, panose, unicode ranges
var selection = stream.getUint16(); var selection = file.getUint16();
if (version < 4 && selection & 0x0300) { if (version < 4 && selection & 0x0300) {
return false; return false;
} }
var firstChar = stream.getUint16(); var firstChar = file.getUint16();
var lastChar = stream.getUint16(); var lastChar = file.getUint16();
if (firstChar > lastChar) { if (firstChar > lastChar) {
return false; return false;
} }
stream.getBytes(6); // skipping sTypoAscender/Descender/LineGap file.skip(6); // skipping sTypoAscender/Descender/LineGap
var usWinAscent = stream.getUint16(); var usWinAscent = file.getUint16();
if (usWinAscent === 0) { if (usWinAscent === 0) {
// makes font unreadable by windows // makes font unreadable by windows
return false; return false;
@ -1590,7 +1590,7 @@ var Font = (function FontClosure() {
var start = (file.start ? file.start : 0) + cmap.offset; var start = (file.start ? file.start : 0) + cmap.offset;
file.pos = start; file.pos = start;
file.getUint16(); // version file.skip(2); // version
var numTables = file.getUint16(); var numTables = file.getUint16();
var potentialTable; var potentialTable;
@ -1665,8 +1665,7 @@ var Font = (function FontClosure() {
} }
var format = file.getUint16(); var format = file.getUint16();
file.getUint16(); // length file.skip(2 + 2); // length + language
file.getUint16(); // language
var hasShortCmap = false; var hasShortCmap = false;
var mappings = []; var mappings = [];
@ -1689,13 +1688,13 @@ var Font = (function FontClosure() {
// re-creating the table in format 4 since the encoding // re-creating the table in format 4 since the encoding
// might be changed // might be changed
var segCount = file.getUint16() >> 1; var segCount = file.getUint16() >> 1;
file.getBytes(6); // skipping range fields file.skip(6); // skipping range fields
var segIndex, var segIndex,
segments = []; segments = [];
for (segIndex = 0; segIndex < segCount; segIndex++) { for (segIndex = 0; segIndex < segCount; segIndex++) {
segments.push({ end: file.getUint16() }); segments.push({ end: file.getUint16() });
} }
file.getUint16(); file.skip(2);
for (segIndex = 0; segIndex < segCount; segIndex++) { for (segIndex = 0; segIndex < segCount; segIndex++) {
segments[segIndex].start = file.getUint16(); segments[segIndex].start = file.getUint16();
} }
@ -2162,7 +2161,7 @@ var Font = (function FontClosure() {
end = start + length; end = start + length;
var version = font.getInt32(); var version = font.getInt32();
// skip rest to the tables // skip rest to the tables
font.getBytes(28); font.skip(28);
var glyphNames; var glyphNames;
var valid = true; var valid = true;
@ -2917,7 +2916,7 @@ var Font = (function FontClosure() {
data: createCmapTable(newMapping.charCodeToGlyphId, numGlyphsOut), 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"] = { tables["OS/2"] = {
tag: "OS/2", tag: "OS/2",
data: createOS2Table( data: createOS2Table(