From f0b4c13c9b12ecaa714948ba7bc4ac051ecb8f98 Mon Sep 17 00:00:00 2001 From: "yuqi.pyq" Date: Fri, 10 Nov 2023 11:04:52 +0800 Subject: [PATCH] fix: provide a mock function to measure text width --- package.json | 2 +- src/util/text.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3a73951da..17f03591b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@antv/component", - "version": "1.0.0-beta.1", + "version": "1.0.0-beta.2", "description": "Visualization components for AntV, based on G.", "license": "MIT", "main": "lib/index.js", diff --git a/src/util/text.ts b/src/util/text.ts index c4ad254b7..28aa3ebc1 100644 --- a/src/util/text.ts +++ b/src/util/text.ts @@ -2,6 +2,11 @@ import { isString, memoize } from '@antv/util'; import type { DisplayObject, Text } from '../shapes'; let ctx: CanvasRenderingContext2D; +let mockMeasureTextWidth: ((text: string, fontSize: number) => number) | undefined; + +export function setMockMeasureTextWidth(mock: (text: string, fontSize: number) => number) { + mockMeasureTextWidth = mock; +} /** * 计算文本在画布中的宽度 @@ -10,6 +15,11 @@ export const measureTextWidth = memoize( (text: string | Text, font?: any): number => { const content = isString(text) ? text : text.style.text.toString(); const { fontSize, fontFamily, fontWeight, fontStyle, fontVariant } = font || getFont(text as Text); + + if (mockMeasureTextWidth) { + return mockMeasureTextWidth(content, fontSize); + } + if (!ctx) { ctx = document.createElement('canvas').getContext('2d') as CanvasRenderingContext2D; }