[engine] 支持预烘焙文本测量值

This commit is contained in:
SmallMain 2024-12-12 21:01:37 +08:00
parent d212be41ae
commit cb79ebc0c9
No known key found for this signature in database

View File

@ -150,6 +150,8 @@ var textUtils = {
__JAPANESE_REG: /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/g, __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]+$/, __KOREAN_REG: /^[\u1100-\u11FF]|[\u3130-\u318F]|[\uA960-\uA97F]|[\uAC00-\uD7AF]|[\uD7B0-\uD7FF]+$/,
measureCache: measureCache,
isUnicodeCJK: function(ch) { isUnicodeCJK: function(ch) {
return this.__CHINESE_REG.test(ch) || this.__JAPANESE_REG.test(ch) || this.__KOREAN_REG.test(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) { safeMeasureText: function (ctx, string, desc) {
let font = desc || ctx.font; let font = desc || ctx.font;
let key = font + "\uD83C\uDFAE" + string; let key = this.computeHash(string, font);
let cache = measureCache.get(key); let cache = measureCache.get(key);
if (cache !== null) { if (cache !== null) {
return cache; return cache;
@ -175,6 +177,24 @@ var textUtils = {
return width; 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 // in case truncate a character on the Supplementary Multilingual Plane
// test case: a = '😉🚗' // test case: a = '😉🚗'
// _safeSubstring(a, 1) === '😉🚗' // _safeSubstring(a, 1) === '😉🚗'