clip

package
v0.15.7 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 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 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 mask clips 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) 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 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(path []PathElement, antiAlias bool) error

PushPath pushes a path-based clip region onto the stack. The path is rasterized into a mask using the current bounds. If antiAlias is true, the mask will use anti-aliased rendering.

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

func (cs *ClipStack) Reset(bounds Rect)

Reset clears all clip entries and restores the original bounds.

type Close

type Close struct{}

Close closes the current subpath.

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 CubicTo

type CubicTo struct {
	Control1 Point
	Control2 Point
	Point    Point
}

CubicTo draws a cubic Bezier curve.

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 LineTo

type LineTo struct {
	Point Point
}

LineTo draws a line to a point.

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(elements []PathElement, bounds Rect, antiAlias bool) (*MaskClipper, error)

NewMaskClipper creates a mask clipper by rasterizing the given path elements into an alpha mask.

Parameters:

  • elements: Path elements to rasterize
  • 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 MoveTo

type MoveTo struct {
	Point Point
}

MoveTo moves to a point without drawing.

type PathElement

type PathElement interface {
	// contains filtered or unexported methods
}

PathElement represents a single element in a path (copy to avoid import cycle).

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 QuadTo

type QuadTo struct {
	Control Point
	Point   Point
}

QuadTo draws a quadratic Bezier curve.

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