From 68beb29f80bfb2626292b5d5d0cc504b8bcd553e Mon Sep 17 00:00:00 2001 From: plainheart Date: Mon, 24 Oct 2022 19:26:25 +0800 Subject: [PATCH 1/2] fix(platformApi): fix wrong regexp for extracting the font size. --- src/core/platform.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/platform.ts b/src/core/platform.ts index 7df2364cb..43059a2d2 100644 --- a/src/core/platform.ts +++ b/src/core/platform.ts @@ -75,8 +75,8 @@ export const platformApi: Platform = { text = text || ''; font = font || DEFAULT_FONT; // Use font size if there is no other method can be used. - const res = /^([0-9]*?)px$/.exec(font); - const fontSize = +(res && res[1]) || DEFAULT_FONT_SIZE; + const res = /(\d+)px/.exec(font); + const fontSize = res && +res[1] || DEFAULT_FONT_SIZE; let width = 0; if (font.indexOf('mono') >= 0) { // is monospace width = fontSize * text.length; @@ -103,7 +103,7 @@ export const platformApi: Platform = { export function setPlatformAPI(newPlatformApis: Partial) { for (let key in platformApi) { - // Don't assign unkown methods. + // Don't assign unknown methods. if ((newPlatformApis as any)[key]) { (platformApi as any)[key] = (newPlatformApis as any)[key]; } From b968b36a25e2556d69cf95b20aee9be000d75a07 Mon Sep 17 00:00:00 2001 From: plainheart Date: Wed, 9 Nov 2022 15:56:31 +0800 Subject: [PATCH 2/2] test(platformApi): add test case for `measureText` in non-canvas environment. --- test/ssr-measureText.html | 98 ++++++++++++++++++++++++++++++ test/ut/spec/core/platform.test.ts | 6 +- 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 test/ssr-measureText.html diff --git a/test/ssr-measureText.html b/test/ssr-measureText.html new file mode 100644 index 000000000..e9a7a2830 --- /dev/null +++ b/test/ssr-measureText.html @@ -0,0 +1,98 @@ + + + + + Image + + + + + + + + + + + \ No newline at end of file diff --git a/test/ut/spec/core/platform.test.ts b/test/ut/spec/core/platform.test.ts index 5f307fe88..f96ca5b44 100644 --- a/test/ut/spec/core/platform.test.ts +++ b/test/ut/spec/core/platform.test.ts @@ -32,5 +32,9 @@ describe('platform', function() { createCanvas: oldCreateCanvas, measureText: oldMeasureText }); - }) + }); + + it('measureText should return correct width', function () { + expect(platform.platformApi.measureText('A', 'normal normal 18px sans-serif').width).toBe(12.06); + }); }); \ No newline at end of file