Skip to content

Commit

Permalink
Refactor CachedTeX to use a Ref instead of being mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
asinghvi17 committed Apr 22, 2024
1 parent 14dad9c commit 503066d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ext/MakieTeXCairoMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function draw_tex(scene, screen::CairoMakie.Screen, cachedtex::MakieTeX.CachedTe
# and PNG, especially when rotated.
if !(MakieTeX.RENDER_EXTRASAFE[])
# retrieve a new Poppler document pointer
document = MakieTeX.update_pointer!(cachedtex)
document = MakieTeX.update_handle!(cachedtex)
# retrieve the first page
page = ccall(
(:poppler_document_get_page, Poppler_jll.libpoppler_glib),
Expand Down Expand Up @@ -198,7 +198,7 @@ function CairoMakie.draw_marker(ctx, marker::MakieTeX.CachedPDF, pos, scale,
# and PNG, especially when rotated.
if !(MakieTeX.RENDER_EXTRASAFE[])
# retrieve a new Poppler document pointer
document = MakieTeX.update_pointer!(marker)
document = MakieTeX.update_handle!(marker)
# retrieve the first page
page = ccall(
(:poppler_document_get_page, Poppler_jll.libpoppler_glib),
Expand Down
44 changes: 25 additions & 19 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,9 @@ Available keyword arguments are:
texdoc(contents; kwargs...) = TeXDocument(contents, true; kwargs...)



mutable struct CachedTeX <: AbstractCachedDocument
"The original `TeXDocument` which is compiled."
doc::Union{TeXDocument, Nothing}
"The resulting compiled PDF"
pdf::Vector{UInt8}
"A pointer to the Poppler handle of the PDF. May be randomly GC'ed by Poppler."
ptr::Ptr{Cvoid} # Poppler handle
"A surface to which Poppler has drawn the PDF. Permanent and cached."
surf::CairoSurface
"The dimensions of the PDF page, for ease of access."
dims::Tuple{Float64, Float64}
end

#=
# Cached documents
=#
"""
CachedPDF(pdf::PDFDocument)
Expand Down Expand Up @@ -356,6 +345,23 @@ function CachedSVG(svg::SVGDocument, rsvg_handle::Rsvg.RsvgHandle, dims::Tuple{F
return CachedSVG(svg, Ref(rsvg_handle), dims, surf, Ref{Tuple{Matrix{ARGB32}, Float64}}((Matrix{ARGB32}(undef, 0, 0), 0)))
end
CachedSVG(svg::String) = CachedSVG(SVGDocument(svg))
getdoc(doc::CachedSVG) = getdoc(doc.doc)
mimetype(::CachedSVG) = MIME"image/svg+xml"()



struct CachedTeX <: AbstractCachedDocument
"The original `TeXDocument` which is compiled."
doc::Union{TeXDocument, Nothing}
"The resulting compiled PDF"
pdf::Vector{UInt8}
"A pointer to the Poppler handle of the PDF. May be randomly GC'ed by Poppler."
ptr::Ref{Ptr{Cvoid}} # Poppler handle
"A surface to which Poppler has drawn the PDF. Permanent and cached."
surf::CairoSurface
"The dimensions of the PDF page, for ease of access."
dims::Tuple{Float64, Float64}
end

"""
CachedTeX(doc::TeXDocument; kwargs...)
Expand Down Expand Up @@ -391,7 +397,7 @@ function CachedTeX(doc::TeXDocument; kwargs...)
ct = CachedTeX(
doc,
pdf,
ptr,
Ref(ptr),
surf,
dims# .+ (1, 1),
)
Expand Down Expand Up @@ -420,7 +426,7 @@ function CachedTeX(pdf::Vector{UInt8}; kwargs...)
ct = CachedTeX(
nothing,
pdf,
ptr,
Ref(ptr),
surf,
dims# .+ (1, 1),
)
Expand All @@ -430,9 +436,9 @@ end
# do not rerun the pipeline on CachedTeX
CachedTeX(ct::CachedTeX) = ct

function update_pointer!(ct::CachedTeX)
ct.ptr = load_pdf(ct.pdf)
return ct.ptr
function update_handle!(ct::CachedTeX)
ct.ptr[] = load_pdf(ct.pdf)
return ct.ptr[]
end

function Base.show(io::IO, ct::CachedTeX)
Expand Down

0 comments on commit 503066d

Please sign in to comment.