From 0527b8bc8d4312c7aea4b48b246fef0ba5df5abb Mon Sep 17 00:00:00 2001
From: mduan <duan.mack@gmail.com>
Date: Fri, 18 Jan 2013 15:06:12 -0800
Subject: [PATCH] Fix invalid version number in truetype 'head' table

---
 src/fonts.js                 | 19 +++++++++++++++++++
 test/pdfs/issue2531.pdf.link |  1 +
 test/test_manifest.json      |  8 ++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 test/pdfs/issue2531.pdf.link

diff --git a/src/fonts.js b/src/fonts.js
index c70831bcb..97a296fbf 100644
--- a/src/fonts.js
+++ b/src/fonts.js
@@ -3254,6 +3254,21 @@ var Font = (function FontClosure() {
         return glyf.length;
       }
 
+      function sanitizeHead(head) {
+        var data = head.data;
+
+        // Validate version:
+        // Should always be 0x00010000
+        var version = int32([data[0], data[1], data[2], data[3]]);
+        if (version >> 16 !== 1) {
+          info('Invalid version in data type: ' + version);
+          data[0] = 0;
+          data[1] = 1;
+          data[2] = 0;
+          data[3] = 0;
+        }
+      }
+
       function sanitizeGlyphLocations(loca, glyf, numGlyphs,
                                       isGlyphLocationsLong) {
         var itemSize, itemDecode, itemEncode;
@@ -3678,6 +3693,10 @@ var Font = (function FontClosure() {
 
       sanitizeTTPrograms(fpgm, prep);
 
+      if (head) {
+        sanitizeHead(head);
+      }
+
       var isGlyphLocationsLong = int16([head.data[50], head.data[51]]);
       if (head && loca && glyf) {
         sanitizeGlyphLocations(loca, glyf, numGlyphs, isGlyphLocationsLong);
diff --git a/test/pdfs/issue2531.pdf.link b/test/pdfs/issue2531.pdf.link
new file mode 100644
index 000000000..70f40062c
--- /dev/null
+++ b/test/pdfs/issue2531.pdf.link
@@ -0,0 +1 @@
+http://www.rcjohnso.com/Looper/Looper.pdf
diff --git a/test/test_manifest.json b/test/test_manifest.json
index 9467dcb6b..f612cea6e 100644
--- a/test/test_manifest.json
+++ b/test/test_manifest.json
@@ -30,6 +30,14 @@
       "rounds": 1,
       "type": "eq"
     },
+    {  "id": "issue2531",
+      "file": "pdfs/issue2531.pdf",
+      "md5": "c58e6642d8a6e2ddd5e07a543ef8f30d",
+      "link": true,
+      "rounds": 1,
+      "pageLimit": 4,
+      "type": "eq"
+    },
     {  "id": "html5-canvas-cheat-sheet-load",
        "file": "pdfs/canvas.pdf",
        "md5": "59510028561daf62e00bf9f6f066b033",