-
Notifications
You must be signed in to change notification settings - Fork 560
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
Shape.bounds returns incorrect rectangle #623
Comments
Hi @mhdhejazi, Thanks for detailed description! I've pushed a fix. |
Actually it's not that easy. We have simple sample in tests: <path d="M10,10C40,10,65,10,95,80S150,150,180,80"/> And on this sample both In this case we need to use context, because at least it returns a bounding box. It's not minimal, but path bounding box completely wrong. So I'll revert this change and we need to wait Apple will fix it. |
I see. |
Agree, but it's the best we can do. In |
* master: (79 commits) Update .gitignore Add Package.swift Revert "Fix exyte#623: Shape.bounds returns incorrect rectangle" Fix exyte#623: Shape.bounds returns incorrect rectangle exyte#640 Remove force unwrap in `SVGParser.doubleFromString` method Fix crash in parsing pattern Formatting issues fix Fix gradient stops parsing Lint formatting issues fixed Formatting fixes fix source files of projects, add to macos target Enable Catalyst support (fixes exyte#610) Fix links in readme Update README.md Added supported platform - macOS Restore method Replace test references Revert "Add print to tests" Trigger build Add print to tests ... # Conflicts: # Source/render/GroupRenderer.swift # Source/svg/SVGParser.swift # Source/views/MacawView.swift
Consider the following simple shape:
<path d="M160 80.3848C160 187.051 0 187.051 0 80.3848C0 -26.2819 160 -26.2819 160 80.3848Z" fill="#C4C4C4"/>
As you can see the size is 160x160, but when I call
Shape.bounds
it returns:x: 0.000000, y: -26.281900, w: 160.000000, h: 213.332900
If we check the curves of this path closely we can understand what's happening. The returned rectangle is bigger because it also includes the control points:
I checked how the bounds are calculated and found out that the native method
CGContext. boundingBoxOfPath
is the culprit since it calculates the bounding box for all the points including the control points:While
CGPath.boundingBoxOfPath
, despite having the exact same name, doesn't include the control points in the calculation:I tried using
ctx.path?.boundingBoxOfPath
instead ofctx.boundingBoxOfPath
here:Macaw/Source/model/scene/Shape.swift
Line 68 in 3acbaff
and it returned the correct result:
x: 0.000000, y: 0.384775, w: 160.000000, h: 159.999675
Searching for
boundingBoxOfPath
in this repo I found out thatPath.bounds()
actually uses the correct method:Macaw/Source/model/geom2d/Path.swift
Lines 15 to 17 in 3acbaff
So I think this method can be used as a workaround until this is fixed (instead of
Shape.bounds
, one can useShape.form.bounds()
).The text was updated successfully, but these errors were encountered: