Documentation ¶
Overview ¶
Package rasterx implements a rasterizer in go. By default rasterx uses ScannerGV to render images which uses the rasterizer in the golang.org/x/image/vector package. The freetype rasterizer under the GNU license can also be used, by downloading the scanFT package.
Copyright 2018 All rights reserved. Created: 5/12/2018 by S.R.Wiley
Index ¶
- Constants
- Variables
- func AddArc(points []float64, cx, cy, px, py float64, p Adder) (lx, ly float64)
- func AddCircle(cx, cy, r float64, p Adder)
- func AddEllipse(cx, cy, rx, ry, rot float64, p Adder)
- func AddRect(minX, minY, maxX, maxY, rot float64, p Adder)
- func AddRoundRect(minX, minY, maxX, maxY, rx, ry, rot float64, gf GapFunc, p Adder)
- func ApplyOpacity(c color.Color, opacity float64) color.NRGBA
- func CalcIntersect(a1, a2, b1, b2 fixed.Point26_6) (x fixed.Point26_6)
- func CircleCircleIntersection(ct, cl fixed.Point26_6, rt, rl fixed.Int26_6) (xt1, xt2 fixed.Point26_6, intersects bool)
- func ClosestPortside(bow, stern, p1, p2 fixed.Point26_6, isIntersecting bool) (xt fixed.Point26_6, intersects bool)
- func CubeTo(ax, ay, bx, by, cx, cy, dx, dy float32, LineTo func(ex, ey float32))
- func DotProd(p fixed.Point26_6, q fixed.Point26_6) fixed.Int52_12
- func FindEllipseCenter(ra, rb *float64, rotX, startX, startY, endX, endY float64, ...) (cx, cy float64)
- func GapToCap(p Adder, a, eNorm fixed.Point26_6, gf GapFunc)
- func Invert(v fixed.Point26_6) fixed.Point26_6
- func Length(v fixed.Point26_6) fixed.Int26_6
- func QuadTo(ax, ay, bx, by, cx, cy float32, LineTo func(dx, dy float32))
- func RadCurvature(p0, p1, p2 fixed.Point26_6, dm fixed.Int52_12) fixed.Int26_6
- func RayCircleIntersection(s1, s2, c fixed.Point26_6, r fixed.Int26_6) (x fixed.Point26_6, intersects bool)
- func RayCircleIntersectionF(s1X, s1Y, s2X, s2Y, cX, cY, r float64) (x, y float64, intersects bool)
- func ToFixedP(x, y float64) (p fixed.Point26_6)
- func ToLength(p fixed.Point26_6, ln fixed.Int26_6) (q fixed.Point26_6)
- type Adder
- type C2Point
- type CapFunc
- type ClipImage
- type ColorFunc
- type ColorFuncImage
- type Dasher
- func (r *Dasher) CubeBezier(b, c, d fixed.Point26_6)
- func (r *Dasher) Line(b fixed.Point26_6)
- func (r *Dasher) QuadBezier(b, c fixed.Point26_6)
- func (r *Dasher) SetStroke(width, miterLimit fixed.Int26_6, capL, capT CapFunc, gp GapFunc, jm JoinMode, ...)
- func (r *Dasher) Start(a fixed.Point26_6)
- func (r *Dasher) Stop(isClosed bool)
- type Filler
- func (r *Filler) Clear()
- func (r *Filler) CubeBezier(b, c, d fixed.Point26_6)
- func (r *Filler) CubeBezierF(sgm Rasterx, b, c, d fixed.Point26_6)
- func (r *Filler) Line(b fixed.Point26_6)
- func (r *Filler) QuadBezier(b, c fixed.Point26_6)
- func (r *Filler) QuadBezierF(sgm Rasterx, b, c fixed.Point26_6)
- func (r *Filler) SetBounds(width, height int)
- func (r *Filler) Start(a fixed.Point26_6)
- func (r *Filler) Stop(isClosed bool)
- type GapFunc
- type GradStop
- type Gradient
- type GradientUnits
- type JoinMode
- type Matrix2D
- func (a Matrix2D) Invert() Matrix2D
- func (a Matrix2D) Mult(b Matrix2D) Matrix2D
- func (a Matrix2D) Rotate(theta float64) Matrix2D
- func (a Matrix2D) Scale(x, y float64) Matrix2D
- func (a Matrix2D) SkewX(theta float64) Matrix2D
- func (a Matrix2D) SkewY(theta float64) Matrix2D
- func (a Matrix2D) TFixed(x fixed.Point26_6) (y fixed.Point26_6)
- func (a Matrix2D) Transform(x1, y1 float64) (x2, y2 float64)
- func (a Matrix2D) TransformVector(x1, y1 float64) (x2, y2 float64)
- func (a Matrix2D) Translate(x, y float64) Matrix2D
- type MatrixAdder
- type Path
- func (p Path) AddTo(q Adder)
- func (p *Path) Clear()
- func (p *Path) CubeBezier(b, c, d fixed.Point26_6)
- func (p *Path) Line(b fixed.Point26_6)
- func (p *Path) QuadBezier(b, c fixed.Point26_6)
- func (p *Path) Start(a fixed.Point26_6)
- func (p *Path) Stop(closeLoop bool)
- func (p Path) String() string
- func (p Path) ToSVGPath() string
- type PathCommand
- type Rasterx
- type Scanner
- type ScannerGV
- func (s *ScannerGV) Clear()
- func (s *ScannerGV) Draw()
- func (s *ScannerGV) GetPathExtent() fixed.Rectangle26_6
- func (s *ScannerGV) Line(b fixed.Point26_6)
- func (s *ScannerGV) SetBounds(width, height int)
- func (s *ScannerGV) SetClip(rect image.Rectangle)
- func (s *ScannerGV) SetColor(clr interface{})
- func (s *ScannerGV) SetWinding(useNonZeroWinding bool)
- func (s *ScannerGV) Start(a fixed.Point26_6)
- type SpreadMethod
- type Stroker
- func (r *Stroker) CalcEndCurvature(p0, p1, p2, q0, q1, q2 fixed.Point26_6, dm fixed.Int52_12, calcRadCuve bool)
- func (r *Stroker) CubeBezier(b, c, d fixed.Point26_6)
- func (r *Stroker) Joiner(p C2Point)
- func (r *Stroker) Line(b fixed.Point26_6)
- func (r *Stroker) LineSeg(sgm Rasterx, b fixed.Point26_6)
- func (r *Stroker) QuadBezier(b, c fixed.Point26_6)
- func (r *Stroker) SetStroke(width, miterLimit fixed.Int26_6, capL, capT CapFunc, gp GapFunc, jm JoinMode)
- func (r *Stroker) Start(a fixed.Point26_6)
- func (r *Stroker) Stop(isClosed bool)
Constants ¶
const MaxDx float64 = math.Pi / 8
MaxDx is the Maximum radians a cubic splice is allowed to span in ellipse parametric when approximating an off-axis ellipse.
Variables ¶
var ( // ButtCap caps lines with a straight line ButtCap CapFunc = func(p Adder, a, eNorm fixed.Point26_6) { p.Start(a.Add(eNorm)) p.Line(a.Sub(eNorm)) } // SquareCap caps lines with a square which is slightly longer than ButtCap SquareCap CapFunc = func(p Adder, a, eNorm fixed.Point26_6) { tpt := a.Add(turnStarboard90(eNorm)) p.Start(a.Add(eNorm)) p.Line(tpt.Add(eNorm)) p.Line(tpt.Sub(eNorm)) p.Line(a.Sub(eNorm)) } // RoundCap caps lines with a half-circle RoundCap CapFunc = func(p Adder, a, eNorm fixed.Point26_6) { GapToCap(p, a, eNorm, RoundGap) } // CubicCap caps lines with a cubic bezier CubicCap CapFunc = func(p Adder, a, eNorm fixed.Point26_6) { GapToCap(p, a, eNorm, CubicGap) } // QuadraticCap caps lines with a quadratic bezier QuadraticCap CapFunc = func(p Adder, a, eNorm fixed.Point26_6) { GapToCap(p, a, eNorm, QuadraticGap) } //FlatGap bridges miter-limit gaps with a straight line FlatGap GapFunc = func(p Adder, a, tNorm, lNorm fixed.Point26_6) { p.Line(a.Add(lNorm)) } // RoundGap bridges miter-limit gaps with a circular arc RoundGap GapFunc = func(p Adder, a, tNorm, lNorm fixed.Point26_6) { strokeArc(p, a, a.Add(tNorm), a.Add(lNorm), true, 0, 0, p.Line) p.Line(a.Add(lNorm)) } // CubicGap bridges miter-limit gaps with a cubic bezier CubicGap GapFunc = func(p Adder, a, tNorm, lNorm fixed.Point26_6) { p.CubeBezier(a.Add(tNorm).Add(turnStarboard90(tNorm)), a.Add(lNorm).Add(turnPort90(lNorm)), a.Add(lNorm)) } // QuadraticGap bridges miter-limit gaps with a quadratic bezier QuadraticGap GapFunc = func(p Adder, a, tNorm, lNorm fixed.Point26_6) { c1, c2 := a.Add(tNorm).Add(turnStarboard90(tNorm)), a.Add(lNorm).Add(turnPort90(lNorm)) cm := c1.Add(c2).Mul(fixed.Int26_6(1 << 5)) p.QuadBezier(cm, a.Add(lNorm)) } )
var Identity = Matrix2D{1, 0, 0, 1, 0, 0}
Identity is the identity matrix
Functions ¶
func AddEllipse ¶
AddEllipse adds an elipse with center at cx,cy, with the indicated x and y radius, (rx, ry), rotated around the center by rot degrees.
func AddRect ¶
AddRect adds a rectangle of the indicated size, rotated around the center by rot degrees.
func AddRoundRect ¶
AddRoundRect adds a rectangle of the indicated size, rotated around the center by rot degrees with rounded corners of radius rx in the x axis and ry in the y axis. gf specifes the shape of the filleting function. Valid values are RoundGap, QuadraticGap, CubicGap, FlatGap, or nil which defaults to a flat gap.
func ApplyOpacity ¶
ApplyOpacity sets the color's alpha channel to the given value
func CalcIntersect ¶
CalcIntersect calculates the points of intersection of two fixed point lines and panics if the determinate is zero. You have been warned.
func CircleCircleIntersection ¶
func CircleCircleIntersection(ct, cl fixed.Point26_6, rt, rl fixed.Int26_6) (xt1, xt2 fixed.Point26_6, intersects bool)
CircleCircleIntersection calculates the points of intersection of two circles or returns with intersects == false if no such points exist.
func ClosestPortside ¶
func ClosestPortside(bow, stern, p1, p2 fixed.Point26_6, isIntersecting bool) (xt fixed.Point26_6, intersects bool)
ClosestPortside returns the closest of p1 or p2 on the port side of the line from the bow to the stern. (port means left side of the direction you are heading) isIntersecting is just convienice to reduce code, and if false returns false, because p1 and p2 are not valid
func CubeTo ¶
CubeTo flattens the cubic Bezier curve into lines through the LineTo func This functions is adapted from the version found in golang.org/x/image/vector
func FindEllipseCenter ¶
func FindEllipseCenter(ra, rb *float64, rotX, startX, startY, endX, endY float64, sweep, smallArc bool) (cx, cy float64)
FindEllipseCenter locates the center of the Ellipse if it exists. If it does not exist, the radius values will be increased minimally for a solution to be possible while preserving the ra to rb ratio. ra and rb arguments are pointers that can be checked after the call to see if the values changed. This method uses coordinate transformations to reduce the problem to finding the center of a circle that includes the origin and an arbitrary point. The center of the circle is then transformed back to the original coordinates and returned.
func QuadTo ¶
QuadTo flattens the quadratic Bezier curve into lines through the LineTo func This functions is adapted from the version found in golang.org/x/image/vector
func RadCurvature ¶
RadCurvature returns the curvature of a Bezier curve end point, given an end point, the two adjacent control points and the degree. The sign of the value indicates if the center of the osculating circle is left or right (port or starboard) of the curve in the forward direction.
func RayCircleIntersection ¶
func RayCircleIntersection(s1, s2, c fixed.Point26_6, r fixed.Int26_6) (x fixed.Point26_6, intersects bool)
RayCircleIntersection calculates the points of intersection of a ray starting at s2 passing through s1 and a circle in fixed point. Returns intersects == false if no solution is possible. If two solutions are possible, the point closest to s2 is returned
func RayCircleIntersectionF ¶
RayCircleIntersectionF calculates in floating point the points of intersection of a ray starting at s2 passing through s1 and a circle in fixed point. Returns intersects == false if no solution is possible. If two solutions are possible, the point closest to s2 is returned
Types ¶
type Adder ¶
type Adder interface { // Start starts a new curve at the given point. Start(a fixed.Point26_6) // Line adds a line segment to the path Line(b fixed.Point26_6) // QuadBezier adds a quadratic bezier curve to the path QuadBezier(b, c fixed.Point26_6) // CubeBezier adds a cubic bezier curve to the path CubeBezier(b, c, d fixed.Point26_6) // Closes the path to the start point if closeLoop is true Stop(closeLoop bool) }
Adder interface for types that can accumlate path commands
type C2Point ¶
C2Point represents a point that connects two stroke segments and holds the tangent, normal and radius of curvature of the trailing and leading segments in fixed point values.
type ClipImage ¶
type ClipImage struct { ColorFuncImage // contains filtered or unexported fields }
ClipImage is a clipable ColorFuncImage
type ColorFuncImage ¶
ColorFuncImage implements and image using the provided colorFunc
type Dasher ¶
type Dasher struct { Stroker Dashes []fixed.Int26_6 DashOffset fixed.Int26_6 // contains filtered or unexported fields }
Dasher struct extends the Stroker and can draw dashed lines with end capping
func NewDasher ¶
NewDasher returns a Dasher ptr with default values. A Dasher has all of the capabilities of a Stroker, Filler, and Scanner, plus the ability to stroke curves with solid lines. Use SetStroke to configure with non-default values.
func (*Dasher) CubeBezier ¶
CubeBezier starts a stroked cubic bezier. It is a low level function exposed for the purposes of callbacks and debugging.
func (*Dasher) QuadBezier ¶
QuadBezier for dashing
func (*Dasher) SetStroke ¶
func (r *Dasher) SetStroke(width, miterLimit fixed.Int26_6, capL, capT CapFunc, gp GapFunc, jm JoinMode, dashes []float64, dashOffset float64)
SetStroke set the parameters for stroking a line. width is the width of the line, miterlimit is the miter cutoff value for miter, arc, miterclip and arcClip joinModes. CapL and CapT are the capping functions for leading and trailing line ends. If one is nil, the other function is used at both ends. gp is the gap function that determines how a gap on the convex side of two lines joining is filled. jm is the JoinMode for curve segments. Dashes is the values for the dash pattern. Pass in nil or an empty slice for no dashes. dashoffset is the starting offset into the dash array.
type Filler ¶
type Filler struct { Scanner // contains filtered or unexported fields }
Filler satisfies Rasterx
func NewFiller ¶
NewFiller returns a Filler ptr with default values. A Filler in addition to rasterizing lines like a Scann, can also rasterize quadratic and cubic bezier curves. If Scanner is nil default scanner ScannerGV is used
func (*Filler) CubeBezier ¶
CubeBezier adds a cubic bezier to the curve
func (*Filler) CubeBezierF ¶
CubeBezierF adds a cubic bezier to the curve. sending the line calls the the sgm Rasterizer
func (*Filler) QuadBezier ¶
QuadBezier adds a quadratic segment to the current curve.
func (*Filler) QuadBezierF ¶
QuadBezierF adds a quadratic segment to the sgm Rasterizer.
func (*Filler) SetBounds ¶
SetBounds sets the maximum width and height of the rasterized image and calls Clear. The width and height are in pixels, not fixed.Int26_6 units.
type Gradient ¶
type Gradient struct { Points [5]float64 Stops []GradStop Bounds struct{ X, Y, W, H float64 } Matrix Matrix2D Spread SpreadMethod Units GradientUnits IsRadial bool }
Gradient holds a description of an SVG 2.0 gradient
func (*Gradient) GetColorFunction ¶
GetColorFunction returns the color function
func (*Gradient) GetColorFunctionUS ¶
GetColorFunctionUS returns the color function using the User Space objMatrix
type GradientUnits ¶
type GradientUnits byte
GradientUnits is the type for gradient units
const ( ObjectBoundingBox GradientUnits = iota UserSpaceOnUse )
SVG bounds paremater constants
type Matrix2D ¶
type Matrix2D struct {
A, B, C, D, E, F float64
}
Matrix2D represents an SVG style matrix
func (Matrix2D) Transform ¶
Transform multiples the input vector by matrix m and outputs the results vector components.
func (Matrix2D) TransformVector ¶
TransformVector is a modidifed version of Transform that ignores the translation components.
type MatrixAdder ¶
MatrixAdder is an adder that applies matrix M to all points
func (*MatrixAdder) CubeBezier ¶
func (t *MatrixAdder) CubeBezier(b, c, d fixed.Point26_6)
CubeBezier adds a cubic segment to the current curve.
func (*MatrixAdder) Line ¶
func (t *MatrixAdder) Line(b fixed.Point26_6)
Line adds a linear segment to the current curve.
func (*MatrixAdder) QuadBezier ¶
func (t *MatrixAdder) QuadBezier(b, c fixed.Point26_6)
QuadBezier adds a quadratic segment to the current curve.
type Path ¶
A Path starts with a PathCommand value followed by zero to three fixed int points.
func (*Path) CubeBezier ¶
CubeBezier adds a cubic segment to the current curve.
func (*Path) QuadBezier ¶
QuadBezier adds a quadratic segment to the current curve.
type PathCommand ¶
PathCommand is the type for the path command token
const ( PathMoveTo PathCommand = iota PathLineTo PathQuadTo PathCubicTo PathClose )
Human readable path constants
type Rasterx ¶
type Rasterx interface { Adder // contains filtered or unexported methods }
Rasterx extends the adder interface to include lineF and joinF functions
type Scanner ¶
type Scanner interface { Start(a fixed.Point26_6) Line(b fixed.Point26_6) Draw() GetPathExtent() fixed.Rectangle26_6 SetBounds(w, h int) SetColor(color interface{}) SetWinding(useNonZeroWinding bool) Clear() // SetClip sets an optional clipping rectangle to restrict rendering // only to that region -- if size is 0 then ignored (set to image.ZR // to clear) SetClip(rect image.Rectangle) }
Scanner interface for path generating types
type ScannerGV ¶
type ScannerGV struct { //a, first fixed.Point26_6 Dest draw.Image Targ image.Rectangle Source image.Image Offset image.Point // contains filtered or unexported fields }
ScannerGV uses the google vector rasterizer
func NewScannerGV ¶
NewScannerGV creates a new Scanner with the given bounds.
func (*ScannerGV) Draw ¶
func (s *ScannerGV) Draw()
Draw renders the accumulate scan to the desination
func (*ScannerGV) GetPathExtent ¶
func (s *ScannerGV) GetPathExtent() fixed.Rectangle26_6
GetPathExtent returns the extent of the path
func (*ScannerGV) SetBounds ¶
SetBounds sets the maximum width and height of the rasterized image and calls Clear. The width and height are in pixels, not fixed.Int26_6 units.
func (*ScannerGV) SetClip ¶
SetClip sets an optional clipping rectangle to restrict rendering only to that region -- if size is 0 then ignored (set to image.ZR to clear)
func (*ScannerGV) SetColor ¶
func (s *ScannerGV) SetColor(clr interface{})
SetColor set the color type for the scanner
func (*ScannerGV) SetWinding ¶
SetWinding set the winding rule for the scanner
type SpreadMethod ¶
type SpreadMethod byte
SpreadMethod is the type for spread parameters
const ( PadSpread SpreadMethod = iota ReflectSpread RepeatSpread )
SVG spread parameter constants
type Stroker ¶
type Stroker struct { Filler CapT, CapL CapFunc // Trailing and leading cap funcs may be set separately JoinGap GapFunc // When gap appears between segments, this function is called JoinMode JoinMode // contains filtered or unexported fields }
Stroker does everything a Filler does, but also allows for stroking and dashed stroking in addition to filling
func NewStroker ¶
NewStroker returns a ptr to a Stroker with default values. A Stroker has all of the capabilities of a Filler and Scanner, plus the ability to stroke curves with solid lines. Use SetStroke to configure with non-default values.
func (*Stroker) CalcEndCurvature ¶
func (r *Stroker) CalcEndCurvature(p0, p1, p2, q0, q1, q2 fixed.Point26_6, dm fixed.Int52_12, calcRadCuve bool)
CalcEndCurvature calculates the radius of curvature given the control points of a bezier curve. It is a low level function exposed for the purposes of callbacks and debugging.
func (*Stroker) CubeBezier ¶
CubeBezier starts a stroked quadratic bezier.
func (*Stroker) Joiner ¶
Joiner is called when two segments of a stroke are joined. it is exposed so that if can be wrapped to generate callbacks for the join points.
func (*Stroker) QuadBezier ¶
QuadBezier starts a stroked quadratic bezier.
func (*Stroker) SetStroke ¶
func (r *Stroker) SetStroke(width, miterLimit fixed.Int26_6, capL, capT CapFunc, gp GapFunc, jm JoinMode)
SetStroke set the parameters for stroking a line. width is the width of the line, miterlimit is the miter cutoff value for miter, arc, miterclip and arcClip joinModes. CapL and CapT are the capping functions for leading and trailing line ends. If one is nil, the other function is used at both ends. If both are nil, both ends are ButtCapped. gp is the gap function that determines how a gap on the convex side of two joining lines is filled. jm is the JoinMode for curve segments.