From e929180aa6c7b475b934f7946b8837d5ce24b3fc Mon Sep 17 00:00:00 2001 From: Andreas Lobinger Date: Mon, 27 Aug 2018 20:03:09 +0200 Subject: [PATCH] CairoScript reduced to stream only (#251) --- src/Cairo.jl | 29 +++++++----------------- test/runtests.jl | 58 ++++++++++++++---------------------------------- 2 files changed, 25 insertions(+), 62 deletions(-) diff --git a/src/Cairo.jl b/src/Cairo.jl index aae2c92..8a4ce5b 100644 --- a/src/Cairo.jl +++ b/src/Cairo.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index d835e7a..7e5cc6c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -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