-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
overrides.jl
188 lines (163 loc) · 7.21 KB
/
overrides.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
################################################################################
# Poly - the not so primitive, primitive #
################################################################################
"""
Special method for polys so we don't fall back to atomic meshes, which are much more
complex and slower to draw than standard paths with single color.
"""
function draw_plot(scene::Scene, screen::CairoScreen, poly::Poly)
# dispatch on input arguments to poly to use smarter drawing methods than
# meshes if possible
draw_poly(scene, screen, poly, to_value.(poly.input_args)...)
end
"""
Fallback method for args without special treatment.
"""
function draw_poly(scene::Scene, screen::CairoScreen, poly, args...)
draw_poly_as_mesh(scene, screen, poly)
end
function draw_poly_as_mesh(scene, screen, poly)
draw_plot(scene, screen, poly.plots[1])
draw_plot(scene, screen, poly.plots[2])
end
# in the rare case of per-vertex colors redirect to mesh drawing
function draw_poly(scene::Scene, screen::CairoScreen, poly, points::Vector{<:Point2}, color::AbstractArray, model, strokecolor, strokewidth)
draw_poly_as_mesh(scene, screen, poly)
end
function draw_poly(scene::Scene, screen::CairoScreen, poly, points::Vector{<:Point2})
draw_poly(scene, screen, poly, points, poly.color[], poly.model[], poly.strokecolor[], poly.strokewidth[])
end
# when color is a Makie.AbstractPattern, we don't need to go to Mesh
function draw_poly(scene::Scene, screen::CairoScreen, poly, points::Vector{<:Point2}, color::Union{Symbol, Colorant, Makie.AbstractPattern},
model, strokecolor, strokewidth)
space = to_value(get(poly, :space, :data))
points = project_position.(Ref(scene), space, points, Ref(model))
Cairo.move_to(screen.context, points[1]...)
for p in points[2:end]
Cairo.line_to(screen.context, p...)
end
Cairo.close_path(screen.context)
if color isa Makie.AbstractPattern
cairopattern = Cairo.CairoPattern(color)
Cairo.pattern_set_extend(cairopattern, Cairo.EXTEND_REPEAT);
Cairo.set_source(screen.context, cairopattern)
else
Cairo.set_source_rgba(screen.context, rgbatuple(to_color(color))...)
end
Cairo.fill_preserve(screen.context)
Cairo.set_source_rgba(screen.context, rgbatuple(to_color(strokecolor))...)
Cairo.set_line_width(screen.context, strokewidth)
Cairo.stroke(screen.context)
end
function draw_poly(scene::Scene, screen::CairoScreen, poly, points_list::Vector{<:Vector{<:Point2}})
broadcast_foreach(points_list, poly.color[],
poly.strokecolor[], poly.strokewidth[]) do points, color, strokecolor, strokewidth
draw_poly(scene, screen, poly, points, color, poly.model[], strokecolor, strokewidth)
end
end
draw_poly(scene::Scene, screen::CairoScreen, poly, rect::Rect2) = draw_poly(scene, screen, poly, [rect])
function draw_poly(scene::Scene, screen::CairoScreen, poly, rects::Vector{<:Rect2})
model = poly.model[]
space = to_value(get(poly, :space, :data))
projected_rects = project_rect.(Ref(scene), space, rects, Ref(model))
color = poly.color[]
if color isa AbstractArray{<:Number}
color = numbers_to_colors(color, poly)
elseif color isa String
# string is erroneously broadcasted as chars otherwise
color = to_color(color)
elseif color isa Makie.AbstractPattern
cairopattern = Cairo.CairoPattern(color)
Cairo.pattern_set_extend(cairopattern, Cairo.EXTEND_REPEAT);
end
strokecolor = poly.strokecolor[]
if strokecolor isa AbstractArray{<:Number}
strokecolor = numbers_to_colors(strokecolor, poly)
elseif strokecolor isa String
# string is erroneously broadcasted as chars otherwise
strokecolor = to_color(strokecolor)
end
broadcast_foreach(projected_rects, color, strokecolor, poly.strokewidth[]) do r, c, sc, sw
Cairo.rectangle(screen.context, origin(r)..., widths(r)...)
if c isa Makie.AbstractPattern
Cairo.set_source(screen.context, cairopattern)
else
Cairo.set_source_rgba(screen.context, rgbatuple(to_color(c))...)
end
Cairo.fill_preserve(screen.context)
Cairo.set_source_rgba(screen.context, rgbatuple(to_color(sc))...)
Cairo.set_line_width(screen.context, sw)
Cairo.stroke(screen.context)
end
end
function polypath(ctx, polygon)
ext = decompose(Point2f, polygon.exterior)
Cairo.move_to(ctx, ext[1]...)
for point in ext[2:end]
Cairo.line_to(ctx, point...)
end
Cairo.close_path(ctx)
interiors = decompose.(Point2f, polygon.interiors)
for interior in interiors
Cairo.move_to(ctx, interior[1]...)
for point in interior[2:end]
Cairo.line_to(ctx, point...)
end
Cairo.close_path(ctx)
end
end
function draw_poly(scene::Scene, screen::CairoScreen, poly, polygons::AbstractArray{<:Polygon})
model = poly.model[]
space = to_value(get(poly, :space, :data))
projected_polys = project_polygon.(Ref(scene), space, polygons, Ref(model))
color = poly.color[]
if color isa AbstractArray{<:Number}
color = numbers_to_colors(color, poly)
elseif color isa String
# string is erroneously broadcasted as chars otherwise
color = to_color(color)
end
strokecolor = poly.strokecolor[]
if strokecolor isa AbstractArray{<:Number}
strokecolor = numbers_to_colors(strokecolor, poly)
elseif strokecolor isa String
# string is erroneously broadcasted as chars otherwise
strokecolor = to_color(strokecolor)
end
broadcast_foreach(projected_polys, color, strokecolor, poly.strokewidth[]) do po, c, sc, sw
polypath(screen.context, po)
Cairo.set_source_rgba(screen.context, rgbatuple(c)...)
Cairo.fill_preserve(screen.context)
Cairo.set_source_rgba(screen.context, rgbatuple(sc)...)
Cairo.set_line_width(screen.context, sw)
Cairo.stroke(screen.context)
end
end
################################################################################
# Band #
# Override because band is usually a polygon, but because it supports #
# gradients as well via `mesh` we have to intercept the poly use #
################################################################################
function draw_plot(scene::Scene, screen::CairoScreen,
band::Band{<:Tuple{<:AbstractVector{<:Point2},<:AbstractVector{<:Point2}}})
if !(band.color[] isa AbstractArray)
upperpoints = band[1][]
lowerpoints = band[2][]
points = vcat(lowerpoints, reverse(upperpoints))
model = band.model[]
space = to_value(get(band, :space, :data))
points = project_position.(Ref(scene), space, points, Ref(model))
Cairo.move_to(screen.context, points[1]...)
for p in points[2:end]
Cairo.line_to(screen.context, p...)
end
Cairo.close_path(screen.context)
Cairo.set_source_rgba(screen.context, rgbatuple(to_color(band.color[]))...)
Cairo.fill(screen.context)
else
for p in band.plots
draw_plot(scene, screen, p)
end
end
nothing
end