Add XObjects fonts to the list of page fonts and has a forEach method to the Dict class

This commit is contained in:
Vivien Nicolas 2011-06-14 21:38:59 +02:00
parent 3625ac93a3
commit 363071aab9
3 changed files with 64 additions and 43 deletions

View File

@ -580,9 +580,22 @@ var FontsUtils = {
* and valid in the point of view of the sanitizer. * and valid in the point of view of the sanitizer.
*/ */
var TrueType = function(aFile) { var TrueType = function(aFile) {
var header = this._readOpenTypeHeader(aFile);
this.data = aFile; this.data = aFile;
}; };
TrueType.prototype = {
_readOpenTypeHeader: function(aFile) {
return {
version: aFile.getBytes(4),
numTables: FontsUtils.bytesToInteger(aFile.getBytes(2)),
searchRange: FontsUtils.bytesToInteger(aFile.getBytes(2)),
entrySelector: FontsUtils.bytesToInteger(aFile.getBytes(2)),
rangeShift: FontsUtils.bytesToInteger(aFile.getBytes(2))
}
}
};
/** /**
* This dictionary holds decoded fonts data. * This dictionary holds decoded fonts data.
*/ */

34
pdf.js
View File

@ -563,6 +563,10 @@ var Dict = (function() {
set: function(key, value) { set: function(key, value) {
this.map[key] = value; this.map[key] = value;
}, },
forEach: function(aCallback) {
for (var key in this.map)
aCallback(key, this.map[key]);
},
toString: function() { toString: function() {
var keys = []; var keys = [];
for (var key in this.map) for (var key in this.map)
@ -1404,16 +1408,30 @@ var Page = (function() {
}, },
get fonts() { get fonts() {
var xref = this.xref; var xref = this.xref;
var fonts = [];
var resources = xref.fetchIfRef(this.resources); var resources = xref.fetchIfRef(this.resources);
var fontResource = resources.get("Font"); var fontsDict = new Dict();
for (var id in fontResource.map) {
var res = xref.fetch(fontResource.get(id)); // Get the fonts use on the page
var descriptor = xref.fetch(res.get("FontDescriptor")); var fontResources = resources.get("Font");
fonts.push(descriptor.get("FontName").toString()); fontResources.forEach(function(fontKey, fontData) {
fontsDict.set(fontKey, xref.fetch(fontData))
});
// Get the fonts use on xobjects of the page if any
var xobjs = xref.fetchIfRef(resources.get("XObject"));
if (xobjs) {
xobjs.forEach(function(key, xobj) {
xobj = xref.fetchIfRef(xobj);
assertWellFormed(IsStream(xobj), "XObject should be a stream");
var xobjFonts = xobj.dict.get("Resources").get("Font");
xobjFonts.forEach(function(fontKey, fontData) {
fontsDict.set(fontKey, xref.fetch(fontData))
});
});
} }
return shadow(this, "fonts", fonts);
return shadow(this, "fonts", fontsDict);
}, },
display: function(gfx) { display: function(gfx) {
var xref = this.xref; var xref = this.xref;

60
test.js
View File

@ -76,47 +76,37 @@ function displayPage(num) {
// of the page to be fully loaded before loading the page // of the page to be fully loaded before loading the page
var fontsReady = true; var fontsReady = true;
var fonts = page.fonts; var fonts = page.fonts;
for (var i = 0; i < fonts.length; i++) { var xref = page.xref;
var fontName = fonts[i].replace("+", "_"); fonts.forEach(function(fontKey, fontDict) {
var font = Fonts[fontName]; var descriptor = xref.fetch(fontDict.get("FontDescriptor"));
if (!font) { var fontName = descriptor.get("FontName").name;
// load the new font fontName = fontName.replace("+", "_");
var xref = page.xref;
var resources = xref.fetchIfRef(page.resources);
var fontResource = resources.get("Font");
for (var id in fontResource.map) {
var res = xref.fetch(fontResource.get(id));
var descriptor = xref.fetch(res.get("FontDescriptor"));
var name = descriptor.get("FontName").toString();
if (name == fontName.replace("_", "+")) {
var subtype = res.get("Subtype").name;
var fontFile = page.xref.fetchIfRef(descriptor.get("FontFile"));
if (!fontFile)
fontFile = page.xref.fetchIfRef(descriptor.get("FontFile2"));
// Generate the custom cmap of the font // Check if the font has been loaded or is still loading
var encoding = xref.fetch(res.get("Encoding")); var font = Fonts[fontName];
var differences = encoding.get("Differences"); if (!font) {
var fontFile = xref.fetchIfRef(descriptor.get2("FontFile", "FontFile2"));
// Generate the custom cmap of the font if needed
var encodingMap = {}; var encodingMap = {};
var index = 0; if (fontDict.has("Encoding")) {
for (var j = 0; j < differences.length; j++) { var encoding = xref.fetchIfRef(fontDict.get("Encoding"));
var data = differences[j]; if (IsDict(encoding)) {
if (IsNum(data)) var differences = encoding.get("Differences");
index = data; var index = 0;
else for (var j = 0; j < differences.length; j++) {
encodingMap[index++] = data; var data = differences[j];
IsNum(data) ? index = data : encodingMap[index++] = data;
}
}
} }
var subtype = fontDict.get("Subtype").name;
new Font(fontName, fontFile, encodingMap, subtype); new Font(fontName, fontFile, encodingMap, subtype);
fontsReady = false; return fontsReady = false;
break; } else if (font.loading) {
} return fontsReady = false;
} }
} else if (font.loading) { });
fontsReady = false;
break;
}
}
// If everything is ready do not delayed the page loading any more // If everything is ready do not delayed the page loading any more
if (fontsReady) if (fontsReady)