Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Text): _getFontDeclaration #9082

Merged
merged 4 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- fix(Text): `_getFontDeclaration` [#9082](https://github.com/fabricjs/fabric.js/pull/9082)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changelog doesn't tell much about the change. It should be more _getFontDeclaration: passed style should take precedence as the test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

point noted

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i also changed the pr description and the merge commit, but forgot to update the changelog.

- 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)
Expand Down
43 changes: 25 additions & 18 deletions src/shapes/Text/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,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,
Expand Down Expand Up @@ -1640,25 +1641,31 @@ 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<
Pick<
TextStyleDeclaration,
'fontFamily' | 'fontStyle' | 'fontWeight' | 'fontSize'
>
> = {},
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 [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a hot path method? (e.g. called when rendering) In that case you should check if a string interpolation ${fontStyle} ${fontWeight} etc. is significantly less expensive than creating an array on the fly and doing join

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we should do that. It is called more than once in rendering

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should recheck our code. We changed so much without measuring, and we don't even have a way of properly measuring.

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(' ');
}

Expand Down
2 changes: 2 additions & 0 deletions test/unit/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading