diff --git a/LICENSE b/LICENSE index db52dec8e..a3e99545a 100644 --- a/LICENSE +++ b/LICENSE @@ -10,6 +10,8 @@ Kalervo Kujala Adil Allawi <@ironymark> Jakob Miland + Artur Adib + Brendan Dahl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), diff --git a/Makefile b/Makefile index c1062d49d..043cb7fb5 100644 --- a/Makefile +++ b/Makefile @@ -223,14 +223,12 @@ FIREFOX_CONTENT_DIR := $(EXTENSION_SRC)/firefox/$(CONTENT_DIR)/ FIREFOX_EXTENSION_FILES_TO_COPY = \ *.js \ *.rdf \ - chrome.manifest \ components \ $(NULL) FIREFOX_EXTENSION_FILES = \ content \ *.js \ install.rdf \ - chrome.manifest \ components \ content \ $(NULL) diff --git a/README.md b/README.md index 09cc95039..80763aa75 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,15 @@ using the pdf.js API. ### Extension -A Firefox extension is also available: +A Firefox extension is availble in two places: -+ http://mozilla.github.com/pdf.js/extensions/firefox/pdf.js.xpi ++ Stable Version: https://addons.mozilla.org/en-US/firefox/addon/pdfjs ++ Development Version: http://mozilla.github.com/pdf.js/extensions/firefox/pdf.js.xpi -Note that this extension is self-updating, and by default Firefox will auto-update extensions on a -daily basis (you can change this through the `extensions.update.interval` option in `about:config`). +The development extension should be quite stable but still might break from time to time. +Also, note that the development extension is updated on every merge and by default Firefox will +auto-update extensions on a daily basis (you can change this through the +`extensions.update.interval` option in `about:config`). For an experimental Chrome extension, get the code as explained below and issue `make extension`. Then open Chrome with the flag `--enable-experimental-extension-apis`, go to `Tools > Extension` diff --git a/extensions/firefox/bootstrap.js b/extensions/firefox/bootstrap.js index f1a712c0c..dc9d51f36 100644 --- a/extensions/firefox/bootstrap.js +++ b/extensions/firefox/bootstrap.js @@ -3,8 +3,9 @@ 'use strict'; +const RESOURCE_NAME = 'pdf.js'; const EXT_PREFIX = 'extensions.uriloader@pdf.js'; -const PDFJS_EVENT_ID = 'pdf.js.message'; + let Cc = Components.classes; let Ci = Components.interfaces; let Cm = Components.manager; @@ -16,24 +17,80 @@ function log(str) { dump(str + '\n'); } +// Register/unregister a class as a component. +let Factory = { + registrar: null, + aClass: null, + register: function(aClass) { + if (this.aClass) { + log('Cannot register more than one class'); + return; + } + this.registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); + this.aClass = aClass; + var proto = aClass.prototype; + this.registrar.registerFactory(proto.classID, proto.classDescription, + proto.contractID, this); + }, + unregister: function() { + if (!this.aClass) { + log('Class was never registered.'); + return; + } + var proto = this.aClass.prototype; + this.registrar.unregisterFactory(proto.classID, this); + this.aClass = null; + }, + // nsIFactory::createInstance + createInstance: function(outer, iid) { + if (outer !== null) + throw Cr.NS_ERROR_NO_AGGREGATION; + return (new (this.aClass)).QueryInterface(iid); + } +}; + +let pdfStreamConverterUrl = null; + +// As of Firefox 13 bootstrapped add-ons don't support automatic registering and +// unregistering of resource urls and components/contracts. Until then we do +// it programatically. See ManifestDirective ManifestParser.cpp for support. function startup(aData, aReason) { - let manifestPath = 'chrome.manifest'; - let manifest = Cc['@mozilla.org/file/local;1'] - .createInstance(Ci.nsILocalFile); - try { - manifest.initWithPath(aData.installPath.path); - manifest.append(manifestPath); - Cm.QueryInterface(Ci.nsIComponentRegistrar).autoRegister(manifest); - Services.prefs.setBoolPref('extensions.pdf.js.active', true); - } catch (e) { - log(e); - } + // Setup the resource url. + var ioService = Services.io; + var resProt = ioService.getProtocolHandler('resource') + .QueryInterface(Ci.nsIResProtocolHandler); + var aliasFile = Cc['@mozilla.org/file/local;1'] + .createInstance(Ci.nsILocalFile); + var componentPath = aData.installPath.clone(); + componentPath.append('content'); + aliasFile.initWithPath(componentPath.path); + var aliasURI = ioService.newFileURI(aliasFile); + resProt.setSubstitution(RESOURCE_NAME, aliasURI); + + // Load the component and register it. + pdfStreamConverterUrl = aData.resourceURI.spec + + 'components/PdfStreamConverter.js'; + Cu.import(pdfStreamConverterUrl); + Factory.register(PdfStreamConverter); + Services.prefs.setBoolPref('extensions.pdf.js.active', true); } function shutdown(aData, aReason) { if (Services.prefs.getBoolPref('extensions.pdf.js.active')) Services.prefs.setBoolPref('extensions.pdf.js.active', false); + var ioService = Services.io; + var resProt = ioService.getProtocolHandler('resource') + .QueryInterface(Ci.nsIResProtocolHandler); + // Remove the resource url. + resProt.setSubstitution(RESOURCE_NAME, null); + // Remove the contract/component. + Factory.unregister(); + // Unload the converter + if (pdfStreamConverterUrl) { + Cu.unload(pdfStreamConverterUrl); + pdfStreamConverterUrl = null; + } } function install(aData, aReason) { diff --git a/extensions/firefox/chrome.manifest b/extensions/firefox/chrome.manifest deleted file mode 100644 index 5351257e7..000000000 --- a/extensions/firefox/chrome.manifest +++ /dev/null @@ -1,5 +0,0 @@ -resource pdf.js content/ - -component {6457a96b-2d68-439a-bcfa-44465fbcdbb1} components/PdfStreamConverter.js -contract @mozilla.org/streamconv;1?from=application/pdf&to=*/* {6457a96b-2d68-439a-bcfa-44465fbcdbb1} - diff --git a/extensions/firefox/components/PdfStreamConverter.js b/extensions/firefox/components/PdfStreamConverter.js index bd3bfffcf..06db4e2d3 100644 --- a/extensions/firefox/components/PdfStreamConverter.js +++ b/extensions/firefox/components/PdfStreamConverter.js @@ -3,24 +3,43 @@ 'use strict'; +var EXPORTED_SYMBOLS = ['PdfStreamConverter']; + const Cc = Components.classes; const Ci = Components.interfaces; const Cr = Components.results; const Cu = Components.utils; const PDFJS_EVENT_ID = 'pdf.js.message'; const PDF_CONTENT_TYPE = 'application/pdf'; -const NS_ERROR_NOT_IMPLEMENTED = 0x80004001; const EXT_PREFIX = 'extensions.uriloader@pdf.js'; +const MAX_DATABASE_LENGTH = 4096; Cu.import('resource://gre/modules/XPCOMUtils.jsm'); Cu.import('resource://gre/modules/Services.jsm'); function log(aMsg) { let msg = 'PdfStreamConverter.js: ' + (aMsg.join ? aMsg.join('') : aMsg); - Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService) - .logStringMessage(msg); + Services.console.logStringMessage(msg); dump(msg + '\n'); } +function getWindow(top, id) { + return top.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindowUtils) + .getOuterWindowWithId(id); +} +function windowID(win) { + return win.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindowUtils) + .outerWindowID; +} +function topWindow(win) { + return win.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebNavigation) + .QueryInterface(Ci.nsIDocShellTreeItem) + .rootTreeItem + .QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindow); +} let application = Cc['@mozilla.org/fuel/application;1'] .getService(Ci.fuelIApplication); let privateBrowsing = Cc['@mozilla.org/privatebrowsing;1'] @@ -38,6 +57,9 @@ ChromeActions.prototype = { setDatabase: function(data) { if (this.inPrivateBrowswing) return; + // Protect against something sending tons of data to setDatabase. + if (data.length > MAX_DATABASE_LENGTH) + return; application.prefs.setValue(EXT_PREFIX + '.database', data); }, getDatabase: function() { @@ -95,13 +117,13 @@ PdfStreamConverter.prototype = { // nsIStreamConverter::convert convert: function(aFromStream, aFromType, aToType, aCtxt) { - return aFromStream; + throw Cr.NS_ERROR_NOT_IMPLEMENTED; }, // nsIStreamConverter::asyncConvertData asyncConvertData: function(aFromType, aToType, aListener, aCtxt) { if (!Services.prefs.getBoolPref('extensions.pdf.js.active')) - throw NS_ERROR_NOT_IMPLEMENTED; + throw Cr.NS_ERROR_NOT_IMPLEMENTED; // Store the listener passed to us this.listener = aListener; }, @@ -121,8 +143,7 @@ PdfStreamConverter.prototype = { aRequest.cancel(Cr.NS_BINDING_ABORTED); // Create a new channel that is viewer loaded as a resource. - var ioService = Cc['@mozilla.org/network/io-service;1'] - .getService(Ci.nsIIOService); + var ioService = Services.io; var channel = ioService.newChannel( 'resource://pdf.js/web/viewer.html', null, null); @@ -133,21 +154,31 @@ PdfStreamConverter.prototype = { // Setup a global listener waiting for the next DOM to be created and verfiy // that its the one we want by its URL. When the correct DOM is found create // an event listener on that window for the pdf.js events that require - // chrome priviledges. - var url = aRequest.URI.spec; - var gb = Services.wm.getMostRecentWindow('navigator:browser'); - var domListener = function domListener(event) { - var doc = event.originalTarget; - var win = doc.defaultView; - if (doc.location.href === url) { - gb.removeEventListener('DOMContentLoaded', domListener); - var requestListener = new RequestListener(new ChromeActions()); + // chrome priviledges. Code snippet from John Galt. + let window = aRequest.loadGroup.groupObserver + .QueryInterface(Ci.nsIWebProgress) + .DOMWindow; + let top = topWindow(window); + let id = windowID(window); + window = null; + + top.addEventListener('DOMWindowCreated', function onDOMWinCreated(event) { + let doc = event.originalTarget; + let win = doc.defaultView; + + if (id == windowID(win)) { + top.removeEventListener('DOMWindowCreated', onDOMWinCreated, true); + if (!doc.documentURIObject.equals(aRequest.URI)) + return; + + let requestListener = new RequestListener(new ChromeActions); win.addEventListener(PDFJS_EVENT_ID, function(event) { requestListener.receive(event); }, false, true); + } else if (!getWindow(top, id)) { + top.removeEventListener('DOMWindowCreated', onDOMWinCreated, true); } - }; - gb.addEventListener('DOMContentLoaded', domListener, false); + }, true); }, // nsIRequestObserver::onStopRequest diff --git a/extensions/firefox/install.rdf b/extensions/firefox/install.rdf index f4e6b43ea..dd86b3245 100644 --- a/extensions/firefox/install.rdf +++ b/extensions/firefox/install.rdf @@ -12,7 +12,7 @@ {ec8030f7-c20a-464f-9b0e-13a3a9e97384} 6.0 - 11.0a1 + 13.0a1 true diff --git a/src/colorspace.js b/src/colorspace.js index d67d928b1..57bc9c846 100644 --- a/src/colorspace.js +++ b/src/colorspace.js @@ -57,6 +57,11 @@ var ColorSpace = (function ColorSpaceClosure() { return new AlternateCS(numComps, ColorSpace.fromIR(alt), PDFFunction.fromIR(tintFnIR)); + case 'LabCS': + var whitePoint = IR[1].WhitePoint; + var blackPoint = IR[1].BlackPoint; + var range = IR[1].Range; + return new LabCS(whitePoint, blackPoint, range); default: error('Unkown name ' + name); } @@ -146,6 +151,8 @@ var ColorSpace = (function ColorSpaceClosure() { var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3])); return ['AlternateCS', numComps, alt, tintFnIR]; case 'Lab': + var params = cs[1].map; + return ['LabCS', params]; default: error('unimplemented color space object "' + mode + '"'); } @@ -409,3 +416,116 @@ var DeviceCmykCS = (function DeviceCmykCSClosure() { return DeviceCmykCS; })(); +// +// LabCS: Based on "PDF Reference, Sixth Ed", p.250 +// +var LabCS = (function LabCSClosure() { + function LabCS(whitePoint, blackPoint, range) { + this.name = 'Lab'; + this.numComps = 3; + this.defaultColor = [0, 0, 0]; + + if (!whitePoint) + error('WhitePoint missing - required for color space Lab'); + blackPoint = blackPoint || [0, 0, 0]; + range = range || [-100, 100, -100, 100]; + + // Translate args to spec variables + this.XW = whitePoint[0]; + this.YW = whitePoint[1]; + this.ZW = whitePoint[2]; + this.amin = range[0]; + this.amax = range[1]; + this.bmin = range[2]; + this.bmax = range[3]; + + // These are here just for completeness - the spec doesn't offer any + // formulas that use BlackPoint in Lab + this.XB = blackPoint[0]; + this.YB = blackPoint[1]; + this.ZB = blackPoint[2]; + + // Validate vars as per spec + if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) + error('Invalid WhitePoint components, no fallback available'); + + if (this.XB < 0 || this.YB < 0 || this.ZB < 0) { + warn('Invalid BlackPoint, falling back to default'); + this.XB = this.YB = this.ZB = 0; + } + + if (this.amin > this.amax || this.bmin > this.bmax) { + warn('Invalid Range, falling back to defaults'); + this.amin = -100; + this.amax = 100; + this.bmin = -100; + this.bmax = 100; + } + }; + + // Function g(x) from spec + function g(x) { + if (x >= 6 / 29) + return x * x * x; + else + return (108 / 841) * (x - 4 / 29); + } + + LabCS.prototype = { + getRgb: function labcs_getRgb(color) { + // Ls,as,bs <---> L*,a*,b* in the spec + var Ls = color[0], as = color[1], bs = color[2]; + + // Adjust limits of 'as' and 'bs' + as = as > this.amax ? this.amax : as; + as = as < this.amin ? this.amin : as; + bs = bs > this.bmax ? this.bmax : bs; + bs = bs < this.bmin ? this.bmin : bs; + + // Computes intermediate variables X,Y,Z as per spec + var M = (Ls + 16) / 116; + var L = M + (as / 500); + var N = M - (bs / 200); + var X = this.XW * g(L); + var Y = this.YW * g(M); + var Z = this.ZW * g(N); + + // XYZ to RGB 3x3 matrix, from: + // http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC18 + var XYZtoRGB = [3.240479, -1.537150, -0.498535, + -0.969256, 1.875992, 0.041556, + 0.055648, -0.204043, 1.057311]; + + return Util.apply3dTransform(XYZtoRGB, [X, Y, Z]); + }, + getRgbBuffer: function labcs_getRgbBuffer(input, bits) { + if (bits == 8) + return input; + var scale = 255 / ((1 << bits) - 1); + var i, length = input.length / 3; + var rgbBuf = new Uint8Array(length); + + var j = 0; + for (i = 0; i < length; ++i) { + // Convert L*, a*, s* into RGB + var rgb = this.getRgb([input[i], input[i + 1], input[i + 2]]); + rgbBuf[j++] = rgb[0]; + rgbBuf[j++] = rgb[1]; + rgbBuf[j++] = rgb[2]; + } + + return rgbBuf; + }, + isDefaultDecode: function labcs_isDefaultDecode(decodeMap) { + // From Table 90 in Adobe's: + // "Document management - Portable document format", 1st ed, 2008 + if (decodeMap[0] === 0 && decodeMap[1] === 100 && + decodeMap[2] === this.amin && decodeMap[3] === this.amax && + decodeMap[4] === this.bmin && decodeMap[5] === this.bmax) + return true; + else + return false; + } + }; + return LabCS; +})(); diff --git a/src/fonts.js b/src/fonts.js index b12707eb5..f8ae7de4c 100644 --- a/src/fonts.js +++ b/src/fonts.js @@ -573,7 +573,8 @@ var FontLoader = { src += ' window.onload = function fontLoaderOnload() {\n'; src += ' parent.postMessage(JSON.stringify(fontNames), "*");\n'; src += ' }'; - src += ''; + // Hack so the end script tag isn't counted if this is inline JS. + src += ''; for (var i = 0, ii = names.length; i < ii; ++i) { src += '

