clip

package
v0.41.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package clip provides geometric clipping for paths and shapes.

Index

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

func NewClipStack(bounds Rect) *ClipStack

NewClipStack creates a new clip stack with the given bounds. The bounds represent the maximum clipping area (typically the canvas size).

func (*ClipStack) Bounds

func (cs *ClipStack) Bounds() Rect

Bounds returns the current effective clip bounds. This is the intersection of all pushed clip regions.

func (*ClipStack) Coverage

func (cs *ClipStack) Coverage(x, y float64) byte

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

func (cs *ClipStack) Depth() int

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

func (cs *ClipStack) IsRRectOnly() bool

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

func (cs *ClipStack) IsRectOnly() bool

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

func (cs *ClipStack) IsVisible(x, y float64) bool

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

func (cs *ClipStack) PushPath(verbs []PathVerb, coords []float64, antiAlias bool) error

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

func (cs *ClipStack) PushRRect(r Rect, radius float64)

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

func (cs *ClipStack) PushRect(r Rect)

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

func (cs *ClipStack) RRectBounds() (Rect, float64, bool)

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.

func (*ClipStack) Reset

func (cs *ClipStack) Reset(bounds Rect)

Reset clears all clip entries and restores the original bounds.

type CubicSeg

type CubicSeg struct {
	P0, P1, P2, P3 Point
}

CubicSeg represents a cubic Bezier segment.

func (CubicSeg) Bounds

func (c CubicSeg) Bounds() Rect

Bounds returns the bounding box of a cubic Bezier.

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) Clip

func (ec *EdgeClipper) Clip() Rect

Clip returns the clip rectangle.

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 LineSeg

type LineSeg struct {
	P0, P1 Point
}

LineSeg represents a line segment.

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.

func Pt

func Pt(x, y float64) Point

Pt creates a Point from x, y coordinates.

func (Point) Add

func (p Point) Add(q Point) Point

Add returns the sum of two points.

func (Point) Lerp

func (p Point) Lerp(q Point, t float64) Point

Lerp performs linear interpolation between p and q.

func (Point) Mul

func (p Point) Mul(s float64) Point

Mul returns the point scaled by s.

func (Point) Sub

func (p Point) Sub(q Point) Point

Sub returns the difference of two points.

type QuadSeg

type QuadSeg struct {
	P0, P1, P2 Point
}

QuadSeg represents a quadratic Bezier segment.

func (QuadSeg) Bounds

func (q QuadSeg) Bounds() Rect

Bounds returns the bounding box of a quadratic Bezier.

type RRectClip added in v0.36.4

type RRectClip struct {
	Rect   Rect
	Radius float64
}

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

type Rect struct {
	X, Y float64 // Top-left corner
	W, H float64 // Width and height
}

Rect represents a rectangle with float64 coordinates.

func NewRect

func NewRect(x, y, w, h float64) Rect

NewRect creates a Rect from position and size.

func (Rect) Bottom

func (r Rect) Bottom() float64

Bottom returns the bottom edge y-coordinate.

func (Rect) Contains

func (r Rect) Contains(p Point) bool

Contains returns true if the point is inside the rectangle.

func (Rect) Intersect

func (r Rect) Intersect(other Rect) Rect

Intersect returns the intersection of two rectangles. Returns an empty rectangle if they don't intersect.

func (Rect) Intersects

func (r Rect) Intersects(other Rect) bool

Intersects returns true if two rectangles overlap.

func (Rect) IsEmpty

func (r Rect) IsEmpty() bool

IsEmpty returns true if the rectangle has zero area.

func (Rect) Right

func (r Rect) Right() float64

Right returns the right edge x-coordinate.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL