]> git.parisson.com Git - pdf.js.git/commitdiff
Make the ProxyCanvas be more a canvas - provide a ctx object to interact through
authorJulian Viereck <julian.viereck@gmail.com>
Wed, 22 Jun 2011 18:04:25 +0000 (20:04 +0200)
committerJulian Viereck <julian.viereck@gmail.com>
Thu, 23 Jun 2011 21:33:23 +0000 (23:33 +0200)
canvas_proxy.js
worker.js

index 0670762e542bbb62ef438127a0ec323c9d18260f..8237574927b8d18c973e36845c388dafe56afc6f 100644 (file)
@@ -64,9 +64,18 @@ function GradientProxy(stack, x0, y0, x1, y1) {
 function CanvasProxy(width, height) {
     var stack = this.$stack = [];
 
+    // Dummy context exposed.
+    var ctx = {};
+    this.getContext = function(type) {
+        if (type != "2d") {
+            throw "CanvasProxy can only provide a 2d context.";
+        }
+        return ctx;
+    }
+
     // Expose only the minimum of the canvas object - there is no dom to do
     // more here.
-    this.canvas = {
+    ctx.canvas = {
         width: width,
         height: height
     }
@@ -114,11 +123,11 @@ function CanvasProxy(width, height) {
         "$showText"
     ];
 
-    this.createLinearGradient = function(x0, y0, x1, y1) {
+    ctx.createLinearGradient = function(x0, y0, x1, y1) {
         return new GradientProxy(stack, x0, y0, x1, y1);
     }
 
-    this.drawImage = function(image, x, y, width, height, sx, sy, swidth, sheight) {
+    ctx.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) {
@@ -137,7 +146,7 @@ function CanvasProxy(width, height) {
     var name;
     for (var i = 0; i < ctxFunc.length; i++) {
         name = ctxFunc[i];
-        this[name] = buildFuncCall(name);
+        ctx[name] = buildFuncCall(name);
     }
 
     var ctxProp = {
@@ -168,20 +177,20 @@ function CanvasProxy(width, height) {
 
     function buildGetter(name) {
         return function() {
-            return this["$" + name];
+            return ctx["$" + name];
         }
     }
 
     function buildSetter(name) {
         return function(value) {
             stack.push(["$", name, value]);
-            return this["$" + name] = value;
+            return ctx["$" + name] = value;
         }
     }
 
     for (var name in ctxProp) {
-        this["$" + name] = ctxProp[name];
-        this.__defineGetter__(name, buildGetter(name));
+        ctx["$" + name] = ctxProp[name];
+        ctx.__defineGetter__(name, buildGetter(name));
 
         // Special treatment for `fillStyle` and `strokeStyle`: The passed style
         // might be a gradient. Need to check for that.
@@ -192,13 +201,13 @@ function CanvasProxy(width, height) {
                         stack.push(["$" + name + "Gradient"]);
                     } else {
                         stack.push(["$", name, value]);
-                        return this["$" + name] = value;
+                        return ctx["$" + name] = value;
                     }
                 }
             }
-            this.__defineSetter__(name, buildSetterStyle(name));
+            ctx.__defineSetter__(name, buildSetterStyle(name));
         } else {
-            this.__defineSetter__(name, buildSetter(name));
+            ctx.__defineSetter__(name, buildSetter(name));
         }
     }
 }
index 6294007b61b29e1a700d9a9e5076f21b6a80ac30..59ad8edea9f1777b19413f14723dee0c00f22bb8 100644 (file)
--- a/worker.js
+++ b/worker.js
@@ -18,11 +18,6 @@ importScripts("glyphlist.js")
 // Use the JpegStreamProxy proxy.
 JpegStream = JpegStreamProxy;
 
-// var array = new Uint8Array(2);
-// array[0] = 1;
-// array[1] = 300;
-// postMessage(array);
-
 var timer = null;
 function tic() {
     timer = Date.now();
@@ -33,15 +28,8 @@ function toc(msg) {
     timer = null;
 }
 
-
+// Create the WebWorkerProxyCanvas.
 var canvas = new CanvasProxy(1224, 1584);
-// canvas.moveTo(0, 10);
-// canvas.lineTo(0, 20);
-// canvas.lineTo(500, 500);
-// canvas.flush();
-// canvas.stroke();
-// canvas.flush();
-log("test");
 
 var pageInterval;
 var pdfDocument = null;
@@ -61,7 +49,7 @@ onmessage = function(event) {
         // page.compile will collect all fonts for us, once we have loaded them
         // we can trigger the actual page rendering with page.display
         var fonts = [];
-        var gfx = new CanvasGraphics(canvas, ImageCanvasProxy);
+        var gfx = new CanvasGraphics(canvas.getContext("2d"), ImageCanvasProxy);
         page.compile(gfx, fonts);
 
         // Inspect fonts and translate the missing one.