From 086f092227feeef9f53ce39a8d4df03474da678d Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 9 Nov 2016 23:08:16 +0800 Subject: [PATCH] add custom font loader add richtext font load improve custom ttf font cache fix almost the cases fix for android mobile device add custom font loader fix font load issue remove unneeded logs --- cocos2d/core/components/CCRichText.js | 35 ++++---- cocos2d/core/label/CCSGLabel.js | 73 ++------------- cocos2d/core/label/CCTextUtils.js | 122 ++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 85 deletions(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index f5c7687ef5..11cdc9d4d6 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -63,9 +63,6 @@ var RichText = cc.Class({ this._labelSegmentsCache = []; this._resetState(); - if(!CC_JSB) { - this._ttfFontLoaded = false; - } if(CC_EDITOR) { this._updateRichTextStatus = debounce(this._updateRichText, 200); @@ -142,8 +139,8 @@ var RichText = cc.Class({ if(this.font === oldValue) return; this._layoutDirty = true; - if(!CC_JSB) { - this._ttfFontLoaded = false; + if(!CC_JSB && this.font) { + this._onTTFLoaded(); } this._updateRichTextStatus(); } @@ -235,30 +232,30 @@ var RichText = cc.Class({ _initSgNode: function () { this._updateRichText(); - var self = this; - document.fonts.ready.then(function() { - self._layoutDirty = true; - self._updateRichText(); - }); + if(!CC_JSB) { + this._onTTFLoaded(); + } }, _createFontLabel: function (string) { var isAsset = this.font instanceof cc.TTFFont; var fntRawUrl = isAsset ? this.font.rawUrl : ''; var sgNode = new _ccsg.Label(string, fntRawUrl); - // node should be resize whenever font changed, needed only on web - if (!CC_JSB) { - sgNode.on('load', this._onTTFLoaded, this); - } return sgNode; }, _onTTFLoaded: function () { - if(!this._ttfFontLoaded) { - this._layoutDirty = true; - this._updateRichText(); - this._ttfFontLoaded = true; - } + var self = this; + + var callback = function () { + self._layoutDirty = true; + self._updateRichText(); + }; + + var isAsset = this.font instanceof cc.TTFFont; + var fntRawUrl = isAsset ? this.font.rawUrl : ''; + + cc.CustomFontLoader.loadTTF(fntRawUrl, callback); }, _measureText: function (styleIndex, string) { diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 1ad8178c1d..10520d0288 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -25,6 +25,7 @@ ****************************************************************************/ var EventTarget = require("../cocos2d/core/event/event-target"); +cc.fontFamilyCache = {}; var FontLetterDefinition = function() { this._u = 0; @@ -425,75 +426,15 @@ _ccsg.Label = _ccsg.Node.extend({ this._notifyLabelSkinDirty(); }, - cleanup: function () { - this._super(); - - //remove the created DIV and style due to loading @font-face - if(this._fontFaceStyle) { - if(document.body.contains(this._fontFaceStyle)) { - document.body.removeChild(this._fontFaceStyle); - } - } - - if(this._preloadDiv) { - if(document.body.contains(this._preloadDiv)) { - document.body.removeChild(this._preloadDiv); - } - } - }, - _loadTTFFont: function(fontHandle) { - var ttfIndex = fontHandle.lastIndexOf(".ttf"); - if (ttfIndex === -1) return fontHandle; - var slashPos = fontHandle.lastIndexOf("/"); - var fontFamilyName; - if (slashPos === -1) fontFamilyName = fontHandle.substring(0, ttfIndex) + "_LABEL"; - else fontFamilyName = fontHandle.substring(slashPos + 1, ttfIndex) + "_LABEL"; var self = this; - if (window.FontFace) { - var fontFace = new FontFace(fontFamilyName, "url('" + fontHandle + "')"); - fontFace.load().then(function(loadedFace) { - document.fonts.add(loadedFace); - self._notifyLabelSkinDirty(); - self.emit('load'); - }); - } else { - //fall back implementations - var doc = document, - fontStyle = document.createElement("style"); - fontStyle.type = "text/css"; - doc.body.appendChild(fontStyle); - this._fontFaceStyle = fontStyle; - var fontStr = ""; - if (isNaN(fontFamilyName - 0)) - fontStr += "@font-face { font-family:" + fontFamilyName + "; src:"; - else - fontStr += "@font-face { font-family:'" + fontFamilyName + "'; src:"; - - fontStr += "url('" + fontHandle + "');"; - - fontStyle.textContent = fontStr + "}"; - - //
.
- var preloadDiv = document.createElement("div"); - var _divStyle = preloadDiv.style; - _divStyle.fontFamily = fontFamilyName; - preloadDiv.innerHTML = "."; - _divStyle.position = "absolute"; - _divStyle.left = "-100px"; - _divStyle.top = "-100px"; - doc.body.appendChild(preloadDiv); - this._preloadDiv = preloadDiv; - fontStyle.onload = function() { - fontStyle.onload = null; - self.scheduleOnce(function() { - self._notifyLabelSkinDirty(); - self.emit("load"); - },0.1); - }; - - } + var fontFamilyName = cc.CustomFontLoader._getFontFamily(fontHandle); + var callback = function () { + self._notifyLabelSkinDirty(); + self.emit('load'); + }; + cc.CustomFontLoader.loadTTF(fontHandle, callback); return fontFamilyName; }, diff --git a/cocos2d/core/label/CCTextUtils.js b/cocos2d/core/label/CCTextUtils.js index 18aa8da906..bae23412df 100644 --- a/cocos2d/core/label/CCTextUtils.js +++ b/cocos2d/core/label/CCTextUtils.js @@ -23,6 +23,127 @@ THE SOFTWARE. ****************************************************************************/ +var CustomFontDescriptor = function() { + this._status = 'unloaded'; + //when font is loaded, it will notify each observer in the observers array + this._observers = []; +}; + +CustomFontDescriptor.prototype.onLoaded = function () { + this._status = 'loaded'; + this._observers.forEach(function(item) { + item(); + }); +}; + +CustomFontDescriptor.prototype.isLoaded = function () { + return this._status === 'loaded'; +}; + +CustomFontDescriptor.prototype.addHandler = function (callback) { + if(this._observers.indexOf(callback) === -1) { + this._observers.push(callback); + } +}; + +var CustomFontLoader = { + _fontCache: {}, + loadTTF: function (url, callback) { + //these platforms support window.FontFace, but it sucks sometimes. + var useFontFace = (cc.sys.browserType !== cc.sys.BROWSER_TYPE_BAIDU + && cc.sys.browserType !== cc.sys.BROWSER_TYPE_BAIDU_APP + && cc.sys.browserType !== cc.sys.BROWSER_TYPE_MOBILE_QQ); + + if (window.FontFace && useFontFace) { + this._loadWithFontFace(url, callback); + } else { + this._loadWithCSS(url, callback); + } + }, + + _loadWithFontFace: function(url, callback) { + var fontFamilyName = this._getFontFamily(url); + + var fontDescriptor = this._fontCache[fontFamilyName]; + if(!fontDescriptor) { + var fontFace = new FontFace(fontFamilyName, "url('" + url + "')"); + document.fonts.add(fontFace); + + fontDescriptor = new CustomFontDescriptor(); + fontDescriptor.addHandler(callback); + this._fontCache[fontFamilyName] = fontDescriptor; + + fontFace.loaded.then(function() { + fontDescriptor.onLoaded(); + }); + } else { + if(!fontDescriptor.isLoaded()) { + fontDescriptor.addHandler(callback); + } + } + }, + + _loadWithCSS: function(url, callback) { + var fontFamilyName = this._getFontFamily(url); + + var fontDescriptor = this._fontCache[fontFamilyName]; + if(!fontDescriptor) { + //fall back implementations + var doc = document; + var fontStyle = document.createElement("style"); + fontStyle.type = "text/css"; + doc.body.appendChild(fontStyle); + + var fontStr = ""; + if (isNaN(fontFamilyName - 0)) + fontStr += "@font-face { font-family:" + fontFamilyName + "; src:"; + else + fontStr += "@font-face { font-family:'" + fontFamilyName + "'; src:"; + + fontStr += "url('" + url + "');"; + + fontStyle.textContent = fontStr + "}"; + + var preloadDiv = document.createElement("div"); + var _divStyle = preloadDiv.style; + _divStyle.fontFamily = fontFamilyName; + preloadDiv.innerHTML = "."; + _divStyle.position = "absolute"; + _divStyle.left = "-100px"; + _divStyle.top = "-100px"; + doc.body.appendChild(preloadDiv); + + fontDescriptor = new CustomFontDescriptor(); + fontDescriptor.addHandler(callback); + this._fontCache[fontFamilyName] = fontDescriptor; + + fontStyle.onload = function() { + setTimeout(function () { + fontDescriptor.onLoaded(); + }, 100); + }; + } else { + if(!fontDescriptor.isLoaded()) { + fontDescriptor.addHandler(callback); + } + } + }, + + _getFontFamily: function (fontHandle) { + var ttfIndex = fontHandle.lastIndexOf(".ttf"); + if (ttfIndex === -1) return fontHandle; + + var slashPos = fontHandle.lastIndexOf("/"); + var fontFamilyName; + if (slashPos === -1) { + fontFamilyName = fontHandle.substring(0, ttfIndex) + "_LABEL"; + } else { + fontFamilyName = fontHandle.substring(slashPos + 1, ttfIndex) + "_LABEL"; + } + return fontFamilyName; + } +}; + var TextUtils = { label_wordRex : /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]+|\S)/, label_symbolRex : /^[!,.:;}\]%\?>、‘“》?。,!]/, @@ -133,3 +254,4 @@ var TextUtils = { }; cc.TextUtils = module.exports = TextUtils; +cc.CustomFontLoader = module.exports = CustomFontLoader;