2011-05-26 23:02:52 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- /
|
|
|
|
/* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */
|
|
|
|
|
2011-06-14 11:52:21 +09:00
|
|
|
var pdfDocument, canvas, pageDisplay, pageNum, pageTimeout;
|
2011-05-26 23:02:52 +09:00
|
|
|
function load() {
|
|
|
|
canvas = document.getElementById("canvas");
|
|
|
|
canvas.mozOpaque = true;
|
2011-06-14 02:16:03 +09:00
|
|
|
pageNum = parseInt(queryParams().page) || 1;
|
|
|
|
fileName = queryParams().file || "compressed.tracemonkey-pldi-09.pdf";
|
|
|
|
open(fileName);
|
2011-05-26 23:02:52 +09:00
|
|
|
}
|
|
|
|
|
2011-06-14 02:16:03 +09:00
|
|
|
function queryParams() {
|
|
|
|
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-05-26 23:02:52 +09:00
|
|
|
function open(url) {
|
|
|
|
document.title = url;
|
|
|
|
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));
|
2011-06-14 02:16:03 +09:00
|
|
|
numPages = pdfDocument.numPages;
|
|
|
|
document.getElementById("numPages").innerHTML = numPages.toString();
|
|
|
|
goToPage(pageNum);
|
2011-05-26 23:02:52 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
req.send(null);
|
|
|
|
}
|
|
|
|
|
2011-06-14 02:16:03 +09:00
|
|
|
function gotoPage(num) {
|
|
|
|
if (0 <= num && num <= numPages)
|
|
|
|
pageNum = num;
|
|
|
|
displayPage(pageNum);
|
|
|
|
}
|
|
|
|
|
2011-05-26 23:02:52 +09:00
|
|
|
function displayPage(num) {
|
2011-06-14 11:52:21 +09:00
|
|
|
if (pageNum != num)
|
|
|
|
window.clearTimeout(pageTimeout);
|
|
|
|
|
2011-05-26 23:02:52 +09:00
|
|
|
document.getElementById("pageNumber").value = num;
|
|
|
|
|
|
|
|
var t0 = Date.now();
|
|
|
|
|
|
|
|
var page = pdfDocument.getPage(pageNum = num);
|
|
|
|
|
2011-06-14 11:35:46 +09:00
|
|
|
function display() {
|
|
|
|
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();
|
2011-05-26 23:02:52 +09:00
|
|
|
|
2011-06-14 11:35:46 +09:00
|
|
|
var gfx = new CanvasGraphics(ctx);
|
|
|
|
page.display(gfx);
|
2011-05-26 23:02:52 +09:00
|
|
|
|
2011-06-14 11:35:46 +09:00
|
|
|
var t2 = Date.now();
|
|
|
|
var infoDisplay = document.getElementById("info");
|
|
|
|
infoDisplay.innerHTML = "Time to render: "+ (t1 - t0) + "/" + (t2 - t1) + " ms";
|
|
|
|
}
|
2011-05-26 23:02:52 +09:00
|
|
|
|
2011-06-14 11:35:46 +09:00
|
|
|
// Loading a font via data uri is asynchronous, so wait for all font
|
|
|
|
// of the page to be fully loaded before loading the page
|
|
|
|
var fontsReady = true;
|
|
|
|
var fonts = page.fonts;
|
2011-06-15 04:38:59 +09:00
|
|
|
var xref = page.xref;
|
|
|
|
fonts.forEach(function(fontKey, fontDict) {
|
|
|
|
var descriptor = xref.fetch(fontDict.get("FontDescriptor"));
|
|
|
|
var fontName = descriptor.get("FontName").name;
|
|
|
|
fontName = fontName.replace("+", "_");
|
|
|
|
|
|
|
|
// Check if the font has been loaded or is still loading
|
|
|
|
var font = Fonts[fontName];
|
|
|
|
if (!font) {
|
2011-06-15 11:46:48 +09:00
|
|
|
var fontFile = descriptor.get2("FontFile", "FontFile2");
|
|
|
|
fontFile = xref.fetchIfRef(fontFile);
|
|
|
|
|
2011-06-15 04:38:59 +09:00
|
|
|
// Generate the custom cmap of the font if needed
|
2011-06-14 18:08:08 +09:00
|
|
|
var encodingMap = {};
|
2011-06-15 04:38:59 +09:00
|
|
|
if (fontDict.has("Encoding")) {
|
|
|
|
var encoding = xref.fetchIfRef(fontDict.get("Encoding"));
|
|
|
|
if (IsDict(encoding)) {
|
2011-06-15 12:40:54 +09:00
|
|
|
|
|
|
|
// Build an map between codes and glyphs
|
2011-06-15 04:38:59 +09:00
|
|
|
var differences = encoding.get("Differences");
|
|
|
|
var index = 0;
|
|
|
|
for (var j = 0; j < differences.length; j++) {
|
|
|
|
var data = differences[j];
|
|
|
|
IsNum(data) ? index = data : encodingMap[index++] = data;
|
|
|
|
}
|
2011-06-15 12:40:54 +09:00
|
|
|
|
|
|
|
// Get the font charset
|
|
|
|
var charset = descriptor.get("CharSet").split("/");
|
|
|
|
|
|
|
|
} else if (IsName(encoding)) {
|
|
|
|
var encoding = Encodings[encoding];
|
|
|
|
var widths = xref.fetchIfRef(fontDict.get("Widths"));
|
|
|
|
var firstchar = xref.fetchIfRef(fontDict.get("FirstChar"));
|
|
|
|
|
|
|
|
var charset = [];
|
|
|
|
for (var j = 0; j < widths.length; j++) {
|
|
|
|
var index = widths[j];
|
|
|
|
if (index)
|
|
|
|
charset.push(encoding[j + firstchar]);
|
|
|
|
}
|
2011-06-15 04:38:59 +09:00
|
|
|
}
|
2011-06-14 18:08:08 +09:00
|
|
|
}
|
|
|
|
|
2011-06-16 06:02:30 +09:00
|
|
|
var fontBBox = descriptor.get("FontBBox");
|
|
|
|
|
2011-06-15 04:38:59 +09:00
|
|
|
var subtype = fontDict.get("Subtype").name;
|
2011-06-16 06:02:30 +09:00
|
|
|
new Font(fontName, fontFile, encodingMap, charset, fontBBox, subtype);
|
2011-06-15 04:38:59 +09:00
|
|
|
return fontsReady = false;
|
|
|
|
} else if (font.loading) {
|
|
|
|
return fontsReady = false;
|
2011-06-14 11:35:46 +09:00
|
|
|
}
|
2011-06-15 04:38:59 +09:00
|
|
|
});
|
2011-05-26 23:02:52 +09:00
|
|
|
|
2011-06-14 11:35:46 +09:00
|
|
|
// If everything is ready do not delayed the page loading any more
|
|
|
|
if (fontsReady)
|
|
|
|
display();
|
2011-06-14 11:52:21 +09:00
|
|
|
else {
|
|
|
|
// FIXME Relying on an event seems much more cleaner here instead
|
|
|
|
// of a setTimeout...
|
|
|
|
pageTimeout = window.setTimeout(displayPage, 150, num);
|
|
|
|
}
|
2011-05-26 23:02:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function nextPage() {
|
|
|
|
if (pageNum < pdfDocument.numPages)
|
|
|
|
displayPage(++pageNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
function prevPage() {
|
|
|
|
if (pageNum > 1)
|
|
|
|
displayPage(--pageNum);
|
|
|
|
}
|
|
|
|
|
2011-06-14 02:16:03 +09:00
|
|
|
function goToPage(num) {
|
2011-05-26 23:02:52 +09:00
|
|
|
if (0 <= num && num <= numPages)
|
|
|
|
displayPage(pageNum = num);
|
|
|
|
}
|
|
|
|
|