Documentation
¶
Overview ¶
Package clip provides geometric clipping for paths and shapes.
Index ¶
- type ClipStack
- func (cs *ClipStack) Bounds() Rect
- func (cs *ClipStack) Coverage(x, y float64) byte
- func (cs *ClipStack) Depth() int
- func (cs *ClipStack) IsRRectOnly() bool
- func (cs *ClipStack) IsRectOnly() bool
- func (cs *ClipStack) IsVisible(x, y float64) bool
- func (cs *ClipStack) Pop()
- func (cs *ClipStack) PushPath(verbs []PathVerb, coords []float64, antiAlias bool) error
- func (cs *ClipStack) PushRRect(r Rect, radius float64)
- func (cs *ClipStack) PushRect(r Rect)
- func (cs *ClipStack) RRectBounds() (Rect, float64, bool)
- func (cs *ClipStack) Reset(bounds Rect)
- type CubicSeg
- type EdgeClipper
- type LineSeg
- type MaskClipper
- type PathVerb
- type Point
- type QuadSeg
- type RRectClip
- type Rect
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClipStack ¶
type ClipStack struct {
// contains filtered or unexported fields
}
ClipStack manages hierarchical clip regions with push/pop operations. It maintains a stack of clip entries, where each entry can be either a rectangular clip, a rounded rectangle clip, or a path-based mask clip.
func NewClipStack ¶
NewClipStack creates a new clip stack with the given bounds. The bounds represent the maximum clipping area (typically the canvas size).
func (*ClipStack) Bounds ¶
Bounds returns the current effective clip bounds. This is the intersection of all pushed clip regions.
func (*ClipStack) Coverage ¶
Coverage returns the combined coverage value (0-255) at the given point. This multiplies the coverage from all clip entries (RRect and mask) in the stack. Returns 0 if the point is outside the current bounds.
func (*ClipStack) Depth ¶
Depth returns the current depth of the clip stack. This is primarily useful for debugging and testing.
func (*ClipStack) IsRRectOnly ¶ added in v0.36.4
IsRRectOnly reports whether the clip stack contains only rectangular and/or rounded rectangle clips (no path-based masks). When true, clipping can be handled via hardware scissor rect (for the bounding box) plus analytic SDF in the fragment shader (for rounded corners).
func (*ClipStack) IsRectOnly ¶ added in v0.36.1
IsRectOnly reports whether the clip stack contains only rectangular clips (no path-based masks and no rounded rectangle clips). When true, clipping can be applied by restricting the destination bounds rather than per-pixel coverage, which preserves bitmap text quality and is significantly faster.
func (*ClipStack) IsVisible ¶
IsVisible returns true if the point (x, y) is within the current clip region. For rectangular clips, this is a simple bounds check. For RRect clips, this checks the SDF distance. For mask clips, this checks if the coverage is non-zero.
func (*ClipStack) Pop ¶
func (cs *ClipStack) Pop()
Pop removes the most recent clip region from the stack. If the stack is empty, this is a no-op.
func (*ClipStack) PushPath ¶
PushPath pushes a path-based clip region onto the stack. The path (given as SOA verb+coords) is rasterized into a mask using the current bounds. If antiAlias is true, the mask will use anti-aliased rendering.
func (*ClipStack) PushRRect ¶ added in v0.36.4
PushRRect pushes a rounded rectangle clip region onto the stack. The new clip bounds are the intersection of the current bounds and the given rectangle. The radius is the corner radius (clamped to half the minimum dimension). If radius is zero, this is equivalent to PushRect.
func (*ClipStack) PushRect ¶
PushRect pushes a rectangular clip region onto the stack. The new clip bounds are the intersection of the current bounds and the given rectangle.
func (*ClipStack) RRectBounds ¶ added in v0.36.4
RRectBounds returns the bounds and radius of the innermost (most recently pushed) rounded rectangle clip entry, or false if no RRect clip is active. When multiple RRect clips are nested, only the innermost is returned — the scissor rect already handles the outer bounds intersection.
type CubicSeg ¶
type CubicSeg struct {
P0, P1, P2, P3 Point
}
CubicSeg represents a cubic Bezier segment.
type EdgeClipper ¶
type EdgeClipper struct {
// contains filtered or unexported fields
}
EdgeClipper clips path edges against a rectangular clip region. Inspired by tiny-skia edge_clipper.rs and Skia's SkEdgeClipper.
func NewEdgeClipper ¶
func NewEdgeClipper(clip Rect) *EdgeClipper
NewEdgeClipper creates an edge clipper for the given bounds.
func (*EdgeClipper) ClipCubic ¶
func (ec *EdgeClipper) ClipCubic(p0, p1, p2, p3 Point) []CubicSeg
ClipCubic clips a cubic Bezier curve to the clip region.
func (*EdgeClipper) ClipLine ¶
func (ec *EdgeClipper) ClipLine(p0, p1 Point) []LineSeg
ClipLine clips a line segment to the clip rectangle using Cohen-Sutherland algorithm. Returns nil if the line is entirely outside, or a slice with the clipped segment.
func (*EdgeClipper) ClipQuadratic ¶
func (ec *EdgeClipper) ClipQuadratic(p0, p1, p2 Point) []QuadSeg
ClipQuadratic clips a quadratic Bezier curve to the clip region. The curve is first chopped at extrema to ensure monotonicity, then clipped.
type MaskClipper ¶
type MaskClipper struct {
// contains filtered or unexported fields
}
MaskClipper performs alpha mask-based clipping for anti-aliased complex clips. It rasterizes a path into a grayscale mask where each pixel's value represents coverage (0 = outside, 255 = fully inside).
func NewMaskClipper ¶
func NewMaskClipper(verbs []PathVerb, coords []float64, bounds Rect, antiAlias bool) (*MaskClipper, error)
NewMaskClipper creates a mask clipper by rasterizing the given path (as SOA verb+coords slices) into an alpha mask.
Parameters:
- verbs: Path verb stream
- coords: Path coordinate stream
- bounds: Bounding rectangle for the mask
- antiAlias: Enable anti-aliased rendering (currently always on)
The mask is stored as FormatGray8 (1 byte per pixel) for memory efficiency.
func (*MaskClipper) ApplyCoverage ¶
func (mc *MaskClipper) ApplyCoverage(x, y float64, srcAlpha byte) byte
ApplyCoverage modulates the source alpha by the mask coverage at the given point. Returns the modulated alpha value (0-255).
func (*MaskClipper) Bounds ¶
func (mc *MaskClipper) Bounds() Rect
Bounds returns the bounding rectangle of the mask.
func (*MaskClipper) Coverage ¶
func (mc *MaskClipper) Coverage(x, y float64) byte
Coverage returns the coverage value (0-255) at the given point. Points outside the mask bounds return 0 (no coverage).
func (*MaskClipper) Mask ¶
func (mc *MaskClipper) Mask() *image.ImageBuf
Mask returns the underlying grayscale image buffer. This is useful for debugging or advanced use cases.
type PathVerb ¶ added in v0.39.0
type PathVerb byte
PathVerb represents a path construction command. Values match gg.PathVerb for zero-cost conversion.
const ( // VerbMoveTo moves the current point. Consumes 2 coords (x, y). VerbMoveTo PathVerb = iota // VerbLineTo draws a line. Consumes 2 coords (x, y). VerbLineTo // VerbQuadTo draws a quadratic Bezier. Consumes 4 coords (cx, cy, x, y). VerbQuadTo // VerbCubicTo draws a cubic Bezier. Consumes 6 coords (c1x, c1y, c2x, c2y, x, y). VerbCubicTo // VerbClose closes the current subpath. Consumes 0 coords. VerbClose )
type Point ¶
type Point struct {
X, Y float64
}
Point represents a 2D point with float64 coordinates.
type QuadSeg ¶
type QuadSeg struct {
P0, P1, P2 Point
}
QuadSeg represents a quadratic Bezier segment.
type RRectClip ¶ added in v0.36.4
RRectClip describes a rounded rectangle clip region. The Rect field holds the bounding rectangle in device coordinates, and Radius is the corner radius (uniform for all four corners).
type Rect ¶
Rect represents a rectangle with float64 coordinates.
func (Rect) Intersect ¶
Intersect returns the intersection of two rectangles. Returns an empty rectangle if they don't intersect.
func (Rect) Intersects ¶
Intersects returns true if two rectangles overlap.