Skip to content

Commit

Permalink
initial format
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 24, 2022
1 parent a2dc819 commit 1e09a72
Show file tree
Hide file tree
Showing 223 changed files with 10,783 additions and 8,520 deletions.
6 changes: 2 additions & 4 deletions CairoMakie/src/CairoMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using Makie: spaces, is_data_space, is_pixel_space, is_relative_space, is_clip_s
using Makie: numbers_to_colors

# 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 @@ -30,9 +30,7 @@ include("utils.jl")
include("primitives.jl")
include("overrides.jl")

function __init__()
activate!()
end
__init__() = activate!()

include("precompiles.jl")

Expand Down
59 changes: 38 additions & 21 deletions CairoMakie/src/cairo-extension.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# 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,19 +17,16 @@ function get_font_matrix(ctx)
end

function cairo_font_face_destroy(font_face)
ccall(
(:cairo_font_face_destroy, Cairo.libcairo),
Cvoid, (Ptr{Cvoid},),
font_face
)
return ccall((:cairo_font_face_destroy, Cairo.libcairo), Cvoid, (Ptr{Cvoid},), font_face)
end

function set_ft_font(ctx, font)

font_face = ccall(
(:cairo_ft_font_face_create_for_ft_face, Cairo.libcairo),
Ptr{Cvoid}, (Makie.FreeTypeAbstraction.FT_Face, Cint),
font, 0
Ptr{Cvoid},
(Makie.FreeTypeAbstraction.FT_Face, Cint),
font,
0,
)

