Skip to content

Commit 818f799

Browse files
committed
work around chrome bug, and work around ff5 bug in the workaround
1 parent 7a306af commit 818f799

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

fonts.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ var Fonts = (function Fonts() {
124124
})();
125125

126126
var FontLoader = {
127+
listeningForFontLoad: false,
128+
127129
bind: function(fonts, callback) {
128130
function checkFontsLoaded() {
129131
for (var i = 0; i < fonts.length; i++) {
@@ -196,6 +198,9 @@ var FontLoader = {
196198
// goes really wonkily, we expect the @font-face for the outer
197199
// document to be processed before the inner. That's still
198200
// fragile, but seems to work in practice.
201+
//
202+
// The postMessage() hackery was added to work around chrome bug
203+
// 82402.
199204

200205
var div = document.createElement("div");
201206
div.setAttribute("style",
@@ -209,6 +214,28 @@ var FontLoader = {
209214
div.innerHTML = html;
210215
document.body.appendChild(div);
211216

217+
if (!this.listeneningForFontLoad) {
218+
window.addEventListener(
219+
"message",
220+
function(e) {
221+
var fontNames = e.data;
222+
// Firefox 5 doesn't parse the JSON here. Welcome to the
223+
// Wonderful Web World.
224+
if ("string" == typeof(fontNames)) {
225+
fontNames = fontNames.split(",");
226+
}
227+
for (var i = 0; i < fontNames.length; ++i) {
228+
var font = Fonts.lookup(fontNames[i]);
229+
font.loading = false;
230+
}
231+
var evt = document.createEvent("Events");
232+
evt.initEvent("pdfjsFontLoad", true, false);
233+
document.documentElement.dispatchEvent(evt);
234+
},
235+
false);
236+
this.listeneningForFontLoad = true;
237+
}
238+
212239
// XXX we should have a time-out here too, and maybe fire
213240
// pdfjsFontLoadFailed?
214241
var src = '<!DOCTYPE HTML><html><head>';
@@ -224,15 +251,7 @@ var FontLoader = {
224251
}
225252
src += ' var fontNames=['+ fontNamesArray +'];\n';
226253
src += ' window.onload = function () {\n'
227-
src += ' var Fonts = top.document.defaultView.Fonts;\n';
228-
src += ' for (var i = 0; i < fontNames.length; ++i) {\n';
229-
src += ' var font = Fonts.lookup(fontNames[i]);\n';
230-
src += ' font.loading = false;\n';
231-
src += ' }\n';
232-
src += ' var doc = top.document;\n';
233-
src += ' var evt = doc.createEvent("Events");\n';
234-
src += ' evt.initEvent("pdfjsFontLoad", true, false);\n'
235-
src += ' doc.documentElement.dispatchEvent(evt);\n';
254+
src += ' top.postMessage(fontNames, "*");\n';
236255
src += ' }';
237256
src += '</script></head><body>';
238257
for (var i = 0; i < names.length; ++i) {
@@ -1117,6 +1136,8 @@ var Font = (function () {
11171136
var rule = "@font-face { font-family:'" + fontName + "';src:" + url + "}";
11181137
var styleSheet = document.styleSheets[0];
11191138
styleSheet.insertRule(rule, styleSheet.cssRules.length);
1139+
1140+
return rule;
11201141
}
11211142
};
11221143

0 commit comments

Comments
 (0)