From c52e09082a15c5d93aa157dad887ec6aaade67fe Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Fri, 26 Aug 2011 20:41:07 -0500 Subject: [PATCH 1/5] Not reading the dictionary tail of streams are not allowed #389 --- pdf.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pdf.js b/pdf.js index 12aa9ca4c..52fd0ffcb 100644 --- a/pdf.js +++ b/pdf.js @@ -2714,7 +2714,9 @@ var Parser = (function() { // stream objects are not allowed inside content streams or // object streams - if (this.allowStreams && IsCmd(this.buf2, 'stream')) { + if (IsCmd(this.buf2, 'stream')) { + if (!this.allowStreams) + return dict; return this.makeStream(dict, cipherTransform); } else { this.shift(); From 26a8ae507de70d53eddb84229b410621432c5f53 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Fri, 26 Aug 2011 22:22:50 -0500 Subject: [PATCH 2/5] code reformat --- pdf.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pdf.js b/pdf.js index 52fd0ffcb..5cfb7dc28 100644 --- a/pdf.js +++ b/pdf.js @@ -2715,9 +2715,8 @@ var Parser = (function() { // stream objects are not allowed inside content streams or // object streams if (IsCmd(this.buf2, 'stream')) { - if (!this.allowStreams) - return dict; - return this.makeStream(dict, cipherTransform); + return this.allowStreams ? + this.makeStream(dict, cipherTransform) : dict; } else { this.shift(); } From 2a0477d3ea925e0c786ba03990891ab0220e5bcf Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Mon, 29 Aug 2011 20:26:02 -0500 Subject: [PATCH 3/5] cmap fixes: hex/dec numbers, arrays, cmaps, ranges --- pdf.js | 65 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/pdf.js b/pdf.js index 201744efe..e46207315 100644 --- a/pdf.js +++ b/pdf.js @@ -4307,53 +4307,78 @@ var PartialEvaluator = (function() { var tokens = []; var token = ''; + var beginArrayToken = {}; var cmap = cmapObj.getBytes(cmapObj.length); for (var i = 0; i < cmap.length; i++) { var byte = cmap[i]; - if (byte == 0x20 || byte == 0x0A || byte == 0x3C || - byte == 0x3E) { + if (byte == 0x20 || byte == 0x0D || byte == 0x0A || + byte == 0x3C || byte == 0x5B || byte == 0x5D) { switch (token) { - case 'useCMap': - error('useCMap is not implemented'); + case 'usecmap': + error('usecmap is not implemented'); break; case 'beginbfchar': case 'beginbfrange': - case 'begincodespacerange': + case 'begincidchar': + case 'begincidrange': token = ''; tokens = []; break; - case 'endcodespacerange': - TODO('Support CMap ranges'); - break; - + case 'endcidrange': case 'endbfrange': for (var j = 0; j < tokens.length; j += 3) { - var startRange = parseInt('0x' + tokens[j]); - var endRange = parseInt('0x' + tokens[j + 1]); - var code = parseInt('0x' + tokens[j + 2]); + var startRange = tokens[j]; + var endRange = tokens[j + 1]; + var code = tokens[j + 2]; + while(startRange < endRange) { + encodingMap[startRange] = code++; + ++startRange; + } } break; + case 'endcidchar': case 'endbfchar': for (var j = 0; j < tokens.length; j += 2) { - var index = parseInt('0x' + tokens[j]); - var code = parseInt('0x' + tokens[j + 1]); + var index = tokens[j]; + var code = tokens[j + 1]; encodingMap[index] = code; } break; + case '': + break; + default: - if (token.length) { - tokens.push(token); - token = ''; - } + if (token[0] >= '0' && token[0] <= '9') + token = parseInt(token, 10); // a number + tokens.push(token); + token = ''; break; } - } else if (byte == 0x5B || byte == 0x5D) { - error('CMAP list parsing is not implemented'); + switch (byte) { + case 0x5B: + // begin list parsing + tokens.push(beginArrayToken); + break; + case 0x5D: + // collect array items + var items = [], item; + while (tokens.length && (item = tokens.pop()) != beginArrayToken) { + items.unshift(item); + } + tokens.push(items); + break; + } + } else if (byte == 0x3E) { + if (token.length) { + // parsing hex number + tokens.push(parseInt(token, 16)); + token = ''; + } } else { token += String.fromCharCode(byte); } From 5326f262e09c5759c5cd8f0e5e621bcfcb43fd19 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Mon, 29 Aug 2011 20:59:38 -0500 Subject: [PATCH 4/5] an-open-web document outline and bookmarks --- pdf.js | 12 ++++++++---- web/viewer.js | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pdf.js b/pdf.js index 201744efe..bf0cf50c1 100644 --- a/pdf.js +++ b/pdf.js @@ -3541,6 +3541,11 @@ var Catalog = (function() { } }, get destinations() { + function fetchDestination(ref) { + var dest = xref.fetchIfRef(ref); + return IsDict(dest) ? dest.get('D') : dest; + } + var xref = this.xref; var dests = {}, nameTreeRef, nameDictionaryRef; var obj = this.catDict.get('Names'); @@ -3551,10 +3556,10 @@ var Catalog = (function() { if (nameDictionaryRef) { // reading simple destination dictionary - obj = xref.fetch(nameDictionaryRef); + obj = xref.fetchIfRef(nameDictionaryRef); obj.forEach(function(key, value) { if (!value) return; - dests[key] = xref.fetch(value).get('D'); + dests[key] = fetchDestination(value); }); } if (nameTreeRef) { @@ -3578,8 +3583,7 @@ var Catalog = (function() { } var names = obj.get('Names'); for (i = 0, n = names.length; i < n; i += 2) { - var dest = xref.fetch(names[i + 1]); - dests[names[i]] = IsDict(dest) ? dest.get('D') : dest; + dests[names[i]] = fetchDestination(names[i + 1]); } } } diff --git a/web/viewer.js b/web/viewer.js index 243afb7e5..b07408bc0 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -91,7 +91,8 @@ var PDFView = { return; // invalid destination // dest array looks like that: var destRef = dest[0]; - var pageNumber = this.pagesRefMap[destRef.num + ' ' + destRef.gen + ' R']; + var pageNumber = destRef instanceof Object ? + this.pagesRefMap[destRef.num + ' ' + destRef.gen + ' R'] : (destRef + 1); if (pageNumber) { this.page = pageNumber; // TODO scroll to specific region on the page, the precise scaling From cf487615cf3a4986b0ba3464c99e9b8a55971fde Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Mon, 29 Aug 2011 21:11:46 -0500 Subject: [PATCH 5/5] Fixing back-button/NaN-hash issue --- web/viewer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/viewer.js b/web/viewer.js index 243afb7e5..809bd5dda 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -42,7 +42,7 @@ var PDFView = { set page(val) { var pages = this.pages; var input = document.getElementById('pageNumber'); - if (val <= 0 || val > pages.length) { + if (!(0 < val && val <= pages.length)) { input.value = this.page; return; } @@ -58,7 +58,7 @@ var PDFView = { }, get page() { - return parseInt(document.location.hash.substring(1)); + return parseInt(document.location.hash.substring(1)) || 1; }, open: function(url, scale) {