[Pkg-javascript-commits] [pdf.js] 280/414: Refactors CMapFactory.create to make it async

David Prévot taffit at moszumanska.debian.org
Tue Jun 28 17:12:30 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository pdf.js.

commit f6d28ca32355f37a643a06d453bed74a21deb21a
Author: Manas <prometheansacrifice at gmail.com>
Date:   Sun Feb 28 21:50:29 2016 +0530

    Refactors CMapFactory.create to make it async
---
 src/core/cmap.js            | 394 ++++++++++++++++++++++----------------------
 src/core/evaluator.js       | 263 +++++++++++++++++++++++------
 src/core/fonts.js           | 134 +--------------
 test/font/font_fpgm_spec.js |  25 +--
 test/font/font_os2_spec.js  |  28 ++--
 test/font/font_post_spec.js |  31 ++--
 test/font/font_test.html    |   1 +
 test/unit/cmap_spec.js      | 156 +++++++++++-------
 8 files changed, 559 insertions(+), 473 deletions(-)

diff --git a/src/core/cmap.js b/src/core/cmap.js
index f68fa0a..e5e23eb 100644
--- a/src/core/cmap.js
+++ b/src/core/cmap.js
@@ -424,31 +424,22 @@ var IdentityCMap = (function IdentityCMapClosure() {
 
 var BinaryCMapReader = (function BinaryCMapReaderClosure() {
   function fetchBinaryData(url) {
-    var nonBinaryRequest = PDFJS.disableWorker;
-    var request = new XMLHttpRequest();
-    request.open('GET', url, false);
-    if (!nonBinaryRequest) {
-      try {
-        request.responseType = 'arraybuffer';
-        nonBinaryRequest = request.responseType !== 'arraybuffer';
-      } catch (e) {
-        nonBinaryRequest = true;
-      }
-    }
-    if (nonBinaryRequest && request.overrideMimeType) {
-      request.overrideMimeType('text/plain; charset=x-user-defined');
-    }
-    request.send(null);
-    if (nonBinaryRequest ? !request.responseText : !request.response) {
-      error('Unable to get binary cMap at: ' + url);
-    }
-    if (nonBinaryRequest) {
-      var data = Array.prototype.map.call(request.responseText, function (ch) {
-        return ch.charCodeAt(0) & 255;
-      });
-      return new Uint8Array(data);
-    }
-    return new Uint8Array(request.response);
+    return new Promise(function (resolve, reject) {
+      var request = new XMLHttpRequest();
+      request.open('GET', url, true);
+      request.responseType = 'arraybuffer';
+      request.onreadystatechange = function () {
+        if (request.readyState === XMLHttpRequest.DONE) {
+          if (!request.response || request.status !== 200 &&
+              request.status !== 0) {
+            reject(new Error('Unable to get binary cMap at: ' + url));
+          } else {
+            resolve(new Uint8Array(request.response));
+          }
+        }
+      };
+      request.send(null);
+    });
   }
 
   function hexToInt(a, size) {
@@ -571,163 +562,163 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
   };
 
   function processBinaryCMap(url, cMap, extend) {
-    var data = fetchBinaryData(url);
-    var stream = new BinaryCMapStream(data);
-
-    var header = stream.readByte();
-    cMap.vertical = !!(header & 1);
-
-    var useCMap = null;
-    var start = new Uint8Array(MAX_NUM_SIZE);
-    var end = new Uint8Array(MAX_NUM_SIZE);
-    var char = new Uint8Array(MAX_NUM_SIZE);
-    var charCode = new Uint8Array(MAX_NUM_SIZE);
-    var tmp = new Uint8Array(MAX_NUM_SIZE);
-    var code;
-
-    var b;
-    while ((b = stream.readByte()) >= 0) {
-      var type = b >> 5;
-      if (type === 7) { // metadata, e.g. comment or usecmap
-        switch (b & 0x1F) {
-          case 0:
-            stream.readString(); // skipping comment
-            break;
-          case 1:
-            useCMap = stream.readString();
-            break;
+    return fetchBinaryData(url).then(function (data) {
+      var stream = new BinaryCMapStream(data);
+      var header = stream.readByte();
+      cMap.vertical = !!(header & 1);
+
+      var useCMap = null;
+      var start = new Uint8Array(MAX_NUM_SIZE);
+      var end = new Uint8Array(MAX_NUM_SIZE);
+      var char = new Uint8Array(MAX_NUM_SIZE);
+      var charCode = new Uint8Array(MAX_NUM_SIZE);
+      var tmp = new Uint8Array(MAX_NUM_SIZE);
+      var code;
+
+      var b;
+      while ((b = stream.readByte()) >= 0) {
+        var type = b >> 5;
+        if (type === 7) { // metadata, e.g. comment or usecmap
+          switch (b & 0x1F) {
+            case 0:
+              stream.readString(); // skipping comment
+              break;
+            case 1:
+              useCMap = stream.readString();
+              break;
+          }
+          continue;
         }
-        continue;
-      }
-      var sequence = !!(b & 0x10);
-      var dataSize = b & 15;
+        var sequence = !!(b & 0x10);
+        var dataSize = b & 15;
 
-      assert(dataSize + 1 <= MAX_NUM_SIZE);
+        assert(dataSize + 1 <= MAX_NUM_SIZE);
 
-      var ucs2DataSize = 1;
-      var subitemsCount = stream.readNumber();
-      var i;
-      switch (type) {
-        case 0: // codespacerange
-          stream.readHex(start, dataSize);
-          stream.readHexNumber(end, dataSize);
-          addHex(end, start, dataSize);
-          cMap.addCodespaceRange(dataSize + 1, hexToInt(start, dataSize),
-                                 hexToInt(end, dataSize));
-          for (i = 1; i < subitemsCount; i++) {
-            incHex(end, dataSize);
-            stream.readHexNumber(start, dataSize);
-            addHex(start, end, dataSize);
+        var ucs2DataSize = 1;
+        var subitemsCount = stream.readNumber();
+        var i;
+        switch (type) {
+          case 0: // codespacerange
+            stream.readHex(start, dataSize);
             stream.readHexNumber(end, dataSize);
             addHex(end, start, dataSize);
             cMap.addCodespaceRange(dataSize + 1, hexToInt(start, dataSize),
                                    hexToInt(end, dataSize));
-          }
-          break;
-        case 1: // notdefrange
-          stream.readHex(start, dataSize);
-          stream.readHexNumber(end, dataSize);
-          addHex(end, start, dataSize);
-          code = stream.readNumber();
-          // undefined range, skipping
-          for (i = 1; i < subitemsCount; i++) {
-            incHex(end, dataSize);
-            stream.readHexNumber(start, dataSize);
-            addHex(start, end, dataSize);
+            for (i = 1; i < subitemsCount; i++) {
+              incHex(end, dataSize);
+              stream.readHexNumber(start, dataSize);
+              addHex(start, end, dataSize);
+              stream.readHexNumber(end, dataSize);
+              addHex(end, start, dataSize);
+              cMap.addCodespaceRange(dataSize + 1, hexToInt(start, dataSize),
+                                     hexToInt(end, dataSize));
+            }
+            break;
+          case 1: // notdefrange
+            stream.readHex(start, dataSize);
             stream.readHexNumber(end, dataSize);
             addHex(end, start, dataSize);
             code = stream.readNumber();
-            // nop
-          }
-          break;
-        case 2: // cidchar
-          stream.readHex(char, dataSize);
-          code = stream.readNumber();
-          cMap.mapOne(hexToInt(char, dataSize), code);
-          for (i = 1; i < subitemsCount; i++) {
-            incHex(char, dataSize);
-            if (!sequence) {
-              stream.readHexNumber(tmp, dataSize);
-              addHex(char, tmp, dataSize);
-            }
-            code = stream.readSigned() + (code + 1);
-            cMap.mapOne(hexToInt(char, dataSize), code);
-          }
-          break;
-        case 3: // cidrange
-          stream.readHex(start, dataSize);
-          stream.readHexNumber(end, dataSize);
-          addHex(end, start, dataSize);
-          code = stream.readNumber();
-          cMap.mapCidRange(hexToInt(start, dataSize), hexToInt(end, dataSize),
-                           code);
-          for (i = 1; i < subitemsCount; i++) {
-            incHex(end, dataSize);
-            if (!sequence) {
+            // undefined range, skipping
+            for (i = 1; i < subitemsCount; i++) {
+              incHex(end, dataSize);
               stream.readHexNumber(start, dataSize);
               addHex(start, end, dataSize);
-            } else {
-              start.set(end);
+              stream.readHexNumber(end, dataSize);
+              addHex(end, start, dataSize);
+              code = stream.readNumber();
+              // nop
             }
+            break;
+          case 2: // cidchar
+            stream.readHex(char, dataSize);
+            code = stream.readNumber();
+            cMap.mapOne(hexToInt(char, dataSize), code);
+            for (i = 1; i < subitemsCount; i++) {
+              incHex(char, dataSize);
+              if (!sequence) {
+                stream.readHexNumber(tmp, dataSize);
+                addHex(char, tmp, dataSize);
+              }
+              code = stream.readSigned() + (code + 1);
+              cMap.mapOne(hexToInt(char, dataSize), code);
+            }
+            break;
+          case 3: // cidrange
+            stream.readHex(start, dataSize);
             stream.readHexNumber(end, dataSize);
             addHex(end, start, dataSize);
             code = stream.readNumber();
             cMap.mapCidRange(hexToInt(start, dataSize), hexToInt(end, dataSize),
                              code);
-          }
-          break;
-        case 4: // bfchar
-          stream.readHex(char, ucs2DataSize);
-          stream.readHex(charCode, dataSize);
-          cMap.mapOne(hexToInt(char, ucs2DataSize),
-                      hexToStr(charCode, dataSize));
-          for (i = 1; i < subitemsCount; i++) {
-            incHex(char, ucs2DataSize);
-            if (!sequence) {
-              stream.readHexNumber(tmp, ucs2DataSize);
-              addHex(char, tmp, ucs2DataSize);
+            for (i = 1; i < subitemsCount; i++) {
+              incHex(end, dataSize);
+              if (!sequence) {
+                stream.readHexNumber(start, dataSize);
+                addHex(start, end, dataSize);
+              } else {
+                start.set(end);
+              }
+              stream.readHexNumber(end, dataSize);
+              addHex(end, start, dataSize);
+              code = stream.readNumber();
+              cMap.mapCidRange(hexToInt(start, dataSize),
+                               hexToInt(end, dataSize), code);
             }
-            incHex(charCode, dataSize);
-            stream.readHexSigned(tmp, dataSize);
-            addHex(charCode, tmp, dataSize);
+            break;
+          case 4: // bfchar
+            stream.readHex(char, ucs2DataSize);
+            stream.readHex(charCode, dataSize);
             cMap.mapOne(hexToInt(char, ucs2DataSize),
                         hexToStr(charCode, dataSize));
-          }
-          break;
-        case 5: // bfrange
-          stream.readHex(start, ucs2DataSize);
-          stream.readHexNumber(end, ucs2DataSize);
-          addHex(end, start, ucs2DataSize);
-          stream.readHex(charCode, dataSize);
-          cMap.mapBfRange(hexToInt(start, ucs2DataSize),
-                          hexToInt(end, ucs2DataSize),
+            for (i = 1; i < subitemsCount; i++) {
+              incHex(char, ucs2DataSize);
+              if (!sequence) {
+                stream.readHexNumber(tmp, ucs2DataSize);
+                addHex(char, tmp, ucs2DataSize);
+              }
+              incHex(charCode, dataSize);
+              stream.readHexSigned(tmp, dataSize);
+              addHex(charCode, tmp, dataSize);
+              cMap.mapOne(hexToInt(char, ucs2DataSize),
                           hexToStr(charCode, dataSize));
-          for (i = 1; i < subitemsCount; i++) {
-            incHex(end, ucs2DataSize);
-            if (!sequence) {
-              stream.readHexNumber(start, ucs2DataSize);
-              addHex(start, end, ucs2DataSize);
-            } else {
-              start.set(end);
             }
+            break;
+          case 5: // bfrange
+            stream.readHex(start, ucs2DataSize);
             stream.readHexNumber(end, ucs2DataSize);
             addHex(end, start, ucs2DataSize);
             stream.readHex(charCode, dataSize);
             cMap.mapBfRange(hexToInt(start, ucs2DataSize),
                             hexToInt(end, ucs2DataSize),
                             hexToStr(charCode, dataSize));
-          }
-          break;
-        default:
-          error('Unknown type: ' + type);
-          break;
+            for (i = 1; i < subitemsCount; i++) {
+              incHex(end, ucs2DataSize);
+              if (!sequence) {
+                stream.readHexNumber(start, ucs2DataSize);
+                addHex(start, end, ucs2DataSize);
+              } else {
+                start.set(end);
+              }
+              stream.readHexNumber(end, ucs2DataSize);
+              addHex(end, start, ucs2DataSize);
+              stream.readHex(charCode, dataSize);
+              cMap.mapBfRange(hexToInt(start, ucs2DataSize),
+                              hexToInt(end, ucs2DataSize),
+                              hexToStr(charCode, dataSize));
+            }
+            break;
+          default:
+            error('Unknown type: ' + type);
+            break;
+        }
       }
-    }
 
-    if (useCMap) {
-      extend(useCMap);
-    }
-    return cMap;
+      if (useCMap) {
+        return extend(useCMap);
+      }
+      return cMap;
+    });
   }
 
   function BinaryCMapReader() {}
@@ -936,47 +927,53 @@ var CMapFactory = (function CMapFactoryClosure() {
       useCMap = embededUseCMap;
     }
     if (useCMap) {
-      extendCMap(cMap, builtInCMapParams, useCMap);
+      return extendCMap(cMap, builtInCMapParams, useCMap);
+    } else {
+      return Promise.resolve(cMap);
     }
   }
 
   function extendCMap(cMap, builtInCMapParams, useCMap) {
-    cMap.useCMap = createBuiltInCMap(useCMap, builtInCMapParams);
-    // If there aren't any code space ranges defined clone all the parent ones
-    // into this cMap.
-    if (cMap.numCodespaceRanges === 0) {
-      var useCodespaceRanges = cMap.useCMap.codespaceRanges;
-      for (var i = 0; i < useCodespaceRanges.length; i++) {
-        cMap.codespaceRanges[i] = useCodespaceRanges[i].slice();
-      }
-      cMap.numCodespaceRanges = cMap.useCMap.numCodespaceRanges;
-    }
-    // Merge the map into the current one, making sure not to override
-    // any previously defined entries.
-    cMap.useCMap.forEach(function(key, value) {
-      if (!cMap.contains(key)) {
-        cMap.mapOne(key, cMap.useCMap.lookup(key));
+    return createBuiltInCMap(useCMap, builtInCMapParams).then(
+        function(newCMap) {
+      cMap.useCMap = newCMap;
+      // If there aren't any code space ranges defined clone all the parent ones
+      // into this cMap.
+      if (cMap.numCodespaceRanges === 0) {
+        var useCodespaceRanges = cMap.useCMap.codespaceRanges;
+        for (var i = 0; i < useCodespaceRanges.length; i++) {
+          cMap.codespaceRanges[i] = useCodespaceRanges[i].slice();
+        }
+        cMap.numCodespaceRanges = cMap.useCMap.numCodespaceRanges;
       }
+      // Merge the map into the current one, making sure not to override
+      // any previously defined entries.
+      cMap.useCMap.forEach(function(key, value) {
+        if (!cMap.contains(key)) {
+          cMap.mapOne(key, cMap.useCMap.lookup(key));
+        }
+      });
+
+      return cMap;
     });
   }
 
   function parseBinaryCMap(name, builtInCMapParams) {
     var url = builtInCMapParams.url + name + '.bcmap';
     var cMap = new CMap(true);
-    new BinaryCMapReader().read(url, cMap, function (useCMap) {
-      extendCMap(cMap, builtInCMapParams, useCMap);
+    return new BinaryCMapReader().read(url, cMap, function (useCMap) {
+      return extendCMap(cMap, builtInCMapParams, useCMap);
     });
-    return cMap;
   }
 
   function createBuiltInCMap(name, builtInCMapParams) {
     if (name === 'Identity-H') {
-      return new IdentityCMap(false, 2);
+      return Promise.resolve(new IdentityCMap(false, 2));
     } else if (name === 'Identity-V') {
-      return new IdentityCMap(true, 2);
+      return Promise.resolve(new IdentityCMap(true, 2));
     }
     if (BUILT_IN_CMAPS.indexOf(name) === -1) {
-      error('Unknown cMap name: ' + name);
+      return Promise.reject(new Error('Unknown cMap name: ' + name));
     }
     assert(builtInCMapParams, 'built-in cMap parameters are not provided');
 
@@ -984,17 +981,28 @@ var CMapFactory = (function CMapFactoryClosure() {
       return parseBinaryCMap(name, builtInCMapParams);
     }
 
-    var request = new XMLHttpRequest();
-    var url = builtInCMapParams.url + name;
-    request.open('GET', url, false);
-    request.send(null);
-    if (!request.responseText) {
-      error('Unable to get cMap at: ' + url);
-    }
-    var cMap = new CMap(true);
-    var lexer = new Lexer(new StringStream(request.responseText));
-    parseCMap(cMap, lexer, builtInCMapParams, null);
-    return cMap;
+    return new Promise(function (resolve, reject) {
+      var url = builtInCMapParams.url + name;
+      var request = new XMLHttpRequest();
+      request.onreadystatechange = function () {
+        if (request.readyState === XMLHttpRequest.DONE) {
+          if (request.status === 200 || request.status === 0) {
+            var cMap = new CMap(true);
+            var lexer = new Lexer(new StringStream(request.responseText));
+            parseCMap(cMap, lexer, builtInCMapParams, null).then(
+                function (parsedCMap) {
+              resolve(parsedCMap);
+            }).catch(function (e) {
+              reject(new Error({ message: 'Invalid CMap data', error: e }));
+            });
+          } else {
+            reject(new Error('Unable to get cMap at: ' + url));
+          }
+        }
+      };
+      request.open('GET', url, true);
+      request.send(null);
+    });
   }
 
   return {
@@ -1004,17 +1012,15 @@ var CMapFactory = (function CMapFactoryClosure() {
       } else if (isStream(encoding)) {
         var cMap = new CMap();
         var lexer = new Lexer(encoding);
-        try {
-          parseCMap(cMap, lexer, builtInCMapParams, useCMap);
-        } catch (e) {
-          warn('Invalid CMap data. ' + e);
-        }
-        if (cMap.isIdentityCMap) {
-          return createBuiltInCMap(cMap.name, builtInCMapParams);
-        }
-        return cMap;
+        return parseCMap(cMap, lexer, builtInCMapParams, useCMap).then(
+            function (parsedCMap) {
+          if (parsedCMap.isIdentityCMap) {
+            return createBuiltInCMap(parsedCMap.name, builtInCMapParams);
+          }
+          return parsedCMap;
+        });
       }
-      error('Encoding required.');
+      return Promise.reject(new Error('Encoding required.'));
     }
   };
 })();
diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index df4fa09..9058d70 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -24,7 +24,7 @@
       'pdfjs/core/fonts', 'pdfjs/core/function', 'pdfjs/core/pattern',
       'pdfjs/core/cmap', 'pdfjs/core/metrics', 'pdfjs/core/bidi',
       'pdfjs/core/encodings', 'pdfjs/core/standard_fonts',
-      'pdfjs/core/unicode'], factory);
+      'pdfjs/core/unicode', 'pdfjs/core/glyphlist'], factory);
   } else if (typeof exports !== 'undefined') {
     factory(exports, require('../shared/util.js'), require('./primitives.js'),
       require('./stream.js'), require('./parser.js'), require('./image.js'),
@@ -32,7 +32,7 @@
       require('./fonts.js'), require('./function.js'), require('./pattern.js'),
       require('./cmap.js'), require('./metrics.js'), require('./bidi.js'),
       require('./encodings.js'), require('./standard_fonts.js'),
-      require('./unicode.js'));
+      require('./unicode.js'), require('./glyphlist.js'));
   } else {
     factory((root.pdfjsCoreEvaluator = {}), root.pdfjsSharedUtil,
       root.pdfjsCorePrimitives, root.pdfjsCoreStream, root.pdfjsCoreParser,
@@ -40,12 +40,13 @@
       root.pdfjsCoreFonts, root.pdfjsCoreFunction, root.pdfjsCorePattern,
       root.pdfjsCoreCMap, root.pdfjsCoreMetrics, root.pdfjsCoreBidi,
       root.pdfjsCoreEncodings, root.pdfjsCoreStandardFonts,
-      root.pdfjsCoreUnicode);
+      root.pdfjsCoreUnicode, root.pdfjsCoreGlyphList);
   }
 }(this, function (exports, sharedUtil, corePrimitives, coreStream, coreParser,
                   coreImage, coreColorSpace, coreMurmurHash3, coreFonts,
                   coreFunction, corePattern, coreCMap, coreMetrics, coreBidi,
-                  coreEncodings, coreStandardFonts, coreUnicode) {
+                  coreEncodings, coreStandardFonts, coreUnicode,
+                  coreGlyphList) {
 
 var FONT_IDENTITY_MATRIX = sharedUtil.FONT_IDENTITY_MATRIX;
 var IDENTITY_MATRIX = sharedUtil.IDENTITY_MATRIX;
@@ -103,6 +104,8 @@ var getSerifFonts = coreStandardFonts.getSerifFonts;
 var getSymbolsFonts = coreStandardFonts.getSymbolsFonts;
 var getNormalizedUnicodes = coreUnicode.getNormalizedUnicodes;
 var reverseIfRtl = coreUnicode.reverseIfRtl;
+var getUnicodeForGlyph = coreUnicode.getUnicodeForGlyph;
+var getGlyphsUnicode = coreGlyphList.getGlyphsUnicode;
 
 var PartialEvaluator = (function PartialEvaluatorClosure() {
   function PartialEvaluator(pdfManager, xref, handler, pageIndex,
@@ -651,8 +654,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
       // TODO move promises into translate font
       var translatedPromise;
       try {
-        translatedPromise = Promise.resolve(
-          this.translateFont(preEvaluatedFont, xref));
+        translatedPromise = this.translateFont(preEvaluatedFont, xref);
       } catch (e) {
         translatedPromise = Promise.reject(e);
       }
@@ -1550,9 +1552,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
                                             xref, properties) {
       // 9.10.2
       var toUnicode = (dict.get('ToUnicode') || baseDict.get('ToUnicode'));
-      if (toUnicode) {
-        properties.toUnicode = this.readToUnicode(toUnicode);
-      }
+      var toUnicodePromise = toUnicode ?
+            this.readToUnicode(toUnicode) : Promise.resolve(undefined);
+
       if (properties.composite) {
         // CIDSystemInfo helps to match CID to glyphs
         var cidSystemInfo = dict.get('CIDSystemInfo');
@@ -1637,44 +1639,189 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
       properties.differences = differences;
       properties.baseEncodingName = baseEncodingName;
       properties.dict = dict;
+      return toUnicodePromise.then(function(toUnicode) {
+        properties.toUnicode = toUnicode;
+        return this.buildToUnicode(properties);
+      }.bind(this)).then(function (toUnicode) {
+        properties.toUnicode = toUnicode;
+        return properties;
+      });
+    },
+
+    /**
+     * Builds a char code to unicode map based on section 9.10 of the spec.
+     * @param {Object} properties Font properties object.
+     * @return {Promise} A Promise resolving to ToUnicodeMap object.
+     */
+    buildToUnicode: function partialEvaluator_buildToUnicode(properties) {
+      // Section 9.10.2 Mapping Character Codes to Unicode Values
+      if (properties.toUnicode && properties.toUnicode.length !== 0) {
+        return Promise.resolve(properties.toUnicode);
+      }
+      // According to the spec if the font is a simple font we should only map
+      // to unicode if the base encoding is MacRoman, MacExpert, or WinAnsi or
+      // the differences array only contains adobe standard or symbol set names,
+      // in pratice it seems better to always try to create a toUnicode
+      // map based of the default encoding.
+      var toUnicode, charcode;
+      if (!properties.composite /* is simple font */) {
+        toUnicode = [];
+        var encoding = properties.defaultEncoding.slice();
+        var baseEncodingName = properties.baseEncodingName;
+        // Merge in the differences array.
+        var differences = properties.differences;
+        for (charcode in differences) {
+          encoding[charcode] = differences[charcode];
+        }
+        var glyphsUnicodeMap = getGlyphsUnicode();
+        for (charcode in encoding) {
+          // a) Map the character code to a character name.
+          var glyphName = encoding[charcode];
+          // b) Look up the character name in the Adobe Glyph List (see the
+          //    Bibliography) to obtain the corresponding Unicode value.
+          if (glyphName === '') {
+            continue;
+          } else if (glyphsUnicodeMap[glyphName] === undefined) {
+            // (undocumented) c) Few heuristics to recognize unknown glyphs
+            // NOTE: Adobe Reader does not do this step, but OSX Preview does
+            var code = 0;
+            switch (glyphName[0]) {
+              case 'G': // Gxx glyph
+                if (glyphName.length === 3) {
+                  code = parseInt(glyphName.substr(1), 16);
+                }
+                break;
+              case 'g': // g00xx glyph
+                if (glyphName.length === 5) {
+                  code = parseInt(glyphName.substr(1), 16);
+                }
+                break;
+              case 'C': // Cddd glyph
+              case 'c': // cddd glyph
+                if (glyphName.length >= 3) {
+                  code = +glyphName.substr(1);
+                }
+                break;
+              default:
+                // 'uniXXXX'/'uXXXX{XX}' glyphs
+                var unicode = getUnicodeForGlyph(glyphName, glyphsUnicodeMap);
+                if (unicode !== -1) {
+                  code = unicode;
+                }
+            }
+            if (code) {
+              // If |baseEncodingName| is one the predefined encodings,
+              // and |code| equals |charcode|, using the glyph defined in the
+              // baseEncoding seems to yield a better |toUnicode| mapping
+              // (fixes issue 5070).
+              if (baseEncodingName && code === +charcode) {
+                var baseEncoding = getEncoding(baseEncodingName);
+                if (baseEncoding && (glyphName = baseEncoding[charcode])) {
+                  toUnicode[charcode] =
+                    String.fromCharCode(glyphsUnicodeMap[glyphName]);
+                  continue;
+                }
+              }
+              toUnicode[charcode] = String.fromCharCode(code);
+            }
+            continue;
+          }
+          toUnicode[charcode] =
+            String.fromCharCode(glyphsUnicodeMap[glyphName]);
+        }
+        return Promise.resolve(new ToUnicodeMap(toUnicode));
+      }
+      // If the font is a composite font that uses one of the predefined CMaps
+      // listed in Table 118 (except Identity–H and Identity–V) or whose
+      // descendant CIDFont uses the Adobe-GB1, Adobe-CNS1, Adobe-Japan1, or
+      // Adobe-Korea1 character collection:
+      if (properties.composite && (
+           (properties.cMap.builtInCMap &&
+            !(properties.cMap instanceof IdentityCMap)) ||
+           (properties.cidSystemInfo.registry === 'Adobe' &&
+             (properties.cidSystemInfo.ordering === 'GB1' ||
+              properties.cidSystemInfo.ordering === 'CNS1' ||
+              properties.cidSystemInfo.ordering === 'Japan1' ||
+              properties.cidSystemInfo.ordering === 'Korea1')))) {
+        // Then:
+        // a) Map the character code to a character identifier (CID) according
+        // to the font’s CMap.
+        // b) Obtain the registry and ordering of the character collection used
+        // by the font’s CMap (for example, Adobe and Japan1) from its
+        // CIDSystemInfo dictionary.
+        var registry = properties.cidSystemInfo.registry;
+        var ordering = properties.cidSystemInfo.ordering;
+        // c) Construct a second CMap name by concatenating the registry and
+        // ordering obtained in step (b) in the format registry–ordering–UCS2
+        // (for example, Adobe–Japan1–UCS2).
+        var ucs2CMapName = new Name(registry + '-' + ordering + '-UCS2');
+        // d) Obtain the CMap with the name constructed in step (c) (available
+        // from the ASN Web site; see the Bibliography).
+        return CMapFactory.create(ucs2CMapName,
+          { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null).then(
+            function (ucs2CMap) {
+          var cMap = properties.cMap;
+          toUnicode = [];
+          cMap.forEach(function(charcode, cid) {
+            assert(cid <= 0xffff, 'Max size of CID is 65,535');
+            // e) Map the CID obtained in step (a) according to the CMap
+            // obtained in step (d), producing a Unicode value.
+            var ucs2 = ucs2CMap.lookup(cid);
+            if (ucs2) {
+              toUnicode[charcode] =
+                String.fromCharCode((ucs2.charCodeAt(0) << 8) +
+                                    ucs2.charCodeAt(1));
+            }
+          });
+          return new ToUnicodeMap(toUnicode);
+        });
+      }
+
+      // The viewer's choice, just use an identity map.
+      return Promise.resolve(new IdentityToUnicodeMap(properties.firstChar,
+                                                      properties.lastChar));
     },
 
     readToUnicode: function PartialEvaluator_readToUnicode(toUnicode) {
-      var cmap, cmapObj = toUnicode;
+      var cmapObj = toUnicode;
       if (isName(cmapObj)) {
-        cmap = CMapFactory.create(cmapObj,
-          { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
-        if (cmap instanceof IdentityCMap) {
-          return new IdentityToUnicodeMap(0, 0xFFFF);
-        }
-        return new ToUnicodeMap(cmap.getMap());
+        return CMapFactory.create(cmapObj,
+          { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null).then(
+            function (cmap) {
+          if (cmap instanceof IdentityCMap) {
+            return new IdentityToUnicodeMap(0, 0xFFFF);
+          }
+          return new ToUnicodeMap(cmap.getMap());
+        });
       } else if (isStream(cmapObj)) {
-        cmap = CMapFactory.create(cmapObj,
-          { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
-        if (cmap instanceof IdentityCMap) {
-          return new IdentityToUnicodeMap(0, 0xFFFF);
-        }
-        var map = new Array(cmap.length);
-        // Convert UTF-16BE
-        // NOTE: cmap can be a sparse array, so use forEach instead of for(;;)
-        // to iterate over all keys.
-        cmap.forEach(function(charCode, token) {
-          var str = [];
-          for (var k = 0; k < token.length; k += 2) {
-            var w1 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
-            if ((w1 & 0xF800) !== 0xD800) { // w1 < 0xD800 || w1 > 0xDFFF
-              str.push(w1);
-              continue;
-            }
-            k += 2;
-            var w2 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
-            str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000);
+        return CMapFactory.create(cmapObj,
+          { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null).then(
+            function (cmap) {
+          if (cmap instanceof IdentityCMap) {
+            return new IdentityToUnicodeMap(0, 0xFFFF);
           }
-          map[charCode] = String.fromCharCode.apply(String, str);
+          var map = new Array(cmap.length);
+          // Convert UTF-16BE
+          // NOTE: cmap can be a sparse array, so use forEach instead of for(;;)
+          // to iterate over all keys.
+          cmap.forEach(function(charCode, token) {
+            var str = [];
+            for (var k = 0; k < token.length; k += 2) {
+              var w1 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
+              if ((w1 & 0xF800) !== 0xD800) { // w1 < 0xD800 || w1 > 0xDFFF
+                str.push(w1);
+                continue;
+              }
+              k += 2;
+              var w2 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
+              str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000);
+            }
+            map[charCode] = String.fromCharCode.apply(String, str);
+          });
+          return new ToUnicodeMap(map);
         });
-        return new ToUnicodeMap(map);
       }
-      return null;
+      return Promise.resolve(null);
     },
 
     readCidToGidMap: function PartialEvaluator_readCidToGidMap(cidToGidStream) {
@@ -1978,10 +2125,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
             firstChar: 0,
             lastChar: maxCharIndex
           };
-          this.extractDataStructures(dict, dict, xref, properties);
-          properties.widths = this.buildCharCodeToWidth(metrics.widths,
-                                                        properties);
-          return new Font(baseFontName, null, properties);
+          return this.extractDataStructures(dict, dict, xref, properties).then(
+              function (properties) {
+            properties.widths = this.buildCharCodeToWidth(metrics.widths,
+                                                          properties);
+            return new Font(baseFontName, null, properties);
+          }.bind(this));
         }
       }
 
@@ -2058,23 +2207,33 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
         coded: false
       };
 
+      var cMapPromise;
       if (composite) {
         var cidEncoding = baseDict.get('Encoding');
         if (isName(cidEncoding)) {
           properties.cidEncoding = cidEncoding.name;
         }
-        properties.cMap = CMapFactory.create(cidEncoding,
-          { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
-        properties.vertical = properties.cMap.vertical;
+        cMapPromise = CMapFactory.create(cidEncoding,
+          { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null).then(
+            function (cMap) {
+          properties.cMap = cMap;
+          properties.vertical = properties.cMap.vertical;
+        });
+      } else {
+        cMapPromise = Promise.resolve(undefined);
       }
-      this.extractDataStructures(dict, baseDict, xref, properties);
-      this.extractWidths(dict, xref, descriptor, properties);
 
-      if (type === 'Type3') {
-        properties.isType3Font = true;
-      }
+      return cMapPromise.then(function () {
+        return this.extractDataStructures(dict, baseDict, xref, properties);
+      }.bind(this)).then(function (properties) {
+        this.extractWidths(dict, xref, descriptor, properties);
+
+        if (type === 'Type3') {
+          properties.isType3Font = true;
+        }
 
-      return new Font(fontName.name, fontFile, properties);
+        return new Font(fontName.name, fontFile, properties);
+      }.bind(this));
     }
   };
 
diff --git a/src/core/fonts.js b/src/core/fonts.js
index 5944325..eb122c7 100644
--- a/src/core/fonts.js
+++ b/src/core/fonts.js
@@ -501,7 +501,7 @@ var Font = (function FontClosure() {
     this.fontMatrix = properties.fontMatrix;
     this.bbox = properties.bbox;
 
-    this.toUnicode = properties.toUnicode = this.buildToUnicode(properties);
+    this.toUnicode = properties.toUnicode;
 
     this.toFontChar = [];
 
@@ -2665,138 +2665,6 @@ var Font = (function FontClosure() {
       return builder.toArray();
     },
 
-    /**
-     * Builds a char code to unicode map based on section 9.10 of the spec.
-     * @param {Object} properties Font properties object.
-     * @return {Object} A ToUnicodeMap object.
-     */
-    buildToUnicode: function Font_buildToUnicode(properties) {
-      // Section 9.10.2 Mapping Character Codes to Unicode Values
-      if (properties.toUnicode && properties.toUnicode.length !== 0) {
-        return properties.toUnicode;
-      }
-      // According to the spec if the font is a simple font we should only map
-      // to unicode if the base encoding is MacRoman, MacExpert, or WinAnsi or
-      // the differences array only contains adobe standard or symbol set names,
-      // in pratice it seems better to always try to create a toUnicode
-      // map based of the default encoding.
-      var toUnicode, charcode;
-      if (!properties.composite /* is simple font */) {
-        toUnicode = [];
-        var encoding = properties.defaultEncoding.slice();
-        var baseEncodingName = properties.baseEncodingName;
-        // Merge in the differences array.
-        var differences = properties.differences;
-        for (charcode in differences) {
-          encoding[charcode] = differences[charcode];
-        }
-        var glyphsUnicodeMap = getGlyphsUnicode();
-        for (charcode in encoding) {
-          // a) Map the character code to a character name.
-          var glyphName = encoding[charcode];
-          // b) Look up the character name in the Adobe Glyph List (see the
-          //    Bibliography) to obtain the corresponding Unicode value.
-          if (glyphName === '') {
-            continue;
-          } else if (glyphsUnicodeMap[glyphName] === undefined) {
-            // (undocumented) c) Few heuristics to recognize unknown glyphs
-            // NOTE: Adobe Reader does not do this step, but OSX Preview does
-            var code = 0;
-            switch (glyphName[0]) {
-              case 'G': // Gxx glyph
-                if (glyphName.length === 3) {
-                  code = parseInt(glyphName.substr(1), 16);
-                }
-                break;
-              case 'g': // g00xx glyph
-                if (glyphName.length === 5) {
-                  code = parseInt(glyphName.substr(1), 16);
-                }
-                break;
-              case 'C': // Cddd glyph
-              case 'c': // cddd glyph
-                if (glyphName.length >= 3) {
-                  code = +glyphName.substr(1);
-                }
-                break;
-              default:
-                // 'uniXXXX'/'uXXXX{XX}' glyphs
-                var unicode = getUnicodeForGlyph(glyphName, glyphsUnicodeMap);
-                if (unicode !== -1) {
-                  code = unicode;
-                }
-            }
-            if (code) {
-              // If |baseEncodingName| is one the predefined encodings,
-              // and |code| equals |charcode|, using the glyph defined in the
-              // baseEncoding seems to yield a better |toUnicode| mapping
-              // (fixes issue 5070).
-              if (baseEncodingName && code === +charcode) {
-                var baseEncoding = getEncoding(baseEncodingName);
-                if (baseEncoding && (glyphName = baseEncoding[charcode])) {
-                  toUnicode[charcode] =
-                    String.fromCharCode(glyphsUnicodeMap[glyphName]);
-                  continue;
-                }
-              }
-              toUnicode[charcode] = String.fromCharCode(code);
-            }
-            continue;
-          }
-          toUnicode[charcode] =
-            String.fromCharCode(glyphsUnicodeMap[glyphName]);
-        }
-        return new ToUnicodeMap(toUnicode);
-      }
-      // If the font is a composite font that uses one of the predefined CMaps
-      // listed in Table 118 (except Identity–H and Identity–V) or whose
-      // descendant CIDFont uses the Adobe-GB1, Adobe-CNS1, Adobe-Japan1, or
-      // Adobe-Korea1 character collection:
-      if (properties.composite && (
-           (properties.cMap.builtInCMap &&
-            !(properties.cMap instanceof IdentityCMap)) ||
-           (properties.cidSystemInfo.registry === 'Adobe' &&
-             (properties.cidSystemInfo.ordering === 'GB1' ||
-              properties.cidSystemInfo.ordering === 'CNS1' ||
-              properties.cidSystemInfo.ordering === 'Japan1' ||
-              properties.cidSystemInfo.ordering === 'Korea1')))) {
-        // Then:
-        // a) Map the character code to a character identifier (CID) according
-        // to the font’s CMap.
-        // b) Obtain the registry and ordering of the character collection used
-        // by the font’s CMap (for example, Adobe and Japan1) from its
-        // CIDSystemInfo dictionary.
-        var registry = properties.cidSystemInfo.registry;
-        var ordering = properties.cidSystemInfo.ordering;
-        // c) Construct a second CMap name by concatenating the registry and
-        // ordering obtained in step (b) in the format registry–ordering–UCS2
-        // (for example, Adobe–Japan1–UCS2).
-        var ucs2CMapName = new Name(registry + '-' + ordering + '-UCS2');
-        // d) Obtain the CMap with the name constructed in step (c) (available
-        // from the ASN Web site; see the Bibliography).
-        var ucs2CMap = CMapFactory.create(ucs2CMapName,
-          { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
-        var cMap = properties.cMap;
-        toUnicode = [];
-        cMap.forEach(function(charcode, cid) {
-          assert(cid <= 0xffff, 'Max size of CID is 65,535');
-          // e) Map the CID obtained in step (a) according to the CMap obtained
-          // in step (d), producing a Unicode value.
-          var ucs2 = ucs2CMap.lookup(cid);
-          if (ucs2) {
-            toUnicode[charcode] =
-              String.fromCharCode((ucs2.charCodeAt(0) << 8) +
-                                  ucs2.charCodeAt(1));
-          }
-        });
-        return new ToUnicodeMap(toUnicode);
-      }
-
-      // The viewer's choice, just use an identity map.
-      return new IdentityToUnicodeMap(properties.firstChar,
-                                      properties.lastChar);
-    },
-
     get spaceWidth() {
       if ('_shadowWidth' in this) {
         return this._shadowWidth;
diff --git a/test/font/font_fpgm_spec.js b/test/font/font_fpgm_spec.js
index c040899..5af5878 100644
--- a/test/font/font_fpgm_spec.js
+++ b/test/font/font_fpgm_spec.js
@@ -6,17 +6,20 @@ describe('font_fpgm', function() {
     it('table was truncated in the middle of functions', function() {
       var output;
       waitsFor(function() { return output; }, 10000);
-      var font = new Font("font", new Stream(font2324), {
-        loadedName: 'font',
-        type: 'CIDFontType2',
-        differences: [],
-        defaultEncoding: [],
-        cMap: CMapFactory.create(new Name('Identity-H'))
-      });
-      ttx(font.data, function(result) { output = result; });
-      runs(function() {
-        verifyTtxOutput(output);
-        expect(/(ENDF\[ \]|SVTCA\[0\])\s*<\/assembly>\s*<\/fpgm>/.test(output)).toEqual(true);
+      CMapFactory.create(new Name('Identity-H')).then(function (cMap) {
+        var font = new Font("font", new Stream(font2324), {
+          loadedName: 'font',
+          type: 'CIDFontType2',
+          differences: [],
+          defaultEncoding: [],
+          cMap: cMap,
+          toUnicode: new ToUnicodeMap([])
+        });
+        ttx(font.data, function(result) { output = result; });
+        runs(function() {
+          verifyTtxOutput(output);
+          expect(/(ENDF\[ \]|SVTCA\[0\])\s*<\/assembly>\s*<\/fpgm>/.test(output)).toEqual(true);
+        });
       });
     });
   });
diff --git a/test/font/font_os2_spec.js b/test/font/font_os2_spec.js
index 70e87d0..4b2e5ce 100644
--- a/test/font/font_os2_spec.js
+++ b/test/font/font_os2_spec.js
@@ -11,7 +11,8 @@ describe('font_post', function() {
         loadedName: 'font',
         type: 'TrueType',
         differences: [],
-        defaultEncoding: []
+        defaultEncoding: [],
+        toUnicode: new ToUnicodeMap([])
       });
       ttx(font.data, function(result) { output = result; });
       runs(function() {
@@ -23,17 +24,20 @@ describe('font_post', function() {
     it('has invalid selection attributes presence', function() {
       var output;
       waitsFor(function() { return output; }, 10000);
-      var font = new Font("font", new Stream(font1282), {
-        loadedName: 'font',
-        type: 'CIDFontType2',
-        differences: [],
-        defaultEncoding: [],
-        cMap: CMapFactory.create(new Name('Identity-H'))
-      });
-      ttx(font.data, function(result) { output = result; });
-      runs(function() {
-        verifyTtxOutput(output);
-        expect(/<OS_2>\s*<version value="3"\/>/.test(output)).toEqual(true);
+      CMapFactory.create(new Name('Identity-H')).then(function (cMap) {
+        var font = new Font("font", new Stream(font1282), {
+          loadedName: 'font',
+          type: 'CIDFontType2',
+          differences: [],
+          defaultEncoding: [],
+          cMap: cMap,
+          toUnicode: new ToUnicodeMap([])
+        });
+        ttx(font.data, function(result) { output = result; });
+        runs(function() {
+          verifyTtxOutput(output);
+          expect(/<OS_2>\s*<version value="3"\/>/.test(output)).toEqual(true);
+        });
       });
     });
   });
diff --git a/test/font/font_post_spec.js b/test/font/font_post_spec.js
index 3de7209..f152c5e 100644
--- a/test/font/font_post_spec.js
+++ b/test/font/font_post_spec.js
@@ -8,17 +8,20 @@ describe('font_post', function() {
     it('has invalid version number', function() {
       var output;
       waitsFor(function() { return output; }, 10000);
-      var font = new Font("font", new Stream(font2109), {
-        loadedName: 'font',
-        type: 'CIDFontType2',
-        differences: [],
-        defaultEncoding: [],
-        cMap: CMapFactory.create(new Name('Identity-H'))
-      });
-      ttx(font.data, function(result) { output = result; });
-      runs(function() {
-        verifyTtxOutput(output);
-        expect(/<post>\s*<formatType value="3\.0"\/>/.test(output)).toEqual(true);
+      CMapFactory.create(new Name('Identity-H')).then(function (cMap) {
+        var font = new Font("font", new Stream(font2109), {
+          loadedName: 'font',
+          type: 'CIDFontType2',
+          differences: [],
+          defaultEncoding: [],
+          cMap: cMap,
+          toUnicode: new ToUnicodeMap([])
+        });
+        ttx(font.data, function(result) { output = result; });
+        runs(function() {
+          verifyTtxOutput(output);
+          expect(/<post>\s*<formatType value="3\.0"\/>/.test(output)).toEqual(true);
+        });
       });
     });
 
@@ -29,7 +32,8 @@ describe('font_post', function() {
         loadedName: 'font',
         type: 'TrueType',
         differences: [],
-        defaultEncoding: []
+        defaultEncoding: [],
+        toUnicode: new ToUnicodeMap([])
       });
       ttx(font.data, function(result) { output = result; });
       runs(function() {
@@ -45,7 +49,8 @@ describe('font_post', function() {
         loadedName: 'font',
         type: 'TrueType',
         differences: [],
-        defaultEncoding: []
+        defaultEncoding: [],
+        toUnicode: new ToUnicodeMap([])
       });
       ttx(font.data, function(result) { output = result; });
       runs(function() {
diff --git a/test/font/font_test.html b/test/font/font_test.html
index feba027..0cf0b0e 100644
--- a/test/font/font_test.html
+++ b/test/font/font_test.html
@@ -27,6 +27,7 @@
           'pdfjs/core/cmap'], function (fonts, stream, primitives, cmap) {
         // Expose some of the PDFJS members to global scope for tests.
         window.Font = fonts.Font;
+        window.ToUnicodeMap = fonts.ToUnicodeMap;
         window.Stream = stream.Stream;
         window.Name = primitives.Name;
         window.CMapFactory = cmap.CMapFactory;
diff --git a/test/unit/cmap_spec.js b/test/unit/cmap_spec.js
index 034d512..9a8462a 100644
--- a/test/unit/cmap_spec.js
+++ b/test/unit/cmap_spec.js
@@ -1,5 +1,5 @@
 /* globals expect, it, describe, StringStream, CMapFactory, Name, CMap,
-           IdentityCMap */
+           IdentityCMap, waitsFor */
 
 'use strict';
 
@@ -7,58 +7,84 @@ var cMapUrl = '../../external/bcmaps/';
 var cMapPacked = true;
 
 describe('cmap', function() {
+  var TEST_TIMEOUT = 20000;
+  function waitsForPromiseResolved(promise, successCallback) {
+    var resolved = false;
+    promise.then(function(val) {
+        resolved = true;
+        successCallback(val);
+      },
+      function(error) {
+        // Shouldn't get here.
+        expect(error).toEqual('the promise should not have been rejected');
+      });
+    waitsFor(function() {
+      return resolved;
+    }, TEST_TIMEOUT);
+  }
+
   it('parses beginbfchar', function() {
     var str = '2 beginbfchar\n' +
               '<03> <00>\n' +
               '<04> <01>\n' +
               'endbfchar\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00));
-    expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
-    expect(cmap.lookup(0x05)).toBeUndefined();
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00));
+      expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
+      expect(cmap.lookup(0x05)).toBeUndefined();
+    });
   });
   it('parses beginbfrange with range', function() {
     var str = '1 beginbfrange\n' +
               '<06> <0B> 0\n' +
               'endbfrange\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    expect(cmap.lookup(0x05)).toBeUndefined();
-    expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00));
-    expect(cmap.lookup(0x0B)).toEqual(String.fromCharCode(0x05));
-    expect(cmap.lookup(0x0C)).toBeUndefined();
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap.lookup(0x05)).toBeUndefined();
+      expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00));
+      expect(cmap.lookup(0x0B)).toEqual(String.fromCharCode(0x05));
+      expect(cmap.lookup(0x0C)).toBeUndefined();
+    });
   });
   it('parses beginbfrange with array', function() {
     var str = '1 beginbfrange\n' +
               '<0D> <12> [ 0 1 2 3 4 5 ]\n' +
               'endbfrange\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    expect(cmap.lookup(0x0C)).toBeUndefined();
-    expect(cmap.lookup(0x0D)).toEqual(0x00);
-    expect(cmap.lookup(0x12)).toEqual(0x05);
-    expect(cmap.lookup(0x13)).toBeUndefined();
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap.lookup(0x0C)).toBeUndefined();
+      expect(cmap.lookup(0x0D)).toEqual(0x00);
+      expect(cmap.lookup(0x12)).toEqual(0x05);
+      expect(cmap.lookup(0x13)).toBeUndefined();
+    });
   });
   it('parses begincidchar', function() {
     var str = '1 begincidchar\n' +
               '<14> 0\n' +
               'endcidchar\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    expect(cmap.lookup(0x14)).toEqual(0x00);
-    expect(cmap.lookup(0x15)).toBeUndefined();
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap.lookup(0x14)).toEqual(0x00);
+      expect(cmap.lookup(0x15)).toBeUndefined();
+    });
   });
   it('parses begincidrange', function() {
     var str = '1 begincidrange\n' +
               '<0016> <001B>   0\n' +
               'endcidrange\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    expect(cmap.lookup(0x15)).toBeUndefined();
-    expect(cmap.lookup(0x16)).toEqual(0x00);
-    expect(cmap.lookup(0x1B)).toEqual(0x05);
-    expect(cmap.lookup(0x1C)).toBeUndefined();
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap.lookup(0x15)).toBeUndefined();
+      expect(cmap.lookup(0x16)).toEqual(0x00);
+      expect(cmap.lookup(0x1B)).toEqual(0x05);
+      expect(cmap.lookup(0x1C)).toBeUndefined();
+    });
   });
   it('decodes codespace ranges', function() {
     var str = '1 begincodespacerange\n' +
@@ -66,65 +92,79 @@ describe('cmap', function() {
               '<00000003> <00000004>\n' +
               'endcodespacerange\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    var c = {};
-    cmap.readCharCode(String.fromCharCode(1), 0, c);
-    expect(c.charcode).toEqual(1);
-    expect(c.length).toEqual(1);
-    cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c);
-    expect(c.charcode).toEqual(3);
-    expect(c.length).toEqual(4);
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      var c = {};
+      cmap.readCharCode(String.fromCharCode(1), 0, c);
+      expect(c.charcode).toEqual(1);
+      expect(c.length).toEqual(1);
+      cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c);
+      expect(c.charcode).toEqual(3);
+      expect(c.length).toEqual(4);
+    });
   });
   it('decodes 4 byte codespace ranges', function() {
     var str = '1 begincodespacerange\n' +
               '<8EA1A1A1> <8EA1FEFE>\n' +
               'endcodespacerange\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    var c = {};
-    cmap.readCharCode(String.fromCharCode(0x8E, 0xA1, 0xA1, 0xA1), 0, c);
-    expect(c.charcode).toEqual(0x8EA1A1A1);
-    expect(c.length).toEqual(4);
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      var c = {};
+      cmap.readCharCode(String.fromCharCode(0x8E, 0xA1, 0xA1, 0xA1), 0, c);
+      expect(c.charcode).toEqual(0x8EA1A1A1);
+      expect(c.length).toEqual(4);
+    });
   });
   it('read usecmap', function() {
     var str = '/Adobe-Japan1-1 usecmap\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream,
+    var cmapPromise = CMapFactory.create(stream,
                                   { url: cMapUrl, packed: cMapPacked }, null);
-    expect(cmap instanceof CMap).toEqual(true);
-    expect(cmap.useCMap).not.toBeNull();
-    expect(cmap.builtInCMap).toBeFalsy();
-    expect(cmap.length).toEqual(0x20A7);
-    expect(cmap.isIdentityCMap).toEqual(false);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap instanceof CMap).toEqual(true);
+      expect(cmap.useCMap).not.toBeNull();
+      expect(cmap.builtInCMap).toBeFalsy();
+      expect(cmap.length).toEqual(0x20A7);
+      expect(cmap.isIdentityCMap).toEqual(false);
+    });
   });
   it('parses cmapname', function() {
     var str = '/CMapName /Identity-H def\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    expect(cmap.name).toEqual('Identity-H');
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap.name).toEqual('Identity-H');
+    });
   });
   it('parses wmode', function() {
     var str = '/WMode 1 def\n';
     var stream = new StringStream(str);
-    var cmap = CMapFactory.create(stream);
-    expect(cmap.vertical).toEqual(true);
+    var cmapPromise = CMapFactory.create(stream);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap.vertical).toEqual(true);
+    });
   });
   it('loads built in cmap', function() {
-    var cmap = CMapFactory.create(new Name('Adobe-Japan1-1'),
+    var cmapPromise = CMapFactory.create(new Name('Adobe-Japan1-1'),
                                   { url: cMapUrl, packed: cMapPacked }, null);
-    expect(cmap instanceof CMap).toEqual(true);
-    expect(cmap.useCMap).toBeNull();
-    expect(cmap.builtInCMap).toBeTruthy();
-    expect(cmap.length).toEqual(0x20A7);
-    expect(cmap.isIdentityCMap).toEqual(false);
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap instanceof CMap).toEqual(true);
+      expect(cmap.useCMap).toBeNull();
+      expect(cmap.builtInCMap).toBeTruthy();
+      expect(cmap.length).toEqual(0x20A7);
+      expect(cmap.isIdentityCMap).toEqual(false);
+    });
   });
   it('loads built in identity cmap', function() {
-    var cmap = CMapFactory.create(new Name('Identity-H'),
+    var cmapPromise = CMapFactory.create(new Name('Identity-H'),
                                   { url: cMapUrl, packed: cMapPacked }, null);
-    expect(cmap instanceof IdentityCMap).toEqual(true);
-    expect(cmap.vertical).toEqual(false);
-    expect(cmap.length).toEqual(0x10000);
-    expect(function() { return cmap.isIdentityCMap; }).toThrow(
-      new Error('should not access .isIdentityCMap'));
+    waitsForPromiseResolved(cmapPromise, function (cmap) {
+      expect(cmap instanceof IdentityCMap).toEqual(true);
+      expect(cmap.vertical).toEqual(false);
+      expect(cmap.length).toEqual(0x10000);
+      expect(function() { return cmap.isIdentityCMap; }).toThrow(
+        new Error('should not access .isIdentityCMap'));
+    });
   });
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git



More information about the Pkg-javascript-commits mailing list