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; }