]> git.parisson.com Git - pdf.js.git/commitdiff
Add a basic embedded CMap reader
authorVivien Nicolas <21@vingtetun.org>
Mon, 20 Jun 2011 00:52:30 +0000 (02:52 +0200)
committerVivien Nicolas <21@vingtetun.org>
Mon, 20 Jun 2011 00:52:30 +0000 (02:52 +0200)
pdf.js

diff --git a/pdf.js b/pdf.js
index 631c1a74af96684d036a958cca91f1aeb103fe76..7414ac7ea115fc8859bdee3534e742bb41c8536f 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -1942,13 +1942,12 @@ var CanvasGraphics = (function() {
                     if (!encoding)
                         error("Unknown font encoding");
 
-                    var firstChar = xref.fetchIfRef(fontDict.get("FirstChar"));
-
                     var index = 0;
                     for (var j = 0; j < encoding.length; j++) {
                         encodingMap[index++] = GlyphsUnicode[encoding[j]];
                     }
 
+                    var firstChar = xref.fetchIfRef(fontDict.get("FirstChar"));
                     var widths = xref.fetchIfRef(fontDict.get("Widths"));
                     assertWellFormed(IsArray(widths) && IsInt(firstChar),
                                      "invalid font Widths or FirstChar");
@@ -1959,8 +1958,66 @@ var CanvasGraphics = (function() {
                     }
                 }
             } else if (fontDict.has("ToUnicode")) {
-              TODO("ToUnicode stream translation not implemented");
-            }
+                var cmapObj = xref.fetchIfRef(fontDict.get("ToUnicode"));
+                if (IsName(cmapObj)) {
+                    error("ToUnicode basic cmap translation not implemented");
+                    encodingMap = {};
+                } else if (IsStream(cmapObj)) {
+                    var tokens = [];
+                    var token = "";
+
+                    var cmap = cmapObj.getBytes(cmapObj.length);
+                    for (var i =0; i < cmap.length; i++) {
+                      var byte = cmap[i];
+                      if (byte == 0x20 || byte == 0x0A || byte == 0x3C || byte == 0x3E) {
+                        switch (token) {
+                          case "useCMap":
+                            error("useCMap is not implemented");
+                            break;
+
+                          case "begincodespacerange":
+                          case "beginbfrange":
+                            token = "";
+                            tokens = [];
+                            break;
+
+                          case "endcodespacerange":
+                            TODO("Support CMap ranges");
+                            break;
+
+                          case "endbfrange":
+                            for (var j = 0; j < tokens.length; j+=3) {
+                              var startRange = parseInt("0x" + tokens[j]);
+                              var endRange = parseInt("0x" + tokens[j+1]);
+                              var code = parseInt("0x" + tokens[j+2]);
+
+                              for (var k = startRange; k <= endRange; k++) {
+                                encodingMap[k] = code;
+                                charset.push(code++);
+                              }
+                            }
+                            break;
+
+                          case "beginfbchar":
+                          case "endfbchar":
+                            error("fbchar parsing is not implemented");
+                            break;
+
+                          default:
+                            if (token.length) {
+                              tokens.push(token);
+                              token = "";
+                            }
+                            break;
+                        }
+                    } else if (byte == 0x5B || byte == 0x5D) {
+                        error("CMAP list parsing is not implemented");
+                    } else {
+                        token += String.fromCharCode(byte);
+                    }
+                  }
+               }
+            } 
 
             var subType = fontDict.get("Subtype");
             var bbox = descriptor.get("FontBBox");