]> git.parisson.com Git - pdf.js.git/commitdiff
use Array.sort to calculate glyph ranges instead of large typed arrays
authorAndreas Gal <andreas.gal@gmail.com>
Mon, 20 Jun 2011 07:33:52 +0000 (03:33 -0400)
committerAndreas Gal <andreas.gal@gmail.com>
Mon, 20 Jun 2011 07:33:52 +0000 (03:33 -0400)
fonts.js

index 2ee8a0d3c5967c321b4ee0d62d5aecf0c16a0583..49a78cb53b3d41e352e82545df954e7a4ec90464 100644 (file)
--- a/fonts.js
+++ b/fonts.js
@@ -8,11 +8,6 @@
  */
 var kMaxFontFileSize = 40000;
 
-/**
- * Maximum number of glyphs per font.
-*/
-var kMaxGlyphsCount = 65526;
-
 /**
  * Maximum time to wait for a font to be loaded by @font-face
  */
@@ -310,24 +305,30 @@ var Font = (function () {
         aOffsets.currentOffset += tableEntry.length;
         aOffsets.virtualOffset += aData.length;
       }
-      
-      function createCMAPTable(aGlyphs) {
-        var characters = new Uint16Array(kMaxGlyphsCount);
-        for (var i = 0; i < aGlyphs.length; i++)
-          characters[aGlyphs[i].unicode] = i + 1;
 
-        // Separate the glyphs into continuous range of codes, aka segment.
+      function getRanges(glyphs) {
+        // Array.sort() sorts by characters, not numerically, so convert to an
+        // array of characters.
+        var codes = [];
+        var length = glyphs.length;
+        for (var n = 0; n < length; ++n)
+          codes.push(String.fromCharCode(glyphs[n].unicode))
+        codes.sort();
+
         var ranges = [];
-        var range = [];
-        var count = characters.length;
-        for (var i = 0; i < count; i++) {
-          if (characters[i]) {
-            range.push(i);
-          } else if (range.length) {
-            ranges.push(range.slice());
-            range = [];
-          }
+        for (var n = 0; n < length; ) {
+          var range = [];
+          do {
+            var current = codes[n++].charCodeAt(0);
+            range.push(current);
+          } while (n < length && current + 1 == codes[n].charCodeAt(0));
+          ranges.push(range);
         }
+        return ranges;
+      }
+
+      function createCMAPTable(aGlyphs) {
+        var ranges = getRanges(aGlyphs);
 
         // The size in bytes of the header is equal to the size of the
         // different fields * length of a short + (size of the 4 parallels arrays