From cb79ebc0c9db33634b656a05f0a051b895d89955 Mon Sep 17 00:00:00 2001 From: SmallMain Date: Thu, 12 Dec 2024 21:01:37 +0800 Subject: [PATCH] =?UTF-8?q?[engine]=20=E6=94=AF=E6=8C=81=E9=A2=84=E7=83=98?= =?UTF-8?q?=E7=84=99=E6=96=87=E6=9C=AC=E6=B5=8B=E9=87=8F=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- engine/cocos2d/core/utils/text-utils.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/engine/cocos2d/core/utils/text-utils.js b/engine/cocos2d/core/utils/text-utils.js index 8fc23cd5..da98c826 100644 --- a/engine/cocos2d/core/utils/text-utils.js +++ b/engine/cocos2d/core/utils/text-utils.js @@ -149,6 +149,8 @@ var textUtils = { __CHINESE_REG: /^[\u4E00-\u9FFF\u3400-\u4DFF]+$/, __JAPANESE_REG: /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/g, __KOREAN_REG: /^[\u1100-\u11FF]|[\u3130-\u318F]|[\uA960-\uA97F]|[\uAC00-\uD7AF]|[\uD7B0-\uD7FF]+$/, + + measureCache: measureCache, isUnicodeCJK: function(ch) { return this.__CHINESE_REG.test(ch) || this.__JAPANESE_REG.test(ch) || this.__KOREAN_REG.test(ch); @@ -162,7 +164,7 @@ var textUtils = { safeMeasureText: function (ctx, string, desc) { let font = desc || ctx.font; - let key = font + "\uD83C\uDFAE" + string; + let key = this.computeHash(string, font); let cache = measureCache.get(key); if (cache !== null) { return cache; @@ -174,6 +176,24 @@ var textUtils = { return width; }, + + computeHash: function (string, desc) { + return desc + "\uD83C\uDFAE" + string; + }, + + getMeasureCache: function () { + const data = {}; + for (const key in measureCache.datas) { + data[key] = measureCache.datas[key].value; + } + return data; + }, + + applyMeasureCache: function (data) { + for (const key in data) { + measureCache.put(key, data[key]); + } + }, // in case truncate a character on the Supplementary Multilingual Plane // test case: a = '😉🚗'