Merge pull request #18 from vingtetun/master

Do not assume charset is defined and move some fonts timing to the right place
This commit is contained in:
Chris Jones 2011-06-17 12:07:28 -07:00
commit 3309bc8ceb
2 changed files with 75 additions and 57 deletions

77
pdf.js
View File

@ -1729,52 +1729,57 @@ var CanvasGraphics = (function() {
var fontName = descriptor.get("FontName").name; var fontName = descriptor.get("FontName").name;
fontName = fontName.replace("+", "_"); fontName = fontName.replace("+", "_");
var font = Fonts[fontName]; var fontFile = descriptor.get2("FontFile", "FontFile2");
if (!font) { if (!fontFile)
var fontFile = descriptor.get2("FontFile", "FontFile2"); errort("FontFile not found for font: " + fontName);
fontFile = xref.fetchIfRef(fontFile); fontFile = xref.fetchIfRef(fontFile);
// Generate the custom cmap of the font if needed // Generate the custom cmap of the font if needed
var encodingMap = {}; var encodingMap = {};
if (fontDict.has("Encoding")) { if (fontDict.has("Encoding")) {
var encoding = xref.fetchIfRef(fontDict.get("Encoding"));
if (IsDict(encoding)) {
// Build an map between codes and glyphs
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;
}
var encoding = xref.fetchIfRef(fontDict.get("Encoding")); // Get the font charset if any
if (IsDict(encoding)) { var charset = descriptor.get("CharSet");
// Build an map between codes and glyphs if (charset)
var differences = encoding.get("Differences"); charset = charset.split("/");
var index = 0;
for (var j = 0; j < differences.length; j++) {
var data = differences[j];
IsNum(data) ? index = data : encodingMap[index++] = data;
}
// Get the font charset } else if (IsName(encoding)) {
var charset = descriptor.get("CharSet").split("/"); var encoding = Encodings[encoding];
var widths = xref.fetchIfRef(fontDict.get("Widths"));
var firstchar = xref.fetchIfRef(fontDict.get("FirstChar"));
} else if (IsName(encoding)) { var charset = [];
var encoding = Encodings[encoding]; for (var j = 0; j < widths.length; j++) {
var widths = xref.fetchIfRef(fontDict.get("Widths")); var index = widths[j];
var firstchar = xref.fetchIfRef(fontDict.get("FirstChar")); if (!index)
continue;
var charset = []; charset.push(encoding[j + firstchar]);
for (var j = 0; j < widths.length; j++) { }
var index = widths[j]; }
if (!index) }
continue;
charset.push(encoding[j + firstchar]);
}
}
}
var properties = { var properties = {
type: fontDict.get("Subtype").name, type: fontDict.get("Subtype").name,
encoding: encodingMap, encoding: encodingMap,
charset: charset, charset: charset,
bbox: descriptor.get("FontBBox") bbox: descriptor.get("FontBBox")
}; };
new Font(fontName, fontFile, properties);
} return {
return Fonts[fontName]; name: fontName,
file: fontFile,
properties: properties
}
}, },
beginDrawing: function(mediaBox) { beginDrawing: function(mediaBox) {

55
test.js
View File

@ -74,30 +74,43 @@ function displayPage(num) {
page.compile(gfx, fonts); page.compile(gfx, fonts);
var t2 = Date.now(); var t2 = Date.now();
var interval = 0; var fontsReady = true;
for (var i = 0; i < fonts.length; i++) {
if (fonts[i].loading) { // Inspect fonts and translate the missing one
interval = 10; var count = fonts.length;
break; for (var i = 0; i < count; i++) {
var font = fonts[i];
if (Fonts[font.name]) {
fontsReady = fontsReady && !Fonts[font.name].loading;
continue;
} }
new Font(font.name, font.file, font.properties);
fontsReady = false;
}
function delayLoadFont() {
for (var i = 0; i < count; i++) {
if (Fonts[font.name].loading)
return;
}
clearInterval(pageInterval);
var t3 = Date.now();
page.display(gfx);
var t4 = Date.now();
var infoDisplay = document.getElementById("info");
infoDisplay.innerHTML = "Time to load/compile/fonts/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + "/" + (t4 - t3) + " ms";
}; };
// FIXME This need to be replaced by an event if (fontsReady) {
pageInterval = setInterval(function() { delayLoadFont();
for (var i = 0; i < fonts.length; i++) { } else {
if (fonts[i].loading) pageInterval = setInterval(delayLoadFont, 10);
return; }
}
var t3 = Date.now();
clearInterval(pageInterval);
page.display(gfx);
var t4 = Date.now();
var infoDisplay = document.getElementById("info");
infoDisplay.innerHTML = "Time to load/compile/fonts/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + "/" + (t4 - t3) + " ms";
}, interval);
} }
function nextPage() { function nextPage() {