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(Fabric.text): account for fontSize in textpath dimensions #7298

Merged
merged 5 commits into from
Aug 26, 2021

Conversation

melchiar
Copy link
Member

Text on a path is currently using the dimensions of the assigned path which can cause the cache size to be too small.

image

This fix adds the calculated text height to the object's dimensions to ensure that nothing is clipped in the cache canvas.
image

Honestly though I'm not crazy about this solution since depending on the path's shape and direction this means the bounding box will sometimes be unnecessarily large. Let me know if you can think of a better idea.

image

@melchiar
Copy link
Member Author

melchiar commented Aug 14, 2021

Obviously this change would require fixing all of the visual tests since the new dimensions change the positioning. I'd like to try to find another solution to this issue if we can though. I've experimented with simply factoring the font size into the cache size calculation and leaving the object's bounding dimensions untouched but doing that seems to cause the cache's resolution suffer.

@asturur
Copy link
Member

asturur commented Aug 14, 2021

a better, but complicated idea, would be to get the object size looking at the Path + the character boxes we create. For each char we have the width and the position + angle. That means we can collect all the 4 corners of each character and throw inside a bucket of points that will give us a bounding box.

src/shapes/text.class.js Outdated Show resolved Hide resolved
@melchiar
Copy link
Member Author

a better, but complicated idea, would be to get the object size looking at the Path + the character boxes we create. For each char we have the width and the position + angle. That means we can collect all the 4 corners of each character and throw inside a bucket of points that will give us a bounding box.

That might be a bit over complicated. I'm imagining a scenario like this where text is curved around the top and bottom of two circles. Aligning the two circles to each other is a lot easier if the bounding boxes are uniform and you can use the same center position.

image

@github-actions
Copy link
Contributor

github-actions bot commented Aug 24, 2021

Code Coverage Summary

> fabric@4.5.1 coverage:report /home/runner/work/fabric.js/fabric.js
> nyc report --reporter=lcov --reporter=text

-----------|---------|----------|---------|---------|-----------------------------------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s                             
-----------|---------|----------|---------|---------|-----------------------------------------------
All files  |   83.31 |    77.06 |   86.01 |   83.02 |                                               
 fabric.js |   83.31 |    77.06 |   86.01 |   83.02 | ...,29853,29978,30058-30123,30246,30345,30562 
-----------|---------|----------|---------|---------|-----------------------------------------------

@melchiar
Copy link
Member Author

I've decided to take a different approach and simply adjust the cache dimensions based on the font size. I think this is a better solution than increasing the text object's dimensions since from a UX perspective the path dimensions are more important in my opinion (this is also the way Illustrator does it).

Anyone who wants to include the text in the bounding box can do so with padding.

@melchiar
Copy link
Member Author

No clipping!
text-path-caching

shouldRedraw = true;
shouldResizeCanvas = true;
additionalWidth = this.fontSize * this.zoomX;
additionalHeight = this.fontSize * this.zoomY;
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't those be additionalWidth += this.fontSize * this.zoomX to take in account the 0.1 extra size of the previous step?
Still looking at the first line height is better.
You don't want a weird styled text to fool you with fontSize 2 on the object and fontSize 45 on the style

Copy link
Member Author

Choose a reason for hiding this comment

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

good point!

@asturur
Copy link
Member

asturur commented Aug 26, 2021

ok just the tests are left out, i can do this in a bit.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 26, 2021

Code Coverage Summary

> fabric@4.5.1 coverage:report /home/runner/work/fabric.js/fabric.js
> nyc report --reporter=lcov --reporter=text

-----------|---------|----------|---------|---------|-----------------------------------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s                             
-----------|---------|----------|---------|---------|-----------------------------------------------
All files  |   83.31 |    77.04 |   86.01 |   83.02 |                                               
 fabric.js |   83.31 |    77.04 |   86.01 |   83.02 | ...,29853,29978,30058-30123,30246,30345,30562 
-----------|---------|----------|---------|---------|-----------------------------------------------

@github-actions
Copy link
Contributor

github-actions bot commented Aug 26, 2021

Code Coverage Summary

> fabric@4.5.1 coverage:report /home/runner/work/fabric.js/fabric.js
> nyc report --reporter=lcov --reporter=text

-----------|---------|----------|---------|---------|-----------------------------------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s                             
-----------|---------|----------|---------|---------|-----------------------------------------------
All files  |   83.31 |    77.06 |   86.01 |   83.02 |                                               
 fabric.js |   83.31 |    77.06 |   86.01 |   83.02 | ...,29853,29978,30058-30123,30246,30345,30562 
-----------|---------|----------|---------|---------|-----------------------------------------------

@asturur asturur merged commit c4c1b7d into master Aug 26, 2021
@asturur asturur mentioned this pull request Aug 27, 2021
@emielmolenaar
Copy link

Thanks for this fix! Nice work.

@asturur asturur deleted the textpath-improvements branch February 4, 2022 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants