Use Font Loading API if available

http://dev.w3.org/csswg/css-font-loading/
This commit is contained in:
Rob Wu 2014-09-30 21:24:31 +02:00
parent 6a230af332
commit d0845df971
2 changed files with 67 additions and 9 deletions

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
/* globals PDFJS, shadow, isWorker, assert, warn, bytesToString, string32,
globalScope */
globalScope, FontFace, Promise */
'use strict';
@ -40,6 +40,12 @@ var FontLoader = {
if (styleElement) {
styleElement.parentNode.removeChild(styleElement);
}
//#if !(MOZCENTRAL)
this.nativeFontFaces.forEach(function(nativeFontFace) {
document.fonts.delete(nativeFontFace);
});
this.nativeFontFaces.length = 0;
//#endif
},
//#if !(MOZCENTRAL)
get loadTestFont() {
@ -97,10 +103,21 @@ var FontLoader = {
return false;
})(),
nativeFontFaces: [],
isFontLoadingAPISupported: !isWorker && !!document.fonts,
addNativeFontFace: function fontLoader_addNativeFontFace(nativeFontFace) {
this.nativeFontFaces.push(nativeFontFace);
document.fonts.add(nativeFontFace);
},
bind: function fontLoaderBind(fonts, callback) {
assert(!isWorker, 'bind() shall be called from main thread');
var rules = [], fontsToLoad = [];
var rules = [];
var fontsToLoad = [];
var fontLoadPromises = [];
for (var i = 0, ii = fonts.length; i < ii; i++) {
var font = fonts[i];
@ -111,15 +128,26 @@ var FontLoader = {
}
font.attached = true;
if (this.isFontLoadingAPISupported) {
var nativeFontFace = font.createNativeFontFace();
if (nativeFontFace) {
fontLoadPromises.push(nativeFontFace.loaded);
}
} else {
var rule = font.bindDOM();
if (rule) {
rules.push(rule);
fontsToLoad.push(font);
}
}
}
var request = FontLoader.queueLoadingCallback(callback);
if (rules.length > 0 && !this.isSyncFontLoadingSupported) {
if (this.isFontLoadingAPISupported) {
Promise.all(fontsToLoad).then(function() {
request.complete();
});
} else if (rules.length > 0 && !this.isSyncFontLoadingSupported) {
FontLoader.prepareFontLoadEvent(rules, fontsToLoad, request);
} else {
request.complete();
@ -284,6 +312,29 @@ var FontFaceObject = (function FontFaceObjectClosure() {
}
}
FontFaceObject.prototype = {
//#if !(MOZCENTRAL)
createNativeFontFace: function FontFaceObject_createNativeFontFace() {
if (!this.data) {
return null;
}
if (PDFJS.disableFontFace) {
this.disableFontFace = true;
return null;
}
var nativeFontFace = new FontFace(this.loadedName, this.data);
FontLoader.addNativeFontFace(nativeFontFace);
if (PDFJS.pdfBug && 'FontInspector' in globalScope &&
globalScope['FontInspector'].enabled) {
globalScope['FontInspector'].fontAdded(this);
}
return nativeFontFace;
},
//#endif
bindDOM: function FontFaceObject_bindDOM() {
if (!this.data) {
return null;

View File

@ -112,13 +112,20 @@ var FontInspector = (function FontInspectorClosure() {
return moreInfo;
}
var moreInfo = properties(fontObj, ['name', 'type']);
var m = /url\(['"]?([^\)"']+)/.exec(url);
var fontName = fontObj.loadedName;
var font = document.createElement('div');
var name = document.createElement('span');
name.textContent = fontName;
var download = document.createElement('a');
download.href = m[1];
if (url) {
url = /url\(['"]?([^\)"']+)/.exec(url);
download.href = url[1];
} else if (fontObj.data) {
url = URL.createObjectURL(new Blob([fontObj.data], {
type: fontObj.mimeType
}));
}
download.href = url;
download.textContent = 'Download';
var logIt = document.createElement('a');
logIt.href = '';