Skip to content

Commit

Permalink
CairoScript reduced to stream only (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
lobingera authored Aug 27, 2018
1 parent 268a2e9 commit e929180
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 62 deletions.
29 changes: 8 additions & 21 deletions src/Cairo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -439,24 +439,17 @@ function format_stride_for_width(format::Integer, width::Integer)
end


## Scripting
## Scripting (only target IO stream)

mutable struct CairoScript <: GraphicsDevice
ptr::Ptr{Nothing}

function CairoScript(filename::AbstractString)
ptr = ccall((:cairo_script_create,_jl_libcairo),
Ptr{Nothing}, (Ptr{UInt8},), String(filename))
self = new(ptr)
@compat finalizer(destroy, self)
self
end
stream::IO

function CairoScript(stream::T) where {T<:IO}
callback = get_stream_callback(T)
ptr = ccall((:cairo_script_create_for_stream,_jl_libcairo), Ptr{Nothing},
(Ptr{Nothing}, Any), callback, stream)
self = new(ptr)
self = new(ptr,stream)
@compat finalizer(destroy, self)
self
end
Expand All @@ -471,26 +464,20 @@ function destroy(s::CairoScript)
nothing
end

function CairoScriptSurface(filename::AbstractString, w::Real, h::Real)
s = CairoScript(filename)
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

function CairoScriptSurface(filename::AbstractString,sc::CairoSurface)
s = CairoScript(filename)
function CairoScriptSurface(stream::IO, sc::CairoSurface)
s = CairoScript(stream)
ptr = ccall((:cairo_script_surface_create_for_target,_jl_libcairo), Ptr{Nothing},
(Ptr{Nothing},Ptr{Nothing}),s.ptr, sc.ptr)
CairoSurface(ptr, sc.width, sc.height)
end

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, stream)
end


mutable struct CairoRectangle
Expand Down
58 changes: 17 additions & 41 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,26 +214,7 @@ end

if Cairo.libcairo_version >= v"1.12.0"

# FixMe! I'm not sure has anything to do with Windows. See comment below.
if !Sys.iswindows()

output_file_name = "a.cs"
surf = CairoScriptSurface(output_file_name,512,512)
hdraw(surf,64,8,4)
# FixMe! The use of gc here is not a real solution but makes the tests pass for now. The issue here is
# that Cairo only writes to the file when the CairoScript handle is finalized. Either Cairo.jl should
# stop supporting this API or handle should be explicitly closed instead of using finalizers.
destroy(surf);GC.gc()

@test isfile(output_file_name)

str_data = read(output_file_name)
@test length(str_data) > 3000
@test str_data[1:10] == [0x25,0x21,0x43,0x61,0x69,0x72,0x6f,0x53,0x63,0x72]
rm(output_file_name)

end

# just write to surface and test for content
io = IOBuffer()
surf = CairoScriptSurface(io,512,512)
hdraw(surf,64,8,4)
Expand All @@ -244,32 +225,27 @@ end

@test length(str_data) > 3000 && str_data[1:10] == [0x25,0x21,0x43,0x61,0x69,0x72,0x6f,0x53,0x63,0x72]

# FixMe! I'm not sure has anything to do with Windows. See comment below.
if !Sys.iswindows()
# _create_for_target
z = zeros(UInt32,512,512);
surf = CairoImageSurface(z, Cairo.FORMAT_ARGB32)
# create_for_target
z = zeros(UInt32,512,512);
surf = CairoImageSurface(z, Cairo.FORMAT_ARGB32)

output_file_name = "a.cs"
scsurf = CairoScriptSurface(output_file_name,surf)
hdraw(scsurf,64,8,8)
finish(surf)
# FixMe! The use of gc here is not a real solution but makes the tests pass for now. The issue here is
# that Cairo only writes to the file when the CairoScript handle is finalized. Either Cairo.jl should
# stop supporting this API or handle should be explicitly closed instead of using finalizers.
destroy(scsurf);GC.gc()
@test isfile(output_file_name)
io = IOBuffer()
scsurf = CairoScriptSurface(io,surf)
hdraw(scsurf,64,8,8)
finish(surf)
destroy(scsurf)

str_data = read(output_file_name)
@test length(str_data) > 3000 && str_data[1:10] == [0x25,0x21,0x43,0x61,0x69,0x72,0x6f,0x53,0x63,0x72]
rm(output_file_name)
seek(io,0)
str_data = Vector{UInt8}(read(io))

@test length(str_data) > 3000 && str_data[1:10] == [0x25,0x21,0x43,0x61,0x69,0x72,0x6f,0x53,0x63,0x72]

d = simple_hist(surf.data)
d = simple_hist(surf.data)

@test length(d) == 1
@test collect(keys(d))[1] == 0x80000080
@test length(d) == 1
@test collect(keys(d))[1] == 0x80000080

end

end
end

Expand Down

0 comments on commit e929180

Please sign in to comment.