]> git.parisson.com Git - pdf.js.git/commitdiff
Rebuilding invalid OS/2 table
authorYury Delendik <ydelendik@mozilla.com>
Fri, 6 Apr 2012 22:52:57 +0000 (15:52 -0700)
committerYury Delendik <ydelendik@mozilla.com>
Fri, 6 Apr 2012 22:52:57 +0000 (15:52 -0700)
src/fonts.js
test/pdfs/issue1502.pdf.link [new file with mode: 0644]
test/test_manifest.json

index 7fdab8fbbf9f00740b40cd62b7f02bce75e07422..2757b88513a8b17c889f4ce3aa6da61a7edef6cf 100644 (file)
@@ -1727,6 +1727,16 @@ var Font = (function FontClosure() {
         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
       var requiredTables = ['OS/2', 'cmap', 'head', 'hhea',
                              'hmtx', 'maxp', 'name', 'post'];
@@ -1734,7 +1744,7 @@ var Font = (function FontClosure() {
       var header = readOpenTypeHeader(font);
       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 = [];
       for (var i = 0; i < numTables; i++) {
         var table = readTableEntry(font);
@@ -1752,6 +1762,8 @@ var Font = (function FontClosure() {
             hmtx = table;
           else if (table.tag == 'head')
             head = table;
+          else if (table.tag == 'OS/2')
+            os2 = table;
 
           requiredTables.splice(index, 1);
         } else {
@@ -1767,7 +1779,7 @@ var Font = (function FontClosure() {
         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
       // end of file. The virtualOffset represents where to put the actual
@@ -1781,21 +1793,10 @@ var Font = (function FontClosure() {
       // of missing tables
       createOpenTypeHeader(header.version, ttf, numTables);
 
-      if (requiredTables.indexOf('OS/2') != -1) {
-        // 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, null, override))
-        });
+      // Invalid OS/2 can break the font for the Windows
+      if (os2 && !isOS2Valid(os2)) {
+        tables.splice(tables.indexOf(os2), 1);
+        os2 = null;
       }
 
       // Ensure the [h/v]mtx tables contains the advance width and
@@ -2076,6 +2077,23 @@ var Font = (function FontClosure() {
       }
       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
       if (requiredTables.indexOf('post') != -1) {
         tables.push({
diff --git a/test/pdfs/issue1502.pdf.link b/test/pdfs/issue1502.pdf.link
new file mode 100644 (file)
index 0000000..37b4e80
--- /dev/null
@@ -0,0 +1 @@
+http://register.consilium.europa.eu/pdf/en/11/st12/st12196.en11.pdf
index 6a083bdf7a994e4931a747cce47fd21113011031..00ad7be119e5b3e3c03c9d500a954a5c962999b3 100644 (file)
       "link": true,
       "type": "eq"
     },
+    {  "id": "issue1502",
+      "file": "pdfs/issue1502.pdf",
+      "md5": "6f0569b7e85561ea784cb0403de89d84",
+      "rounds": 1,
+      "pageLimit": 5,
+      "link": true,
+      "type": "eq"
+    },
     {  "id": "issue1317",
       "file": "pdfs/issue1317.pdf",
       "md5": "6fb46275b30c48c8985617d4f86199e3",