Update the multi_page_viewer.js code to work with the new FontLoader API

This commit is contained in:
Vivien Nicolas 2011-06-24 21:46:48 +02:00
parent 66dbc718b0
commit 83fabc49c2

View File

@ -3,6 +3,8 @@
"use strict"; "use strict";
var pageTimeout;
var PDFViewer = { var PDFViewer = {
queryParams: {}, queryParams: {},
@ -75,81 +77,45 @@ var PDFViewer = {
}, },
drawPage: function(num) { drawPage: function(num) {
if (!PDFViewer.pdf) { if (!PDFViewer.pdf)
return; return;
}
var div = document.getElementById('pageContainer' + num); var div = document.getElementById('pageContainer' + num);
var canvas = document.createElement('canvas'); var canvas = document.createElement('canvas');
if (div && !div.hasChildNodes()) { if (div && !div.hasChildNodes()) {
div.appendChild(canvas);
var page = PDFViewer.pdf.getPage(num); var page = PDFViewer.pdf.getPage(num);
canvas.id = 'page' + num; canvas.id = 'page' + num;
canvas.mozOpaque = true; canvas.mozOpaque = true;
// Canvas dimensions must be specified in CSS pixels. CSS pixels // Canvas dimensions must be specified in CSS pixels. CSS pixels
// are always 96 dpi. These dimensions are 8.5in x 11in at 96dpi. // are always 96 dpi. These dimensions are 8.5in x 11in at 96dpi.
canvas.width = PDFViewer.pageWidth(); canvas.width = PDFViewer.pageWidth();
canvas.height = PDFViewer.pageHeight(); canvas.height = PDFViewer.pageHeight();
div.appendChild(canvas);
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
ctx.save(); ctx.save();
ctx.fillStyle = 'rgb(255, 255, 255)'; ctx.fillStyle = 'rgb(255, 255, 255)';
ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore(); ctx.restore();
var gfx = new CanvasGraphics(ctx); var gfx = new CanvasGraphics(ctx);
var fonts = [];
// page.compile will collect all fonts for us, once we have loaded them // page.compile will collect all fonts for us, once we have loaded them
// we can trigger the actual page rendering with page.display // we can trigger the actual page rendering with page.display
var fonts = [];
page.compile(gfx, fonts); page.compile(gfx, fonts);
var areFontsReady = true; var loadFont = function() {
if (!FontsLoader.bind(fonts)) {
// Inspect fonts and translate the missing one pageTimeout = window.setTimeout(loadFont, 10);
var fontCount = fonts.length; return;
for (var i = 0; i < fontCount; i++) {
var font = fonts[i];
if (Fonts[font.name]) {
areFontsReady = areFontsReady && !Fonts[font.name].loading;
continue;
} }
page.display(gfx);
new Font(font.name, font.file, font.properties);
areFontsReady = false;
} }
loadFont();
var pageInterval;
var delayLoadFont = function() {
for (var i = 0; i < fontCount; i++) {
if (Fonts[font.name].loading) {
return;
}
}
clearInterval(pageInterval);
while (div.hasChildNodes()) {
div.removeChild(div.firstChild);
}
PDFViewer.drawPage(num);
}
if (!areFontsReady) {
pageInterval = setInterval(delayLoadFont, 10);
return;
}
page.display(gfx);
} }
}, },
@ -258,7 +224,6 @@ var PDFViewer = {
}; };
window.onload = function() { window.onload = function() {
// Parse the URL query parameters into a cached object. // Parse the URL query parameters into a cached object.
PDFViewer.queryParams = function() { PDFViewer.queryParams = function() {
var qs = window.location.search.substring(1); var qs = window.location.search.substring(1);