From d30fac043856baa207f2786a971f9f63fe990450 Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Sun, 4 Sep 2011 19:46:06 -0500 Subject: [PATCH 1/4] Exact positioning of the bookmark --- web/viewer.js | 94 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/web/viewer.js b/web/viewer.js index a29ecf703..2490c39e9 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -8,6 +8,8 @@ var kDefaultScale = 1.5; var kDefaultScaleDelta = 1.1; var kCacheSize = 20; var kCssUnits = 96.0 / 72.0; +var kScrollbarPadding = 40; + var Cache = function(size) { var data = []; @@ -44,7 +46,7 @@ var PDFView = { window.dispatchEvent(event); }, - parseScale: function(value) { + parseScale: function(value, resetAutoSettings) { var scale = parseFloat(value); if (scale) { this.setScale(scale, true); @@ -54,15 +56,18 @@ var PDFView = { return; var currentPage = this.pages[this.page - 1]; - var scrollbarPadding = 40; - var pageWidthScale = (window.innerWidth - scrollbarPadding) / + var pageWidthScale = (window.innerWidth - kScrollbarPadding) / currentPage.width / kCssUnits; - var pageHeightScale = (window.innerHeight - scrollbarPadding) / + var pageHeightScale = (window.innerHeight - kScrollbarPadding) / currentPage.height / kCssUnits; if ('page-width' == value) - this.setScale(pageWidthScale); - else if ('page-fit' == value) - this.setScale(Math.min(pageWidthScale, pageHeightScale)); + this.setScale(pageWidthScale, resetAutoSettings); + if ('page-height' == value) + this.setScale(pageHeightScale, resetAutoSettings); + if ('page-fit' == value) { + this.setScale( + Math.min(pageWidthScale, pageHeightScale), resetAutoSettings); + } }, zoomIn: function() { @@ -129,8 +134,8 @@ var PDFView = { 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 - // required. + var currentPage = this.pages[pageNumber - 1]; + currentPage.scrollIntoView(dest); } }, @@ -266,11 +271,80 @@ var PageView = function(container, content, id, width, height, link.style.width = Math.ceil(links[i].width * scale) + 'px'; link.style.height = Math.ceil(links[i].height * scale) + 'px'; link.href = links[i].url || ''; - bindLink(link, ('dest' in links[i]) ? links[i].dest : null); + if (!links[i].url) + bindLink(link, ('dest' in links[i]) ? links[i].dest : null); div.appendChild(link); } } + this.scrollIntoView = function(dest) { + var x = 0, y = 0; + var width = 0, height = 0, widthScale, heightScale; + var scale = 0; + switch (dest[1].name) { + default: + return; + case 'XYZ': + x = dest[2]; + y = dest[3]; + scale = dest[4]; + break; + case 'Fit': + case 'FitB': + scale = 'page-fit'; + break; + case 'FitH': + case 'FitBH': + y = dest[2]; + scale = 'page-width'; + break; + case 'FitV': + case 'FitBV': + x = dest[2]; + scale = 'page-height'; + break; + case 'FitR': + x = dest[2]; + y = dest[3]; + width = dest[4] - x; + height = dest[5] - y; + widthScale = (window.innerWidth - kScrollbarPadding) / + width / kCssUnits; + heightScale = (window.innerHeight - kScrollbarPadding) / + height / kCssUnits; + scale = Math.min(widthScale, heightScale); + break; + } + + var boundingRect = [ + this.content.rotatePoint(x, y), + this.content.rotatePoint(x + width, y + height) + ]; + + if (scale) + PDFView.setScale(scale, true); + + setTimeout(function() { + // letting page to re-layout before scrolling + var scale = PDFView.currentScale; + var x = Math.min(boundingRect[0].x, boundingRect[1].x); + var y = Math.min(boundingRect[0].y, boundingRect[1].y); + var width = Math.abs(boundingRect[0].x - boundingRect[1].x); + var height = Math.abs(boundingRect[0].y - boundingRect[1].y); + + // using temporary div to scroll it into view + var tempDiv = document.createElement('div'); + tempDiv.style.position = 'absolute'; + tempDiv.style.left = Math.floor(x * scale) + 'px'; + tempDiv.style.top = Math.floor(y * scale) + 'px'; + tempDiv.style.width = Math.ceil(width * scale) + 'px'; + tempDiv.style.height = Math.ceil(height * scale) + 'px'; + div.appendChild(tempDiv); + tempDiv.scrollIntoView(true); + div.removeChild(tempDiv); + }, 0); + }; + this.draw = function() { if (div.hasChildNodes()) { this.updateStats(); From 80304b69761910a43a1281b042a424b237ee7681 Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Mon, 5 Sep 2011 23:11:48 +0300 Subject: [PATCH 2/4] Fix a bug and speed up graycs_getRgbBuffer. An rgbBuffer was created which was three times as big as intended. It also caused that the function was very slow when rendering cable.pdf. Which led to a slow script warning dialog. With this fix the function speeds up ten-fold in the firebug profile. --- pdf.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pdf.js b/pdf.js index 7e748cfd8..9a476ccef 100644 --- a/pdf.js +++ b/pdf.js @@ -5469,9 +5469,8 @@ var DeviceGrayCS = (function() { }, getRgbBuffer: function graycs_getRgbBuffer(input, bits) { var scale = 255 / ((1 << bits) - 1); - var length = input.length * 3; - var rgbBuf = new Uint8Array(length); - for (var i = 0, j = 0; i < length; ++i) { + var rgbBuf = new Uint8Array(input.length * 3); + for (var i = 0, j = 0; i < input.length; ++i) { var c = (scale * input[i]) | 0; rgbBuf[j++] = c; rgbBuf[j++] = c; From d4b8326496bdc65af50892df0cebe5ac897e7737 Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Tue, 6 Sep 2011 00:11:24 +0300 Subject: [PATCH 3/4] Fix a bug and speed up graycs_getRgbBuffer. An rgbBuffer was created which was three times as big as intended. It also caused that the function was very slow when rendering cable.pdf. Which led to a slow script warning dialog. With this fix the function speeds up fifteen-fold in the firebug profile. --- pdf.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pdf.js b/pdf.js index 9a476ccef..44197b4ab 100644 --- a/pdf.js +++ b/pdf.js @@ -5469,8 +5469,9 @@ var DeviceGrayCS = (function() { }, getRgbBuffer: function graycs_getRgbBuffer(input, bits) { var scale = 255 / ((1 << bits) - 1); - var rgbBuf = new Uint8Array(input.length * 3); - for (var i = 0, j = 0; i < input.length; ++i) { + var length = input.length; + var rgbBuf = new Uint8Array(length * 3); + for (var i = 0, j = 0; i < length; ++i) { var c = (scale * input[i]) | 0; rgbBuf[j++] = c; rgbBuf[j++] = c; From 3abe9095564ef0ba4996455a62e537d5f18c67fc Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Mon, 5 Sep 2011 22:19:20 -0500 Subject: [PATCH 4/4] Allow test.py serve files with spaces in the name --- test/test.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test.py b/test/test.py index d1e798d86..0d40efbc8 100644 --- a/test/test.py +++ b/test/test.py @@ -126,14 +126,15 @@ class PDFTestHandler(BaseHTTPRequestHandler): self.wfile.write("

PDFs of " + path + "

\n") for filename in os.listdir(location): if filename.lower().endswith('.pdf'): - self.wfile.write("" + + self.wfile.write("" + filename + "
\n") self.wfile.write("") def do_GET(self): url = urlparse(self.path) # Ignore query string - path, _ = url.path, url.query + path, _ = urllib.unquote_plus(url.path), url.query path = os.path.abspath(os.path.realpath(DOC_ROOT + os.sep + path)) prefix = os.path.commonprefix(( path, DOC_ROOT )) _, ext = os.path.splitext(path.lower())