Skip to content

Commit

Permalink
Merge pull request #41 from caryliu1999/v2.3.0-youyou-font
Browse files Browse the repository at this point in the history
Optimize the code of BMFont and LetterFont.
  • Loading branch information
2youyou2 committed Jul 22, 2019
2 parents 61a24ba + 01da8c0 commit cf6b579
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 1,097 deletions.
85 changes: 84 additions & 1 deletion cocos2d/core/assets/CCBitmapFont.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,59 @@
THE SOFTWARE.
****************************************************************************/

let FontLetterDefinition = function() {
this.u = 0;
this.v = 0;
this.w = 0;
this.h = 0;
this.offsetX = 0;
this.offsetY = 0;
this.textureID = 0;
this.valid = false;
this.xAdvance = 0;
};

let FontAtlas = function (texture) {
this._letterDefinitions = {};
this._texture = texture;
};

FontAtlas.prototype = {
constructor: FontAtlas,
addLetterDefinitions (letter, letterDefinition) {
this._letterDefinitions[letter] = letterDefinition;
},
cloneLetterDefinition () {
let copyLetterDefinitions = {};
for (let key in this._letterDefinitions) {
let value = new FontLetterDefinition();
cc.js.mixin(value, this._letterDefinitions[key]);
copyLetterDefinitions[key] = value;
}
return copyLetterDefinitions;
},
getTexture () {
return this._texture;
},
getLetter (key) {
return this._letterDefinitions[key];
},
getLetterDefinitionForChar (char) {
let key = char.charCodeAt(0);
let hasKey = this._letterDefinitions.hasOwnProperty(key);
let letter;
if (hasKey) {
letter = this._letterDefinitions[key];
} else {
letter = null;
}
return letter;
},
clear () {
this._letterDefinitions = {};
}
};

/**
* @module cc
*/
Expand Down Expand Up @@ -52,9 +105,39 @@ var BitmapFont = cc.Class({
default: -1
},
//用来缓存 BitmapFont 解析之后的数据
_fntConfig: null
_fntConfig: null,
_fontDefDictionary: null
},

onLoad () {
let spriteFrame = this.spriteFrame;
let fntConfig = this._fntConfig;
if (!this._fontDefDictionary) {
this._fontDefDictionary = new FontAtlas(spriteFrame._texture);
}

let fontDict = fntConfig.fontDefDictionary;
for (let fontDef in fontDict) {
let letter = new FontLetterDefinition();

let rect = fontDict[fontDef].rect;
letter.offsetX = fontDict[fontDef].xOffset;
letter.offsetY = fontDict[fontDef].yOffset;
letter.w = rect.width;
letter.h = rect.height;
letter.u = rect.x;
letter.v = rect.y;
//FIXME: only one texture supported for now
letter.textureID = 0;
letter.valid = true;
letter.xAdvance = fontDict[fontDef].xAdvance;

this._fontDefDictionary.addLetterDefinitions(fontDef, letter);
}
}
});

cc.BitmapFont = BitmapFont;
cc.BitmapFont.FontLetterDefinition = FontLetterDefinition;
cc.BitmapFont.FontAtlas = FontAtlas;
module.exports = BitmapFont;
4 changes: 0 additions & 4 deletions cocos2d/core/components/CCLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,6 @@ let Label = cc.Class({
if (CC_EDITOR && value) {
this._userDefinedFont = value;
}
// release reference
cc.Label.FontAtlasManager.releaseFontAtlas(this.font, this.node._id);
this._N$file = value;
if (value && this._isSystemFontUsed)
this._isSystemFontUsed = false;
Expand Down Expand Up @@ -548,8 +546,6 @@ let Label = cc.Class({
this._ttfTexture.destroy();
this._ttfTexture = null;
}
// release reference
cc.Label.FontAtlasManager.releaseFontAtlas(this.font, this.node._id);
this._super();
},

Expand Down
Loading

0 comments on commit cf6b579

Please sign in to comment.