Skip to content

Commit

Permalink
get svg working
Browse files Browse the repository at this point in the history
  • Loading branch information
asinghvi17 committed Apr 9, 2024
1 parent 0537e32 commit ac2d58b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/rendering/svg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ function CachedSVG(svg::SVGDocument)
handle = svg2rsvg(svg.svg)
surf, ctx = rsvg2recordsurf(handle)
dh = Rsvg.handle_get_dimensions(handle)
dims = round.(Int, (dh.width, dh.height))
return CachedSVG(svg, Ref(handle), dims, surf)
dims = Float64.((dh.width, dh.height))
return CachedSVG(svg, handle, dims, surf)
end

function rasterize(ct::CachedSVG, scale::Real = 1)
if last(ct.image_cache[]) == scale
return first(ct.image_cache[])
else
img = rsvg2img(ct.handle[], scale)
ct.image_cache[] = (img, scale)
return img
end
end

function svg2rsvg(svg::String, dpi = 72.0)
Expand Down
22 changes: 15 additions & 7 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,30 @@ function CachedPDF(pdf::PDFDocument, poppler_handle::Ptr{Cvoid}, dims::Tuple{Flo
return CachedPDF(pdf, Ref(poppler_handle), dims, surf, Ref{Tuple{Matrix{ARGB32}, Float64}}((Matrix{ARGB32}(undef, 0, 0), 0)))
end

function CachedPDF(pdf::PDFDocument, page::Int = 0)
pdf = Vector{UInt8}(pdf.pdf)
ptr = load_pdf(pdf)
surf = page2recordsurf(ptr, page)
dims = pdf_get_page_size(ptr, page)

return CachedPDF(pdf, Ref(ptr), dims, surf)
end

struct CachedSVG <: AbstractCachedDocument
"The original `SVGDocument` which is cached here, i.e., the text of that SVG."
svg::SVGDocument
"A pointer to the Rsvg handle of the SVG. May be randomly GC'ed by Rsvg, so is stored as a `Ref` in case it has to be refreshed."
handle::Ref{Rsvg.RsvgHandle}
"The dimensions of the SVG in points, for ease of access."
dims::Tuple{Float64, Float64}
"A Cairo surface to which Rsvg has drawn the SVG. Permanent and cached."
surf::CairoSurface
"A cache for a (rendered_image, scale_factor) pair. This is used to avoid re-rendering the PDF."
image_cache::Ref{Tuple{Matrix{ARGB32}, Float64}}
end

Makie.convert_attribute(x::AbstractCachedDocument, ::Makie.key"marker") = x
Makie.convert_attribute(x::AbstractCachedDocument, ::Makie.key"marker", ::Makie.key"scatter") = x
Makie.to_spritemarker(x::AbstractCachedDocument) = x

function CachedSVG(svg::SVGDocument, rsvg_handle::Rsvg.RsvgHandle, dims::Tuple{Float64, Float64}, surf::CairoSurface)
return CachedSVG(svg, Ref(rsvg_handle), dims, surf, Ref{Tuple{Matrix{ARGB32}, Float64}}((Matrix{ARGB32}(undef, 0, 0), 0)))
end


"""
CachedTeX(doc::TeXDocument; kwargs...)
Expand Down

0 comments on commit ac2d58b

Please sign in to comment.