Skip to content

Commit 59be125

Browse files
committed
Add FastStroke option to skip Settle on Path.Offset and Path.Stroke, see #334
1 parent 740de25 commit 59be125

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

path_stroke.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import (
44
"math"
55
)
66

7+
// FastStroke skips "settling" the resulting paths for Path.Offset and Path.Stroke, which ensures a well-formed path without overlapping
8+
// nor holes. This fixes overlapping strokes or the inner-bend for corners. Inner-bends are currently already fixed for two line
9+
// segments, but will otherwise leave a hole. Overlapping strokes may not be a problem when using the NonZero winding order for drawing.
10+
// Strokes that are significantly larger than the path itself may be problematic with this enabled, but it may provide a stark
11+
// performance gain for most (trivial) cases.
12+
var FastStroke = false
13+
714
// NOTE: implementation inspired from github.com/golang/freetype/raster/stroke.go
815

916
// Capper implements Cap, with rhs the path to append to, halfWidth the half width of the stroke, pivot the pivot point around which to construct a cap, and n0 the normal at the start of the path. The length of n0 is equal to the halfWidth.
@@ -643,7 +650,7 @@ func (p *Path) Offset(w float64, tolerance float64) *Path {
643650
} else {
644651
r = lhs
645652
}
646-
if pi.Closed() {
653+
if pi.Closed() && !FastStroke {
647654
if pi.CCW() {
648655
r = r.Settle(Positive)
649656
} else {
@@ -671,17 +678,31 @@ func (p *Path) Stroke(w float64, cr Capper, jr Joiner, tolerance float64) *Path
671678
continue
672679
} else if lhs == nil {
673680
// open path
674-
q = q.Append(rhs.Settle(Positive))
681+
if FastStroke {
682+
q = q.Append(rhs)
683+
} else {
684+
q = q.Append(rhs.Settle(Positive))
685+
}
675686
} else {
676687
// closed path
677688
// inner path should go opposite direction to cancel the outer path
678689
if pi.CCW() {
679-
q = q.Append(rhs.Settle(Positive))
680-
q = q.Append(lhs.Settle(Positive).Reverse())
690+
if FastStroke {
691+
q = q.Append(rhs)
692+
q = q.Append(lhs.Reverse())
693+
} else {
694+
q = q.Append(rhs.Settle(Positive))
695+
q = q.Append(lhs.Settle(Positive).Reverse())
696+
}
681697
} else {
682698
// outer first, then inner
683-
q = q.Append(lhs.Settle(Negative))
684-
q = q.Append(rhs.Settle(Negative).Reverse())
699+
if FastStroke {
700+
q = q.Append(lhs.Reverse())
701+
q = q.Append(rhs)
702+
} else {
703+
q = q.Append(lhs.Settle(Negative))
704+
q = q.Append(rhs.Settle(Negative).Reverse())
705+
}
685706
}
686707
}
687708
}

0 commit comments

Comments
 (0)