var Fonts = (function Fonts() {
var kScalePrecision = 40;
- var fonts = Object.create(null);
+ var fonts = [];
if (!isWorker) {
var ctx = document.createElement('canvas').getContext('2d');
ctx.scale(1 / kScalePrecision, 1);
}
- function Font(name, data, properties) {
+ function FontInfo(name, data, properties, id) {
this.name = name;
this.data = data;
this.properties = properties;
+ this.id = id;
this.loading = true;
this.charsCache = Object.create(null);
this.sizes = [];
var charsCache;
var measureCache;
+ var fontCount = 0;
+
return {
registerFont: function fonts_registerFont(fontName, data, properties) {
- fonts[fontName] = new Font(fontName, data, properties);
+ fonts.push(new FontInfo(fontName, data, properties, fontCount));
+ return fontCount++;
},
blacklistFont: function fonts_blacklistFont(fontName) {
- registerFont(fontName, null, {});
+ var id = registerFont(fontName, null, {});
markLoaded(fontName);
+ return id;
},
- lookup: function fonts_lookup(fontName) {
- return fonts[fontName];
+ lookupById: function fonts_lookupById(id) {
+ return fonts[id];
},
- setActive: function fonts_setActive(fontName, size) {
+ setActive: function fonts_setActive(font, size) {
// |current| can be null is fontName is a built-in font
// (e.g. "sans-serif")
- if ((current = fonts[fontName])) {
+ if ((current = fonts[font.id])) {
charsCache = current.charsCache;
var sizes = current.sizes;
if (!(measureCache = sizes[size]))
measureCache = sizes[size] = Object.create(null);
}
- ctx.font = (size * kScalePrecision) + 'px "' + fontName + '"';
+ ctx.font = (size * kScalePrecision) + 'px "' + font.loadedName + '"';
},
charsToUnicode: function fonts_chars2Unicode(chars) {
if (!charsCache)
bind: function(fonts, callback) {
function checkFontsLoaded() {
for (var i = 0; i < fonts.length; i++) {
- var font = fonts[i];
- if (Fonts.lookup(font.name).loading) {
+ var id = fonts[i].fontDict.fontObj.id;
+ if (Fonts.lookupById(id).loading) {
return false;
}
}
return true;
}
- var rules = [], names = [];
+ var rules = [], names = [], ids = [];
for (var i = 0; i < fonts.length; i++) {
var font = fonts[i];
- if (!Fonts.lookup(font.name)) {
- var obj = new Font(font.name, font.file, font.properties);
-
- var str = '';
- var data = Fonts.lookup(font.name).data;
- var length = data.length;
- for (var j = 0; j < length; j++)
- str += String.fromCharCode(data[j]);
-
- var rule = isWorker ? obj.bindWorker(str) : obj.bindDOM(str);
- if (rule) {
- rules.push(rule);
- names.push(font.name);
- }
+
+ var obj = new Font(font.name, font.file, font.properties);
+ font.fontDict.fontObj = obj;
+
+ var str = '';
+ var data = Fonts.lookupById(obj.id).data;
+ var length = data.length;
+ for (var j = 0; j < length; j++)
+ str += String.fromCharCode(data[j]);
+
+ var rule = isWorker ? obj.bindWorker(str) : obj.bindDOM(str);
+ if (rule) {
+ rules.push(rule);
+ names.push(obj.loadedName);
+ ids.push(obj.id);
}
}
if (!isWorker && rules.length) {
- FontLoader.prepareFontLoadEvent(rules, names);
+ FontLoader.prepareFontLoadEvent(rules, names, ids);
}
if (!checkFontsLoaded()) {
// loaded in a subdocument. It's expected that the load of |rules|
// has already started in this (outer) document, so that they should
// be ordered before the load in the subdocument.
- prepareFontLoadEvent: function(rules, names) {
+ prepareFontLoadEvent: function(rules, names, ids) {
/** Hack begin */
// There's no event when a font has finished downloading so the
// following code is a dirty hack to 'guess' when a font is
function(e) {
var fontNames = JSON.parse(e.data);
for (var i = 0; i < fontNames.length; ++i) {
- var font = Fonts.lookup(fontNames[i]);
+ var font = Fonts.lookupById(ids[i]);
font.loading = false;
}
var evt = document.createEvent('Events');
this.encoding = properties.encoding;
// If the font has already been decoded simply return it
- if (Fonts.lookup(name)) {
- this.font = Fonts.lookup(name).data;
- return;
- }
+ //if (Fonts.lookup(name)) {
+ // this.font = Fonts.lookup(name).data;
+ // return;
+ //}
// If the font is to be ignored, register it like an already loaded font
// to avoid the cost of waiting for it be be loaded by the platform.
if (properties.ignore) {
- Fonts.blacklistFont(name);
+ this.id = Fonts.blacklistFont(name);
+ this.loadedName = 'pdfFont' + this.id;
return;
}
break;
}
this.data = data;
- Fonts.registerFont(name, data, properties);
+
+ this.id = Fonts.registerFont(name, data, properties);
+ this.loadedName = 'pdfFont' + this.id;
};
function stringToArray(str) {
},
bindDOM: function font_bindDom(data) {
- var fontName = this.name;
+ var fontName = this.loadedName;
// Add the font-face rule to the document
var url = ('url(data:' + this.mimetype + ';base64,' +
return {
name: fontName,
- file: fontFile,
- properties: properties
- };
+ fontDict: fontDict,
+ file: fontFile,
+ properties: properties
+ };
},
beginDrawing: function(mediaBox) {
var font = xref.fetchIfRef(fontRes.get(args[0].name));
assertWellFormed(IsDict(font));
if (!font.translated) {
+ // sbarman marker
font.translated = this.translateFont(font, xref, resources);
if (fonts && font.translated) {
// keep track of each font we translated so the caller can
return;
var fontName = '';
- var fontDescriptor = font.get('FontDescriptor');
- if (fontDescriptor && fontDescriptor.num) {
- var fontDescriptor = this.xref.fetchIfRef(fontDescriptor);
- fontName = fontDescriptor.get('FontName').name.replace('+', '_');
- }
+ var fontObj = font.fontObj;
+ if (fontObj)
+ fontName = fontObj.loadedName;
if (!fontName) {
// TODO: fontDescriptor is not available, fallback to default font
fontName = 'sans-serif';
}
- this.current.fontName = fontName;
+ this.current.font = fontObj;
this.current.fontSize = size;
if (this.ctx.$setFont) {
this.ctx.$setFont(fontName, size);
} else {
this.ctx.font = size + 'px "' + fontName + '"';
- Fonts.setActive(fontName, size);
+ Fonts.setActive(font, size);
}
},
setTextRenderingMode: function(mode) {
text = Fonts.charsToUnicode(text);
this.ctx.translate(this.current.x, -1 * this.current.y);
- var font = Fonts.lookup(this.current.fontName);
+ var font = Fonts.lookupById(this.current.font.id);
if (font && font.properties.textMatrix)
this.ctx.transform.apply(this.ctx, font.properties.textMatrix);