ccall((:cairo_set_font_face, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, font_face)
Expand All @@ -39,30 +42,44 @@ 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)
Cvoid,
(Ptr{Nothing}, Cdouble, Cdouble),
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 @@ -71,5 +88,5 @@ function get_render_type(surface::Cairo.CairoSurface)
typ == Cairo.CAIRO_SURFACE_TYPE_PS && return EPS
typ == Cairo.CAIRO_SURFACE_TYPE_SVG && return SVG
typ == Cairo.CAIRO_SURFACE_TYPE_IMAGE && return IMAGE
error("Unsupported surface type: $(typ)")
return error("Unsupported surface type: $(typ)")
end
20 changes: 7 additions & 13 deletions CairoMakie/src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
Try to run a command. Return `true` if `cmd` runs and is successful (exits with a code of `0`).
Return `false` otherwise.
"""
function tryrun(cmd::Cmd)
tryrun(cmd::Cmd) =
try
return success(cmd)
catch e
return false
end
end

function openurl(url::String)
if Sys.isapple()
Expand All @@ -38,13 +37,13 @@ function display_path(type::String)
return abspath(joinpath(@__DIR__, "display." * type))
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)
function Base.display(screen::Screen{IMAGE}, scene::Scene; connect = false)
path = display_path("png")
Makie.push_screen!(scene, screen)
cairo_draw(screen, scene)
Expand All @@ -63,7 +62,7 @@ function Makie.backend_show(screen::Screen{SVG}, io::IO, ::MIME"image/svg+xml",
screen2 = Screen(screen, io2, SVG)
cairo_draw(screen2, scene)
Cairo.flush(screen2.surface)
Cairo.finish(screen2.surface)
return Cairo.finish(screen2.surface)
end

# for some reason, in the svg, surfaceXXX ids keep counting up,
Expand Down Expand Up @@ -116,14 +115,9 @@ end
# Disabling mimes and showable

const DISABLED_MIMES = Set{String}()
const SUPPORTED_MIMES = Set([
"image/svg+xml",
"application/pdf",
"application/postscript",
"image/png"
])

function Makie.backend_showable(::Type{Screen}, ::MIME{SYM}) where SYM
const SUPPORTED_MIMES = Set(["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: 4 additions & 6 deletions CairoMakie/src/infrastructure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function get_all_plots(scene, plots = AbstractPlot[])
for c in scene.children
get_all_plots(c, plots)
end
plots
return plots
end

function prepare_for_scene(screen::Screen, scene::Scene)
Expand Down Expand Up @@ -90,13 +90,13 @@ function draw_background(screen::Screen, scene::Scene)
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 = pixelarea(scene)[]
Cairo.rectangle(cr, origin(r)..., widths(r)...) # background
fill(cr)
end
Cairo.restore(cr)
foreach(child_scene-> draw_background(screen, child_scene), scene.children)
return foreach(child_scene -> draw_background(screen, child_scene), scene.children)
end

function draw_plot(scene::Scene, screen::Screen, primitive::Combined)
Expand Down Expand Up @@ -150,6 +150,4 @@ function draw_plot_as_image(scene::Scene, screen::Screen, primitive::Combined, s
return
end

function draw_atomic(::Scene, ::Screen, x)
@warn "$(typeof(x)) is not supported by cairo right now"
end
draw_atomic(::Scene, ::Screen, x) = @warn "$(typeof(x)) is not supported by cairo right now"
82 changes: 53 additions & 29 deletions CairoMakie/src/overrides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,64 @@
# 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::Screen, 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)...)
return draw_poly(scene, screen, poly, to_value.(poly.input_args)...)
end

"""
Fallback method for args without special treatment.
"""
function draw_poly(scene::Scene, screen::Screen, poly, args...)
draw_poly_as_mesh(scene, screen, poly)
end
draw_poly(scene::Scene, screen::Screen, poly, args...) = draw_poly_as_mesh(scene, screen, poly)

function draw_poly_as_mesh(scene, screen, poly)
draw_plot(scene, screen, poly.plots[1])
draw_plot(scene, screen, poly.plots[2])
return 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::Screen, poly, points::Vector{<:Point2}, color::AbstractArray, model, strokecolor, strokewidth)
draw_poly_as_mesh(scene, screen, poly)
function draw_poly(
scene::Scene,
screen::Screen,
poly,
points::Vector{<:Point2},
color::AbstractArray,
model,
strokecolor,
strokewidth,
)
return draw_poly_as_mesh(scene, screen, poly)
end

function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2})
draw_poly(scene, screen, poly, points, poly.color[], poly.model[], poly.strokecolor[], poly.strokewidth[])
return 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::Screen, poly, points::Vector{<:Point2}, color::Union{Symbol, Colorant, Makie.AbstractPattern},
model, strokecolor, strokewidth)
function draw_poly(
scene::Scene,
screen::Screen,
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]...)
Expand All @@ -47,7 +69,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2},
Cairo.close_path(screen.context)
if color isa Makie.AbstractPattern
cairopattern = Cairo.CairoPattern(color)
Cairo.pattern_set_extend(cairopattern, Cairo.EXTEND_REPEAT);
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))...)
Expand All @@ -56,14 +78,17 @@ function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2},
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)
return Cairo.stroke(screen.context)
end

function draw_poly(scene::Scene, screen::Screen, 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)
broadcast_foreach(
points_list,
poly.color[],
poly.strokecolor[],
poly.strokewidth[],
) do points, color, strokecolor, strokewidth
return draw_poly(scene, screen, poly, points, color, poly.model[], strokecolor, strokewidth)
end
end

Expand All @@ -82,7 +107,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, rects::Vector{<:Rect2})
color = to_color(color)
elseif color isa Makie.AbstractPattern
cairopattern = Cairo.CairoPattern(color)
Cairo.pattern_set_extend(cairopattern, Cairo.EXTEND_REPEAT);
Cairo.pattern_set_extend(cairopattern, Cairo.EXTEND_REPEAT)
end
strokecolor = poly.strokecolor[]
if strokecolor isa AbstractArray{<:Number}
Expand All @@ -102,7 +127,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, rects::Vector{<:Rect2})
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)
return Cairo.stroke(screen.context)
end
end

Expand Down Expand Up @@ -151,21 +176,21 @@ function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<
Cairo.fill_preserve(screen.context)
Cairo.set_source_rgba(screen.context, rgbatuple(sc)...)
Cairo.set_line_width(screen.context, sw)
Cairo.stroke(screen.context)
return 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::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)
upperpoints = band[1][]
lowerpoints = band[2][]
Expand All @@ -186,7 +211,7 @@ function draw_plot(scene::Scene, screen::Screen,
end
end

nothing
return nothing
end

#################################################################################
Expand All @@ -197,7 +222,6 @@ end
#################################################################################

function draw_plot(scene::Scene, screen::Screen, tric::Tricontourf)

pol = only(tric.plots)::Poly
colornumbers = pol.color[]
colors = numbers_to_colors(colornumbers, pol)
Expand All @@ -211,7 +235,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]
Cairo.set_source_rgba(screen.context, rgbatuple(col)...)
Cairo.fill(screen.context)
end
Expand Down
Loading

0 comments on commit 1e09a72

Please sign in to comment.