vector: use value type for subpath

Updates #3060
This commit is contained in:
Hajime Hoshi 2024-08-10 14:46:49 +09:00
parent 68380e506e
commit 309c886c2e

View File

@ -65,11 +65,11 @@ type subpath struct {
closed bool closed bool
} }
func (s *subpath) pointCount() int { func (s subpath) pointCount() int {
return len(s.points) return len(s.points)
} }
func (s *subpath) lastPoint() point { func (s subpath) lastPoint() point {
return s.points[len(s.points)-1] return s.points[len(s.points)-1]
} }
@ -100,7 +100,7 @@ func (s *subpath) close() {
type Path struct { type Path struct {
ops []op ops []op
subpaths []*subpath subpaths []subpath
} }
func (p *Path) reset() { func (p *Path) reset() {
@ -108,10 +108,7 @@ func (p *Path) reset() {
p.subpaths = p.subpaths[:0] p.subpaths = p.subpaths[:0]
} }
func (p *Path) ensureSubpaths() []*subpath { func (p *Path) ensureSubpaths() []subpath {
// TODO: Probably it is better to avoid returning a slice since allocation is heavy.
// What about walkSubpaths(func(*subpath))?
if len(p.subpaths) > 0 || len(p.ops) == 0 { if len(p.subpaths) > 0 || len(p.ops) == 0 {
return p.subpaths return p.subpaths
} }
@ -120,7 +117,7 @@ func (p *Path) ensureSubpaths() []*subpath {
for _, op := range p.ops { for _, op := range p.ops {
switch op.typ { switch op.typ {
case opTypeMoveTo: case opTypeMoveTo:
p.subpaths = append(p.subpaths, &subpath{ p.subpaths = append(p.subpaths, subpath{
points: []point{op.p1}, points: []point{op.p1},
}) })
cur = op.p1 cur = op.p1
@ -197,7 +194,7 @@ func (p *Path) Close() {
func (p *Path) lineTo(pt point) { func (p *Path) lineTo(pt point) {
if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed { if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed {
p.subpaths = append(p.subpaths, &subpath{ p.subpaths = append(p.subpaths, subpath{
points: []point{pt}, points: []point{pt},
}) })
return return
@ -462,8 +459,7 @@ func (p *Path) close() {
if len(p.subpaths) == 0 { if len(p.subpaths) == 0 {
return return
} }
subpath := p.subpaths[len(p.subpaths)-1] p.subpaths[len(p.subpaths)-1].close()
subpath.close()
} }
// AppendVerticesAndIndicesForFilling appends vertices and indices to fill this path and returns them. // AppendVerticesAndIndicesForFilling appends vertices and indices to fill this path and returns them.