Skip to content

Commit

Permalink
runic -i .
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Nov 6, 2024
1 parent d79c746 commit 2667ab4
Show file tree
Hide file tree
Showing 292 changed files with 11,426 additions and 9,770 deletions.
4 changes: 2 additions & 2 deletions CairoMakie/src/CairoMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using Makie: numbers_to_colors
using Makie: Mat3f, Mat4f, Mat3d, Mat4d

# re-export Makie, including deprecated names
for name in names(Makie, all=true)
for name in names(Makie, all = true)
if Base.isexported(Makie, name)
@eval using Makie: $(name)
@eval export $(name)
Expand All @@ -31,7 +31,7 @@ include("primitives.jl")
include("overrides.jl")

function __init__()
activate!()
return activate!()
end

include("precompiles.jl")
Expand Down
37 changes: 22 additions & 15 deletions CairoMakie/src/cairo-extension.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TODO, move those to Cairo?

function set_font_matrix(ctx, matrix)
ccall((:cairo_set_font_matrix, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, Ref(matrix))
return ccall((:cairo_set_font_matrix, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, Ref(matrix))
end

function get_font_matrix(ctx)
Expand All @@ -11,7 +11,7 @@ function get_font_matrix(ctx)
end

function pattern_set_matrix(ctx, matrix)
ccall((:cairo_pattern_set_matrix, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, Ref(matrix))
return ccall((:cairo_pattern_set_matrix, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, Ref(matrix))
end

function pattern_get_matrix(ctx)
Expand All @@ -21,7 +21,7 @@ function pattern_get_matrix(ctx)
end

function cairo_font_face_destroy(font_face)
ccall(
return ccall(
(:cairo_font_face_destroy, Cairo.libcairo),
Cvoid, (Ptr{Cvoid},),
font_face
Expand Down Expand Up @@ -49,30 +49,35 @@ end

function show_glyph(ctx, glyph, x, y)
cg = Ref(CairoGlyph(glyph, x, y))
ccall((:cairo_show_glyphs, Cairo.libcairo),
Nothing, (Ptr{Nothing}, Ptr{CairoGlyph}, Cint),
ctx.ptr, cg, 1)
return ccall(
(:cairo_show_glyphs, Cairo.libcairo),
Nothing, (Ptr{Nothing}, Ptr{CairoGlyph}, Cint),
ctx.ptr, cg, 1
)
end

function glyph_path(ctx, glyph, x, y)
cg = Ref(CairoGlyph(glyph, x, y))
ccall((:cairo_glyph_path, Cairo.libcairo),
Nothing, (Ptr{Nothing}, Ptr{CairoGlyph}, Cint),
ctx.ptr, cg, 1)
return ccall(
(:cairo_glyph_path, Cairo.libcairo),
Nothing, (Ptr{Nothing}, Ptr{CairoGlyph}, Cint),
ctx.ptr, cg, 1
)
end

function surface_set_device_scale(surf, device_x_scale, device_y_scale=device_x_scale)
function surface_set_device_scale(surf, device_x_scale, device_y_scale = device_x_scale)
# this sets a scaling factor on the lowest level that is "hidden" so its even
# enabled when the drawing space is reset for strokes
# that means it can be used to increase or decrease the image resolution
ccall(
return ccall(
(:cairo_surface_set_device_scale, Cairo.libcairo),
Cvoid, (Ptr{Nothing}, Cdouble, Cdouble),
surf.ptr, device_x_scale, device_y_scale)
surf.ptr, device_x_scale, device_y_scale
)
end

function set_miter_limit(ctx, limit)
ccall((:cairo_set_miter_limit, Cairo.libcairo), Cvoid, (Ptr{Nothing}, Cdouble), ctx.ptr, limit)
return ccall((:cairo_set_miter_limit, Cairo.libcairo), Cvoid, (Ptr{Nothing}, Cdouble), ctx.ptr, limit)
end

function get_render_type(surface::Cairo.CairoSurface)
Expand All @@ -88,6 +93,8 @@ end
function restrict_pdf_version!(surface::Cairo.CairoSurface, v::Integer)
@assert surface.ptr != C_NULL
0 v 3 || throw(ArgumentError("version must be 0, 1, 2, or 3 (received $v)"))
ccall((:cairo_pdf_surface_restrict_to_version, Cairo.libcairo), Nothing,
(Ptr{UInt8}, Int32), surface.ptr, v)
return ccall(
(:cairo_pdf_surface_restrict_to_version, Cairo.libcairo), Nothing,
(Ptr{UInt8}, Int32), surface.ptr, v
)
end
31 changes: 16 additions & 15 deletions CairoMakie/src/display.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#########################################
# Backend interface to Makie #
#########################################
Expand Down Expand Up @@ -28,17 +27,17 @@ function openurl(url::String)
tryrun(`python -mwebbrowser $(url)`) && return
# our last hope
tryrun(`python3 -mwebbrowser $(url)`) && return
@warn("Can't find a way to open a browser, open $(url) manually!")
return @warn("Can't find a way to open a browser, open $(url) manually!")
end

function Base.display(screen::Screen, scene::Scene; connect=false)
function Base.display(screen::Screen, scene::Scene; connect = false)
# Nothing to do, since drawing is done in the other functions
# TODO write to file and implement upenurl
return screen
end

function Base.display(screen::Screen{IMAGE}, scene::Scene; connect=false, screen_config...)
config = Makie.merge_screen_config(ScreenConfig, Dict{Symbol,Any}(screen_config))
function Base.display(screen::Screen{IMAGE}, scene::Scene; connect = false, screen_config...)
config = Makie.merge_screen_config(ScreenConfig, Dict{Symbol, Any}(screen_config))
screen = Makie.apply_screen_config!(screen, config, scene)
path = joinpath(mktempdir(), "display.png")
Makie.push_screen!(scene, screen)
Expand Down Expand Up @@ -75,7 +74,7 @@ function Makie.backend_show(screen::Screen{SVG}, io::IO, ::MIME"image/svg+xml",
# across svgs when embedding them on websites.
# the hash and therefore the salt will always be the same for the same file
# so the output is deterministic
salt = repr(CRC32c.crc32c(svg))[end-7:end]
salt = repr(CRC32c.crc32c(svg))[(end - 7):end]

# matches:
# id="someid"
Expand Down Expand Up @@ -111,15 +110,17 @@ end
# Disabling mimes and showable

const DISABLED_MIMES = Set{String}()
const SUPPORTED_MIMES = Set([
map(x->string(x()), Makie.WEB_MIMES)...,
"image/svg+xml",
"application/pdf",
"application/postscript",
"image/png"
])

function Makie.backend_showable(::Type{Screen}, ::MIME{SYM}) where SYM
const SUPPORTED_MIMES = Set(
[
map(x -> string(x()), Makie.WEB_MIMES)...,
"image/svg+xml",
"application/pdf",
"application/postscript",
"image/png",
]
)

function Makie.backend_showable(::Type{Screen}, ::MIME{SYM}) where {SYM}
supported_mimes = Base.setdiff(SUPPORTED_MIMES, DISABLED_MIMES)
return string(SYM) in supported_mimes
end
Expand Down
10 changes: 5 additions & 5 deletions CairoMakie/src/infrastructure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function cairo_draw(screen::Screen, scene::Scene)
draw_background(screen, scene)

allplots = Makie.collect_atomic_plots(scene; is_atomic_plot = is_cairomakie_atomic_plot)
sort!(allplots; by=Makie.zvalue2d)
sort!(allplots; by = Makie.zvalue2d)
# If the backend is not a vector surface (i.e., PNG/ARGB),
# then there is no point in rasterizing twice.
should_rasterize = is_vector_backend(screen.surface)
Expand Down Expand Up @@ -116,7 +116,7 @@ function draw_background(screen::Screen, scene::Scene, root_h)
Cairo.save(cr)
if scene.clear[]
bg = scene.backgroundcolor[]
Cairo.set_source_rgba(cr, red(bg), green(bg), blue(bg), alpha(bg));
Cairo.set_source_rgba(cr, red(bg), green(bg), blue(bg), alpha(bg))
r = viewport(scene)[]
# Makie has (0,0) at bottom left, Cairo at top left. Makie extends up,
# Cairo down. Negative height breaks other backgrounds
Expand All @@ -125,7 +125,7 @@ function draw_background(screen::Screen, scene::Scene, root_h)
fill(cr)
end
Cairo.restore(cr)
foreach(child_scene-> draw_background(screen, child_scene, root_h), scene.children)
return foreach(child_scene -> draw_background(screen, child_scene, root_h), scene.children)
end

function draw_plot(scene::Scene, screen::Screen, primitive::Plot)
Expand All @@ -149,7 +149,7 @@ end
# instead of the whole Scene
# - Recognize when a screen is an image surface, and set scale to render the plot
# at the scale of the device pixel
function draw_plot_as_image(scene::Scene, screen::Screen{RT}, primitive::Plot, scale::Number = 1) where RT
function draw_plot_as_image(scene::Scene, screen::Screen{RT}, primitive::Plot, scale::Number = 1) where {RT}
# you can provide `p.rasterize = scale::Int` or `p.rasterize = true`, both of which are numbers

# Extract scene width in device independent units
Expand Down Expand Up @@ -183,5 +183,5 @@ function draw_plot_as_image(scene::Scene, screen::Screen{RT}, primitive::Plot, s
end

function draw_atomic(::Scene, ::Screen, x)
@warn "$(typeof(x)) is not supported by cairo right now"
return @warn "$(typeof(x)) is not supported by cairo right now"
end
59 changes: 34 additions & 25 deletions CairoMakie/src/overrides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ function draw_plot(scene::Scene, screen::Screen, poly::Poly)
# dispatch on input arguments to poly to use smarter drawing methods than
# meshes if possible.
# however, since recipes exist, we can't explicitly handle all cases here
# so, we should also take a look at converted
# so, we should also take a look at converted
# First, we check whether a `draw_poly` method exists for the input arguments
# before conversion:
return if Base.hasmethod(draw_poly, Tuple{Scene, Screen, typeof(poly), typeof.(to_value.(poly.args))...})
draw_poly(scene, screen, poly, to_value.(poly.args)...)
# If not, we check whether a `draw_poly` method exists for the arguments after conversion
# (`plot.converted`). This allows anything which decomposes to be checked for.
# If not, we check whether a `draw_poly` method exists for the arguments after conversion
# (`plot.converted`). This allows anything which decomposes to be checked for.
elseif Base.hasmethod(draw_poly, Tuple{Scene, Screen, typeof(poly), typeof.(to_value.(poly.converted))...})
draw_poly(scene, screen, poly, to_value.(poly.converted)...)
# In the worst case, we return to drawing the polygon as a mesh + lines.
# In the worst case, we return to drawing the polygon as a mesh + lines.
else
draw_poly_as_mesh(scene, screen, poly)
end
Expand All @@ -34,24 +34,27 @@ function draw_poly_as_mesh(scene, screen, poly)
for i in eachindex(poly.plots)
draw_plot(scene, screen, poly.plots[i])
end
return
end

# As a general fallback, draw all polys as meshes.
# This also applies for e.g. per-vertex color.
function draw_poly(scene::Scene, screen::Screen, poly, points, color, model, strokecolor, strokestyle, strokewidth)
draw_poly_as_mesh(scene, screen, poly)
return draw_poly_as_mesh(scene, screen, poly)
end

function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2})
color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
strokestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())
draw_poly(scene, screen, poly, points, color, poly.model[], strokecolor, strokestyle, poly.strokewidth[])
return draw_poly(scene, screen, poly, points, color, poly.model[], strokecolor, strokestyle, poly.strokewidth[])
end

# when color is a Makie.AbstractPattern, we don't need to go to Mesh
function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2}, color::Union{Colorant, Cairo.CairoPattern},
model, strokecolor, strokestyle, strokewidth)
function draw_poly(
scene::Scene, screen::Screen, poly, points::Vector{<:Point2}, color::Union{Colorant, Cairo.CairoPattern},
model, strokecolor, strokestyle, strokewidth
)
space = to_value(get(poly, :space, :data))
points = clip_poly(poly.clip_planes[], points, space, model)
points = _project_position(scene, space, points, model, true)
Expand All @@ -67,17 +70,19 @@ function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2},
Cairo.set_source_rgba(screen.context, rgbatuple(to_color(strokecolor))...)
Cairo.set_line_width(screen.context, strokewidth)
isnothing(strokestyle) || Cairo.set_dash(screen.context, diff(Float64.(strokestyle)) .* strokewidth)
Cairo.stroke(screen.context)
return Cairo.stroke(screen.context)
end

function draw_poly(scene::Scene, screen::Screen, poly, points_list::Vector{<:Vector{<:Point2}})
color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
strokestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())

broadcast_foreach(points_list, color,
strokecolor, strokestyle, poly.strokewidth[], Ref(poly.model[])) do points, color, strokecolor, strokestyle, strokewidth, model
draw_poly(scene, screen, poly, points, color, model, strokecolor, strokestyle, strokewidth)
return broadcast_foreach(
points_list, color,
strokecolor, strokestyle, poly.strokewidth[], Ref(poly.model[])
) do points, color, strokecolor, strokestyle, strokewidth, model
draw_poly(scene, screen, poly, points, color, model, strokecolor, strokestyle, strokewidth)
end
end

Expand All @@ -101,7 +106,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, shapes::Vector{<:Union{Re
error("Wrong type for linestyle: $(poly.linestyle[]).")
end
strokecolor = to_cairo_color(poly.strokecolor[], poly)
broadcast_foreach(projected_shapes, color, strokecolor, poly.strokewidth[]) do shape, c, sc, sw
return broadcast_foreach(projected_shapes, color, strokecolor, poly.strokewidth[]) do shape, c, sc, sw
create_shape_path!(screen.context, shape)
set_source(screen.context, c)
Cairo.fill_preserve(screen.context)
Expand All @@ -124,17 +129,18 @@ function project_shape(scene, space, shape::BezierPath, model)
push!(commands, project_command(cmd, scene, space, model))
end
end
BezierPath(commands)
return BezierPath(commands)
end

function create_shape_path!(ctx, r::Rect2)
Cairo.rectangle(ctx, origin(r)..., widths(r)...)
return Cairo.rectangle(ctx, origin(r)..., widths(r)...)
end

function create_shape_path!(ctx, b::BezierPath)
for cmd in b.commands
path_command(ctx, cmd)
end
return
end

function polypath(ctx, polygon)
Expand All @@ -157,6 +163,7 @@ function polypath(ctx, polygon)
end
Cairo.close_path(ctx)
end
return
end

draw_poly(scene::Scene, screen::Screen, poly, polygon::Polygon) = draw_poly(scene, screen, poly, [polygon])
Expand All @@ -174,7 +181,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<
strokecolor = to_cairo_color(poly.strokecolor[], poly)
strokestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())

broadcast_foreach(projected_polys, color, strokecolor, strokestyle, poly.strokewidth[]) do po, c, sc, ss, sw
return broadcast_foreach(projected_polys, color, strokecolor, strokestyle, poly.strokewidth[]) do po, c, sc, ss, sw
polypath(screen.context, po)
set_source(screen.context, c)
Cairo.fill_preserve(screen.context)
Expand All @@ -185,17 +192,17 @@ function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<

end

function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<: MultiPolygon})
function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<:MultiPolygon})
model = poly.model[]
space = to_value(get(poly, :space, :data))
projected_polys = map(polygons) do polygon
projected_polys = map(polygons) do polygon
project_multipolygon(poly, space, polygon, poly.clip_planes[], model)
end

