From cc543d98ccdae9c6b5f695bbfc88181befa30561 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 9 Nov 2016 18:17:49 +0800 Subject: [PATCH 1/4] add custom font support --- cocos2d/core/components/CCRichText.js | 52 +++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index b621d2ace1..f5c7687ef5 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -63,6 +63,9 @@ var RichText = cc.Class({ this._labelSegmentsCache = []; this._resetState(); + if(!CC_JSB) { + this._ttfFontLoaded = false; + } if(CC_EDITOR) { this._updateRichTextStatus = debounce(this._updateRichText, 200); @@ -126,6 +129,26 @@ var RichText = cc.Class({ } }, + /** + * !#en Custom TTF font of RichText + * !#zh 富文本定制字体 + * @property {cc.TTFFont} font + */ + font: { + default: null, + type: cc.TTFFont, + tooltip: 'i18n:COMPONENT.richtext.font', + notify: function (oldValue) { + if(this.font === oldValue) return; + + this._layoutDirty = true; + if(!CC_JSB) { + this._ttfFontLoaded = false; + } + this._updateRichTextStatus(); + } + }, + /** * !#en The maximize width of the RichText * !#zh 富文本的最大宽度 @@ -212,6 +235,30 @@ var RichText = cc.Class({ _initSgNode: function () { this._updateRichText(); + var self = this; + document.fonts.ready.then(function() { + self._layoutDirty = true; + self._updateRichText(); + }); + }, + + _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; + } }, _measureText: function (styleIndex, string) { @@ -219,7 +266,7 @@ var RichText = cc.Class({ var func = function (string) { var label; if(self._labelSegmentsCache.length === 0) { - label = new _ccsg.Label(string); + label = self._createFontLabel(string); self._labelSegmentsCache.push(label); } else { label = self._labelSegmentsCache[0]; @@ -285,7 +332,7 @@ var RichText = cc.Class({ _addLabelSegment: function(stringToken, styleIndex) { var labelSegment; if(this._labelSegmentsCache.length === 0) { - labelSegment = new _ccsg.Label(stringToken); + labelSegment = this._createFontLabel(stringToken); } else { labelSegment = this._labelSegmentsCache.pop(); labelSegment.setString(stringToken); @@ -296,6 +343,7 @@ var RichText = cc.Class({ this._applyTextAttribute(labelSegment); labelSegment.setAnchorPoint(cc.p(0, 0)); + labelSegment.getContentSize(); this._sgNode.addChild(labelSegment); this._labelSegments.push(labelSegment); From 086f092227feeef9f53ce39a8d4df03474da678d Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 9 Nov 2016 23:08:16 +0800 Subject: [PATCH 2/4] 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; From 3aee3a4e4b15b8765130b44ad61b2a0a99984b65 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 10 Nov 2016 16:29:04 +0800 Subject: [PATCH 3/4] remove duplicated code --- cocos2d/core/components/CCRichText.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index 11cdc9d4d6..96d71390e3 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -238,10 +238,13 @@ var RichText = cc.Class({ }, _createFontLabel: function (string) { + return new _ccsg.Label(string, this._getFontRawUrl()); + }, + + _getFontRawUrl: function() { var isAsset = this.font instanceof cc.TTFFont; var fntRawUrl = isAsset ? this.font.rawUrl : ''; - var sgNode = new _ccsg.Label(string, fntRawUrl); - return sgNode; + return fntRawUrl; }, _onTTFLoaded: function () { @@ -252,10 +255,7 @@ var RichText = cc.Class({ self._updateRichText(); }; - var isAsset = this.font instanceof cc.TTFFont; - var fntRawUrl = isAsset ? this.font.rawUrl : ''; - - cc.CustomFontLoader.loadTTF(fntRawUrl, callback); + cc.CustomFontLoader.loadTTF(this._getFontRawUrl(), callback); }, _measureText: function (styleIndex, string) { From fc9429a04f6bcf99210ca172ef4c7c22e68f7f4b Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 10 Nov 2016 20:54:05 +0800 Subject: [PATCH 4/4] remove unused code --- cocos2d/core/components/CCRichText.js | 1 - 1 file changed, 1 deletion(-) diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index 96d71390e3..d5ffba3fdd 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -340,7 +340,6 @@ var RichText = cc.Class({ this._applyTextAttribute(labelSegment); labelSegment.setAnchorPoint(cc.p(0, 0)); - labelSegment.getContentSize(); this._sgNode.addChild(labelSegment); this._labelSegments.push(labelSegment);