diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js
index b621d2ace1..d5ffba3fdd 100644
--- a/cocos2d/core/components/CCRichText.js
+++ b/cocos2d/core/components/CCRichText.js
@@ -126,6 +126,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.font) {
+ this._onTTFLoaded();
+ }
+ this._updateRichTextStatus();
+ }
+ },
+
/**
* !#en The maximize width of the RichText
* !#zh 富文本的最大宽度
@@ -212,6 +232,30 @@ var RichText = cc.Class({
_initSgNode: function () {
this._updateRichText();
+ if(!CC_JSB) {
+ this._onTTFLoaded();
+ }
+ },
+
+ _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 : '';
+ return fntRawUrl;
+ },
+
+ _onTTFLoaded: function () {
+ var self = this;
+
+ var callback = function () {
+ self._layoutDirty = true;
+ self._updateRichText();
+ };
+
+ cc.CustomFontLoader.loadTTF(this._getFontRawUrl(), callback);
},
_measureText: function (styleIndex, string) {
@@ -219,7 +263,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 +329,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);
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;