color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
strokestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())
broadcast_foreach(projected_polys, color, strokecolor, strokestyle, poly.strokewidth[]) do mpo, c, sc, ss, sw
return broadcast_foreach(projected_polys, color, strokecolor, strokestyle, poly.strokewidth[]) do mpo, c, sc, ss, sw
for po in mpo.polygons
polypath(screen.context, po)
set_source(screen.context, c)
Expand Down Expand Up @@ -223,7 +230,7 @@ function band_segment_ranges(lowerpoints, upperpoints)
for i in eachindex(lowerpoints, upperpoints)
if isnan(lowerpoints[i]) || isnan(upperpoints[i])
if start !== nothing && i - start > 1 # more than one point
push!(ranges, start:i-1)
push!(ranges, start:(i - 1))
end
start = nothing
elseif start === nothing
Expand All @@ -235,8 +242,10 @@ function band_segment_ranges(lowerpoints, upperpoints)
return ranges
end

function draw_plot(scene::Scene, screen::Screen,
band::Band{<:Tuple{<:AbstractVector{<:Point2},<:AbstractVector{<:Point2}}})
function draw_plot(
scene::Scene, screen::Screen,
band::Band{<:Tuple{<:AbstractVector{<:Point2}, <:AbstractVector{<:Point2}}}
)

