From 9a8eae69d746c120b7e7ace8a846562db939cdb0 Mon Sep 17 00:00:00 2001 From: SmallMain Date: Fri, 13 Dec 2024 16:42:37 +0800 Subject: [PATCH] =?UTF-8?q?[engine]=20=E6=94=AF=E6=8C=81=E7=83=98=E7=84=99?= =?UTF-8?q?=E5=92=8C=E9=A2=84=E5=8A=A0=E8=BD=BD=E6=96=87=E6=9C=AC=E6=B8=B2?= =?UTF-8?q?=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/renderer/utils/label/letter-font.js | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/engine/cocos2d/core/renderer/utils/label/letter-font.js b/engine/cocos2d/core/renderer/utils/label/letter-font.js index 9b815ce7..8a9a8203 100644 --- a/engine/cocos2d/core/renderer/utils/label/letter-font.js +++ b/engine/cocos2d/core/renderer/utils/label/letter-font.js @@ -327,6 +327,22 @@ class LetterAtlases { */ _fontDefDictionary = new FontAtlas(null); + /** + * 所有获取过的字符集合 + */ + letterCache = null; + + /** + * 是否记录所有获取过的字符 + */ + get enableLetterCache() { + return this._enableLetterCache; + } + set enableLetterCache(v) { + this._enableLetterCache = v; + this.letterCache = v ? {} : null; + } + _enableLetterCache = false; constructor() { const handler = new cc.sp.MultiHandler(); @@ -336,6 +352,12 @@ class LetterAtlases { cc.director.on(cc.Director.EVENT_BEFORE_SCENE_LAUNCH, this.beforeSceneLoad, this); } + static init() { + if (!_shareAtlas) { + _shareAtlas = new LetterAtlases(); + cc.Label._shareAtlas = _shareAtlas; + } + } insertLetterTexture(letterTexture) { for (const atlas of this.atlases) { @@ -428,6 +450,21 @@ class LetterAtlases { let hash = char.charCodeAt(0) + labelInfo.hash; let letter = this._fontDefDictionary._letterDefinitions[hash]; if (!letter) { + if (this._enableLetterCache) { + const canvas = Label._canvasPool.get(); + this.letterCache[hash] = { + char, + hash: labelInfo.hash, + measure: textUtils.safeMeasureText(canvas.context, char, labelInfo.fontDesc), + fontDesc: labelInfo.fontDesc, + fontSize: labelInfo.fontSize, + margin: labelInfo.margin, + out: labelInfo.out.toHEX(), + color: labelInfo.color.toHEX(), + isOutlined: labelInfo.isOutlined, + }; + Label._canvasPool.put(canvas); + } let temp = new LetterTexture(char, labelInfo); temp.updateRenderData(); letter = this.insertLetterTexture(temp); @@ -442,6 +479,36 @@ class LetterAtlases { return letter; } + + + cacheLetter(info) { + textUtils.applyMeasureCache({ [textUtils.computeHash(info.char, info.fontDesc)]: info.measure }); + shareLabelInfo.hash = info.hash; + shareLabelInfo.fontDesc = info.fontDesc; + shareLabelInfo.margin = info.margin; + shareLabelInfo.out = cc.Color.fromHEX(cc.color(), info.out); + shareLabelInfo.fontSize = info.fontSize; + shareLabelInfo.color = cc.Color.fromHEX(cc.color(), info.color); + shareLabelInfo.isOutlined = info.isOutlined; + this.getLetterDefinitionForChar(info.char, shareLabelInfo); + } + + + getLetterCache() { + const arr = []; + for (const key in this.letterCache) { + const cache = this.letterCache[key]; + arr.push(cache); + } + return arr; + } + + + applyLetterCache(data) { + for (const cache of data) { + this.cacheLetter(cache); + } + } checkMaterialAndUpdateTexId(letter) { @@ -629,3 +696,5 @@ export default class LetterFontAssembler extends WebglBmfontAssembler { LetterFontAssembler.prototype.floatsPerVert = 6; LetterFontAssembler.prototype.texIdOffset = 5; LetterFontAssembler.prototype.isMulti = true; + +Label.LetterAtlases = LetterAtlases;