From 0f6291c7b9361fc8c9b6eb2223034b17b78f3df2 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Sun, 18 Dec 2011 12:53:30 -0600 Subject: [PATCH 01/63] Move text layer UI to viewer.js; fixes adding div with single char; replaces innerHTML to textContent --- src/canvas.js | 50 +++++++------------------------------------------- web/viewer.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index cd49c88b1..75165855c 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -255,8 +255,9 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { } // Scale so that canvas units are the same as PDF user space units this.ctx.scale(cw / mediaBox.width, ch / mediaBox.height); - this.textDivs = []; - this.textLayerQueue = []; + + if (this.textLayer) + this.textLayer.beginLayout(); }, executeIRQueue: function canvasGraphicsExecuteIRQueue(codeIR, @@ -320,27 +321,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { endDrawing: function canvasGraphicsEndDrawing() { this.ctx.restore(); - var textLayer = this.textLayer; - if (!textLayer) - return; - - var self = this; - var textDivs = this.textDivs; - this.textLayerTimer = setInterval(function renderTextLayer() { - if (textDivs.length === 0) { - clearInterval(self.textLayerTimer); - return; - } - var textDiv = textDivs.shift(); - if (textDiv.dataset.textLength > 1) { // avoid div by zero - textLayer.appendChild(textDiv); - // Adjust div width (via letterSpacing) to match canvas text - // Due to the .offsetWidth calls, this is slow - textDiv.style.letterSpacing = - ((textDiv.dataset.canvasWidth - textDiv.offsetWidth) / - (textDiv.dataset.textLength - 1)) + 'px'; - } - }, 0); + if (this.textLayer) + this.textLayer.endLayout(); }, // Graphics state @@ -630,24 +612,6 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { return geometry; }, - pushTextDivs: function canvasGraphicsPushTextDivs(text) { - var div = document.createElement('div'); - var fontSize = this.current.fontSize; - - // vScale and hScale already contain the scaling to pixel units - // as mozCurrentTransform reflects ctx.scale() changes - // (see beginDrawing()) - var fontHeight = fontSize * text.geom.vScale; - div.dataset.canvasWidth = text.canvasWidth * text.geom.hScale; - - div.style.fontSize = fontHeight + 'px'; - div.style.fontFamily = this.current.font.loadedName || 'sans-serif'; - div.style.left = text.geom.x + 'px'; - div.style.top = (text.geom.y - fontHeight) + 'px'; - div.innerHTML = text.str; - div.dataset.textLength = text.length; - this.textDivs.push(div); - }, showText: function canvasGraphicsShowText(str, skipTextSelection) { var ctx = this.ctx; var current = this.current; @@ -753,7 +717,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { } if (textSelection) - this.pushTextDivs(text); + this.textLayer.appendText(text, font.loadedName, fontSize); return text; }, @@ -819,7 +783,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { } if (textSelection) - this.pushTextDivs(text); + this.textLayer.appendText(text, font.loadedName, fontSize); }, nextLineShowText: function canvasGraphicsNextLineShowText(text) { this.nextLine(); diff --git a/web/viewer.js b/web/viewer.js index daf0174ab..afaf03e04 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -595,7 +595,7 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight, this.updateStats(); if (this.onAfterDraw) this.onAfterDraw(); - }).bind(this), textLayer + }).bind(this), new TextLayerBuilder(textLayer) ); setupLinks(this.content, this.scale); @@ -726,6 +726,53 @@ var DocumentOutlineView = function documentOutlineView(outline) { } }; +var TextLayerBuilder = function textLayerBuilder(textLayerDiv) { + this.textLayerDiv = textLayerDiv; + + this.beginLayout = function textLayerBuilderBeginLayout() { + this.textDivs = []; + this.textLayerQueue = []; + }; + + this.endLayout = function textLayerBuilderEndLayout() { + var self = this; + var textDivs = this.textDivs; + var textLayerDiv = this.textLayerDiv; + this.textLayerTimer = setInterval(function renderTextLayer() { + if (textDivs.length === 0) { + clearInterval(self.textLayerTimer); + return; + } + var textDiv = textDivs.shift(); + if (textDiv.dataset.textLength >= 1) { // avoid div by zero + textLayerDiv.appendChild(textDiv); + // Adjust div width (via letterSpacing) to match canvas text + // Due to the .offsetWidth calls, this is slow + textDiv.style.letterSpacing = + ((textDiv.dataset.canvasWidth - textDiv.offsetWidth) / + (textDiv.dataset.textLength - 1)) + 'px'; + } + }, 0); + }; + + this.appendText = function textLayerBuilderAppendText(text, + fontName, fontSize) { + var textDiv = document.createElement('div'); + + // vScale and hScale already contain the scaling to pixel units + var fontHeight = fontSize * text.geom.vScale; + textDiv.dataset.canvasWidth = text.canvasWidth * text.geom.hScale; + + textDiv.style.fontSize = fontHeight + 'px'; + textDiv.style.fontFamily = fontName || 'sans-serif'; + textDiv.style.left = text.geom.x + 'px'; + textDiv.style.top = (text.geom.y - fontHeight) + 'px'; + textDiv.textContent = text.str; + textDiv.dataset.textLength = text.length; + this.textDivs.push(textDiv); + }; +}; + window.addEventListener('load', function webViewerLoad(evt) { var params = document.location.search.substring(1).split('&'); for (var i = 0; i < params.length; i++) { From a52aacab5a8925350e3208a575080f243ff48ccf Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Sun, 18 Dec 2011 16:15:53 -0600 Subject: [PATCH 02/63] Fix the text layer testing --- test/driver.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/driver.js b/test/driver.js index 64fceee90..85d25658a 100644 --- a/test/driver.js +++ b/test/driver.js @@ -165,9 +165,14 @@ function nextPage(task, loadError) { canvas.height = pageHeight * pdfToCssUnitsCoef; clear(ctx); - // using non-attached to the document div to test + // using the text layer builder that does nothing to test // text layer creation operations - var textLayer = document.createElement('div'); + var textLayerBuilder = { + beginLayout: function nullTextLayerBuilderBeginLayout() {}, + endLayout: function nullTextLayerBuilderEndLayout() {}, + appendText: function nullTextLayerBuilderAppendText(text, fontName, + fontSize) {} + }; page.startRendering( ctx, @@ -177,7 +182,7 @@ function nextPage(task, loadError) { failureMessage = 'render : ' + error.message; snapshotCurrentPage(task, failureMessage); }, - textLayer + textLayerBuilder ); } catch (e) { failure = 'page setup : ' + e.toString(); From d44f9f207437384e1ed7004e3843829a5fece0af Mon Sep 17 00:00:00 2001 From: Saebekassebil Date: Thu, 22 Dec 2011 22:29:01 +0100 Subject: [PATCH 03/63] Implemented Settings manager. Now remembering scroll positions --- src/core.js | 10 +++++++- src/obj.js | 2 +- web/viewer.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/core.js b/src/core.js index 93cbc72ac..ac140dafe 100644 --- a/src/core.js +++ b/src/core.js @@ -527,6 +527,14 @@ var PDFDocModel = (function PDFDocModelClosure() { this.startXRef, this.mainXRefEntriesOffset); this.catalog = new Catalog(this.xref); + + if(this.xref.trailer && this.xref.trailer.has('ID')) { + var fileID = ''; + this.xref.trailer.get('ID')[0].split('').forEach(function(el) { + fileID += Number(el.charCodeAt(0)).toString(16); + }); + this.fileID = fileID; + } }, get numPages() { var linearization = this.linearization; @@ -560,7 +568,7 @@ var PDFDoc = (function PDFDocClosure() { this.data = data; this.stream = stream; this.pdf = new PDFDocModel(stream); - + this.fileID = this.pdf.fileID; this.catalog = this.pdf.catalog; this.objs = new PDFObjects(); diff --git a/src/obj.js b/src/obj.js index 453014a91..c0e67efbb 100644 --- a/src/obj.js +++ b/src/obj.js @@ -273,7 +273,7 @@ var XRef = (function XRefClosure() { this.entries = []; this.xrefstms = {}; var trailerDict = this.readXRef(startXRef); - + this.trailer = trailerDict; // prepare the XRef cache this.cache = []; diff --git a/web/viewer.js b/web/viewer.js index e2ffcef29..5e8ec773f 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -25,6 +25,61 @@ var Cache = function cacheCache(size) { }; }; +// Settings Manager - This is a utility for saving settings +// First we see if localStorage is available, which isn't pt. in FF due to bug #495747 +// If not, we use FUEL in FF and fallback to Cookies for other browsers. +(function(parent) { +var COOKIE_WORKS = (function() { + document.cookie = 'they=work'; + return document.cookie.length > 0; +})(); + +var LOCALSTORAGE_WORKS = (function() { + try { + if(typeof localStorage != 'undefined') { + return true; + } + } catch(e) { + return false; + } + return true; +})(); + +var extPrefix = 'extensions.uriloader@pdf.js'; + +var Settings = { + set: function(name, val) { + if(location.protocol == 'chrome:' && !LOCALSTORAGE_WORKS) { + Application.prefs.setValue(extPrefix + '.' + name, val); + } else if(LOCALSTORAGE_WORKS) { + localStorage.setItem(name, val); + } else if(COOKIE_WORKS) { + var cookieString = name + '=' + escape(val); + var expire = (new Date((new Date().getTime())+1000*60*60*24*365)).toGMTString(); + cookieString += '; expires='+expire; + document.cookie = cookieString; + } + }, + + get: function(name, defaultValue) { + if(location.protocol == 'chrome:' && !LOCALSTORAGE_WORKS) { + return Application.prefs.getValue(extPrefix + '.' + name, defaultValue); + } else if(LOCALSTORAGE_WORKS) { + return localStorage.getItem(name) || defaultValue; + } else if(COOKIE_WORKS) { + var res = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' ); + if (res) { + return unescape(res[2]); + } else { + return fallback; + } + } + } +}; + +parent.Settings = Settings; +})(this); + var cache = new Cache(kCacheSize); var currentPageNumber = 1; @@ -292,6 +347,15 @@ var PDFView = { pagesRefMap[pageRef.num + ' ' + pageRef.gen + ' R'] = i; } + var id = pdf.fileID; + if (id) { + var scroll = Settings.get(id + '.scroll', -1); + if (scroll != -1) { + setTimeout(function scrollWindow() { + window.scrollTo(0, scroll); + }, 0); + } + } this.pagesRefMap = pagesRefMap; this.destinations = pdf.catalog.destinations; this.setScale(scale || kDefaultScale, true); @@ -831,6 +895,10 @@ function updateViewarea() { window.addEventListener('scroll', function webViewerScroll(evt) { updateViewarea(); + var fileID; + if((fileID = PDFView.pages[0].content.pdf.fileID)) { + Settings.set(fileID+'.scroll', window.pageYOffset); + } }, true); @@ -888,7 +956,6 @@ window.addEventListener('change', function webViewerChange(evt) { // implemented in Firefox. var file = files[0]; fileReader.readAsBinaryString(file); - document.title = file.name; // URL does not reflect proper document location - hiding some icons. From 0de0e92bc4fa1c09c19fdf4178e589a18ee46051 Mon Sep 17 00:00:00 2001 From: Saebekassebil Date: Thu, 22 Dec 2011 23:44:42 +0100 Subject: [PATCH 04/63] Added #getFingerprint method to PDFDocModel --- src/core.js | 17 ++++++++++++++++- web/viewer.js | 8 ++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/core.js b/src/core.js index ac140dafe..664ecf33c 100644 --- a/src/core.js +++ b/src/core.js @@ -542,6 +542,21 @@ var PDFDocModel = (function PDFDocModelClosure() { // shadow the prototype getter return shadow(this, 'numPages', num); }, + getFingerprint: function pdfDocGetFingerprint() { + if(this.fileID) { + return this.fileID; + } else { + // If we got no fileID, then we generate one, from the first 100 bytes of PDF + var data = this.stream.bytes.subarray(0, 100); + var hash = calculateMD5(data, 0, data.length); + var strHash = ''; + for(var i = 0, length = hash.length; i < length; i++) { + strHash += Number(hash[i]).toString(16); + } + + return strHash; + } + }, getPage: function pdfDocGetPage(n) { return this.catalog.getPage(n); } @@ -568,7 +583,7 @@ var PDFDoc = (function PDFDocClosure() { this.data = data; this.stream = stream; this.pdf = new PDFDocModel(stream); - this.fileID = this.pdf.fileID; + this.fingerprint = this.pdf.getFingerprint(); this.catalog = this.pdf.catalog; this.objs = new PDFObjects(); diff --git a/web/viewer.js b/web/viewer.js index 5e8ec773f..d4afa8faa 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -347,7 +347,7 @@ var PDFView = { pagesRefMap[pageRef.num + ' ' + pageRef.gen + ' R'] = i; } - var id = pdf.fileID; + var id = pdf.fingerprint; if (id) { var scroll = Settings.get(id + '.scroll', -1); if (scroll != -1) { @@ -895,9 +895,9 @@ function updateViewarea() { window.addEventListener('scroll', function webViewerScroll(evt) { updateViewarea(); - var fileID; - if((fileID = PDFView.pages[0].content.pdf.fileID)) { - Settings.set(fileID+'.scroll', window.pageYOffset); + var id; + if((id = PDFView.pages[0].content.pdf.fingerprint)) { + Settings.set(id+'.scroll', window.pageYOffset); } }, true); From d7754a402e726c15275c3580a053cb9a51ca6f87 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Thu, 22 Dec 2011 17:43:14 -0600 Subject: [PATCH 05/63] Correct stroke width for text; convert intel-load test to eq-test --- src/canvas.js | 3 +++ test/test_manifest.json | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index cd49c88b1..00858c937 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -672,6 +672,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { ctx.translate(current.x, current.y); ctx.scale(textHScale, 1); + ctx.lineWidth /= current.textMatrix[0]; if (textSelection) { this.save(); @@ -708,6 +709,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { } else { ctx.save(); this.applyTextTransforms(); + ctx.lineWidth /= current.textMatrix[0] * fontMatrix[0]; + if (textSelection) text.geom = this.getTextGeometry(); diff --git a/test/test_manifest.json b/test/test_manifest.json index 5b88b3136..5a1efd75d 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -17,12 +17,13 @@ "rounds": 1, "type": "load" }, - { "id": "intelisa-load", + { "id": "intelisa-eq", "file": "pdfs/intelisa.pdf", "md5": "f5712097d29287a97f1278839814f682", "link": true, + "pageLimit": 100, "rounds": 1, - "type": "load" + "type": "eq" }, { "id": "pdfspec-load", "file": "pdfs/pdf.pdf", From ac8f0e2c87b66f2ff51bf0b4ef5a1a60814a6c4c Mon Sep 17 00:00:00 2001 From: Saebekassebil Date: Fri, 23 Dec 2011 23:36:37 +0100 Subject: [PATCH 06/63] Address yury's comments, and remove unnecessary hash settings --- web/viewer.js | 100 +++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 55 deletions(-) diff --git a/web/viewer.js b/web/viewer.js index d4afa8faa..4eeb3a46e 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -28,57 +28,49 @@ var Cache = function cacheCache(size) { // Settings Manager - This is a utility for saving settings // First we see if localStorage is available, which isn't pt. in FF due to bug #495747 // If not, we use FUEL in FF and fallback to Cookies for other browsers. -(function(parent) { -var COOKIE_WORKS = (function() { - document.cookie = 'they=work'; - return document.cookie.length > 0; -})(); +var Settings = (function settingsClosure() { + var isCookiesEnabled = (function() { + document.cookie = 'they=work'; + return document.cookie.length > 0; + })(); -var LOCALSTORAGE_WORKS = (function() { - try { - if(typeof localStorage != 'undefined') { - return true; + var isLocalStorageEnabled = (function localStorageEnabledTest() { + try { + localStorage; + } catch(e) { + return false; } - } catch(e) { - return false; - } - return true; -})(); + return true; + })(); -var extPrefix = 'extensions.uriloader@pdf.js'; + var extPrefix = 'extensions.uriloader@pdf.js'; -var Settings = { - set: function(name, val) { - if(location.protocol == 'chrome:' && !LOCALSTORAGE_WORKS) { - Application.prefs.setValue(extPrefix + '.' + name, val); - } else if(LOCALSTORAGE_WORKS) { - localStorage.setItem(name, val); - } else if(COOKIE_WORKS) { - var cookieString = name + '=' + escape(val); - var expire = (new Date((new Date().getTime())+1000*60*60*24*365)).toGMTString(); - cookieString += '; expires='+expire; - document.cookie = cookieString; - } - }, + return { + set: function settingsSet(name, val) { + if(location.protocol == 'chrome:' && !isLocalStorageEnabled) { + Application.prefs.setValue(extPrefix + '.' + name, val); + } else if(isLocalStorageEnabled) { + localStorage.setItem(name, val); + } else if(isCookiesEnabled) { + var cookieString = name + '=' + escape(val); + var expire = (new Date((new Date().getTime())+1000*60*60*24*365)).toGMTString(); + cookieString += '; expires='+expire; + document.cookie = cookieString; + } + }, - get: function(name, defaultValue) { - if(location.protocol == 'chrome:' && !LOCALSTORAGE_WORKS) { - return Application.prefs.getValue(extPrefix + '.' + name, defaultValue); - } else if(LOCALSTORAGE_WORKS) { - return localStorage.getItem(name) || defaultValue; - } else if(COOKIE_WORKS) { - var res = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' ); - if (res) { - return unescape(res[2]); - } else { - return fallback; + get: function settingsGet(name, defaultValue) { + if(location.protocol == 'chrome:' && !isLocalStorageEnabled) { + return Application.prefs.getValue(extPrefix + '.' + name, defaultValue); + } else if(isLocalStorageEnabled) { + return localStorage.getItem(name) || defaultValue; + } else if(isCookiesEnabled) { + var res = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' ); + return res ? unescape(res[2]) : defaultValue; } } - } -}; - -parent.Settings = Settings; -})(this); + }; +})(); var cache = new Cache(kCacheSize); var currentPageNumber = 1; @@ -347,15 +339,6 @@ var PDFView = { pagesRefMap[pageRef.num + ' ' + pageRef.gen + ' R'] = i; } - var id = pdf.fingerprint; - if (id) { - var scroll = Settings.get(id + '.scroll', -1); - if (scroll != -1) { - setTimeout(function scrollWindow() { - window.scrollTo(0, scroll); - }, 0); - } - } this.pagesRefMap = pagesRefMap; this.destinations = pdf.catalog.destinations; this.setScale(scale || kDefaultScale, true); @@ -371,8 +354,15 @@ var PDFView = { this.setHash(this.initialBookmark); this.initialBookmark = null; } - else - this.page = 1; + else { + var scroll = Settings.get(pdf.fingerprint + '.scroll', -1); + if (scroll != -1) { + setTimeout(function scrollWindow() { + window.scrollTo(0, scroll); + }, 0); + } else + this.page = 1; + } }, setHash: function pdfViewSetHash(hash) { From c7375745ae4c1332763ff01ccd52c58a75458d35 Mon Sep 17 00:00:00 2001 From: Saebekassebil Date: Fri, 23 Dec 2011 23:56:01 +0100 Subject: [PATCH 07/63] Too rash. Fixes gjslint errors --- web/viewer.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/web/viewer.js b/web/viewer.js index 4eeb3a46e..f04023ae8 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -26,7 +26,7 @@ var Cache = function cacheCache(size) { }; // Settings Manager - This is a utility for saving settings -// First we see if localStorage is available, which isn't pt. in FF due to bug #495747 +// First we see if localStorage is available, FF bug #495747 // If not, we use FUEL in FF and fallback to Cookies for other browsers. var Settings = (function settingsClosure() { var isCookiesEnabled = (function() { @@ -37,7 +37,7 @@ var Settings = (function settingsClosure() { var isLocalStorageEnabled = (function localStorageEnabledTest() { try { localStorage; - } catch(e) { + } catch (e) { return false; } return true; @@ -47,25 +47,27 @@ var Settings = (function settingsClosure() { return { set: function settingsSet(name, val) { - if(location.protocol == 'chrome:' && !isLocalStorageEnabled) { - Application.prefs.setValue(extPrefix + '.' + name, val); - } else if(isLocalStorageEnabled) { + if (location.protocol == 'chrome:' && !isLocalStorageEnabled) { + Application.prefs.setValue(extPrefix + '.' + name, val); + } else if (isLocalStorageEnabled) { localStorage.setItem(name, val); - } else if(isCookiesEnabled) { + } else if (isCookiesEnabled) { var cookieString = name + '=' + escape(val); - var expire = (new Date((new Date().getTime())+1000*60*60*24*365)).toGMTString(); - cookieString += '; expires='+expire; - document.cookie = cookieString; - } + var expire = new Date(); + expire.setTime(expire.getTime() + 1000 * 60 * 60 * 24 * 365); + cookieString += '; expires=' + expire.toGMTString(); + document.cookie = cookieString; + } }, get: function settingsGet(name, defaultValue) { - if(location.protocol == 'chrome:' && !isLocalStorageEnabled) { - return Application.prefs.getValue(extPrefix + '.' + name, defaultValue); - } else if(isLocalStorageEnabled) { + if (location.protocol == 'chrome:' && !isLocalStorageEnabled) { + var preferenceName = extPrefix + '.' + name; + return Application.prefs.getValue(preferenceName, defaultValue); + } else if (isLocalStorageEnabled) { return localStorage.getItem(name) || defaultValue; - } else if(isCookiesEnabled) { - var res = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' ); + } else if (isCookiesEnabled) { + var res = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)'); return res ? unescape(res[2]) : defaultValue; } } @@ -360,7 +362,7 @@ var PDFView = { setTimeout(function scrollWindow() { window.scrollTo(0, scroll); }, 0); - } else + } else this.page = 1; } }, @@ -885,10 +887,8 @@ function updateViewarea() { window.addEventListener('scroll', function webViewerScroll(evt) { updateViewarea(); - var id; - if((id = PDFView.pages[0].content.pdf.fingerprint)) { - Settings.set(id+'.scroll', window.pageYOffset); - } + var fingerprint = PDFView.pages[0].content.pdf.fingerprint; + Settings.set(fingerprint + '.scroll', window.pageYOffset); }, true); From 1089c30b56659d46dee2fd570e3c4957028cab19 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Fri, 23 Dec 2011 19:41:12 -0800 Subject: [PATCH 08/63] Adding type4 postscript function support. --- src/function.js | 515 ++++++++++++++++++++++++++++++++++++- test/unit/function_spec.js | 223 ++++++++++++++++ test/unit/unit_test.html | 19 ++ 3 files changed, 751 insertions(+), 6 deletions(-) create mode 100644 test/unit/function_spec.js diff --git a/src/function.js b/src/function.js index 6b0063218..e9099a68a 100644 --- a/src/function.js +++ b/src/function.js @@ -336,16 +336,519 @@ var PDFFunction = (function PDFFunctionClosure() { }; }, - constructPostScript: function pdfFunctionConstructPostScript() { - return [CONSTRUCT_POSTSCRIPT]; + constructPostScript: function pdfFunctionConstructPostScript(fn, dict, xref) { + var domain = dict.get('Domain'); + var range = dict.get('Range'); + + if (!domain) + error('No domain.'); + + if(!range) + error('No range.') + + var lexer = new PostScriptLexer(fn); + var parser = new PostScriptParser(lexer); + var code = parser.parse(); + + return [CONSTRUCT_POSTSCRIPT, domain, range, code]; }, - constructPostScriptFromIR: function pdfFunctionConstructPostScriptFromIR() { - TODO('unhandled type of function'); - return function constructPostScriptFromIRResult() { - return [255, 105, 180]; + constructPostScriptFromIR: + function pdfFunctionConstructPostScriptFromIR(IR) { + var domain = IR[1]; + var range = IR[2]; + var code = IR[3]; + var numOutputs = range.length / 2; + var evaluator = new PostScriptEvaluator(code); + // Cache the values for a big speed up, the cache size is limited though + // since the number of possible values can be huge from a PS function. + var cache = new FunctionCache(); + return function constructPostScriptFromIRResult(args) { + var initialStack = []; + for (var i = 0, ii = (domain.length / 2); i < ii; ++i) { + initialStack.push(args[i]); + } + + var key = initialStack.join('_'); + if (cache.has(key)) + return cache.get(key); + + var stack = evaluator.execute(initialStack); + var transformed = new Array(numOutputs); + for (i = numOutputs - 1; i >= 0; --i) { + var out = stack.pop(); + var rangeIndex = 2 * i; + if (out < range[rangeIndex]) + out = range[rangeIndex]; + else if (out > range[rangeIndex + 1]) + out = range[rangeIndex + 1]; + transformed[i] = out; + } + cache.set(key, transformed); + return transformed; }; } }; })(); +var FunctionCache = (function FunctionCache() { + var MAX_CACHE_SIZE = 1024; + function FunctionCache() { + this.cache = {}; + this.total = 0; + } + FunctionCache.prototype = { + has: function(key) { + return key in this.cache + }, + get: function(key) { + return this.cache[key]; + }, + set: function(key, value) { + if (this.total < MAX_CACHE_SIZE) { + this.cache[key] = value; + this.total++; + } + } + }; + return FunctionCache; +})(); + +var PostScriptStack = (function PostScriptStack() { + var MAX_STACK_SIZE = 100; + function PostScriptStack(initialStack) { + this.stack = initialStack || []; + } + + PostScriptStack.prototype = { + push: function push(value) { + if (this.stack.length >= MAX_STACK_SIZE) + error('PostScript function stack overflow.'); + this.stack.push(value); + }, + pop: function pop() { + if (this.stack.length <= 0) + error('PostScript function stack underflow.'); + return this.stack.pop(); + }, + copy: function copy(n) { + if (this.stack.length + n >= MAX_STACK_SIZE) + error('PostScript function stack overflow.'); + var part = this.stack.slice(this.stack.length - n); + this.stack = this.stack.concat(part); + }, + index: function index(n) { + this.push(this.stack[this.stack.length - n - 1]); + }, + roll: function roll(n, p) { + // rotate the last n stack elements p times + var a = this.stack.splice(this.stack.length - n, n); + // algorithm from http://jsfromhell.com/array/rotate + var l = a.length, p = (Math.abs(p) >= l && (p %= l), + p < 0 && (p += l), p), i, x; + for(; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p)) + for(i = l; i > p; x = a[--i], a[i] = a[i - p], a[i - p] = x); + this.stack = this.stack.concat(a); + } + }; + return PostScriptStack; +})(); +var PostScriptEvaluator = (function PostScriptEvaluator() { + function PostScriptEvaluator(code) { + this.code = code; + console.log(code); + } + PostScriptEvaluator.prototype = { + execute: function(initialStack) { + var stack = new PostScriptStack(initialStack); + var counter = 0; + var code = this.code; + var a, b; + while (counter < this.code.length) { + var instruction = this.code[counter++]; + var operator = instruction[0]; + switch (operator) { + // non standard ps operators + case 'push': + stack.push(instruction[1]); + break; + case 'jz': // jump if false + a = stack.pop(); + if (!a) + counter = instruction[1]; + break; + case 'j': // jump + counter = instruction[1]; + break; + + // all ps operators in alphabetical order (excluding if/ifelse) + case 'abs': + a = stack.pop(); + stack.push(Math.abs(a)); + break; + case 'add': + b = stack.pop(); + a = stack.pop(); + stack.push(a + b); + break; + case 'and': + b = stack.pop(); + a = stack.pop(); + if (isBool(a) && isBool(b)) + stack.push(a && b); + else + stack.push(a & b); + break; + case 'atan': + a = stack.pop(); + stack.push(Math.atan(a)); + break; + case 'bitshift': + b = stack.pop(); + a = stack.pop(); + if (a > 0) + stack.push(a << b); + else + stack.push(a >> b); + break; + case 'ceiling': + a = stack.pop(); + stack.push(Math.ceil(a)); + break; + case 'copy': + a = stack.pop(); + stack.copy(a); + break; + case 'cos': + a = stack.pop(); + stack.push(Math.cos(a)); + break; + case 'cvi': + a = stack.pop(); + if (a >= 0) + stack.push(Math.floor(a)); + else + stack.push(Math.ceil(a)); + break; + case 'cvr': + // noop + break; + case 'div': + b = stack.pop(); + a = stack.pop(); + stack.push(a / b); + break; + case 'dup': + stack.copy(1); + break; + case 'eq': + b = stack.pop(); + a = stack.pop(); + stack.push(a == b); + break; + case 'exch': + stack.roll(2, 1); + break; + case 'exp': + b = stack.pop(); + a = stack.pop(); + stack.push(Math.pow(a, b)); + break; + case 'false': + stack.push(false); + break; + case 'floor': + a = stack.pop(); + stack.push(Math.floor(a)); + break; + case 'ge': + b = stack.pop(); + a = stack.pop(); + stack.push(a >= b); + break; + case 'gt': + b = stack.pop(); + a = stack.pop(); + stack.push(a > b); + break; + case 'idiv': + b = stack.pop(); + a = stack.pop(); + stack.push(Math.floor(a / b)); + break; + case 'index': + a = stack.pop(); + stack.index(a); + break; + case 'le': + b = stack.pop(); + a = stack.pop(); + stack.push(a <= b); + break; + case 'ln': + a = stack.pop(); + stack.push(Math.log(a)); + break; + case 'log': + a = stack.pop(); + stack.push(Math.log(a) / Math.LN10); + break; + case 'lt': + b = stack.pop(); + a = stack.pop(); + stack.push(a < b); + break; + case 'mod': + b = stack.pop(); + a = stack.pop(); + stack.push(a % b); + break; + case 'mul': + b = stack.pop(); + a = stack.pop(); + stack.push(a * b); + break; + case 'ne': + b = stack.pop(); + a = stack.pop(); + stack.push(a != b); + break; + case 'neg': + a = stack.pop(); + stack.push(-1 * b); + break; + case 'not': + a = stack.pop(); + if (isBool(a) && isBool(b)) + stack.push(a && b); + else + stack.push(a & b); + break; + case 'or': + b = stack.pop(); + a = stack.pop(); + if (isBool(a) && isBool(b)) + stack.push(a || b); + else + stack.push(a | b); + break; + case 'pop': + stack.pop(); + break; + case 'roll': + b = stack.pop(); + a = stack.pop(); + stack.roll(a, b); + break; + case 'round': + a = stack.pop(); + stack.push(Math.round(a)); + break; + case 'sin': + a = stack.pop(); + stack.push(Math.sin(a)); + break; + case 'sqrt': + a = stack.pop(); + stack.push(Math.sqrt(a)); + break; + case 'sub': + b = stack.pop(); + a = stack.pop(); + stack.push(a - b); + break; + case 'true': + stack.push(true); + break; + case 'truncate': + a = stack.pop(); + if (a >= 0) + stack.push(Math.floor(a)); + else + stack.push(Math.ceil(a)); + break; + case 'xor': + b = stack.pop(); + a = stack.pop(); + if (isBool(a) && isBool(b)) + stack.push((a ^ b) ? true : false); + else + stack.push(a ^ b); + break; + default: + error('Unknown operator ' + operator); + break + } + } + return stack.stack; + } + } + return PostScriptEvaluator; +})(); + +var PostScriptParser = (function PostScriptParser() { + function PostScriptParser(lexer) { + this.lexer = lexer; + this.code = []; + this.token; + this.prev; + } + PostScriptParser.prototype = { + nextToken: function nextToken() { + this.prev = this.token; + this.token = this.lexer.getToken(); + }, + accept: function accept(type) { + if (this.token.type == type) { + this.nextToken(); + return true; + } + return false; + }, + expect: function expect(type) { + if (this.accept(type)) + return true; + error('Unexpected symbol: found ' + this.token.type + ' expected ' + + type + '.'); + }, + parse: function parse() { + this.nextToken(); + this.expect(PostScriptTokenTypes.LBRACE); + this.parseBlock(); + this.expect(PostScriptTokenTypes.RBRACE); + return this.code; + }, + parseBlock: function parseBlock() { + while (true) { + if (this.accept(PostScriptTokenTypes.NUMBER)) { + this.code.push(['push', this.prev.value]); + } else if (this.accept(PostScriptTokenTypes.OPERATOR)) { + this.code.push([this.prev.value]); + } else if (this.accept(PostScriptTokenTypes.LBRACE)) { + this.parseCondition(); + } else { + return; + } + } + }, + parseCondition: function parseCondition() { + var counter = this.code.length - 1; + var condition = []; + this.code.push(condition); + this.parseBlock(); + this.expect(PostScriptTokenTypes.RBRACE); + if (this.accept(PostScriptTokenTypes.IF)) { + // The true block is right after the 'if' so it just falls through on + // true else it jumps and skips the true block. + condition.push('jz', this.code.length); + } else if(this.accept(PostScriptTokenTypes.LBRACE)) { + var jump = []; + this.code.push(jump); + var endOfTrue = this.code.length; + this.parseBlock(); + this.expect(PostScriptTokenTypes.RBRACE); + this.expect(PostScriptTokenTypes.IFELSE); + // The jump is added at the end of the true block to skip the false + // block. + jump.push('j', this.code.length); + condition.push('jz', endOfTrue); + } else { + error('PS Function: error parsing conditional.'); + } + } + }; + return PostScriptParser; +})(); + +var PostScriptTokenTypes = { + LBRACE: 0, + RBRACE: 1, + NUMBER: 2, + OPERATOR: 3, + IF: 4, + IFELSE: 5 +}; + +var PostScriptToken = (function PostScriptToken() { + function PostScriptToken(type, value) { + this.type = type; + this.value = value; + } + return PostScriptToken; +})(); + +var PostScriptLexer = (function PostScriptLexer() { + function PostScriptLexer(stream) { + this.stream = stream; + } + PostScriptLexer.prototype = { + getToken: function getToken() { + var s = ''; + var ch; + var comment = false; + var stream = this.stream; + + // skip comments + while (true) { + if (!(ch = stream.getChar())) + return EOF; + + if (comment) { + if (ch == '\x0a' || ch == '\x0d') + comment = false; + } else if (ch == '%') { + comment = true; + } else if (!Lexer.isSpace(ch)) { + break; + } + } + switch (ch) { + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + case '+': case '-': case '.': + return new PostScriptToken(PostScriptTokenTypes.NUMBER, + this.getNumber(ch)); + case '{': + return new PostScriptToken(PostScriptTokenTypes.LBRACE, '{'); + case '}': + return new PostScriptToken(PostScriptTokenTypes.RBRACE, '}'); + } + // operator + var str = ch.toLowerCase(); + while (true) { + ch = stream.lookChar().toLowerCase(); + if (ch >= 'a' && ch <= 'z') + str += ch; + else + break; + stream.skip(); + } + switch (str) { + case 'if': + return new PostScriptToken(PostScriptTokenTypes.IF, str); + case 'ifelse': + return new PostScriptToken(PostScriptTokenTypes.IFELSE, str); + default: + return new PostScriptToken(PostScriptTokenTypes.OPERATOR, str); + } + }, + getNumber: function getNumber(ch) { + var str = ch; + var stream = this.stream; + while (true) { + ch = stream.lookChar(); + if ((ch >= '0' && ch <= '9') || ch == '-' || ch == '.') + str += ch; + else + break; + stream.skip(); + } + var value = parseFloat(str); + if (isNaN(value)) + error('Invalid floating point number: ' + value); + return value; + } + }; + return PostScriptLexer; +})(); + diff --git a/test/unit/function_spec.js b/test/unit/function_spec.js new file mode 100644 index 000000000..7c336a65d --- /dev/null +++ b/test/unit/function_spec.js @@ -0,0 +1,223 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ + +'use strict'; + +describe('function', function() { + beforeEach(function () { + this.addMatchers({ + toMatchArray: function(expected) { + var actual = this.actual; + if (actual.length != expected.length) + return false; + for (var i = 0; i < expected.length; i++) { + var a = actual[i], b = expected[i]; + if (isArray(b)) { + if (a.length != b.length) + return false; + for (var j = 0; j < a.length; j++) { + var suba = a[j], subb = b[j]; + if (suba !== subb) + return false + } + } else { + if (a !== b) + return false; + } + } + return true; + } + }); + }); + + describe('PostScriptParser', function() { + function parse(program) { + var stream = new StringStream(program); + var parser = new PostScriptParser(new PostScriptLexer(stream)); + return parser.parse(); + } + it('parses empty programs', function() { + var output = parse('{}'); + expect(output.length).toEqual(0); + }); + it('parses positive numbers', function() { + var number = 999; + var program = parse('{ ' + number + ' }'); + var expectedProgram = [ + ['push', number] + ]; + expect(program).toMatchArray(expectedProgram); + }); + it('parses negative numbers', function() { + var number = -999; + var program = parse('{ ' + number + ' }'); + var expectedProgram = [ + ['push', number] + ]; + expect(program).toMatchArray(expectedProgram); + }); + it('parses negative floats', function() { + var number = 3.3; + var program = parse('{ ' + number + ' }'); + var expectedProgram = [ + ['push', number] + ]; + expect(program).toMatchArray(expectedProgram); + }); + it('parses operators', function() { + var program = parse('{ sub }'); + var expectedProgram = [ + ['sub'] + ]; + expect(program).toMatchArray(expectedProgram); + }); + it('parses if statements', function() { + var program = parse('{ { 99 } if }'); + var expectedProgram = [ + ['jz', 2], + ['push', 99] + ]; + expect(program).toMatchArray(expectedProgram); + }); + it('parses ifelse statements', function() { + var program = parse('{ { 99 } { 44 } ifelse }'); + var expectedProgram = [ + ['jz', 3], + ['push', 99], + ['j', 4], + ['push', 44], + ]; + expect(program).toMatchArray(expectedProgram); + }); + it('handles missing brackets', function() { + expect(function() { parse('{'); }).toThrow( + new Error('Unexpected symbol: found undefined expected 1.')); + }); + }); + + describe('PostScriptEvaluator', function() { + function evaluate(program) { + var stream = new StringStream(program); + var parser = new PostScriptParser(new PostScriptLexer(stream)); + var code = parser.parse(); + var evaluator = new PostScriptEvaluator(code); + var output = evaluator.execute(); + console.log(output); + return output; + } + it('pushes stack', function() { + var stack = evaluate('{ 99 }'); + var expectedStack = [99]; + expect(stack).toMatchArray(expectedStack); + }); + it('handles if with true', function() { + var stack = evaluate('{ 1 {99} if }'); + var expectedStack = [99]; + expect(stack).toMatchArray(expectedStack); + }); + it('handles if with false', function() { + var stack = evaluate('{ 0 {99} if }'); + var expectedStack = []; + expect(stack).toMatchArray(expectedStack); + }); + it('handles ifelse with true', function() { + var stack = evaluate('{ 1 {99} {77} ifelse }'); + var expectedStack = [99]; + expect(stack).toMatchArray(expectedStack); + }); + it('handles ifelse with false', function() { + var stack = evaluate('{ 0 {99} {77} ifelse }'); + var expectedStack = [77]; + expect(stack).toMatchArray(expectedStack); + }); + it('handles nested if', function() { + var stack = evaluate('{ 1 {1 {77} if} if }'); + var expectedStack = [77]; + expect(stack).toMatchArray(expectedStack); + }); + + it('abs', function() { + var stack = evaluate('{ -2 abs }'); + var expectedStack = [2]; + expect(stack).toMatchArray(expectedStack); + }); + it('adds', function() { + var stack = evaluate('{ 1 2 add }'); + var expectedStack = [3]; + expect(stack).toMatchArray(expectedStack); + }); + it('boolean ands', function() { + var stack = evaluate('{ true false and }'); + var expectedStack = [false]; + expect(stack).toMatchArray(expectedStack); + }); + it('bitwise ands', function() { + var stack = evaluate('{ 254 1 and }'); + var expectedStack = [254 & 1]; + expect(stack).toMatchArray(expectedStack); + }); + // TODO atan + // TODO bitshift + // TODO ceiling + // TODO copy + // TODO cos + // TODO cvi + // TODO cvr + // TODO div + it('duplicates', function() { + var stack = evaluate('{ 99 dup }'); + var expectedStack = [99, 99]; + expect(stack).toMatchArray(expectedStack); + }); + // TODO eq + it('exchanges', function() { + var stack = evaluate('{ 44 99 exch }'); + var expectedStack = [99, 44]; + expect(stack).toMatchArray(expectedStack); + }); + // TODO exp + // TODO false + // TODO floor + // TODO ge + // TODO gt + // TODO idiv + it('duplicates index', function() { + var stack = evaluate('{ 4 3 2 1 2 index }'); + var expectedStack = [4, 3, 2, 1, 3]; + expect(stack).toMatchArray(expectedStack); + }); + // TODO le + // TODO ln + // TODO log + // TODO lt + // TODO mod + // TODO mul + // TODO ne + // TODO neg + // TODO not + // TODO or + it('pops stack', function() { + var stack = evaluate('{ 1 2 pop }'); + var expectedStack = [1]; + expect(stack).toMatchArray(expectedStack); + }); + it('rolls stack right', function() { + var stack = evaluate('{ 1 3 2 2 4 1 roll }'); + var expectedStack = [2, 1, 3, 2]; + expect(stack).toMatchArray(expectedStack); + }); + it('rolls stack left', function() { + var stack = evaluate('{ 1 3 2 2 4 -1 roll }'); + var expectedStack = [3, 2, 2, 1]; + expect(stack).toMatchArray(expectedStack); + }); + // TODO round + // TODO sin + // TODO sqrt + // TODO sub + // TODO true + // TODO truncate + // TODO xor + }); +}); + diff --git a/test/unit/unit_test.html b/test/unit/unit_test.html index 1fc28ef83..8d0af03d6 100644 --- a/test/unit/unit_test.html +++ b/test/unit/unit_test.html @@ -11,9 +11,28 @@ + + + + + + + + + + + + + + + + + + +