Browse Source

Merge pull request #5366 from Rob--W/use-font-loader-api

Use font loader api
Yury Delendik 11 years ago
parent
commit
1858fbfe82
  1. 7
      src/display/api.js
  2. 69
      src/display/font_loader.js
  3. 11
      web/debugger.js

7
src/display/api.js

@ -17,8 +17,9 @@
/* globals PDFJS, isArrayBuffer, error, combineUrl, createPromiseCapability, /* globals PDFJS, isArrayBuffer, error, combineUrl, createPromiseCapability,
StatTimer, globalScope, MessageHandler, info, FontLoader, Util, warn, StatTimer, globalScope, MessageHandler, info, FontLoader, Util, warn,
Promise, PasswordResponses, PasswordException, InvalidPDFException, Promise, PasswordResponses, PasswordException, InvalidPDFException,
MissingPDFException, UnknownErrorException, FontFace, loadJpegStream, MissingPDFException, UnknownErrorException, FontFaceObject,
createScratchCanvas, CanvasGraphics, UnexpectedResponseException */ loadJpegStream, createScratchCanvas, CanvasGraphics,
UnexpectedResponseException */
'use strict'; 'use strict';
@ -962,7 +963,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
this.commonObjs.resolve(id, error); this.commonObjs.resolve(id, error);
break; break;
} else { } else {
font = new FontFace(exportedData); font = new FontFaceObject(exportedData);
} }
FontLoader.bind( FontLoader.bind(

69
src/display/font_loader.js

@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
/* globals PDFJS, shadow, isWorker, assert, warn, bytesToString, string32, /* globals PDFJS, shadow, isWorker, assert, warn, bytesToString, string32,
globalScope */ globalScope, FontFace, Promise */
'use strict'; 'use strict';
@ -40,6 +40,12 @@ var FontLoader = {
if (styleElement) { if (styleElement) {
styleElement.parentNode.removeChild(styleElement); styleElement.parentNode.removeChild(styleElement);
} }
//#if !(MOZCENTRAL)
this.nativeFontFaces.forEach(function(nativeFontFace) {
document.fonts.delete(nativeFontFace);
});
this.nativeFontFaces.length = 0;
//#endif
}, },
//#if !(MOZCENTRAL) //#if !(MOZCENTRAL)
get loadTestFont() { get loadTestFont() {
@ -97,10 +103,21 @@ var FontLoader = {
return false; 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) { bind: function fontLoaderBind(fonts, callback) {
assert(!isWorker, 'bind() shall be called from main thread'); 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++) { for (var i = 0, ii = fonts.length; i < ii; i++) {
var font = fonts[i]; var font = fonts[i];
@ -111,15 +128,26 @@ var FontLoader = {
} }
font.attached = true; font.attached = true;
if (this.isFontLoadingAPISupported) {
var nativeFontFace = font.createNativeFontFace();
if (nativeFontFace) {
fontLoadPromises.push(nativeFontFace.loaded);
}
} else {
var rule = font.bindDOM(); var rule = font.bindDOM();
if (rule) { if (rule) {
rules.push(rule); rules.push(rule);
fontsToLoad.push(font); fontsToLoad.push(font);
} }
} }
}
var request = FontLoader.queueLoadingCallback(callback); 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); FontLoader.prepareFontLoadEvent(rules, fontsToLoad, request);
} else { } else {
request.complete(); request.complete();
@ -271,8 +299,8 @@ var FontLoader = {
//#endif //#endif
}; };
var FontFace = (function FontFaceClosure() { var FontFaceObject = (function FontFaceObjectClosure() {
function FontFace(name, file, properties) { function FontFaceObject(name, file, properties) {
this.compiledGlyphs = {}; this.compiledGlyphs = {};
if (arguments.length === 1) { if (arguments.length === 1) {
// importing translated data // importing translated data
@ -283,8 +311,31 @@ var FontFace = (function FontFaceClosure() {
return; return;
} }
} }
FontFace.prototype = { FontFaceObject.prototype = {
bindDOM: function FontFace_bindDOM() { //#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) { if (!this.data) {
return null; return null;
} }
@ -311,7 +362,7 @@ var FontFace = (function FontFaceClosure() {
return rule; return rule;
}, },
getPathGenerator: function (objs, character) { getPathGenerator: function FontLoader_getPathGenerator(objs, character) {
if (!(character in this.compiledGlyphs)) { if (!(character in this.compiledGlyphs)) {
var js = objs.get(this.loadedName + '_path_' + character); var js = objs.get(this.loadedName + '_path_' + character);
/*jshint -W054 */ /*jshint -W054 */
@ -320,5 +371,5 @@ var FontFace = (function FontFaceClosure() {
return this.compiledGlyphs[character]; return this.compiledGlyphs[character];
} }
}; };
return FontFace; return FontFaceObject;
})(); })();

11
web/debugger.js

@ -112,13 +112,20 @@ var FontInspector = (function FontInspectorClosure() {
return moreInfo; return moreInfo;
} }
var moreInfo = properties(fontObj, ['name', 'type']); var moreInfo = properties(fontObj, ['name', 'type']);
var m = /url\(['"]?([^\)"']+)/.exec(url);
var fontName = fontObj.loadedName; var fontName = fontObj.loadedName;
var font = document.createElement('div'); var font = document.createElement('div');
var name = document.createElement('span'); var name = document.createElement('span');
name.textContent = fontName; name.textContent = fontName;
var download = document.createElement('a'); 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'; download.textContent = 'Download';
var logIt = document.createElement('a'); var logIt = document.createElement('a');
logIt.href = ''; logIt.href = '';

Loading…
Cancel
Save