From c262c3d4fe01b18d54d3c8110d3a3447fe715bda Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Sun, 9 Jul 2023 15:28:18 +0900 Subject: [PATCH 1/4] fix(): `_getFontDeclaration` closes #9010 closes #9041 --- src/shapes/Text/Text.ts | 44 ++++++++++++++++++++--------------------- test/unit/text.js | 2 ++ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/shapes/Text/Text.ts b/src/shapes/Text/Text.ts index dcbe6506213..b821759e5b7 100644 --- a/src/shapes/Text/Text.ts +++ b/src/shapes/Text/Text.ts @@ -1,11 +1,7 @@ import { cache } from '../../cache'; import { DEFAULT_SVG_FONT_SIZE } from '../../constants'; import type { ObjectEvents } from '../../EventTypeDefs'; -import type { - CompleteTextStyleDeclaration, - TextStyle, - TextStyleDeclaration, -} from './StyledText'; +import type { CompleteTextStyleDeclaration, TextStyle } from './StyledText'; import { StyledText } from './StyledText'; import { SHARED_ATTRIBUTES } from '../../parser/attributes'; import { parseAttributes } from '../../parser/parseAttributes'; @@ -785,9 +781,10 @@ export class Text< ) { const fontCache = cache.getFontCache(charStyle), fontDeclaration = this._getFontDeclaration(charStyle), - previousFontDeclaration = this._getFontDeclaration(prevCharStyle), couple = previousChar + _char, - stylesAreEqual = fontDeclaration === previousFontDeclaration, + stylesAreEqual = + previousChar && + fontDeclaration === this._getFontDeclaration(prevCharStyle), fontMultiplier = charStyle.fontSize / this.CACHE_FONT_SIZE; let width: number | undefined, coupleWidth: number | undefined, @@ -1640,25 +1637,26 @@ export class Text< * @returns {String} font declaration formatted for canvas context. */ _getFontDeclaration( - styleObject?: TextStyleDeclaration, + { + fontFamily = this.fontFamily, + fontStyle = this.fontStyle, + fontWeight = this.fontWeight, + fontSize = this.fontSize, + }: Partial = {}, forMeasuring?: boolean ): string { - const style = styleObject || this, - family = this.fontFamily, - fontIsGeneric = Text.genericFonts.indexOf(family.toLowerCase()) > -1; - const fontFamily = - family === undefined || - family.indexOf("'") > -1 || - family.indexOf(',') > -1 || - family.indexOf('"') > -1 || - fontIsGeneric - ? style.fontFamily - : `"${style.fontFamily}"`; + const parsedFontFamily = + fontFamily.includes("'") || + fontFamily.includes('"') || + fontFamily.includes(',') || + Text.genericFonts.includes(fontFamily.toLowerCase()) + ? fontFamily + : `"${fontFamily}"`; return [ - style.fontStyle, - style.fontWeight, - forMeasuring ? this.CACHE_FONT_SIZE + 'px' : style.fontSize + 'px', - fontFamily, + fontStyle, + fontWeight, + `${forMeasuring ? this.CACHE_FONT_SIZE : fontSize}px`, + parsedFontFamily, ].join(' '); } diff --git a/test/unit/text.js b/test/unit/text.js index fa6c3919f6b..16661aafeab 100644 --- a/test/unit/text.js +++ b/test/unit/text.js @@ -97,6 +97,8 @@ text.fontFamily = '\'Times New Roman\''; fontDecl = text._getFontDeclaration(); assert.equal(fontDecl, 'normal normal 40px \'Times New Roman\''); + fontDecl = text._getFontDeclaration({ fontFamily: 'Arial' }); + assert.equal(fontDecl, 'normal normal 40px \"Arial\"', 'passed style should take precedence'); }); QUnit.test('_getFontDeclaration with coma', function(assert) { From 304b75b04f3104adfec98fbb6d77056d01ff2d5c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 9 Jul 2023 06:30:10 +0000 Subject: [PATCH 2/4] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad67c1cda04..838c2f89178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [next] +- fix(): `_getFontDeclaration` [#9082](https://github.com/fabricjs/fabric.js/pull/9082) - chore(TS): Fix ITextBehaviour enterEditing type [#9075](https://github.com/fabricjs/fabric.js/pull/9075) - chore(TS): export FabricObjectProps and GroupProps [#9025](https://github.com/fabricjs/fabric.js/pull/9025) - chore(TS): Replace BaseFabricObject with FabricObject [#9016](https://github.com/fabricjs/fabric.js/pull/9016) From ceb947acf41ff6e9758691bf08a251518a6f8676 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 9 Jul 2023 06:31:44 +0000 Subject: [PATCH 3/4] update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 838c2f89178..178fb64b803 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## [next] -- fix(): `_getFontDeclaration` [#9082](https://github.com/fabricjs/fabric.js/pull/9082) +- fix(Text): `_getFontDeclaration` [#9082](https://github.com/fabricjs/fabric.js/pull/9082) - chore(TS): Fix ITextBehaviour enterEditing type [#9075](https://github.com/fabricjs/fabric.js/pull/9075) - chore(TS): export FabricObjectProps and GroupProps [#9025](https://github.com/fabricjs/fabric.js/pull/9025) - chore(TS): Replace BaseFabricObject with FabricObject [#9016](https://github.com/fabricjs/fabric.js/pull/9016) From 6002fb6f07ec6b10f2144cc72ef295476bebbd03 Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Sun, 9 Jul 2023 15:41:26 +0900 Subject: [PATCH 4/4] Update Text.ts --- src/shapes/Text/Text.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/shapes/Text/Text.ts b/src/shapes/Text/Text.ts index b821759e5b7..d5eeb24324c 100644 --- a/src/shapes/Text/Text.ts +++ b/src/shapes/Text/Text.ts @@ -1,7 +1,11 @@ import { cache } from '../../cache'; import { DEFAULT_SVG_FONT_SIZE } from '../../constants'; import type { ObjectEvents } from '../../EventTypeDefs'; -import type { CompleteTextStyleDeclaration, TextStyle } from './StyledText'; +import type { + CompleteTextStyleDeclaration, + TextStyle, + TextStyleDeclaration, +} from './StyledText'; import { StyledText } from './StyledText'; import { SHARED_ATTRIBUTES } from '../../parser/attributes'; import { parseAttributes } from '../../parser/parseAttributes'; @@ -1642,7 +1646,12 @@ export class Text< fontStyle = this.fontStyle, fontWeight = this.fontWeight, fontSize = this.fontSize, - }: Partial = {}, + }: Partial< + Pick< + TextStyleDeclaration, + 'fontFamily' | 'fontStyle' | 'fontWeight' | 'fontSize' + > + > = {}, forMeasuring?: boolean ): string { const parsedFontFamily =