Skip to content

Commit

Permalink
Implement PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
asinghvi17 committed Apr 13, 2024
1 parent 841982c commit 8dea568
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
44 changes: 43 additions & 1 deletion ext/MakieTeXCairoMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function rsvg_handle_set_stylesheet(handle::RsvgHandle, style_string::String)
end

function CairoMakie.draw_marker(ctx, marker::MakieTeX.CachedSVG, pos, scale,
strokecolor #= unused =#, strokewidth #= unused =#,
strokecolor #= used =#, strokewidth #= used =#,
marker_offset, rotation)
Cairo.save(ctx)
# Obtain the initial color from the pattern.
Expand Down Expand Up @@ -180,5 +180,47 @@ function CairoMakie.draw_marker(ctx, marker::MakieTeX.CachedSVG, pos, scale,
end


function CairoMakie.draw_marker(ctx, marker::MakieTeX.CachedPDF, pos, scale,
strokecolor #= unused =#, strokewidth #= unused =#,
marker_offset, rotation)
Cairo.save(ctx)
Cairo.translate(ctx,
pos[1] #= the initial marker position =# + marker_offset[1] #= the marker offset =# - scale[1]#= center of the marker =#,
pos[2] #= the initial marker position =# + marker_offset[2] #= the marker offset =# - scale[2]#= center of the marker =#,)
Cairo.rotate(ctx, CairoMakie.to_2d_rotation(rotation))
Cairo.scale(
ctx,
scale[1] / w,
scale[2] / h
)
# the rendering pipeline
# first is the "safe" Poppler pipeline, with better results in PDF
# and PNG, especially when rotated.
if !(MakieTeX.RENDER_EXTRASAFE[])
# retrieve a new Poppler document pointer
document = MakieTeX.update_pointer!(marker)
# retrieve the first page
page = ccall(
(:poppler_document_get_page, Poppler_jll.libpoppler_glib),
Ptr{Cvoid},
(Ptr{Cvoid}, Cint),
document, marker.doc.page # page 0 is first page
)
# Render the page to the surface
ccall(
(:poppler_page_render, Poppler_jll.libpoppler_glib),
Cvoid,
(Ptr{Cvoid}, Ptr{Cvoid}),
page, ctx.ptr
)
else # "extra-safe" Cairo pipeline, also somewhat faster.
# render the cached CairoSurface to the screen.
# bad with PNG output though.
Cairo.set_source(ctx, marker.surf, 0, 0)
Cairo.paint(ctx)
end
# restore context and end
Cairo.restore(ctx)
end

end
2 changes: 1 addition & 1 deletion src/rendering/pdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function rasterize(pdf::CachedPDF, scale::Real = 1)
pdf.image_cache[] = (img, scale)
return img
end

end

# Pure poppler pipeline - directly from PDF to Cairo surface.

"""
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/tex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# TeX rendering
=#

function render(ct::CachedTeX, scale::Float64 = 1)
function rasterize(ct::CachedTeX, scale::Float64 = 1)
end

# The main compilation method - compiles arbitrary LaTeX documents
Expand Down
30 changes: 28 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Is converted to [`CachedPDF`](@ref) for use in plotting.
"""
struct PDFDocument <: AbstractDocument
doc::String
page::Union{Nothing, Int}
page::Int
end
PDFDocument(doc::String) = PDFDocument(doc, 0)
Cached(x::PDFDocument) = CachedPDF(x)
Expand All @@ -84,7 +84,9 @@ Is converted to [`CachedPDF`](@ref) for use in plotting.
"""
struct EPSDocument <: AbstractDocument
doc::String
page::Int
end
EPSDocument(doc::String, page::Int = 0) = EPSDocument(doc, page)
Cached(x::EPSDocument) = CachedPDF(x)


Expand Down Expand Up @@ -178,14 +180,21 @@ end
"""
CachedPDF(pdf::PDFDocument)
Holds a PDF document along with a Poppler handle and a Cairo surface to which it has already
been rendered.
## Usage
```julia
CachedPDF(read("path/to/pdf.pdf"), [page = 0])
CachedPDF(read("path/to/pdf.pdf", String), [page = 0])
CachedPDF(PDFDocument(...), [page = 0])
```
## Fields
$(FIELDS)
"""
struct CachedPDF <: AbstractCachedDocument
"A reference to the `PDFDocument` which is cached here."
Expand All @@ -206,7 +215,24 @@ end
CachedPDF(pdf::String) = CachedPDF(PDFDocument(pdf))


"""
CachedSVG(svg::SVGDocument)
Holds an SVG document along with an Rsvg handle and a Cairo surface to which it has already
been rendered.
## Usage
```julia
CachedSVG(read("path/to/svg.svg"))
CachedSVG(read("path/to/svg.svg", String))
CachedSVG(SVGDocument(...))
```
## Fields
$(FIELDS)
"""
struct CachedSVG <: AbstractCachedDocument
"The original `SVGDocument` which is cached here, i.e., the text of that SVG."
doc::SVGDocument
Expand Down

0 comments on commit 8dea568

Please sign in to comment.