Skip to content

Commit

Permalink
Redesign the CairoSurface type (#249)
Browse files Browse the repository at this point in the history
* redesign the CairoSurface type
* add a test
  • Loading branch information
tknopp authored and lobingera committed Aug 22, 2018
1 parent 2017ef4 commit 268a2e9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
49 changes: 37 additions & 12 deletions src/Cairo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,18 @@ end

get_readstream_callback(::Type{T}) where T = @cfunction read_from_stream_callback Int32 (Ref{T}, Ptr{UInt8}, UInt32)

mutable struct CairoSurface{T<:Union{UInt32,RGB24,ARGB32}} <: GraphicsDevice
abstract type CairoSurface{T<:Union{UInt32,RGB24,ARGB32}} <: GraphicsDevice end

mutable struct CairoSurfaceBase{T<:Union{UInt32,RGB24,ARGB32}} <: CairoSurface{T}
ptr::Ptr{Nothing}
width::Float64
height::Float64
data::Matrix{T}

function CairoSurface{T}(ptr::Ptr{Nothing}, w, h) where {T}
self = new{T}(ptr, w, h)
@compat finalizer(destroy, self)
self
end
function CairoSurface{T}(ptr::Ptr{Nothing}, w, h, data::Matrix{T}) where {T}
self = new{T}(ptr, w, h, data)
@compat finalizer(destroy, self)
self
end
function CairoSurface{T}(ptr::Ptr{Nothing}) where {T}
ccall(
(:cairo_surface_reference,_jl_libcairo),
Expand All @@ -183,8 +179,37 @@ mutable struct CairoSurface{T<:Union{UInt32,RGB24,ARGB32}} <: GraphicsDevice
end
end


mutable struct CairoSurfaceImage{T<:Union{UInt32,RGB24,ARGB32}} <: CairoSurface{T}
ptr::Ptr{Nothing}
width::Float64
height::Float64
data::Matrix{T}

function CairoSurface{T}(ptr::Ptr{Nothing}, w, h, data::Matrix{T}) where {T}
self = new{T}(ptr, w, h, data)
@compat finalizer(destroy, self)
self
end
end

mutable struct CairoSurfaceIOStream{T<:Union{UInt32,RGB24,ARGB32}} <: CairoSurface{T}
ptr::Ptr{Nothing}
width::Float64
height::Float64
stream::IO

function CairoSurface{T}(ptr::Ptr{Nothing}, w, h, stream::IO) where {T}
self = new{T}(ptr, w, h, stream)
@compat finalizer(destroy, self)
self
end
end


CairoSurface(ptr, w, h) = CairoSurface{UInt32}(ptr, w, h)
CairoSurface(ptr, w, h, data) = CairoSurface{eltype(data)}(ptr, w, h, data)
CairoSurface(ptr, w, h, stream::IO) = CairoSurface{UInt32}(ptr, w, h, stream)
CairoSurface(ptr) = CairoSurface{UInt32}(ptr)

width(surface::CairoSurface) = surface.width
Expand Down Expand Up @@ -268,7 +293,7 @@ function CairoPDFSurface(stream::T, w::Real, h::Real) where {T<:IO}
callback = get_stream_callback(T)
ptr = ccall((:cairo_pdf_surface_create_for_stream,_jl_libcairo), Ptr{Nothing},
(Ptr{Nothing}, Any, Float64, Float64), callback, stream, w, h)
CairoSurface(ptr, w, h)
CairoSurface(ptr, w, h, stream)
end

function CairoPDFSurface(filename::AbstractString, w_pts::Real, h_pts::Real)
Expand All @@ -285,7 +310,7 @@ function CairoEPSSurface(stream::T, w::Real, h::Real) where {T<:IO}
(Ptr{Nothing}, Any, Float64, Float64), callback, stream, w, h)
ccall((:cairo_ps_surface_set_eps,_jl_libcairo), Nothing,
(Ptr{Nothing},Int32), ptr, 1)
CairoSurface(ptr, w, h)
CairoSurface(ptr, w, h, stream)
end

function CairoEPSSurface(filename::AbstractString, w_pts::Real, h_pts::Real)
Expand All @@ -304,7 +329,7 @@ function CairoPSSurface(stream::T, w::Real, h::Real) where {T<:IO}
(Ptr{Nothing}, Any, Float64, Float64), callback, stream, w, h)
ccall((:cairo_ps_surface_set_eps,_jl_libcairo), Nothing,
(Ptr{Nothing},Int32), ptr, 0)
CairoSurface(ptr, w, h)
CairoSurface(ptr, w, h, stream)
end

function CairoPSSurface(filename::AbstractString, w_pts::Real, h_pts::Real)
Expand Down Expand Up @@ -350,7 +375,7 @@ function CairoSVGSurface(stream::T, w::Real, h::Real) where {T<:IO}
callback = get_stream_callback(T)
ptr = ccall((:cairo_svg_surface_create_for_stream,_jl_libcairo), Ptr{Nothing},
(Ptr{Nothing}, Any, Float64, Float64), callback, stream, w, h)
CairoSurface(ptr, w, h)
CairoSurface(ptr, w, h, stream)
end

function CairoSVGSurface(filename::AbstractString, w::Real, h::Real)
Expand Down Expand Up @@ -464,7 +489,7 @@ function CairoScriptSurface(stream::IO, w::Real, h::Real)
s = CairoScript(stream)
ptr = ccall((:cairo_script_surface_create,_jl_libcairo), Ptr{Nothing},
(Ptr{Nothing},Int32,Float64,Float64),s.ptr ,CONTENT_COLOR_ALPHA, w, h)
CairoSurface(ptr, w, h)
CairoSurface(ptr, w, h, stream)
end


Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import Base.show

using Compat.Test

# Test that an the CairoSurface holds a reference to the passed IOBuffer
@testset "IOBuffer Rooting " begin
CairoSVGSurface(IOBuffer(), 200, 110)
GC.gc()
GC.gc()
end

# Image Surface
@testset "Image Surface " begin
Expand Down

0 comments on commit 268a2e9

Please sign in to comment.