gpu,op/clip: implement stroked paths

Flat and Square caps are implemented.
Bevel joins are implemented.

Round caps, Round joins and Miter joins are left for another PR.

Signed-off-by: Sebastien Binet <s@sbinet.org>
This commit is contained in:
Sebastien Binet
2020-11-09 14:13:11 +00:00
committed by Elias Naur
parent 936eb52b7e
commit 33c5fb63db
9 changed files with 605 additions and 9 deletions
+24
View File
@@ -39,6 +39,8 @@ func (p *Path) Pos() f32.Point { return p.pen }
type Op struct {
call op.CallOp
bounds image.Rectangle
width float32 // Width of the stroked path, 0 for outline paths.
style StrokeStyle // Style of the stroked path, 0 for outline paths.
}
func (p Op) Add(o *op.Ops) {
@@ -50,6 +52,8 @@ func (p Op) Add(o *op.Ops) {
bo.PutUint32(data[5:], uint32(p.bounds.Min.Y))
bo.PutUint32(data[9:], uint32(p.bounds.Max.X))
bo.PutUint32(data[13:], uint32(p.bounds.Max.Y))
bo.PutUint32(data[17:], math.Float32bits(p.width))
data[21] = uint8(p.style.Cap)
}
// Begin the path, storing the path data and final Op into ops.
@@ -321,6 +325,26 @@ func (p *Path) Outline() Op {
}
}
// Stroke returns a stroked path with the specified width
// and configuration.
// If the provided width is <= 0, the path won't be stroked.
func (p *Path) Stroke(width float32, sty StrokeStyle) Op {
if width <= 0 {
// Explicitly discard the macro to ignore the path.
p.macro.Stop()
return Op{
call: op.Record(p.ops).Stop(),
}
}
c := p.macro.Stop()
return Op{
call: c,
width: width,
style: sty,
}
}
// Rect represents the clip area of a pixel-aligned rectangle.
type Rect image.Rectangle
+23
View File
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: Unlicense OR MIT
package clip
// StrokeStyle describes how a stroked path should be drawn.
// StrokeStyle zero value draws a Bevel-joined and Flat-capped stroked path.
type StrokeStyle struct {
Cap StrokeCap
}
// StrokeCap describes the head or tail of a stroked path.
type StrokeCap uint8
const (
// FlatCap caps stroked paths with a flat cap, joining the right-hand
// and left-hand sides of a stroked path with a straight line.
FlatCap StrokeCap = iota
// SquareCap caps stroked paths with a square cap, joining the right-hand
// and left-hand sides of a stroked path with a half square of length
// the stroked path's width.
SquareCap
)