From 268a2e98a2912baddcb68beed233d8ad3e712bd6 Mon Sep 17 00:00:00 2001 From: Tobias Knopp Date: Wed, 22 Aug 2018 19:44:00 +0200 Subject: [PATCH] Redesign the CairoSurface type (#249) * redesign the CairoSurface type * add a test --- src/Cairo.jl | 49 ++++++++++++++++++++++++++++++++++++------------ test/runtests.jl | 6 ++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/Cairo.jl b/src/Cairo.jl index e5a9ceb..aae2c92 100644 --- a/src/Cairo.jl +++ b/src/Cairo.jl @@ -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), @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index cbb908f..d835e7a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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