Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/pixie/paths.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,9 @@ proc strokeShapes(
if d <= 0.0:
raise newException(PixieError, "Invalid line dash value")

var
dashIndex = 0
dashRemaining = if dashes.len > 0: dashes[0] else: 0.float32
for i in 1 ..< shape.len:
let
pos = shape[i]
Expand All @@ -2063,16 +2066,16 @@ proc strokeShapes(
var distance = dist(prevPos, pos)
let dir = dir(pos, prevPos)
var currPos = prevPos
block dashLoop:
while true:
for i, d in dashes:
if i mod 2 == 0:
let d = min(distance, d)
shapeStroke.add(makeRect(currPos, currPos + dir * d))
currPos += dir * d
distance -= d
if distance <= 0:
break dashLoop
while distance > 0:
let d = min(distance, dashRemaining)
if dashIndex mod 2 == 0:
shapeStroke.add(makeRect(currPos, currPos + dir * d))
currPos += dir * d
distance -= d
dashRemaining -= d
if dashRemaining <= 0:
dashIndex = (dashIndex + 1) mod dashes.len
dashRemaining = dashes[dashIndex]
else:
shapeStroke.add(makeRect(prevPos, pos))

Expand Down
7 changes: 7 additions & 0 deletions tests/test_paths.nim
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ block:

image.xray("tests/paths/dashes.png")

block:
# The dash pattern continues across segments instead of restarting.
let path = parsePath("M 0 0 L 3 0 L 6 0 L 9 0 L 12 0")

doAssert path.strokeOverlaps(vec2(2, 0), dashes = @[4.float32, 4])
doAssert not path.strokeOverlaps(vec2(5, 0), dashes = @[4.float32, 4])

block:
proc miterTest(angle, limit: float32) =
let
Expand Down