-
Notifications
You must be signed in to change notification settings - Fork 10
/
compound.jl
68 lines (61 loc) · 2.12 KB
/
compound.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
struct CompoundStyle{T<:FloatCoordinate} <: ContinuousStyle{false}
styles::Vector{Style}
grid::Vector{T}
end
Combines styles together, typically for use with a [`CompoundSegment`](@ref).
- `styles`: Array of styles making up the object. This is deep-copied by the outer
constructor.
- `grid`: An array of `t` values needed for rendering the parameteric path.
"""
struct CompoundStyle{T<:FloatCoordinate} <: ContinuousStyle{false}
styles::Vector{Style}
grid::Vector{T}
tag::Symbol
end
function (s::CompoundStyle)(t)
l0 = s.grid[1]
t < l0 && return s.styles[1], t - l0
for i in 2:(length(s.grid) - 1)
l1 = s.grid[i]
(l0 <= t) && (t < l1) && return s.styles[i-1], t - l0
l0 = s.grid[i]
end
return s.styles[length(s.grid) - 1], t - l0
end
copy(s::CompoundStyle, tag=gensym()) = (typeof(s))(deepcopy(s.styles), copy(s.grid), tag)
CompoundStyle(seg::AbstractVector{Segment{T}}, sty::AbstractVector, tag=gensym()) where {T} =
CompoundStyle(deepcopy(Vector{Style}(sty)), makegrid(seg, sty), tag)
"""
makegrid(segments::AbstractVector{T}, styles) where {T<:Segment}
Returns a collection with the values of `t` to use for
rendering a `CompoundSegment` with a `CompoundStyle`.
"""
function makegrid(segments::AbstractVector{T}, styles) where {T<:Segment}
isempty(segments) && error("Cannot use makegrid with zero segments.")
length(segments) != length(styles) &&
error("Must have same number of segments and styles.")
grid = Vector{eltype(T)}(undef, length(segments)+1)
grid[1] = zero(eltype(T))
v = view(grid, 2:length(grid))
v .= pathlength.(segments)
cumsum!(grid,grid)
end
for x in (:extent, :width)
@eval function ($x)(s::CompoundStyle, t)
sty, teff = s(t)
($x)(sty, teff)
end
end
summary(::CompoundStyle) = "Compound style"
function translate(s::CompoundStyle, x, tag=gensym())
s′ = copy(s, tag)
s′.grid .-= x
return s′
end
function pin(s::CompoundStyle; start=nothing, stop=nothing, tag=gensym())
if start !== nothing
return translate(s, start, tag)
end
return copy(s, tag)
end