mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
fix custom ttf md5 cache
This commit is contained in:
@@ -55,15 +55,21 @@ var CustomFontLoader = {
|
||||
_allFontsLoaded: false,
|
||||
_intervalId: 0,
|
||||
loadTTF: function (url, callback) {
|
||||
var fontFamilyName = this._getFontFamily(url);
|
||||
|
||||
var md5Pipe = cc.loader.md5Pipe;
|
||||
if (md5Pipe) {
|
||||
url = md5Pipe.transformURL(url);
|
||||
}
|
||||
//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);
|
||||
this._loadWithFontFace(fontFamilyName, url, callback);
|
||||
} else {
|
||||
this._loadWithCSS(url, callback);
|
||||
this._loadWithCSS(fontFamilyName, url, callback);
|
||||
}
|
||||
|
||||
if (this._intervalId === 0) {
|
||||
@@ -95,8 +101,7 @@ var CustomFontLoader = {
|
||||
}
|
||||
},
|
||||
|
||||
_loadWithFontFace: function(url, callback) {
|
||||
var fontFamilyName = this._getFontFamily(url);
|
||||
_loadWithFontFace: function(fontFamilyName, url, callback) {
|
||||
|
||||
var fontDescriptor = this._fontCache[fontFamilyName];
|
||||
if(!fontDescriptor) {
|
||||
@@ -107,7 +112,7 @@ var CustomFontLoader = {
|
||||
fontDescriptor.addHandler(callback);
|
||||
this._fontCache[fontFamilyName] = fontDescriptor;
|
||||
|
||||
fontFace.loaded.then(function() {
|
||||
fontFace.load().then(function() {
|
||||
fontDescriptor.onLoaded();
|
||||
});
|
||||
} else {
|
||||
@@ -117,9 +122,7 @@ var CustomFontLoader = {
|
||||
}
|
||||
},
|
||||
|
||||
_loadWithCSS: function(url, callback) {
|
||||
var fontFamilyName = this._getFontFamily(url);
|
||||
|
||||
_loadWithCSS: function(fontFamilyName, url, callback) {
|
||||
var fontDescriptor = this._fontCache[fontFamilyName];
|
||||
if(!fontDescriptor) {
|
||||
//fall back implementations
|
||||
|
||||
@@ -39,26 +39,31 @@ var MD5Pipe = function (md5AssetsMap, libraryBase, rawAssetsBase) {
|
||||
MD5Pipe.ID = ID;
|
||||
|
||||
MD5Pipe.prototype.handle = function(item) {
|
||||
var key = item.url;
|
||||
item.url = this.transformURL(item.url);
|
||||
return item;
|
||||
};
|
||||
|
||||
MD5Pipe.prototype.transformURL = function (url) {
|
||||
var key = url;
|
||||
if (key.startsWith(this.libraryBase)) {
|
||||
key = key.slice(this.libraryBase.length);
|
||||
} else if(key.startsWith(this.rawAssetsBase)) {
|
||||
key = key.slice(this.rawAssetsBase.length);
|
||||
} else {
|
||||
return item;
|
||||
return url;
|
||||
}
|
||||
let hashValue = this.md5AssetsMap[key];
|
||||
if (hashValue) {
|
||||
var matched = false;
|
||||
item.url = item.url.replace(ExtnameRegex, function(match, p1) {
|
||||
url = url.replace(ExtnameRegex, function(match, p1) {
|
||||
matched = true;
|
||||
return '.' + hashValue + p1;
|
||||
});
|
||||
if (!matched) {
|
||||
item.url = item.url + '.' + hashValue;
|
||||
url = url + '.' + hashValue;
|
||||
}
|
||||
}
|
||||
return item;
|
||||
return url;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -286,7 +286,9 @@ var AssetLibrary = {
|
||||
|
||||
var md5AssetsMap = options.md5AssetsMap;
|
||||
if (md5AssetsMap) {
|
||||
cc.loader.insertPipeAfter(cc.loader.assetLoader, new MD5Pipe(md5AssetsMap, _libraryBase, _rawAssetsBase));
|
||||
var md5Pipe = new MD5Pipe(md5AssetsMap, _libraryBase, _rawAssetsBase);
|
||||
cc.loader.insertPipeAfter(cc.loader.assetLoader, md5Pipe);
|
||||
cc.loader.md5Pipe = md5Pipe;
|
||||
}
|
||||
|
||||
// init raw assets
|
||||
|
||||
Reference in New Issue
Block a user