pdf.js/web/viewer.js

100 lines
2.8 KiB
JavaScript
Raw Normal View History

2011-07-06 15:06:45 +09:00
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
2011-07-06 15:06:45 +09:00
'use strict';
2011-06-19 09:35:28 +09:00
var pdfDocument, canvas, pageScale, pageDisplay, pageNum, numPages;
2011-06-17 16:06:19 +09:00
function load(userInput) {
2011-07-06 15:06:45 +09:00
canvas = document.getElementById('canvas');
canvas.mozOpaque = true;
pageNum = ('page' in queryParams()) ? parseInt(queryParams().page) : 1;
pageScale = ('scale' in queryParams()) ? parseInt(queryParams().scale) : 1.5;
var fileName = userInput;
if (!userInput) {
fileName = queryParams().file || 'compressed.tracemonkey-pldi-09.pdf';
}
open(fileName);
}
2011-06-14 02:16:03 +09:00
function queryParams() {
2011-07-06 15:06:45 +09:00
var qs = window.location.search.substring(1);
var kvs = qs.split('&');
var params = { };
for (var i = 0; i < kvs.length; ++i) {
var kv = kvs[i].split('=');
params[unescape(kv[0])] = unescape(kv[1]);
}
return params;
2011-06-14 02:16:03 +09:00
}
function open(url) {
2011-07-06 15:06:45 +09:00
document.title = url;
var req = new XMLHttpRequest();
req.open('GET', url);
req.mozResponseType = req.responseType = 'arraybuffer';
req.expected = (document.URL.indexOf('file:') == 0) ? 0 : 200;
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == req.expected) {
var data = (req.mozResponseArrayBuffer || req.mozResponse ||
req.responseArrayBuffer || req.response);
pdfDocument = new PDFDoc(new Stream(data));
numPages = pdfDocument.numPages;
document.getElementById('numPages').innerHTML = numPages.toString();
goToPage(pageNum);
}
};
req.send(null);
}
2011-06-14 02:16:03 +09:00
function gotoPage(num) {
2011-07-06 15:06:45 +09:00
if (0 <= num && num <= numPages)
pageNum = num;
displayPage(pageNum);
2011-06-14 02:16:03 +09:00
}
function displayPage(num) {
2011-07-06 15:06:45 +09:00
document.getElementById('pageNumber').value = num;
2011-07-06 15:06:45 +09:00
var t0 = Date.now();
2011-07-06 15:06:45 +09:00
var page = pdfDocument.getPage(pageNum = num);
2011-07-06 15:06:45 +09:00
var pdfToCssUnitsCoef = 96.0 / 72.0;
var pageWidth = (page.mediaBox[2] - page.mediaBox[0]);
var pageHeight = (page.mediaBox[3] - page.mediaBox[1]);
canvas.width = pageScale * pageWidth * pdfToCssUnitsCoef;
canvas.height = pageScale * pageHeight * pdfToCssUnitsCoef;
2011-06-26 23:15:33 +09:00
2011-07-06 15:06:45 +09:00
var t1 = Date.now();
var ctx = canvas.getContext('2d');
ctx.save();
ctx.fillStyle = 'rgb(255, 255, 255)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
page.startRendering(
ctx,
function() {
var infoDisplay = document.getElementById('info');
var stats = page.stats;
var t2 = stats.compile, t3 = stats.fonts, t4 = stats.render;
infoDisplay.innerHTML = 'Time to load/compile/fonts/render: ' +
(t1 - t0) + '/' + (t2 - t1) + '/' + (t3 - t2) + '/' + (t4 - t3) + ' ms';
});
}
function nextPage() {
2011-07-06 15:06:45 +09:00
if (pageNum < pdfDocument.numPages)
displayPage(++pageNum);
}
function prevPage() {
2011-07-06 15:06:45 +09:00
if (pageNum > 1)
displayPage(--pageNum);
}
2011-06-14 02:16:03 +09:00
function goToPage(num) {
if (0 <= num && num <= numPages)
displayPage(pageNum = num);
}