op/paint: rename PathBuilder to Path

There was no "Path" to "Build", so let's just use the simpler name.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-10-03 13:34:01 +02:00
parent 96d3d74cb3
commit 6dad034b22
2 changed files with 21 additions and 23 deletions
+19 -21
View File
@@ -13,12 +13,11 @@ import (
"gioui.org/op" "gioui.org/op"
) )
// PathBuilder builds and adds a general ClipOp clip path // Path constructs and adds a ClipOp clip path consisting of lines and
// from lines and curves. // Bézier curves. Path generates no garbage and can be used for
// PathBuilder generates no garbage and can be used for // dynamic paths; path data is stored directly in the Ops list
// dynamic paths; path data is stored directly in the Ops // supplied to Begin.
// list supplied to Init. type Path struct {
type PathBuilder struct {
ops *op.Ops ops *op.Ops
firstVert int firstVert int
nverts int nverts int
@@ -44,14 +43,13 @@ func (p ClipOp) Add(o *op.Ops) {
o.Write(data) o.Write(data)
} }
// Init the builder and specify the operations list for // Begin the path, storing the path data and final ClipOp into ops.
// storing the path data and final ClipOp. func (p *Path) Begin(ops *op.Ops) {
func (p *PathBuilder) Init(ops *op.Ops) {
p.ops = ops p.ops = ops
} }
// MoveTo moves the pen to the given position. // MoveTo moves the pen to the given position.
func (p *PathBuilder) Move(to f32.Point) { func (p *Path) Move(to f32.Point) {
p.end() p.end()
to = to.Add(p.pen) to = to.Add(p.pen)
p.maxy = to.Y p.maxy = to.Y
@@ -59,7 +57,7 @@ func (p *PathBuilder) Move(to f32.Point) {
} }
// end completes the current contour. // end completes the current contour.
func (p *PathBuilder) end() { func (p *Path) end() {
aux := p.ops.Aux() aux := p.ops.Aux()
bo := binary.LittleEndian bo := binary.LittleEndian
// Fill in maximal Y coordinates of the NW and NE corners. // Fill in maximal Y coordinates of the NW and NE corners.
@@ -71,25 +69,25 @@ func (p *PathBuilder) end() {
} }
// Line records a line from the pen to end. // Line records a line from the pen to end.
func (p *PathBuilder) Line(to f32.Point) { func (p *Path) Line(to f32.Point) {
to = to.Add(p.pen) to = to.Add(p.pen)
p.lineTo(to) p.lineTo(to)
} }
func (p *PathBuilder) lineTo(to f32.Point) { func (p *Path) lineTo(to f32.Point) {
// Model lines as degenerate quadratic Béziers. // Model lines as degenerate quadratic Béziers.
p.quadTo(to.Add(p.pen).Mul(.5), to) p.quadTo(to.Add(p.pen).Mul(.5), to)
} }
// Quad records a quadratic Bézier from the pen to end // Quad records a quadratic Bézier from the pen to end
// with the control point ctrl. // with the control point ctrl.
func (p *PathBuilder) Quad(ctrl, to f32.Point) { func (p *Path) Quad(ctrl, to f32.Point) {
ctrl = ctrl.Add(p.pen) ctrl = ctrl.Add(p.pen)
to = to.Add(p.pen) to = to.Add(p.pen)
p.quadTo(ctrl, to) p.quadTo(ctrl, to)
} }
func (p *PathBuilder) quadTo(ctrl, to f32.Point) { func (p *Path) quadTo(ctrl, to f32.Point) {
// Zero width curves don't contribute to stenciling. // Zero width curves don't contribute to stenciling.
if p.pen.X == to.X && p.pen.X == ctrl.X { if p.pen.X == to.X && p.pen.X == ctrl.X {
p.pen = to p.pen = to
@@ -144,7 +142,7 @@ func (p *PathBuilder) quadTo(ctrl, to f32.Point) {
// Cube records a cubic Bézier from the pen through // Cube records a cubic Bézier from the pen through
// two control points ending in to. // two control points ending in to.
func (p *PathBuilder) Cube(ctrl0, ctrl1, to f32.Point) { func (p *Path) Cube(ctrl0, ctrl1, to f32.Point) {
ctrl0 = ctrl0.Add(p.pen) ctrl0 = ctrl0.Add(p.pen)
ctrl1 = ctrl1.Add(p.pen) ctrl1 = ctrl1.Add(p.pen)
to = to.Add(p.pen) to = to.Add(p.pen)
@@ -163,7 +161,7 @@ func (p *PathBuilder) Cube(ctrl0, ctrl1, to f32.Point) {
// approxCube approximates a cubic Bézier by a series of quadratic // approxCube approximates a cubic Bézier by a series of quadratic
// curves. // curves.
func (p *PathBuilder) approxCubeTo(splits int, maxDist float32, ctrl0, ctrl1, to f32.Point) int { func (p *Path) approxCubeTo(splits int, maxDist float32, ctrl0, ctrl1, to f32.Point) int {
// The idea is from // The idea is from
// https://caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html // https://caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html
// where a quadratic approximates a cubic by eliminating its t³ term // where a quadratic approximates a cubic by eliminating its t³ term
@@ -220,7 +218,7 @@ func (p *PathBuilder) approxCubeTo(splits int, maxDist float32, ctrl0, ctrl1, to
return splits return splits
} }
func (p *PathBuilder) expand(b f32.Rectangle) { func (p *Path) expand(b f32.Rectangle) {
if !p.hasBounds { if !p.hasBounds {
p.hasBounds = true p.hasBounds = true
inf := float32(math.Inf(+1)) inf := float32(math.Inf(+1))
@@ -232,7 +230,7 @@ func (p *PathBuilder) expand(b f32.Rectangle) {
p.bounds = p.bounds.Union(b) p.bounds = p.bounds.Union(b)
} }
func (p *PathBuilder) vertex(cornerx, cornery int16, ctrl, to f32.Point) { func (p *Path) vertex(cornerx, cornery int16, ctrl, to f32.Point) {
p.nverts++ p.nverts++
v := path.Vertex{ v := path.Vertex{
CornerX: cornerx, CornerX: cornerx,
@@ -261,7 +259,7 @@ func (p *PathBuilder) vertex(cornerx, cornery int16, ctrl, to f32.Point) {
p.ops.Write(data) p.ops.Write(data)
} }
func (p *PathBuilder) simpleQuadTo(ctrl, to f32.Point) { func (p *Path) simpleQuadTo(ctrl, to f32.Point) {
if p.pen.Y > p.maxy { if p.pen.Y > p.maxy {
p.maxy = p.pen.Y p.maxy = p.pen.Y
} }
@@ -284,7 +282,7 @@ func (p *PathBuilder) simpleQuadTo(ctrl, to f32.Point) {
// End the path and add the resulting ClipOp to // End the path and add the resulting ClipOp to
// the operation list passed to Init. // the operation list passed to Init.
func (p *PathBuilder) End() { func (p *Path) End() {
p.end() p.end()
ClipOp{ ClipOp{
bounds: p.bounds, bounds: p.bounds,
+2 -2
View File
@@ -237,13 +237,13 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, opts text.LayoutOpt
func textPath(ppem fixed.Int26_6, f *opentype, str text.String) op.MacroOp { func textPath(ppem fixed.Int26_6, f *opentype, str text.String) op.MacroOp {
var lastPos f32.Point var lastPos f32.Point
var builder paint.PathBuilder var builder paint.Path
ops := new(op.Ops) ops := new(op.Ops)
builder.Init(ops)
var x fixed.Int26_6 var x fixed.Int26_6
var advIdx int var advIdx int
var m op.MacroOp var m op.MacroOp
m.Record(ops) m.Record(ops)
builder.Begin(ops)
for _, r := range str.String { for _, r := range str.String {
if !unicode.IsSpace(r) { if !unicode.IsSpace(r) {
segs, ok := f.LoadGlyph(ppem, r) segs, ok := f.LoadGlyph(ppem, r)