From 9012fbd0105400995e0271c606bf2dc7ccb03cd1 Mon Sep 17 00:00:00 2001
From: Vivien Nicolas <21@vingtetun.org>
Date: Tue, 6 Sep 2011 15:11:07 +0200
Subject: [PATCH 1/4] Use sans-serif for font fallback by default

---
 pdf.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pdf.js b/pdf.js
index 35da1e332..4a7d0fe74 100644
--- a/pdf.js
+++ b/pdf.js
@@ -4824,7 +4824,7 @@ var CanvasGraphics = (function() {
                                    (fontObj.bold ? 'bold' : 'normal');
 
         var italic = fontObj.italic ? 'italic' : 'normal';
-        var rule = italic + ' ' + bold + ' ' + size + 'px "' + name + '"';
+        var rule = italic + ' ' + bold + ' ' + size + 'px "' + name + '", "sans-serif"';
         this.ctx.font = rule;
       }
     },

From b1a85c62295e0b35fa284a41ef6761d59074a8d5 Mon Sep 17 00:00:00 2001
From: Vivien Nicolas <21@vingtetun.org>
Date: Tue, 6 Sep 2011 15:12:33 +0200
Subject: [PATCH 2/4] Fix cmap encoding records to pass the sanitizer in the
 case of a duplicate platform: 1, encoding: 0

---
 fonts.js | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/fonts.js b/fonts.js
index 50eafc566..f53a2098b 100755
--- a/fonts.js
+++ b/fonts.js
@@ -857,9 +857,42 @@ var Font = (function Font() {
           });
         }
 
+        // Check that table are sorted by platformID then encodingID,
+        var tables = [records[0]];
+        for (var i = 1; i < numRecords; i++) {
+          // The sanitizer will drop the font if 2 tables have the same
+          // platformID and the same encodingID, this will be correct for
+          // most cases but if the font has been made for Mac it could
+          // exist a few platformID: 1, encodingID: 0 but with a different
+          // language field and that's correct. But the sanitizer does not
+          // seem to support this case.
+          var current = records[i];
+          var previous = records[i - 1];
+          if (((current.platformID << 16) + current.encodingID) <=
+             ((previous.platformID << 16) + previous.encodingID))
+                continue;
+          tables.push(current);
+        }
+
+        var missing = numRecords - tables.length;
+        if (numRecords - tables.length) {
+          numRecords = tables.length;
+          var data = string16(version) + string16(numRecords);
+
+          for (var i = 0; i < numRecords; i++) {
+            var table = tables[i];
+            data += string16(table.platformID) +
+                    string16(table.encodingID) +
+                    string32(table.offset);
+          }
+
+          for (var i = 0; i < data.length; i++)
+            cmap.data[i] = data.charCodeAt(i); 
+        }
+
         var encoding = properties.encoding;
         for (var i = 0; i < numRecords; i++) {
-          var table = records[i];
+          var table = tables[i];
           font.pos = start + table.offset;
 
           var format = int16(font.getBytes(2));

From 5e37bf7aeb82cbe227f50cfbfe964a1187546a53 Mon Sep 17 00:00:00 2001
From: Vivien Nicolas <21@vingtetun.org>
Date: Tue, 6 Sep 2011 17:26:48 +0200
Subject: [PATCH 3/4] Do not alter the encoding if no file is going to be
 altered

---
 pdf.js | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/pdf.js b/pdf.js
index 601518f57..e16567c21 100644
--- a/pdf.js
+++ b/pdf.js
@@ -4304,6 +4304,9 @@ var PartialEvaluator = (function() {
             var index = GlyphsUnicode[glyph] || i;
             glyphsMap[glyph] = encodingMap[i] = index;
 
+            if (!fontFile)
+              continue;
+
             if (index <= 0x1f || (index >= 127 && index <= 255))
               glyphsMap[glyph] = encodingMap[i] += kCmapGlyphOffset;
           }

From 0e062668e9252a30918ec80d55e33bb5be1d13df Mon Sep 17 00:00:00 2001
From: Vivien Nicolas <21@vingtetun.org>
Date: Wed, 7 Sep 2011 00:19:58 +0200
Subject: [PATCH 4/4] Fix review comments from #424

---
 fonts.js | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fonts.js b/fonts.js
index f53a2098b..8c4638a3b 100755
--- a/fonts.js
+++ b/fonts.js
@@ -858,6 +858,11 @@ var Font = (function Font() {
         }
 
         // Check that table are sorted by platformID then encodingID,
+        records.sort(function(a, b) {
+          return ((a.platformID << 16) + a.encodingID) -
+                 ((b.platformID << 16) + b.encodingID)
+        });
+
         var tables = [records[0]];
         for (var i = 1; i < numRecords; i++) {
           // The sanitizer will drop the font if 2 tables have the same
@@ -875,7 +880,7 @@ var Font = (function Font() {
         }
 
         var missing = numRecords - tables.length;
-        if (numRecords - tables.length) {
+        if (missing) {
           numRecords = tables.length;
           var data = string16(version) + string16(numRecords);