if !(band.color[] isa AbstractArray)
basecolor = to_cairo_color(band.color[], band)
Expand Down Expand Up @@ -266,12 +275,12 @@ function draw_plot(scene::Scene, screen::Screen,
end
end

nothing
return nothing
end

# Override `is_cairomakie_atomic_plot` to allow this dispatch of `band` to remain a unit,
# instead of auto-decomposing in lines and mesh.
function is_cairomakie_atomic_plot(plot::Band{<:Tuple{<:AbstractVector{<:Point2},<:AbstractVector{<:Point2}}})
function is_cairomakie_atomic_plot(plot::Band{<:Tuple{<:AbstractVector{<:Point2}, <:AbstractVector{<:Point2}}})
return true
end

Expand All @@ -295,7 +304,7 @@ function draw_plot(scene::Scene, screen::Screen, tric::Tricontourf)
function draw_tripolys(polys, colornumbers, colors)
for (i, (pol, colnum, col)) in enumerate(zip(polys, colornumbers, colors))
polypath(screen.context, pol)
if i == length(colornumbers) || colnum != colornumbers[i+1]
if i == length(colornumbers) || colnum != colornumbers[i + 1]
set_source(screen.context, col)
Cairo.fill(screen.context)
end
Expand Down
Loading

0 comments on commit 2667ab4

Please sign in to comment.