Browse Source

Merge pull request #1504 from yurydelendik/issue-1502

Rebuilding invalid OS/2 table
Brendan Dahl 13 years ago
parent
commit
56079d77bc
  1. 52
      src/fonts.js

52
src/fonts.js

@ -1726,6 +1726,16 @@ var Font = (function FontClosure() {
properties.glyphNames = glyphNames; properties.glyphNames = glyphNames;
} }
function isOS2Valid(os2Table) {
var data = os2Table.data;
// usWinAscent == 0 makes font unreadable by windows
var usWinAscent = (data[74] << 8) | data[75];
if (usWinAscent == 0)
return false;
return true;
}
// Check that required tables are present // Check that required tables are present
var requiredTables = ['OS/2', 'cmap', 'head', 'hhea', var requiredTables = ['OS/2', 'cmap', 'head', 'hhea',
'hmtx', 'maxp', 'name', 'post']; 'hmtx', 'maxp', 'name', 'post'];
@ -1733,7 +1743,7 @@ var Font = (function FontClosure() {
var header = readOpenTypeHeader(font); var header = readOpenTypeHeader(font);
var numTables = header.numTables; var numTables = header.numTables;
var cmap, post, maxp, hhea, hmtx, vhea, vmtx, head, loca, glyf; var cmap, post, maxp, hhea, hmtx, vhea, vmtx, head, loca, glyf, os2;
var tables = []; var tables = [];
for (var i = 0; i < numTables; i++) { for (var i = 0; i < numTables; i++) {
var table = readTableEntry(font); var table = readTableEntry(font);
@ -1751,6 +1761,8 @@ var Font = (function FontClosure() {
hmtx = table; hmtx = table;
else if (table.tag == 'head') else if (table.tag == 'head')
head = table; head = table;
else if (table.tag == 'OS/2')
os2 = table;
requiredTables.splice(index, 1); requiredTables.splice(index, 1);
} else { } else {
@ -1766,7 +1778,7 @@ var Font = (function FontClosure() {
tables.push(table); tables.push(table);
} }
var numTables = header.numTables + requiredTables.length; var numTables = tables.length + requiredTables.length;
// header and new offsets. Table entry information is appended to the // header and new offsets. Table entry information is appended to the
// end of file. The virtualOffset represents where to put the actual // end of file. The virtualOffset represents where to put the actual
@ -1780,21 +1792,10 @@ var Font = (function FontClosure() {
// of missing tables // of missing tables
createOpenTypeHeader(header.version, ttf, numTables); createOpenTypeHeader(header.version, ttf, numTables);
if (requiredTables.indexOf('OS/2') != -1) { // Invalid OS/2 can break the font for the Windows
// extract some more font properties from the OpenType head and if (os2 && !isOS2Valid(os2)) {
// hhea tables; yMin and descent value are always negative tables.splice(tables.indexOf(os2), 1);
var override = { os2 = null;
unitsPerEm: int16([head.data[18], head.data[19]]),
yMax: int16([head.data[42], head.data[43]]),
yMin: int16([head.data[38], head.data[39]]) - 0x10000,
ascent: int16([hhea.data[4], hhea.data[5]]),
descent: int16([hhea.data[6], hhea.data[7]]) - 0x10000
};
tables.push({
tag: 'OS/2',
data: stringToArray(createOS2Table(properties, null, override))
});
} }
// Ensure the [h/v]mtx tables contains the advance width and // Ensure the [h/v]mtx tables contains the advance width and
@ -2075,6 +2076,23 @@ var Font = (function FontClosure() {
} }
this.unicodeIsEnabled = unicodeIsEnabled; this.unicodeIsEnabled = unicodeIsEnabled;
if (!os2) {
// extract some more font properties from the OpenType head and
// hhea tables; yMin and descent value are always negative
var override = {
unitsPerEm: int16([head.data[18], head.data[19]]),
yMax: int16([head.data[42], head.data[43]]),
yMin: int16([head.data[38], head.data[39]]) - 0x10000,
ascent: int16([hhea.data[4], hhea.data[5]]),
descent: int16([hhea.data[6], hhea.data[7]]) - 0x10000
};
tables.push({
tag: 'OS/2',
data: stringToArray(createOS2Table(properties, glyphs, override))
});
}
// Rewrite the 'post' table if needed // Rewrite the 'post' table if needed
if (requiredTables.indexOf('post') != -1) { if (requiredTables.indexOf('post') != -1) {
tables.push({ tables.push({

Loading…
Cancel
Save