From bf728495aa4c86688772ea8bace2e47e3bb3b94b Mon Sep 17 00:00:00 2001 From: zhouxinyu Date: Tue, 12 Nov 2024 15:28:23 +0800 Subject: [PATCH 1/3] fix: fix issue with text clip --- .../vrender-core/fix-text-clip_2024-11-12-07-27.json | 10 ++++++++++ .../src/core/contributions/textMeasure/AtextMeasure.ts | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 common/changes/@visactor/vrender-core/fix-text-clip_2024-11-12-07-27.json diff --git a/common/changes/@visactor/vrender-core/fix-text-clip_2024-11-12-07-27.json b/common/changes/@visactor/vrender-core/fix-text-clip_2024-11-12-07-27.json new file mode 100644 index 000000000..768529177 --- /dev/null +++ b/common/changes/@visactor/vrender-core/fix-text-clip_2024-11-12-07-27.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@visactor/vrender-core", + "comment": "fix: fix issue with text clip", + "type": "none" + } + ], + "packageName": "@visactor/vrender-core" +} diff --git a/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts b/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts index 75016c997..902fb63e5 100644 --- a/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts +++ b/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts @@ -276,7 +276,7 @@ export class ATextMeasure implements ITextMeasure { rightIdx: number ): { str: string; width: number } { const middleIdx = Math.ceil((leftIdx + rightIdx) / 2); - const subText = text.substring(middleIdx - 1, text.length - 1); + const subText = text.substring(middleIdx - 1, text.length); const strWidth = this.measureTextWidth(subText, options); let length: number; if (strWidth > width) { @@ -285,21 +285,21 @@ export class ATextMeasure implements ITextMeasure { return { str: '', width: 0 }; } // 如果子字符串长度小于1,而且大于给定宽的话,返回空字符串 // 先判断是不是左侧的那个字符 - const str = text.substring(middleIdx, text.length - 1); + const str = text.substring(middleIdx, text.length); // 如果到左侧的字符小于或等于width,那么说明就是左侧的字符 length = this.measureTextWidth(str, options); if (length <= width) { return { str, width: length }; } // 返回leftIdx到middleIdx - return this._clipTextStart(text, options, width, middleIdx, text.length - 1); + return this._clipTextStart(text, options, width, middleIdx, text.length); } else if (strWidth < width) { // 如果字符串的宽度小于限制宽度 if (middleIdx <= 0) { return { str: text, width: this.measureTextWidth(text, options) }; } // 如果已经到结尾了,返回text // 先判断是不是右侧的那个字符 - const str = text.substring(middleIdx - 2, text.length - 1); + const str = text.substring(middleIdx - 2, text.length); // 如果到右侧的字符大于或等于width,那么说明就是这个字符串 length = this.measureTextWidth(str, options); if (length >= width) { From 10e48e5af458afc933f4e8f0fc6cc272c1fdda35 Mon Sep 17 00:00:00 2001 From: zhouxinyu Date: Tue, 12 Nov 2024 16:01:10 +0800 Subject: [PATCH 2/3] feat: add exit check --- .../src/core/contributions/textMeasure/AtextMeasure.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts b/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts index 902fb63e5..10567774d 100644 --- a/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts +++ b/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts @@ -231,6 +231,12 @@ export class ATextMeasure implements ITextMeasure { leftIdx: number, rightIdx: number ): { str: string; width: number } { + // 添加退出条件,如果leftIdx和rightIdx相等,那么就返回这个字符串(理论上这时出问题了) + if (leftIdx === rightIdx) { + console.warn(`【_clipTextEnd】不应该走到这里${text}, ${leftIdx}, ${rightIdx}`); + const subText = text.substring(0, rightIdx + 1); + return { str: subText, width: this.measureTextWidth(subText, options) }; + } const middleIdx = Math.floor((leftIdx + rightIdx) / 2); const subText = text.substring(0, middleIdx + 1); const strWidth = this.measureTextWidth(subText, options); From eb336bdb015cd5a1e9ca7c99f871470fa94a89b2 Mon Sep 17 00:00:00 2001 From: zhouxinyu Date: Wed, 13 Nov 2024 14:25:06 +0800 Subject: [PATCH 3/3] fix: change console warn --- .../src/core/contributions/textMeasure/AtextMeasure.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts b/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts index 10567774d..810e9ef04 100644 --- a/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts +++ b/packages/vrender-core/src/core/contributions/textMeasure/AtextMeasure.ts @@ -4,6 +4,7 @@ import type { ICanvas, IContext2d, EnvType } from '../../../interface'; import type { TextOptionsType, ITextMeasure } from '../../../interface/text'; import { DefaultTextAttribute, DefaultTextStyle } from '../../../graphic/config'; import { testLetter } from '../../../graphic/richtext/utils'; +import { Logger } from '@visactor/vutils'; @injectable() export class ATextMeasure implements ITextMeasure { @@ -233,7 +234,8 @@ export class ATextMeasure implements ITextMeasure { ): { str: string; width: number } { // 添加退出条件,如果leftIdx和rightIdx相等,那么就返回这个字符串(理论上这时出问题了) if (leftIdx === rightIdx) { - console.warn(`【_clipTextEnd】不应该走到这里${text}, ${leftIdx}, ${rightIdx}`); + Logger.getInstance().warn(`【_clipTextEnd】不应该走到这里${text}, ${leftIdx}, ${rightIdx}`); + // console.warn(`【_clipTextEnd】不应该走到这里${text}, ${leftIdx}, ${rightIdx}`); const subText = text.substring(0, rightIdx + 1); return { str: subText, width: this.measureTextWidth(subText, options) }; }