]> git.parisson.com Git - pdf.js.git/commitdiff
Add JpegStreamProxy - doesnt seem to be used anywhere in the example.pdf file
authorJulian Viereck <julian.viereck@gmail.com>
Wed, 22 Jun 2011 12:02:54 +0000 (14:02 +0200)
committerJulian Viereck <julian.viereck@gmail.com>
Thu, 23 Jun 2011 21:33:23 +0000 (23:33 +0200)
canvas_proxy.js
viewer_worker.html
worker.js

index 610cdcdbaa4d4e195749ac10b0cbb6800a312533..c7234d9c963f1a0a506fff718dc46f9a7da7f320 100644 (file)
@@ -19,6 +19,38 @@ ImageCanvasProxy.prototype.getCanvas = function() {
     return this;
 }
 
+var JpegStreamProxyCounter = 0;
+// WebWorker Proxy for JpegStream.
+var JpegStreamProxy = (function() {
+    function constructor(bytes, dict) {
+        this.id = JpegStreamProxyCounter++;
+        this.dict = dict;
+
+        // create DOM image.
+        postMessage("jpeg_stream");
+        postMessage({
+            id:  this.id,
+            str: bytesToString(bytes)
+        });
+
+        // var img = new Image();
+        // img.src = "data:image/jpeg;base64," + window.btoa(bytesToString(bytes));
+        // this.domImage = img;
+    }
+
+    constructor.prototype = {
+        getImage: function() {
+            return this;
+            // return this.domImage;
+        },
+        getChar: function() {
+            error("internal error: getChar is not valid on JpegStream");
+        }
+    };
+
+    return constructor;
+})();
+
 function CanvasProxy(width, height) {
     var stack = this.$stack = [];
 
@@ -72,9 +104,11 @@ function CanvasProxy(width, height) {
         "$showText"
     ];
 
-    this.drawImage = function(canvas, x, y) {
-        if (canvas instanceof ImageCanvasProxy) {
-            stack.push(["$drawCanvas", [canvas.imgData, x, y, canvas.width, canvas.height]]);
+    this.drawImage = function(image, x, y, width, height, sx, sy, swidth, sheight) {
+        if (image instanceof ImageCanvasProxy) {
+            stack.push(["$drawCanvas", [image.imgData, x, y, image.width, image.height]]);
+        } else if(image instanceof JpegStreamProxy) {
+            stack.push(["$drawImage", [image.id, x, y, sx, sy, swidth, sheight]])
         } else {
             throw "unkown type to drawImage";
         }
@@ -139,9 +173,7 @@ function CanvasProxy(width, height) {
 }
 
 CanvasProxy.prototype.flush = function() {
-    // postMessage("log");
-    // postMessage(JSON.stringify([this.$stack.length]));
     postMessage("canvas_proxy_stack");
-    postMessage(JSON.stringify(this.$stack));
+    postMessage(this.$stack);
     this.$stack.length = 0;
 }
index ced71679ecccb80b81d31c7985b34d773a69a0ab..fd436db7566b1f86fe8aec5a09d6cf6145126aa2 100644 (file)
@@ -13,6 +13,7 @@ function toc(msg) {
 }
 
 var myWorker = new Worker('worker.js');
+var images = {};
 
 var currentX = 0;
 var currentXStack = [];
@@ -47,6 +48,15 @@ var special = {
         var imgData = ctx.getImageData(0, 0, width, height);
         imgData.data = data;
         ctx.putImageData(imgData, x, y);
+    },
+
+    "$drawImage": function(id, x, y, sx, sy, swidth, sheight) {
+        var image = images[id];
+        if (!image) {
+            throw "Image not found";
+        }
+        ctx.drawImage(image, x, y, image.width, image.height,
+            sx, sy, swidth, sheight);
     }
 }
 
@@ -70,6 +80,7 @@ const CANVAS_PROXY_STACK = 1;
 const LOG = 2;
 const FONT = 3;
 const PDF_NUM_PAGE = 4;
+const JPEG_STREAM = 5;
 
 var onMessageState = WAIT;
 var fontStr = null;
@@ -96,11 +107,21 @@ myWorker.onmessage = function(event) {
                 case "font":
                     onMessageState = FONT;
                     return;
+                case "jpeg_stream":
+                    onMessageState = JPEG_STREAM;
+                    return;
                 default:
                     throw "unkown state: " + data
             }
             break;
 
+        case JPEG_STREAM:
+            var img = new Image();
+            img.src = "data:image/jpeg;base64," + window.btoa(data.str);
+            images[data.id] = img;
+            console.log("got image", data.id)
+            break;
+
         case PDF_NUM_PAGE:
             console.log(data);
             maxPages = parseInt(data);
@@ -133,7 +154,7 @@ myWorker.onmessage = function(event) {
             break;
 
         case CANVAS_PROXY_STACK:
-            var stack = JSON.parse(data);
+            var stack = data;
             gStack = stack;
             console.log("canvas stack size", stack.length)
 
@@ -188,7 +209,7 @@ function nextPage() {
 }
 
 function prevPage() {
-    if (currentPage == 0) return;
+    if (currentPage == 1) return;
     currentPage--;
     showPage(currentPage);
 }
index dcb87a8119be89f5550504fbd7d4b0721cd242f8..6294007b61b29e1a700d9a9e5076f21b6a80ac30 100644 (file)
--- a/worker.js
+++ b/worker.js
@@ -15,6 +15,9 @@ importScripts("pdf.js");
 importScripts("fonts.js");
 importScripts("glyphlist.js")
 
+// Use the JpegStreamProxy proxy.
+JpegStream = JpegStreamProxy;
+
 // var array = new Uint8Array(2);
 // array[0] = 1;
 // array[1] = 300;