Skip to content

Commit

Permalink
intial format
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 24, 2022
1 parent bb1c1de commit 440bcff
Show file tree
Hide file tree
Showing 212 changed files with 4,985 additions and 5,952 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
30 changes: 12 additions & 18 deletions CairoMakie/src/cairo-extension.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# 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))
end
set_font_matrix(ctx, matrix) = ccall((:cairo_set_font_matrix, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, Ref(matrix))

function get_font_matrix(ctx)
matrix = Cairo.CairoMatrix()
ccall((:cairo_get_font_matrix, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, Ref(matrix))
return matrix
end

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

function set_ft_font(ctx, font)

Expand All @@ -40,18 +36,18 @@ 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)
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)
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
Expand All @@ -61,9 +57,7 @@ function surface_set_device_scale(surf, device_x_scale, device_y_scale=device_x_
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)
end
set_miter_limit(ctx, limit) = ccall((:cairo_set_miter_limit, Cairo.libcairo), Cvoid, (Ptr{Nothing}, Cdouble), ctx.ptr, limit)

function get_render_type(surface::Cairo.CairoSurface)
typ = ccall((:cairo_surface_get_type, Cairo.libcairo), Cint, (Ptr{Nothing},), surface.ptr)
Expand Down
10 changes: 4 additions & 6 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 @@ -133,7 +132,7 @@ end
Converts anything like `"png", :png, "image/png", MIME"image/png"()` to `"image/png"`.
"""
function to_mime_string(mime::Union{String, Symbol, MIME})
to_mime_string(mime::Union{String, Symbol, MIME}) =
if mime isa MIME
mime_str = string(mime)
if !(mime_str in SUPPORTED_MIMES)
Expand All @@ -147,7 +146,6 @@ function to_mime_string(mime::Union{String, Symbol, MIME})
end
return mime_str
end
end

"""
disable_mime!(mime::Union{String, Symbol, MIME}...)
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"
27 changes: 10 additions & 17 deletions CairoMakie/src/overrides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ 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])
Expand All @@ -27,17 +25,13 @@ 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)
end
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})
draw_poly(scene, screen, poly, points, poly.color[], poly.model[], poly.strokecolor[], poly.strokewidth[])
end
draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2}) = draw_poly(scene, screen, poly, points, poly.color[], poly.model[], poly.strokecolor[], poly.strokewidth[])

# 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)
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 +41,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 @@ -59,13 +53,12 @@ function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2},
Cairo.stroke(screen.context)
end

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

draw_poly(scene::Scene, screen::Screen, poly, rect::Rect2) = draw_poly(scene, screen, poly, [rect])

Expand All @@ -82,7 +75,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 @@ -164,7 +157,7 @@ end
################################################################################

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

if !(band.color[] isa AbstractArray)
upperpoints = band[1][]
Expand Down Expand Up @@ -211,7 +204,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 440bcff

Please sign in to comment.