Hi

'; } diff --git a/src/util.js b/src/util.js index 7aa69503a..f00fcd1ce 100644 --- a/src/util.js +++ b/src/util.js @@ -78,21 +78,39 @@ var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0]; var Util = (function UtilClosure() { function Util() {} + Util.makeCssRgb = function makergb(r, g, b) { var ri = (255 * r) | 0, gi = (255 * g) | 0, bi = (255 * b) | 0; return 'rgb(' + ri + ',' + gi + ',' + bi + ')'; }; + Util.makeCssCmyk = function makecmyk(c, m, y, k) { c = (new DeviceCmykCS()).getRgb([c, m, y, k]); var ri = (255 * c[0]) | 0, gi = (255 * c[1]) | 0, bi = (255 * c[2]) | 0; return 'rgb(' + ri + ',' + gi + ',' + bi + ')'; }; + + // For 2d affine transforms Util.applyTransform = function apply(p, m) { var xt = p[0] * m[0] + p[1] * m[2] + m[4]; var yt = p[0] * m[1] + p[1] * m[3] + m[5]; return [xt, yt]; }; + // Apply a generic 3d matrix M on a 3-vector v: + // | a b c | | X | + // | d e f | x | Y | + // | g h i | | Z | + // M is assumed to be serialized as [a,b,c,d,e,f,g,h,i], + // with v as [X,Y,Z] + Util.apply3dTransform = function apply3d(m, v) { + return [ + m[0] * v[0] + m[1] * v[1] + m[2] * v[2], + m[3] * v[0] + m[4] * v[1] + m[5] * v[2], + m[6] * v[0] + m[7] * v[1] + m[8] * v[2] + ]; + } + Util.sign = function sign(num) { return num < 0 ? -1 : 1; }; diff --git a/test/pdfs/issue1133.pdf.link b/test/pdfs/issue1133.pdf.link new file mode 100644 index 000000000..2480ba8f8 --- /dev/null +++ b/test/pdfs/issue1133.pdf.link @@ -0,0 +1 @@ +http://www.cscw2012.org/docs/program.pdf diff --git a/test/test_manifest.json b/test/test_manifest.json index 2b0541edb..3a1b5bd70 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -445,6 +445,13 @@ "link": false, "type": "eq" }, + { "id": "issue1133", + "file": "pdfs/issue1133.pdf", + "md5": "d1b61580cb100e3df93d33703af1773a", + "rounds": 1, + "link": true, + "type": "eq" + }, { "id": "issue1049", "file": "pdfs/issue1049.pdf", "md5": "15473fffcdde9fb8f3756a4cf1aab347", diff --git a/web/viewer.js b/web/viewer.js index ec05c7135..0ca0a753d 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -109,7 +109,7 @@ var Settings = (function SettingsClosure() { var database = null; var index; if (isFirefoxExtension) - database = FirefoxCom.request('getDatabase', null); + database = FirefoxCom.request('getDatabase', null) || '{}'; else if (isLocalStorageEnabled) database = localStorage.getItem('database') || '{}'; else @@ -131,8 +131,6 @@ var Settings = (function SettingsClosure() { index = database.files.push({fingerprint: fingerprint}) - 1; this.file = database.files[index]; this.database = database; - if (isLocalStorageEnabled) - localStorage.setItem('database', JSON.stringify(database)); } Settings.prototype = {