Skip to content

Commit 57bd9a9

Browse files
authored
Merge pull request #27 from lobingera/pr27
added stream sync + gio infra
2 parents 986f07e + 65548f2 commit 57bd9a9

File tree

7 files changed

+160
-4
lines changed

7 files changed

+160
-4
lines changed

LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The Rsvg.jl package is licensed under the MIT "Expat" License:
22

3-
> Copyright (c) 2015: lobingera.
3+
> Copyright (c) 2015-2018: lobingera.
44
>
55
> Permission is hereby granted, free of charge, to any person obtaining
66
> a copy of this software and associated documentation files (the

deps/build.jl

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,33 @@ using Compat
44

55
@BinDeps.setup
66

7+
78
rsvg = library_dependency("rsvg", aliases = ["librsvg", "librsvg-2.2", "librsvg-2-2", "librsvg-2", "librsvg-2.so.2"])
9+
gio = library_dependency("gio", aliases = ["libgio-2.0", "libgio-2.0-0"])
10+
11+
812

913
@static if Compat.Sys.islinux() begin
10-
provides(AptGet, "librsvg2-2",rsvg)
14+
provides(AptGet, "librsvg2-2", rsvg)
15+
provides(AptGet, "libgio", gio)
1116
end
1217
end
1318

1419
@static if Compat.Sys.isapple() begin
1520
using Homebrew
1621
provides(Homebrew.HB, "librsvg", [rsvg], os=:Darwin)
22+
provides(Homebrew.HB, "libgio", [gio], os=:Darwin)
1723
end
1824
end
1925

2026
@static if Compat.Sys.iswindows() begin
2127
using WinRPM
2228
provides(WinRPM.RPM,"librsvg-2-2",rsvg,os = :Windows)
29+
provides(WinRPM.RPM,"glib2",gio,os = :Windows)
2330
end
2431
end
2532

26-
@BinDeps.install Dict(:rsvg => :librsvg)
33+
@BinDeps.install Dict([
34+
(:rsvg => :librsvg),
35+
(:gio, :libgio),
36+
])

src/Rsvg.jl

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ isfile(depsjl) ? include(depsjl) : error("Rsvg not properly ",
88
using Cairo, Compat
99

1010
include("gerror.jl")
11+
include("gio.jl")
1112
include("types.jl")
1213
include("calls.jl")
1314

src/calls.jl

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
# collection of calls
22

3+
# /**
4+
# * rsvg_handle_new_from_stream_sync:
5+
# * @input_stream: a #GInputStream
6+
# * @base_file: (allow-none): a #GFile, or %NULL
7+
# * @flags: flags from #RsvgHandleFlags
8+
# * @cancellable: (allow-none): a #GCancellable, or %NULL
9+
# * @error: (allow-none): a location to store a #GError, or %NULL
10+
# *
11+
# * Creates a new #RsvgHandle for @stream.
12+
# *
13+
# * If @cancellable is not %NULL, then the operation can be cancelled by
14+
# * triggering the cancellable object from another thread. If the
15+
# * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
16+
# * returned.
17+
# *
18+
# * Returns: a new #RsvgHandle on success, or %NULL with @error filled in
19+
# *
20+
# * Since: 2.32
21+
# */
22+
# RsvgHandle *
23+
# rsvg_handle_new_from_stream_sync (GInputStream *input_stream,
24+
# GFile *base_file,
25+
# RsvgHandleFlags flags,
26+
# GCancellable *cancellable,
27+
# GError **error)
28+
# {
29+
30+
331
"""
432
handle_get_dimensions(handle::RsvgHandle, dimension_data::RsvgDimensionData)
533
"""
@@ -66,11 +94,39 @@ end
6694
"""
6795
handle_new_from_data(data::AbstractString)
6896
"""
69-
handle_new_from_data(data::AbstractString) = handle_new_from_data(data::AbstractString,GError(0,0,0))
97+
function handle_new_from_data(data::AbstractString)
98+
99+
perr = Ref{Ptr{GError}}(C_NULL)
100+
101+
g_input_stream = Rsvg.glib_memory_input_stream_new_from_data(data)
102+
103+
ptr = ccall((:rsvg_handle_new_from_stream_sync, librsvg), Ptr{Nothing},
104+
(GInputStream,Ptr{Nothing},UInt32,Ptr{Nothing},Ptr{Ptr{GError}}),
105+
g_input_stream,C_NULL,0,C_NULL,perr)
106+
107+
108+
if ptr == C_NULL
109+
err = unsafe_load(perr[])
110+
message = unsafe_string(err.message)
111+
ccall((:g_error_free, librsvg), Cvoid, (Ptr{GError},), perr[])
112+
error(message)
113+
end
114+
115+
RsvgHandle(ptr)
116+
end
117+
70118

71119
"""
72120
handle_free(handle::RsvgHandle)
73121
"""
74122
function handle_free(handle::RsvgHandle)
75123
ccall((:rsvg_handle_free,librsvg), Nothing, (RsvgHandle,), handle)
76124
end
125+
126+
"""
127+
handle_free(handle::GInputStream)
128+
"""
129+
function handle_free(handle::GInputStream)
130+
ccall((:g_input_stream_close,libgio), Nothing,
131+
(GInputStream,Ptr{Nothing},Ptr{Nothing}), handle,C_NULL,C_NULL)
132+
end

src/gio.jl

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# g_memory_input_stream_new_from_data ()
2+
# GInputStream *
3+
# g_memory_input_stream_new_from_data (const void *data,
4+
# gssize len,
5+
# GDestroyNotify destroy);
6+
# Creates a new GMemoryInputStream with data in memory of a given size.
7+
# Parameters
8+
# data
9+
# input data.
10+
# [array length=len][element-type guint8][transfer full]
11+
# len
12+
# length of the data, may be -1 if data is a nul-terminated string
13+
# destroy
14+
# function that is called to free data , or NULL.
15+
# [nullable]
16+
# Returns
17+
# new GInputStream read from data of len bytes.
18+
19+
mutable struct GInputStream
20+
ptr::Ptr{Nothing}
21+
22+
function GInputStream(ptr::Ptr{Nothing})
23+
self = new(ptr)
24+
@compat finalizer(destroy, self)
25+
self
26+
end
27+
end
28+
29+
function destroy(handle::GInputStream)
30+
if handle.ptr == C_NULL
31+
return
32+
end
33+
handle_free(handle)
34+
handle.ptr = C_NULL
35+
nothing
36+
end
37+
38+
39+
function glib_memory_input_stream_new_from_data(data::AbstractString)
40+
ptr = ccall((:g_memory_input_stream_new_from_data, libgio), Ptr{Nothing},
41+
(Ptr{UInt8},UInt32,Ptr{Nothing}), string(data), sizeof(data),C_NULL)
42+
GInputStream(ptr)
43+
end

test/runtests.jl

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ f = tempname() * ".png"
6363
test_render_string_to_png(f);
6464
@test stat(f).size > 0
6565

66+
f = tempname() * ".png"
67+
test_render_long_string_to_png(3000,f);
68+
@test stat(f).size > 0
69+
70+
f = tempname() * ".png"
71+
test_render_long_string_to_png(60000,f);
72+
@test stat(f).size > 0
73+
6674
end
6775

6876
@testset "roundtrip, render svg to svg" begin

test/test.jl

+38
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,44 @@ function test_render_string_to_png(output::AbstractString="b.png",content_string
5656
Cairo.write_to_png(cs,output);
5757
end
5858

59+
testc0 = """
60+
c -11.06653,0 -20.16601,9.09944 -20.16601,20.16604 0,11.0665
61+
9.09948,20.166 20.16601,20.166 11.06654,0 20.16602,-9.0995
62+
20.16602,-20.166 0,-11.0666 -9.09948,-20.16604 -20.16602,-20.16604
63+
z m 0,12.00004 c 4.58126,0 8.16602,3.5847 8.16602,8.166 0,4.5812
64+
-3.58476,8.166 -8.16602,8.166 -4.58125,0 -8.16601,-3.5848
65+
-8.16601,-8.166 0,-4.5813 3.58476,-8.166 8.16601,-8.166 z
66+
"""
67+
68+
function test_render_long_string_to_png(content_length::Int,output::AbstractString="b.png")
69+
70+
sa = Array{AbstractString}(undef,2+(4*content_length))
71+
r = rand(content_length,2)
72+
73+
sa[1] = "<svg version=\"1.1\" fill=\"#" *
74+
"0088ff\" " *
75+
"height=\"520\" width=\"520\" " *
76+
">" *
77+
"<g>"
78+
79+
p = 1 # index into sa, p+=1 is post-increment
80+
81+
for k=1:content_length
82+
sa[p+=1] = @sprintf("<path id=\"i%07d\" d=\"",k)
83+
sa[p+=1] = @sprintf("M %13.6f,%13.6f",20.0 + 460.0*r[k,1],40.0 + 480.0*r[k,2])
84+
sa[p+=1] = testc0
85+
sa[p+=1] = "\"/> "
86+
end
87+
sa[p+=1] = "</g> </svg>"
88+
89+
r = Rsvg.handle_new_from_data(join(sa,'\n'));
90+
cs = Cairo.CairoImageSurface(600,600,Cairo.FORMAT_ARGB32);
91+
c = Cairo.CairoContext(cs);
92+
Rsvg.handle_render_cairo(c,r);
93+
Cairo.write_to_png(cs,output);
94+
end
95+
96+
5997
function test_roundtrip(filename_in::AbstractString,filename_out::AbstractString)
6098

6199
# file should be available

0 commit comments

Comments
 (0)