From 4ed1f26279f7c08e45b53e1be34ec466cc4e40a7 Mon Sep 17 00:00:00 2001 From: Lasse Fister Date: Tue, 20 Feb 2018 18:07:00 +0100 Subject: [PATCH 1/2] [loadFonts] optionally avoid duplicate XHRs for same files; closes #28 Supersedes 1969828 in PR #28 by @kontur. --- lib/loadFonts.js | 140 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 104 insertions(+), 36 deletions(-) diff --git a/lib/loadFonts.js b/lib/loadFonts.js index c02996f..146e2bc 100644 --- a/lib/loadFonts.js +++ b/lib/loadFonts.js @@ -4,40 +4,74 @@ define([ opentype ) { "use strict"; - /*globals FileReader, XMLHttpRequest, console*/ + /*globals setTimeout, FileReader, XMLHttpRequest, console*/ - /** - * Callback for when a fontfile has been loaded - * - * @param i: index of the font loaded - * @param fontFileName - * @param err: null, error-object or string with error message - * @param fontArraybuffer - */ - function onLoadFont(i, fontFileName, err, fontArraybuffer) { - /* jshint validthis: true */ - var font; + function _publishLoaded(task, data) { + var state = task.state + , index = task.index + , fontFileName = task.name + , err = data.error + , font = data.font + , fontArraybuffer = data.arraybuffer + ; + + if(err) { + console.warn('Can\'t load font', fontFileName, ' with error:', err); + state.countAll--; + } + else { + state.pubsub.publish('loadFont', index, fontFileName, font, fontArraybuffer); + state.countLoaded += 1; + } + + if(state.countLoaded === state.countAll) + state.pubsub.publish('allFontsLoaded', state.countAll); + } + + function _getLoadedFontData(err, fontArraybuffer) { + var err_ = err || null + , font = null + ; if(!err) { try { - font = opentype.parse(fontArraybuffer); + font = opentype.parse(fontArraybuffer); } catch (parseError) { - err = parseError; + err_ = parseError; } } - if(err) { - console.warn('Can\'t load font', fontFileName, ' with error:', err); - this.countAll--; - } - else { - this.pubsub.publish('loadFont', i, fontFileName, font, fontArraybuffer); - this.countLoaded += 1; - } + return { + error:err_ + , font: font + , arraybuffer:fontArraybuffer + }; + } + + function _onLoadQueued(cache, key, err, fontArraybuffer) { + var data, queue, i, l, task; - if(this.countLoaded === this.countAll) - this.pubsub.publish('allFontsLoaded', this.countAll); + cache.loaded[key] = data = _getLoadedFontData(err, fontArraybuffer); + queue = cache.queues[key]; + delete cache.queues[key]; + for(i=0,l=queue.length;i Date: Thu, 2 Aug 2018 11:21:52 +0300 Subject: [PATCH 2/2] Modified returned css family name to be single word without spaces --- lib/services/FontsData.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/services/FontsData.js b/lib/services/FontsData.js index 0fd22d8..2f3afb5 100644 --- a/lib/services/FontsData.js +++ b/lib/services/FontsData.js @@ -292,8 +292,11 @@ define([ return font.tables.os2.usWeightClass; }; - // Keeping this, maybe we'll have to transform this name further for CSS? - _p._getCSSFamilyName = _p._getFamilyName; + // Truncate the family name to single word for more resilient browser rendering + // TODO Possibly perform further transformation for the CSS name + _p._getCSSFamilyName = function (fontIndex) { + return this._getFamilyName(fontIndex).replace(/\s/gi, ""); + } _p._getIsItalic = function(fontIndex) { var font = this._data[fontIndex].font