From cf87cf2f397e6dbf3583db726bfdffe33785109b Mon Sep 17 00:00:00 2001 From: Wouter Horlings Date: Fri, 18 Jul 2025 16:05:28 +0200 Subject: [PATCH] WIP: line styling --- .golangci.yml | 3 +-- pkg/pattern/path/path.go | 36 +++++++++--------------------------- pkg/pattern/path/splines.go | 12 ++++++++---- pkg/pattern/path/style.go | 33 +++++++++++++++++++++++++++++++++ pkg/pattern/pattern.go | 4 ++-- pkg/pattern/template/line.go | 6 +++--- 6 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 pkg/pattern/path/style.go diff --git a/.golangci.yml b/.golangci.yml index 0d46f05..e29c38b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,10 +6,9 @@ linters: - nlreturn # covered by wsl cuddle rules - nonamedreturns # named returns are accepted - mnd - - gomnd # deprecated - exhaustruct - - execinquery + - tenv severity: default-severity: major diff --git a/pkg/pattern/path/path.go b/pkg/pattern/path/path.go index 36b2d7b..77c54d5 100644 --- a/pkg/pattern/path/path.go +++ b/pkg/pattern/path/path.go @@ -2,33 +2,25 @@ package path import ( - "image/color" - "git.wtrh.nl/patterns/gopatterns/pkg/pattern/point" "github.com/tdewolff/canvas" ) // Path defines a set of straight lines through points. type Path struct { - points []point.Point - thickness float64 - color color.RGBA + points []point.Point + style Style } // NewPath returns a new [Path]. -func NewPath(points ...point.Point) *Path { - black := canvas.Black - return &Path{points: points, color: black, thickness: 0.2} -} - -// NewPathWithStyle returns a new [Path] with the specified thickness and color. -func NewPathWithStyle(thickness float64, color color.RGBA, points ...point.Point) Path { - return Path{points: points, color: color, thickness: thickness} +func NewPath(points []point.Point) *Path { + return &Path{points: points, style: NewDefaultStyle()} } -// SetThickness updates the tickness of the line -func (p *Path) SetThickness(thickness float64) { - p.thickness = thickness +// WithStyle updates the style of the Path. +func (p *Path) WithStyle(Style Style) *Path { + p.style = Style + return p } // Draw the path to the provided [canvas.Canvas]. @@ -38,17 +30,7 @@ func (p *Path) Draw(c *canvas.Canvas) error { polyline.Add(next.Vector().Values()) } - c.RenderPath(polyline.ToPath(), - canvas.Style{ - Fill: canvas.Paint{}, - Stroke: canvas.Paint{Color: p.color}, - StrokeWidth: p.thickness, - StrokeCapper: canvas.RoundCap, - StrokeJoiner: canvas.BevelJoin, - DashOffset: 0, - Dashes: nil, - FillRule: 0, - }, canvas.Identity) + c.RenderPath(polyline.ToPath(), p.style.ToCanvas(), canvas.Identity) return nil } diff --git a/pkg/pattern/path/splines.go b/pkg/pattern/path/splines.go index 92cf81c..de8a413 100644 --- a/pkg/pattern/path/splines.go +++ b/pkg/pattern/path/splines.go @@ -2,6 +2,7 @@ package path import ( "fmt" + "image/color" "git.wtrh.nl/patterns/gopatterns/pkg/pattern/point" "github.com/tdewolff/canvas" @@ -19,9 +20,9 @@ type Spline struct { // NewSpline returns a new spline through points. Start and end points can be provided as // the start and stop direction of the spline. When start or end point arguments are left nil there // are no constraints on the direction. -func NewSpline(start, end point.Point, points ...point.Point) Spline { +func NewSpline(start, end point.Point, points []point.Point) Spline { s := Spline{ - Path: NewPath(points...), + Path: NewPath(points), start: start, end: end, } @@ -78,8 +79,11 @@ func (p Spline) Draw(c *canvas.Canvas) error { points = append(points, p.points[len(p.points)-1]) - path := NewPath(points...) - path.SetThickness(p.thickness) + path := NewPath(points).WithStyle(Style{ + Thickness: p.Path.style.Thickness, + Color: color.RGBA{}, + Dashes: nil, + }) if err = path.Draw(c); err != nil { return fmt.Errorf("draw spline points to canvas: %w", err) diff --git a/pkg/pattern/path/style.go b/pkg/pattern/path/style.go new file mode 100644 index 0000000..b430870 --- /dev/null +++ b/pkg/pattern/path/style.go @@ -0,0 +1,33 @@ +package path + +import ( + "image/color" + + "github.com/tdewolff/canvas" +) + +type Style struct { + Thickness float64 + Color color.RGBA + Dashes []float64 +} + +func (s Style) ToCanvas() canvas.Style { + return canvas.Style{ + Fill: canvas.Paint{}, + Stroke: canvas.Paint{Color: s.Color}, + StrokeWidth: s.Thickness, + StrokeCapper: canvas.RoundCap, + StrokeJoiner: canvas.BevelJoin, + DashOffset: 0, + Dashes: s.Dashes, + FillRule: 0, + } +} + +func NewDefaultStyle() Style { + return Style{ + Thickness: 0.2, + Color: canvas.Black, + } +} diff --git a/pkg/pattern/pattern.go b/pkg/pattern/pattern.go index 3d1ac70..e066893 100644 --- a/pkg/pattern/pattern.go +++ b/pkg/pattern/pattern.go @@ -11,7 +11,7 @@ import ( "gopkg.in/Knetic/govaluate.v3" ) -// Pattern contains all the points, lines and dimensions to draw a pattern to a canvas. +// The Pattern contains all the points, lines and dimensions to draw a pattern to a canvas. type Pattern struct { points map[point.ID]point.Point lines []pathDrawer @@ -34,7 +34,7 @@ func (p *Pattern) AddLine(line pathDrawer) { } // GetPoints returns a slice with points for the given IDs. -func (p *Pattern) GetPoints(id ...point.ID) []point.Point { +func (p *Pattern) GetPoints(id []point.ID) []point.Point { points := make([]point.Point, 0, len(id)) for _, i := range id { points = append(points, p.GetPoint(i)) diff --git a/pkg/pattern/template/line.go b/pkg/pattern/template/line.go index 1a10083..799dd27 100644 --- a/pkg/pattern/template/line.go +++ b/pkg/pattern/template/line.go @@ -28,7 +28,7 @@ type Curve struct { // Build adds the line to the provided [pattern.Pattern]. func (l Line) Build(pat *pattern.Pattern) error { - points := pat.GetPoints(l.Through...) + points := pat.GetPoints(l.Through) for _, p := range points { p.SetDraw() } @@ -37,7 +37,7 @@ func (l Line) Build(pat *pattern.Pattern) error { case l.Curve != nil: startPoint := pat.GetPoint(l.Curve.Start) endPoint := pat.GetPoint(l.Curve.End) - spline := path.NewSpline(startPoint, endPoint, points...) + spline := path.NewSpline(startPoint, endPoint, points).WithStyle(path.Style{}) if l.Style != nil && l.Style.Thickness != nil { spline.SetThickness(*l.Style.Thickness) @@ -45,7 +45,7 @@ func (l Line) Build(pat *pattern.Pattern) error { pat.AddLine(spline) default: - newPath := path.NewPath(points...) + newPath := path.NewPath(points) if l.Style != nil && l.Style.Thickness != nil { newPath.SetThickness(*l.Style.Thickness) }