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 06123b5 commit 389be7a
Show file tree
Hide file tree
Showing 222 changed files with 9,626 additions and 7,496 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
38 changes: 21 additions & 17 deletions CairoMakie/src/cairo-extension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,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
)
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,26 +36,33 @@ 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)
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)
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(
(: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)
Expand Down
18 changes: 6 additions & 12 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 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
8 changes: 3 additions & 5 deletions CairoMakie/src/infrastructure.jl
Original file line number Diff line number Diff line change
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)
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"
57 changes: 36 additions & 21 deletions CairoMakie/src/overrides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# 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.
Expand All @@ -16,18 +15,24 @@ 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])
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)
function draw_poly(
scene::Scene,
screen::Screen,
poly,
points::Vector{<:Point2},
color::AbstractArray,
model,
strokecolor,
strokewidth,
)
draw_poly_as_mesh(scene, screen, poly)
end

Expand All @@ -36,8 +41,16 @@ function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2})
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 +60,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 @@ -60,10 +73,13 @@ function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2},
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
draw_poly(scene, screen, poly, points, color, poly.model[], strokecolor, strokewidth)
end
end

Expand All @@ -82,7 +98,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 Down Expand Up @@ -153,19 +169,19 @@ function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<
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::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 Down Expand Up @@ -197,7 +213,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 +226,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 389be7a

Please sign in to comment.