CodingMeSwiftly / UIBezierPath-Superpowers

MIT License
78 stars 16 forks source link

Zero element length crashes #6

Closed CodingMeSwiftly closed 6 years ago

CodingMeSwiftly commented 6 years ago

Fixes issue: https://github.com/ImJCabus/UIBezierPath-Superpowers/issues/5

Most of the public API requires the user to pass a fraction parameter called atFractionOfLength. Internally, we use this fraction to retrieve the path element at the given fraction of the paths total length to do further calculations. Hence, there is really no way of accounting for zero length path elements without restructuring most of the library.

Nevertheless, users may create UIBezierPath instances composed of such elements and iOS will actually draw each "dot" in the path taking into account the provided lineWidth, lineCap, etc., so we need to do something about this.

To prevent crashes, we give every path element a length. This means we can leave all calculations as is. Inherently "empty" elements will receive a fixed length value.

After some reasoning, we decided to set this fixed value to 1. It's a reasonable default value which should represent "dots" in the path quite well.

If there was any possibility we could access path drawing parameters, we could actually provide a much more fitting value. The properties on UIBezierPath (lineWidth, lineCapStyle) are no good, because UIBezierPath is frequently used in conjunction with CAShapeLayer and UIKit (CoreGraphics actually) will use the properties provided there to draw the path and ignore properties of the path itself.

This is the best and swiftest (badum tss) fix I can think of right now. Maybe in the future it's worth revisiting this, but for the moment I thing we're good.

CodingMeSwiftly commented 6 years ago

Also fixes https://github.com/ImJCabus/UIBezierPath-Superpowers/issues/1