From a82536881de47589edfa74c955594762acbaa897 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Fri, 1 Jul 2011 16:59:20 -0500 Subject: [PATCH 1/6] Compatibility hacks for IE, Opera, Safari --- compatibility.js | 140 +++++++++++++++++++++++++++++++++++++++++++++++ fonts.js | 2 +- viewer.html | 2 + worker/client.js | 2 +- 4 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 compatibility.js diff --git a/compatibility.js b/compatibility.js new file mode 100644 index 000000000..8b7f63b95 --- /dev/null +++ b/compatibility.js @@ -0,0 +1,140 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ + +// Checking if the typed arrays are supported +(function() { + if (typeof Uint8Array !== "undefined") + return; + + function subarray(start, end) { + return this.slice(start, end); + } + + function set_(array, offset) { + if (arguments.length < 2) offset = 0; + for (var i = 0, n = array.length; i < n; ++i, ++offset) + this[offset] = array[i] & 0xFF; + } + + function TypedArray(arg1) { + var result; + if (typeof arg1 === "number") { + result = new Array(arg1); + for (var i = 0; i < arg1; ++i) + result[i] = 0; + } else + result = arg1.slice(0); + result.subarray = subarray; + result.buffer = result; + result.byteLength = result.length; + result.set = set_; + if (typeof arg1 === "object" && arg1.buffer) + result.buffer = arg1.buffer; + + return result; + } + + window.Uint8Array = TypedArray; + + // we don't need support for set, byteLength for 32-bit array + // so we can use the TypedArray as well + window.Uint32Array = TypedArray; + window.Int32Array = TypedArray; +})(); + +// Object.create() ? +(function() { + if (typeof Object.create !== "undefined") + return; + + Object.create = function(proto) { + var constructor = function() {}; + constructor.prototype = proto; + return new constructor(); + }; +})(); + +// Object.defineProperty() ? +(function() { + if (typeof Object.defineProperty !== "undefined") + return; + + Object.defineProperty = function(obj, name, def) { + delete obj[name]; + if ("get" in def) + obj.__defineGetter__(name, def["get"]); + if ("set" in def) + obj.__defineSetter__(name, def["set"]); + if ("value" in def) { + obj.__defineSetter__(name, function(value) { + this.__defineGetter__(name, function() { + return value; + }); + return value; + }); + obj[name] = def.value; + } + }; +})(); + +// No XMLHttpRequest.response ? +(function() { + var xhrPrototype = XMLHttpRequest.prototype; + if ("response" in xhrPrototype || + "mozResponseArrayBuffer" in xhrPrototype || + "mozResponse" in xhrPrototype || + "responseArrayBuffer" in xhrPrototype) + return; + // IE ? + if (typeof VBArray !== "undefined") { + Object.defineProperty(xhrPrototype, "response", { + get: function() { + return new Uint8Array( new VBArray(this.responseBody).toArray() ); + } + }); + return; + } + + // other browsers + function responseTypeSetter() { + // will be only called to set "arraybuffer" + this.overrideMimeType("text/plain; charset=x-user-defined"); + } + if (typeof xhrPrototype.overrideMimeType === "function") { + Object.defineProperty(xhrPrototype, "responseType", { set: responseTypeSetter }); + } + function responseGetter() { + var text = this.responseText; + var i, n = text.length; + var result = new Uint8Array(n); + for (i = 0; i < n; ++i) + result[i] = text.charCodeAt(i) & 0xFF; + return result; + } + Object.defineProperty(xhrPrototype, "response", { get: responseGetter }); +})(); + +// window.btoa (base64 encode function) ? +(function() { + if ("btoa" in window) + return; + + var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + + window.btoa = function(chars) { + var buffer = ""; + var i, n; + for (i = 0, n = chars.length; i < n; i += 3) { + var b1 = chars.charCodeAt(i) & 0xFF; + var b2 = chars.charCodeAt(i + 1) & 0xFF; + var b3 = chars.charCodeAt(i + 2) & 0xFF; + var d1 = b1 >> 2, d2 = ((b1 & 3) << 4) | (b2 >> 4); + var d3 = i + 1 < n ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64; + var d4 = i + 2 < n ? (b3 & 0x3F) : 64; + buffer += digits.charAt(d1) + digits.charAt(d2) + digits.charAt(d3) + digits.charAt(d4); + } + return buffer; + }; +})(); + + diff --git a/fonts.js b/fonts.js index 9782fc9a1..3db99daf3 100644 --- a/fonts.js +++ b/fonts.js @@ -1057,7 +1057,7 @@ var Font = (function () { var url = "url(data:" + this.mimetype + ";base64," + window.btoa(data) + ");"; var rule = "@font-face { font-family:'" + fontName + "';src:" + url + "}"; var styleSheet = document.styleSheets[0]; - styleSheet.insertRule(rule, styleSheet.length); + styleSheet.insertRule(rule, styleSheet.cssRules.length); } }; diff --git a/viewer.html b/viewer.html index c600547f0..e73357d43 100644 --- a/viewer.html +++ b/viewer.html @@ -1,8 +1,10 @@ + Simple pdf.js page viewer + diff --git a/worker/client.js b/worker/client.js index fd98b857e..359c69ff4 100644 --- a/worker/client.js +++ b/worker/client.js @@ -285,7 +285,7 @@ function WorkerPDFDoc(canvas) { var url = "url(data:" + data.mimetype + ";base64," + base64 + ");"; var rule = "@font-face { font-family:'" + data.fontName + "';src:" + url + "}"; var styleSheet = document.styleSheets[0]; - styleSheet.insertRule(rule, styleSheet.length); + styleSheet.insertRule(rule, styleSheet.cssRules.length); // Just adding the font-face to the DOM doesn't make it load. It // seems it's loaded once Gecko notices it's used. Therefore, From a6f4a9228671983665684f1aaf1563f26154d50a Mon Sep 17 00:00:00 2001 From: Adil Allawi Date: Tue, 5 Jul 2011 23:14:20 +0100 Subject: [PATCH 2/6] OpenOffice includes the optional reference to a Font dictionary in the Resources dictionary. This breaks SetFont in pdf.js as the Font in this.res is not a dictionary but an xref. --- pdf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdf.js b/pdf.js index a9f0ee935..132d16ec1 100644 --- a/pdf.js +++ b/pdf.js @@ -3803,7 +3803,7 @@ var CanvasGraphics = (function() { this.current.leading = leading; }, setFont: function(fontRef, size) { - var font = this.res.get("Font"); + var font = this.xref.fetchIfRef(this.res.get("Font")); if (!IsDict(font)) return; From 2179cd0943dcb6fca604ef2f64962813399e6877 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Tue, 5 Jul 2011 18:17:08 -0500 Subject: [PATCH 3/6] Move compatibility.js into web folder, enable it for viewer.html --- compatibility.js => web/compatibility.js | 0 web/viewer.html | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename compatibility.js => web/compatibility.js (100%) diff --git a/compatibility.js b/web/compatibility.js similarity index 100% rename from compatibility.js rename to web/compatibility.js diff --git a/web/viewer.html b/web/viewer.html index feb1bb00f..df9604db4 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -4,7 +4,7 @@ Simple pdf.js page viewer - + From 5d337b1c767833e86d1c9d73dba2d2d93de9645b Mon Sep 17 00:00:00 2001 From: Adil Allawi Date: Wed, 6 Jul 2011 00:43:47 +0100 Subject: [PATCH 4/6] Added sample OpenOffice file to tests --- test/pdfs/DiwanProfile.pdf.link | 1 + test/test_manifest.json | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 test/pdfs/DiwanProfile.pdf.link diff --git a/test/pdfs/DiwanProfile.pdf.link b/test/pdfs/DiwanProfile.pdf.link new file mode 100644 index 000000000..12694632f --- /dev/null +++ b/test/pdfs/DiwanProfile.pdf.link @@ -0,0 +1 @@ +http://oannis.com/DiwanProfile.pdf \ No newline at end of file diff --git a/test/test_manifest.json b/test/test_manifest.json index 4302e1f6e..d8a8e8f7d 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -42,5 +42,11 @@ "link": true, "rounds": 1, "type": "eq" + }, + { "id": "openoffice-pdf", + "file": "pdfs/DiwanProfile.pdf", + "link": true, + "rounds": 1, + "type": "load" } ] From e5762f3ec821f05ac6b46ebe2dfb6965a1192182 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Tue, 5 Jul 2011 21:32:15 -0500 Subject: [PATCH 5/6] Function.prototype.bind emulation; enable compatibility.js for multi_page_viewer --- web/compatibility.js | 13 +++++++++++++ web/multi_page_viewer.html | 1 + 2 files changed, 14 insertions(+) diff --git a/web/compatibility.js b/web/compatibility.js index 8b7f63b95..948d737c7 100644 --- a/web/compatibility.js +++ b/web/compatibility.js @@ -137,4 +137,17 @@ }; })(); +// Function.prototype.bind ? +(function() { + if (typeof Function.prototype.bind !== "undefined") + return; + Function.prototype.bind = function(obj) { + var fn = this, headArgs = Array.prototype.slice.call(arguments, 1); + var binded = function(tailArgs) { + var args = headArgs.concat(tailArgs); + return fn.apply(obj, args); + }; + return binded; + }; +})(); diff --git a/web/multi_page_viewer.html b/web/multi_page_viewer.html index 841d2dba9..1a164255f 100644 --- a/web/multi_page_viewer.html +++ b/web/multi_page_viewer.html @@ -4,6 +4,7 @@ pdf.js Multi-Page Viewer + From b4461eeb26739446503249f309e2e7ae7fa19aac Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Wed, 6 Jul 2011 00:59:22 -0400 Subject: [PATCH 6/6] Fix test page in non FileAPI supporting browsers --- web/multi_page_viewer.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/multi_page_viewer.html b/web/multi_page_viewer.html index 1a164255f..70a7ea6b3 100644 --- a/web/multi_page_viewer.html +++ b/web/multi_page_viewer.html @@ -35,7 +35,7 @@ Zoom - + Open File