Skip to content
snap_to_pixel_edge

snap_to_pixel_edge

Mojo function 🡭

snap_to_pixel_edge

fn def snap_to_pixel_edge(value: Float64) -> Float64

value moved to the nearest pixel boundary, for the edge of an axis-aligned filled rectangle.

Pixel k spans k - 0.5 to k + 0.5, so the boundaries are the half-integers and this returns the nearest one. A rectangle edge landing between two boundaries reads as a soft edge, and a hard edge is worth more there than the fraction; paths, strokes and text keep their exact geometry, which is what makes them sharp.

Snapping in user space matters under begin_supersampled: the Float64 fill_rect snaps in device space, which turns a user-space fraction into an anti-aliased edge after the downsample rather than removing it. A user-space snap puts the mapped edge on a device block boundary, so the downsampled edge is hard. Both place the edge on the same pixel; only the crispness differs.

The tie, a coordinate exactly on a pixel center, is equidistant from the boundary on either side and is where data lands all the time: a symmetric domain’s midpoint, a category band’s center. The rule here sends it to the boundary above, and _SNAP_TIE_TOLERANCE makes a coordinate one ULP under the center follow the same rule, so intercept + slope * v landing at 2.4999999999999996 snaps where 2.5 does rather than a whole pixel away, and two platforms whose last bit differs agree.

Args:

  • value (Float64): A user-space coordinate.

Returns:

Float64: The nearest half-integer.